--- Coro/Event/Event.xs 2006/12/01 19:41:06 1.13 +++ Coro/Event/Event.xs 2007/01/24 16:22:08 1.19 @@ -8,47 +8,65 @@ #include "EventAPI.h" #include "../Coro/CoroAPI.h" -#define CD_CORO 0 +#define CD_WAIT 0 /* wait queue */ #define CD_TYPE 1 #define CD_OK 2 -#define CD_PRIO 3 /* hardcoded in Coro::Event */ -#define CD_HITS 4 /* hardcoded in Coro::Event */ -#define CD_GOT 5 /* hardcoded in Coro::Event, Coro::Handle */ -#define CD_MAX 5 +#define CD_HITS 3 /* hardcoded in Coro::Event */ +#define CD_GOT 4 /* hardcoded in Coro::Event, Coro::Handle */ +#define CD_MAX 4 + +static HV *coro_event_event_stash; static void coro_std_cb (pe_event *pe) { AV *priv = (AV *)pe->ext_data; IV type = SvIV (AvARRAY (priv)[CD_TYPE]); - SV **cd_coro; + AV *cd_wait; + SV *coro; - SvIV_set (AvARRAY (priv)[CD_PRIO], pe->prio); SvIV_set (AvARRAY (priv)[CD_HITS], pe->hits); - - if (type == 1) - SvIV_set (AvARRAY (priv)[CD_GOT], ((pe_ioevent *)pe)->got); - - GEventAPI->stop (pe->up, 0); + SvIV_set (AvARRAY (priv)[CD_GOT], type ? ((pe_ioevent *)pe)->got : 0); AvARRAY (priv)[CD_OK] = &PL_sv_yes; - cd_coro = &AvARRAY(priv)[CD_CORO]; - if (*cd_coro != &PL_sv_undef) + cd_wait = (AV *)AvARRAY(priv)[CD_WAIT]; + + coro = av_shift (cd_wait); + if (coro != &PL_sv_undef) { - AvARRAY (priv)[CD_OK] = &PL_sv_yes; - CORO_READY (*cd_coro); - SvREFCNT_dec (*cd_coro); - *cd_coro = &PL_sv_undef; + CORO_READY (coro); + SvREFCNT_dec (coro); } + + if (av_len (cd_wait) < 0) + GEventAPI->stop (pe->up, 0); } static void asynccheck_hook (void *data) { - /* ceding from C means allocating a stack, but we assume this is a rare case */ - while (CORO_NREADY) - CORO_CEDE; + /* this loops as long as we have _other_ coros with the same or higher priority */ + while (CORO_NREADY && CORO_CEDE) + ; +} + +static double +prepare_hook (void *data) +{ + /* this yields once to another coro with any priority */ + if (CORO_NREADY) + { + CORO_CEDE_NOTSELF; + /* + * timers might have changed, and Event fails to notice this + * so we have to assume the worst. If Event didn't have that bug, + * we would only need to do this if CORO_NREADY is != 0 now. + */ + return 0.; + } + else + return 85197.73; /* this is as good as any value, but it factors badly with common values */ } MODULE = Coro::Event PACKAGE = Coro::Event @@ -57,10 +75,13 @@ BOOT: { + coro_event_event_stash = gv_stashpv ("Coro::Event::Event", TRUE); + I_EVENT_API ("Coro::Event"); I_CORO_API ("Coro::Event"); GEventAPI->add_hook ("asynccheck", (void *)asynccheck_hook, 0); + GEventAPI->add_hook ("prepare", (void *)prepare_hook, 0); } void @@ -74,13 +95,11 @@ { AV *priv = newAV (); - SV *rv = newRV_noinc ((SV *)priv); - av_extend (priv, CD_MAX); - AvARRAY (priv)[CD_CORO] = &PL_sv_undef; + av_fill (priv, CD_MAX); + AvARRAY (priv)[CD_WAIT] = (SV *)newAV (); /* AV in AV _should_ not be exposed to perl */ AvARRAY (priv)[CD_TYPE] = newSViv (type); AvARRAY (priv)[CD_OK ] = &PL_sv_no; - AvARRAY (priv)[CD_PRIO] = newSViv (0); AvARRAY (priv)[CD_HITS] = newSViv (0); AvARRAY (priv)[CD_GOT ] = newSViv (0); SvREADONLY_on (priv); @@ -90,7 +109,7 @@ /* make sure Event does not use PERL_MAGIC_uvar, which */ /* we abuse for non-uvar purposes. */ - sv_magicext (SvRV (self), rv, PERL_MAGIC_uvar, 0, 0, 0); + sv_magicext (SvRV (self), newRV_noinc ((SV *)priv), PERL_MAGIC_uvar, 0, 0, 0); } } @@ -107,14 +126,36 @@ XSRETURN_NO; /* got an event */ } + av_push ((AV *)AvARRAY (priv)[CD_WAIT], SvREFCNT_inc (CORO_CURRENT)); + if (!w->running) GEventAPI->start (w, 1); - if (AvARRAY (priv)[CD_CORO] == &PL_sv_undef) - AvARRAY (priv)[CD_CORO] = SvREFCNT_inc (CORO_CURRENT); - else if (AvARRAY (priv)[CD_CORO] != CORO_CURRENT) - croak ("Coro::Event::next can only be called from a single coroutine at a time, caught"); - XSRETURN_YES; /* schedule */ } +SV * +_event (SV *self) + CODE: +{ + if (GIMME_V == G_VOID) + XSRETURN_EMPTY; + + { + pe_watcher *w = GEventAPI->sv_2watcher (self); + AV *priv = (AV *)w->ext_data; + + RETVAL = newRV_inc ((SV *)priv); + + /* may need to bless it now */ + if (!SvOBJECT (priv)) + { + SvREADONLY_off ((SV *)priv); + sv_bless (RETVAL, coro_event_event_stash); + SvREADONLY_on ((SV *)priv); + } + } +} + OUTPUT: + RETVAL +