--- Coro/Coro/State.xs 2007/09/21 01:09:36 1.155 +++ Coro/Coro/State.xs 2007/10/06 00:35:41 1.195 @@ -77,6 +77,14 @@ # define SvRV_set(s,v) SvRV(s) = (v) #endif +/* 5.8.8 */ +#ifndef GV_NOTQUAL +# define GV_NOTQUAL 0 +#endif +#ifndef newSV +# define newSV(l) NEWSV(0,l) +#endif + #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 # undef CORO_STACKGUARD #endif @@ -90,10 +98,9 @@ # define CORO_PREFER_PERL_FUNCTIONS 0 #endif -/* The next macro should declare a variable stacklevel that contains and approximation - * to the current C stack pointer. Its property is that it changes with each call - * and should be unique. */ -#define dSTACKLEVEL int stacklevel +/* The next macros try to return the current stack pointer, in an as + * portable way as possible. */ +#define dSTACKLEVEL volatile char stacklevel #define STACKLEVEL ((void *)&stacklevel) #define IN_DESTRUCT (PL_main_cv == Nullcv) @@ -101,11 +108,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" @@ -135,9 +147,26 @@ static HV *coro_state_stash, *coro_stash; static SV *coro_mortal; /* will be freed after next transfer */ +static GV *irsgv; /* $/ */ +static GV *stdoutgv; /* *STDOUT */ + +/* async_pool helper stuff */ +static SV *sv_pool_rss; +static SV *sv_pool_size; +static AV *av_async_pool; + static struct coro_cctx *cctx_first; static int cctx_count, cctx_idle; +enum { + 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 */ typedef struct coro_cctx { struct coro_cctx *next; @@ -155,7 +184,7 @@ #if CORO_USE_VALGRIND int valgrind_id; #endif - char inuse, mapped; + unsigned char flags; } coro_cctx; enum { @@ -173,14 +202,13 @@ /* 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 *deffh; /* default filehandle */ SV *irssv; /* $/ */ SV *irssv_sv; /* real $/ cache */ @@ -188,8 +216,15 @@ # include "state.h" #undef VAR + /* statistics */ + int usecount; /* number of transfers to this coro */ + /* coro process data */ int prio; + //SV *throw; + + /* async_pool */ + SV *saved_deffh; /* linked list */ struct coro *next, *prev; @@ -288,6 +323,32 @@ : mg_find ((SV *)cv, PERL_MAGIC_coro) \ : 0 +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 (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) @@ -295,7 +356,7 @@ MAGIC *mg = CORO_MAGIC (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 { @@ -317,7 +378,7 @@ MAGIC *mg = CORO_MAGIC (cv); AV *av; - if (!mg) + if (expect_false (!mg)) { sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); mg = mg_find ((SV *)cv, PERL_MAGIC_coro); @@ -327,7 +388,7 @@ 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); @@ -335,11 +396,6 @@ /** 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) { @@ -347,32 +403,17 @@ # include "state.h" #undef VAR - 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); - - 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) = c->defsv; + GvAV (PL_defgv) = c->defav; + GvSV (PL_errgv) = c->errsv; + GvSV (irsgv) = c->irssv_sv; { 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); @@ -397,20 +438,19 @@ * (and reinitialize) all cv's in the whole callchain :( */ - EXTEND (SP, 3 + 1); - PUSHs (Nullsv); + XPUSHs (Nullsv); /* 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)); @@ -423,22 +463,21 @@ } } - if (top_si->si_type == PERLSI_MAIN) + if (expect_true (top_si->si_type == PERLSI_MAIN)) break; top_si = top_si->si_prev; - ccstk = top_si->si_cxstack; - cxix = top_si->si_cxix; + ccstk = top_si->si_cxstack; + cxix = top_si->si_cxix; } 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; + c->defav = GvAV (PL_defgv); + c->defsv = DEFSV; + c->errsv = ERRSV; + c->irssv_sv = GvSV (irsgv); #define VAR(name,type)c->name = PL_ ## name; # include "state.h" @@ -457,7 +496,7 @@ static void coro_init_stacks (pTHX) { - PL_curstackinfo = new_stackinfo(64, 4); + PL_curstackinfo = new_stackinfo(64, 6); PL_curstackinfo->si_type = PERLSI_MAIN; PL_curstack = PL_curstackinfo->si_stack; PL_mainstack = PL_curstack; /* remember in case we switch stacks */ @@ -488,9 +527,9 @@ PL_savestack_max = 64; #if !PERL_VERSION_ATLEAST (5,9,0) - New(54,PL_retstack,8,OP*); + New(54,PL_retstack,4,OP*); PL_retstack_ix = 0; - PL_retstack_max = 8; + PL_retstack_max = 4; #endif } #endif @@ -501,23 +540,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; @@ -543,9 +565,9 @@ } 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) { @@ -557,9 +579,9 @@ } rss += sizeof (coro->curstackinfo); - rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *); + rss += sizeof (SV) + 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 += sizeof (SV) + 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); @@ -576,53 +598,216 @@ /** coroutine stack handling ************************************************/ 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 = 1; PL_localizing = 0; PL_dirty = 0; PL_restartop = 0; + + 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 = 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 */ + ENTER; /* necessary e.g. for dounwind and to balance the xsub-entersub */ +} + +static void +coro_destroy (pTHX_ struct coro *coro) +{ + 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); + } + + 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 (coro->saved_deffh); + //SvREFCNT_dec (coro->throw); + + coro_destroy_stacks (aTHX); } static void free_coro_mortal (pTHX) { - if (coro_mortal) + if (expect_true (coro_mortal)) { SvREFCNT_dec (coro_mortal); coro_mortal = 0; } } +static int +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 (cctx->flags & CC_TRACE_ALL) + { + 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 (cx->blk_sub.hasargs ? 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; + } + } + } + } + } + + TAINT_NOT; + return 0; +} + /* inject a fake call to Coro::State::_cctx_init into the execution */ /* _cctx_init should be careful, as it could be called at almost any time */ /* during execution of a perl program */ @@ -632,6 +817,11 @@ dSP; LOGOP myop; + PL_top_env = &PL_start_env; + + if (cctx->flags & CC_TRACE) + PL_runops = runops_trace; + Zero (&myop, 1, LOGOP); myop.op_next = PL_op; myop.op_flags = OPf_WANT_VOID | OPf_STACKED; @@ -657,7 +847,8 @@ /* coro_run is the alternative tail of transfer(), so unlock here. */ UNLOCK; - PL_top_env = &PL_start_env; + /* 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); @@ -701,7 +892,7 @@ # endif stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; - cctx->mapped = 1; + cctx->flags |= CC_MAPPED; } else #endif @@ -738,7 +929,7 @@ #endif #if HAVE_MMAP - if (cctx->mapped) + if (cctx->flags & CC_MAPPED) munmap (cctx->sptr, cctx->ssize); else #endif @@ -747,22 +938,24 @@ 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) + if (expect_true (!CCTX_EXPIRED (cctx))) return cctx; cctx_destroy (cctx); } - PL_op = PL_op->op_next; return cctx_new (); } @@ -770,13 +963,12 @@ 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; --cctx_idle; - assert (!first->inuse); cctx_destroy (first); } @@ -787,56 +979,62 @@ /** 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 (expect_false (PL_lex_state != LEX_NOTPARSING)) + croak ("Coro::State::transfer called while parsing, but this is not supported"); + } +} + +/* always use the TRANSFER macro */ static void NOINLINE transfer (pTHX_ struct coro *prev, struct coro *next) { dSTACKLEVEL; /* 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); - prev->cctx->inuse = 1; prev->flags &= ~CF_NEW; 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) + 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); - /* need a new stack */ - assert (!next->cctx); + coro_setup (aTHX_ next); } else { @@ -848,25 +1046,27 @@ 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))) { /* 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))) + next->cctx = cctx_get (aTHX); + cctx_put (prev__cctx); - prev__cctx->inuse = 0; } - if (!next->cctx) - { - next->cctx = cctx_get (aTHX); - assert (!next->cctx->inuse); - next->cctx->inuse = 1; - } + ++next->usecount; - if (prev__cctx != next->cctx) + if (expect_true (!next->cctx)) + next->cctx = cctx_get (aTHX); + + if (expect_false (prev__cctx != next->cctx)) { prev__cctx->top_env = PL_top_env; PL_top_env = next->cctx->top_env; @@ -884,6 +1084,7 @@ }; #define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) +#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) /** high level stuff ********************************************************/ @@ -910,18 +1111,13 @@ { 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.. */ @@ -976,34 +1172,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 @@ -1016,19 +1190,6 @@ 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; -} - /** Coro ********************************************************************/ static void @@ -1095,7 +1256,7 @@ next_sv = coro_deq (aTHX_ PRIO_MIN); /* nothing to schedule: call the idle handler */ - if (!next_sv) + if (expect_false (!next_sv)) { dSP; UNLOCK; @@ -1115,7 +1276,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); @@ -1130,11 +1291,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); @@ -1181,7 +1342,7 @@ prepare_cede (aTHX_ &ta); - if (ta.prev != ta.next) + if (expect_true (ta.prev != ta.next)) { TRANSFER (ta); return 1; @@ -1205,7 +1366,33 @@ 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->runops = RUNOPS_DEFAULT; + } +} + +MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ PROTOTYPES: DISABLE @@ -1216,15 +1403,15 @@ #endif BOOT_PAGESIZE; + irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); + stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); + 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; @@ -1247,8 +1434,7 @@ 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; @@ -1259,30 +1445,16 @@ sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->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: @@ -1326,7 +1498,7 @@ BARRIER; TRANSFER (ta); - if (GIMME_V != G_VOID && ta.next != ta.prev) + if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev)) XSRETURN_YES; } @@ -1378,14 +1550,15 @@ } void -_eval (Coro::State coro, SV *coderef) +call (Coro::State coro, SV *coderef) + ALIAS: + eval = 1 CODE: { if (coro->mainstack) { struct coro temp; Zero (&temp, 1, struct coro); - temp.save = CORO_SAVE_ALL; if (!(coro->flags & CF_RUNNING)) { @@ -1399,7 +1572,10 @@ SAVETMPS; PUSHMARK (SP); PUTBACK; - call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); + if (ix) + eval_sv (coderef, 0); + else + call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); SPAGAIN; FREETMPS; LEAVE; @@ -1413,7 +1589,7 @@ } } } - + SV * is_ready (Coro::State coro) PROTOTYPE: $ @@ -1427,11 +1603,36 @@ OUTPUT: RETVAL +void +api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) + +SV * +has_stack (Coro::State coro) + PROTOTYPE: $ + CODE: + RETVAL = boolSV (!!coro->cctx); + 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 @@ -1442,6 +1643,13 @@ { 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); + + coro_current = get_sv ("Coro::current", FALSE); + SvREADONLY_on (coro_current); + coro_stash = gv_stashpv ("Coro", TRUE); newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); @@ -1451,9 +1659,6 @@ newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); - coro_current = get_sv ("Coro::current", FALSE); - SvREADONLY_on (coro_current); - for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) coro_ready[i] = newAV (); @@ -1461,7 +1666,6 @@ SV *sv = perl_get_sv("Coro::API", 1); coroapi.schedule = api_schedule; - coroapi.save = api_save; coroapi.cede = api_cede; coroapi.cede_notself = api_cede_notself; coroapi.ready = api_ready; @@ -1520,6 +1724,70 @@ OUTPUT: RETVAL +# for async_pool speedup +void +_pool_1 (SV *cb) + CODE: +{ + 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 (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); + + invoke_av = (AV *)SvRV (invoke); + len = av_len (invoke_av); + + sv_setsv (cb, AvARRAY (invoke_av)[0]); + + if (len > 0) + { + av_fill (defav, len - 1); + for (i = 0; i < len; ++i) + av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); + } + + SvREFCNT_dec (invoke); +} + +void +_pool_2 (SV *cb) + CODE: +{ + struct coro *coro = SvSTATE (coro_current); + + sv_setsv (cb, &PL_sv_undef); + + 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"); + + 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->prio = 0; + + if (coro->cctx && (coro->cctx->flags & CC_TRACE)) + api_trace (coro_current, 0); + + av_push (av_async_pool, newSVsv (coro_current)); +} + + MODULE = Coro::State PACKAGE = Coro::AIO SV *