--- Coro/Coro/State.xs 2003/11/15 03:53:37 1.48 +++ Coro/Coro/State.xs 2007/01/04 23:49:27 1.134 @@ -1,10 +1,52 @@ +#include "libcoro/coro.c" + #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "patchlevel.h" -#if PATCHLEVEL < 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 USE_VALGRIND +# include +#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 @@ -25,325 +67,269 @@ # endif #endif -#include "libcoro/coro.c" - -#include +/* 5.8.7 */ +#ifndef SvRV_set +# define SvRV_set(s,v) SvRV(s) = (v) +#endif -#ifdef HAVE_MMAP -# include -# include -# ifndef MAP_ANONYMOUS -# ifdef MAP_ANON -# define MAP_ANONYMOUS MAP_ANON -# else -# undef HAVE_MMAP -# endif -# endif +#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 +# undef STACKGUARD #endif -#define MAY_FLUSH /* increases codesize and is rarely used */ +#ifndef STACKGUARD +# define STACKGUARD 0 +#endif -#define SUB_INIT "Coro::State::initialize" -#define UCORO_STATE "_coro_state" +/* prefer perl internal functions over our own? */ +#ifndef PREFER_PERL_FUNCTIONS +# define 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 void *stacklevel = &stacklevel +#define dSTACKLEVEL int stacklevel +#define STACKLEVEL ((void *)&stacklevel) #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 + +#define NOINLINE attribute ((noinline)) #include "CoroAPI.h" +#ifdef USE_ITHREADS +static perl_mutex coro_mutex; +# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) +# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) +#else +# define LOCK (void)0 +# define UNLOCK (void)0 +#endif + +struct io_state +{ + int errorno; + I32 laststype; + int laststatval; + Stat_t statcache; +}; + static struct CoroAPI coroapi; +static AV *main_mainstack; /* used to differentiate between $main and others */ +static HV *coro_state_stash, *coro_stash; +static SV *coro_mortal; /* will be freed after next transfer */ -/* this is actually not only the c stack but also c registers etc... */ -typedef struct { - int refcnt; /* pointer reference counter */ - int usecnt; /* shared by how many coroutines */ - int gencnt; /* generation counter */ +static struct coro_cctx *cctx_first; +static int cctx_count, cctx_idle; - coro_context cctx; +/* this is a structure representing a c-level coroutine */ +typedef struct coro_cctx { + struct coro_cctx *next; + /* the stack */ void *sptr; long ssize; /* positive == mmap, otherwise malloc */ -} coro_stack; + /* cpu state */ + 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_context cctx; + + int inuse; + +#if USE_VALGRIND + int valgrind_id; +#endif +} 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 top-level JMPENV for each coroutine, needed to catch dies. */ - JMPENV start_env; + /* the c coroutine allocated to this perl coroutine, if any */ + coro_cctx *cctx; - /* the optional C context */ - coro_stack *stack; - void *cursp; - int gencnt; + /* 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; /* $@ */ + 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; - 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; - COP *curcop; - JMPENV *top_env; +#define VAR(name,type) type name; +# include "state.h" +#undef VAR - /* data associated with this coroutine (initial args) */ - AV *args; + /* coro process data */ + int prio; }; typedef struct coro *Coro__State; typedef struct coro *Coro__State_or_hashref; -static AV *main_mainstack; /* used to differentiate between $main and others */ -static HV *coro_state_stash; -static SV *ucoro_state_sv; -static U32 ucoro_state_hash; -static HV *padlist_cache; -static SV *coro_mortal; /* will be freed after next transfer */ - -/* mostly copied from op.c:cv_clone2 */ -STATIC AV * -clone_padlist (AV *protopadlist) +static AV * +coro_clone_padlist (CV *cv) { - AV *av; - I32 ix; - AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE); - AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE); - SV **pname = AvARRAY (protopad_name); - SV **ppad = AvARRAY (protopad); - I32 fname = AvFILLp (protopad_name); - I32 fpad = AvFILLp (protopad); - AV *newpadlist, *newpad_name, *newpad; - SV **npad; - - newpad_name = newAV (); - for (ix = fname; ix >= 0; ix--) - av_store (newpad_name, ix, SvREFCNT_inc (pname[ix])); - - newpad = newAV (); - av_fill (newpad, AvFILLp (protopad)); - npad = AvARRAY (newpad); + AV *padlist = CvPADLIST (cv); + AV *newpadlist, *newpad; newpadlist = newAV (); AvREAL_off (newpadlist); - av_store (newpadlist, 0, (SV *) newpad_name); - av_store (newpadlist, 1, (SV *) newpad); - - av = newAV (); /* will be @_ */ - av_extend (av, 0); - av_store (newpad, 0, (SV *) av); - AvFLAGS (av) = AVf_REIFY; - - for (ix = fpad; ix > 0; ix--) - { - SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; - if (namesv && namesv != &PL_sv_undef) - { - char *name = SvPVX (namesv); /* XXX */ - if (SvFLAGS (namesv) & SVf_FAKE || *name == '&') - { /* lexical from outside? */ - npad[ix] = SvREFCNT_inc (ppad[ix]); - } - else - { /* our own lexical */ - SV *sv; - if (*name == '&') - sv = SvREFCNT_inc (ppad[ix]); - else if (*name == '@') - sv = (SV *) newAV (); - else if (*name == '%') - sv = (SV *) newHV (); - else - sv = NEWSV (0, 0); - if (!SvPADBUSY (sv)) - SvPADMY_on (sv); - npad[ix] = sv; - } - } - else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix])) - { - npad[ix] = SvREFCNT_inc (ppad[ix]); - } - else - { - SV *sv = NEWSV (0, 0); - SvPADTMP_on (sv); - npad[ix] = sv; - } - } - -#if 0 /* return -ENOTUNDERSTOOD */ - /* Now that vars are all in place, clone nested closures. */ - - for (ix = fpad; ix > 0; ix--) { - SV* namesv = (ix <= fname) ? pname[ix] : Nullsv; - if (namesv - && namesv != &PL_sv_undef - && !(SvFLAGS(namesv) & SVf_FAKE) - && *SvPVX(namesv) == '&' - && CvCLONE(ppad[ix])) - { - CV *kid = cv_clone((CV*)ppad[ix]); - SvREFCNT_dec(ppad[ix]); - CvCLONE_on(kid); - SvPADMY_on(kid); - npad[ix] = (SV*)kid; - } - } +#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); + + av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); + av_store (newpadlist, 1, (SV *)newpad); return newpadlist; } -#ifdef MAY_FLUSH -STATIC void +static void free_padlist (AV *padlist) { /* may be during global destruction */ - if (SvREFCNT(padlist)) + if (SvREFCNT (padlist)) { - I32 i = AvFILLp(padlist); + I32 i = AvFILLp (padlist); while (i >= 0) { - SV **svp = av_fetch(padlist, i--, FALSE); - SV *sv = svp ? *svp : Nullsv; - if (sv) - SvREFCNT_dec(sv); + SV **svp = av_fetch (padlist, i--, FALSE); + if (svp) + { + SV *sv; + while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) + SvREFCNT_dec (sv); + + SvREFCNT_dec (*svp); + } } - SvREFCNT_dec((SV*)padlist); - } + SvREFCNT_dec ((SV*)padlist); + } } -#endif + +static int +coro_cv_free (pTHX_ SV *sv, MAGIC *mg) +{ + AV *padlist; + AV *av = (AV *)mg->mg_obj; + + /* casting is fun. */ + while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) + free_padlist (padlist); + + SvREFCNT_dec (av); + + return 0; +} + +#define PERL_MAGIC_coro PERL_MAGIC_ext + +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 +static void get_padlist (CV *cv) { - SV **he = hv_fetch (padlist_cache, (void *)&cv, sizeof (CV *), 0); + MAGIC *mg = CORO_MAGIC (cv); + AV *av; - if (he && AvFILLp ((AV *)*he) >= 0) - CvPADLIST (cv) = (AV *)av_pop ((AV *)*he); + if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) + CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; else - CvPADLIST (cv) = clone_padlist (CvPADLIST (cv)); + { +#if 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); +#endif + } } -STATIC void +static void put_padlist (CV *cv) { - SV **he = hv_fetch (padlist_cache, (void *)&cv, sizeof (CV *), 1); + MAGIC *mg = CORO_MAGIC (cv); + AV *av; - if (SvTYPE (*he) != SVt_PVAV) + if (!mg) { - SvREFCNT_dec (*he); - *he = (SV *)newAV (); + sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); + mg = mg_find ((SV *)cv, PERL_MAGIC_coro); + mg->mg_virtual = &vtbl_coro; + mg->mg_obj = (SV *)newAV (); } - av_push ((AV *)*he, (SV *)CvPADLIST (cv)); -} + av = (AV *)mg->mg_obj; -#ifdef MAY_FLUSH -STATIC void -flush_padlist_cache () -{ - HV *hv = padlist_cache; - padlist_cache = newHV (); - - if (hv_iterinit (hv)) - { - HE *he; - AV *padlist; - - while (!!(he = hv_iternext (hv))) - { - AV *av = (AV *)HeVAL(he); - - /* casting is fun. */ - while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) - free_padlist (padlist); - } - } + if (AvFILLp (av) >= AvMAX (av)) + av_extend (av, AvMAX (av) + 1); - SvREFCNT_dec (hv); + AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); } -#endif #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 (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_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; - PL_retstack = c->retstack; - PL_retstack_ix = c->retstack_ix; - PL_retstack_max = c->retstack_max; - PL_curcop = c->curcop; - PL_top_env = c->top_env; +#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->irssv) + { + if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) + SvREFCNT_dec (c->irssv); + 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; @@ -352,18 +338,9 @@ /* now do the ugly restore mess */ while ((cv = (CV *)POPs)) { - AV *padlist = (AV *)POPs; - - if (padlist) - { - put_padlist (cv); /* mark this padlist as available */ - CvPADLIST(cv) = padlist; -#ifdef USE_THREADS - /*CvOWNER(cv) = (struct perl_thread *)POPs;*/ -#endif - } - - ++CvDEPTH(cv); + put_padlist (cv); /* mark this padlist as available */ + CvDEPTH (cv) = PTR2IV (POPs); + CvPADLIST (cv) = (AV *)POPs; } PUTBACK; @@ -371,7 +348,7 @@ } static void -save_state(pTHX_ Coro__State c, int flags) +save_perl (Coro__State c) { { dSP; @@ -384,6 +361,7 @@ * (and reinitialize) all cv's in the whole callchain :( */ + EXTEND (SP, 3 + 1); PUSHs (Nullsv); /* this loop was inspired by pp_caller */ for (;;) @@ -392,39 +370,21 @@ { PERL_CONTEXT *cx = &ccstk[cxix--]; - if (CxTYPE(cx) == CXt_SUB) + if (CxTYPE (cx) == CXt_SUB) { CV *cv = cx->blk_sub.cv; - if (CvDEPTH(cv)) - { -#ifdef USE_THREADS - /*XPUSHs ((SV *)CvOWNER(cv));*/ - /*CvOWNER(cv) = 0;*/ - /*error must unlock this cv etc.. etc...*/ -#endif - 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); - get_padlist (cv); /* this is a monster */ + CvDEPTH (cv) = 0; + get_padlist (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) @@ -438,52 +398,29 @@ 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->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; - c->retstack = PL_retstack; - c->retstack_ix = PL_retstack_ix; - c->retstack_max = PL_retstack_max; - c->curcop = PL_curcop; - c->top_env = PL_top_env; + 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->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; + +#define VAR(name,type)c->name = PL_ ## name; +# include "state.h" +#undef VAR } /* * allocate various perl stacks. This is an exact copy * of perl.c:init_stacks, except that it uses less memory - * on the assumption that coroutines do not usually need - * a lot of stackspace. + * on the (sometimes correct) assumption that coroutines do + * not usually need a lot of stackspace. */ -STATIC void -coro_init_stacks (pTHX) +#if PREFER_PERL_FUNCTIONS +# define coro_init_stacks init_stacks +#else +static void +coro_init_stacks () { - PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); + PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); PL_curstackinfo->si_type = PERLSI_MAIN; PL_curstack = PL_curstackinfo->si_stack; PL_mainstack = PL_curstack; /* remember in case we switch stacks */ @@ -492,47 +429,56 @@ 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,128,SV*); PL_tmps_floor = -1; PL_tmps_ix = -1; - PL_tmps_max = 96; + PL_tmps_max = 128; - New(54,PL_markstack,16,I32); + New(54,PL_markstack,32,I32); PL_markstack_ptr = PL_markstack; - PL_markstack_max = PL_markstack + 16; + PL_markstack_max = PL_markstack + 32; #ifdef SET_MARK_OFFSET SET_MARK_OFFSET; #endif - New(54,PL_scopestack,16,I32); + New(54,PL_scopestack,32,I32); PL_scopestack_ix = 0; - PL_scopestack_max = 16; + PL_scopestack_max = 32; - New(54,PL_savestack,96,ANY); + New(54,PL_savestack,64,ANY); PL_savestack_ix = 0; - PL_savestack_max = 96; + PL_savestack_max = 64; - New(54,PL_retstack,8,OP*); +#if !PERL_VERSION_ATLEAST (5,9,0) + New(54,PL_retstack,16,OP*); PL_retstack_ix = 0; - PL_retstack_max = 8; + PL_retstack_max = 16; +#endif } +#endif /* * destroy the stacks, the callchain etc... */ -STATIC void -destroy_stacks(pTHX) +static void +coro_destroy_stacks () { if (!IN_DESTRUCT) { - /* is this ugly, I ask? */ - while (PL_scopestack_ix) - LEAVE; - - /* sure it is, but more important: is it correct?? :/ */ - while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ - FREETMPS; + /* restore all saved variables and stuff */ + LEAVE_SCOPE (0); + assert (PL_tmps_floor == -1); + + /* free all temporaries */ + FREETMPS; + assert (PL_tmps_ix == -1); + + /* unwind all extra stacks */ + POPSTACK_TO (PL_mainstack); + + /* unwind main stack */ + dounwind (-1); } while (PL_curstackinfo->si_next) @@ -542,288 +488,441 @@ { PERL_SI *p = PL_curstackinfo->si_prev; - { - dSP; - SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); - PUTBACK; /* possibly superfluous */ - } - if (!IN_DESTRUCT) - { - dounwind(-1); - SvREFCNT_dec(PL_curstackinfo->si_stack); - } + SvREFCNT_dec (PL_curstackinfo->si_stack); - Safefree(PL_curstackinfo->si_cxstack); - Safefree(PL_curstackinfo); + Safefree (PL_curstackinfo->si_cxstack); + Safefree (PL_curstackinfo); PL_curstackinfo = p; } - Safefree(PL_tmps_stack); - Safefree(PL_markstack); - Safefree(PL_scopestack); - Safefree(PL_savestack); - Safefree(PL_retstack); + Safefree (PL_tmps_stack); + Safefree (PL_markstack); + Safefree (PL_scopestack); + Safefree (PL_savestack); +#if !PERL_VERSION_ATLEAST (5,9,0) + Safefree (PL_retstack); +#endif } static void -allocate_stack (Coro__State ctx, int alloc) +setup_coro (struct coro *coro) { - coro_stack *stack; + /* + * emulate part of the perl startup here. + */ - New (0, stack, 1, coro_stack); + coro_init_stacks (); - stack->refcnt = 1; - stack->usecnt = 1; - stack->gencnt = ctx->gencnt = 0; - if (alloc) - { -#if HAVE_MMAP - stack->ssize = 128 * 1024 * sizeof (long); /* 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); - if (stack->sptr == (void *)-1) -#endif - { - /*FIXME*//*D*//* reasonable stack size! */ - stack->ssize = -4096 * sizeof (long); - New (0, stack->sptr, 4096, long); - } - } - else - stack->sptr = 0; + PL_curcop = &PL_compiling; + PL_in_eval = EVAL_NULL; + PL_curpm = 0; + PL_localizing = 0; + PL_dirty = 0; + PL_restartop = 0; + + { + dSP; + LOGOP myop; + + SvREFCNT_dec (GvAV (PL_defgv)); + GvAV (PL_defgv) = coro->args; coro->args = 0; + + Zero (&myop, 1, LOGOP); + myop.op_next = Nullop; + myop.op_flags = OPf_WANT_VOID; + + PUSHMARK (SP); + XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); + PUTBACK; + PL_op = (OP *)&myop; + PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); + SPAGAIN; + } - ctx->stack = stack; + ENTER; /* necessary e.g. for dounwind */ } static void -deallocate_stack (Coro__State ctx) +free_coro_mortal () { - coro_stack *stack = ctx->stack; - - ctx->stack = 0; - - if (stack) + if (coro_mortal) { - if (!--stack->refcnt) - { -#ifdef HAVE_MMAP - if (stack->ssize > 0 && stack->sptr) - munmap (stack->sptr, stack->ssize); - else -#endif - Safefree (stack->sptr); - - Safefree (stack); - } - else if (ctx->gencnt == stack->gencnt) - --stack->usecnt; + SvREFCNT_dec (coro_mortal); + coro_mortal = 0; } } +/* inject a fake call to Coro::State::_cctx_init into the execution */ +static void NOINLINE +prepare_cctx (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; +} + static void -setup_coro (void *arg) +coro_run (void *arg) { + /* coro_run is the alternative tail of transfer(), so unlock here. */ + UNLOCK; + /* - * emulate part of the perl startup here. + * this is a _very_ stripped down perl interpreter ;) */ - dSP; - Coro__State ctx = (Coro__State)arg; - SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); + PL_top_env = &PL_start_env; - coro_init_stacks (aTHX); - /*PL_curcop = 0;*/ - /*PL_in_eval = PL_in_eval;*/ /* inherit */ - SvREFCNT_dec (GvAV (PL_defgv)); - GvAV (PL_defgv) = ctx->args; ctx->args = 0; + /* inject call to cctx_init */ + prepare_cctx ((coro_cctx *)arg); - SPAGAIN; + /* somebody will hit me for both perl_run and PL_restartop */ + PL_restartop = PL_op; + perl_run (PL_curinterp); - if (ctx->stack) - { - ctx->cursp = 0; + fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); + abort (); +} - PUSHMARK(SP); - PUTBACK; - (void) call_sv (sub_init, G_VOID|G_NOARGS|G_EVAL); +static coro_cctx * +cctx_new () +{ + coro_cctx *cctx; - if (SvTRUE (ERRSV)) - croak (NULL); - else - croak ("FATAL: CCTXT coroutine returned!"); - } - else + ++cctx_count; + + Newz (0, cctx, 1, coro_cctx); + +#if HAVE_MMAP + + cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + 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) { - UNOP myop; + perror ("FATAL: unable to mmap stack for coroutine"); + _exit (EXIT_FAILURE); + } + +# if STACKGUARD + mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); +# endif - PL_op = (OP *)&myop; +#else - Zero(&myop, 1, UNOP); - myop.op_next = Nullop; - myop.op_flags = OPf_WANT_VOID; - - PUSHMARK(SP); - XPUSHs (sub_init); - /* - * the next line is slightly wrong, as PL_op->op_next - * is actually being executed so we skip the first op. - * that doesn't matter, though, since it is only - * pp_nextstate and we never return... - * ah yes, and I don't care anyways ;) - */ - PUTBACK; - PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); - SPAGAIN; + cctx->ssize = STACKSIZE * (long)sizeof (long); + New (0, cctx->sptr, STACKSIZE, long); - ENTER; /* necessary e.g. for dounwind */ + if (!cctx->sptr) + { + perror ("FATAL: unable to malloc stack for coroutine"); + _exit (EXIT_FAILURE); } + +#endif + +#if USE_VALGRIND + cctx->valgrind_id = VALGRIND_STACK_REGISTER ( + STACKGUARD * PAGESIZE + (char *)cctx->sptr, + cctx->ssize + (char *)cctx->sptr + ); +#endif + + coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); + + return cctx; } static void -continue_coro (void *arg) +cctx_destroy (coro_cctx *cctx) { - /* - * this is a _very_ stripped down perl interpreter ;) - */ - Coro__State ctx = (Coro__State)arg; - JMPENV coro_start_env; + if (!cctx) + return; - PL_top_env = &ctx->start_env; + --cctx_count; - ctx->cursp = 0; - PL_op = PL_op->op_next; - CALLRUNOPS(aTHX); +#if USE_VALGRIND + VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); +#endif - abort (); +#if HAVE_MMAP + munmap (cctx->sptr, cctx->ssize); +#else + Safefree (cctx->sptr); +#endif + + Safefree (cctx); } -STATIC void -transfer(pTHX_ struct coro *prev, struct coro *next, int flags) +static coro_cctx * +cctx_get () +{ + coro_cctx *cctx; + + if (cctx_first) + { + cctx = cctx_first; + cctx_first = cctx->next; + --cctx_idle; + } + else + { + cctx = cctx_new (); + PL_op = PL_op->op_next; + } + + return cctx; +} + +static void +cctx_put (coro_cctx *cctx) +{ + /* 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; +} + +/* never call directly, always through the coro_state_transfer global variable */ +static void NOINLINE +transfer (struct coro *prev, struct coro *next) { dSTACKLEVEL; - static struct coro *xnext; - if (prev != next) + /* sometimes transfer is only called to set idle_sp */ + if (!next) { - xnext = 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_cctx *prev__cctx; - if (next->mainstack) + if (prev->flags & CF_NEW) { - 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; + } - /* mark this state as in-use */ - next->mainstack = 0; - next->tmps_ix = -2; + /*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"); - /* stacklevel changed? if yes, grab the stack for us! */ - if (flags & TRANSFER_SAVE_CCTXT) - { - if (!prev->stack) - allocate_stack (prev, 0); - else if (prev->cursp != stacklevel - && prev->stack->usecnt > 1) - { - prev->gencnt = ++prev->stack->gencnt; - prev->stack->usecnt = 1; - } - - /* has our stack been invalidated? */ - if (next->stack && next->stack->gencnt != next->gencnt) - { - deallocate_stack (next); - allocate_stack (next, 1); - coro_create (&(next->stack->cctx), - continue_coro, (void *)next, - next->stack->sptr, labs (next->stack->ssize)); - } + if (next->flags & CF_RUNNING) + croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); - coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); - /* don't add any code here */ - } + 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_perl (prev); + /* setup coroutine call */ + setup_coro (next); + /* need a new stack */ + assert (!next->cctx); } - else if (next->tmps_ix == -2) - croak ("tried to transfer to running coroutine"); else { - SAVE (prev, -1); /* first get rid of the old state */ + /* coroutine already started */ + save_perl (prev); + load_perl (next); + } - if (flags & TRANSFER_SAVE_CCTXT) - { - if (!prev->stack) - allocate_stack (prev, 0); + prev__cctx = prev->cctx; - if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) - { - PL_top_env = &next->start_env; - - setup_coro (next); - - prev->stack->refcnt++; - prev->stack->usecnt++; - next->stack = prev->stack; - next->gencnt = prev->gencnt; - } - else - { - assert (!next->stack); - allocate_stack (next, 1); - coro_create (&(next->stack->cctx), - setup_coro, (void *)next, - next->stack->sptr, labs (next->stack->ssize)); - coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); - /* don't add any code here */ - } - } - else - setup_coro (next); + /* possibly "free" the cctx */ + if (prev__cctx->idle_sp == STACKLEVEL) + { + /* 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)); + + prev->cctx = 0; + + cctx_put (prev__cctx); + prev__cctx->inuse = 0; + } + + if (!next->cctx) + { + next->cctx = cctx_get (); + assert (!next->cctx->inuse); + next->cctx->inuse = 1; + } + + 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); } - /* - * xnext is now either prev or next, depending on wether - * we switched the c stack or not. that's why I use a global - * variable, that should become thread-specific at one point. - */ - xnext->cursp = stacklevel; + free_coro_mortal (); + UNLOCK; } +} - if (coro_mortal) +struct transfer_args +{ + struct coro *prev, *next; +}; + +#define TRANSFER(ta) transfer ((ta).prev, (ta).next) + +static int +coro_state_destroy (struct coro *coro) +{ + if (coro->flags & CF_DESTROYED) + return 0; + + coro->flags |= CF_DESTROYED; + + if (coro->mainstack && coro->mainstack != main_mainstack) { - SvREFCNT_dec (coro_mortal); - coro_mortal = 0; + 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); + + coro_destroy_stacks (); + + load_perl (&temp); /* this will get rid of defsv etc.. */ + + coro->mainstack = 0; + } + + cctx_destroy (coro->cctx); + SvREFCNT_dec (coro->args); + + return 1; +} + +static int +coro_state_free (pTHX_ SV *sv, MAGIC *mg) +{ + struct coro *coro = (struct coro *)mg->mg_ptr; + mg->mg_ptr = 0; + + if (--coro->refcnt < 0) + { + coro_state_destroy (coro); + Safefree (coro); } + + return 0; +} + +static int +coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params) +{ + struct coro *coro = (struct coro *)mg->mg_ptr; + + ++coro->refcnt; + + return 0; } -#define SV_CORO(sv,func) \ - do { \ - if (SvROK (sv)) \ - sv = SvRV (sv); \ - \ - if (SvTYPE(sv) == SVt_PVHV) \ - { \ - HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ - \ - if (!he) \ - croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ - \ - (sv) = SvRV (HeVAL(he)); \ - } \ - \ - /* must also be changed inside Coro::Cont::yield */ \ - if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \ - croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ - \ - } while(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) +{ + HV *stash; + MAGIC *mg; + + if (SvROK (coro)) + coro = SvRV (coro); -#define SvSTATE(sv) (struct coro *)SvIV (sv) + 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 = SvMAGIC (coro); + assert (mg->mg_type == PERL_MAGIC_ext); + return (struct coro *)mg->mg_ptr; +} static void -api_transfer(pTHX_ SV *prev, SV *next, int flags) +prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) { - SV_CORO (prev, "Coro::transfer"); - SV_CORO (next, "Coro::transfer"); + ta->prev = SvSTATE (prev_sv); + ta->next = SvSTATE (next_sv); +} - transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); +static void +api_transfer (SV *prev_sv, SV *next_sv) +{ + struct transfer_args ta; + + prepare_transfer (&ta, prev_sv, next_sv); + TRANSFER (ta); +} + +static int +api_save (SV *coro_sv, int new_save) +{ + struct coro *coro = SvSTATE (coro_sv); + int old_save = coro->save; + + if (new_save >= 0) + coro->save = new_save; + + return old_save; } /** Coro ********************************************************************/ @@ -836,29 +935,15 @@ #define PRIO_MIN -4 /* for Coro.pm */ -static GV *coro_current, *coro_idle; -static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; +static SV *coro_current; +static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; static int coro_nready; static void -coro_enq (SV *sv) +coro_enq (SV *coro_sv) { - if (SvTYPE (sv) == SVt_PVHV) - { - SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); - int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; - - prio = prio > PRIO_MAX ? PRIO_MAX - : prio < PRIO_MIN ? PRIO_MIN - : prio; - - av_push (coro_ready [prio - PRIO_MIN], sv); - coro_nready++; - - return; - } - - croak ("Coro::ready tried to enqueue something that is not a coroutine"); + av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); + coro_nready++; } static SV * @@ -871,212 +956,303 @@ min_prio = 0; for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) - if (av_len (coro_ready[prio]) >= 0) + 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) { - if (SvROK (coro)) - coro = SvRV (coro); + struct coro *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; - coro_enq (SvREFCNT_inc (coro)); + LOCK; + coro_enq (SvREFCNT_inc (coro_sv)); + UNLOCK; + + return 1; +} + +static int +api_is_ready (SV *coro_sv) +{ + return !!(SvSTATE (coro_sv)->flags & CF_READY); } static void -api_schedule (void) +prepare_schedule (struct transfer_args *ta) { - SV *prev, *next; + SV *prev_sv, *next_sv; + + for (;;) + { + LOCK; + next_sv = coro_deq (PRIO_MIN); + UNLOCK; - prev = SvRV (GvSV (coro_current)); - next = coro_deq (PRIO_MIN); + /* nothing to schedule: call the idle handler */ + if (!next_sv) + { + dSP; - if (!next) - next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); + ENTER; + SAVETMPS; - /* free this only after the transfer */ - coro_mortal = prev; - SV_CORO (prev, "Coro::schedule"); + PUSHMARK (SP); + PUTBACK; + call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); + + FREETMPS; + LEAVE; + continue; + } + + ta->next = SvSTATE (next_sv); + + /* cannot transfer to destroyed coros, skip and look for next */ + if (ta->next->flags & CF_DESTROYED) + { + SvREFCNT_dec (next_sv); + continue; + } - SvRV (GvSV (coro_current)) = next; + break; + } - SV_CORO (next, "Coro::schedule"); + /* free this only after the transfer */ + prev_sv = SvRV (coro_current); + SvRV_set (coro_current, next_sv); + ta->prev = SvSTATE (prev_sv); + + assert (ta->next->flags & CF_READY); + ta->next->flags &= ~CF_READY; + + LOCK; + free_coro_mortal (); + coro_mortal = prev_sv; + UNLOCK; +} + +static void +prepare_cede (struct transfer_args *ta) +{ + api_ready (coro_current); + prepare_schedule (ta); +} - transfer (aTHX_ SvSTATE (prev), SvSTATE (next), - TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); +static int +prepare_cede_notself (struct transfer_args *ta) +{ + if (coro_nready) + { + SV *prev = SvRV (coro_current); + prepare_schedule (ta); + api_ready (prev); + return 1; + } + else + return 0; } static void +api_schedule (void) +{ + struct transfer_args ta; + + prepare_schedule (&ta); + TRANSFER (ta); +} + +static int api_cede (void) { - coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); + struct transfer_args ta; - api_schedule (); + prepare_cede (&ta); + + if (ta.prev != ta.next) + { + TRANSFER (ta); + return 1; + } + else + return 0; +} + +static int +api_cede_notself (void) +{ + struct transfer_args ta; + + if (prepare_cede_notself (&ta)) + { + TRANSFER (ta); + return 1; + } + else + return 0; } MODULE = Coro::State PACKAGE = Coro::State -PROTOTYPES: ENABLE +PROTOTYPES: DISABLE BOOT: -{ /* {} necessary for stoopid perl-5.6.x */ - ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); - PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); - coro_state_stash = gv_stashpv ("Coro::State", TRUE); +{ +#ifdef USE_ITHREADS + MUTEX_INIT (&coro_mutex); +#endif + BOOT_PAGESIZE; - 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_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); + coro_state_stash = gv_stashpv ("Coro::State", TRUE); - if (!padlist_cache) - padlist_cache = newHV (); + 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_ALL", newSViv (CORO_SAVE_ALL)); main_mainstack = PL_mainstack; coroapi.ver = CORO_API_VERSION; coroapi.transfer = api_transfer; + + assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); } -Coro::State -_newprocess(args) - SV * args - PROTOTYPE: $ +SV * +new (char *klass, ...) CODE: - Coro__State coro; +{ + struct coro *coro; + HV *hv; + int i; - if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) - croak ("Coro::State::_newprocess expects an arrayref"); - Newz (0, coro, 1, struct coro); + coro->args = newAV (); + coro->save = CORO_SAVE_ALL; + coro->flags = CF_NEW; + + 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)); - coro->args = (AV *)SvREFCNT_inc (SvRV (args)); - coro->mainstack = 0; /* actual work is done inside transfer */ - coro->stack = 0; - - /* same as JMPENV_BOOTSTRAP */ - /* we might be able to recycle start_env, but safe is safe */ - //Zero(&coro->start_env, 1, JMPENV); - coro->start_env.je_ret = -1; - coro->start_env.je_mustcatch = TRUE; - - RETVAL = coro; + for (i = 1; i < items; i++) + av_push (coro->args, newSVsv (ST (i))); +} OUTPUT: RETVAL -void -transfer(prev, next, flags) - SV *prev - SV *next - int flags - PROTOTYPE: @ - CODE: - PUTBACK; - SV_CORO (next, "Coro::transfer"); - SV_CORO (prev, "Coro::transfer"); - transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); - SPAGAIN; +int +save (SV *coro, int new_save = -1) + CODE: + RETVAL = api_save (coro, new_save); + OUTPUT: + RETVAL void -DESTROY(coro) - Coro::State coro +_set_stacklevel (...) + ALIAS: + Coro::State::transfer = 1 + Coro::schedule = 2 + Coro::cede = 3 + Coro::cede_notself = 4 CODE: +{ + struct transfer_args ta; - if (coro->mainstack && coro->mainstack != main_mainstack) + switch (ix) { - struct coro temp; - - PUTBACK; - SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); - LOAD(aTHX_ coro); - SPAGAIN; - - destroy_stacks (aTHX); - - LOAD((&temp)); /* this will get rid of defsv etc.. */ - SPAGAIN; + case 0: + ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); + ta.next = 0; + break; + + case 1: + if (items != 2) + croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); + + prepare_transfer (&ta, ST (0), ST (1)); + break; + + case 2: + prepare_schedule (&ta); + break; + + case 3: + prepare_cede (&ta); + break; + + case 4: + if (!prepare_cede_notself (&ta)) + XSRETURN_EMPTY; - coro->mainstack = 0; + break; } - deallocate_stack (coro); - SvREFCNT_dec (coro->args); - Safefree (coro); + BARRIER; + TRANSFER (ta); +} -void -flush() +bool +_destroy (SV *coro_sv) CODE: -#ifdef MAY_FLUSH - flush_padlist_cache (); -#endif + RETVAL = coro_state_destroy (SvSTATE (coro_sv)); + OUTPUT: + RETVAL void -_exit(code) +_exit (code) int code PROTOTYPE: $ CODE: -#if defined(__GLIBC__) || _POSIX_C_SOURCE _exit (code); -#else - signal (SIGTERM, SIG_DFL); - raise (SIGTERM); - exit (code); -#endif -MODULE = Coro::State PACKAGE = Coro::Cont - -# this is slightly dirty (should expose a c-level api) - -void -yield(...) - PROTOTYPE: @ - CODE: - static SV *returnstk; - SV *sv; - AV *defav = GvAV (PL_defgv); - struct coro *prev, *next; - - if (!returnstk) - returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE)); - - /* set up @_ -- ugly */ - av_clear (defav); - av_fill (defav, items - 1); - while (items--) - av_store (defav, items, SvREFCNT_inc (ST(items))); - - mg_get (returnstk); /* isn't documentation wrong for mg_get? */ - sv = av_pop ((AV *)SvRV (returnstk)); - prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); - next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); - SvREFCNT_dec (sv); +int +cctx_count () + CODE: + RETVAL = cctx_count; + OUTPUT: + RETVAL - transfer(aTHX_ prev, next, 0); +int +cctx_idle () + CODE: + RETVAL = cctx_idle; + OUTPUT: + RETVAL MODULE = Coro::State PACKAGE = Coro -# this is slightly dirty (should expose a c-level api) - BOOT: { int i; - HV *stash = gv_stashpv ("Coro", TRUE); - newCONSTSUB (stash, "PRIO_MAX", newSViv (PRIO_MAX)); - newCONSTSUB (stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); - newCONSTSUB (stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); - newCONSTSUB (stash, "PRIO_LOW", newSViv (PRIO_LOW)); - newCONSTSUB (stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); - newCONSTSUB (stash, "PRIO_MIN", newSViv (PRIO_MIN)); + coro_stash = gv_stashpv ("Coro", TRUE); + + newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); + newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); + newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); + newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); + 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 (); @@ -1084,42 +1260,105 @@ { 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); - SvREADONLY_on(sv); + sv_setiv (sv, (IV)&coroapi); + SvREADONLY_on (sv); } } void -ready(self) - SV * self +_set_current (SV *current) PROTOTYPE: $ CODE: - api_ready (self); + SvREFCNT_dec (SvRV (coro_current)); + SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); int -nready(...) +prio (Coro::State coro, int newprio = 0) + ALIAS: + nice = 1 + CODE: +{ + RETVAL = coro->prio; + + if (items > 1) + { + if (ix) + newprio = coro->prio - newprio; + + if (newprio < PRIO_MIN) newprio = PRIO_MIN; + if (newprio > PRIO_MAX) newprio = PRIO_MAX; + + coro->prio = newprio; + } +} + OUTPUT: + RETVAL + +SV * +ready (SV *self) + PROTOTYPE: $ + CODE: + RETVAL = boolSV (api_ready (self)); + OUTPUT: + RETVAL + +SV * +is_ready (SV *self) + PROTOTYPE: $ + CODE: + RETVAL = boolSV (api_is_ready (self)); + OUTPUT: + RETVAL + +int +nready (...) PROTOTYPE: CODE: RETVAL = coro_nready; OUTPUT: RETVAL -void -schedule(...) - PROTOTYPE: +MODULE = Coro::State PACKAGE = Coro::AIO + +SV * +_get_state () CODE: - api_schedule (); +{ + struct io_state *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 void -cede(...) - PROTOTYPE: +_set_state (char *data_) + PROTOTYPE: $ CODE: - api_cede (); +{ + struct io_state *data = (void *)data_; + + errno = data->errorno; + PL_laststype = data->laststype; + PL_laststatval = data->laststatval; + PL_statcache = data->statcache; +}