--- Coro/Coro/State.xs 2009/06/29 04:30:25 1.358 +++ Coro/Coro/State.xs 2009/08/13 02:35:41 1.368 @@ -8,7 +8,7 @@ #include "XSUB.h" #include "perliol.h" -#include "patchlevel.h" +#include "schmorp.h" #include #include @@ -53,65 +53,6 @@ /* the maximum number of idle cctx that will be pooled */ static int cctx_max_idle = 4; -#define PERL_VERSION_ATLEAST(a,b,c) \ - (PERL_REVISION > (a) \ - || (PERL_REVISION == (a) \ - && (PERL_VERSION > (b) \ - || (PERL_VERSION == (b) && PERL_SUBVERSION >= (c))))) - -#if !PERL_VERSION_ATLEAST (5,6,0) -# ifndef PL_ppaddr -# define PL_ppaddr ppaddr -# endif -# ifndef call_sv -# define call_sv perl_call_sv -# endif -# ifndef get_sv -# define get_sv perl_get_sv -# endif -# ifndef get_cv -# define get_cv perl_get_cv -# endif -# ifndef IS_PADGV -# define IS_PADGV(v) 0 -# endif -# ifndef IS_PADCONST -# define IS_PADCONST(v) 0 -# endif -#endif - -/* 5.11 */ -#ifndef CxHASARGS -# define CxHASARGS(cx) (cx)->blk_sub.hasargs -#endif - -/* 5.10.0 */ -#ifndef SvREFCNT_inc_NN -# define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv) -#endif - -/* 5.8.8 */ -#ifndef GV_NOTQUAL -# define GV_NOTQUAL 0 -#endif -#ifndef newSV -# define newSV(l) NEWSV(0,l) -#endif -#ifndef CvISXSUB_on -# define CvISXSUB_on(cv) (void)cv -#endif -#ifndef CvISXSUB -# define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE) -#endif -#ifndef Newx -# define Newx(ptr,nitems,type) New (0,ptr,nitems,type) -#endif - -/* 5.8.7 */ -#ifndef SvRV_set -# define SvRV_set(s,v) SvRV(s) = (v) -#endif - #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 # undef CORO_STACKGUARD #endif @@ -172,6 +113,7 @@ #endif static double (*nvtime)(); /* so why doesn't it take void? */ +static void (*u2time)(pTHX_ UV ret[2]); /* we hijack an hopefully unused CV flag for our purposes */ #define CVf_SLF 0x4000 @@ -207,6 +149,12 @@ /* Coro::AnyEvent */ static SV *sv_activity; +/* enable processtime/realtime profiling */ +static char enable_times; +typedef U32 coro_ts[2]; +static coro_ts time_real, time_cpu; +static char times_valid; + static struct coro_cctx *cctx_first; static int cctx_count, cctx_idle; @@ -305,6 +253,9 @@ AV *on_enter; AV *on_leave; + /* times */ + coro_ts t_cpu, t_real; + /* linked list */ struct coro *next, *prev; }; @@ -334,6 +285,26 @@ static struct coro *coro_first; #define coro_nready coroapi.nready +/** Coro::Select ************************************************************/ + +static OP *(*coro_old_pp_sselect) (pTHX); +static SV *coro_select_select; + +/* horrible hack, but if it works... */ +static OP * +coro_pp_sselect (aTHX) +{ + dSP; + PUSHMARK (SP - 4); /* fake argument list */ + XPUSHs (coro_select_select); + PUTBACK; + + /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */ + PL_op->op_flags |= OPf_STACKED; + PL_op->op_private = 0; + return PL_ppaddr [OP_ENTERSUB](aTHX); +} + /** lowlevel stuff **********************************************************/ static SV * @@ -366,18 +337,51 @@ return get_hv (name, create); } -/* may croak */ -INLINE CV * -coro_sv_2cv (pTHX_ SV *sv) -{ - HV *st; - GV *gvp; - CV *cv = sv_2cv (sv, &st, &gvp, 0); +INLINE void +coro_times_update () +{ +#ifdef coro_clock_gettime + struct timespec ts; - if (!cv) - croak ("code reference expected"); + ts.tv_sec = ts.tv_nsec = 0; + coro_clock_gettime (CORO_CLOCK_THREAD_CPUTIME_ID, &ts); + time_cpu [0] = ts.tv_sec; time_cpu [1] = ts.tv_nsec; + + ts.tv_sec = ts.tv_nsec = 0; + coro_clock_gettime (CORO_CLOCK_MONOTONIC, &ts); + time_real [0] = ts.tv_sec; time_real [1] = ts.tv_nsec; +#else + dTHX; + UV tv[2]; - return cv; + u2time (aTHX_ tv); + time_real [0] = tv [0]; + time_real [1] = tv [1] * 1000; +#endif +} + +INLINE void +coro_times_add (struct coro *c) +{ + c->t_real [1] += time_real [1]; + if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; } + c->t_real [0] += time_real [0]; + + c->t_cpu [1] += time_cpu [1]; + if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; } + c->t_cpu [0] += time_cpu [0]; +} + +INLINE void +coro_times_sub (struct coro *c) +{ + if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; } + c->t_real [1] -= time_real [1]; + c->t_real [0] -= time_real [0]; + + if (c->t_cpu [1] < time_cpu [1]) { c->t_cpu [1] += 1000000000; --c->t_cpu [0]; } + c->t_cpu [1] -= time_cpu [1]; + c->t_cpu [0] -= time_cpu [0]; } /*****************************************************************************/ @@ -490,7 +494,7 @@ /* perl manages to free our internal AV and _then_ call us */ if (IN_DESTRUCT) - return; + return 0; /* casting is fun. */ while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) @@ -589,6 +593,14 @@ slf_frame = c->slf_frame; CORO_THROW = c->except; + if (expect_false (enable_times)) + { + if (expect_false (!times_valid)) + coro_times_update (); + + coro_times_sub (c); + } + if (expect_false (c->on_enter)) { int i; @@ -609,6 +621,14 @@ on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]); } + times_valid = 0; + + if (expect_false (enable_times)) + { + coro_times_update (); times_valid = 1; + coro_times_add (c); + } + c->except = CORO_THROW; c->slf_frame = slf_frame; @@ -665,7 +685,7 @@ if (SLOT_COUNT >= 2) CXINC; if (SLOT_COUNT >= 3) CXINC; { - int i; + unsigned int i; for (i = 3; i < SLOT_COUNT; ++i) CXINC; } @@ -980,6 +1000,12 @@ /* copy throw, in case it was set before coro_setup */ CORO_THROW = coro->except; + + if (expect_false (enable_times)) + { + coro_times_update (); + coro_times_sub (coro); + } } static void @@ -1035,7 +1061,7 @@ } { - int i; + unsigned int i; for (i = 0; i < sizeof (svf) / sizeof (*svf); ++i) SvREFCNT_dec (svf [i]); @@ -1251,7 +1277,8 @@ perl_run (PL_curinterp); /* * Unfortunately, there is no way to get at the return values of the - * coro body here, as perl_run destroys these + * coro body here, as perl_run destroys these. Likewise, we cannot catch + * runtime errors here, as this is just a random interpreter, not a thread. */ /* @@ -1610,29 +1637,6 @@ TRANSFER (ta, 1); } -/*****************************************************************************/ -/* gensub: simple closure generation utility */ - -#define GENSUB_ARG CvXSUBANY (cv).any_ptr - -/* create a closure from XS, returns a code reference */ -/* the arg can be accessed via GENSUB_ARG from the callback */ -/* the callback must use dXSARGS/XSRETURN */ -static SV * -gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg) -{ - CV *cv = (CV *)newSV (0); - - sv_upgrade ((SV *)cv, SVt_PVCV); - - CvANON_on (cv); - CvISXSUB_on (cv); - CvXSUB (cv) = xsub; - GENSUB_ARG = arg; - - return newRV_noinc ((SV *)cv); -} - /** Coro ********************************************************************/ INLINE void @@ -1667,43 +1671,38 @@ return 0; } +static void +invoke_sv_ready_hook_helper (void) +{ + dTHX; + dSP; + + ENTER; + SAVETMPS; + + PUSHMARK (SP); + PUTBACK; + call_sv (coro_readyhook, G_VOID | G_DISCARD); + + FREETMPS; + LEAVE; +} + static int api_ready (pTHX_ SV *coro_sv) { - struct coro *coro; - SV *sv_hook; - void (*xs_hook)(void); - - coro = SvSTATE (coro_sv); + struct coro *coro = SvSTATE (coro_sv); if (coro->flags & CF_READY) return 0; coro->flags |= CF_READY; - sv_hook = coro_nready ? 0 : coro_readyhook; - xs_hook = coro_nready ? 0 : coroapi.readyhook; - coro_enq (aTHX_ coro); - ++coro_nready; - - if (sv_hook) - { - dSP; - - ENTER; - SAVETMPS; - - PUSHMARK (SP); - PUTBACK; - call_sv (sv_hook, G_VOID | G_DISCARD); - - FREETMPS; - LEAVE; - } - if (xs_hook) - xs_hook (); + if (!coro_nready++) + if (coroapi.readyhook) + coroapi.readyhook (); return 1; } @@ -1919,9 +1918,14 @@ HV *hv = (HV *)SvRV (coro_current); AV *av = newAV (); - av_extend (av, items - 1); - for (i = 0; i < items; ++i) - av_push (av, SvREFCNT_inc_NN (arg [i])); + /* items are actually not so common, so optimise for this case */ + if (items) + { + av_extend (av, items - 1); + + for (i = 0; i < items; ++i) + av_push (av, SvREFCNT_inc_NN (arg [i])); + } hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); @@ -2021,7 +2025,7 @@ coro_rouse_callback (pTHX_ CV *cv) { dXSARGS; - SV *data = (SV *)GENSUB_ARG; + SV *data = (SV *)S_GENSUB_ARG; if (SvTYPE (SvRV (data)) != SVt_PVAV) { @@ -2097,8 +2101,8 @@ croak ("Coro::rouse_wait called with illegal callback argument,"); { - CV *cv = (CV *)SvRV (cb); /* for GENSUB_ARG */ - SV *data = (SV *)GENSUB_ARG; + CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */ + SV *data = (SV *)S_GENSUB_ARG; frame->data = (void *)data; frame->prepare = SvTYPE (SvRV (data)) == SVt_PVAV ? prepare_nop : prepare_schedule; @@ -2112,7 +2116,7 @@ HV *hv = (HV *)SvRV (coro_current); struct coro *coro = SvSTATE_hv (hv); SV *data = newRV_inc ((SV *)hv); - SV *cb = gensub (aTHX_ coro_rouse_callback, (void *)data); + SV *cb = s_gensub (aTHX_ coro_rouse_callback, (void *)data); sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0); SvREFCNT_dec (data); /* magicext increases the refcount */ @@ -2645,7 +2649,7 @@ { /* callback form */ AV *av = (AV *)SvRV (arg [0]); - CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); + SV *cb_cv = s_get_cv_croak (arg [1]); av_push (av, SvREFCNT_inc_NN (cb_cv)); @@ -2715,7 +2719,7 @@ if (items >= 2) { - CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); + SV *cb_cv = s_get_cv_croak (arg [1]); av_push (av, SvREFCNT_inc_NN (cb_cv)); if (SvIVX (AvARRAY (av)[0])) @@ -2760,7 +2764,7 @@ coro_aio_callback (pTHX_ CV *cv) { dXSARGS; - AV *state = (AV *)GENSUB_ARG; + AV *state = (AV *)S_GENSUB_ARG; SV *coro = av_pop (state); SV *data_sv = newSV (sizeof (struct io_state)); @@ -2884,7 +2888,7 @@ PUSHs (arg [i]); /* now push the callback closure */ - PUSHs (sv_2mortal (gensub (aTHX_ coro_aio_callback, (void *)SvREFCNT_inc_NN ((SV *)state)))); + PUSHs (sv_2mortal (s_gensub (aTHX_ coro_aio_callback, (void *)SvREFCNT_inc_NN ((SV *)state)))); /* now call the AIO function - we assume our request is uncancelable */ PUTBACK; @@ -2913,6 +2917,64 @@ # include "clone.c" #endif +/*****************************************************************************/ + +static SV * +coro_new (HV *stash, SV **argv, int argc, int is_coro) +{ + SV *coro_sv; + struct coro *coro; + MAGIC *mg; + HV *hv; + SV *cb; + int i; + + if (argc > 0) + { + cb = s_get_cv_croak (argv [0]); + + if (!is_coro) + { + if (CvISXSUB (cb)) + croak ("Coro::State doesn't support XS functions as coroutine start, caught"); + + if (!CvROOT (cb)) + croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught"); + } + } + + Newz (0, coro, 1, struct coro); + coro->args = newAV (); + coro->flags = CF_NEW; + + if (coro_first) coro_first->prev = coro; + coro->next = coro_first; + coro_first = coro; + + coro->hv = hv = newHV (); + mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); + mg->mg_flags |= MGf_DUP; + coro_sv = sv_bless (newRV_noinc ((SV *)hv), stash); + + if (argc > 0) + { + av_extend (coro->args, argc + is_coro - 1); + + if (is_coro) + { + av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb)); + cb = (SV *)cv_coro_run; + } + + coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb); + + for (i = 1; i < argc; i++) + av_push (coro->args, newSVsv (argv [i])); + } + + return coro_sv; +} + MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ PROTOTYPES: DISABLE @@ -2981,67 +3043,21 @@ if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); nvtime = INT2PTR (double (*)(), SvIV (*svp)); + + svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0); + u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp)); } assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); } SV * -new (char *klass, ...) +new (SV *klass, ...) ALIAS: Coro::new = 1 CODE: -{ - struct coro *coro; - MAGIC *mg; - HV *hv; - CV *cb; - int i; - - if (items > 1) - { - cb = coro_sv_2cv (aTHX_ ST (1)); - - if (!ix) - { - if (CvISXSUB (cb)) - croak ("Coro::State doesn't support XS functions as coroutine start, caught"); - - if (!CvROOT (cb)) - croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught"); - } - } - - Newz (0, coro, 1, struct coro); - coro->args = newAV (); - coro->flags = CF_NEW; - - if (coro_first) coro_first->prev = coro; - coro->next = coro_first; - coro_first = coro; - - coro->hv = hv = newHV (); - mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); - mg->mg_flags |= MGf_DUP; - RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); - - if (items > 1) - { - av_extend (coro->args, items - 1 + ix - 1); - - if (ix) - { - av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb)); - cb = cv_coro_run; - } - - coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb); - - for (i = 2; i < items; i++) - av_push (coro->args, newSVsv (ST (i))); - } -} - OUTPUT: + RETVAL = coro_new (ix ? coro_stash : coro_state_stash, &ST (1), items - 1, ix); + OUTPUT: RETVAL void @@ -3264,6 +3280,43 @@ coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */ +SV * +enable_times (int enabled = enable_times) + CODE: +{ + RETVAL = boolSV (enable_times); + + if (enabled != enable_times) + { + enable_times = enabled; + + coro_times_update (); + (enabled ? coro_times_sub : coro_times_add)(SvSTATE (coro_current)); + } +} + OUTPUT: + RETVAL + +void +times (Coro::State self) + PPCODE: +{ + struct coro *current = SvSTATE (coro_current); + + if (expect_false (current == self)) + { + coro_times_update (); + coro_times_add (SvSTATE (coro_current)); + } + + EXTEND (SP, 2); + PUSHs (sv_2mortal (newSVnv (self->t_real [0] + self->t_real [1] * 1e-9))); + PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9))); + + if (expect_false (current == self)) + coro_times_sub (SvSTATE (coro_current)); +} + MODULE = Coro::State PACKAGE = Coro BOOT: @@ -3310,6 +3363,15 @@ } } +SV * +async (...) + PROTOTYPE: &@ + CODE: + RETVAL = coro_new (coro_stash, &ST (0), items, 1); + api_ready (RETVAL); + OUTPUT: + RETVAL + void terminate (...) CODE: @@ -3353,7 +3415,16 @@ CODE: SvREFCNT_dec (coro_readyhook); SvGETMAGIC (hook); - coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; + if (SvOK (hook)) + { + coro_readyhook = newSVsv (hook); + CORO_READYHOOK = invoke_sv_ready_hook_helper; + } + else + { + coro_readyhook = 0; + CORO_READYHOOK = 0; + } int prio (Coro::State coro, int newprio = 0) @@ -3369,8 +3440,8 @@ if (ix) newprio = coro->prio - newprio; - if (newprio < PRIO_MIN) newprio = PRIO_MIN; - if (newprio > PRIO_MAX) newprio = PRIO_MAX; + if (newprio < CORO_PRIO_MIN) newprio = CORO_PRIO_MIN; + if (newprio > CORO_PRIO_MAX) newprio = CORO_PRIO_MAX; coro->prio = newprio; } @@ -3479,7 +3550,7 @@ struct coro *coro = SvSTATE_current; AV **avp = ix ? &coro->on_leave : &coro->on_enter; - block = (SV *)coro_sv_2cv (aTHX_ block); + block = s_get_cv_croak (block); if (!*avp) *avp = newAV (); @@ -3689,10 +3760,32 @@ _register (char *target, char *proto, SV *req) CODE: { - CV *req_cv = coro_sv_2cv (aTHX_ req); + SV *req_cv = s_get_cv_croak (req); /* newXSproto doesn't return the CV on 5.8 */ CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); sv_setpv ((SV *)slf_cv, proto); sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); } +MODULE = Coro::State PACKAGE = Coro::Select + +void +patch_pp_sselect () + CODE: + if (!coro_old_pp_sselect) + { + coro_select_select = (SV *)get_cv ("Coro::Select::select", 0); + coro_old_pp_sselect = PL_ppaddr [OP_SSELECT]; + PL_ppaddr [OP_SSELECT] = coro_pp_sselect; + } + +void +unpatch_pp_sselect () + CODE: + if (coro_old_pp_sselect) + { + PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; + coro_old_pp_sselect = 0; + } + +