xref: /libev/event.c (revision 93823e6c)
1e3a38431SPaul Bohm /*
2e3a38431SPaul Bohm  * libevent compatibility layer
3e3a38431SPaul Bohm  *
4*93823e6cSPaul Bohm  * Copyright (c) 2007,2008,2009,2010,2012 Marc Alexander Lehmann <[email protected]>
5e3a38431SPaul Bohm  * All rights reserved.
6e3a38431SPaul Bohm  *
7e3a38431SPaul Bohm  * Redistribution and use in source and binary forms, with or without modifica-
8e3a38431SPaul Bohm  * tion, are permitted provided that the following conditions are met:
9e3a38431SPaul Bohm  *
10e3a38431SPaul Bohm  *   1.  Redistributions of source code must retain the above copyright notice,
11e3a38431SPaul Bohm  *       this list of conditions and the following disclaimer.
12e3a38431SPaul Bohm  *
13e3a38431SPaul Bohm  *   2.  Redistributions in binary form must reproduce the above copyright
14e3a38431SPaul Bohm  *       notice, this list of conditions and the following disclaimer in the
15e3a38431SPaul Bohm  *       documentation and/or other materials provided with the distribution.
16e3a38431SPaul Bohm  *
17e3a38431SPaul Bohm  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18e3a38431SPaul Bohm  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
19e3a38431SPaul Bohm  * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
20e3a38431SPaul Bohm  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
21e3a38431SPaul Bohm  * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22e3a38431SPaul Bohm  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23e3a38431SPaul Bohm  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24e3a38431SPaul Bohm  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
25e3a38431SPaul Bohm  * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26e3a38431SPaul Bohm  * OF THE POSSIBILITY OF SUCH DAMAGE.
27e3a38431SPaul Bohm  *
28e3a38431SPaul Bohm  * Alternatively, the contents of this file may be used under the terms of
29e3a38431SPaul Bohm  * the GNU General Public License ("GPL") version 2 or any later version,
30e3a38431SPaul Bohm  * in which case the provisions of the GPL are applicable instead of
31e3a38431SPaul Bohm  * the above. If you wish to allow the use of your version of this file
32e3a38431SPaul Bohm  * only under the terms of the GPL and not to allow others to use your
33e3a38431SPaul Bohm  * version of this file under the BSD license, indicate your decision
34e3a38431SPaul Bohm  * by deleting the provisions above and replace them with the notice
35e3a38431SPaul Bohm  * and other provisions required by the GPL. If you do not delete the
36e3a38431SPaul Bohm  * provisions above, a recipient may use your version of this file under
37e3a38431SPaul Bohm  * either the BSD or the GPL.
38e3a38431SPaul Bohm  */
39e3a38431SPaul Bohm 
40e3a38431SPaul Bohm #include <stddef.h>
41e3a38431SPaul Bohm #include <stdlib.h>
42e3a38431SPaul Bohm #include <assert.h>
43e3a38431SPaul Bohm 
44e3a38431SPaul Bohm #ifdef EV_EVENT_H
45e3a38431SPaul Bohm # include EV_EVENT_H
46e3a38431SPaul Bohm #else
47e3a38431SPaul Bohm # include "event.h"
48e3a38431SPaul Bohm #endif
49e3a38431SPaul Bohm 
50e3a38431SPaul Bohm #if EV_MULTIPLICITY
51e3a38431SPaul Bohm # define dLOOPev struct ev_loop *loop = (struct ev_loop *)ev->ev_base
52e3a38431SPaul Bohm # define dLOOPbase struct ev_loop *loop = (struct ev_loop *)base
53e3a38431SPaul Bohm #else
54e3a38431SPaul Bohm # define dLOOPev
55e3a38431SPaul Bohm # define dLOOPbase
56e3a38431SPaul Bohm #endif
57e3a38431SPaul Bohm 
58e3a38431SPaul Bohm /* never accessed, will always be cast from/to ev_loop */
59e3a38431SPaul Bohm struct event_base
60e3a38431SPaul Bohm {
61e3a38431SPaul Bohm   int dummy;
62e3a38431SPaul Bohm };
63e3a38431SPaul Bohm 
64e3a38431SPaul Bohm static struct event_base *ev_x_cur;
65e3a38431SPaul Bohm 
66e3a38431SPaul Bohm static ev_tstamp
ev_tv_get(struct timeval * tv)67e3a38431SPaul Bohm ev_tv_get (struct timeval *tv)
68e3a38431SPaul Bohm {
69e3a38431SPaul Bohm   if (tv)
70e3a38431SPaul Bohm     {
71e3a38431SPaul Bohm       ev_tstamp after = tv->tv_sec + tv->tv_usec * 1e-6;
72e3a38431SPaul Bohm       return after ? after : 1e-6;
73e3a38431SPaul Bohm     }
74e3a38431SPaul Bohm   else
75e3a38431SPaul Bohm     return -1.;
76e3a38431SPaul Bohm }
77e3a38431SPaul Bohm 
78e3a38431SPaul Bohm #define EVENT_STRINGIFY(s) # s
79e3a38431SPaul Bohm #define EVENT_VERSION(a,b) EVENT_STRINGIFY (a) "." EVENT_STRINGIFY (b)
80e3a38431SPaul Bohm 
81*93823e6cSPaul Bohm const char *
event_get_version(void)82*93823e6cSPaul Bohm event_get_version (void)
83e3a38431SPaul Bohm {
84e3a38431SPaul Bohm   /* returns ABI, not API or library, version */
85e3a38431SPaul Bohm   return EVENT_VERSION (EV_VERSION_MAJOR, EV_VERSION_MINOR);
86e3a38431SPaul Bohm }
87e3a38431SPaul Bohm 
88*93823e6cSPaul Bohm const char *
event_get_method(void)89*93823e6cSPaul Bohm event_get_method (void)
90e3a38431SPaul Bohm {
91e3a38431SPaul Bohm   return "libev";
92e3a38431SPaul Bohm }
93e3a38431SPaul Bohm 
event_init(void)94e3a38431SPaul Bohm void *event_init (void)
95e3a38431SPaul Bohm {
96e3a38431SPaul Bohm #if EV_MULTIPLICITY
97e3a38431SPaul Bohm   if (ev_x_cur)
98e3a38431SPaul Bohm     ev_x_cur = (struct event_base *)ev_loop_new (EVFLAG_AUTO);
99e3a38431SPaul Bohm   else
100e3a38431SPaul Bohm     ev_x_cur = (struct event_base *)ev_default_loop (EVFLAG_AUTO);
101e3a38431SPaul Bohm #else
102e3a38431SPaul Bohm   assert (("libev: multiple event bases not supported when not compiled with EV_MULTIPLICITY", !ev_x_cur));
103e3a38431SPaul Bohm 
104e3a38431SPaul Bohm   ev_x_cur = (struct event_base *)(long)ev_default_loop (EVFLAG_AUTO);
105e3a38431SPaul Bohm #endif
106e3a38431SPaul Bohm 
107e3a38431SPaul Bohm   return ev_x_cur;
108e3a38431SPaul Bohm }
109e3a38431SPaul Bohm 
110*93823e6cSPaul Bohm const char *
event_base_get_method(const struct event_base * base)111*93823e6cSPaul Bohm event_base_get_method (const struct event_base *base)
112*93823e6cSPaul Bohm {
113*93823e6cSPaul Bohm   return "libev";
114*93823e6cSPaul Bohm }
115*93823e6cSPaul Bohm 
116*93823e6cSPaul Bohm struct event_base *
event_base_new(void)117*93823e6cSPaul Bohm event_base_new (void)
118*93823e6cSPaul Bohm {
119*93823e6cSPaul Bohm #if EV_MULTIPLICITY
120*93823e6cSPaul Bohm   return (struct event_base *)ev_loop_new (EVFLAG_AUTO);
121*93823e6cSPaul Bohm #else
122*93823e6cSPaul Bohm   assert (("libev: multiple event bases not supported when not compiled with EV_MULTIPLICITY"));
123*93823e6cSPaul Bohm   return NULL;
124*93823e6cSPaul Bohm #endif
125*93823e6cSPaul Bohm }
126*93823e6cSPaul Bohm 
event_base_free(struct event_base * base)127e3a38431SPaul Bohm void event_base_free (struct event_base *base)
128e3a38431SPaul Bohm {
129e3a38431SPaul Bohm   dLOOPbase;
130e3a38431SPaul Bohm 
131e3a38431SPaul Bohm #if EV_MULTIPLICITY
132e3a38431SPaul Bohm   if (!ev_is_default_loop (loop))
133e3a38431SPaul Bohm     ev_loop_destroy (loop);
134e3a38431SPaul Bohm #endif
135e3a38431SPaul Bohm }
136e3a38431SPaul Bohm 
event_dispatch(void)137e3a38431SPaul Bohm int event_dispatch (void)
138e3a38431SPaul Bohm {
139e3a38431SPaul Bohm   return event_base_dispatch (ev_x_cur);
140e3a38431SPaul Bohm }
141e3a38431SPaul Bohm 
142e3a38431SPaul Bohm #ifdef EV_STANDALONE
event_set_log_callback(event_log_cb cb)143e3a38431SPaul Bohm void event_set_log_callback (event_log_cb cb)
144e3a38431SPaul Bohm {
145e3a38431SPaul Bohm   /* nop */
146e3a38431SPaul Bohm }
147e3a38431SPaul Bohm #endif
148e3a38431SPaul Bohm 
event_loop(int flags)149e3a38431SPaul Bohm int event_loop (int flags)
150e3a38431SPaul Bohm {
151e3a38431SPaul Bohm   return event_base_loop (ev_x_cur, flags);
152e3a38431SPaul Bohm }
153e3a38431SPaul Bohm 
event_loopexit(struct timeval * tv)154e3a38431SPaul Bohm int event_loopexit (struct timeval *tv)
155e3a38431SPaul Bohm {
156e3a38431SPaul Bohm   return event_base_loopexit (ev_x_cur, tv);
157e3a38431SPaul Bohm }
158e3a38431SPaul Bohm 
event_get_callback(const struct event * ev)159*93823e6cSPaul Bohm event_callback_fn event_get_callback
160*93823e6cSPaul Bohm (const struct event *ev)
161*93823e6cSPaul Bohm {
162*93823e6cSPaul Bohm   return ev->ev_callback;
163*93823e6cSPaul Bohm }
164*93823e6cSPaul Bohm 
165e3a38431SPaul Bohm static void
ev_x_cb(struct event * ev,int revents)166e3a38431SPaul Bohm ev_x_cb (struct event *ev, int revents)
167e3a38431SPaul Bohm {
168e3a38431SPaul Bohm   revents &= EV_READ | EV_WRITE | EV_TIMER | EV_SIGNAL;
169e3a38431SPaul Bohm 
170e3a38431SPaul Bohm   ev->ev_res = revents;
171e3a38431SPaul Bohm   ev->ev_callback (ev->ev_fd, (short)revents, ev->ev_arg);
172e3a38431SPaul Bohm }
173e3a38431SPaul Bohm 
174e3a38431SPaul Bohm static void
ev_x_cb_sig(EV_P_ struct ev_signal * w,int revents)175e3a38431SPaul Bohm ev_x_cb_sig (EV_P_ struct ev_signal *w, int revents)
176e3a38431SPaul Bohm {
177e3a38431SPaul Bohm   struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.sig));
178e3a38431SPaul Bohm 
179e3a38431SPaul Bohm   if (revents & EV_ERROR)
180e3a38431SPaul Bohm     event_del (ev);
181e3a38431SPaul Bohm 
182e3a38431SPaul Bohm   ev_x_cb (ev, revents);
183e3a38431SPaul Bohm }
184e3a38431SPaul Bohm 
185e3a38431SPaul Bohm static void
ev_x_cb_io(EV_P_ struct ev_io * w,int revents)186e3a38431SPaul Bohm ev_x_cb_io (EV_P_ struct ev_io *w, int revents)
187e3a38431SPaul Bohm {
188e3a38431SPaul Bohm   struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io));
189e3a38431SPaul Bohm 
190e3a38431SPaul Bohm   if ((revents & EV_ERROR) || !(ev->ev_events & EV_PERSIST))
191e3a38431SPaul Bohm     event_del (ev);
192e3a38431SPaul Bohm 
193e3a38431SPaul Bohm   ev_x_cb (ev, revents);
194e3a38431SPaul Bohm }
195e3a38431SPaul Bohm 
196e3a38431SPaul Bohm static void
ev_x_cb_to(EV_P_ struct ev_timer * w,int revents)197e3a38431SPaul Bohm ev_x_cb_to (EV_P_ struct ev_timer *w, int revents)
198e3a38431SPaul Bohm {
199e3a38431SPaul Bohm   struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to));
200e3a38431SPaul Bohm 
201e3a38431SPaul Bohm   event_del (ev);
202e3a38431SPaul Bohm 
203e3a38431SPaul Bohm   ev_x_cb (ev, revents);
204e3a38431SPaul Bohm }
205e3a38431SPaul Bohm 
event_set(struct event * ev,int fd,short events,void (* cb)(int,short,void *),void * arg)206e3a38431SPaul Bohm void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg)
207e3a38431SPaul Bohm {
208e3a38431SPaul Bohm   if (events & EV_SIGNAL)
209e3a38431SPaul Bohm     ev_init (&ev->iosig.sig, ev_x_cb_sig);
210e3a38431SPaul Bohm   else
211e3a38431SPaul Bohm     ev_init (&ev->iosig.io, ev_x_cb_io);
212e3a38431SPaul Bohm 
213e3a38431SPaul Bohm   ev_init (&ev->to, ev_x_cb_to);
214e3a38431SPaul Bohm 
215e3a38431SPaul Bohm   ev->ev_base     = ev_x_cur; /* not threadsafe, but it's how libevent works */
216e3a38431SPaul Bohm   ev->ev_fd       = fd;
217e3a38431SPaul Bohm   ev->ev_events   = events;
218e3a38431SPaul Bohm   ev->ev_pri      = 0;
219e3a38431SPaul Bohm   ev->ev_callback = cb;
220e3a38431SPaul Bohm   ev->ev_arg      = arg;
221e3a38431SPaul Bohm   ev->ev_res      = 0;
222e3a38431SPaul Bohm   ev->ev_flags    = EVLIST_INIT;
223e3a38431SPaul Bohm }
224e3a38431SPaul Bohm 
event_once(int fd,short events,void (* cb)(int,short,void *),void * arg,struct timeval * tv)225e3a38431SPaul Bohm int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
226e3a38431SPaul Bohm {
227e3a38431SPaul Bohm   return event_base_once (ev_x_cur, fd, events, cb, arg, tv);
228e3a38431SPaul Bohm }
229e3a38431SPaul Bohm 
event_add(struct event * ev,struct timeval * tv)230e3a38431SPaul Bohm int event_add (struct event *ev, struct timeval *tv)
231e3a38431SPaul Bohm {
232e3a38431SPaul Bohm   dLOOPev;
233e3a38431SPaul Bohm 
234e3a38431SPaul Bohm   if (ev->ev_events & EV_SIGNAL)
235e3a38431SPaul Bohm     {
236e3a38431SPaul Bohm       if (!ev_is_active (&ev->iosig.sig))
237e3a38431SPaul Bohm         {
238e3a38431SPaul Bohm           ev_signal_set (&ev->iosig.sig, ev->ev_fd);
239e3a38431SPaul Bohm           ev_signal_start (EV_A_ &ev->iosig.sig);
240e3a38431SPaul Bohm 
241e3a38431SPaul Bohm           ev->ev_flags |= EVLIST_SIGNAL;
242e3a38431SPaul Bohm         }
243e3a38431SPaul Bohm     }
244e3a38431SPaul Bohm   else if (ev->ev_events & (EV_READ | EV_WRITE))
245e3a38431SPaul Bohm     {
246e3a38431SPaul Bohm       if (!ev_is_active (&ev->iosig.io))
247e3a38431SPaul Bohm         {
248e3a38431SPaul Bohm           ev_io_set (&ev->iosig.io, ev->ev_fd, ev->ev_events & (EV_READ | EV_WRITE));
249e3a38431SPaul Bohm           ev_io_start (EV_A_ &ev->iosig.io);
250e3a38431SPaul Bohm 
251e3a38431SPaul Bohm           ev->ev_flags |= EVLIST_INSERTED;
252e3a38431SPaul Bohm         }
253e3a38431SPaul Bohm     }
254e3a38431SPaul Bohm 
255e3a38431SPaul Bohm   if (tv)
256e3a38431SPaul Bohm     {
257e3a38431SPaul Bohm       ev->to.repeat = ev_tv_get (tv);
258e3a38431SPaul Bohm       ev_timer_again (EV_A_ &ev->to);
259e3a38431SPaul Bohm       ev->ev_flags |= EVLIST_TIMEOUT;
260e3a38431SPaul Bohm     }
261e3a38431SPaul Bohm   else
262e3a38431SPaul Bohm     {
263e3a38431SPaul Bohm       ev_timer_stop (EV_A_ &ev->to);
264e3a38431SPaul Bohm       ev->ev_flags &= ~EVLIST_TIMEOUT;
265e3a38431SPaul Bohm     }
266e3a38431SPaul Bohm 
267e3a38431SPaul Bohm   ev->ev_flags |= EVLIST_ACTIVE;
268e3a38431SPaul Bohm 
269e3a38431SPaul Bohm   return 0;
270e3a38431SPaul Bohm }
271e3a38431SPaul Bohm 
event_del(struct event * ev)272e3a38431SPaul Bohm int event_del (struct event *ev)
273e3a38431SPaul Bohm {
274e3a38431SPaul Bohm   dLOOPev;
275e3a38431SPaul Bohm 
276e3a38431SPaul Bohm   if (ev->ev_events & EV_SIGNAL)
277e3a38431SPaul Bohm     ev_signal_stop (EV_A_ &ev->iosig.sig);
278e3a38431SPaul Bohm   else if (ev->ev_events & (EV_READ | EV_WRITE))
279e3a38431SPaul Bohm     ev_io_stop (EV_A_ &ev->iosig.io);
280e3a38431SPaul Bohm 
281e3a38431SPaul Bohm   if (ev_is_active (&ev->to))
282e3a38431SPaul Bohm     ev_timer_stop (EV_A_ &ev->to);
283e3a38431SPaul Bohm 
284e3a38431SPaul Bohm   ev->ev_flags = EVLIST_INIT;
285e3a38431SPaul Bohm 
286e3a38431SPaul Bohm   return 0;
287e3a38431SPaul Bohm }
288e3a38431SPaul Bohm 
event_active(struct event * ev,int res,short ncalls)289e3a38431SPaul Bohm void event_active (struct event *ev, int res, short ncalls)
290e3a38431SPaul Bohm {
291e3a38431SPaul Bohm   dLOOPev;
292e3a38431SPaul Bohm 
293e3a38431SPaul Bohm   if (res & EV_TIMEOUT)
294e3a38431SPaul Bohm     ev_feed_event (EV_A_ &ev->to, res & EV_TIMEOUT);
295e3a38431SPaul Bohm 
296e3a38431SPaul Bohm   if (res & EV_SIGNAL)
297e3a38431SPaul Bohm     ev_feed_event (EV_A_ &ev->iosig.sig, res & EV_SIGNAL);
298e3a38431SPaul Bohm 
299e3a38431SPaul Bohm   if (res & (EV_READ | EV_WRITE))
300e3a38431SPaul Bohm     ev_feed_event (EV_A_ &ev->iosig.io, res & (EV_READ | EV_WRITE));
301e3a38431SPaul Bohm }
302e3a38431SPaul Bohm 
event_pending(struct event * ev,short events,struct timeval * tv)303e3a38431SPaul Bohm int event_pending (struct event *ev, short events, struct timeval *tv)
304e3a38431SPaul Bohm {
305e3a38431SPaul Bohm   short revents = 0;
306e3a38431SPaul Bohm   dLOOPev;
307e3a38431SPaul Bohm 
308e3a38431SPaul Bohm   if (ev->ev_events & EV_SIGNAL)
309e3a38431SPaul Bohm     {
310e3a38431SPaul Bohm       /* sig */
311e3a38431SPaul Bohm       if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig))
312e3a38431SPaul Bohm         revents |= EV_SIGNAL;
313e3a38431SPaul Bohm     }
314e3a38431SPaul Bohm   else if (ev->ev_events & (EV_READ | EV_WRITE))
315e3a38431SPaul Bohm     {
316e3a38431SPaul Bohm       /* io */
317e3a38431SPaul Bohm       if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io))
318e3a38431SPaul Bohm         revents |= ev->ev_events & (EV_READ | EV_WRITE);
319e3a38431SPaul Bohm     }
320e3a38431SPaul Bohm 
321e3a38431SPaul Bohm   if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to))
322e3a38431SPaul Bohm     {
323e3a38431SPaul Bohm       revents |= EV_TIMEOUT;
324e3a38431SPaul Bohm 
325e3a38431SPaul Bohm       if (tv)
326e3a38431SPaul Bohm         {
327e3a38431SPaul Bohm           ev_tstamp at = ev_now (EV_A);
328e3a38431SPaul Bohm 
329e3a38431SPaul Bohm           tv->tv_sec  = (long)at;
330e3a38431SPaul Bohm           tv->tv_usec = (long)((at - (ev_tstamp)tv->tv_sec) * 1e6);
331e3a38431SPaul Bohm         }
332e3a38431SPaul Bohm     }
333e3a38431SPaul Bohm 
334e3a38431SPaul Bohm   return events & revents;
335e3a38431SPaul Bohm }
336e3a38431SPaul Bohm 
event_priority_init(int npri)337e3a38431SPaul Bohm int event_priority_init (int npri)
338e3a38431SPaul Bohm {
339e3a38431SPaul Bohm   return event_base_priority_init (ev_x_cur, npri);
340e3a38431SPaul Bohm }
341e3a38431SPaul Bohm 
event_priority_set(struct event * ev,int pri)342e3a38431SPaul Bohm int event_priority_set (struct event *ev, int pri)
343e3a38431SPaul Bohm {
344e3a38431SPaul Bohm   ev->ev_pri = pri;
345e3a38431SPaul Bohm 
346e3a38431SPaul Bohm   return 0;
347e3a38431SPaul Bohm }
348e3a38431SPaul Bohm 
event_base_set(struct event_base * base,struct event * ev)349e3a38431SPaul Bohm int event_base_set (struct event_base *base, struct event *ev)
350e3a38431SPaul Bohm {
351e3a38431SPaul Bohm   ev->ev_base = base;
352e3a38431SPaul Bohm 
353e3a38431SPaul Bohm   return 0;
354e3a38431SPaul Bohm }
355e3a38431SPaul Bohm 
event_base_loop(struct event_base * base,int flags)356e3a38431SPaul Bohm int event_base_loop (struct event_base *base, int flags)
357e3a38431SPaul Bohm {
358e3a38431SPaul Bohm   dLOOPbase;
359e3a38431SPaul Bohm 
360*93823e6cSPaul Bohm   return !ev_run (EV_A_ flags);
361e3a38431SPaul Bohm }
362e3a38431SPaul Bohm 
event_base_dispatch(struct event_base * base)363e3a38431SPaul Bohm int event_base_dispatch (struct event_base *base)
364e3a38431SPaul Bohm {
365e3a38431SPaul Bohm   return event_base_loop (base, 0);
366e3a38431SPaul Bohm }
367e3a38431SPaul Bohm 
368e3a38431SPaul Bohm static void
ev_x_loopexit_cb(int revents,void * base)369e3a38431SPaul Bohm ev_x_loopexit_cb (int revents, void *base)
370e3a38431SPaul Bohm {
371e3a38431SPaul Bohm   dLOOPbase;
372e3a38431SPaul Bohm 
373e3a38431SPaul Bohm   ev_break (EV_A_ EVBREAK_ONE);
374e3a38431SPaul Bohm }
375e3a38431SPaul Bohm 
event_base_loopexit(struct event_base * base,struct timeval * tv)376e3a38431SPaul Bohm int event_base_loopexit (struct event_base *base, struct timeval *tv)
377e3a38431SPaul Bohm {
378e3a38431SPaul Bohm   ev_tstamp after = ev_tv_get (tv);
379e3a38431SPaul Bohm   dLOOPbase;
380e3a38431SPaul Bohm 
381e3a38431SPaul Bohm   ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., ev_x_loopexit_cb, (void *)base);
382e3a38431SPaul Bohm 
383e3a38431SPaul Bohm   return 0;
384e3a38431SPaul Bohm }
385e3a38431SPaul Bohm 
386e3a38431SPaul Bohm struct ev_x_once
387e3a38431SPaul Bohm {
388e3a38431SPaul Bohm   int fd;
389e3a38431SPaul Bohm   void (*cb)(int, short, void *);
390e3a38431SPaul Bohm   void *arg;
391e3a38431SPaul Bohm };
392e3a38431SPaul Bohm 
393e3a38431SPaul Bohm static void
ev_x_once_cb(int revents,void * arg)394e3a38431SPaul Bohm ev_x_once_cb (int revents, void *arg)
395e3a38431SPaul Bohm {
396e3a38431SPaul Bohm   struct ev_x_once *once = (struct ev_x_once *)arg;
397e3a38431SPaul Bohm 
398e3a38431SPaul Bohm   once->cb (once->fd, (short)revents, once->arg);
399e3a38431SPaul Bohm   free (once);
400e3a38431SPaul Bohm }
401e3a38431SPaul Bohm 
event_base_once(struct event_base * base,int fd,short events,void (* cb)(int,short,void *),void * arg,struct timeval * tv)402e3a38431SPaul Bohm int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
403e3a38431SPaul Bohm {
404e3a38431SPaul Bohm   struct ev_x_once *once = (struct ev_x_once *)malloc (sizeof (struct ev_x_once));
405e3a38431SPaul Bohm   dLOOPbase;
406e3a38431SPaul Bohm 
407e3a38431SPaul Bohm   if (!once)
408e3a38431SPaul Bohm     return -1;
409e3a38431SPaul Bohm 
410e3a38431SPaul Bohm   once->fd  = fd;
411e3a38431SPaul Bohm   once->cb  = cb;
412e3a38431SPaul Bohm   once->arg = arg;
413e3a38431SPaul Bohm 
414e3a38431SPaul Bohm   ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), ev_tv_get (tv), ev_x_once_cb, (void *)once);
415e3a38431SPaul Bohm 
416e3a38431SPaul Bohm   return 0;
417e3a38431SPaul Bohm }
418e3a38431SPaul Bohm 
event_base_priority_init(struct event_base * base,int npri)419e3a38431SPaul Bohm int event_base_priority_init (struct event_base *base, int npri)
420e3a38431SPaul Bohm {
421e3a38431SPaul Bohm   /*dLOOPbase;*/
422e3a38431SPaul Bohm 
423e3a38431SPaul Bohm   return 0;
424e3a38431SPaul Bohm }
425e3a38431SPaul Bohm 
426