--- Coro/Coro/State.xs 2007/09/30 13:15:01 1.172 +++ Coro/Coro/State.xs 2007/10/05 23:38:40 1.193 @@ -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; @@ -186,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 */ @@ -202,10 +212,14 @@ #undef VAR /* statistics */ - int usecount; /* number of switches to this coro */ + 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; @@ -377,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) { @@ -389,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; @@ -475,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" @@ -542,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; @@ -586,7 +562,7 @@ static size_t coro_rss (pTHX_ struct coro *coro) { - size_t rss = sizeof (coro); + size_t rss = sizeof (*coro); if (coro->mainstack) { @@ -598,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); @@ -617,7 +593,7 @@ /** coroutine stack handling ************************************************/ static void -setup_coro (pTHX_ struct coro *coro) +coro_setup (pTHX_ struct coro *coro) { /* * emulate part of the perl startup here. @@ -632,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 @@ -677,98 +690,110 @@ { PERL_ASYNC_CHECK (); - if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) + if (cctx->flags & CC_TRACE_ALL) { - PERL_CONTEXT *cx = &cxstack[cxstack_ix]; - SV **bot, **top; - AV *av = newAV (); /* return values */ - SV **cb; - runops_proc_t old_runops = PL_runops; - dSP; - ENTER; - SAVETMPS; - EXTEND (SP, 3); - PL_runops = RUNOPS_DEFAULT; - - 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++)); - - 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 = old_runops; - } - - if (oldcop != PL_curcop) - { - oldcop = PL_curcop; - - if (PL_curcop != &PL_compiling) + 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; - 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); + + 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); - PL_runops = RUNOPS_DEFAULT; + 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 (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) + if (oldcop != PL_curcop) + { + oldcop = PL_curcop; + + if (PL_curcop != &PL_compiling) { - PERL_CONTEXT *cx = &cxstack[cxstack_ix]; + 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; + } - if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) + oldcxix = cxstack_ix; + } + + if (cctx->flags & CC_TRACE_LINE) { - GV *gv = CvGV (cx->blk_sub.cv); - SV *fullname = sv_2mortal (newSV (0)); - if (isGV (gv)) - gv_efullname3 (fullname, gv, 0); + dSP; + PL_runops = RUNOPS_DEFAULT; + ENTER; + SAVETMPS; + EXTEND (SP, 3); + PL_runops = RUNOPS_DEFAULT; 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); + PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); + PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); PUTBACK; - cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); + 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; } - - oldcxix = cxstack_ix; } - - if (cctx->flags & CC_TRACE_LINE) - { - 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 = old_runops; } } } @@ -907,6 +932,9 @@ 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) { @@ -916,7 +944,7 @@ cctx_first = cctx->next; --cctx_idle; - if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE)) + if (!CCTX_EXPIRED (cctx)) return cctx; cctx_destroy (cctx); @@ -1000,7 +1028,7 @@ /* first get rid of the old state */ save_perl (aTHX_ prev); /* setup coroutine call */ - setup_coro (aTHX_ next); + coro_setup (aTHX_ next); } else { @@ -1019,6 +1047,10 @@ prev->cctx = 0; + /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ + if (CCTX_EXPIRED (prev__cctx)) + next->cctx = cctx_get (aTHX); + cctx_put (prev__cctx); } @@ -1072,18 +1104,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.. */ @@ -1156,19 +1183,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 @@ -1345,7 +1359,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 @@ -1356,6 +1396,9 @@ #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, "CC_TRACE" , newSViv (CC_TRACE)); @@ -1363,14 +1406,6 @@ newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE)); newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL)); - 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)); - main_mainstack = PL_mainstack; main_top_env = PL_top_env; @@ -1392,8 +1427,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; @@ -1404,30 +1438,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: @@ -1532,7 +1552,6 @@ { struct coro temp; Zero (&temp, 1, struct coro); - temp.save = CORO_SAVE_ALL; if (!(coro->flags & CF_RUNNING)) { @@ -1578,28 +1597,7 @@ RETVAL void -trace (Coro::State coro, int flags = CC_TRACE | CC_TRACE_SUB) - CODE: - 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 |= flags & (CC_TRACE | CC_TRACE_ALL); - } - else - if (coro->cctx && coro->cctx->flags & CC_TRACE) - { - coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL); - coro->cctx->flags |= CC_NOREUSE; - - if (coro->flags & CF_RUNNING) - PL_runops = RUNOPS_DEFAULT; - else - coro->runops = RUNOPS_DEFAULT; - } +api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) SV * has_stack (Coro::State coro) @@ -1642,7 +1640,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); @@ -1661,7 +1659,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; @@ -1725,15 +1722,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); @@ -1760,6 +1761,9 @@ 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"); @@ -1767,8 +1771,12 @@ 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)); }