--- Coro/Coro/State.xs 2008/11/19 05:52:42 1.303 +++ Coro/Coro/State.xs 2008/12/15 00:30:40 1.340 @@ -57,7 +57,7 @@ (PERL_REVISION > (a) \ || (PERL_REVISION == (a) \ && (PERL_VERSION > (b) \ - || (PERL_VERSION == (b) && PERLSUBVERSION >= (c))))) + || (PERL_VERSION == (b) && PERL_SUBVERSION >= (c))))) #if !PERL_VERSION_ATLEAST (5,6,0) # ifndef PL_ppaddr @@ -100,6 +100,12 @@ #ifndef CvISXSUB_on # define CvISXSUB_on(cv) (void)cv #endif +#ifndef CvISXSUB +# define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE) +#endif +#ifndef Newx +# define Newx(ptr,nitems,type) New (0,ptr,nitems,type) +#endif /* 5.8.7 */ #ifndef SvRV_set @@ -129,7 +135,7 @@ # define STACKLEVEL ((void *)&stacklevel) #endif -#define IN_DESTRUCT (PL_main_cv == Nullcv) +#define IN_DESTRUCT PL_dirty #if __GNUC__ >= 3 # define attribute(x) __attribute__(x) @@ -169,6 +175,10 @@ static HV *coro_state_stash, *coro_stash; static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ +static AV *av_destroy; /* destruction queue */ +static SV *sv_manager; /* the manager coro */ +static SV *sv_idle; /* $Coro::idle */ + static GV *irsgv; /* $/ */ static GV *stdoutgv; /* *STDOUT */ static SV *rv_diehook; @@ -178,7 +188,11 @@ /* async_pool helper stuff */ static SV *sv_pool_rss; static SV *sv_pool_size; -static AV *av_async_pool; +static SV *sv_async_pool_idle; /* description string */ +static AV *av_async_pool; /* idle pool */ +static SV *sv_Coro; /* class string */ +static CV *cv_pool_handler; +static CV *cv_coro_state_new; /* Coro::AnyEvent */ static SV *sv_activity; @@ -217,6 +231,10 @@ unsigned char flags; } coro_cctx; +coro_cctx *cctx_current; /* the currently running cctx */ + +/*****************************************************************************/ + enum { CF_RUNNING = 0x0001, /* coroutine is running */ CF_READY = 0x0002, /* coroutine is ready */ @@ -231,6 +249,7 @@ AV *defav; SV *errsv; SV *irsgv; + HV *hinthv; #define VAR(name,type) type name; # include "state.h" #undef VAR @@ -248,10 +267,11 @@ 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 */ + CV *startcv; /* the CV to execute */ + 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 */ void (*on_destroy)(pTHX_ struct coro *coro); /* statistics */ @@ -264,6 +284,12 @@ /* async_pool */ SV *saved_deffh; + SV *invoke_cb; + AV *invoke_av; + + /* on_enter/on_leave */ + AV *on_enter; + AV *on_leave; /* linked list */ struct coro *next, *prev; @@ -290,6 +316,7 @@ static SV *coro_current; static SV *coro_readyhook; static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; +static CV *cv_coro_run, *cv_coro_terminate; static struct coro *coro_first; #define coro_nready coroapi.nready @@ -325,8 +352,74 @@ return get_hv (name, create); } +/* may croak */ +INLINE CV * +coro_sv_2cv (pTHX_ SV *sv) +{ + HV *st; + GV *gvp; + CV *cv = sv_2cv (sv, &st, &gvp, 0); + + if (!cv) + croak ("code reference expected"); + + return cv; +} + +/*****************************************************************************/ +/* magic glue */ + +#define CORO_MAGIC_type_cv 26 +#define CORO_MAGIC_type_state PERL_MAGIC_ext + +#define CORO_MAGIC_NN(sv, type) \ + (expect_true (SvMAGIC (sv)->mg_type == type) \ + ? SvMAGIC (sv) \ + : mg_find (sv, type)) + +#define CORO_MAGIC(sv, type) \ + (expect_true (SvMAGIC (sv)) \ + ? CORO_MAGIC_NN (sv, type) \ + : 0) + +#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) +#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) + +INLINE 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)) + +/* faster than SvSTATE, but expects a coroutine hv */ +#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr) +#define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) + +/*****************************************************************************/ +/* padlist management and caching */ + static AV * -coro_clone_padlist (pTHX_ CV *cv) +coro_derive_padlist (pTHX_ CV *cv) { AV *padlist = CvPADLIST (cv); AV *newpadlist, *newpad; @@ -341,7 +434,7 @@ newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; --AvFILLp (padlist); - av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); + av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0])); av_store (newpadlist, 1, (SV *)newpad); return newpadlist; @@ -351,22 +444,26 @@ free_padlist (pTHX_ AV *padlist) { /* may be during global destruction */ - if (SvREFCNT (padlist)) + if (!IN_DESTRUCT) { I32 i = AvFILLp (padlist); - while (i >= 0) + + while (i > 0) /* special-case index 0 */ { - SV **svp = av_fetch (padlist, i--, FALSE); - if (svp) - { - SV *sv; - while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) - SvREFCNT_dec (sv); + /* we try to be extra-careful here */ + AV *av = (AV *)AvARRAY (padlist)[i--]; + I32 j = AvFILLp (av); - SvREFCNT_dec (*svp); - } + while (j >= 0) + SvREFCNT_dec (AvARRAY (av)[j--]); + + AvFILLp (av) = -1; + SvREFCNT_dec (av); } + SvREFCNT_dec (AvARRAY (padlist)[0]); + + AvFILLp (padlist) = -1; SvREFCNT_dec ((SV*)padlist); } } @@ -386,57 +483,11 @@ return 0; } -#define CORO_MAGIC_type_cv 26 -#define CORO_MAGIC_type_state PERL_MAGIC_ext - static MGVTBL coro_cv_vtbl = { 0, 0, 0, 0, coro_cv_free }; -#define CORO_MAGIC_NN(sv, type) \ - (expect_true (SvMAGIC (sv)->mg_type == type) \ - ? SvMAGIC (sv) \ - : mg_find (sv, type)) - -#define CORO_MAGIC(sv, type) \ - (expect_true (SvMAGIC (sv)) \ - ? CORO_MAGIC_NN (sv, type) \ - : 0) - -#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) -#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) - -INLINE 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)) - -/* faster than SvSTATE, but expects a coroutine hv */ -#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr) -#define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) - /* the next two functions merely cache the padlists */ static void get_padlist (pTHX_ CV *cv) @@ -451,12 +502,12 @@ #if CORO_PREFER_PERL_FUNCTIONS /* this is probably cleaner? but also slower! */ /* in practise, it seems to be less stable */ - CV *cp = Perl_cv_clone (cv); + CV *cp = Perl_cv_clone (aTHX_ cv); CvPADLIST (cv) = CvPADLIST (cp); CvPADLIST (cp) = 0; SvREFCNT_dec (cp); #else - CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); + CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); #endif } } @@ -473,7 +524,7 @@ av = (AV *)mg->mg_obj; if (expect_false (AvFILLp (av) >= AvMAX (av))) - av_extend (av, AvMAX (av) + 1); + av_extend (av, AvFILLp (av) + 1); AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); } @@ -481,6 +532,9 @@ /** load & save, init *******************************************************/ static void +on_enterleave_call (pTHX_ SV *cb); + +static void load_perl (pTHX_ Coro__State c) { perl_slots *slot = c->slot; @@ -488,10 +542,11 @@ PL_mainstack = c->mainstack; - GvSV (PL_defgv) = slot->defsv; - GvAV (PL_defgv) = slot->defav; - GvSV (PL_errgv) = slot->errsv; - GvSV (irsgv) = slot->irsgv; + GvSV (PL_defgv) = slot->defsv; + GvAV (PL_defgv) = slot->defav; + GvSV (PL_errgv) = slot->errsv; + GvSV (irsgv) = slot->irsgv; + GvHV (PL_hintgv) = slot->hinthv; #define VAR(name,type) PL_ ## name = slot->name; # include "state.h" @@ -515,11 +570,27 @@ slf_frame = c->slf_frame; CORO_THROW = c->except; + + if (expect_false (c->on_enter)) + { + int i; + + for (i = 0; i <= AvFILLp (c->on_enter); ++i) + on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); + } } static void save_perl (pTHX_ Coro__State c) { + if (expect_false (c->on_leave)) + { + int i; + + for (i = AvFILLp (c->on_leave); i >= 0; --i) + on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]); + } + c->except = CORO_THROW; c->slf_frame = slf_frame; @@ -587,10 +658,11 @@ { 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); + slot->defav = GvAV (PL_defgv); + slot->defsv = DEFSV; + slot->errsv = ERRSV; + slot->irsgv = GvSV (irsgv); + slot->hinthv = GvHV (PL_hintgv); #define VAR(name,type) slot->name = PL_ ## name; # include "state.h" @@ -605,7 +677,7 @@ * not usually need a lot of stackspace. */ #if CORO_PREFER_PERL_FUNCTIONS -# define coro_init_stacks init_stacks +# define coro_init_stacks(thx) init_stacks () #else static void coro_init_stacks (pTHX) @@ -678,6 +750,15 @@ #endif } +#define CORO_RSS \ + rss += sizeof (SYM (curstackinfo)); \ + rss += (SYM (curstackinfo->si_cxmax) + 1) * sizeof (PERL_CONTEXT); \ + rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (SYM (curstack))) * sizeof (SV *); \ + rss += SYM (tmps_max) * sizeof (SV *); \ + rss += (SYM (markstack_max) - SYM (markstack_ptr)) * sizeof (I32); \ + rss += SYM (scopestack_max) * sizeof (I32); \ + rss += SYM (savestack_max) * sizeof (ANY); + static size_t coro_rss (pTHX_ struct coro *coro) { @@ -685,33 +766,17 @@ if (coro->mainstack) { - perl_slots tmp_slot; - perl_slots *slot; - if (coro->flags & CF_RUNNING) { - slot = &tmp_slot; - - #define VAR(name,type) slot->name = PL_ ## name; - # include "state.h" - #undef VAR + #define SYM(sym) PL_ ## sym + CORO_RSS; + #undef SYM } else - slot = coro->slot; - - if (slot) { - 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,10,0) - rss += slot->retstack_max * sizeof (OP *); -#endif + #define SYM(sym) coro->slot->sym + CORO_RSS; + #undef SYM } } @@ -734,11 +799,8 @@ /* * 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 + * and instead of trying 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) @@ -801,7 +863,7 @@ if (svp) { SV *old = *svp; - *svp = newSVsv (sv); + *svp = SvOK (sv) ? newSVsv (sv) : 0; SvREFCNT_dec (old); return 0; } @@ -823,6 +885,12 @@ return 0; } +static int +slf_check_repeat (pTHX_ struct CoroSLF *frame) +{ + return 1; +} + static UNOP coro_setup_op; static void NOINLINE /* noinline to keep it out of the transfer fast path */ @@ -837,6 +905,9 @@ PL_curcop = &PL_compiling; PL_in_eval = EVAL_NULL; PL_comppad = 0; + PL_comppad_name = 0; + PL_comppad_name_fill = 0; + PL_comppad_name_floor = 0; PL_curpm = 0; PL_curpad = 0; PL_localizing = 0; @@ -845,6 +916,7 @@ #if PERL_VERSION_ATLEAST (5,10,0) PL_parser = 0; #endif + PL_hints = 0; /* recreate the die/warn hooks */ PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); @@ -854,6 +926,7 @@ 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); + GvHV (PL_hintgv) = 0; PL_rs = newSVsv (GvSV (irsgv)); PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); @@ -863,14 +936,14 @@ Zero (&myop, 1, UNOP); myop.op_next = Nullop; + myop.op_type = OP_ENTERSUB; myop.op_flags = OPf_WANT_VOID; PUSHMARK (SP); - XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); + PUSHs ((SV *)coro->startcv); PUTBACK; PL_op = (OP *)&myop; PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); - SPAGAIN; } /* this newly created coroutine might be run on an existing cctx which most @@ -881,9 +954,9 @@ /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ coro_setup_op.op_next = PL_op; - coro_setup_op.op_type = OP_CUSTOM; + coro_setup_op.op_type = OP_ENTERSUB; coro_setup_op.op_ppaddr = pp_slf; - /* no flags required, as an init function won't be called */ + /* no flags etc. required, as an init function won't be called */ PL_op = (OP *)&coro_setup_op; @@ -892,7 +965,7 @@ } static void -coro_destruct (pTHX_ struct coro *coro) +coro_unwind_stacks (pTHX) { if (!IN_DESTRUCT) { @@ -910,6 +983,12 @@ /* unwind main stack */ dounwind (-1); } +} + +static void +coro_destruct_perl (pTHX_ struct coro *coro) +{ + coro_unwind_stacks (aTHX); SvREFCNT_dec (GvSV (PL_defgv)); SvREFCNT_dec (GvAV (PL_defgv)); @@ -917,13 +996,15 @@ SvREFCNT_dec (PL_defoutgv); SvREFCNT_dec (PL_rs); SvREFCNT_dec (GvSV (irsgv)); + SvREFCNT_dec (GvHV (PL_hintgv)); SvREFCNT_dec (PL_diehook); SvREFCNT_dec (PL_warnhook); - SvREFCNT_dec (CORO_THROW); SvREFCNT_dec (coro->saved_deffh); SvREFCNT_dec (coro->rouse_cb); + SvREFCNT_dec (coro->invoke_cb); + SvREFCNT_dec (coro->invoke_av); coro_destruct_stacks (aTHX); } @@ -943,16 +1024,14 @@ { COP *oldcop = 0; int oldcxix = -2; - struct coro *coro = SvSTATE_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 (cctx_current->flags & CC_TRACE_ALL) { - if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) + if (PL_op->op_type == OP_LEAVESUB && cctx_current->flags & CC_TRACE_SUB) { PERL_CONTEXT *cx = &cxstack[cxstack_ix]; SV **bot, **top; @@ -999,13 +1078,12 @@ { SV **cb; - if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) + if (oldcxix != cxstack_ix && cctx_current->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)); @@ -1020,7 +1098,7 @@ PUSHMARK (SP); PUSHs (&PL_sv_yes); PUSHs (fullname); - PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); + 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); @@ -1033,7 +1111,7 @@ oldcxix = cxstack_ix; } - if (cctx->flags & CC_TRACE_LINE) + if (cctx_current->flags & CC_TRACE_LINE) { dSP; @@ -1062,14 +1140,12 @@ return 0; } -static struct coro_cctx *cctx_ssl_cctx; static struct CoroSLF cctx_ssl_frame; static void slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) { - ta->prev = (struct coro *)cctx_ssl_cctx; - ta->next = 0; + ta->prev = 0; } static int @@ -1082,11 +1158,11 @@ /* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ static void NOINLINE -cctx_prepare (pTHX_ coro_cctx *cctx) +cctx_prepare (pTHX) { PL_top_env = &PL_start_env; - if (cctx->flags & CC_TRACE) + if (cctx_current->flags & CC_TRACE) PL_runops = runops_trace; /* we already must be executing an SLF op, there is no other valid way @@ -1095,7 +1171,6 @@ slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ - cctx_ssl_cctx = cctx; cctx_ssl_frame = slf_frame; slf_frame.prepare = slf_prepare_set_stacklevel; @@ -1128,7 +1203,7 @@ /* PL_nop = PL_nop->op_next */ /* inject a fake subroutine call to cctx_init */ - cctx_prepare (aTHX_ (coro_cctx *)arg); + cctx_prepare (aTHX); /* cctx_run is the alternative tail of transfer() */ transfer_tail (aTHX); @@ -1136,13 +1211,19 @@ /* somebody or something will hit me for both perl_run and PL_restartop */ PL_restartop = PL_op; perl_run (PL_curinterp); + /* + * Unfortunately, there is no way to get at the return values of the + * coro body here, as perl_run destroys these + */ /* * If perl-run returns we assume exit() was being called or the coro * fell off the end, which seems to be the only valid (non-bug) * reason for perl_run to return. We try to exit by jumping to the * bootstrap-time "top" top_env, as we cannot restore the "main" - * coroutine as Coro has no such concept + * coroutine as Coro has no such concept. + * This actually isn't valid with the pthread backend, but OSes requiring + * that backend are too broken to do it in a standards-compliant way. */ PL_top_env = main_top_env; JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ @@ -1229,6 +1310,8 @@ if (!cctx) return; + assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary? + --cctx_count; coro_destroy (&cctx->cctx); @@ -1301,7 +1384,7 @@ 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,"); + croak ("Coro::State::transfer called with a suspended 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,"); @@ -1323,19 +1406,18 @@ dSTACKLEVEL; /* sometimes transfer is only called to set idle_sp */ - if (expect_false (!next)) + if (expect_false (!prev)) { - ((coro_cctx *)prev)->idle_sp = STACKLEVEL; - assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ + cctx_current->idle_sp = STACKLEVEL; + assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ } else if (expect_true (prev != next)) { - coro_cctx *prev__cctx; + coro_cctx *cctx_prev; if (expect_false (prev->flags & CF_NEW)) { /* create a new empty/source context */ - prev->cctx = cctx_new_empty (); prev->flags &= ~CF_NEW; prev->flags |= CF_RUNNING; } @@ -1356,39 +1438,39 @@ else load_perl (aTHX_ next); - prev__cctx = prev->cctx; - /* possibly untie and reuse the cctx */ if (expect_true ( - prev__cctx->idle_sp == STACKLEVEL - && !(prev__cctx->flags & CC_TRACE) + cctx_current->idle_sp == STACKLEVEL + && !(cctx_current->flags & CC_TRACE) && !force_cctx )) { /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ - assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); + assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->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) + /* 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 running cctx while still in use */ + if (expect_false (CCTX_EXPIRED (cctx_current))) + if (expect_true (!next->cctx)) next->cctx = cctx_get (aTHX); - cctx_put (prev__cctx); + cctx_put (cctx_current); } + else + prev->cctx = cctx_current; ++next->usecount; - if (expect_true (!next->cctx)) - next->cctx = cctx_get (aTHX); + cctx_prev = cctx_current; + cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX); - if (expect_false (prev__cctx != next->cctx)) + next->cctx = 0; + + if (expect_false (cctx_prev != cctx_current)) { - prev__cctx->top_env = PL_top_env; - PL_top_env = next->cctx->top_env; - coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); + cctx_prev->top_env = PL_top_env; + PL_top_env = cctx_current->top_env; + coro_transfer (&cctx_prev->cctx, &cctx_current->cctx); } transfer_tail (aTHX); @@ -1420,24 +1502,29 @@ else coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ - if (coro->mainstack && coro->mainstack != main_mainstack) + if (coro->mainstack + && coro->mainstack != main_mainstack + && coro->slot + && !PL_dirty) { - struct coro temp; + struct coro *current = SvSTATE_current; - assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING))); + assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack)); - save_perl (aTHX_ &temp); + save_perl (aTHX_ current); load_perl (aTHX_ coro); - coro_destruct (aTHX_ coro); + coro_destruct_perl (aTHX_ coro); - load_perl (aTHX_ &temp); + load_perl (aTHX_ current); coro->slot = 0; } cctx_destroy (coro->cctx); + SvREFCNT_dec (coro->startcv); SvREFCNT_dec (coro->args); + SvREFCNT_dec (CORO_THROW); if (coro->next) coro->next->prev = coro->prev; if (coro->prev) coro->prev->next = coro->next; @@ -1551,9 +1638,6 @@ SV *sv_hook; void (*xs_hook)(void); - if (SvROK (coro_sv)) - coro_sv = SvRV (coro_sv); - coro = SvSTATE (coro_sv); if (coro->flags & CF_READY) @@ -1594,56 +1678,72 @@ return !!(SvSTATE (coro_sv)->flags & CF_READY); } +/* expects to own a reference to next->hv */ INLINE void -prepare_schedule (pTHX_ struct coro_transfer_args *ta) +prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) { - SV *prev_sv, *next_sv; + SV *prev_sv = SvRV (coro_current); + + ta->prev = SvSTATE_hv (prev_sv); + ta->next = next; + TRANSFER_CHECK (*ta); + + SvRV_set (coro_current, (SV *)next->hv); + + free_coro_mortal (aTHX); + coro_mortal = prev_sv; +} + +static void +prepare_schedule (pTHX_ struct coro_transfer_args *ta) +{ for (;;) { - next_sv = coro_deq (aTHX); + SV *next_sv = coro_deq (aTHX); - /* nothing to schedule: call the idle handler */ - if (expect_false (!next_sv)) + if (expect_true (next_sv)) { - dSP; - - ENTER; - SAVETMPS; + struct coro *next = SvSTATE_hv (next_sv); - PUSHMARK (SP); - PUTBACK; - call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); + /* cannot transfer to destroyed coros, skip and look for next */ + if (expect_false (next->flags & CF_DESTROYED)) + SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ + else + { + next->flags &= ~CF_READY; + --coro_nready; - FREETMPS; - LEAVE; - continue; + prepare_schedule_to (aTHX_ ta, next); + break; + } } - - ta->next = SvSTATE_hv (next_sv); - - /* cannot transfer to destroyed coros, skip and look for next */ - if (expect_false (ta->next->flags & CF_DESTROYED)) + else { - SvREFCNT_dec (next_sv); - /* coro_nready has already been taken care of by destroy */ - continue; - } + /* nothing to schedule: call the idle handler */ + if (SvROK (sv_idle) + && SvOBJECT (SvRV (sv_idle))) + { + ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ + api_ready (aTHX_ SvRV (sv_idle)); + --coro_nready; + } + else + { + dSP; - --coro_nready; - break; - } + ENTER; + SAVETMPS; - /* free this only after the transfer */ - prev_sv = SvRV (coro_current); - ta->prev = SvSTATE_hv (prev_sv); - TRANSFER_CHECK (*ta); - assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); - ta->next->flags &= ~CF_READY; - SvRV_set (coro_current, next_sv); + PUSHMARK (SP); + PUTBACK; + call_sv (sv_idle, G_VOID | G_DISCARD); - free_coro_mortal (aTHX); - coro_mortal = prev_sv; + FREETMPS; + LEAVE; + } + } + } } INLINE void @@ -1676,6 +1776,16 @@ TRANSFER (ta, 1); } +static void +api_schedule_to (pTHX_ SV *coro_sv) +{ + struct coro_transfer_args ta; + struct coro *next = SvSTATE (coro_sv); + + SvREFCNT_inc_NN (coro_sv); + prepare_schedule_to (aTHX_ &ta, next); +} + static int api_cede (pTHX) { @@ -1712,12 +1822,15 @@ { struct coro *coro = SvSTATE (coro_sv); + if (coro->flags & CF_RUNNING) + croak ("cannot enable tracing on a running coroutine, caught"); + if (flags & CC_TRACE) { if (!coro->cctx) coro->cctx = cctx_new_run (); else if (!(coro->cctx->flags & CC_TRACE)) - croak ("cannot enable tracing on coroutine with custom stack,"); + croak ("cannot enable tracing on coroutine with custom stack, caught"); coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); } @@ -1732,6 +1845,139 @@ } } +static void +coro_call_on_destroy (pTHX_ struct coro *coro) +{ + SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0); + SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0); + + if (on_destroyp) + { + AV *on_destroy = (AV *)SvRV (*on_destroyp); + + while (AvFILLp (on_destroy) >= 0) + { + dSP; /* don't disturb outer sp */ + SV *cb = av_pop (on_destroy); + + PUSHMARK (SP); + + if (statusp) + { + int i; + AV *status = (AV *)SvRV (*statusp); + EXTEND (SP, AvFILLp (status) + 1); + + for (i = 0; i <= AvFILLp (status); ++i) + PUSHs (AvARRAY (status)[i]); + } + + PUTBACK; + call_sv (sv_2mortal (cb), G_VOID | G_DISCARD); + } + } +} + +static void +slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) +{ + int i; + HV *hv = (HV *)SvRV (coro_current); + AV *av = newAV (); + + av_extend (av, items - 1); + for (i = 0; i < items; ++i) + av_push (av, SvREFCNT_inc_NN (arg [i])); + + hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); + + av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ + api_ready (aTHX_ sv_manager); + + frame->prepare = prepare_schedule; + frame->check = slf_check_repeat; + + /* as a minor optimisation, we could unwind all stacks here */ + /* but that puts extra pressure on pp_slf, and is not worth much */ + /*coro_unwind_stacks (aTHX);*/ +} + +/*****************************************************************************/ +/* async pool handler */ + +static int +slf_check_pool_handler (pTHX_ struct CoroSLF *frame) +{ + HV *hv = (HV *)SvRV (coro_current); + struct coro *coro = (struct coro *)frame->data; + + if (!coro->invoke_cb) + return 1; /* loop till we have invoke */ + else + { + hv_store (hv, "desc", sizeof ("desc") - 1, + newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); + + coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv); + + { + dSP; + XPUSHs (sv_2mortal (coro->invoke_cb)); coro->invoke_cb = 0; + PUTBACK; + } + + SvREFCNT_dec (GvAV (PL_defgv)); + GvAV (PL_defgv) = coro->invoke_av; + coro->invoke_av = 0; + + return 0; + } +} + +static void +slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) +{ + HV *hv = (HV *)SvRV (coro_current); + struct coro *coro = SvSTATE_hv ((SV *)hv); + + if (expect_true (coro->saved_deffh)) + { + /* subsequent iteration */ + SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; + coro->saved_deffh = 0; + + if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) + || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) + { + coro->invoke_cb = SvREFCNT_inc_NN ((SV *)cv_coro_terminate); + coro->invoke_av = newAV (); + + frame->prepare = prepare_nop; + } + else + { + av_clear (GvAV (PL_defgv)); + hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0); + + coro->prio = 0; + + if (coro->cctx && (coro->cctx->flags & CC_TRACE)) + api_trace (aTHX_ coro_current, 0); + + frame->prepare = prepare_schedule; + av_push (av_async_pool, SvREFCNT_inc (hv)); + } + } + else + { + /* first iteration, simply fall through */ + frame->prepare = prepare_nop; + } + + frame->check = slf_check_pool_handler; + frame->data = (void *)coro; +} + /*****************************************************************************/ /* rouse callback */ @@ -1746,7 +1992,6 @@ if (SvTYPE (SvRV (data)) != SVt_PVAV) { /* first call, set args */ - int i; AV *av = newAV (); SV *coro = SvRV (data); @@ -1900,6 +2145,34 @@ } static void +slf_prepare_schedule_to (pTHX_ struct coro_transfer_args *ta) +{ + struct coro *next = (struct coro *)slf_frame.data; + + SvREFCNT_inc_NN (next->hv); + prepare_schedule_to (aTHX_ ta, next); +} + +static void +slf_init_schedule_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) +{ + if (!items) + croak ("Coro::schedule_to expects a coroutine argument, caught"); + + frame->data = (void *)SvSTATE (arg [0]); + frame->prepare = slf_prepare_schedule_to; + frame->check = slf_check_nop; +} + +static void +slf_init_cede_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) +{ + api_ready (aTHX_ SvRV (coro_current)); + + slf_init_schedule_to (aTHX_ frame, cv, arg, items); +} + +static void slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) { frame->prepare = prepare_cede; @@ -2052,12 +2325,59 @@ slf_argc = 0; PL_op->op_ppaddr = pp_slf; - PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ + /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ PL_op = (OP *)&slf_restore; } /*****************************************************************************/ +/* dynamic wind */ + +static void +on_enterleave_call (pTHX_ SV *cb) +{ + dSP; + + PUSHSTACK; + + PUSHMARK (SP); + PUTBACK; + call_sv (cb, G_VOID | G_DISCARD); + SPAGAIN; + + POPSTACK; +} + +static SV * +coro_avp_pop_and_free (pTHX_ AV **avp) +{ + AV *av = *avp; + SV *res = av_pop (av); + + if (AvFILLp (av) < 0) + { + *avp = 0; + SvREFCNT_dec (av); + } + + return res; +} + +static void +coro_pop_on_enter (pTHX_ void *coro) +{ + SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_enter); + SvREFCNT_dec (cb); +} + +static void +coro_pop_on_leave (pTHX_ void *coro) +{ + SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave); + on_enterleave_call (aTHX_ sv_2mortal (cb)); +} + +/*****************************************************************************/ /* PerlIO::cede */ typedef struct @@ -2145,7 +2465,13 @@ /* unfortunately, building manually saves memory */ Newx (ary, 2, SV *); AvALLOC (av) = ary; +#if PERL_VERSION_ATLEAST (5,10,0) AvARRAY (av) = ary; +#else + /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */ + /* -DDEBUGGING flags this as a bug, despite it perfectly working */ + SvPVX ((SV *)av) = (char *)ary; +#endif AvMAX (av) = 1; AvFILLp (av) = 0; ary [0] = newSViv (count); @@ -2175,13 +2501,20 @@ cb = av_shift (av); if (SvOBJECT (cb)) - api_ready (aTHX_ cb); - else - croak ("callbacks not yet supported"); + { + api_ready (aTHX_ cb); + --count; + } + else if (SvTYPE (cb) == SVt_PVCV) + { + dSP; + PUSHMARK (SP); + XPUSHs (sv_2mortal (newRV_inc ((SV *)av))); + PUTBACK; + call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR); + } SvREFCNT_dec (cb); - - --count; } } @@ -2273,8 +2606,25 @@ static void slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) { - slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); - frame->check = slf_check_semaphore_wait; + if (items >= 2) + { + /* callback form */ + AV *av = (AV *)SvRV (arg [0]); + CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); + + av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); + + if (SvIVX (AvARRAY (av)[0]) > 0) + coro_semaphore_adjust (aTHX_ av, 0); + + frame->prepare = prepare_nop; + frame->check = slf_check_nop; + } + else + { + slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); + frame->check = slf_check_semaphore_wait; + } } /* signal */ @@ -2356,7 +2706,7 @@ SV *coro = av_pop (state); SV *data_sv = newSV (sizeof (struct io_state)); - av_extend (state, items); + av_extend (state, items - 1); sv_upgrade (data_sv, SVt_PV); SvCUR_set (data_sv, sizeof (struct io_state)); @@ -2501,6 +2851,10 @@ /*****************************************************************************/ +#if CORO_CLONE +# include "clone.c" +#endif + MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ PROTOTYPES: DISABLE @@ -2514,6 +2868,8 @@ #endif BOOT_PAGESIZE; + cctx_current = cctx_new_empty (); + irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); @@ -2542,12 +2898,10 @@ SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); if (!PL_custom_op_names) PL_custom_op_names = newHV (); - hv_store_ent (PL_custom_op_names, slf, - newSVpv ("coro_slf", 0), 0); + hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0); if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); - hv_store_ent (PL_custom_op_descs, slf, - newSVpv ("coro schedule like function", 0), 0); + hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0); } coroapi.ver = CORO_API_VERSION; @@ -2576,13 +2930,30 @@ SV * new (char *klass, ...) + ALIAS: + Coro::new = 1 CODE: { struct coro *coro; MAGIC *mg; HV *hv; + CV *cb; int i; + if (items > 1) + { + cb = coro_sv_2cv (aTHX_ ST (1)); + + if (!ix) + { + if (CvISXSUB (cb)) + croak ("Coro::State doesn't support XS functions as coroutine start, caught"); + + if (!CvROOT (cb)) + croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught"); + } + } + Newz (0, coro, 1, struct coro); coro->args = newAV (); coro->flags = CF_NEW; @@ -2596,9 +2967,21 @@ 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))); + if (items > 1) + { + av_extend (coro->args, items - 1 + ix - 1); + + if (ix) + { + av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb)); + cb = cv_coro_run; + } + + coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb); + + for (i = 2; i < items; i++) + av_push (coro->args, newSVsv (ST (i))); + } } OUTPUT: RETVAL @@ -2622,6 +3005,25 @@ CODE: _exit (code); +SV * +clone (Coro::State coro) + CODE: +{ +#if CORO_CLONE + struct coro *ncoro = coro_clone (aTHX_ coro); + MAGIC *mg; + /* TODO: too much duplication */ + ncoro->hv = newHV (); + mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); + mg->mg_flags |= MGf_DUP; + RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv)); +#else + croak ("Coro::State->clone has not been configured into this installation of Coro, realised"); +#endif +} + OUTPUT: + RETVAL + int cctx_stacksize (int new_stacksize = 0) PROTOTYPE: ;$ @@ -2680,39 +3082,34 @@ { if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) { - struct coro temp; + struct coro *current = SvSTATE_current; - if (!(coro->flags & CF_RUNNING)) + if (current != coro) { PUTBACK; - save_perl (aTHX_ &temp); + save_perl (aTHX_ current); load_perl (aTHX_ coro); + SPAGAIN; } - { - dSP; - ENTER; - SAVETMPS; - PUTBACK; - PUSHSTACK; - PUSHMARK (SP); + PUSHSTACK; + + PUSHMARK (SP); + PUTBACK; - if (ix) - eval_sv (coderef, 0); - else - 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); - POPSTACK; - SPAGAIN; - FREETMPS; - LEAVE; - PUTBACK; - } + SPAGAIN; + POPSTACK; - if (!(coro->flags & CF_RUNNING)) + if (current != coro) { + PUTBACK; save_perl (aTHX_ coro); - load_perl (aTHX_ &temp); + load_perl (aTHX_ current); SPAGAIN; } } @@ -2751,7 +3148,8 @@ has_cctx (Coro::State coro) PROTOTYPE: $ CODE: - RETVAL = boolSV (!!coro->cctx); + /* maybe manage the running flag differently */ + RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING)); OUTPUT: RETVAL @@ -2781,7 +3179,7 @@ force_cctx () PROTOTYPE: CODE: - SvSTATE_current->cctx->idle_sp = 0; + cctx_current->idle_sp = 0; void swap_defsv (Coro::State self) @@ -2806,12 +3204,20 @@ { int i; - av_async_pool = coro_get_av (aTHX_ "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); - - coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); - SvREADONLY_on (coro_current); + sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); + sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); + cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); + cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); + coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); + av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); + av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); + sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); + sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); + + sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); + sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); + cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); + cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); coro_stash = gv_stashpv ("Coro", TRUE); @@ -2829,6 +3235,7 @@ SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); coroapi.schedule = api_schedule; + coroapi.schedule_to = api_schedule_to; coroapi.cede = api_cede; coroapi.cede_notself = api_cede_notself; coroapi.ready = api_ready; @@ -2843,11 +3250,26 @@ } void +terminate (...) + CODE: + CORO_EXECUTE_SLF_XS (slf_init_terminate); + +void schedule (...) CODE: CORO_EXECUTE_SLF_XS (slf_init_schedule); void +schedule_to (...) + CODE: + CORO_EXECUTE_SLF_XS (slf_init_schedule_to); + +void +cede_to (...) + CODE: + CORO_EXECUTE_SLF_XS (slf_init_cede_to); + +void cede (...) CODE: CORO_EXECUTE_SLF_XS (slf_init_cede); @@ -2858,6 +3280,12 @@ CORO_EXECUTE_SLF_XS (slf_init_cede_notself); void +_cancel (Coro::State self) + CODE: + coro_state_destroy (aTHX_ self); + coro_call_on_destroy (aTHX_ self); + +void _set_current (SV *current) PROTOTYPE: $ CODE: @@ -2910,76 +3338,53 @@ OUTPUT: RETVAL -# for async_pool speedup void -_pool_1 (SV *cb) +_pool_handler (...) CODE: -{ - HV *hv = (HV *)SvRV (coro_current); - struct coro *coro = SvSTATE_hv ((SV *)hv); - AV *defav = GvAV (PL_defgv); - SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); - AV *invoke_av; - int i, len; - - if (!invoke) - { - SV *old = PL_diehook; - PL_diehook = 0; - SvREFCNT_dec (old); - croak ("\3async_pool terminate\2\n"); - } - - SvREFCNT_dec (coro->saved_deffh); - coro->saved_deffh = SvREFCNT_inc_NN ((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_NN (AvARRAY (invoke_av)[i + 1])); - } -} + CORO_EXECUTE_SLF_XS (slf_init_pool_handler); void -_pool_2 (SV *cb) - CODE: +async_pool (SV *cv, ...) + PROTOTYPE: &@ + PPCODE: { - HV *hv = (HV *)SvRV (coro_current); - struct coro *coro = SvSTATE_hv ((SV *)hv); - - sv_setsv (cb, &PL_sv_undef); + HV *hv = (HV *)av_pop (av_async_pool); + AV *av = newAV (); + SV *cb = ST (0); + int i; - SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; - coro->saved_deffh = 0; + av_extend (av, items - 2); + for (i = 1; i < items; ++i) + av_push (av, SvREFCNT_inc_NN (ST (i))); - if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) - || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) + if ((SV *)hv == &PL_sv_undef) { - SV *old = PL_diehook; - PL_diehook = 0; - SvREFCNT_dec (old); - croak ("\3async_pool terminate\2\n"); + PUSHMARK (SP); + EXTEND (SP, 2); + PUSHs (sv_Coro); + PUSHs ((SV *)cv_pool_handler); + PUTBACK; + call_sv ((SV *)cv_coro_state_new, G_SCALAR); + SPAGAIN; + + hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); } - av_clear (GvAV (PL_defgv)); - hv_store (hv, "desc", sizeof ("desc") - 1, - newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); + { + struct coro *coro = SvSTATE_hv (hv); - coro->prio = 0; + assert (!coro->invoke_cb); + assert (!coro->invoke_av); + coro->invoke_cb = SvREFCNT_inc (cb); + coro->invoke_av = av; + } - if (coro->cctx && (coro->cctx->flags & CC_TRACE)) - api_trace (aTHX_ coro_current, 0); + api_ready (aTHX_ (SV *)hv); - av_push (av_async_pool, newSVsv (coro_current)); + if (GIMME_V != G_VOID) + XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); + else + SvREFCNT_dec (hv); } SV * @@ -2991,11 +3396,36 @@ RETVAL void -rouse_wait (SV *cb = 0) +rouse_wait (...) PROTOTYPE: ;$ PPCODE: CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); +void +on_enter (SV *block) + ALIAS: + on_leave = 1 + PROTOTYPE: & + CODE: +{ + struct coro *coro = SvSTATE_current; + AV **avp = ix ? &coro->on_leave : &coro->on_enter; + + block = (SV *)coro_sv_2cv (aTHX_ block); + + if (!*avp) + *avp = newAV (); + + av_push (*avp, SvREFCNT_inc (block)); + + if (!ix) + on_enterleave_call (aTHX_ block); + + LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around xs calls */ + SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro); + ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around xs calls */ +} + MODULE = Coro::State PACKAGE = PerlIO::cede @@ -3038,12 +3468,12 @@ coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); void -down (SV *self) +down (...) CODE: CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); void -wait (SV *self) +wait (...) CODE: CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait); @@ -3096,7 +3526,7 @@ RETVAL void -wait (SV *self) +wait (...) CODE: CORO_EXECUTE_SLF_XS (slf_init_signal_wait); @@ -3163,9 +3593,7 @@ _register (char *target, char *proto, SV *req) CODE: { - HV *st; - GV *gvp; - CV *req_cv = sv_2cv (req, &st, &gvp, 0); + CV *req_cv = coro_sv_2cv (aTHX_ req); /* newXSproto doesn't return the CV on 5.8 */ CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); sv_setpv ((SV *)slf_cv, proto);