--- Coro/Coro/State.xs 2006/11/26 02:16:19 1.92 +++ Coro/Coro/State.xs 2007/09/21 01:09:36 1.155 @@ -1,14 +1,57 @@ -#define PERL_NO_GET_CONTEXT - #include "libcoro/coro.c" +#define PERL_NO_GET_CONTEXT + #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "patchlevel.h" -#if PERL_VERSION < 6 +#include +#include +#include + +#ifdef HAVE_MMAP +# include +# include +# ifndef MAP_ANONYMOUS +# ifdef MAP_ANON +# define MAP_ANONYMOUS MAP_ANON +# else +# undef HAVE_MMAP +# endif +# endif +# include +# ifndef PAGESIZE +# define PAGESIZE pagesize +# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) +static long pagesize; +# else +# define BOOT_PAGESIZE (void)0 +# endif +#else +# define PAGESIZE 0 +# define BOOT_PAGESIZE (void)0 +#endif + +#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 */ +#define MAX_IDLE_CCTX 8 + +#define PERL_VERSION_ATLEAST(a,b,c) \ + (PERL_REVISION > (a) \ + || (PERL_REVISION == (a) \ + && (PERL_VERSION > (b) \ + || (PERL_VERSION == (b) && PERLSUBVERSION >= (c))))) + +#if !PERL_VERSION_ATLEAST (5,6,0) # ifndef PL_ppaddr # define PL_ppaddr ppaddr # endif @@ -29,35 +72,22 @@ # endif #endif -#include -#include +/* 5.8.7 */ +#ifndef SvRV_set +# define SvRV_set(s,v) SvRV(s) = (v) +#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 -#ifdef HAVE_MMAP -# include -# include -# ifndef MAP_ANONYMOUS -# ifdef MAP_ANON -# define MAP_ANONYMOUS MAP_ANON -# else -# undef HAVE_MMAP -# endif -# endif -# include -# ifndef PAGESIZE -# define PAGESIZE pagesize -# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) -static long pagesize; -# else -# define BOOT_PAGESIZE -# endif +/* prefer perl internal functions over our own? */ +#ifndef CORO_PREFER_PERL_FUNCTIONS +# define CORO_PREFER_PERL_FUNCTIONS 0 #endif /* The next macro should declare a variable stacklevel that contains and approximation @@ -68,11 +98,17 @@ #define IN_DESTRUCT (PL_main_cv == Nullcv) -#define labs(l) ((l) >= 0 ? (l) : -(l)) +#if __GNUC__ >= 3 +# define attribute(x) __attribute__(x) +# define BARRIER __asm__ __volatile__ ("" : : : "memory") +#else +# define attribute(x) +# define BARRIER +#endif -#include "CoroAPI.h" +#define NOINLINE attribute ((noinline)) -#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ +#include "CoroAPI.h" #ifdef USE_ITHREADS static perl_mutex coro_mutex; @@ -83,82 +119,103 @@ # define UNLOCK (void)0 #endif +/* helper storage struct for Coro::AIO */ +struct io_state +{ + int errorno; + I32 laststype; + int laststatval; + 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 */ +static struct coro_cctx *cctx_first; +static int cctx_count, cctx_idle; + /* this is a structure representing a c-level coroutine */ -typedef struct coro_stack { - struct coro_stack *next; +typedef struct coro_cctx { + struct coro_cctx *next; - void *idle_sp; /* original stacklevel when coroutine was created */ + /* the stack */ void *sptr; - long ssize; /* positive == mmap, otherwise malloc */ + size_t ssize; /* cpu state */ - coro_context cctx; + void *idle_sp; /* sp of top-level transfer/schedule/cede call */ + JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ JMPENV *top_env; -} coro_stack; + coro_context cctx; -/* the (fake) coro_stack representing the main program */ -static coro_stack *main_stack; +#if CORO_USE_VALGRIND + int valgrind_id; +#endif + char inuse, mapped; +} coro_cctx; + +enum { + CF_RUNNING = 0x0001, /* coroutine is running */ + CF_READY = 0x0002, /* coroutine is ready */ + CF_NEW = 0x0004, /* has never been switched to */ + CF_DESTROYED = 0x0008, /* coroutine data has been freed */ +}; /* this is a structure representing a perl-level coroutine */ struct coro { /* the c coroutine allocated to this perl coroutine, if any */ - coro_stack *stack; + coro_cctx *cctx; /* 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; + AV *defav; /* @_ */ + SV *defsv; /* $_ */ + SV *errsv; /* $@ */ + GV *deffh; /* default filehandle */ + SV *irssv; /* $/ */ + SV *irssv_sv; /* real $/ cache */ - /* saved global state not related to stacks */ - U8 dowarn; - I32 in_eval; - - /* the stacks and related info (callchain etc..) */ - PERL_SI *curstackinfo; - AV *curstack; - AV *mainstack; - SV **stack_sp; - OP *op; - SV **curpad; - AV *comppad; - CV *compcv; - SV **stack_base; - SV **stack_max; - SV **tmps_stack; - I32 tmps_floor; - I32 tmps_ix; - I32 tmps_max; - I32 *markstack; - I32 *markstack_ptr; - I32 *markstack_max; - I32 *scopestack; - I32 scopestack_ix; - I32 scopestack_max; - ANY *savestack; - I32 savestack_ix; - I32 savestack_max; - OP **retstack; - I32 retstack_ix; - I32 retstack_max; - PMOP *curpm; - COP *curcop; +#define VAR(name,type) type name; +# include "state.h" +#undef VAR /* 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 (pTHX_ CV *cv) { @@ -167,10 +224,10 @@ newpadlist = newAV (); AvREAL_off (newpadlist); -#if PERL_VERSION < 9 - Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); -#else +#if PERL_VERSION_ATLEAST (5,9,0) Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); +#else + Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); #endif newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; --AvFILLp (padlist); @@ -224,19 +281,27 @@ static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; +#define CORO_MAGIC(cv) \ + SvMAGIC (cv) \ + ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ + ? SvMAGIC (cv) \ + : mg_find ((SV *)cv, PERL_MAGIC_coro) \ + : 0 + /* the next two functions merely cache the padlists */ static void get_padlist (pTHX_ CV *cv) { - MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); + MAGIC *mg = CORO_MAGIC (cv); + AV *av; - if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) - CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); + if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) + CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; else { -#if 0 - /* this should work - but it doesn't :( */ - CV *cp = Perl_cv_clone (aTHX_ cv); +#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); @@ -249,7 +314,8 @@ static void put_padlist (pTHX_ CV *cv) { - MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); + MAGIC *mg = CORO_MAGIC (cv); + AV *av; if (!mg) { @@ -259,57 +325,47 @@ mg->mg_obj = (SV *)newAV (); } - av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv)); + av = (AV *)mg->mg_obj; + + if (AvFILLp (av) >= AvMAX (av)) + av_extend (av, AvMAX (av) + 1); + + AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); } +/** load & save, init *******************************************************/ + #define SB do { #define SE } while (0) -#define LOAD(state) load_state(aTHX_ (state)); -#define SAVE(state,flags) save_state(aTHX_ (state),(flags)); - -#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE +#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE static void -load_state(pTHX_ Coro__State c) +load_perl (pTHX_ Coro__State c) { - PL_dowarn = c->dowarn; - PL_in_eval = c->in_eval; - - PL_curstackinfo = c->curstackinfo; - PL_curstack = c->curstack; - PL_mainstack = c->mainstack; - PL_stack_sp = c->stack_sp; - PL_op = c->op; - PL_curpad = c->curpad; - PL_comppad = c->comppad; - PL_compcv = c->compcv; - PL_stack_base = c->stack_base; - PL_stack_max = c->stack_max; - PL_tmps_stack = c->tmps_stack; - PL_tmps_floor = c->tmps_floor; - PL_tmps_ix = c->tmps_ix; - PL_tmps_max = c->tmps_max; - PL_markstack = c->markstack; - PL_markstack_ptr = c->markstack_ptr; - PL_markstack_max = c->markstack_max; - PL_scopestack = c->scopestack; - PL_scopestack_ix = c->scopestack_ix; - PL_scopestack_max = c->scopestack_max; - PL_savestack = c->savestack; - PL_savestack_ix = c->savestack_ix; - PL_savestack_max = c->savestack_max; -#if PERL_VERSION < 9 - PL_retstack = c->retstack; - PL_retstack_ix = c->retstack_ix; - PL_retstack_max = c->retstack_max; -#endif - PL_curpm = c->curpm; - PL_curcop = c->curcop; +#define VAR(name,type) PL_ ## name = c->name; +# 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); + } + } { dSP; @@ -318,15 +374,9 @@ /* now do the ugly restore mess */ while ((cv = (CV *)POPs)) { - AV *padlist = (AV *)POPs; - - if (padlist) - { - put_padlist (aTHX_ cv); /* mark this padlist as available */ - CvPADLIST(cv) = padlist; - } - - ++CvDEPTH(cv); + put_padlist (aTHX_ cv); /* mark this padlist as available */ + CvDEPTH (cv) = PTR2IV (POPs); + CvPADLIST (cv) = (AV *)POPs; } PUTBACK; @@ -334,7 +384,7 @@ } static void -save_state(pTHX_ Coro__State c, int flags) +save_perl (pTHX_ Coro__State c) { { dSP; @@ -347,6 +397,7 @@ * (and reinitialize) all cv's in the whole callchain :( */ + EXTEND (SP, 3 + 1); PUSHs (Nullsv); /* this loop was inspired by pp_caller */ for (;;) @@ -355,34 +406,21 @@ { 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; - if (CvDEPTH(cv)) - { - EXTEND (SP, CvDEPTH(cv)*2); - while (--CvDEPTH(cv)) - { - /* this tells the restore code to increment CvDEPTH */ - PUSHs (Nullsv); - PUSHs ((SV *)cv); - } - - PUSHs ((SV *)CvPADLIST(cv)); + if (CvDEPTH (cv)) + { + EXTEND (SP, 3); + PUSHs ((SV *)CvPADLIST (cv)); + PUSHs (INT2PTR (SV *, CvDEPTH (cv))); PUSHs ((SV *)cv); + CvDEPTH (cv) = 0; get_padlist (aTHX_ cv); } } -#ifdef CXt_FORMAT - else if (CxTYPE(cx) == CXt_FORMAT) - { - /* I never used formats, so how should I know how these are implemented? */ - /* my bold guess is as a simple, plain sub... */ - croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); - } -#endif } if (top_si->si_type == PERLSI_MAIN) @@ -396,43 +434,15 @@ PUTBACK; } - c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; - c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; - c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; - - c->dowarn = PL_dowarn; - c->in_eval = PL_in_eval; - - c->curstackinfo = PL_curstackinfo; - c->curstack = PL_curstack; - c->mainstack = PL_mainstack; - c->stack_sp = PL_stack_sp; - c->op = PL_op; - c->curpad = PL_curpad; - c->comppad = PL_comppad; - c->compcv = PL_compcv; - c->stack_base = PL_stack_base; - c->stack_max = PL_stack_max; - c->tmps_stack = PL_tmps_stack; - c->tmps_floor = PL_tmps_floor; - c->tmps_ix = PL_tmps_ix; - c->tmps_max = PL_tmps_max; - c->markstack = PL_markstack; - c->markstack_ptr = PL_markstack_ptr; - c->markstack_max = PL_markstack_max; - c->scopestack = PL_scopestack; - c->scopestack_ix = PL_scopestack_ix; - c->scopestack_max = PL_scopestack_max; - c->savestack = PL_savestack; - c->savestack_ix = PL_savestack_ix; - c->savestack_max = PL_savestack_max; -#if PERL_VERSION < 9 - c->retstack = PL_retstack; - c->retstack_ix = PL_retstack_ix; - c->retstack_max = PL_retstack_max; -#endif - c->curpm = PL_curpm; - c->curcop = PL_curcop; + 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; +# include "state.h" +#undef VAR } /* @@ -441,12 +451,13 @@ * on the (sometimes correct) assumption that coroutines do * not usually need a lot of stackspace. */ +#if CORO_PREFER_PERL_FUNCTIONS +# define coro_init_stacks init_stacks +#else static void coro_init_stacks (pTHX) { - LOCK; - - PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); + PL_curstackinfo = new_stackinfo(64, 4); PL_curstackinfo->si_type = PERLSI_MAIN; PL_curstack = PL_curstackinfo->si_stack; PL_mainstack = PL_curstack; /* remember in case we switch stacks */ @@ -455,10 +466,10 @@ PL_stack_sp = PL_stack_base; PL_stack_max = PL_stack_base + AvMAX(PL_curstack); - New(50,PL_tmps_stack,96,SV*); + New(50,PL_tmps_stack,64,SV*); PL_tmps_floor = -1; PL_tmps_ix = -1; - PL_tmps_max = 96; + PL_tmps_max = 64; New(54,PL_markstack,16,I32); PL_markstack_ptr = PL_markstack; @@ -472,34 +483,39 @@ PL_scopestack_ix = 0; PL_scopestack_max = 16; - New(54,PL_savestack,96,ANY); + New(54,PL_savestack,64,ANY); PL_savestack_ix = 0; - PL_savestack_max = 96; + PL_savestack_max = 64; -#if PERL_VERSION < 9 +#if !PERL_VERSION_ATLEAST (5,9,0) New(54,PL_retstack,8,OP*); PL_retstack_ix = 0; PL_retstack_max = 8; #endif - - UNLOCK; } +#endif /* * destroy the stacks, the callchain etc... */ static void -destroy_stacks(pTHX) +coro_destroy_stacks (pTHX) { if (!IN_DESTRUCT) { - /* is this ugly, I ask? */ + /* restore all saved variables and stuff */ LEAVE_SCOPE (0); + assert (PL_tmps_floor == -1); - /* sure it is, but more important: is it correct?? :/ */ + /* free all temporaries */ FREETMPS; + assert (PL_tmps_ix == -1); + + /* unwind all extra stacks */ + POPSTACK_TO (PL_mainstack); - /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/ + /* unwind main stack */ + dounwind (-1); } while (PL_curstackinfo->si_next) @@ -509,17 +525,8 @@ { PERL_SI *p = PL_curstackinfo->si_prev; - { /*D*//*remove*/ - dSP; - SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); - PUTBACK; /* possibly superfluous */ - } - if (!IN_DESTRUCT) - { - dounwind (-1);/*D*//*remove*/ - SvREFCNT_dec (PL_curstackinfo->si_stack); - } + SvREFCNT_dec (PL_curstackinfo->si_stack); Safefree (PL_curstackinfo->si_cxstack); Safefree (PL_curstackinfo); @@ -530,47 +537,84 @@ Safefree (PL_markstack); Safefree (PL_scopestack); Safefree (PL_savestack); -#if PERL_VERSION < 9 +#if !PERL_VERSION_ATLEAST (5,9,0) Safefree (PL_retstack); #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. */ - dTHX; - dSP; - UNOP myop; - SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE); - coro_init_stacks (aTHX); - /*PL_curcop = 0;*/ - /*PL_in_eval = PL_in_eval;*/ /* inherit */ - SvREFCNT_dec (GvAV (PL_defgv)); - GvAV (PL_defgv) = coro->args; coro->args = 0; - SPAGAIN; + PL_curcop = &PL_compiling; + PL_in_eval = EVAL_NULL; + PL_comppad = 0; + PL_curpm = 0; + PL_localizing = 0; + PL_dirty = 0; + PL_restartop = 0; - Zero (&myop, 1, UNOP); - myop.op_next = Nullop; - myop.op_flags = OPf_WANT_VOID; + { + dSP; + LOGOP myop; - PL_op = (OP *)&myop; + SvREFCNT_dec (GvAV (PL_defgv)); + GvAV (PL_defgv) = coro->args; coro->args = 0; - PUSHMARK(SP); - XPUSHs (sub_init); - PUTBACK; - PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); - SPAGAIN; + Zero (&myop, 1, LOGOP); + myop.op_next = Nullop; + myop.op_flags = OPf_WANT_VOID; + + PUSHMARK (SP); + XPUSHs (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 -free_coro_mortal () +free_coro_mortal (pTHX) { if (coro_mortal) { @@ -579,216 +623,340 @@ } } +/* 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 (pTHX_ coro_cctx *cctx) +{ + dSP; + LOGOP myop; + + Zero (&myop, 1, LOGOP); + myop.op_next = PL_op; + myop.op_flags = OPf_WANT_VOID | OPf_STACKED; + + PUSHMARK (SP); + EXTEND (SP, 2); + PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); + PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); + PUTBACK; + PL_op = (OP *)&myop; + PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); + SPAGAIN; +} + +/* + * this is a _very_ stripped down perl interpreter ;) + */ static void coro_run (void *arg) { - /* - * this is a _very_ stripped down perl interpreter ;) - */ dTHX; - PL_top_env = &PL_start_env; + /* coro_run is the alternative tail of transfer(), so unlock here. */ UNLOCK; - sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV ((coro_stack *)arg)); - sv_setiv (get_sv ("Coro::State::cctx_restartop", FALSE), PTR2IV (PL_op)); - PL_restartop = CvSTART (get_cv ("Coro::State::cctx_init", FALSE)); + PL_top_env = &PL_start_env; + + /* inject a fake subroutine call to cctx_init */ + prepare_cctx (aTHX_ (coro_cctx *)arg); - /* somebody will hit me for both perl_run and PL_restart_op */ - perl_run (aTHX_ PERL_GET_CONTEXT); + /* somebody or something will hit me for both perl_run and PL_restartop */ + PL_restartop = PL_op; + perl_run (PL_curinterp); - 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_stack * -stack_new () +static coro_cctx * +cctx_new () { - coro_stack *stack; + coro_cctx *cctx; + void *stack_start; + size_t stack_size; - New (0, stack, 1, coro_stack); + ++cctx_count; + + Newz (0, cctx, 1, coro_cctx); #if HAVE_MMAP - stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */ - stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); + 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 (stack->sptr == (void *)-1) + if (cctx->sptr != (void *)-1) { - perror ("FATAL: unable to mmap stack for coroutine"); - _exit (EXIT_FAILURE); +# if CORO_STACKGUARD + mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); +# endif + 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); - mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); - -#else - - stack->ssize = STACKSIZE * (long)sizeof (long); - New (0, stack->sptr, STACKSIZE, long); + if (!cctx->sptr) + { + perror ("FATAL: unable to allocate stack for coroutine"); + _exit (EXIT_FAILURE); + } - if (!stack->sptr) - { - perror (stderr, "FATAL: unable to malloc stack for coroutine"); - _exit (EXIT_FAILURE); + stack_start = cctx->sptr; + stack_size = cctx->ssize; } -#endif - - coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->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 stack; + return cctx; } static void -stack_free (coro_stack *stack) +cctx_destroy (coro_cctx *cctx) { - if (!stack || stack == main_stack) + if (!cctx) return; + --cctx_count; + +#if CORO_USE_VALGRIND + VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); +#endif + #if HAVE_MMAP - munmap (stack->sptr, stack->ssize); -#else - Safefree (stack->sptr); + if (cctx->mapped) + munmap (cctx->sptr, cctx->ssize); + else #endif + Safefree (cctx->sptr); - Safefree (stack); + Safefree (cctx); } -static coro_stack *stack_first; - -static coro_stack * -stack_get () +static coro_cctx * +cctx_get (pTHX) { - coro_stack *stack; - - if (stack_first) + while (cctx_first) { - stack = stack_first; - stack_first = stack->next; + coro_cctx *cctx = cctx_first; + cctx_first = cctx->next; + --cctx_idle; + + if (cctx->ssize >= coro_stacksize) + return cctx; + + cctx_destroy (cctx); } - else - { - stack = stack_new (); - PL_op = PL_op->op_next; - } - return stack; + PL_op = PL_op->op_next; + return cctx_new (); } static void -stack_put (coro_stack *stack) +cctx_put (coro_cctx *cctx) { - stack->next = stack_first; - stack_first = stack; + /* free another cctx if overlimit */ + if (cctx_idle >= MAX_IDLE_CCTX) + { + coro_cctx *first = cctx_first; + cctx_first = first->next; + --cctx_idle; + + assert (!first->inuse); + cctx_destroy (first); + } + + ++cctx_idle; + cctx->next = cctx_first; + cctx_first = cctx; } +/** coroutine switching *****************************************************/ + /* never call directly, always through the coro_state_transfer global variable */ -static void -transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) +static void NOINLINE +transfer (pTHX_ struct coro *prev, struct coro *next) { dSTACKLEVEL; /* sometimes transfer is only called to set idle_sp */ - if (flags == TRANSFER_SET_STACKLEVEL) - ((coro_stack *)prev)->idle_sp = STACKLEVEL; + if (!next) + { + ((coro_cctx *)prev)->idle_sp = STACKLEVEL; + assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ + } else if (prev != next) { - coro_stack *prev__stack; - - LOCK; + coro_cctx *prev__cctx; - if (next->mainstack) + if (prev->flags & CF_NEW) { - /* coroutine already started */ - SAVE (prev, flags); - LOAD (next); + /* create a new empty context */ + Newz (0, prev->cctx, 1, coro_cctx); + prev->cctx->inuse = 1; + prev->flags &= ~CF_NEW; + prev->flags |= CF_RUNNING; } - else + + /*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; + + LOCK; + + if (next->flags & CF_NEW) { /* need to start coroutine */ + next->flags &= ~CF_NEW; /* first get rid of the old state */ - SAVE (prev, -1); + save_perl (aTHX_ prev); /* setup coroutine call */ - setup_coro (next); - /* need a stack */ - next->stack = 0; + setup_coro (aTHX_ next); + /* need a new stack */ + assert (!next->cctx); + } + else + { + /* coroutine already started */ + save_perl (aTHX_ prev); + load_perl (aTHX_ next); } - prev__stack = prev->stack; + prev__cctx = prev->cctx; - /* possibly "free" the stack */ - if (prev__stack->idle_sp == STACKLEVEL) + /* possibly "free" the cctx */ + if (prev__cctx->idle_sp == STACKLEVEL) { - stack_put (prev__stack); - prev->stack = 0; - } + /* 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)); - if (!next->stack) - next->stack = stack_get (); + prev->cctx = 0; - if (prev__stack != next->stack) + cctx_put (prev__cctx); + prev__cctx->inuse = 0; + } + + if (!next->cctx) { - prev__stack->top_env = PL_top_env; - PL_top_env = next->stack->top_env; - coro_transfer (&prev__stack->cctx, &next->stack->cctx); + next->cctx = cctx_get (aTHX); + assert (!next->cctx->inuse); + next->cctx->inuse = 1; } - free_coro_mortal (); + if (prev__cctx != next->cctx) + { + prev__cctx->top_env = PL_top_env; + PL_top_env = next->cctx->top_env; + coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); + } + free_coro_mortal (aTHX); UNLOCK; } } -/* use this function pointer to call the above function */ -/* this is done to increase chances of the compiler not inlining the call */ -/* not static to make it even harder for the compiler (and theoretically impossible in most cases */ -void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl; - struct transfer_args { struct coro *prev, *next; - int flags; }; -#define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags) +#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) -static void -coro_state_destroy (struct coro *coro) +/** high level stuff ********************************************************/ + +static int +coro_state_destroy (pTHX_ struct coro *coro) { - if (coro->refcnt--) - return; + 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; - SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); - LOAD (aTHX_ coro); + assert (!(coro->flags & CF_RUNNING)); + + Zero (&temp, 1, struct coro); + temp.save = CORO_SAVE_ALL; - destroy_stacks (aTHX); + if (coro->flags & CF_RUNNING) + croak ("FATAL: tried to destroy currently running coroutine"); - LOAD ((&temp)); /* this will get rid of defsv etc.. */ + save_perl (aTHX_ &temp); + load_perl (aTHX_ coro); + + coro_destroy_stacks (aTHX); + + load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ coro->mainstack = 0; } - stack_free (coro->stack); + cctx_destroy (coro->cctx); SvREFCNT_dec (coro->args); - Safefree (coro); + + 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; } static int -coro_state_clear (SV *sv, MAGIC *mg) +coro_state_free (pTHX_ SV *sv, MAGIC *mg) { struct coro *coro = (struct coro *)mg->mg_ptr; mg->mg_ptr = 0; - coro_state_destroy (coro); + coro->hv = 0; + + if (--coro->refcnt < 0) + { + coro_state_destroy (aTHX_ coro); + Safefree (coro); + } return 0; } static int -coro_state_dup (MAGIC *mg, CLONE_PARAMS *params) +coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params) { struct coro *coro = (struct coro *)mg->mg_ptr; @@ -797,10 +965,19 @@ return 0; } -static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; +static MGVTBL coro_state_vtbl = { + 0, 0, 0, 0, + coro_state_free, + 0, +#ifdef MGf_DUP + coro_state_dup, +#else +# define MGf_DUP 0 +#endif +}; static struct coro * -SvSTATE (SV *coro) +SvSTATE_ (pTHX_ SV *coro) { HV *stash; MAGIC *mg; @@ -816,55 +993,48 @@ 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 (pTHX_ struct transfer_args *ta, SV *prev, SV *next, int flags) +prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) { - ta->prev = SvSTATE (prev); - ta->next = SvSTATE (next); - ta->flags = flags; + ta->prev = SvSTATE (prev_sv); + ta->next = SvSTATE (next_sv); } static void -api_transfer (SV *prev, SV *next, int flags) +api_transfer (SV *prev_sv, SV *next_sv) { dTHX; struct transfer_args ta; - prepare_transfer (aTHX_ &ta, prev, next, flags); + prepare_transfer (aTHX_ &ta, prev_sv, next_sv); TRANSFER (ta); } -/** Coro ********************************************************************/ +static int +api_save (SV *coro_sv, int new_save) +{ + dTHX; + struct coro *coro = SvSTATE (coro_sv); + int old_save = coro->save; -#define PRIO_MAX 3 -#define PRIO_HIGH 1 -#define PRIO_NORMAL 0 -#define PRIO_LOW -1 -#define PRIO_IDLE -3 -#define PRIO_MIN -4 + if (new_save >= 0) + coro->save = new_save; -/* for Coro.pm */ -static GV *coro_current, *coro_idle; -static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; -static int coro_nready; + return old_save; +} + +/** Coro ********************************************************************/ static void -coro_enq (pTHX_ SV *sv) +coro_enq (pTHX_ SV *coro_sv) { - int prio; - - if (SvTYPE (sv) != SVt_PVHV) - croak ("Coro::ready tried to enqueue something that is not a coroutine"); - - prio = SvSTATE (sv)->prio; - - av_push (coro_ready [prio - PRIO_MIN], sv); - coro_nready++; + av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); } static SV * @@ -878,84 +1048,119 @@ 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; } -static void -api_ready (SV *coro) +static int +api_ready (SV *coro_sv) { dTHX; + struct coro *coro; - if (SvROK (coro)) - coro = SvRV (coro); + if (SvROK (coro_sv)) + coro_sv = SvRV (coro_sv); + + coro = SvSTATE (coro_sv); + + if (coro->flags & CF_READY) + return 0; + + coro->flags |= CF_READY; LOCK; - coro_enq (aTHX_ SvREFCNT_inc (coro)); + coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); + ++coro_nready; UNLOCK; + + return 1; } -static void -prepare_schedule (aTHX_ struct transfer_args *ta) +static int +api_is_ready (SV *coro_sv) { - SV *current, *prev, *next; - - LOCK; + dTHX; + return !!(SvSTATE (coro_sv)->flags & CF_READY); +} - current = GvSV (coro_current); +static void +prepare_schedule (pTHX_ struct transfer_args *ta) +{ + SV *prev_sv, *next_sv; for (;;) { LOCK; + next_sv = coro_deq (aTHX_ PRIO_MIN); - next = coro_deq (aTHX_ PRIO_MIN); - - if (next) - break; + /* nothing to schedule: call the idle handler */ + if (!next_sv) + { + dSP; + UNLOCK; - UNLOCK; + ENTER; + SAVETMPS; - { - dSP; + PUSHMARK (SP); + PUTBACK; + call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); + + FREETMPS; + LEAVE; + continue; + } - ENTER; - SAVETMPS; + ta->next = SvSTATE (next_sv); - PUSHMARK (SP); - PUTBACK; - call_sv (GvSV (coro_idle), G_DISCARD); + /* 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; + } - FREETMPS; - LEAVE; - } + --coro_nready; + UNLOCK; + break; } - prev = SvRV (current); - SvRV (current) = next; - /* free this only after the transfer */ - free_coro_mortal (); - coro_mortal = prev; + prev_sv = SvRV (coro_current); + SvRV_set (coro_current, next_sv); + ta->prev = SvSTATE (prev_sv); - ta->prev = SvSTATE (prev); - ta->next = SvSTATE (next); - ta->flags = TRANSFER_SAVE_ALL; + assert (ta->next->flags & CF_READY); + ta->next->flags &= ~CF_READY; + LOCK; + free_coro_mortal (aTHX); + coro_mortal = prev_sv; UNLOCK; } static void -prepare_cede (aTHX_ struct transfer_args *ta) +prepare_cede (pTHX_ struct transfer_args *ta) { - LOCK; - coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); - UNLOCK; + api_ready (coro_current); + prepare_schedule (aTHX_ ta); +} - prepare_schedule (ta); +static int +prepare_cede_notself (pTHX_ struct transfer_args *ta) +{ + if (coro_nready) + { + SV *prev = SvRV (coro_current); + prepare_schedule (aTHX_ ta); + api_ready (prev); + return 1; + } + else + return 0; } static void @@ -964,18 +1169,40 @@ dTHX; struct transfer_args ta; - prepare_schedule (&ta); + prepare_schedule (aTHX_ &ta); TRANSFER (ta); } -static void +static int api_cede (void) { dTHX; struct transfer_args ta; - prepare_cede (&ta); - TRANSFER (ta); + prepare_cede (aTHX_ &ta); + + if (ta.prev != ta.next) + { + TRANSFER (ta); + return 1; + } + else + return 0; +} + +static int +api_cede_notself (void) +{ + dTHX; + struct transfer_args ta; + + if (prepare_cede_notself (aTHX_ &ta)) + { + TRANSFER (ta); + return 1; + } + else + return 0; } MODULE = Coro::State PACKAGE = Coro::State @@ -991,18 +1218,23 @@ coro_state_stash = gv_stashpv ("Coro::State", TRUE); - newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); - newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); - newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); + 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; + + while (main_top_env->je_prev) + main_top_env = main_top_env->je_prev; coroapi.ver = CORO_API_VERSION; coroapi.transfer = api_transfer; - Newz (0, main_stack, 1, coro_stack); - main_stack->idle_sp = (void *)-1; - assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); } @@ -1016,27 +1248,48 @@ Newz (0, coro, 1, struct coro); coro->args = newAV (); + 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)); for (i = 1; i < items; i++) av_push (coro->args, newSVsv (ST (i))); - - coro->stack = main_stack; - /*coro->mainstack = 0; *//*actual work is done inside transfer */ - /*coro->stack = 0;*/ } 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 + void -transfer (...) +_set_stacklevel (...) ALIAS: - Coro::schedule = 1 - Coro::cede = 2 - _set_stacklevel = 3 + Coro::State::transfer = 1 + Coro::schedule = 2 + Coro::cede = 3 + Coro::cede_notself = 4 CODE: { struct transfer_args ta; @@ -1044,47 +1297,45 @@ switch (ix) { case 0: - if (items != 3) - croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); - - prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); + ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); + ta.next = 0; break; case 1: - prepare_schedule (&ta); + if (items != 2) + croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); + + prepare_transfer (aTHX_ &ta, ST (0), ST (1)); break; case 2: - prepare_cede (&ta); + prepare_schedule (aTHX_ &ta); break; case 3: - ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); - ta.next = 0; - ta.flags = TRANSFER_SET_STACKLEVEL; + prepare_cede (aTHX_ &ta); break; - } - TRANSFER (ta); -} + case 4: + if (!prepare_cede_notself (aTHX_ &ta)) + XSRETURN_EMPTY; -void -_clone_state_from (SV *dst, SV *src) - CODE: -{ - struct coro *coro_src = SvSTATE (src); + break; + } - sv_unmagic (SvRV (dst), PERL_MAGIC_ext); + BARRIER; + TRANSFER (ta); - ++coro_src->refcnt; - sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; + if (GIMME_V != G_VOID && ta.next != ta.prev) + XSRETURN_YES; } -void -_nonlocal_goto (IV nextop) +bool +_destroy (SV *coro_sv) CODE: - /* uuh, somebody will kill me again for this */ - PL_op->op_next = INT2PTR (OP *, nextop); + RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); + OUTPUT: + RETVAL void _exit (code) @@ -1093,37 +1344,97 @@ CODE: _exit (code); -MODULE = Coro::State PACKAGE = Coro::Cont +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; + OUTPUT: + RETVAL + +int +cctx_idle () + CODE: + RETVAL = cctx_idle; + OUTPUT: + RETVAL void -yield (...) - PROTOTYPE: @ - CODE: +list () + PPCODE: { - SV *yieldstack; - SV *sv; - AV *defav = GvAV (PL_defgv); - struct coro *prev, *next; - - yieldstack = *hv_fetch ( - (HV *)SvRV (GvSV (coro_current)), - "yieldstack", sizeof ("yieldstack") - 1, - 0 - ); - - /* set up @_ -- ugly */ - av_clear (defav); - av_fill (defav, items - 1); - while (items--) - av_store (defav, items, SvREFCNT_inc (ST(items))); - - sv = av_pop ((AV *)SvRV (yieldstack)); - prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0)); - next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0)); - SvREFCNT_dec (sv); + struct coro *coro; + for (coro = coro_first; coro; coro = coro->next) + if (coro->hv) + XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); +} - coro_state_transfer (aTHX_ prev, next, 0); +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 + +IV +rss (Coro::State coro) + PROTOTYPE: $ + CODE: + RETVAL = coro_rss (coro); + OUTPUT: + RETVAL + MODULE = Coro::State PACKAGE = Coro @@ -1140,8 +1451,8 @@ newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); - coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); - coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); + coro_current = get_sv ("Coro::current", FALSE); + SvREADONLY_on (coro_current); for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) coro_ready[i] = newAV (); @@ -1149,11 +1460,14 @@ { SV *sv = perl_get_sv("Coro::API", 1); - coroapi.schedule = api_schedule; - coroapi.cede = api_cede; - coroapi.ready = api_ready; - coroapi.nready = &coro_nready; - coroapi.current = coro_current; + coroapi.schedule = api_schedule; + coroapi.save = api_save; + coroapi.cede = api_cede; + coroapi.cede_notself = api_cede_notself; + coroapi.ready = api_ready; + coroapi.is_ready = api_is_ready; + coroapi.nready = &coro_nready; + coroapi.current = coro_current; GCoroAPI = &coroapi; sv_setiv (sv, (IV)&coroapi); @@ -1161,6 +1475,13 @@ } } +void +_set_current (SV *current) + PROTOTYPE: $ + CODE: + SvREFCNT_dec (SvRV (coro_current)); + SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); + int prio (Coro::State coro, int newprio = 0) ALIAS: @@ -1172,7 +1493,7 @@ if (items > 1) { if (ix) - newprio += coro->prio; + newprio = coro->prio - newprio; if (newprio < PRIO_MIN) newprio = PRIO_MIN; if (newprio > PRIO_MAX) newprio = PRIO_MAX; @@ -1180,12 +1501,16 @@ coro->prio = newprio; } } + OUTPUT: + RETVAL -void +SV * ready (SV *self) PROTOTYPE: $ CODE: - api_ready (self); + RETVAL = boolSV (api_ready (self)); + OUTPUT: + RETVAL int nready (...) @@ -1201,19 +1526,17 @@ _get_state () CODE: { - struct { - int errorno; - int laststype; - int laststatval; - Stat_t statcache; - } data; - - data.errorno = errno; - data.laststype = PL_laststype; - data.laststatval = PL_laststatval; - data.statcache = PL_statcache; + struct io_state *data; - RETVAL = newSVpvn ((char *)&data, sizeof data); + RETVAL = newSV (sizeof (struct io_state)); + data = (struct io_state *)SvPVX (RETVAL); + SvCUR_set (RETVAL, sizeof (struct io_state)); + SvPOK_only (RETVAL); + + data->errorno = errno; + data->laststype = PL_laststype; + data->laststatval = PL_laststatval; + data->statcache = PL_statcache; } OUTPUT: RETVAL @@ -1223,15 +1546,11 @@ PROTOTYPE: $ CODE: { - struct { - int errorno; - int laststype; - int laststatval; - Stat_t statcache; - } *data = (void *)data_; + struct io_state *data = (void *)data_; - errno = data->errorno; - PL_laststype = data->laststype; + errno = data->errorno; + PL_laststype = data->laststype; PL_laststatval = data->laststatval; - PL_statcache = data->statcache; + PL_statcache = data->statcache; } +