--- Coro/Event/Event.xs 2001/08/17 01:45:39 1.3 +++ Coro/Event/Event.xs 2006/11/23 23:54:02 1.10 @@ -4,55 +4,68 @@ #include +/* this useful idiom is unfortunately missing... */ +static void +confess (const char *msg) +{ + dSP; + + PUSHMARK(SP); + XPUSHs (sv_2mortal(newSVpv("only one coroutine can wait for an event at any given time",0))); + PUTBACK; + call_pv ("Carp::confess", G_VOID); +} + #include "EventAPI.h" #include "../Coro/CoroAPI.h" +#ifndef PE_PERLCB +# define PE_PERLCB 0x020 /* not public, but we need it :( */ +#endif + #define CD_CORO 0 #define CD_TYPE 1 -#define CD_W 2 -#define CD_GOT 3 -#define CD_MAX 3 -/* no support for hits and prio so far. */ - -#define EV_CLASS "Coro::Event::Ev" - -static HV *ev_stash; -static pe_idle *scheduler; -static int do_schedule; - -#define NEED_SCHEDULE if (!do_schedule) \ - { \ - do_schedule = 1; \ - GEventAPI->now ((pe_watcher *)scheduler); \ - } +#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 EV_CLASS "Coro::Event" + static void coro_std_cb(pe_event *pe) { - AV *av = (AV *)SvRV ((SV *)pe->ext_data); - IV type = SvIV (*av_fetch (av, CD_TYPE, 1)); - SV *cd_coro = *av_fetch (av, CD_CORO, 1); + AV *priv = (AV *)pe->ext_data; + IV type = SvIV (*av_fetch (priv, CD_TYPE, 1)); + SV **cd_coro = &AvARRAY(priv)[CD_CORO]; - av_store (av, CD_W, SvREFCNT_inc (pe->up->mysv)); + sv_setiv (AvARRAY(priv)[CD_PRIO], pe->prio); + sv_setiv (AvARRAY(priv)[CD_HITS], pe->hits); if (type == 1) - av_store (av, CD_GOT, newSViv (((pe_ioevent *)pe)->got)); - - GEventAPI->stop (pe->up, 0); + sv_setiv (AvARRAY(priv)[CD_GOT], ((pe_ioevent *)pe)->got); - if (SvROK (cd_coro)) + if (*cd_coro != &PL_sv_undef) + { + CORO_READY (*cd_coro); + SvREFCNT_dec (*cd_coro); + *cd_coro = &PL_sv_undef; + } + else { - CORO_READY (cd_coro); - NEED_SCHEDULE; + AvARRAY(priv)[CD_OK] = &PL_sv_yes; + GEventAPI->stop (pe->up, 0); } } -static void -scheduler_cb(pe_event *pe) +static double +prepare_hook (void *data) { while (CORO_NREADY) CORO_CEDE; - do_schedule = 0; + return 1e20; } MODULE = Coro::Event PACKAGE = Coro::Event @@ -61,17 +74,10 @@ BOOT: { - I_EVENT_API("Coro::Event"); + I_EVENT_API ("Coro::Event"); I_CORO_API ("Coro::Event"); - ev_stash = gv_stashpv (EV_CLASS, TRUE); - - /* create a fake idle handler (we only ever call now) */ - scheduler = GEventAPI->new_idle (0, 0); - scheduler->base.callback = scheduler_cb; - scheduler->min_interval = newSVnv (0); - scheduler->max_interval = newSVnv (0); - GEventAPI->stop ((pe_watcher *)scheduler, 0); + GEventAPI->add_hook ("prepare", (void *)prepare_hook, 0); } void @@ -80,45 +86,53 @@ int type CODE: pe_watcher *w = GEventAPI->sv_2watcher (self); - AV *priv = newAV (); - SV *rv; - - av_extend (priv, CD_MAX); - av_store (priv, CD_TYPE, newSViv (type)); - rv = sv_bless (newRV_noinc ((SV *)priv), ev_stash); + if (WaFLAGS (w) & PE_PERLCB) + croak ("Coro::Event watchers must not have a perl callback (see Coro::Event), caught"); + { + AV *priv = newAV (); + SV *rv = newRV_noinc ((SV *)priv); + + av_extend (priv, CD_MAX); + av_store (priv, CD_CORO, &PL_sv_undef); + av_store (priv, CD_TYPE, newSViv (type)); + av_store (priv, CD_OK , &PL_sv_no); + av_store (priv, CD_PRIO, newSViv (0)); + av_store (priv, CD_HITS, newSViv (0)); + av_store (priv, CD_GOT , type ? newSViv (0) : &PL_sv_undef); + SvREADONLY_on (priv); + + w->callback = coro_std_cb; + w->ext_data = priv; + + hv_store ((HV *)SvRV (self), + EV_CLASS, strlen (EV_CLASS), + rv, 0); - hv_store ((HV *)SvRV (self), - EV_CLASS, strlen (EV_CLASS), - rv, 0); - - w->ext_data = rv; - w->callback = coro_std_cb; + GEventAPI->start (w, 0); + } void -_next0(self) - SV * self - CODE: - pe_watcher *w = GEventAPI->sv_2watcher (self); - AV *priv = (AV *)SvRV ((SV *)w->ext_data); - - GEventAPI->start (w, 1); - - if (SvROK (*av_fetch (priv, CD_CORO, 1))) - croak ("only one coroutine can wait for an event"); - - av_store (priv, CD_CORO, SvREFCNT_inc (CORO_CURRENT)); - -SV * -_next1(self) +_next(self) SV * self CODE: pe_watcher *w = GEventAPI->sv_2watcher (self); - AV *priv = (AV *)SvRV ((SV *)w->ext_data); + AV *priv = (AV *)w->ext_data; - av_store (priv, CD_CORO, &PL_sv_undef); + if (!w->running) + GEventAPI->start (w, 1); - RETVAL = SvREFCNT_inc ((SV *)w->ext_data); - OUTPUT: - RETVAL + if (AvARRAY(priv)[CD_OK] == &PL_sv_yes) + { + AvARRAY(priv)[CD_OK] = &PL_sv_no; + XSRETURN_NO; + } + else + { + if (AvARRAY(priv)[CD_CORO] != &PL_sv_undef) + confess ("only one coroutine can wait for an event"); + + AvARRAY(priv)[CD_CORO] = SvREFCNT_inc (CORO_CURRENT); + XSRETURN_YES; + }