--- cvsroot/EV/EV.xs 2007/10/29 08:48:07 1.11 +++ cvsroot/EV/EV.xs 2007/10/31 14:44:35 1.21 @@ -2,49 +2,29 @@ #include "perl.h" #include "XSUB.h" -#include -#include - -#include -#include -#include -#include - -/* workaround for evhttp.h requiring obscure bsd headers */ -#ifndef TAILQ_ENTRY -#define TAILQ_ENTRY(type) \ -struct { \ - struct type *tqe_next; /* next element */ \ - struct type **tqe_prev; /* address of previous next element */ \ -} -#endif /* !TAILQ_ENTRY */ -#include - -#define EV_NONE 0 -#define EV_UNDEF -1 +/*#include */ #define TIMEOUT_NONE HUGE_VAL +#define HAVE_EPOLL 0 +#define EV_PROTOTYPES 1 #include "EV/EVAPI.h" -typedef struct event_base *Base; +#include "libev/ev.c" + typedef int Signal; static struct EVAPI evapi; -static HV *stash_base, *stash_event; - -static double tv_get (struct timeval *tv) -{ - return tv->tv_sec + tv->tv_usec * 1e-6; -} - -static void tv_set (struct timeval *tv, double val) -{ - tv->tv_sec = (long)val; - tv->tv_usec = (long)((val - (double)tv->tv_sec) * 1e6); - -} +static HV + *stash_watcher, + *stash_io, + *stash_time, + *stash_timer, + *stash_periodic, + *stash_signal, + *stash_idle, + *stash_check; static int sv_signum (SV *sig) @@ -61,44 +41,13 @@ return -1; } -static void -api_once (int fd, short events, double timeout, void (*cb)(int, short, void *), void *arg) -{ - if (timeout) - { - struct timeval tv; - tv_set (&tv, timeout); - event_once (fd, events, cb, arg, &tv); - } - else - event_once (fd, events, cb, arg, 0); -} - ///////////////////////////////////////////////////////////////////////////// // Event -typedef struct ev { - struct event ev; - SV *cb, *fh; - SV *self; /* contains this struct */ - double timeout; - double interval; - unsigned char active; - unsigned char abstime; -} *Event; +static void e_cb (struct ev_watcher *w, int revents); -static double -e_now (void) -{ - struct timeval tv; - gettimeofday (&tv, 0); - - return tv_get (&tv); -} - -static void e_cb (int fd, short events, void *arg); - -static int sv_fileno (SV *fh) +static int +sv_fileno (SV *fh) { SvGETMAGIC (fh); @@ -114,121 +63,85 @@ return -1; } -static Event -e_new (SV *fh, short events, SV *cb) +static void * +e_new (int size, SV *cb_sv) { - int fd = sv_fileno (fh); - Event ev; - SV *self = NEWSV (0, sizeof (struct ev)); + struct ev_watcher *w; + SV *self = NEWSV (0, size); SvPOK_only (self); - SvCUR_set (self, sizeof (struct ev)); - - ev = (Event)SvPVX (self); - - ev->fh = newSVsv (fh); - ev->cb = newSVsv (cb); - ev->self = self; - ev->timeout = TIMEOUT_NONE; - ev->interval = 0.; - ev->abstime = 0; - ev->active = 0; - - event_set (&ev->ev, fd, events, e_cb, (void *)ev); - - return ev; -} + SvCUR_set (self, size); -static struct timeval * -e_tv (Event ev) -{ - static struct timeval tv; - double to = ev->timeout; + w = (struct ev_watcher *)SvPVX (self); - if (to == TIMEOUT_NONE) - return 0; + evw_init (w, e_cb); - if (ev->abstime) - { - double now = e_now (); + w->fh = 0; + w->cb_sv = newSVsv (cb_sv); + w->self = self; - if (ev->interval) - ev->timeout = (to += ceil ((now - to) / ev->interval) * ev->interval); - - to -= now; - } - else if (to < 0.) - to = 0.; - - tv_set (&tv, to); - - return &tv; + return (void *)w; } -static SV *e_self (Event ev) +static SV * +e_bless (struct ev_watcher *w, HV *stash) { SV *rv; - if (SvOBJECT (ev->self)) - rv = newRV_inc (ev->self); + if (SvOBJECT (w->self)) + rv = newRV_inc (w->self); else { - rv = newRV_noinc (ev->self); - sv_bless (rv, stash_event); - SvREADONLY_on (ev->self); + rv = newRV_noinc (w->self); + sv_bless (rv, stash); + SvREADONLY_on (w->self); } return rv; } -static int -e_start (Event ev) -{ - if (ev->active) event_del (&ev->ev); - ev->active = 1; - return event_add (&ev->ev, e_tv (ev)); -} - -static int e_stop (Event ev) -{ - return ev->active - ? (ev->active = 0), event_del (&ev->ev) - : 0; -} - static void -e_cb (int fd, short events, void *arg) +e_cb (struct ev_watcher *w, int revents) { - struct ev *ev = (struct ev*)arg; dSP; + I32 mark = SP - PL_stack_base; + SV *sv_self, *sv_events; + static SV *sv_events_cache; - ENTER; - SAVETMPS; + sv_self = newRV_inc (w->self); /* w->self MUST be blessed by now */ - if (!(ev->ev.ev_events & EV_PERSIST) || (events & EV_TIMEOUT)) - ev->active = 0; + if (sv_events_cache) + { + sv_events = sv_events_cache; sv_events_cache = 0; + SvIV_set (sv_events, revents); + } + else + sv_events = newSViv (revents); PUSHMARK (SP); EXTEND (SP, 2); - PUSHs (sv_2mortal (e_self (ev))); - PUSHs (sv_2mortal (newSViv (events))); + PUSHs (sv_self); + PUSHs (sv_events); PUTBACK; - call_sv (ev->cb, G_DISCARD | G_VOID | G_EVAL); + call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL); + SP = PL_stack_base + mark; PUTBACK; - if (ev->interval && !ev->active) - e_start (ev); + SvREFCNT_dec (sv_self); - FREETMPS; + if (sv_events_cache) + SvREFCNT_dec (sv_events); + else + sv_events_cache = sv_events; if (SvTRUE (ERRSV)) { PUSHMARK (SP); PUTBACK; call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR); + SP = PL_stack_base + mark; PUTBACK; } - - LEAVE; } +#if 0 ///////////////////////////////////////////////////////////////////////////// // DNS @@ -280,11 +193,17 @@ LEAVE; } +#endif + +#define CHECK_REPEAT(repeat) if (repeat < 0.) \ + croak (# repeat " value must be >= 0"); ///////////////////////////////////////////////////////////////////////////// // XS interface functions -MODULE = EV PACKAGE = EV PREFIX = event_ +MODULE = EV PACKAGE = EV PREFIX = ev_ + +PROTOTYPES: ENABLE BOOT: { @@ -296,284 +215,304 @@ IV iv; } *civ, const_iv[] = { # define const_iv(pfx, name) { # name, (IV) pfx ## name }, + const_iv (EV_, UNDEF) const_iv (EV_, NONE) const_iv (EV_, TIMEOUT) const_iv (EV_, READ) const_iv (EV_, WRITE) const_iv (EV_, SIGNAL) - const_iv (EV_, PERSIST) - const_iv (EV, LOOP_ONCE) + const_iv (EV_, IDLE) + const_iv (EV_, CHECK) + const_iv (EV_, ERROR) + + const_iv (EV, LOOP_ONESHOT) const_iv (EV, LOOP_NONBLOCK) - const_iv (EV, BUFFER_READ) - const_iv (EV, BUFFER_WRITE) - const_iv (EV, BUFFER_EOF) - const_iv (EV, BUFFER_ERROR) - const_iv (EV, BUFFER_TIMEOUT) + + const_iv (EV, METHOD_NONE) + const_iv (EV, METHOD_SELECT) + const_iv (EV, METHOD_EPOLL) }; for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); - stash_base = gv_stashpv ("EV::Base" , 1); - stash_event = gv_stashpv ("EV::Event", 1); + stash_watcher = gv_stashpv ("EV::Watcher" , 1); + stash_io = gv_stashpv ("EV::IO" , 1); + stash_time = gv_stashpv ("EV::Time" , 1); + stash_timer = gv_stashpv ("EV::Timer" , 1); + stash_periodic = gv_stashpv ("EV::Periodic", 1); + stash_signal = gv_stashpv ("EV::Signal" , 1); + stash_idle = gv_stashpv ("EV::Idle" , 1); + stash_check = gv_stashpv ("EV::Check" , 1); { SV *sv = perl_get_sv ("EV::API", TRUE); perl_get_sv ("EV::API", TRUE); /* silence 5.10 warning */ - evapi.ver = EV_API_VERSION; - evapi.rev = EV_API_REVISION; - evapi.now = e_now; - evapi.once = api_once; + /* the poor man's shared library emulator */ + evapi.ver = EV_API_VERSION; + evapi.rev = EV_API_REVISION; + evapi.sv_fileno = sv_fileno; + evapi.sv_signum = sv_signum; + evapi.now = &ev_now; + evapi.method = &ev_method; + evapi.loop_done = &ev_loop_done; + evapi.time = ev_time; + evapi.loop = ev_loop; + evapi.once = ev_once; + evapi.io_start = evio_start; + evapi.io_stop = evio_stop; + evapi.timer_start = evtimer_start; + evapi.timer_stop = evtimer_stop; + evapi.timer_again = evtimer_again; + evapi.periodic_start = evperiodic_start; + evapi.periodic_stop = evperiodic_stop; + evapi.signal_start = evsignal_start; + evapi.signal_stop = evsignal_stop; + evapi.idle_start = evidle_start; + evapi.idle_stop = evidle_stop; + evapi.check_start = evcheck_start; + evapi.check_stop = evcheck_stop; sv_setiv (sv, (IV)&evapi); SvREADONLY_on (sv); } } -double now () +NV ev_now () CODE: - RETVAL = e_now (); + RETVAL = ev_now; OUTPUT: RETVAL -const char *version () - ALIAS: - method = 1 +int ev_method () CODE: - RETVAL = ix ? event_get_method () : event_get_version (); + RETVAL = ev_method; OUTPUT: RETVAL -Base event_init () - -int event_priority_init (int npri) +NV ev_time () -int event_dispatch () +void ev_init (int flags = 0) -int event_loop (int flags = 0) +void ev_loop (int flags = 0) -int event_loopexit (double after = 0) +void ev_loop_done (int value = 1) CODE: -{ - struct timeval tv; - tv_set (&tv, after); - event_loopexit (&tv); -} + ev_loop_done = value; -Event event (SV *cb) +struct ev_io *io (SV *fh, int events, SV *cb) + ALIAS: + io_ns = 1 CODE: - RETVAL = e_new (NEWSV (0, 0), 0, cb); + RETVAL = e_new (sizeof (struct ev_io), cb); + RETVAL->fh = newSVsv (fh); + evio_set (RETVAL, sv_fileno (RETVAL->fh), events); + if (!ix) evio_start (RETVAL); OUTPUT: RETVAL -Event io (SV *fh, short events, SV *cb) +struct ev_timer *timer (NV after, NV repeat, SV *cb) ALIAS: - io_ns = 1 + timer_ns = 1 + INIT: + CHECK_REPEAT (repeat); CODE: - RETVAL = e_new (fh, events, cb); - if (!ix) e_start (RETVAL); + RETVAL = e_new (sizeof (struct ev_timer), cb); + evtimer_set (RETVAL, after, repeat); + if (!ix) evtimer_start (RETVAL); OUTPUT: RETVAL -Event timed_io (SV *fh, short events, double timeout, SV *cb) +struct ev_periodic *periodic (NV at, NV interval, SV *cb) ALIAS: - timed_io_ns = 1 + periodic_ns = 1 + INIT: + CHECK_REPEAT (interval); CODE: -{ - events = timeout ? events & ~EV_PERSIST : events | EV_PERSIST; - - RETVAL = e_new (fh, events, cb); - - if (timeout) - { - RETVAL->timeout = timeout; - RETVAL->interval = 1; - } - - if (!ix) e_start (RETVAL); -} + RETVAL = e_new (sizeof (struct ev_periodic), cb); + evperiodic_set (RETVAL, at, interval); + if (!ix) evperiodic_start (RETVAL); OUTPUT: RETVAL -Event timer (double after, int repeat, SV *cb) +struct ev_signal *signal (Signal signum, SV *cb) ALIAS: - timer_ns = 1 + signal_ns = 1 CODE: - RETVAL = e_new (NEWSV (0, 0), 0, cb); - RETVAL->timeout = after; - RETVAL->interval = repeat; - if (!ix) e_start (RETVAL); + RETVAL = e_new (sizeof (struct ev_signal), cb); + evsignal_set (RETVAL, signum); + if (!ix) evsignal_start (RETVAL); OUTPUT: RETVAL -Event timer_abs (double at, double interval, SV *cb) +struct ev_idle *idle (SV *cb) ALIAS: - timer_abs_ns = 1 + idle_ns = 1 CODE: - RETVAL = e_new (NEWSV (0, 0), 0, cb); - RETVAL->timeout = at; - RETVAL->interval = interval; - RETVAL->abstime = 1; - if (!ix) e_start (RETVAL); + RETVAL = e_new (sizeof (struct ev_idle), cb); + evidle_set (RETVAL); + if (!ix) evidle_start (RETVAL); OUTPUT: RETVAL -Event signal (Signal signum, SV *cb) +struct ev_check *check (SV *cb) ALIAS: - signal_ns = 1 + check_ns = 1 CODE: - RETVAL = e_new (ST (0), EV_SIGNAL | EV_PERSIST, cb); - RETVAL->ev.ev_fd = signum; - if (!ix) e_start (RETVAL); + RETVAL = e_new (sizeof (struct ev_check), cb); + evcheck_set (RETVAL); + if (!ix) evcheck_start (RETVAL); OUTPUT: RETVAL + PROTOTYPES: DISABLE +MODULE = EV PACKAGE = EV::Watcher PREFIX = ev_ -MODULE = EV PACKAGE = EV::Base PREFIX = event_base_ +int ev_is_active (struct ev_watcher *w) -Base new () +SV *cb (struct ev_watcher *w, SV *new_cb = 0) CODE: - RETVAL = event_init (); +{ + RETVAL = newSVsv (w->cb_sv); + + if (items > 1) + sv_setsv (w->cb_sv, new_cb); +} OUTPUT: RETVAL -int event_base_dispatch (Base base) +MODULE = EV PACKAGE = EV::IO PREFIX = evio_ -int event_base_loop (Base base, int flags = 0) +void evio_start (struct ev_io *w) -int event_base_loopexit (Base base, double after) +void evio_stop (struct ev_io *w) + +void set (struct ev_io *w, SV *fh, int events) CODE: { - struct timeval tv; - tv.tv_sec = (long)after; - tv.tv_usec = (long)(after - tv.tv_sec) * 1e6; - event_base_loopexit (base, &tv); -} + int active = w->active; + if (active) evio_stop (w); -int event_base_priority_init (Base base, int npri) + sv_setsv (w->fh, fh); + evio_set (w, sv_fileno (w->fh), events); -void event_base_set (Base base, Event ev) - C_ARGS: base, &ev->ev + if (active) evio_start (w); +} -void DESTROY (Base base) +SV *fh (struct ev_io *w, SV *new_fh = 0) CODE: - /*event_base_free (base);*/ /* causes too many problems */ - - -MODULE = EV PACKAGE = EV::Event PREFIX = event_ +{ + RETVAL = newSVsv (w->fh); -int event_priority_set (Event ev, int pri) - C_ARGS: &ev->ev, pri + if (items > 1) + { + int active = w->active; + if (active) evio_stop (w); -int event_add (Event ev, double timeout = TIMEOUT_NONE) - CODE: - ev->timeout = timeout; - ev->abstime = 0; - RETVAL = e_start (ev); - OUTPUT: - RETVAL + sv_setsv (w->fh, new_fh); + evio_set (w, sv_fileno (w->fh), w->events); -int event_start (Event ev) - CODE: - RETVAL = e_start (ev); - OUTPUT: - RETVAL - -int event_del (Event ev) - ALIAS: - stop = 0 - CODE: - RETVAL = e_stop (ev); + if (active) evio_start (w); + } +} OUTPUT: RETVAL -void DESTROY (Event ev) +short events (struct ev_io *w, short new_events = EV_UNDEF) CODE: - e_stop (ev); - SvREFCNT_dec (ev->cb); - SvREFCNT_dec (ev->fh); - -SV *cb (Event ev, SV *new_cb = 0) - CODE: - RETVAL = newSVsv (ev->cb); - if (items > 1) - sv_setsv (ev->cb, new_cb); - OUTPUT: - RETVAL +{ + RETVAL = w->events; -SV *fh (Event ev, SV *new_fh = 0) - CODE: - RETVAL = newSVsv (ev->fh); if (items > 1) { - if (ev->active) event_del (&ev->ev); - sv_setsv (ev->fh, new_fh); - ev->ev.ev_fd = sv_fileno (ev->fh); - ev->ev.ev_events &= ev->ev.ev_events & ~EV_SIGNAL; - if (ev->active) event_add (&ev->ev, e_tv (ev)); + int active = w->active; + if (active) evio_stop (w); + + evio_set (w, w->fd, new_events); + + if (active) evio_start (w); } +} OUTPUT: RETVAL -SV *signal (Event ev, SV *new_signal = 0) +MODULE = EV PACKAGE = EV::Signal PREFIX = evsignal_ + +void evsignal_start (struct ev_signal *w) + +void evsignal_stop (struct ev_signal *w) + +void set (struct ev_signal *w, SV *signal = 0) CODE: { - Signal signum; + Signal signum = sv_signum (signal); /* may croak here */ + int active = w->active; - if (items > 1) - signum = sv_signum (new_signal); /* may croak here */ + if (active) evsignal_stop (w); + evsignal_set (w, signum); + if (active) evsignal_start (w); +} - RETVAL = newSVsv (ev->fh); +MODULE = EV PACKAGE = EV::Time - if (items > 1) - { - if (ev->active) event_del (&ev->ev); - sv_setsv (ev->fh, new_signal); - ev->ev.ev_fd = signum; - ev->ev.ev_events |= EV_SIGNAL; - if (ev->active) event_add (&ev->ev, e_tv (ev)); - } -} - OUTPUT: - RETVAL +MODULE = EV PACKAGE = EV::Timer PREFIX = evtimer_ -short events (Event ev, short new_events = EV_UNDEF) - CODE: - RETVAL = ev->ev.ev_events; - if (items > 1) - { - if (ev->active) event_del (&ev->ev); - ev->ev.ev_events = new_events; - if (ev->active) event_add (&ev->ev, e_tv (ev)); - } - OUTPUT: - RETVAL +void evtimer_start (struct ev_timer *w) + INIT: + CHECK_REPEAT (w->repeat); -double timeout (Event ev, double new_timeout = 0., int repeat = 0) +void evtimer_stop (struct ev_timer *w) + +void evtimer_again (struct ev_timer *w) + INIT: + CHECK_REPEAT (w->repeat); + +void set (struct ev_timer *w, NV after, NV repeat = 0.) + INIT: + CHECK_REPEAT (repeat); CODE: - RETVAL = ev->timeout; - if (items > 1) - { - if (ev->active) event_del (&ev->ev); - ev->timeout = new_timeout; - ev->interval = repeat; - ev->abstime = 0; - if (ev->active) event_add (&ev->ev, e_tv (ev)); - } - OUTPUT: - RETVAL +{ + int active = w->active; + if (active) evtimer_stop (w); + evtimer_set (w, after, repeat); + if (active) evtimer_start (w); +} -void timeout_abs (Event ev, double at, double interval = 0.) +MODULE = EV PACKAGE = EV::Periodic PREFIX = evperiodic_ + +void evperiodic_start (struct ev_periodic *w) + INIT: + CHECK_REPEAT (w->interval); + +void evperiodic_stop (struct ev_periodic *w) + +void set (struct ev_periodic *w, NV at, NV interval = 0.) + INIT: + CHECK_REPEAT (interval); CODE: - if (ev->active) event_del (&ev->ev); - ev->timeout = at; - ev->interval = interval; - ev->abstime = 1; - if (ev->active) event_add (&ev->ev, e_tv (ev)); +{ + int active = w->active; + if (active) evperiodic_stop (w); + evperiodic_set (w, at, interval); + if (active) evperiodic_start (w); +} + +MODULE = EV PACKAGE = EV::Idle PREFIX = evidle_ + +void evidle_start (struct ev_idle *w) + +void evidle_stop (struct ev_idle *w) + +MODULE = EV PACKAGE = EV::Check PREFIX = evcheck_ +void evcheck_start (struct ev_check *w) + +void evcheck_stop (struct ev_check *w) + +#if 0 MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_ @@ -705,6 +644,8 @@ #void DESTROY (struct evhttp_request *req); +#endif +