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.
38 #ifdef EV_MULTIPLICITY
39 # define dLOOPev struct ev_loop *loop = ev->ev_base->loop
40 # define dLOOPbase struct ev_loop *loop = base->loop
48 #ifdef EV_MULTIPLICITY
54 static struct event_base x_base, *x_cur;
57 tv_set (struct timeval *tv, ev_tstamp at)
59 tv->tv_sec = (long)at;
60 tv->tv_usec = (long)((at - (ev_tstamp)tv->tv_sec) * 1e6);
64 tv_get (struct timeval *tv)
67 return tv->tv_sec + tv->tv_usec * 1e-6;
72 #define EVENT_VERSION(a,b) # a "." # b
74 const char *event_get_version (void)
76 return EVENT_VERSION (EV_VERSION_MAJOR, EV_VERSION_MINOR);
79 const char *event_get_method (void)
84 void *event_init (void)
86 if (!x_cur && ev_init (0))
87 return x_cur = &x_base;
92 void event_base_free (struct event_base *base)
99 int event_dispatch (void)
101 return event_base_dispatch (x_cur);
105 void event_set_log_callback (event_log_cb cb)
111 int event_loop (int flags)
113 return event_base_loop (x_cur, flags);
116 int event_loopexit (struct timeval *tv)
118 return event_base_loopexit (x_cur, tv);
122 x_cb (struct event *ev, int revents)
124 revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL;
126 ev->ev_res = revents;
127 ev->ev_callback (ev->ev_fd, revents, ev->ev_arg);
131 x_cb_sig (EV_P_ struct ev_signal *w, int revents)
133 x_cb ((struct event *)(((char *)w) - offsetof (struct event, iosig.sig)), revents);
137 x_cb_io (EV_P_ struct ev_io *w, int revents)
139 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io));
141 if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w))
148 x_cb_to (EV_P_ struct ev_timer *w, int revents)
150 x_cb ((struct event *)(((char *)w) - offsetof (struct event, to)), revents);
153 void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg)
155 if (events & EV_SIGNAL)
156 ev_watcher_init (&ev->iosig.sig, x_cb_sig);
158 ev_watcher_init (&ev->iosig.io, x_cb_io);
160 ev_watcher_init (&ev->to, x_cb_to);
164 ev->ev_events = events;
166 ev->ev_callback = cb;
171 int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
173 return event_base_once (x_cur, fd, events, cb, arg, tv);
176 int event_add (struct event *ev, struct timeval *tv)
180 /* disable all watchers */
183 if (ev->ev_events & EV_SIGNAL)
185 ev_signal_set (&ev->iosig.sig, ev->ev_fd);
186 ev_signal_start (EV_A_ &ev->iosig.sig);
188 else if (ev->ev_events & (EV_READ | EV_WRITE))
190 ev_io_set (&ev->iosig.io, ev->ev_fd, ev->ev_events & (EV_READ | EV_WRITE));
191 ev_io_start (EV_A_ &ev->iosig.io);
196 ev_timer_set (&ev->to, tv_get (tv), 0.);
197 ev_timer_start (EV_A_ &ev->to);
203 int event_del (struct event *ev)
207 if (ev->ev_events & EV_SIGNAL)
210 if (ev_is_active (&ev->iosig.sig))
211 ev_signal_stop (EV_A_ &ev->iosig.sig);
216 if (ev_is_active (&ev->iosig.io))
217 ev_io_stop (EV_A_ &ev->iosig.io);
220 if (ev_is_active (&ev->to))
221 ev_timer_stop (EV_A_ &ev->to);
226 int event_pending (struct event *ev, short events, struct timeval *tv)
232 if (ev->ev_events & EV_SIGNAL)
235 if (ev->iosig.sig.pending)
236 revents |= EV_SIGNAL;
241 if (ev->iosig.io.pending)
242 revents |= ev->ev_events & (EV_READ | EV_WRITE);
247 revents |= EV_TIMEOUT;
250 tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */
253 return events & revents;
256 int event_priority_init (int npri)
258 return event_base_priority_init (x_cur, npri);
261 int event_priority_set (struct event *ev, int pri)
268 int event_base_set (struct event_base *base, struct event *ev)
275 int event_base_loop (struct event_base *base, int flags)
279 ev_loop (EV_A_ flags);
284 int event_base_dispatch (struct event_base *base)
286 return event_base_loop (base, 0);
290 x_loopexit_cb (EV_P_ int revents, void *arg)
295 int event_base_loopexit (struct event_base *base, struct timeval *tv)
298 ev_tstamp after = tv_get (tv);
300 ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base);
308 void (*cb)(int, short, void *);
313 x_once_cb (int revents, void *arg)
315 struct x_once *once = arg;
317 once->cb (once->fd, revents, once->arg);
321 int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
324 struct x_once *once = malloc (sizeof (struct x_once));
333 ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), tv_get (tv), x_once_cb, (void *)once);
338 int event_base_priority_init (struct event_base *base, int npri)