X-Git-Url: https://git.llucax.com/software/libev.git/blobdiff_plain/228b0b302ab0857b23efad5a8b2429de2c0ccc00..5e20ccfe261679c78f9aa365ac087fb0700e0f2b:/event.c?ds=inline diff --git a/event.c b/event.c index b1c335e..6e26a48 100644 --- a/event.c +++ b/event.c @@ -32,18 +32,25 @@ #include #include #include +#include #include "event.h" -#define dLOOPev -#define dLOOPbase +#if EV_MULTIPLICITY +# define dLOOPev struct ev_loop *loop = (struct ev_loop *)ev->ev_base +# define dLOOPbase struct ev_loop *loop = (struct ev_loop *)base +#else +# define dLOOPev +# define dLOOPbase +#endif +/* never accessed, will always be cast from/to ev_loop */ struct event_base { int dummy; }; -static struct event_base x_base, *x_cur; +static struct event_base *x_cur; static void tv_set (struct timeval *tv, ev_tstamp at) @@ -75,15 +82,27 @@ const char *event_get_method (void) void *event_init (void) { - if (!x_cur && ev_init (0)) - return x_cur = &x_base; +#if EV_MULTIPLICITY + if (x_cur) + x_cur = (struct event_base *)ev_loop_new (EVMETHOD_AUTO); + else + x_cur = ev_default_loop (EVMETHOD_AUTO); +#else + assert (("multiple event bases not supported when not compiled with EV_MULTIPLICITY", !x_cur)); - return 0; + x_cur = (struct event_base *)ev_default_loop (EVMETHOD_AUTO); +#endif + + return x_cur; } void event_base_free (struct event_base *base) { - /* nop */ + dLOOPbase; + +#if EV_MULTIPLICITY + ev_loop_delete (loop); +#endif } int event_dispatch (void) @@ -129,7 +148,7 @@ x_cb_io (EV_P_ struct ev_io *w, int revents) struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w)) - ev_io_stop (w); + ev_io_stop (EV_A_ w); x_cb (ev, revents); } @@ -142,14 +161,19 @@ x_cb_to (EV_P_ struct ev_timer *w, int revents) void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) { - if (events & EV_SIGNAL) - ev_watcher_init (&ev->iosig.sig, x_cb_sig); - else - ev_watcher_init (&ev->iosig.io, x_cb_io); + if (!ev->initialised) + { + ev->initialised = 1; + + if (events & EV_SIGNAL) + ev_watcher_init (&ev->iosig.sig, x_cb_sig); + else + ev_watcher_init (&ev->iosig.io, x_cb_io); - ev_watcher_init (&ev->to, x_cb_to); + ev_watcher_init (&ev->to, x_cb_to); + } - ev->ev_base = x_cur; + ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */ ev->ev_fd = fd; ev->ev_events = events; ev->ev_pri = 0; @@ -265,7 +289,8 @@ int event_base_set (struct event_base *base, struct event *ev) int event_base_loop (struct event_base *base, int flags) { dLOOPbase; - ev_loop (EV_A_ flags | EVLOOP_ONESHOT); + + ev_loop (EV_A_ flags); return 0; } @@ -276,9 +301,11 @@ int event_base_dispatch (struct event_base *base) } static void -x_loopexit_cb (EV_P_ int revents, void *arg) +x_loopexit_cb (int revents, void *base) { - ev_unloop (EV_A_ 2); + dLOOPbase; + + ev_unloop (EV_A_ EVUNLOOP_ONCE); } int event_base_loopexit (struct event_base *base, struct timeval *tv)