--- Coro/Coro/State.xs 2007/04/13 12:56:55 1.148 +++ Coro/Coro/State.xs 2007/09/30 13:43:45 1.175 @@ -90,10 +90,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) @@ -135,9 +134,23 @@ static HV *coro_state_stash, *coro_stash; static SV *coro_mortal; /* will be freed after next transfer */ +/* 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 +168,7 @@ #if CORO_USE_VALGRIND int valgrind_id; #endif - char inuse, mapped; + unsigned char flags; } coro_cctx; enum { @@ -188,8 +201,15 @@ # include "state.h" #undef VAR + /* statistics */ + int usecount; /* number of switches to this coro */ + /* coro process data */ int prio; + + /* linked list */ + struct coro *next, *prev; + HV *hv; /* the perl hash associated with this coro, if any */ }; typedef struct coro *Coro__State; @@ -208,6 +228,7 @@ static SV *coro_current; static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; static int coro_nready; +static struct coro *coro_first; /** lowlevel stuff **********************************************************/ @@ -283,6 +304,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) @@ -376,13 +423,11 @@ PUTBACK; } - assert (!PL_comppad || AvARRAY (PL_comppad));//D } static void save_perl (pTHX_ Coro__State c) { - assert (!PL_comppad || AvARRAY (PL_comppad));//D { dSP; I32 cxix = cxstack_ix; @@ -394,8 +439,7 @@ * (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 (;;) { @@ -403,7 +447,7 @@ { PERL_CONTEXT *cx = &ccstk[cxix--]; - if (CxTYPE (cx) == CXt_SUB) + if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT) { CV *cv = cx->blk_sub.cv; @@ -424,8 +468,8 @@ 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; @@ -454,7 +498,7 @@ static void coro_init_stacks (pTHX) { - PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); + 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 */ @@ -463,31 +507,31 @@ PL_stack_sp = PL_stack_base; PL_stack_max = PL_stack_base + AvMAX(PL_curstack); - New(50,PL_tmps_stack,128,SV*); + New(50,PL_tmps_stack,64,SV*); PL_tmps_floor = -1; PL_tmps_ix = -1; - PL_tmps_max = 128; + PL_tmps_max = 64; - New(54,PL_markstack,32,I32); + New(54,PL_markstack,16,I32); PL_markstack_ptr = PL_markstack; - PL_markstack_max = PL_markstack + 32; + PL_markstack_max = PL_markstack + 16; #ifdef SET_MARK_OFFSET SET_MARK_OFFSET; #endif - New(54,PL_scopestack,32,I32); + New(54,PL_scopestack,16,I32); PL_scopestack_ix = 0; - PL_scopestack_max = 32; + PL_scopestack_max = 16; New(54,PL_savestack,64,ANY); PL_savestack_ix = 0; PL_savestack_max = 64; #if !PERL_VERSION_ATLEAST (5,9,0) - New(54,PL_retstack,16,OP*); + New(54,PL_retstack,4,OP*); PL_retstack_ix = 0; - PL_retstack_max = 16; + PL_retstack_max = 4; #endif } #endif @@ -539,6 +583,37 @@ #endif } +static size_t +coro_rss (pTHX_ struct coro *coro) +{ + size_t rss = sizeof (coro); + + if (coro->mainstack) + { + if (coro->flags & CF_RUNNING) + { + #define VAR(name,type)coro->name = PL_ ## name; + # include "state.h" + #undef VAR + } + + rss += sizeof (coro->curstackinfo); + rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *); + rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); + rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); + rss += coro->tmps_max * sizeof (SV *); + rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); + rss += coro->scopestack_max * sizeof (I32); + rss += coro->savestack_max * sizeof (ANY); + +#if !PERL_VERSION_ATLEAST (5,9,0) + rss += coro->retstack_max * sizeof (OP *); +#endif + } + + return rss; +} + /** coroutine stack handling ************************************************/ static void @@ -549,6 +624,7 @@ */ coro_init_stacks (aTHX); + PL_runops = RUNOPS_DEFAULT; PL_curcop = &PL_compiling; PL_in_eval = EVAL_NULL; PL_comppad = 0; @@ -569,7 +645,7 @@ myop.op_flags = OPf_WANT_VOID; PUSHMARK (SP); - XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); + XPUSHs (av_shift (GvAV (PL_defgv))); PUTBACK; PL_op = (OP *)&myop; PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); @@ -589,15 +665,143 @@ } } +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 shoukld be careful, as it could be called at almost any time */ -/* during execution of a pelr program */ +/* _cctx_init should be careful, as it could be called at almost any time */ +/* during execution of a perl program */ static void NOINLINE prepare_cctx (pTHX_ coro_cctx *cctx) { 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; @@ -623,7 +827,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); @@ -632,10 +837,13 @@ PL_restartop = PL_op; perl_run (PL_curinterp); - /* If perl-run returns we assume exit() was being called, 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 */ + /* + * 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 + */ PL_top_env = main_top_env; JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ } @@ -664,7 +872,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 @@ -701,7 +909,7 @@ #endif #if HAVE_MMAP - if (cctx->mapped) + if (cctx->flags & CC_MAPPED) munmap (cctx->sptr, cctx->ssize); else #endif @@ -719,13 +927,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 (); } @@ -739,7 +946,6 @@ cctx_first = first->next; --cctx_idle; - assert (!first->inuse); cctx_destroy (first); } @@ -750,7 +956,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) { @@ -770,21 +995,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; @@ -798,8 +1012,6 @@ save_perl (aTHX_ prev); /* setup coroutine call */ setup_coro (aTHX_ next); - /* need a new stack */ - assert (!next->cctx); } else { @@ -811,7 +1023,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)); @@ -819,15 +1031,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) { @@ -847,6 +1056,7 @@ }; #define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) +#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) /** high level stuff ********************************************************/ @@ -876,7 +1086,7 @@ assert (!(coro->flags & CF_RUNNING)); Zero (&temp, 1, struct coro); - temp.save = CORO_SAVE_DEF; + temp.save = CORO_SAVE_ALL; if (coro->flags & CF_RUNNING) croak ("FATAL: tried to destroy currently running coroutine"); @@ -894,6 +1104,10 @@ cctx_destroy (coro->cctx); SvREFCNT_dec (coro->args); + if (coro->next) coro->next->prev = coro->prev; + if (coro->prev) coro->prev->next = coro->next; + if (coro == coro_first) coro_first = coro->next; + return 1; } @@ -903,6 +1117,8 @@ struct coro *coro = (struct coro *)mg->mg_ptr; mg->mg_ptr = 0; + coro->hv = 0; + if (--coro->refcnt < 0) { coro_state_destroy (aTHX_ coro); @@ -933,34 +1149,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 @@ -1087,11 +1281,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); @@ -1162,7 +1356,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 @@ -1175,6 +1395,11 @@ coro_state_stash = gv_stashpv ("Coro::State", TRUE); + 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)); + 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)); @@ -1208,7 +1433,11 @@ coro->save = CORO_SAVE_DEF; coro->flags = CF_NEW; - hv = newHV (); + if (coro_first) coro_first->prev = coro; + coro->next = coro_first; + coro_first = coro; + + coro->hv = hv = newHV (); sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); @@ -1320,12 +1549,118 @@ OUTPUT: RETVAL +void +list () + PPCODE: +{ + struct coro *coro; + for (coro = coro_first; coro; coro = coro->next) + if (coro->hv) + XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); +} + +void +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)) + { + save_perl (aTHX_ &temp); + load_perl (aTHX_ coro); + } + + { + dSP; + ENTER; + SAVETMPS; + PUSHMARK (SP); + PUTBACK; + if (ix) + eval_sv (coderef, 0); + else + call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); + SPAGAIN; + FREETMPS; + LEAVE; + PUTBACK; + } + + if (!(coro->flags & CF_RUNNING)) + { + save_perl (aTHX_ coro); + load_perl (aTHX_ &temp); + } + } +} + +SV * +is_ready (Coro::State coro) + PROTOTYPE: $ + ALIAS: + is_ready = CF_READY + is_running = CF_RUNNING + is_new = CF_NEW + is_destroyed = CF_DESTROYED + CODE: + RETVAL = boolSV (coro->flags & ix); + 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: + switch (ix) + { + case 0: RETVAL = coro_rss (aTHX_ coro); break; + case 1: RETVAL = coro->usecount; break; + } + OUTPUT: + RETVAL + + MODULE = Coro::State PACKAGE = Coro BOOT: { 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)); @@ -1335,9 +1670,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 (); @@ -1396,14 +1728,6 @@ OUTPUT: RETVAL -SV * -is_ready (SV *self) - PROTOTYPE: $ - CODE: - RETVAL = boolSV (api_is_ready (self)); - OUTPUT: - RETVAL - int nready (...) PROTOTYPE: @@ -1412,6 +1736,64 @@ OUTPUT: RETVAL +# for async_pool speedup +void +_pool_1 (SV *cb) + CODE: +{ + int i, len; + HV *hv = (HV *)SvRV (coro_current); + AV *defav = GvAV (PL_defgv); + SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); + AV *invoke_av; + + if (!invoke) + croak ("\3terminate\2\n"); + + 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); + + 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->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)); +} + + MODULE = Coro::State PACKAGE = Coro::AIO SV *