--- cvsroot/EV/EV.xs 2009/04/25 14:12:48 1.123 +++ cvsroot/EV/EV.xs 2009/09/18 21:28:33 1.134 @@ -2,12 +2,19 @@ #include "perl.h" #include "XSUB.h" -/*#include */ - /* fix perl api breakage */ #undef signal #undef sigaction +#include "schmorp.h" + +/* old API compatibility */ +static int +sv_fileno (SV *fh) +{ + return s_fileno (fh, 0); +} + #define EV_PROTOTYPES 1 #define EV_USE_NANOSLEEP EV_USE_MONOTONIC #define EV_H @@ -26,25 +33,6 @@ # include #endif -/* 5.10.0 */ -#ifndef SvREFCNT_inc_NN -# define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv) -#endif - -/* 5.6.x */ -#ifndef SvRV_set -# define SvRV_set(a,b) SvRV ((a)) = (b) -#endif - -#if __GNUC__ >= 3 -# define expect(expr,value) __builtin_expect ((expr),(value)) -#else -# define expect(expr,value) (expr) -#endif - -#define expect_false(expr) expect ((expr) != 0, 0) -#define expect_true(expr) expect ((expr) != 0, 1) - #define e_loop(w) INT2PTR (struct ev_loop *, SvIVX ((w)->loop)) #define WFLAG_KEEPALIVE 1 @@ -78,15 +66,38 @@ } while (0) #define RESET(type,w,seta) \ - do { \ - int active = ev_is_active (w); \ - if (active) STOP (type, w); \ - ev_ ## type ## _set seta; \ - if (active) START (type, w); \ - } while (0) + do { \ + int active = ev_is_active (w); \ + if (active) STOP (type, w); \ + ev_ ## type ## _set seta; \ + if (active) START (type, w); \ + } while (0) typedef int Signal; +/* horrible... */ +#define CHECK_SIGNAL_CAN_START(w) \ + do { \ + /* dive into the internals of libev to avoid aborting in libev */ \ + if (signals [(w)->signum - 1].loop \ + && signals [(w)->signum - 1].loop != e_loop (w)) \ + croak ("unable to start signal watcher, signal %d already registered in another loop", w->signum); \ + } while (0) + +#define START_SIGNAL(w) \ + do { \ + CHECK_SIGNAL_CAN_START (w); \ + START (signal, w); \ + } while (0) \ + +#define RESET_SIGNAL(w,seta) \ + do { \ + int active = ev_is_active (w); \ + if (active) STOP (signal, w); \ + ev_ ## signal ## _set seta; \ + if (active) START_SIGNAL (w); \ + } while (0) + static SV *default_loop_sv; static struct EVAPI evapi; @@ -107,70 +118,15 @@ *stash_fork, *stash_async; -#ifndef SIG_SIZE -/* kudos to Slaven Rezic for the idea */ -static char sig_size [] = { SIG_NUM }; -# define SIG_SIZE (sizeof (sig_size) + 1) -#endif - -static Signal -sv_signum (SV *sig) -{ - Signal signum; - - SvGETMAGIC (sig); - - for (signum = 1; signum < SIG_SIZE; ++signum) - if (strEQ (SvPV_nolen (sig), PL_sig_name [signum])) - return signum; - - signum = SvIV (sig); - - if (signum > 0 && signum < SIG_SIZE) - return signum; - - return -1; -} - ///////////////////////////////////////////////////////////////////////////// // Event static void e_cb (EV_P_ ev_watcher *w, int revents); -static int -sv_fileno (SV *fh) -{ - SvGETMAGIC (fh); - - if (SvROK (fh)) - fh = SvRV (fh); - - if (SvTYPE (fh) == SVt_PVGV) - return PerlIO_fileno (IoIFP (sv_2io (fh))); - - if (SvOK (fh) && (SvIV (fh) >= 0) && (SvIV (fh) < 0x7fffffffL)) - return SvIV (fh); - - return -1; -} - -static SV * -e_get_cv (SV *cb_sv) -{ - HV *st; - GV *gvp; - CV *cv = sv_2cv (cb_sv, &st, &gvp, 0); - - if (!cv) - croak ("EV watcher callback must be a CODE reference"); - - return (SV *)cv; -} - static void * e_new (int size, SV *cb_sv, SV *loop) { - SV *cv = cb_sv ? e_get_cv (cb_sv) : 0; + SV *cv = cb_sv ? s_get_cv_croak (cb_sv) : 0; ev_watcher *w; SV *self = NEWSV (0, size); SvPOK_only (self); @@ -432,9 +388,12 @@ const_iv (EV, BACKEND_KQUEUE) const_iv (EV, BACKEND_DEVPOLL) const_iv (EV, BACKEND_PORT) + const_iv (EV, BACKEND_ALL) const_iv (EV, FLAG_AUTO) - const_iv (EV, FLAG_NOENV) const_iv (EV, FLAG_FORKCHECK) + const_iv (EV, FLAG_NOENV) + const_iv (EV, FLAG_NOSIGFD) + const_iv (EV, FLAG_NOINOTIFY) const_iv (EV_, VERSION_MAJOR) const_iv (EV_, VERSION_MINOR) @@ -466,7 +425,7 @@ evapi.ver = EV_API_VERSION; evapi.rev = EV_API_REVISION; evapi.sv_fileno = sv_fileno; - evapi.sv_signum = sv_signum; + evapi.sv_signum = s_signum; evapi.supported_backends = ev_supported_backends (); evapi.recommended_backends = ev_recommended_backends (); evapi.embeddable_backends = ev_embeddable_backends (); @@ -476,12 +435,19 @@ evapi.loop_destroy = ev_loop_destroy; evapi.loop_fork = ev_loop_fork; evapi.loop_count = ev_loop_count; + evapi.loop_depth = ev_loop_depth; + evapi.set_userdata = ev_set_userdata; + evapi.userdata = ev_userdata; evapi.now = ev_now; evapi.now_update = ev_now_update; evapi.suspend = ev_suspend; evapi.resume = ev_resume; evapi.backend = ev_backend; evapi.unloop = ev_unloop; + evapi.invoke_pending = ev_invoke_pending; + evapi.pending_count = ev_pending_count; + evapi.set_loop_release_cb = ev_set_loop_release_cb; + evapi.set_invoke_pending_cb= ev_set_invoke_pending_cb; evapi.ref = ev_ref; evapi.unref = ev_unref; evapi.loop = ev_loop; @@ -491,6 +457,7 @@ evapi.timer_start = ev_timer_start; evapi.timer_stop = ev_timer_stop; evapi.timer_again = ev_timer_again; + evapi.timer_remaining = ev_timer_remaining; evapi.periodic_start = ev_periodic_start; evapi.periodic_stop = ev_periodic_stop; evapi.signal_start = ev_signal_start; @@ -574,9 +541,15 @@ unsigned int ev_backend () C_ARGS: evapi.default_loop +void ev_loop_verify () + C_ARGS: evapi.default_loop + unsigned int ev_loop_count () C_ARGS: evapi.default_loop +unsigned int ev_loop_depth () + C_ARGS: evapi.default_loop + void ev_set_io_collect_interval (NV interval) C_ARGS: evapi.default_loop, interval @@ -595,20 +568,33 @@ void ev_feed_signal_event (SV *signal) CODE: { - Signal signum = sv_signum (signal); + Signal signum = s_signum (signal); CHECK_SIG (signal, signum); ev_feed_signal_event (evapi.default_loop, signum); } +unsigned int ev_pending_count () + C_ARGS: evapi.default_loop + +void ev_invoke_pending () + C_ARGS: evapi.default_loop + ev_io *io (SV *fh, int events, SV *cb) ALIAS: io_ns = 1 + _ae_io = 2 CODE: { - int fd = sv_fileno (fh); + int fd = s_fileno (fh, events & EV_WRITE); CHECK_FD (fh, fd); + if (ix == 2) + { + ix = 0; + events = events ? EV_WRITE : EV_READ; + } + RETVAL = e_new (sizeof (ev_io), cb, default_loop_sv); RETVAL->fh = newSVsv (fh); ev_io_set (RETVAL, fd, events); @@ -651,12 +637,12 @@ signal_ns = 1 CODE: { - Signal signum = sv_signum (signal); + Signal signum = s_signum (signal); CHECK_SIG (signal, signum); RETVAL = e_new (sizeof (ev_signal), cb, default_loop_sv); ev_signal_set (RETVAL, signum); - if (!ix) START (signal, RETVAL); + if (!ix) START_SIGNAL (RETVAL); } OUTPUT: RETVAL @@ -752,7 +738,7 @@ CODE: ev_once ( evapi.default_loop, - sv_fileno (fh), events, + s_fileno (fh, events & EV_WRITE), events, SvOK (timeout) ? SvNV (timeout) : -1., e_once_cb, newSVsv (cb) @@ -796,7 +782,7 @@ { if (items > 1) { - new_cb = e_get_cv (new_cb); + new_cb = s_get_cv_croak (new_cb); RETVAL = newRV_noinc (w->cb_sv); w->cb_sv = SvREFCNT_inc (new_cb); } @@ -876,7 +862,7 @@ void set (ev_io *w, SV *fh, int events) CODE: { - int fd = sv_fileno (fh); + int fd = s_fileno (fh, events & EV_WRITE); CHECK_FD (fh, fd); sv_setsv (w->fh, fh); @@ -888,7 +874,7 @@ { if (items > 1) { - int fd = sv_fileno (new_fh); + int fd = s_fileno (new_fh, w->events & EV_WRITE); CHECK_FD (new_fh, fd); RETVAL = w->fh; @@ -917,7 +903,7 @@ void ev_signal_start (ev_signal *w) CODE: - START (signal, w); + START_SIGNAL (w); void ev_signal_stop (ev_signal *w) CODE: @@ -931,10 +917,10 @@ void set (ev_signal *w, SV *signal) CODE: { - Signal signum = sv_signum (signal); + Signal signum = s_signum (signal); CHECK_SIG (signal, signum); - RESET (signal, w, (w, signum)); + RESET_SIGNAL (w, (w, signum)); } int signal (ev_signal *w, SV *new_signal = 0) @@ -944,10 +930,10 @@ if (items > 1) { - Signal signum = sv_signum (new_signal); + Signal signum = s_signum (new_signal); CHECK_SIG (new_signal, signum); - RESET (signal, w, (w, signum)); + RESET_SIGNAL (w, (w, signum)); } } OUTPUT: @@ -972,6 +958,9 @@ ev_timer_again (e_loop (w), w); UNREF (w); +NV ev_timer_remaining (ev_timer *w) + C_ARGS: e_loop (w), w + void DESTROY (ev_timer *w) CODE: STOP (timer, w); @@ -1301,18 +1290,24 @@ unsigned int ev_loop_count (struct ev_loop *loop) +unsigned int ev_loop_depth (struct ev_loop *loop) + void ev_loop (struct ev_loop *loop, int flags = 0) void ev_unloop (struct ev_loop *loop, int how = 1) void ev_feed_fd_event (struct ev_loop *loop, int fd, int revents = EV_NONE) +unsigned int ev_pending_count (struct ev_loop *loop) + +void ev_invoke_pending (struct ev_loop *loop) + #if 0 void ev_feed_signal_event (struct ev_loop *loop, SV *signal) CODE: { - Signal signum = sv_signum (signal); + Signal signum = s_signum (signal); CHECK_SIG (signal, signum); ev_feed_signal_event (loop, signum); @@ -1325,7 +1320,7 @@ io_ns = 1 CODE: { - int fd = sv_fileno (fh); + int fd = s_fileno (fh, events & EV_WRITE); CHECK_FD (fh, fd); RETVAL = e_new (sizeof (ev_io), cb, ST (0)); @@ -1365,25 +1360,21 @@ OUTPUT: RETVAL -#if 0 - ev_signal *signal (struct ev_loop *loop, SV *signal, SV *cb) ALIAS: signal_ns = 1 CODE: { - Signal signum = sv_signum (signal); + Signal signum = s_signum (signal); CHECK_SIG (signal, signum); RETVAL = e_new (sizeof (ev_signal), cb, ST (0)); ev_signal_set (RETVAL, signum); - if (!ix) START (signal, RETVAL); + if (!ix) START_SIGNAL (RETVAL); } OUTPUT: RETVAL -#endif - ev_idle *idle (struct ev_loop *loop, SV *cb) ALIAS: idle_ns = 1 @@ -1475,7 +1466,7 @@ CODE: ev_once ( loop, - sv_fileno (fh), events, + s_fileno (fh, events & EV_WRITE), events, SvOK (timeout) ? SvNV (timeout) : -1., e_once_cb, newSVsv (cb)