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
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 # include <sys/time.h>
47 # define dLOOPev struct ev_loop *loop = (struct ev_loop *)ev->ev_base
48 # define dLOOPbase struct ev_loop *loop = (struct ev_loop *)base
54 /* never accessed, will always be cast from/to ev_loop */
60 static struct event_base *x_cur;
63 tv_set (struct timeval *tv, ev_tstamp at)
65 tv->tv_sec = (long)at;
66 tv->tv_usec = (long)((at - (ev_tstamp)tv->tv_sec) * 1e6);
70 tv_get (struct timeval *tv)
73 return tv->tv_sec + tv->tv_usec * 1e-6;
78 #define EVENT_VERSION(a,b) # a "." # b
80 const char *event_get_version (void)
82 return EVENT_VERSION (EV_VERSION_MAJOR, EV_VERSION_MINOR);
85 const char *event_get_method (void)
90 void *event_init (void)
94 x_cur = (struct event_base *)ev_loop_new (EVMETHOD_AUTO);
96 x_cur = (struct event_base *)ev_default_loop (EVMETHOD_AUTO);
98 assert (("multiple event bases not supported when not compiled with EV_MULTIPLICITY", !x_cur));
100 x_cur = (struct event_base *)(long)ev_default_loop (EVMETHOD_AUTO);
106 void event_base_free (struct event_base *base)
111 if (ev_default_loop (EVMETHOD_AUTO) != loop)
112 ev_loop_destroy (loop);
116 int event_dispatch (void)
118 return event_base_dispatch (x_cur);
122 void event_set_log_callback (event_log_cb cb)
128 int event_loop (int flags)
130 return event_base_loop (x_cur, flags);
133 int event_loopexit (struct timeval *tv)
135 return event_base_loopexit (x_cur, tv);
139 x_cb (struct event *ev, int revents)
141 revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL;
143 ev->ev_res = revents;
144 ev->ev_callback (ev->ev_fd, revents, ev->ev_arg);
148 x_cb_sig (EV_P_ struct ev_signal *w, int revents)
150 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.sig));
152 if (revents & EV_ERROR)
159 x_cb_io (EV_P_ struct ev_io *w, int revents)
161 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io));
163 if (revents & EV_ERROR)
165 else if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w))
166 ev_io_stop (EV_A_ w);
172 x_cb_to (EV_P_ struct ev_timer *w, int revents)
174 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to));
181 void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg)
183 if (events & EV_SIGNAL)
184 ev_watcher_init (&ev->iosig.sig, x_cb_sig);
186 ev_watcher_init (&ev->iosig.io, x_cb_io);
188 ev_watcher_init (&ev->to, x_cb_to);
190 ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */
192 ev->ev_events = events;
194 ev->ev_callback = cb;
199 int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
201 return event_base_once (x_cur, fd, events, cb, arg, tv);
204 int event_add (struct event *ev, struct timeval *tv)
208 /* disable all watchers */
211 if (ev->ev_events & EV_SIGNAL)
213 ev_signal_set (&ev->iosig.sig, ev->ev_fd);
214 ev_signal_start (EV_A_ &ev->iosig.sig);
216 else if (ev->ev_events & (EV_READ | EV_WRITE))
218 ev_io_set (&ev->iosig.io, ev->ev_fd, ev->ev_events & (EV_READ | EV_WRITE));
219 ev_io_start (EV_A_ &ev->iosig.io);
224 ev_timer_set (&ev->to, tv_get (tv), 0.);
225 ev_timer_start (EV_A_ &ev->to);
231 int event_del (struct event *ev)
235 if (ev->ev_events & EV_SIGNAL)
238 if (ev_is_active (&ev->iosig.sig))
239 ev_signal_stop (EV_A_ &ev->iosig.sig);
241 else if (ev->ev_events & (EV_READ | EV_WRITE))
244 if (ev_is_active (&ev->iosig.io))
245 ev_io_stop (EV_A_ &ev->iosig.io);
248 if (ev_is_active (&ev->to))
249 ev_timer_stop (EV_A_ &ev->to);
254 void event_active (struct event *ev, int res, short ncalls)
258 if (res & EV_TIMEOUT)
259 ev_feed_event (EV_A_ &ev->to, res & EV_TIMEOUT);
262 ev_feed_event (EV_A_ &ev->iosig.sig, res & EV_SIGNAL);
264 if (res & (EV_READ | EV_WRITE))
265 ev_feed_event (EV_A_ &ev->iosig.io, res & (EV_READ | EV_WRITE));
268 int event_pending (struct event *ev, short events, struct timeval *tv)
274 if (ev->ev_events & EV_SIGNAL)
277 if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig))
278 revents |= EV_SIGNAL;
280 else if (ev->ev_events & (EV_READ | EV_WRITE))
283 if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io))
284 revents |= ev->ev_events & (EV_READ | EV_WRITE);
287 if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to))
289 revents |= EV_TIMEOUT;
292 tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */
295 return events & revents;
298 int event_priority_init (int npri)
300 return event_base_priority_init (x_cur, npri);
303 int event_priority_set (struct event *ev, int pri)
310 int event_base_set (struct event_base *base, struct event *ev)
317 int event_base_loop (struct event_base *base, int flags)
321 ev_loop (EV_A_ flags);
326 int event_base_dispatch (struct event_base *base)
328 return event_base_loop (base, 0);
332 x_loopexit_cb (int revents, void *base)
336 ev_unloop (EV_A_ EVUNLOOP_ONCE);
339 int event_base_loopexit (struct event_base *base, struct timeval *tv)
341 ev_tstamp after = tv_get (tv);
344 ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base);
352 void (*cb)(int, short, void *);
357 x_once_cb (int revents, void *arg)
359 struct x_once *once = (struct x_once *)arg;
361 once->cb (once->fd, revents, once->arg);
365 int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
367 struct x_once *once = (struct x_once *)malloc (sizeof (struct x_once));
377 ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), tv_get (tv), x_once_cb, (void *)once);
382 int event_base_priority_init (struct event_base *base, int npri)