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.
45 static struct event_base x_base, *x_cur;
48 tv_set (struct timeval *tv, ev_tstamp at)
50 tv->tv_sec = (long)at;
51 tv->tv_usec = (long)((at - (ev_tstamp)tv->tv_sec) * 1e6);
55 tv_get (struct timeval *tv)
58 return tv->tv_sec + tv->tv_usec * 1e-6;
63 #define EVENT_VERSION(a,b) # a "." # b
65 const char *event_get_version (void)
67 return EVENT_VERSION (EV_VERSION_MAJOR, EV_VERSION_MINOR);
70 const char *event_get_method (void)
75 void *event_init (void)
78 return x_cur = &x_base;
83 void event_base_free (struct event_base *base)
88 int event_dispatch (void)
90 return event_base_dispatch (x_cur);
93 void event_set_log_callback (event_log_cb cb)
98 int event_loop (int flags)
100 return event_base_loop (x_cur, flags);
103 int event_loopexit (struct timeval *tv)
105 event_base_loopexit (x_cur, tv);
109 x_cb (struct event *ev, int revents)
111 if (ev->ev_events & EV_SIGNAL)
114 if (ev_is_active (&ev->iosig.sig))
116 ev_signal_stop (&ev->iosig.sig);
123 if (!(ev->ev_events & EV_PERSIST) && ev_is_active (&ev->iosig.io))
125 ev_io_stop (&ev->iosig.io);
130 revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL;
132 if (revents & EV_TIMEOUT)
135 ev->ev_res = revents;
136 ev->ev_callback (ev->ev_fd, revents, ev->ev_arg);
140 x_cb_io (struct ev_io *w, int revents)
142 x_cb ((struct event *)(((char *)w) - offsetof (struct event, iosig.io)), revents);
146 x_cb_to (struct ev_timer *w, int revents)
148 x_cb ((struct event *)(((char *)w) - offsetof (struct event, to)), revents);
152 x_cb_sig (struct ev_signal *w, int revents)
154 x_cb ((struct event *)(((char *)w) - offsetof (struct event, iosig.sig)), revents);
157 void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg)
159 if (events & EV_SIGNAL)
160 ev_watcher_init (&ev->iosig.sig, x_cb_sig);
162 ev_watcher_init (&ev->iosig.io, x_cb_io);
164 ev_watcher_init (&ev->to, x_cb_to);
168 ev->ev_events = events;
170 ev->ev_callback = cb;
175 int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
177 event_base_once (x_cur, fd, events, cb, arg, tv);
180 int event_add (struct event *ev, struct timeval *tv)
182 /* disable all watchers */
185 if (ev->ev_events & EV_SIGNAL)
187 ev_signal_set (&ev->iosig.sig, ev->ev_fd);
188 ev_signal_start (&ev->iosig.sig);
191 else if (ev->ev_events & (EV_READ | EV_WRITE))
193 ev_io_set (&ev->iosig.io, ev->ev_fd, ev->ev_events & (EV_READ | EV_WRITE));
194 ev_io_start (&ev->iosig.io);
200 ev_timer_set (&ev->to, tv_get (tv), 0.);
201 ev_timer_start (&ev->to);
208 int event_del (struct event *ev)
210 if (ev->ev_events & EV_SIGNAL)
213 if (ev_is_active (&ev->iosig.sig))
215 ev_signal_stop (&ev->iosig.sig);
222 if (ev_is_active (&ev->iosig.io))
224 ev_io_stop (&ev->iosig.io);
229 if (ev_is_active (&ev->to))
231 ev_timer_stop (&ev->to);
238 int event_pending (struct event *ev, short events, struct timeval *tv)
242 if (ev->ev_events & EV_SIGNAL)
245 if (ev->iosig.sig.pending)
246 revents |= EV_SIGNAL;
251 if (ev->iosig.io.pending)
252 revents |= ev->ev_events & (EV_READ | EV_WRITE);
257 revents |= EV_TIMEOUT;
260 tv_set (tv, ev_now); /* not sure if this is right :) */
263 return events & revents;
266 int event_priority_init (int npri)
268 return event_base_priority_init (x_cur, npri);
271 int event_priority_set (struct event *ev, int pri)
278 int event_base_set (struct event_base *base, struct event *ev)
285 int event_base_loop (struct event_base *base, int flags)
289 ev_loop (flags | EVLOOP_ONESHOT);
291 while (!(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) && x_actives && !ev_loop_done);
296 int event_base_dispatch (struct event_base *base)
298 return event_base_loop (base, 0);
302 x_loopexit_cb (int revents, void *arg)
307 int event_base_loopexit (struct event_base *base, struct timeval *tv)
309 ev_tstamp after = tv_get (tv);
311 ev_once (-1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base);
317 void (*cb)(int, short, void *);
322 x_once_cb (int revents, void *arg)
324 struct x_once *once = arg;
326 once->cb (once->fd, revents, once->arg);
330 int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
332 struct x_once *once = malloc (sizeof (struct x_once));
341 ev_once (fd, events & (EV_READ | EV_WRITE), tv_get (tv), x_once_cb, (void *)once);
346 int event_base_priority_init (struct event_base *base, int npri)