--- Coro/Coro/State.xs 2007/01/04 23:49:27 1.134 +++ Coro/Coro/State.xs 2007/09/23 21:49:58 1.162 @@ -1,5 +1,7 @@ #include "libcoro/coro.c" +#define PERL_NO_GET_CONTEXT + #include "EXTERN.h" #include "perl.h" #include "XSUB.h" @@ -33,8 +35,11 @@ # define BOOT_PAGESIZE (void)0 #endif -#if USE_VALGRIND +#if CORO_USE_VALGRIND # include +# define REGISTER_STACK(cctx,start,end) (cctx)->valgrind_id = VALGRIND_STACK_REGISTER ((start), (end)) +#else +# define REGISTER_STACK(cctx,start,end) #endif /* the maximum number of idle cctx that will be pooled */ @@ -73,22 +78,21 @@ #endif #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 -# undef STACKGUARD +# undef CORO_STACKGUARD #endif -#ifndef STACKGUARD -# define STACKGUARD 0 +#ifndef CORO_STACKGUARD +# define CORO_STACKGUARD 0 #endif /* prefer perl internal functions over our own? */ -#ifndef PREFER_PERL_FUNCTIONS -# define PREFER_PERL_FUNCTIONS 0 +#ifndef CORO_PREFER_PERL_FUNCTIONS +# 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) @@ -114,6 +118,7 @@ # define UNLOCK (void)0 #endif +/* helper storage struct for Coro::AIO */ struct io_state { int errorno; @@ -122,11 +127,18 @@ Stat_t statcache; }; +static size_t coro_stacksize = CORO_STACKSIZE; static struct CoroAPI coroapi; static AV *main_mainstack; /* used to differentiate between $main and others */ +static JMPENV *main_top_env; 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; @@ -136,7 +148,7 @@ /* the stack */ void *sptr; - long ssize; /* positive == mmap, otherwise malloc */ + size_t ssize; /* cpu state */ void *idle_sp; /* sp of top-level transfer/schedule/cede call */ @@ -144,11 +156,10 @@ JMPENV *top_env; coro_context cctx; - int inuse; - -#if USE_VALGRIND +#if CORO_USE_VALGRIND int valgrind_id; #endif + char inuse, mapped; } coro_cctx; enum { @@ -173,6 +184,7 @@ AV *defav; /* @_ */ SV *defsv; /* $_ */ SV *errsv; /* $@ */ + GV *deffh; /* default filehandle */ SV *irssv; /* $/ */ SV *irssv_sv; /* real $/ cache */ @@ -182,13 +194,34 @@ /* 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; typedef struct coro *Coro__State_or_hashref; +/** Coro ********************************************************************/ + +#define PRIO_MAX 3 +#define PRIO_HIGH 1 +#define PRIO_NORMAL 0 +#define PRIO_LOW -1 +#define PRIO_IDLE -3 +#define PRIO_MIN -4 + +/* for Coro.pm */ +static SV *coro_current; +static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; +static int coro_nready; +static struct coro *coro_first; + +/** lowlevel stuff **********************************************************/ + static AV * -coro_clone_padlist (CV *cv) +coro_clone_padlist (pTHX_ CV *cv) { AV *padlist = CvPADLIST (cv); AV *newpadlist, *newpad; @@ -210,7 +243,7 @@ } static void -free_padlist (AV *padlist) +free_padlist (pTHX_ AV *padlist) { /* may be during global destruction */ if (SvREFCNT (padlist)) @@ -241,7 +274,7 @@ /* casting is fun. */ while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) - free_padlist (padlist); + free_padlist (aTHX_ padlist); SvREFCNT_dec (av); @@ -261,7 +294,7 @@ /* the next two functions merely cache the padlists */ static void -get_padlist (CV *cv) +get_padlist (pTHX_ CV *cv) { MAGIC *mg = CORO_MAGIC (cv); AV *av; @@ -270,20 +303,20 @@ CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; else { -#if PREFER_PERL_FUNCTIONS +#if CORO_PREFER_PERL_FUNCTIONS /* this is probably cleaner, but also slower? */ CV *cp = Perl_cv_clone (cv); CvPADLIST (cv) = CvPADLIST (cp); CvPADLIST (cp) = 0; SvREFCNT_dec (cp); #else - CvPADLIST (cv) = coro_clone_padlist (cv); + CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); #endif } } static void -put_padlist (CV *cv) +put_padlist (pTHX_ CV *cv) { MAGIC *mg = CORO_MAGIC (cv); AV *av; @@ -304,13 +337,15 @@ AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); } +/** 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 (Coro__State c) +load_perl (pTHX_ Coro__State c) { #define VAR(name,type) PL_ ## name = c->name; # include "state.h" @@ -319,10 +354,15 @@ 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); + { + SvREFCNT_dec (c->irssv); + c->irssv = 0; + } else { REPLACE_SV (PL_rs, c->irssv); @@ -338,7 +378,7 @@ /* now do the ugly restore mess */ while ((cv = (CV *)POPs)) { - put_padlist (cv); /* mark this padlist as available */ + put_padlist (aTHX_ cv); /* mark this padlist as available */ CvDEPTH (cv) = PTR2IV (POPs); CvPADLIST (cv) = (AV *)POPs; } @@ -348,7 +388,7 @@ } static void -save_perl (Coro__State c) +save_perl (pTHX_ Coro__State c) { { dSP; @@ -361,8 +401,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 (;;) { @@ -370,7 +409,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; @@ -382,7 +421,7 @@ PUSHs ((SV *)cv); CvDEPTH (cv) = 0; - get_padlist (cv); + get_padlist (aTHX_ cv); } } } @@ -391,8 +430,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; @@ -401,6 +440,7 @@ 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; #define VAR(name,type)c->name = PL_ ## name; @@ -414,13 +454,13 @@ * on the (sometimes correct) assumption that coroutines do * not usually need a lot of stackspace. */ -#if PREFER_PERL_FUNCTIONS +#if CORO_PREFER_PERL_FUNCTIONS # define coro_init_stacks init_stacks #else static void -coro_init_stacks () +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 */ @@ -429,31 +469,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 @@ -462,7 +502,7 @@ * destroy the stacks, the callchain etc... */ static void -coro_destroy_stacks () +coro_destroy_stacks (pTHX) { if (!IN_DESTRUCT) { @@ -505,17 +545,50 @@ #endif } +static size_t +coro_rss (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 -setup_coro (struct coro *coro) +setup_coro (pTHX_ struct coro *coro) { /* * emulate part of the perl startup here. */ - - coro_init_stacks (); + coro_init_stacks (aTHX); PL_curcop = &PL_compiling; PL_in_eval = EVAL_NULL; + PL_comppad = 0; PL_curpm = 0; PL_localizing = 0; PL_dirty = 0; @@ -533,7 +606,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); @@ -544,7 +617,7 @@ } static void -free_coro_mortal () +free_coro_mortal (pTHX) { if (coro_mortal) { @@ -554,8 +627,10 @@ } /* 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 */ static void NOINLINE -prepare_cctx (coro_cctx *cctx) +prepare_cctx (pTHX_ coro_cctx *cctx) { dSP; LOGOP myop; @@ -574,32 +649,43 @@ SPAGAIN; } +/* + * this is a _very_ stripped down perl interpreter ;) + */ static void coro_run (void *arg) { + dTHX; + /* coro_run is the alternative tail of transfer(), so unlock here. */ UNLOCK; - /* - * this is a _very_ stripped down perl interpreter ;) - */ PL_top_env = &PL_start_env; - /* inject call to cctx_init */ - prepare_cctx ((coro_cctx *)arg); + /* inject a fake subroutine call to cctx_init */ + prepare_cctx (aTHX_ (coro_cctx *)arg); - /* somebody will hit me for both perl_run and PL_restartop */ + /* somebody or something will hit me for both perl_run and PL_restartop */ PL_restartop = PL_op; perl_run (PL_curinterp); - fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); - abort (); + /* + * 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 */ } static coro_cctx * cctx_new () { coro_cctx *cctx; + void *stack_start; + size_t stack_size; ++cctx_count; @@ -607,41 +693,37 @@ #if HAVE_MMAP - cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; + cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; /* mmap supposedly does allocate-on-write for us */ cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); - if (cctx->sptr == (void *)-1) + if (cctx->sptr != (void *)-1) { - perror ("FATAL: unable to mmap stack for coroutine"); - _exit (EXIT_FAILURE); - } - -# if STACKGUARD - mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); +# if CORO_STACKGUARD + mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); # endif - -#else - - cctx->ssize = STACKSIZE * (long)sizeof (long); - New (0, cctx->sptr, STACKSIZE, long); - - if (!cctx->sptr) - { - perror ("FATAL: unable to malloc stack for coroutine"); - _exit (EXIT_FAILURE); + stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; + stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; + cctx->mapped = 1; } - + else #endif + { + cctx->ssize = coro_stacksize * (long)sizeof (long); + New (0, cctx->sptr, coro_stacksize, long); -#if USE_VALGRIND - cctx->valgrind_id = VALGRIND_STACK_REGISTER ( - STACKGUARD * PAGESIZE + (char *)cctx->sptr, - cctx->ssize + (char *)cctx->sptr - ); -#endif + if (!cctx->sptr) + { + perror ("FATAL: unable to allocate stack for coroutine"); + _exit (EXIT_FAILURE); + } - coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); + stack_start = cctx->sptr; + stack_size = cctx->ssize; + } + + REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); + coro_create (&cctx->cctx, coro_run, (void *)cctx, stack_start, stack_size); return cctx; } @@ -654,37 +736,37 @@ --cctx_count; -#if USE_VALGRIND +#if CORO_USE_VALGRIND VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); #endif #if HAVE_MMAP - munmap (cctx->sptr, cctx->ssize); -#else - Safefree (cctx->sptr); + if (cctx->mapped) + munmap (cctx->sptr, cctx->ssize); + else #endif + Safefree (cctx->sptr); Safefree (cctx); } static coro_cctx * -cctx_get () +cctx_get (pTHX) { - coro_cctx *cctx; - - if (cctx_first) + while (cctx_first) { - cctx = cctx_first; + coro_cctx *cctx = cctx_first; cctx_first = cctx->next; --cctx_idle; + + if (cctx->ssize >= coro_stacksize) + return cctx; + + cctx_destroy (cctx); } - else - { - cctx = cctx_new (); - PL_op = PL_op->op_next; - } - return cctx; + PL_op = PL_op->op_next; + return cctx_new (); } static void @@ -706,9 +788,11 @@ cctx_first = cctx; } +/** coroutine switching *****************************************************/ + /* never call directly, always through the coro_state_transfer global variable */ static void NOINLINE -transfer (struct coro *prev, struct coro *next) +transfer (pTHX_ struct coro *prev, struct coro *next) { dSTACKLEVEL; @@ -751,17 +835,17 @@ /* need to start coroutine */ next->flags &= ~CF_NEW; /* first get rid of the old state */ - save_perl (prev); + save_perl (aTHX_ prev); /* setup coroutine call */ - setup_coro (next); + setup_coro (aTHX_ next); /* need a new stack */ assert (!next->cctx); } else { /* coroutine already started */ - save_perl (prev); - load_perl (next); + save_perl (aTHX_ prev); + load_perl (aTHX_ next); } prev__cctx = prev->cctx; @@ -780,7 +864,7 @@ if (!next->cctx) { - next->cctx = cctx_get (); + next->cctx = cctx_get (aTHX); assert (!next->cctx->inuse); next->cctx->inuse = 1; } @@ -792,7 +876,7 @@ coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); } - free_coro_mortal (); + free_coro_mortal (aTHX); UNLOCK; } } @@ -802,33 +886,47 @@ struct coro *prev, *next; }; -#define TRANSFER(ta) transfer ((ta).prev, (ta).next) +#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) + +/** high level stuff ********************************************************/ static int -coro_state_destroy (struct coro *coro) +coro_state_destroy (pTHX_ struct coro *coro) { if (coro->flags & CF_DESTROYED) return 0; coro->flags |= CF_DESTROYED; + + if (coro->flags & CF_READY) + { + /* reduce nready, as destroying a ready coro effectively unreadies it */ + /* alternative: look through all ready queues and remove the coro */ + LOCK; + --coro_nready; + UNLOCK; + } + else + coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ if (coro->mainstack && coro->mainstack != main_mainstack) { + struct coro temp; + assert (!(coro->flags & CF_RUNNING)); - struct coro temp; 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 (&temp); - load_perl (coro); + save_perl (aTHX_ &temp); + load_perl (aTHX_ coro); - coro_destroy_stacks (); + coro_destroy_stacks (aTHX); - load_perl (&temp); /* this will get rid of defsv etc.. */ + load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ coro->mainstack = 0; } @@ -836,6 +934,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; } @@ -845,9 +947,11 @@ struct coro *coro = (struct coro *)mg->mg_ptr; mg->mg_ptr = 0; + coro->hv = 0; + if (--coro->refcnt < 0) { - coro_state_destroy (coro); + coro_state_destroy (aTHX_ coro); Safefree (coro); } @@ -876,7 +980,7 @@ }; static struct coro * -SvSTATE (SV *coro) +SvSTATE_ (pTHX_ SV *coro) { HV *stash; MAGIC *mg; @@ -892,13 +996,14 @@ croak ("Coro::State object required"); } - mg = SvMAGIC (coro); - assert (mg->mg_type == PERL_MAGIC_ext); + mg = CORO_MAGIC (coro); return (struct coro *)mg->mg_ptr; } +#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) + static void -prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) +prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) { ta->prev = SvSTATE (prev_sv); ta->next = SvSTATE (next_sv); @@ -907,15 +1012,17 @@ static void api_transfer (SV *prev_sv, SV *next_sv) { + dTHX; struct transfer_args ta; - prepare_transfer (&ta, prev_sv, next_sv); + prepare_transfer (aTHX_ &ta, prev_sv, next_sv); TRANSFER (ta); } static int api_save (SV *coro_sv, int new_save) { + dTHX; struct coro *coro = SvSTATE (coro_sv); int old_save = coro->save; @@ -927,27 +1034,14 @@ /** Coro ********************************************************************/ -#define PRIO_MAX 3 -#define PRIO_HIGH 1 -#define PRIO_NORMAL 0 -#define PRIO_LOW -1 -#define PRIO_IDLE -3 -#define PRIO_MIN -4 - -/* for Coro.pm */ -static SV *coro_current; -static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; -static int coro_nready; - static void -coro_enq (SV *coro_sv) +coro_enq (pTHX_ SV *coro_sv) { av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); - coro_nready++; } static SV * -coro_deq (int min_prio) +coro_deq (pTHX_ int min_prio) { int prio = PRIO_MAX - PRIO_MIN; @@ -957,10 +1051,7 @@ for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) if (AvFILLp (coro_ready [prio]) >= 0) - { - coro_nready--; - return av_shift (coro_ready [prio]); - } + return av_shift (coro_ready [prio]); return 0; } @@ -968,6 +1059,7 @@ static int api_ready (SV *coro_sv) { + dTHX; struct coro *coro; if (SvROK (coro_sv)) @@ -981,7 +1073,8 @@ coro->flags |= CF_READY; LOCK; - coro_enq (SvREFCNT_inc (coro_sv)); + coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); + ++coro_nready; UNLOCK; return 1; @@ -990,24 +1083,25 @@ static int api_is_ready (SV *coro_sv) { + dTHX; return !!(SvSTATE (coro_sv)->flags & CF_READY); } static void -prepare_schedule (struct transfer_args *ta) +prepare_schedule (pTHX_ struct transfer_args *ta) { SV *prev_sv, *next_sv; for (;;) { LOCK; - next_sv = coro_deq (PRIO_MIN); - UNLOCK; + next_sv = coro_deq (aTHX_ PRIO_MIN); /* nothing to schedule: call the idle handler */ if (!next_sv) { dSP; + UNLOCK; ENTER; SAVETMPS; @@ -1026,10 +1120,14 @@ /* cannot transfer to destroyed coros, skip and look for next */ if (ta->next->flags & CF_DESTROYED) { + UNLOCK; SvREFCNT_dec (next_sv); + /* coro_nready is already taken care of by destroy */ continue; } + --coro_nready; + UNLOCK; break; } @@ -1042,25 +1140,25 @@ ta->next->flags &= ~CF_READY; LOCK; - free_coro_mortal (); + free_coro_mortal (aTHX); coro_mortal = prev_sv; UNLOCK; } static void -prepare_cede (struct transfer_args *ta) +prepare_cede (pTHX_ struct transfer_args *ta) { api_ready (coro_current); - prepare_schedule (ta); + prepare_schedule (aTHX_ ta); } static int -prepare_cede_notself (struct transfer_args *ta) +prepare_cede_notself (pTHX_ struct transfer_args *ta) { if (coro_nready) { SV *prev = SvRV (coro_current); - prepare_schedule (ta); + prepare_schedule (aTHX_ ta); api_ready (prev); return 1; } @@ -1071,18 +1169,20 @@ static void api_schedule (void) { + dTHX; struct transfer_args ta; - prepare_schedule (&ta); + prepare_schedule (aTHX_ &ta); TRANSFER (ta); } static int api_cede (void) { + dTHX; struct transfer_args ta; - prepare_cede (&ta); + prepare_cede (aTHX_ &ta); if (ta.prev != ta.next) { @@ -1096,9 +1196,10 @@ static int api_cede_notself (void) { + dTHX; struct transfer_args ta; - if (prepare_cede_notself (&ta)) + if (prepare_cede_notself (aTHX_ &ta)) { TRANSFER (ta); return 1; @@ -1124,9 +1225,15 @@ 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; + + while (main_top_env->je_prev) + main_top_env = main_top_env->je_prev; coroapi.ver = CORO_API_VERSION; coroapi.transfer = api_transfer; @@ -1144,10 +1251,14 @@ Newz (0, coro, 1, struct coro); coro->args = newAV (); - coro->save = CORO_SAVE_ALL; + 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)); @@ -1164,6 +1275,17 @@ 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 + void _set_stacklevel (...) ALIAS: @@ -1186,19 +1308,19 @@ if (items != 2) croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); - prepare_transfer (&ta, ST (0), ST (1)); + prepare_transfer (aTHX_ &ta, ST (0), ST (1)); break; case 2: - prepare_schedule (&ta); + prepare_schedule (aTHX_ &ta); break; case 3: - prepare_cede (&ta); + prepare_cede (aTHX_ &ta); break; case 4: - if (!prepare_cede_notself (&ta)) + if (!prepare_cede_notself (aTHX_ &ta)) XSRETURN_EMPTY; break; @@ -1206,12 +1328,15 @@ BARRIER; TRANSFER (ta); + + if (GIMME_V != G_VOID && ta.next != ta.prev) + XSRETURN_YES; } bool _destroy (SV *coro_sv) CODE: - RETVAL = coro_state_destroy (SvSTATE (coro_sv)); + RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); OUTPUT: RETVAL @@ -1223,6 +1348,15 @@ _exit (code); int +cctx_stacksize (int new_stacksize = 0) + CODE: + RETVAL = coro_stacksize; + if (new_stacksize) + coro_stacksize = new_stacksize; + OUTPUT: + RETVAL + +int cctx_count () CODE: RETVAL = cctx_count; @@ -1236,12 +1370,96 @@ 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 +_eval (Coro::State coro, SV *coderef) + 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; + 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 + +SV * +has_stack (Coro::State coro) + PROTOTYPE: $ + CODE: + RETVAL = boolSV (!!coro->cctx); + OUTPUT: + RETVAL + +IV +rss (Coro::State coro) + PROTOTYPE: $ + CODE: + RETVAL = coro_rss (coro); + 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)); @@ -1251,9 +1469,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 (); @@ -1312,14 +1527,6 @@ OUTPUT: RETVAL -SV * -is_ready (SV *self) - PROTOTYPE: $ - CODE: - RETVAL = boolSV (api_is_ready (self)); - OUTPUT: - RETVAL - int nready (...) PROTOTYPE: @@ -1328,6 +1535,59 @@ 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 (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, + newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); + coro->save = CORO_SAVE_DEF; + coro->prio = 0; + av_push (av_async_pool, newSVsv (coro_current)); +} + + MODULE = Coro::State PACKAGE = Coro::AIO SV *