--- Coro/Coro/State.xs 2007/09/23 21:49:58 1.162 +++ Coro/Coro/State.xs 2007/10/05 21:51:40 1.192 @@ -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 @@ -134,6 +142,9 @@ 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; @@ -142,6 +153,15 @@ 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; @@ -159,7 +179,7 @@ #if CORO_USE_VALGRIND int valgrind_id; #endif - char inuse, mapped; + unsigned char flags; } coro_cctx; enum { @@ -177,14 +197,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 */ @@ -192,8 +211,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; @@ -292,6 +318,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 (SvTYPE (coro) != SVt_PVHV) + croak ("Coro::State object required"); + + 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)) + /* the next two functions merely cache the padlists */ static void get_padlist (pTHX_ CV *cv) @@ -339,11 +391,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) { @@ -351,25 +398,10 @@ # 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; @@ -437,11 +469,10 @@ 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" @@ -504,23 +535,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,9 +560,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) { @@ -560,9 +574,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); @@ -579,13 +593,14 @@ /** 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; @@ -593,27 +608,64 @@ 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); + + ENTER; /* necessary e.g. for dounwind */ { 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 */ +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 @@ -626,6 +678,129 @@ } } +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; + + 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 */ @@ -635,6 +810,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; @@ -660,7 +840,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); @@ -704,7 +885,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 @@ -741,7 +922,7 @@ #endif #if HAVE_MMAP - if (cctx->mapped) + if (cctx->flags & CC_MAPPED) munmap (cctx->sptr, cctx->ssize); else #endif @@ -759,13 +940,12 @@ cctx_first = cctx->next; --cctx_idle; - if (cctx->ssize >= coro_stacksize) + if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE)) return cctx; cctx_destroy (cctx); } - PL_op = PL_op->op_next; return cctx_new (); } @@ -779,7 +959,6 @@ cctx_first = first->next; --cctx_idle; - assert (!first->inuse); cctx_destroy (first); } @@ -790,7 +969,26 @@ /** coroutine switching *****************************************************/ -/* never call directly, always through the coro_state_transfer global variable */ +static void NOINLINE +transfer_check (pTHX_ struct coro *prev, struct coro *next) +{ + if (prev != next) + { + if (!(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 (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"); + + if (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) { @@ -810,21 +1008,10 @@ { /* 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; @@ -837,9 +1024,7 @@ /* 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 { @@ -851,7 +1036,7 @@ prev__cctx = prev->cctx; /* possibly "free" the cctx */ - if (prev__cctx->idle_sp == STACKLEVEL) + if (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)); @@ -859,15 +1044,12 @@ prev->cctx = 0; cctx_put (prev__cctx); - prev__cctx->inuse = 0; } + ++next->usecount; + if (!next->cctx) - { - next->cctx = cctx_get (aTHX); - assert (!next->cctx->inuse); - next->cctx->inuse = 1; - } + next->cctx = cctx_get (aTHX); if (prev__cctx != next->cctx) { @@ -887,6 +1069,7 @@ }; #define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) +#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) /** high level stuff ********************************************************/ @@ -913,18 +1096,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.. */ @@ -979,34 +1157,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 @@ -1019,19 +1175,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 @@ -1133,11 +1276,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); @@ -1208,7 +1351,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 @@ -1219,15 +1388,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; @@ -1250,8 +1419,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; @@ -1262,30 +1430,17 @@ 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: @@ -1381,14 +1536,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)) { @@ -1402,7 +1558,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; @@ -1416,7 +1575,7 @@ } } } - + SV * is_ready (Coro::State coro) PROTOTYPE: $ @@ -1430,6 +1589,9 @@ OUTPUT: RETVAL +void +api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) + SV * has_stack (Coro::State coro) PROTOTYPE: $ @@ -1438,11 +1600,25 @@ 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 @@ -1457,7 +1633,7 @@ 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); + coro_current = get_sv ("Coro::current", FALSE); SvREADONLY_on (coro_current); coro_stash = gv_stashpv ("Coro", TRUE); @@ -1476,7 +1652,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; @@ -1540,15 +1715,19 @@ _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 (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); @@ -1575,15 +1754,22 @@ 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"); av_clear (GvAV (PL_defgv)); - hv_store (SvRV (coro_current), "desc", sizeof ("desc") - 1, + 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)); }