2 * libevent compatibility layer
4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
7 * Redistribution and use in source and binary forms, with or without modifica-
8 * tion, are permitted provided that the following conditions are met:
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
19 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
21 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
25 * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26 * OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU General Public License ("GPL") version 2 or any later version,
30 * in which case the provisions of the GPL are applicable instead of
31 * the above. If you wish to allow the use of your version of this file
32 * only under the terms of the GPL and not to allow others to use your
33 * version of this file under the BSD license, indicate your decision
34 * by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL. If you do not delete the
36 * provisions above, a recipient may use your version of this file under
37 * either the BSD or the GPL.
45 # include <sys/time.h>
55 # define dLOOPev struct ev_loop *loop = (struct ev_loop *)ev->ev_base
56 # define dLOOPbase struct ev_loop *loop = (struct ev_loop *)base
62 /* never accessed, will always be cast from/to ev_loop */
68 static struct event_base *x_cur;
71 tv_set (struct timeval *tv, ev_tstamp at)
73 tv->tv_sec = (long)at;
74 tv->tv_usec = (long)((at - (ev_tstamp)tv->tv_sec) * 1e6);
78 tv_get (struct timeval *tv)
81 return tv->tv_sec + tv->tv_usec * 1e-6;
86 #define EVENT_VERSION(a,b) # a "." # b
88 const char *event_get_version (void)
90 return EVENT_VERSION (EV_VERSION_MAJOR, EV_VERSION_MINOR);
93 const char *event_get_method (void)
98 void *event_init (void)
102 x_cur = (struct event_base *)ev_loop_new (EVFLAG_AUTO);
104 x_cur = (struct event_base *)ev_default_loop (EVFLAG_AUTO);
106 assert (("multiple event bases not supported when not compiled with EV_MULTIPLICITY", !x_cur));
108 x_cur = (struct event_base *)(long)ev_default_loop (EVFLAG_AUTO);
114 void event_base_free (struct event_base *base)
119 if (ev_default_loop (EVFLAG_AUTO) != loop)
120 ev_loop_destroy (loop);
124 int event_dispatch (void)
126 return event_base_dispatch (x_cur);
130 void event_set_log_callback (event_log_cb cb)
136 int event_loop (int flags)
138 return event_base_loop (x_cur, flags);
141 int event_loopexit (struct timeval *tv)
143 return event_base_loopexit (x_cur, tv);
147 x_cb (struct event *ev, int revents)
149 revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL;
151 ev->ev_res = revents;
152 ev->ev_callback (ev->ev_fd, (short)revents, ev->ev_arg);
156 x_cb_sig (EV_P_ struct ev_signal *w, int revents)
158 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.sig));
160 if (revents & EV_ERROR)
167 x_cb_io (EV_P_ struct ev_io *w, int revents)
169 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io));
171 if ((revents & EV_ERROR) || !(ev->ev_events & EV_PERSIST))
178 x_cb_to (EV_P_ struct ev_timer *w, int revents)
180 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to));
187 void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg)
189 if (events & EV_SIGNAL)
190 ev_init (&ev->iosig.sig, x_cb_sig);
192 ev_init (&ev->iosig.io, x_cb_io);
194 ev_init (&ev->to, x_cb_to);
196 ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */
198 ev->ev_events = events;
200 ev->ev_callback = cb;
203 ev->ev_flags = EVLIST_INIT;
206 int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
208 return event_base_once (x_cur, fd, events, cb, arg, tv);
211 int event_add (struct event *ev, struct timeval *tv)
215 if (ev->ev_events & EV_SIGNAL)
217 if (!ev_is_active (&ev->iosig.sig))
219 ev_signal_set (&ev->iosig.sig, ev->ev_fd);
220 ev_signal_start (EV_A_ &ev->iosig.sig);
222 ev->ev_flags |= EVLIST_SIGNAL;
225 else if (ev->ev_events & (EV_READ | EV_WRITE))
227 if (!ev_is_active (&ev->iosig.io))
229 ev_io_set (&ev->iosig.io, ev->ev_fd, ev->ev_events & (EV_READ | EV_WRITE));
230 ev_io_start (EV_A_ &ev->iosig.io);
232 ev->ev_flags |= EVLIST_INSERTED;
238 ev->to.repeat = tv_get (tv);
239 ev_timer_again (EV_A_ &ev->to);
240 ev->ev_flags |= EVLIST_TIMEOUT;
244 ev_timer_stop (EV_A_ &ev->to);
245 ev->ev_flags &= ~EVLIST_TIMEOUT;
248 ev->ev_flags |= EVLIST_ACTIVE;
253 int event_del (struct event *ev)
257 if (ev->ev_events & EV_SIGNAL)
258 ev_signal_stop (EV_A_ &ev->iosig.sig);
259 else if (ev->ev_events & (EV_READ | EV_WRITE))
260 ev_io_stop (EV_A_ &ev->iosig.io);
262 if (ev_is_active (&ev->to))
263 ev_timer_stop (EV_A_ &ev->to);
265 ev->ev_flags = EVLIST_INIT;
270 void event_active (struct event *ev, int res, short ncalls)
274 if (res & EV_TIMEOUT)
275 ev_feed_event (EV_A_ &ev->to, res & EV_TIMEOUT);
278 ev_feed_event (EV_A_ &ev->iosig.sig, res & EV_SIGNAL);
280 if (res & (EV_READ | EV_WRITE))
281 ev_feed_event (EV_A_ &ev->iosig.io, res & (EV_READ | EV_WRITE));
284 int event_pending (struct event *ev, short events, struct timeval *tv)
290 if (ev->ev_events & EV_SIGNAL)
293 if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig))
294 revents |= EV_SIGNAL;
296 else if (ev->ev_events & (EV_READ | EV_WRITE))
299 if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io))
300 revents |= ev->ev_events & (EV_READ | EV_WRITE);
303 if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to))
305 revents |= EV_TIMEOUT;
308 tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */
311 return events & revents;
314 int event_priority_init (int npri)
316 return event_base_priority_init (x_cur, npri);
319 int event_priority_set (struct event *ev, int pri)
326 int event_base_set (struct event_base *base, struct event *ev)
333 int event_base_loop (struct event_base *base, int flags)
337 ev_loop (EV_A_ flags);
342 int event_base_dispatch (struct event_base *base)
344 return event_base_loop (base, 0);
348 x_loopexit_cb (int revents, void *base)
352 ev_unloop (EV_A_ EVUNLOOP_ONE);
355 int event_base_loopexit (struct event_base *base, struct timeval *tv)
357 ev_tstamp after = tv_get (tv);
360 ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base);
368 void (*cb)(int, short, void *);
373 x_once_cb (int revents, void *arg)
375 struct x_once *once = (struct x_once *)arg;
377 once->cb (once->fd, (short)revents, once->arg);
381 int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
383 struct x_once *once = (struct x_once *)malloc (sizeof (struct x_once));
393 ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), tv_get (tv), x_once_cb, (void *)once);
398 int event_base_priority_init (struct event_base *base, int npri)