--- cvsroot/EV/EV.xs 2008/10/28 08:08:28 1.116 +++ cvsroot/EV/EV.xs 2009/07/14 20:31:21 1.128 @@ -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,38 +33,30 @@ # include #endif -/* 5.10.0 */ -#ifndef SvREFCNT_inc_NN -# define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv) -#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 +#define WFLAG_UNREFED 2 /* has been unref'ed */ #define UNREF(w) \ - if (!((w)->e_flags & WFLAG_KEEPALIVE) \ - && !ev_is_active (w)) \ - ev_unref (e_loop (w)); + if (!((w)->e_flags & (WFLAG_KEEPALIVE | WFLAG_UNREFED)) \ + && ev_is_active (w)) \ + { \ + ev_unref (e_loop (w)); \ + (w)->e_flags |= WFLAG_UNREFED; \ + } #define REF(w) \ - if (!((w)->e_flags & WFLAG_KEEPALIVE) \ - && ev_is_active (w)) \ - ev_ref (e_loop (w)); + if ((w)->e_flags & WFLAG_UNREFED) \ + { \ + (w)->e_flags &= ~WFLAG_UNREFED; \ + ev_ref (e_loop (w)); \ + } #define START(type,w) \ do { \ - UNREF (w); \ ev_ ## type ## _start (e_loop (w), w); \ + UNREF (w); \ } while (0) #define STOP(type,w) \ @@ -96,70 +95,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); @@ -216,6 +160,11 @@ I32 mark = SP - PL_stack_base; SV *sv_self, *sv_events; + /* libev might have stopped the watcher */ + if (expect_false (w->e_flags & WFLAG_UNREFED) + && !ev_is_active (w)) + REF (w); + if (expect_true (sv_self_cache)) { sv_self = sv_self_cache; sv_self_cache = 0; @@ -246,7 +195,7 @@ PUTBACK; call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL); - if (expect_false (sv_self_cache)) + if (expect_false (SvREFCNT (sv_self) != 1 || sv_self_cache)) SvREFCNT_dec (sv_self); else { @@ -255,12 +204,12 @@ sv_self_cache = sv_self; } - if (expect_false (sv_events_cache)) + if (expect_false (SvREFCNT (sv_events) != 1 || sv_events_cache)) SvREFCNT_dec (sv_events); else sv_events_cache = sv_events; - if (SvTRUE (ERRSV)) + if (expect_false (SvTRUE (ERRSV))) { SPAGAIN; PUSHMARK (SP); @@ -385,16 +334,28 @@ const_iv (EV_, UNDEF) const_iv (EV_, NONE) - const_iv (EV_, TIMEOUT) const_iv (EV_, READ) const_iv (EV_, WRITE) + const_iv (EV_, IO) + const_iv (EV_, TIMEOUT) + const_iv (EV_, TIMER) + const_iv (EV_, PERIODIC) const_iv (EV_, SIGNAL) + const_iv (EV_, CHILD) + const_iv (EV_, STAT) const_iv (EV_, IDLE) + const_iv (EV_, PREPARE) const_iv (EV_, CHECK) + const_iv (EV_, EMBED) + const_iv (EV_, FORK) + const_iv (EV_, ASYNC) + const_iv (EV_, CUSTOM) const_iv (EV_, ERROR) - const_iv (EV, LOOP_ONESHOT) const_iv (EV, LOOP_NONBLOCK) + const_iv (EV, LOOP_ONESHOT) + + const_iv (EV, UNLOOP_CANCEL) const_iv (EV, UNLOOP_ONE) const_iv (EV, UNLOOP_ALL) @@ -407,6 +368,9 @@ const_iv (EV, FLAG_AUTO) const_iv (EV, FLAG_NOENV) const_iv (EV, FLAG_FORKCHECK) + + const_iv (EV_, VERSION_MAJOR) + const_iv (EV_, VERSION_MINOR) }; for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) @@ -435,7 +399,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 (); @@ -445,10 +409,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; @@ -522,6 +495,8 @@ unsigned int ev_embeddable_backends () +void ev_sleep (NV interval) + NV ev_time () NV ev_now () @@ -530,12 +505,24 @@ void ev_now_update () C_ARGS: evapi.default_loop +void ev_suspend () + C_ARGS: evapi.default_loop + +void ev_resume () + C_ARGS: evapi.default_loop + 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 @@ -554,7 +541,7 @@ 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); @@ -565,7 +552,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, default_loop_sv); @@ -610,7 +597,7 @@ 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); @@ -711,7 +698,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) @@ -742,8 +729,8 @@ if (items > 1 && ((new_value ^ w->e_flags) & WFLAG_KEEPALIVE)) { - REF (w); w->e_flags = (w->e_flags & ~WFLAG_KEEPALIVE) | new_value; + REF (w); UNREF (w); } } @@ -755,7 +742,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); } @@ -835,7 +822,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); @@ -847,7 +834,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; @@ -890,7 +877,7 @@ 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)); @@ -903,7 +890,7 @@ 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)); @@ -928,7 +915,6 @@ INIT: CHECK_REPEAT (w->repeat); CODE: - REF (w); ev_timer_again (e_loop (w), w); UNREF (w); @@ -957,7 +943,6 @@ void ev_periodic_again (ev_periodic *w) CODE: - REF (w); ev_periodic_again (e_loop (w), w); UNREF (w); @@ -1250,6 +1235,10 @@ void ev_now_update (struct ev_loop *loop) +void ev_suspend (struct ev_loop *loop) + +void ev_resume (struct ev_loop *loop) + void ev_set_io_collect_interval (struct ev_loop *loop, NV interval) void ev_set_timeout_collect_interval (struct ev_loop *loop, NV interval) @@ -1258,6 +1247,8 @@ 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) @@ -1269,7 +1260,7 @@ 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); @@ -1282,7 +1273,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)); @@ -1329,7 +1320,7 @@ 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)); @@ -1432,7 +1423,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)