--- Coro/Coro/State.xs 2007/09/26 19:26:48 1.165 +++ Coro/Coro/State.xs 2008/04/24 10:31:59 1.231 @@ -1,6 +1,7 @@ #include "libcoro/coro.c" #define PERL_NO_GET_CONTEXT +#define PERL_EXT #include "EXTERN.h" #include "perl.h" @@ -12,6 +13,15 @@ #include #include +#ifdef WIN32 +# undef setjmp +# undef longjmp +# undef _exit +#include +#else +# include /* most portable stdint.h */ +#endif + #ifdef HAVE_MMAP # include # include @@ -72,6 +82,19 @@ # endif #endif +/* 5.8.8 */ +#ifndef GV_NOTQUAL +# define GV_NOTQUAL 0 +#endif +#ifndef newSV +# define newSV(l) NEWSV(0,l) +#endif + +/* 5.11 */ +#ifndef CxHASARGS +# define CxHASARGS(cx) (cx)->blk_sub.hasargs +#endif + /* 5.8.7 */ #ifndef SvRV_set # define SvRV_set(s,v) SvRV(s) = (v) @@ -100,11 +123,16 @@ #if __GNUC__ >= 3 # define attribute(x) __attribute__(x) # define BARRIER __asm__ __volatile__ ("" : : : "memory") +# define expect(expr,value) __builtin_expect ((expr),(value)) #else # define attribute(x) # define BARRIER +# define expect(expr,value) (expr) #endif +#define expect_false(expr) expect ((expr) != 0, 0) +#define expect_true(expr) expect ((expr) != 0, 1) + #define NOINLINE attribute ((noinline)) #include "CoroAPI.h" @@ -132,7 +160,13 @@ static AV *main_mainstack; /* used to differentiate between $main and others */ static JMPENV *main_top_env; static HV *coro_state_stash, *coro_stash; -static SV *coro_mortal; /* will be freed after next transfer */ +static volatile SV *coro_mortal; /* will be freed after next transfer */ + +static GV *irsgv; /* $/ */ +static GV *stdoutgv; /* *STDOUT */ +static SV *rv_diehook; +static SV *rv_warnhook; +static HV *hv_sig; /* %SIG */ /* async_pool helper stuff */ static SV *sv_pool_rss; @@ -143,9 +177,12 @@ static int cctx_count, cctx_idle; enum { - CC_MAPPED = 0x01, - CC_TRACE = 0x02, - CC_NOREUSE = 0x04, /* throw this away after tracing */ + CC_MAPPED = 0x01, + CC_NOREUSE = 0x02, /* throw this away after tracing */ + CC_TRACE = 0x04, + CC_TRACE_SUB = 0x08, /* trace sub calls */ + CC_TRACE_LINE = 0x10, /* trace each statement */ + CC_TRACE_ALL = CC_TRACE_SUB | CC_TRACE_LINE, }; /* this is a structure representing a c-level coroutine */ @@ -175,35 +212,45 @@ CF_DESTROYED = 0x0008, /* coroutine data has been freed */ }; +/* the structure where most of the perl state is stored, overlaid on the cxstack */ +typedef struct { + SV *defsv; + AV *defav; + SV *errsv; + SV *irsgv; +#define VAR(name,type) type name; +# include "state.h" +#undef VAR +} perl_slots; + +#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) + /* this is a structure representing a perl-level coroutine */ struct coro { /* the c coroutine allocated to this perl coroutine, if any */ coro_cctx *cctx; - /* data associated with this coroutine (initial args) */ - AV *args; - int refcnt; - int save; /* CORO_SAVE flags */ - int flags; /* CF_ flags */ - - /* optionally saved, might be zero */ - AV *defav; /* @_ */ - SV *defsv; /* $_ */ - SV *errsv; /* $@ */ - GV *deffh; /* default filehandle */ - SV *irssv; /* $/ */ - SV *irssv_sv; /* real $/ cache */ - -#define VAR(name,type) type name; -# include "state.h" -#undef VAR + /* process data */ + AV *mainstack; + perl_slots *slot; /* basically the saved sp */ + + AV *args; /* data associated with this coroutine (initial args) */ + int refcnt; /* coroutines are refcounted, yes */ + int flags; /* CF_ flags */ + HV *hv; /* the perl hash associated with this coro, if any */ + + /* statistics */ + int usecount; /* number of transfers to this coro */ /* coro process data */ int prio; + SV *throw; /* exception to be thrown */ + + /* async_pool */ + SV *saved_deffh; /* linked list */ struct coro *next, *prev; - HV *hv; /* the perl hash associated with this coro, if any */ }; typedef struct coro *Coro__State; @@ -226,6 +273,36 @@ /** lowlevel stuff **********************************************************/ +static SV * +coro_get_sv (pTHX_ const char *name, int create) +{ +#if PERL_VERSION_ATLEAST (5,10,0) + /* silence stupid and wrong 5.10 warning that I am unable to switch off */ + get_sv (name, create); +#endif + return get_sv (name, create); +} + +static AV * +coro_get_av (pTHX_ const char *name, int create) +{ +#if PERL_VERSION_ATLEAST (5,10,0) + /* silence stupid and wrong 5.10 warning that I am unable to switch off */ + get_av (name, create); +#endif + return get_av (name, create); +} + +static HV * +coro_get_hv (pTHX_ const char *name, int create) +{ +#if PERL_VERSION_ATLEAST (5,10,0) + /* silence stupid and wrong 5.10 warning that I am unable to switch off */ + get_hv (name, create); +#endif + return get_hv (name, create); +} + static AV * coro_clone_padlist (pTHX_ CV *cv) { @@ -234,7 +311,7 @@ newpadlist = newAV (); AvREAL_off (newpadlist); -#if PERL_VERSION_ATLEAST (5,9,0) +#if PERL_VERSION_ATLEAST (5,10,0) Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); #else Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); @@ -282,30 +359,61 @@ while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) free_padlist (aTHX_ padlist); - SvREFCNT_dec (av); - return 0; } -#define PERL_MAGIC_coro PERL_MAGIC_ext +#define CORO_MAGIC_type_cv PERL_MAGIC_ext +#define CORO_MAGIC_type_state PERL_MAGIC_ext -static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; +static MGVTBL coro_cv_vtbl = { + 0, 0, 0, 0, + coro_cv_free +}; -#define CORO_MAGIC(cv) \ - SvMAGIC (cv) \ - ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ - ? SvMAGIC (cv) \ - : mg_find ((SV *)cv, PERL_MAGIC_coro) \ +#define CORO_MAGIC(sv,type) \ + SvMAGIC (sv) \ + ? SvMAGIC (sv)->mg_type == type \ + ? SvMAGIC (sv) \ + : mg_find (sv, type) \ : 0 +#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) +#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) + +static struct coro * +SvSTATE_ (pTHX_ SV *coro) +{ + HV *stash; + MAGIC *mg; + + if (SvROK (coro)) + coro = SvRV (coro); + + if (expect_false (SvTYPE (coro) != SVt_PVHV)) + croak ("Coro::State object required"); + + stash = SvSTASH (coro); + if (expect_false (stash != coro_stash && stash != coro_state_stash)) + { + /* very slow, but rare, check */ + if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) + croak ("Coro::State object required"); + } + + mg = CORO_MAGIC_state (coro); + return (struct coro *)mg->mg_ptr; +} + +#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) + /* the next two functions merely cache the padlists */ static void get_padlist (pTHX_ CV *cv) { - MAGIC *mg = CORO_MAGIC (cv); + MAGIC *mg = CORO_MAGIC_cv (cv); AV *av; - if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) + if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; else { @@ -324,20 +432,15 @@ static void put_padlist (pTHX_ CV *cv) { - MAGIC *mg = CORO_MAGIC (cv); + MAGIC *mg = CORO_MAGIC_cv (cv); AV *av; - if (!mg) - { - sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); - mg = mg_find ((SV *)cv, PERL_MAGIC_coro); - mg->mg_virtual = &vtbl_coro; - mg->mg_obj = (SV *)newAV (); - } + if (expect_false (!mg)) + mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); av = (AV *)mg->mg_obj; - if (AvFILLp (av) >= AvMAX (av)) + if (expect_false (AvFILLp (av) >= AvMAX (av))) av_extend (av, AvMAX (av) + 1); AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); @@ -345,44 +448,30 @@ /** load & save, init *******************************************************/ -#define SB do { -#define SE } while (0) - -#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE - static void load_perl (pTHX_ Coro__State c) { -#define VAR(name,type) PL_ ## name = c->name; -# include "state.h" -#undef VAR + perl_slots *slot = c->slot; + c->slot = 0; - if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); - if (c->defsv) REPLACE_SV (DEFSV , c->defsv); - if (c->errsv) REPLACE_SV (ERRSV , c->errsv); - if (c->deffh) REPLACE_SV (PL_defoutgv , c->deffh); + PL_mainstack = c->mainstack; - if (c->irssv) - { - if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) - { - SvREFCNT_dec (c->irssv); - c->irssv = 0; - } - else - { - REPLACE_SV (PL_rs, c->irssv); - if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); - sv_setsv (c->irssv_sv, PL_rs); - } - } + GvSV (PL_defgv) = slot->defsv; + GvAV (PL_defgv) = slot->defav; + GvSV (PL_errgv) = slot->errsv; + GvSV (irsgv) = slot->irsgv; + + #define VAR(name,type) PL_ ## name = slot->name; + # include "state.h" + #undef VAR { dSP; + CV *cv; /* now do the ugly restore mess */ - while ((cv = (CV *)POPs)) + while (expect_true (cv = (CV *)POPs)) { put_padlist (aTHX_ cv); /* mark this padlist as available */ CvDEPTH (cv) = PTR2IV (POPs); @@ -411,15 +500,15 @@ /* this loop was inspired by pp_caller */ for (;;) { - while (cxix >= 0) + while (expect_true (cxix >= 0)) { PERL_CONTEXT *cx = &ccstk[cxix--]; - if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT) + if (expect_true (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT)) { CV *cv = cx->blk_sub.cv; - if (CvDEPTH (cv)) + if (expect_true (CvDEPTH (cv))) { EXTEND (SP, 3); PUSHs ((SV *)CvPADLIST (cv)); @@ -432,7 +521,7 @@ } } - if (top_si->si_type == PERLSI_MAIN) + if (expect_true (top_si->si_type == PERLSI_MAIN)) break; top_si = top_si->si_prev; @@ -443,15 +532,32 @@ PUTBACK; } - c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; - c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; - c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; - c->deffh = c->save & CORO_SAVE_DEFFH ? (GV *)SvREFCNT_inc (PL_defoutgv) : 0; - c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; + /* allocate some space on the context stack for our purposes */ + /* we manually unroll here, as usually 2 slots is enough */ + if (SLOT_COUNT >= 1) CXINC; + if (SLOT_COUNT >= 2) CXINC; + if (SLOT_COUNT >= 3) CXINC; + { + int i; + for (i = 3; i < SLOT_COUNT; ++i) + CXINC; + } + cxstack_ix -= SLOT_COUNT; /* undo allocation */ -#define VAR(name,type)c->name = PL_ ## name; -# include "state.h" -#undef VAR + c->mainstack = PL_mainstack; + + { + perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); + + slot->defav = GvAV (PL_defgv); + slot->defsv = DEFSV; + slot->errsv = ERRSV; + slot->irsgv = GvSV (irsgv); + + #define VAR(name,type) slot->name = PL_ ## name; + # include "state.h" + #undef VAR + } } /* @@ -466,7 +572,7 @@ static void coro_init_stacks (pTHX) { - PL_curstackinfo = new_stackinfo(64, 6); + PL_curstackinfo = new_stackinfo(32, 8); PL_curstackinfo->si_type = PERLSI_MAIN; PL_curstack = PL_curstackinfo->si_stack; PL_mainstack = PL_curstack; /* remember in case we switch stacks */ @@ -475,10 +581,10 @@ PL_stack_sp = PL_stack_base; PL_stack_max = PL_stack_base + AvMAX(PL_curstack); - New(50,PL_tmps_stack,64,SV*); + New(50,PL_tmps_stack,32,SV*); PL_tmps_floor = -1; PL_tmps_ix = -1; - PL_tmps_max = 64; + PL_tmps_max = 32; New(54,PL_markstack,16,I32); PL_markstack_ptr = PL_markstack; @@ -488,15 +594,15 @@ SET_MARK_OFFSET; #endif - New(54,PL_scopestack,16,I32); + New(54,PL_scopestack,8,I32); PL_scopestack_ix = 0; - PL_scopestack_max = 16; + PL_scopestack_max = 8; - New(54,PL_savestack,64,ANY); + New(54,PL_savestack,24,ANY); PL_savestack_ix = 0; - PL_savestack_max = 64; + PL_savestack_max = 24; -#if !PERL_VERSION_ATLEAST (5,9,0) +#if !PERL_VERSION_ATLEAST (5,10,0) New(54,PL_retstack,4,OP*); PL_retstack_ix = 0; PL_retstack_max = 4; @@ -510,23 +616,6 @@ static void coro_destroy_stacks (pTHX) { - if (!IN_DESTRUCT) - { - /* restore all saved variables and stuff */ - LEAVE_SCOPE (0); - assert (PL_tmps_floor == -1); - - /* free all temporaries */ - FREETMPS; - assert (PL_tmps_ix == -1); - - /* unwind all extra stacks */ - POPSTACK_TO (PL_mainstack); - - /* unwind main stack */ - dounwind (-1); - } - while (PL_curstackinfo->si_next) PL_curstackinfo = PL_curstackinfo->si_next; @@ -546,36 +635,42 @@ Safefree (PL_markstack); Safefree (PL_scopestack); Safefree (PL_savestack); -#if !PERL_VERSION_ATLEAST (5,9,0) +#if !PERL_VERSION_ATLEAST (5,10,0) Safefree (PL_retstack); #endif } static size_t -coro_rss (struct coro *coro) +coro_rss (pTHX_ struct coro *coro) { - size_t rss = sizeof (coro); + size_t rss = sizeof (*coro); if (coro->mainstack) { + perl_slots tmp_slot; + perl_slots *slot; + if (coro->flags & CF_RUNNING) { - #define VAR(name,type)coro->name = PL_ ## name; + slot = &tmp_slot; + + #define VAR(name,type) slot->name = PL_ ## name; # include "state.h" #undef VAR } + else + slot = coro->slot; - rss += sizeof (coro->curstackinfo); - rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *); - rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); - rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); - rss += coro->tmps_max * sizeof (SV *); - rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); - rss += coro->scopestack_max * sizeof (I32); - rss += coro->savestack_max * sizeof (ANY); + rss += sizeof (slot->curstackinfo); + rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); + rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); + rss += slot->tmps_max * sizeof (SV *); + rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); + rss += slot->scopestack_max * sizeof (I32); + rss += slot->savestack_max * sizeof (ANY); -#if !PERL_VERSION_ATLEAST (5,9,0) - rss += coro->retstack_max * sizeof (OP *); +#if !PERL_VERSION_ATLEAST (5,10,0) + rss += slot->retstack_max * sizeof (OP *); #endif } @@ -584,88 +679,275 @@ /** coroutine stack handling ************************************************/ +static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); +static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); + +/* + * This overrides the default magic get method of %SIG elements. + * The original one doesn't provide for reading back of PL_diehook/PL_warnhook + * and instead of tryign to save and restore the hash elements, we just provide + * readback here. + * We only do this when the hook is != 0, as they are often set to 0 temporarily, + * not expecting this to actually change the hook. This is a potential problem + * when a schedule happens then, but we ignore this. + */ +static int +coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) +{ + const char *s = MgPV_nolen_const (mg); + + if (*s == '_') + { + if (strEQ (s, "__DIE__" ) && PL_diehook ) return sv_setsv (sv, PL_diehook ), 0; + if (strEQ (s, "__WARN__") && PL_warnhook) return sv_setsv (sv, PL_warnhook), 0; + } + + return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; +} + +static int +coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) +{ + const char *s = MgPV_nolen_const (mg); + + if (*s == '_') + { + SV **svp = 0; + + if (strEQ (s, "__DIE__" )) svp = &PL_diehook; + if (strEQ (s, "__WARN__")) svp = &PL_warnhook; + + if (svp) + { + SV *old = *svp; + *svp = newSVsv (sv); + SvREFCNT_dec (old); + return 0; + } + } + + return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; +} + static void -setup_coro (pTHX_ struct coro *coro) +coro_setup (pTHX_ struct coro *coro) { /* * emulate part of the perl startup here. */ coro_init_stacks (aTHX); + PL_runops = RUNOPS_DEFAULT; PL_curcop = &PL_compiling; PL_in_eval = EVAL_NULL; PL_comppad = 0; PL_curpm = 0; + PL_curpad = 0; PL_localizing = 0; PL_dirty = 0; PL_restartop = 0; +#if PERL_VERSION_ATLEAST (5,10,0) + PL_parser = 0; +#endif + + /* recreate the die/warn hooks */ + PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); + PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); + + GvSV (PL_defgv) = newSV (0); + GvAV (PL_defgv) = coro->args; coro->args = 0; + GvSV (PL_errgv) = newSV (0); + GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); + PL_rs = newSVsv (GvSV (irsgv)); + PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); { dSP; LOGOP myop; - SvREFCNT_dec (GvAV (PL_defgv)); - GvAV (PL_defgv) = coro->args; coro->args = 0; - Zero (&myop, 1, LOGOP); myop.op_next = Nullop; myop.op_flags = OPf_WANT_VOID; PUSHMARK (SP); - XPUSHs (av_shift (GvAV (PL_defgv))); + XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); PUTBACK; PL_op = (OP *)&myop; PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); SPAGAIN; } - ENTER; /* necessary e.g. for dounwind */ + /* this newly created coroutine might be run on an existing cctx which most + * likely was suspended in set_stacklevel, called from entersub. + * set_stacklevl doesn't do anything on return, but entersub does LEAVE, + * so we ENTER here for symmetry + */ + ENTER; } static void -free_coro_mortal (pTHX) +coro_destroy (pTHX_ struct coro *coro) { - if (coro_mortal) + if (!IN_DESTRUCT) { - SvREFCNT_dec (coro_mortal); - coro_mortal = 0; + /* restore all saved variables and stuff */ + LEAVE_SCOPE (0); + assert (PL_tmps_floor == -1); + + /* free all temporaries */ + FREETMPS; + assert (PL_tmps_ix == -1); + + /* unwind all extra stacks */ + POPSTACK_TO (PL_mainstack); + + /* unwind main stack */ + dounwind (-1); } + + 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 (PL_diehook); + SvREFCNT_dec (PL_warnhook); + + SvREFCNT_dec (coro->saved_deffh); + SvREFCNT_dec (coro->throw); + + coro_destroy_stacks (aTHX); } static void -do_trace (pTHX) +free_coro_mortal (pTHX) { - if (PL_curcop != &PL_compiling) + if (expect_true (coro_mortal)) { - runops_proc_t old_runops = PL_runops; - dSP; - ENTER; - SAVETMPS; - PUSHMARK (SP); - PUTBACK; - PL_runops = RUNOPS_DEFAULT; - call_pv ("Coro::_do_trace", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD | G_NOARGS); - PL_runops = old_runops; - SPAGAIN; - FREETMPS; - LEAVE; - PUTBACK; + SvREFCNT_dec (coro_mortal); + coro_mortal = 0; } } static int -runops_coro (pTHX) +runops_trace (pTHX) { COP *oldcop = 0; + int oldcxix = -2; + struct coro *coro = SvSTATE (coro_current); /* trace cctx is tied to specific coro */ + coro_cctx *cctx = coro->cctx; while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) { PERL_ASYNC_CHECK (); - if (oldcop != PL_curcop) + if (cctx->flags & CC_TRACE_ALL) { - oldcop = PL_curcop; - do_trace (aTHX); + if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) + { + PERL_CONTEXT *cx = &cxstack[cxstack_ix]; + SV **bot, **top; + AV *av = newAV (); /* return values */ + SV **cb; + dSP; + + GV *gv = CvGV (cx->blk_sub.cv); + SV *fullname = sv_2mortal (newSV (0)); + if (isGV (gv)) + gv_efullname3 (fullname, gv, 0); + + bot = PL_stack_base + cx->blk_oldsp + 1; + top = cx->blk_gimme == G_ARRAY ? SP + 1 + : cx->blk_gimme == G_SCALAR ? bot + 1 + : bot; + + av_extend (av, top - bot); + while (bot < top) + av_push (av, SvREFCNT_inc (*bot++)); + + PL_runops = RUNOPS_DEFAULT; + ENTER; + SAVETMPS; + EXTEND (SP, 3); + PUSHMARK (SP); + PUSHs (&PL_sv_no); + PUSHs (fullname); + PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); + PUTBACK; + cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); + if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); + SPAGAIN; + FREETMPS; + LEAVE; + PL_runops = runops_trace; + } + + if (oldcop != PL_curcop) + { + oldcop = PL_curcop; + + if (PL_curcop != &PL_compiling) + { + SV **cb; + + if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) + { + PERL_CONTEXT *cx = &cxstack[cxstack_ix]; + + if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) + { + runops_proc_t old_runops = PL_runops; + dSP; + GV *gv = CvGV (cx->blk_sub.cv); + SV *fullname = sv_2mortal (newSV (0)); + + if (isGV (gv)) + gv_efullname3 (fullname, gv, 0); + + PL_runops = RUNOPS_DEFAULT; + ENTER; + SAVETMPS; + EXTEND (SP, 3); + PUSHMARK (SP); + PUSHs (&PL_sv_yes); + PUSHs (fullname); + PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); + PUTBACK; + cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); + if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); + SPAGAIN; + FREETMPS; + LEAVE; + PL_runops = runops_trace; + } + + oldcxix = cxstack_ix; + } + + if (cctx->flags & CC_TRACE_LINE) + { + dSP; + + PL_runops = RUNOPS_DEFAULT; + ENTER; + SAVETMPS; + EXTEND (SP, 3); + PL_runops = RUNOPS_DEFAULT; + PUSHMARK (SP); + PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); + PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); + PUTBACK; + cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0); + if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); + SPAGAIN; + FREETMPS; + LEAVE; + PL_runops = runops_trace; + } + } + } } } @@ -677,7 +959,7 @@ /* _cctx_init should be careful, as it could be called at almost any time */ /* during execution of a perl program */ static void NOINLINE -prepare_cctx (pTHX_ coro_cctx *cctx) +cctx_prepare (pTHX_ coro_cctx *cctx) { dSP; LOGOP myop; @@ -685,7 +967,7 @@ PL_top_env = &PL_start_env; if (cctx->flags & CC_TRACE) - PL_runops = runops_coro; + PL_runops = runops_trace; Zero (&myop, 1, LOGOP); myop.op_next = PL_op; @@ -705,18 +987,18 @@ * this is a _very_ stripped down perl interpreter ;) */ static void -coro_run (void *arg) +cctx_run (void *arg) { dTHX; - /* coro_run is the alternative tail of transfer(), so unlock here. */ + /* cctx_run is the alternative tail of transfer(), so unlock here. */ UNLOCK; /* we now skip the entersub that lead to transfer() */ PL_op = PL_op->op_next; /* inject a fake subroutine call to cctx_init */ - prepare_cctx (aTHX_ (coro_cctx *)arg); + cctx_prepare (aTHX_ (coro_cctx *)arg); /* somebody or something will hit me for both perl_run and PL_restartop */ PL_restartop = PL_op; @@ -745,7 +1027,6 @@ Newz (0, cctx, 1, coro_cctx); #if HAVE_MMAP - cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; /* mmap supposedly does allocate-on-write for us */ cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); @@ -776,7 +1057,7 @@ } REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); - coro_create (&cctx->cctx, coro_run, (void *)cctx, stack_start, stack_size); + coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); return cctx; } @@ -803,16 +1084,19 @@ Safefree (cctx); } +/* wether this cctx should be destructed */ +#define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) + static coro_cctx * cctx_get (pTHX) { - while (cctx_first) + while (expect_true (cctx_first)) { coro_cctx *cctx = cctx_first; cctx_first = cctx->next; --cctx_idle; - if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE)) + if (expect_true (!CCTX_EXPIRED (cctx))) return cctx; cctx_destroy (cctx); @@ -825,7 +1109,7 @@ cctx_put (coro_cctx *cctx) { /* free another cctx if overlimit */ - if (cctx_idle >= MAX_IDLE_CCTX) + if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) { coro_cctx *first = cctx_first; cctx_first = first->next; @@ -841,23 +1125,45 @@ /** coroutine switching *****************************************************/ -/* never call directly, always through the coro_state_transfer global variable */ +static void +transfer_check (pTHX_ struct coro *prev, struct coro *next) +{ + if (expect_true (prev != next)) + { + if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) + croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states"); + + if (expect_false (next->flags & CF_RUNNING)) + croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); + + if (expect_false (next->flags & CF_DESTROYED)) + croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); + +#if !PERL_VERSION_ATLEAST (5,10,0) + if (expect_false (PL_lex_state != LEX_NOTPARSING)) + croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version"); +#endif + } +} + +/* always use the TRANSFER macro */ static void NOINLINE -transfer (pTHX_ struct coro *prev, struct coro *next) +transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) { dSTACKLEVEL; + static volatile int has_throw; /* sometimes transfer is only called to set idle_sp */ - if (!next) + if (expect_false (!next)) { ((coro_cctx *)prev)->idle_sp = STACKLEVEL; assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ } - else if (prev != next) + else if (expect_true (prev != next)) { coro_cctx *prev__cctx; - if (prev->flags & CF_NEW) + if (expect_false (prev->flags & CF_NEW)) { /* create a new empty context */ Newz (0, prev->cctx, 1, coro_cctx); @@ -865,54 +1171,55 @@ prev->flags |= CF_RUNNING; } - /*TODO: must not croak here */ - if (!prev->flags & CF_RUNNING) - croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); - - if (next->flags & CF_RUNNING) - croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); - - if (next->flags & CF_DESTROYED) - croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); - prev->flags &= ~CF_RUNNING; next->flags |= CF_RUNNING; LOCK; - if (next->flags & CF_NEW) + /* first get rid of the old state */ + save_perl (aTHX_ prev); + + if (expect_false (next->flags & CF_NEW)) { /* need to start coroutine */ next->flags &= ~CF_NEW; - /* first get rid of the old state */ - save_perl (aTHX_ prev); /* setup coroutine call */ - setup_coro (aTHX_ next); + coro_setup (aTHX_ next); } else - { - /* coroutine already started */ - save_perl (aTHX_ prev); - load_perl (aTHX_ next); - } + load_perl (aTHX_ next); prev__cctx = prev->cctx; /* possibly "free" the cctx */ - if (prev__cctx->idle_sp == STACKLEVEL) + if (expect_true ( + prev__cctx->idle_sp == STACKLEVEL + && !(prev__cctx->flags & CC_TRACE) + && !force_cctx + )) { /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); prev->cctx = 0; + /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ + /* without this the next cctx_get might destroy the prev__cctx while still in use */ + if (expect_false (CCTX_EXPIRED (prev__cctx))) + if (!next->cctx) + next->cctx = cctx_get (aTHX); + cctx_put (prev__cctx); } - if (!next->cctx) + ++next->usecount; + + if (expect_true (!next->cctx)) next->cctx = cctx_get (aTHX); - if (prev__cctx != next->cctx) + has_throw = !!next->throw; + + if (expect_false (prev__cctx != next->cctx)) { prev__cctx->top_env = PL_top_env; PL_top_env = next->cctx->top_env; @@ -921,6 +1228,19 @@ free_coro_mortal (aTHX); UNLOCK; + + if (expect_false (has_throw)) + { + struct coro *coro = SvSTATE (coro_current); + + if (coro->throw) + { + SV *exception = coro->throw; + coro->throw = 0; + sv_setsv (ERRSV, exception); + croak (0); + } + } } } @@ -929,7 +1249,8 @@ struct coro *prev, *next; }; -#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) +#define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx)) +#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) /** high level stuff ********************************************************/ @@ -956,22 +1277,17 @@ { struct coro temp; - assert (!(coro->flags & CF_RUNNING)); - - Zero (&temp, 1, struct coro); - temp.save = CORO_SAVE_ALL; - if (coro->flags & CF_RUNNING) croak ("FATAL: tried to destroy currently running coroutine"); save_perl (aTHX_ &temp); load_perl (aTHX_ coro); - coro_destroy_stacks (aTHX); + coro_destroy (aTHX_ coro); - load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ + load_perl (aTHX_ &temp); - coro->mainstack = 0; + coro->slot = 0; } cctx_destroy (coro->cctx); @@ -1022,34 +1338,12 @@ #endif }; -static struct coro * -SvSTATE_ (pTHX_ SV *coro) -{ - HV *stash; - MAGIC *mg; - - if (SvROK (coro)) - coro = SvRV (coro); - - stash = SvSTASH (coro); - if (stash != coro_stash && stash != coro_state_stash) - { - /* very slow, but rare, check */ - if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) - croak ("Coro::State object required"); - } - - mg = CORO_MAGIC (coro); - return (struct coro *)mg->mg_ptr; -} - -#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) - static void prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) { ta->prev = SvSTATE (prev_sv); ta->next = SvSTATE (next_sv); + TRANSFER_CHECK (*ta); } static void @@ -1059,20 +1353,7 @@ struct transfer_args ta; prepare_transfer (aTHX_ &ta, prev_sv, next_sv); - TRANSFER (ta); -} - -static int -api_save (SV *coro_sv, int new_save) -{ - dTHX; - struct coro *coro = SvSTATE (coro_sv); - int old_save = coro->save; - - if (new_save >= 0) - coro->save = new_save; - - return old_save; + TRANSFER (ta, 1); } /** Coro ********************************************************************/ @@ -1084,15 +1365,11 @@ } static SV * -coro_deq (pTHX_ int min_prio) +coro_deq (pTHX) { - int prio = PRIO_MAX - PRIO_MIN; - - min_prio -= PRIO_MIN; - if (min_prio < 0) - min_prio = 0; + int prio; - for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) + for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) if (AvFILLp (coro_ready [prio]) >= 0) return av_shift (coro_ready [prio]); @@ -1138,10 +1415,10 @@ for (;;) { LOCK; - next_sv = coro_deq (aTHX_ PRIO_MIN); + next_sv = coro_deq (aTHX); /* nothing to schedule: call the idle handler */ - if (!next_sv) + if (expect_false (!next_sv)) { dSP; UNLOCK; @@ -1152,6 +1429,7 @@ PUSHMARK (SP); PUTBACK; call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); + SPAGAIN; FREETMPS; LEAVE; @@ -1161,7 +1439,7 @@ ta->next = SvSTATE (next_sv); /* cannot transfer to destroyed coros, skip and look for next */ - if (ta->next->flags & CF_DESTROYED) + if (expect_false (ta->next->flags & CF_DESTROYED)) { UNLOCK; SvREFCNT_dec (next_sv); @@ -1176,11 +1454,11 @@ /* free this only after the transfer */ prev_sv = SvRV (coro_current); - SvRV_set (coro_current, next_sv); ta->prev = SvSTATE (prev_sv); - + TRANSFER_CHECK (*ta); assert (ta->next->flags & CF_READY); ta->next->flags &= ~CF_READY; + SvRV_set (coro_current, next_sv); LOCK; free_coro_mortal (aTHX); @@ -1216,7 +1494,7 @@ struct transfer_args ta; prepare_schedule (aTHX_ &ta); - TRANSFER (ta); + TRANSFER (ta, 1); } static int @@ -1227,9 +1505,9 @@ prepare_cede (aTHX_ &ta); - if (ta.prev != ta.next) + if (expect_true (ta.prev != ta.next)) { - TRANSFER (ta); + TRANSFER (ta, 1); return 1; } else @@ -1244,14 +1522,40 @@ if (prepare_cede_notself (aTHX_ &ta)) { - TRANSFER (ta); + TRANSFER (ta, 1); return 1; } else return 0; } -MODULE = Coro::State PACKAGE = Coro::State +static void +api_trace (SV *coro_sv, int flags) +{ + dTHX; + struct coro *coro = SvSTATE (coro_sv); + + if (flags & CC_TRACE) + { + if (!coro->cctx) + coro->cctx = cctx_new (); + else if (!(coro->cctx->flags & CC_TRACE)) + croak ("cannot enable tracing on coroutine with custom stack"); + + coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); + } + else if (coro->cctx && coro->cctx->flags & CC_TRACE) + { + coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL); + + if (coro->flags & CF_RUNNING) + PL_runops = RUNOPS_DEFAULT; + else + coro->slot->runops = RUNOPS_DEFAULT; + } +} + +MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ PROTOTYPES: DISABLE @@ -1262,15 +1566,24 @@ #endif BOOT_PAGESIZE; + irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); + stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); + + orig_sigelem_get = PL_vtbl_sigelem.svt_get; + PL_vtbl_sigelem.svt_get = coro_sigelem_get; + orig_sigelem_set = PL_vtbl_sigelem.svt_set; + PL_vtbl_sigelem.svt_set = coro_sigelem_set; + + hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); + rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); + rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); + coro_state_stash = gv_stashpv ("Coro::State", TRUE); - newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); - newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); - newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); - newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); - newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH)); - newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF)); - newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); + newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); + newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); + newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE)); + newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL)); main_mainstack = PL_mainstack; main_top_env = PL_top_env; @@ -1279,6 +1592,7 @@ main_top_env = main_top_env->je_prev; coroapi.ver = CORO_API_VERSION; + coroapi.rev = CORO_API_REVISION; coroapi.transfer = api_transfer; assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); @@ -1289,12 +1603,12 @@ CODE: { struct coro *coro; + MAGIC *mg; HV *hv; int i; Newz (0, coro, 1, struct coro); - coro->args = newAV (); - coro->save = CORO_SAVE_DEF; + coro->args = newAV (); coro->flags = CF_NEW; if (coro_first) coro_first->prev = coro; @@ -1302,33 +1616,20 @@ coro_first = coro; coro->hv = hv = newHV (); - sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; + 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)); + av_extend (coro->args, items - 1); for (i = 1; i < items; i++) av_push (coro->args, newSVsv (ST (i))); } OUTPUT: RETVAL -int -save (SV *coro, int new_save = -1) - CODE: - RETVAL = api_save (coro, new_save); - OUTPUT: - RETVAL - -int -save_also (SV *coro_sv, int save_also) - CODE: -{ - struct coro *coro = SvSTATE (coro_sv); - RETVAL = coro->save; - coro->save |= save_also; -} - OUTPUT: - RETVAL - +# these not obviously related functions are all rolled into the same xs +# function to increase chances that they all will call transfer with the same +# stack offset void _set_stacklevel (...) ALIAS: @@ -1340,6 +1641,7 @@ { struct transfer_args ta; + PUTBACK; switch (ix) { case 0: @@ -1368,12 +1670,13 @@ break; } + SPAGAIN; BARRIER; - TRANSFER (ta); - - if (GIMME_V != G_VOID && ta.next != ta.prev) - XSRETURN_YES; + PUTBACK; + TRANSFER (ta, 0); + SPAGAIN; /* might be the sp of a different coroutine now */ + /* be extra careful not to ever do anything after TRANSFER */ } bool @@ -1432,11 +1735,10 @@ if (coro->mainstack) { struct coro temp; - Zero (&temp, 1, struct coro); - temp.save = CORO_SAVE_ALL; if (!(coro->flags & CF_RUNNING)) { + PUTBACK; save_perl (aTHX_ &temp); load_perl (aTHX_ coro); } @@ -1445,12 +1747,16 @@ dSP; ENTER; SAVETMPS; - PUSHMARK (SP); PUTBACK; + PUSHSTACK; + PUSHMARK (SP); + if (ix) eval_sv (coderef, 0); else call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); + + POPSTACK; SPAGAIN; FREETMPS; LEAVE; @@ -1461,10 +1767,11 @@ { save_perl (aTHX_ coro); load_perl (aTHX_ &temp); + SPAGAIN; } } } - + SV * is_ready (Coro::State coro) PROTOTYPE: $ @@ -1479,25 +1786,7 @@ RETVAL void -trace (Coro::State coro, int enable = 0) - CODE: - if (enable) - { - if (coro->flags & CF_RUNNING) - croak ("cannot enable tracing on running coroutine"); - - if (coro->cctx) - croak ("cannot enable tracing on coroutine with custom stack"); - - coro->cctx = cctx_new (); - coro->cctx->flags |= CC_TRACE; - } - else - if (coro->cctx && coro->cctx->flags & CC_TRACE) - { - coro->cctx->flags &= ~CC_TRACE; - coro->cctx->flags |= CC_NOREUSE; - } +api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) SV * has_stack (Coro::State coro) @@ -1507,14 +1796,33 @@ OUTPUT: RETVAL +int +is_traced (Coro::State coro) + PROTOTYPE: $ + CODE: + RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; + OUTPUT: + RETVAL + IV rss (Coro::State coro) PROTOTYPE: $ + ALIAS: + usecount = 1 CODE: - RETVAL = coro_rss (coro); + switch (ix) + { + case 0: RETVAL = coro_rss (aTHX_ coro); break; + case 1: RETVAL = coro->usecount; break; + } OUTPUT: RETVAL +void +force_cctx () + CODE: + struct coro *coro = SvSTATE (coro_current); + coro->cctx->idle_sp = 0; MODULE = Coro::State PACKAGE = Coro @@ -1522,14 +1830,14 @@ { int i; - sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); - sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); - av_async_pool = get_av ("Coro::async_pool", TRUE); + sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); + sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); + av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); - coro_current = get_sv ("Coro::current", FALSE); + coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); SvREADONLY_on (coro_current); - coro_stash = gv_stashpv ("Coro", TRUE); + coro_stash = gv_stashpv ("Coro", TRUE); newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); @@ -1542,10 +1850,10 @@ coro_ready[i] = newAV (); { - SV *sv = perl_get_sv("Coro::API", 1); + SV *sv = perl_get_sv ("Coro::API", TRUE); + perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */ coroapi.schedule = api_schedule; - coroapi.save = api_save; coroapi.cede = api_cede; coroapi.cede_notself = api_cede_notself; coroapi.ready = api_ready; @@ -1604,19 +1912,49 @@ OUTPUT: RETVAL +void +throw (Coro::State self, SV *throw = &PL_sv_undef) + PROTOTYPE: $;$ + CODE: + SvREFCNT_dec (self->throw); + self->throw = SvOK (throw) ? newSVsv (throw) : 0; + +void +swap_defsv (Coro::State self) + PROTOTYPE: $ + ALIAS: + swap_defav = 1 + CODE: + if (!self->slot) + croak ("cannot swap state with coroutine that has no saved state"); + else + { + SV **src = ix ? (SV **)&GvAV (PL_defgv) : &GvSV (PL_defgv); + SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; + + SV *tmp = *src; *src = *dst; *dst = tmp; + } + # for async_pool speedup void _pool_1 (SV *cb) CODE: { - int i, len; + struct coro *coro = SvSTATE (coro_current); HV *hv = (HV *)SvRV (coro_current); AV *defav = GvAV (PL_defgv); SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); AV *invoke_av; + int i, len; if (!invoke) - croak ("\3terminate\2\n"); + { + SvREFCNT_dec (PL_diehook); PL_diehook = 0; + croak ("\3async_pool terminate\2\n"); + } + + SvREFCNT_dec (coro->saved_deffh); + coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); hv_store (hv, "desc", sizeof ("desc") - 1, newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); @@ -1644,15 +1982,25 @@ sv_setsv (cb, &PL_sv_undef); - if (coro_rss (coro) > SvIV (sv_pool_rss) + SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; + coro->saved_deffh = 0; + + if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) - croak ("\3terminate\2\n"); + { + SvREFCNT_dec (PL_diehook); PL_diehook = 0; + croak ("\3async_pool terminate\2\n"); + } av_clear (GvAV (PL_defgv)); hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); - coro->save = CORO_SAVE_DEF; + coro->prio = 0; + + if (coro->cctx && (coro->cctx->flags & CC_TRACE)) + api_trace (coro_current, 0); + av_push (av_async_pool, newSVsv (coro_current)); }