--- Coro/Event/Event.xs 2001/08/18 02:58:38 1.6 +++ Coro/Event/Event.xs 2006/11/23 23:54:02 1.10 @@ -11,7 +11,7 @@ dSP; PUSHMARK(SP); - XPUSHs (sv_2mortal(newSVpv("only one coroutine can wait for an event",0))); + XPUSHs (sv_2mortal(newSVpv("only one coroutine can wait for an event at any given time",0))); PUTBACK; call_pv ("Carp::confess", G_VOID); } @@ -19,6 +19,10 @@ #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_OK 2 @@ -29,15 +33,6 @@ #define EV_CLASS "Coro::Event" -static pe_idle *scheduler; -static int do_schedule; - -#define NEED_SCHEDULE if (!do_schedule) \ - { \ - do_schedule = 1; \ - GEventAPI->now ((pe_watcher *)scheduler); \ - } - static void coro_std_cb(pe_event *pe) { @@ -56,7 +51,6 @@ CORO_READY (*cd_coro); SvREFCNT_dec (*cd_coro); *cd_coro = &PL_sv_undef; - NEED_SCHEDULE; } else { @@ -65,13 +59,13 @@ } } -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 @@ -80,16 +74,10 @@ BOOT: { - I_EVENT_API("Coro::Event"); + I_EVENT_API ("Coro::Event"); I_CORO_API ("Coro::Event"); - /* create a fake idle handler (we only ever call now) */ - scheduler = GEventAPI->new_idle (0, 0); - scheduler->base.callback = scheduler_cb; - scheduler->base.prio = PE_PRIO_NORMAL; /* StarvePrio */ - 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 @@ -98,24 +86,31 @@ int type CODE: pe_watcher *w = GEventAPI->sv_2watcher (self); - 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); + 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); + + GEventAPI->start (w, 0); + } void _next(self) @@ -141,14 +136,3 @@ XSRETURN_YES; } -MODULE = Coro::Event PACKAGE = Coro - -# overwrite the ready function -void -ready(self) - SV * self - CODE: - NEED_SCHEDULE; - CORO_READY (self); - -