--- Coro/Coro/State.xs 2009/06/09 18:56:45 1.346 +++ Coro/Coro/State.xs 2009/08/22 22:36:23 1.371 @@ -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 @@ -139,7 +80,7 @@ #if __GNUC__ >= 3 # define attribute(x) __attribute__(x) -# define expect(expr,value) __builtin_expect ((expr),(value)) +# define expect(expr,value) __builtin_expect ((expr), (value)) # define INLINE static inline #else # define attribute(x) @@ -161,7 +102,18 @@ # endif #endif +#ifdef __linux +# include /* for timespec */ +# include /* for SYS_* */ +# ifdef SYS_clock_gettime +# define coro_clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) +# define CORO_CLOCK_MONOTONIC 1 +# define CORO_CLOCK_THREAD_CPUTIME_ID 3 +# endif +#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 @@ -192,11 +144,16 @@ static AV *av_async_pool; /* idle pool */ static SV *sv_Coro; /* class string */ static CV *cv_pool_handler; -static CV *cv_coro_state_new; /* 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; @@ -263,6 +220,9 @@ /* the C coroutine allocated to this perl coroutine, if any */ coro_cctx *cctx; + /* ready queue */ + struct coro *next_ready; + /* state data */ struct CoroSLF slf_frame; /* saved slf frame */ AV *mainstack; @@ -292,6 +252,9 @@ AV *on_enter; AV *on_leave; + /* times */ + coro_ts t_cpu, t_real; + /* linked list */ struct coro *next, *prev; }; @@ -306,21 +269,41 @@ /** Coro ********************************************************************/ -#define PRIO_MAX 3 -#define PRIO_HIGH 1 -#define PRIO_NORMAL 0 -#define PRIO_LOW -1 -#define PRIO_IDLE -3 -#define PRIO_MIN -4 +#define CORO_PRIO_MAX 3 +#define CORO_PRIO_HIGH 1 +#define CORO_PRIO_NORMAL 0 +#define CORO_PRIO_LOW -1 +#define CORO_PRIO_IDLE -3 +#define CORO_PRIO_MIN -4 /* for Coro.pm */ static SV *coro_current; static SV *coro_readyhook; -static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; +static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ static CV *cv_coro_run, *cv_coro_terminate; 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 (pTHX) +{ + 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 * @@ -353,18 +336,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; + + 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]; + + u2time (aTHX_ tv); + time_real [0] = tv [0]; + time_real [1] = tv [1] * 1000; +#endif +} - if (!cv) - croak ("code reference expected"); +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]; +} - return cv; +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]; } /*****************************************************************************/ @@ -475,6 +491,10 @@ AV *padlist; AV *av = (AV *)mg->mg_obj; + /* perl manages to free our internal AV and _then_ call us */ + if (IN_DESTRUCT) + return 0; + /* casting is fun. */ while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) free_padlist (aTHX_ padlist); @@ -572,6 +592,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; @@ -592,6 +620,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; @@ -614,7 +650,7 @@ { PERL_CONTEXT *cx = &ccstk[cxix--]; - if (expect_true (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT)) + if (expect_true (CxTYPE (cx) == CXt_SUB) || expect_false (CxTYPE (cx) == CXt_FORMAT)) { CV *cv = cx->blk_sub.cv; @@ -648,7 +684,7 @@ if (SLOT_COUNT >= 2) CXINC; if (SLOT_COUNT >= 3) CXINC; { - int i; + unsigned int i; for (i = 3; i < SLOT_COUNT; ++i) CXINC; } @@ -963,6 +999,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 @@ -989,25 +1031,45 @@ static void coro_destruct_perl (pTHX_ struct coro *coro) { - coro_unwind_stacks (aTHX); + SV *svf [9]; - SvREFCNT_dec (GvSV (PL_defgv)); - SvREFCNT_dec (GvAV (PL_defgv)); - SvREFCNT_dec (GvSV (PL_errgv)); - SvREFCNT_dec (PL_defoutgv); - SvREFCNT_dec (PL_rs); - SvREFCNT_dec (GvSV (irsgv)); - SvREFCNT_dec (GvHV (PL_hintgv)); + { + struct coro *current = SvSTATE_current; - SvREFCNT_dec (PL_diehook); - SvREFCNT_dec (PL_warnhook); - - SvREFCNT_dec (coro->saved_deffh); - SvREFCNT_dec (coro->rouse_cb); - SvREFCNT_dec (coro->invoke_cb); - SvREFCNT_dec (coro->invoke_av); + assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack)); + + save_perl (aTHX_ current); + load_perl (aTHX_ coro); - coro_destruct_stacks (aTHX); + coro_unwind_stacks (aTHX); + coro_destruct_stacks (aTHX); + + // now save some sv's to be free'd later + svf [0] = GvSV (PL_defgv); + svf [1] = (SV *)GvAV (PL_defgv); + svf [2] = GvSV (PL_errgv); + svf [3] = (SV *)PL_defoutgv; + svf [4] = PL_rs; + svf [5] = GvSV (irsgv); + svf [6] = (SV *)GvHV (PL_hintgv); + svf [7] = PL_diehook; + svf [8] = PL_warnhook; + assert (9 == sizeof (svf) / sizeof (*svf)); + + load_perl (aTHX_ current); + } + + { + unsigned int i; + + for (i = 0; i < sizeof (svf) / sizeof (*svf); ++i) + SvREFCNT_dec (svf [i]); + + SvREFCNT_dec (coro->saved_deffh); + SvREFCNT_dec (coro->rouse_cb); + SvREFCNT_dec (coro->invoke_cb); + SvREFCNT_dec (coro->invoke_av); + } } INLINE void @@ -1214,7 +1276,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. */ /* @@ -1504,20 +1567,7 @@ && coro->mainstack != main_mainstack && coro->slot && !PL_dirty) - { - struct coro *current = SvSTATE_current; - - assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack)); - - save_perl (aTHX_ current); - load_perl (aTHX_ coro); - - coro_destruct_perl (aTHX_ coro); - - load_perl (aTHX_ current); - - coro->slot = 0; - } + coro_destruct_perl (aTHX_ coro); cctx_destroy (coro->cctx); SvREFCNT_dec (coro->startcv); @@ -1586,86 +1636,72 @@ 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 coro_enq (pTHX_ struct coro *coro) { - av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv)); + struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; + + SvREFCNT_inc_NN (coro->hv); + + coro->next_ready = 0; + *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; + ready [1] = coro; } -INLINE SV * +INLINE struct coro * coro_deq (pTHX) { int prio; - for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) - if (AvFILLp (coro_ready [prio]) >= 0) - return av_shift (coro_ready [prio]); + for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) + { + struct coro **ready = coro_ready [prio]; + + if (ready [0]) + { + struct coro *coro = ready [0]; + ready [0] = coro->next_ready; + return coro; + } + } 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; } @@ -1698,15 +1734,13 @@ { for (;;) { - SV *next_sv = coro_deq (aTHX); + struct coro *next = coro_deq (aTHX); - if (expect_true (next_sv)) + if (expect_true (next)) { - struct coro *next = SvSTATE_hv (next_sv); - /* cannot transfer to destroyed coros, skip and look for next */ if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED))) - SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ + SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */ else { next->flags &= ~CF_READY; @@ -1883,9 +1917,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); @@ -1985,7 +2024,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) { @@ -2061,8 +2100,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; @@ -2076,7 +2115,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 */ @@ -2457,7 +2496,7 @@ static SV * coro_waitarray_new (pTHX_ int count) { - /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ + /* a waitarray=semaphore contains a counter IV in $sem->[0] and any waiters after that */ AV *av = newAV (); SV **ary; @@ -2609,9 +2648,9 @@ { /* 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, (SV *)SvREFCNT_inc_NN (cb_cv)); + av_push (av, SvREFCNT_inc_NN (cb_cv)); if (SvIVX (AvARRAY (av)[0]) > 0) coro_semaphore_adjust (aTHX_ av, 0); @@ -2645,8 +2684,20 @@ cb = av_shift (av); - api_ready (aTHX_ cb); - sv_setiv (cb, 0); /* signal waiter */ + if (SvTYPE (cb) == SVt_PVCV) + { + dSP; + PUSHMARK (SP); + XPUSHs (sv_2mortal (newRV_inc ((SV *)av))); + PUTBACK; + call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR); + } + else + { + api_ready (aTHX_ cb); + sv_setiv (cb, 0); /* signal waiter */ + } + SvREFCNT_dec (cb); --count; @@ -2665,7 +2716,18 @@ { AV *av = (AV *)SvRV (arg [0]); - if (SvIVX (AvARRAY (av)[0])) + if (items >= 2) + { + SV *cb_cv = s_get_cv_croak (arg [1]); + av_push (av, SvREFCNT_inc_NN (cb_cv)); + + if (SvIVX (AvARRAY (av)[0])) + coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */ + + frame->prepare = prepare_nop; + frame->check = slf_check_nop; + } + else if (SvIVX (AvARRAY (av)[0])) { SvIVX (AvARRAY (av)[0]) = 0; frame->prepare = prepare_nop; @@ -2673,7 +2735,7 @@ } else { - SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */ + SV *waiter = newSVsv (coro_current); /* owned by signal av */ av_push (av, waiter); @@ -2701,7 +2763,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)); @@ -2825,7 +2887,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; @@ -2854,6 +2916,64 @@ # include "clone.c" #endif +/*****************************************************************************/ + +static SV * +coro_new (pTHX_ 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 @@ -2922,67 +3042,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", !PRIO_NORMAL)); + 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 (aTHX_ ix ? coro_stash : coro_state_stash, &ST (1), items - 1, ix); + OUTPUT: RETVAL void @@ -3136,6 +3210,7 @@ struct coro *current = SvSTATE_current; SV **throwp = self == current ? &CORO_THROW : &self->except; SvREFCNT_dec (*throwp); + SvGETMAGIC (throw); *throwp = SvOK (throw) ? newSVsv (throw) : 0; } @@ -3204,12 +3279,47 @@ 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: { - int i; - sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); @@ -3223,19 +3333,16 @@ sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); - cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); - + CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */ + coro_stash = gv_stashpv ("Coro", TRUE); - newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); - newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); - newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); - newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); - newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); - newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); - - for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) - coro_ready[i] = newAV (); + newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); + newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); + newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); + newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (CORO_PRIO_LOW)); + newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (CORO_PRIO_IDLE)); + newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (CORO_PRIO_MIN)); { SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); @@ -3255,6 +3362,15 @@ } } +SV * +async (...) + PROTOTYPE: &@ + CODE: + RETVAL = coro_new (aTHX_ coro_stash, &ST (0), items, 1); + api_ready (aTHX_ RETVAL); + OUTPUT: + RETVAL + void terminate (...) CODE: @@ -3297,7 +3413,17 @@ PROTOTYPE: $ CODE: SvREFCNT_dec (coro_readyhook); - coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; + SvGETMAGIC (hook); + 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) @@ -3313,8 +3439,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; } @@ -3371,15 +3497,9 @@ if ((SV *)hv == &PL_sv_undef) { - PUSHMARK (SP); - EXTEND (SP, 2); - PUSHs (sv_Coro); - PUSHs ((SV *)cv_pool_handler); - PUTBACK; - call_sv ((SV *)cv_coro_state_new, G_SCALAR); - SPAGAIN; - - hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); + SV *sv = coro_new (aTHX_ coro_stash, (SV **)&cv_pool_handler, 1, 1); + hv = (HV *)SvREFCNT_inc_NN (SvRV (sv)); + SvREFCNT_dec (sv); } { @@ -3423,7 +3543,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 (); @@ -3450,10 +3570,22 @@ SV * new (SV *klass, SV *count = 0) CODE: +{ + int semcnt = 1; + + if (count) + { + SvGETMAGIC (count); + + if (SvOK (count)) + semcnt = SvIV (count); + } + RETVAL = sv_bless ( - coro_waitarray_new (aTHX_ count && SvOK (count) ? SvIV (count) : 1), + coro_waitarray_new (aTHX_ semcnt), GvSTASH (CvGV (cv)) ); +} OUTPUT: RETVAL @@ -3621,10 +3753,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; + } + +