X-Git-Url: https://git.llucax.com/software/libev.git/blobdiff_plain/36ed1953891a670e15f5e113491596630f21676c..d5b686568688adf526735190000d81a260db6c36:/event.c?ds=inline diff --git a/event.c b/event.c index 1c2bc73..cf7739c 100644 --- a/event.c +++ b/event.c @@ -32,26 +32,26 @@ #include #include #include +#include +#include "ev.h" #include "event.h" -#ifdef EV_MULTIPLICITY -# define dLOOPev struct ev_loop *loop = ev->ev_base->loop -# define dLOOPbase struct ev_loop *loop = base->loop +#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 { -#ifdef EV_MULTIPLICITY - struct ev_loop *loop; -#endif 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) @@ -83,17 +83,28 @@ 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 = (struct event_base *)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 *)(long)ev_default_loop (EVMETHOD_AUTO); +#endif + + return x_cur; } void event_base_free (struct event_base *base) { dLOOPbase; - /* nop */ +#if EV_MULTIPLICITY + if (ev_default_loop (EVMETHOD_AUTO) != loop) + ev_loop_destroy (loop); +#endif } int event_dispatch (void) @@ -139,7 +150,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); } @@ -147,7 +158,11 @@ x_cb_io (EV_P_ struct ev_io *w, int revents) static void x_cb_to (EV_P_ struct ev_timer *w, int revents) { - x_cb ((struct event *)(((char *)w) - offsetof (struct event, to)), revents); + struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to)); + + event_del (ev); + + x_cb (ev, revents); } void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) @@ -159,7 +174,7 @@ void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, v 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; @@ -210,7 +225,7 @@ int event_del (struct event *ev) if (ev_is_active (&ev->iosig.sig)) ev_signal_stop (EV_A_ &ev->iosig.sig); } - else + else if (ev->ev_events & (EV_READ | EV_WRITE)) { /* io */ if (ev_is_active (&ev->iosig.io)) @@ -232,17 +247,17 @@ int event_pending (struct event *ev, short events, struct timeval *tv) if (ev->ev_events & EV_SIGNAL) { /* sig */ - if (ev->iosig.sig.pending) + if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) revents |= EV_SIGNAL; } - else + else if (ev->ev_events & (EV_READ | EV_WRITE)) { /* io */ - if (ev->iosig.io.pending) + if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io)) revents |= ev->ev_events & (EV_READ | EV_WRITE); } - if (ev->to.pending) + if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to)) { revents |= EV_TIMEOUT; @@ -287,9 +302,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) @@ -337,7 +354,7 @@ int event_base_once (struct event_base *base, int fd, short events, void (*cb)(i int event_base_priority_init (struct event_base *base, int npri) { - dLOOPbase; + /*dLOOPbase;*/ return 0; }