--- Coro/Coro/State.xs 2006/11/24 00:31:21 1.84 +++ Coro/Coro/State.xs 2006/11/27 02:08:55 1.106 @@ -1,5 +1,3 @@ -#define PERL_NO_GET_CONTEXT - #include "libcoro/coro.c" #include "EXTERN.h" @@ -8,7 +6,17 @@ #include "patchlevel.h" -#if PERL_VERSION < 6 +#if USE_VALGRIND +# include +#endif + +#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,6 +37,7 @@ # endif #endif +#include #include #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 @@ -55,24 +64,33 @@ # define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) static long pagesize; # else -# define BOOT_PAGESIZE +# define BOOT_PAGESIZE (void)0 # endif +#else +# define PAGESIZE 0 +# define BOOT_PAGESIZE (void)0 #endif -#define SUB_INIT "Coro::State::initialize" -#define UCORO_STATE "_coro_state" - /* 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) +#else +# define attribute(x) +#endif + +#define NOINLINE attribute ((noinline)) #include "CoroAPI.h" +#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ + #ifdef USE_ITHREADS static perl_mutex coro_mutex; # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) @@ -84,28 +102,35 @@ static struct CoroAPI coroapi; 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 *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 */ - - 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 *top_env; + coro_context cctx; + +#if USE_VALGRIND + int valgrind_id; +#endif +} coro_cctx; + +/* this is a structure representing a perl-level coroutine */ struct coro { - /* the optional C context */ - coro_stack *stack; - void *cursp; - int gencnt; + /* the c coroutine allocated to this perl coroutine, if any */ + coro_cctx *cctx; + + /* data associated with this coroutine (initial args) */ + AV *args; + int refcnt; /* optionally saved, might be zero */ AV *defav; @@ -145,27 +170,26 @@ I32 retstack_max; PMOP *curpm; COP *curcop; - JMPENV *top_env; - /* 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 * -coro_clone_padlist (pTHX_ CV *cv) +coro_clone_padlist (CV *cv) { AV *padlist = CvPADLIST (cv); AV *newpadlist, *newpad; 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); @@ -177,7 +201,7 @@ } static void -free_padlist (pTHX_ AV *padlist) +free_padlist (AV *padlist) { /* may be during global destruction */ if (SvREFCNT (padlist)) @@ -208,7 +232,7 @@ /* casting is fun. */ while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) - free_padlist (aTHX_ padlist); + free_padlist (padlist); SvREFCNT_dec (av); @@ -221,7 +245,7 @@ /* the next two functions merely cache the padlists */ static void -get_padlist (pTHX_ CV *cv) +get_padlist (CV *cv) { MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); @@ -230,19 +254,19 @@ else { #if 0 - /* this should work - but it doesn't :( */ - CV *cp = Perl_cv_clone (aTHX_ cv); + /* 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 (aTHX_ cv); + CvPADLIST (cv) = coro_clone_padlist (cv); #endif } } static void -put_padlist (pTHX_ CV *cv) +put_padlist (CV *cv) { MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); @@ -260,13 +284,13 @@ #define SB do { #define SE } while (0) -#define LOAD(state) load_state(aTHX_ (state)); -#define SAVE(state,flags) save_state(aTHX_ (state),(flags)); +#define LOAD(state) load_state((state)); +#define SAVE(state,flags) save_state((state),(flags)); #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE static void -load_state(pTHX_ Coro__State c) +load_state(Coro__State c) { PL_dowarn = c->dowarn; PL_in_eval = c->in_eval; @@ -294,14 +318,13 @@ PL_savestack = c->savestack; PL_savestack_ix = c->savestack_ix; PL_savestack_max = c->savestack_max; -#if PERL_VERSION < 9 +#if !PERL_VERSION_ATLEAST (5,9,0) 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; - PL_top_env = c->top_env; if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); if (c->defsv) REPLACE_SV (DEFSV , c->defsv); @@ -318,7 +341,7 @@ if (padlist) { - put_padlist (aTHX_ cv); /* mark this padlist as available */ + put_padlist (cv); /* mark this padlist as available */ CvPADLIST(cv) = padlist; } @@ -330,7 +353,7 @@ } static void -save_state(pTHX_ Coro__State c, int flags) +save_state(Coro__State c, int flags) { { dSP; @@ -368,7 +391,7 @@ PUSHs ((SV *)CvPADLIST(cv)); PUSHs ((SV *)cv); - get_padlist (aTHX_ cv); + get_padlist (cv); } } #ifdef CXt_FORMAT @@ -422,14 +445,13 @@ c->savestack = PL_savestack; c->savestack_ix = PL_savestack_ix; c->savestack_max = PL_savestack_max; -#if PERL_VERSION < 9 +#if !PERL_VERSION_ATLEAST (5,9,0) 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->top_env = PL_top_env; } /* @@ -439,10 +461,8 @@ * not usually need a lot of stackspace. */ static void -coro_init_stacks (pTHX) +coro_init_stacks () { - LOCK; - PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); PL_curstackinfo->si_type = PERLSI_MAIN; PL_curstack = PL_curstackinfo->si_stack; @@ -473,20 +493,18 @@ PL_savestack_ix = 0; PL_savestack_max = 96; -#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; } /* * destroy the stacks, the callchain etc... */ static void -destroy_stacks(pTHX) +coro_destroy_stacks () { if (!IN_DESTRUCT) { @@ -527,285 +545,362 @@ 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 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; + PL_curcop = 0; + PL_in_eval = 0; + PL_curpm = 0; - if (alloc) - { -#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); - if (stack->sptr != (void *)-1) - { -# if STACKGUARD - mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); -# endif - } - else -#endif - { - stack->ssize = - (STACKSIZE * (long)sizeof (long)); - New (0, stack->sptr, STACKSIZE, long); - } - } - else - stack->sptr = 0; + { + dSP; + LOGOP myop; + + /* I have no idea why this is needed, but it is */ + PUSHMARK (SP); + + 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; + + PL_op = (OP *)&myop; + + PUSHMARK (SP); + XPUSHs ((SV *)get_cv ("Coro::State::coro_init", FALSE)); + PUTBACK; + 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; } } +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; + + sv_setiv (get_sv ("Coro::State::cctx", FALSE), PTR2IV (cctx)); + + PUSHMARK (SP); + XPUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE)); + PUTBACK; + PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); + SPAGAIN; +} + static void -setup_coro (void *arg) +coro_run (void *arg) { + /* coro_run is the alternative epilogue of transfer() */ + UNLOCK; + /* - * emulate part of the perl startup here. + * this is a _very_ stripped down perl interpreter ;) */ - dTHX; - dSP; - Coro__State ctx = (Coro__State)arg; - SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); + PL_top_env = &PL_start_env; + /* inject call to cctx_init */ + prepare_cctx ((coro_cctx *)arg); - 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; + /* somebody will hit me for both perl_run and PL_restartop */ + perl_run (PL_curinterp); - SPAGAIN; + fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); + abort (); +} - if (ctx->stack) - { - ctx->cursp = 0; +static coro_cctx * +cctx_new () +{ + coro_cctx *cctx; - PUSHMARK(SP); - PUTBACK; - (void) call_sv (sub_init, G_VOID|G_NOARGS|G_EVAL); + New (0, cctx, 1, coro_cctx); - if (SvTRUE (ERRSV)) - croak (NULL); - else - croak ("FATAL: CCTXT coroutine returned!"); - } - else +#if HAVE_MMAP + + cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; + /* mmap suppsedly 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); + } - Zero(&myop, 1, UNOP); - myop.op_next = Nullop; - myop.op_flags = OPf_WANT_VOID; - - PL_op = (OP *)&myop; - - 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; +# if STACKGUARD + mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); +# endif + +#else - ENTER; /* necessary e.g. for dounwind */ + 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); } + +#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_free (coro_cctx *cctx) { - /* - * this is a _very_ stripped down perl interpreter ;) - */ - dTHX; - Coro__State ctx = (Coro__State)arg; + if (!cctx) + return; - PL_top_env = &PL_start_env; +#if USE_VALGRIND + VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); +#endif - ctx->cursp = 0; - PL_op = PL_op->op_next; - CALLRUNOPS(aTHX); +#if HAVE_MMAP + munmap (cctx->sptr, cctx->ssize); +#else + Safefree (cctx->sptr); +#endif - abort (); + Safefree (cctx); +} + +static coro_cctx *cctx_first; +static int cctx_count, cctx_idle; + +static coro_cctx * +cctx_get () +{ + coro_cctx *cctx; + + if (cctx_first) + { + --cctx_idle; + cctx = cctx_first; + cctx_first = cctx->next; + } + else + { + ++cctx_count; + cctx = cctx_new (); + PL_op = PL_op->op_next; + } + + return cctx; } -/* never call directly, always through the coro_state_transfer global variable */ static void -transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) +cctx_put (coro_cctx *cctx) +{ + ++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, int flags) { dSTACKLEVEL; - if (prev != next) + /* sometimes transfer is only called to set idle_sp */ + if (flags == TRANSFER_SET_STACKLEVEL) + ((coro_cctx *)prev)->idle_sp = STACKLEVEL; + else if (prev != next) { - /* has this coro been created yet? */ + coro_cctx *prev__cctx; + + LOCK; + if (next->mainstack) { - LOCK; + /* coroutine already started */ SAVE (prev, flags); LOAD (next); - UNLOCK; - - /* mark this state as in-use */ - next->mainstack = 0; - next->tmps_ix = -2; - - /* 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)); - } - - coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); - prev->cursp = stacklevel; - /* don't add any code here */ - } - else - next->cursp = stacklevel; } - else if (next->tmps_ix == -2) - croak ("tried to transfer to running coroutine"); else { - LOCK; - SAVE (prev, -1); /* first get rid of the old state */ - UNLOCK; + /* need to start coroutine */ + /* first get rid of the old state */ + SAVE (prev, -1); + /* setup coroutine call */ + setup_coro (next); + /* need a stack */ + next->cctx = 0; + } - /* create the coroutine for the first time */ - if (flags & TRANSFER_SAVE_CCTXT) - { - if (!prev->stack) - allocate_stack (prev, 0); + if (!prev->cctx) + /* create a new empty context */ + Newz (0, prev->cctx, 1, coro_cctx); - /* the new coroutine starts with start_env again */ - PL_top_env = &PL_start_env; + prev__cctx = prev->cctx; - if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) - { - setup_coro (next); - next->cursp = stacklevel; - - 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)); - prev->cursp = stacklevel; - /* don't add any code here */ - } - } - else - { - setup_coro (next); - next->cursp = stacklevel; - } + /* possibly "free" the cctx */ + if (prev__cctx->idle_sp == STACKLEVEL) + { + cctx_put (prev__cctx); + prev->cctx = 0; } + + if (!next->cctx) + next->cctx = cctx_get (); + + 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 (); + + UNLOCK; } +} - LOCK; - if (coro_mortal) +struct transfer_args +{ + struct coro *prev, *next; + int flags; +}; + +#define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags) + +static void +coro_state_destroy (struct coro *coro) +{ + if (coro->refcnt--) + return; + + if (coro->mainstack && coro->mainstack != main_mainstack) { - SvREFCNT_dec (coro_mortal); - coro_mortal = 0; + struct coro temp; + + SAVE ((&temp), TRANSFER_SAVE_ALL); + LOAD (coro); + + coro_destroy_stacks (); + + LOAD ((&temp)); /* this will get rid of defsv etc.. */ + + coro->mainstack = 0; } - UNLOCK; + + cctx_free (coro->cctx); + SvREFCNT_dec (coro->args); + Safefree (coro); } -void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl; +static int +coro_state_clear (pTHX_ SV *sv, MAGIC *mg) +{ + struct coro *coro = (struct coro *)mg->mg_ptr; + mg->mg_ptr = 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) - -#define SvSTATE(sv) INT2PTR (struct coro *, SvIVX (sv)) + coro_state_destroy (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; +} + +static MGVTBL coro_state_vtbl = { + 0, 0, 0, 0, + coro_state_clear, + 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); + + 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 +prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags) +{ + ta->prev = SvSTATE (prev); + ta->next = SvSTATE (next); + ta->flags = flags; +} static void -api_transfer (pTHX_ SV *prev, SV *next, int flags) +api_transfer (SV *prev, SV *next, int flags) { - SV_CORO (prev, "Coro::transfer"); - SV_CORO (next, "Coro::transfer"); + dTHX; + struct transfer_args ta; - coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); + prepare_transfer (&ta, prev, next, flags); + TRANSFER (ta); } /** Coro ********************************************************************/ @@ -823,27 +918,21 @@ static int coro_nready; static void -coro_enq (pTHX_ SV *sv) +coro_enq (SV *sv) { - SV **xprio; int prio; if (SvTYPE (sv) != SVt_PVHV) croak ("Coro::ready tried to enqueue something that is not a coroutine"); - xprio = hv_fetch ((HV *)sv, "prio", 4, 0); - prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; - - prio = prio > PRIO_MAX ? PRIO_MAX - : prio < PRIO_MIN ? PRIO_MIN - : prio; + prio = SvSTATE (sv)->prio; av_push (coro_ready [prio - PRIO_MIN], sv); coro_nready++; } static SV * -coro_deq (pTHX_ int min_prio) +coro_deq (int min_prio) { int prio = PRIO_MAX - PRIO_MIN; @@ -870,186 +959,243 @@ coro = SvRV (coro); LOCK; - coro_enq (aTHX_ SvREFCNT_inc (coro)); + coro_enq (SvREFCNT_inc (coro)); UNLOCK; } static void -api_schedule (void) +prepare_schedule (struct transfer_args *ta) { - dTHX; + SV *current, *prev, *next; - SV *prev, *next; + current = GvSV (coro_current); - LOCK; + for (;;) + { + LOCK; + next = coro_deq (PRIO_MIN); + UNLOCK; + + if (next) + break; - prev = SvRV (GvSV (coro_current)); - next = coro_deq (aTHX_ PRIO_MIN); + { + dSP; + + ENTER; + SAVETMPS; + + PUSHMARK (SP); + PUTBACK; + call_sv (GvSV (coro_idle), G_DISCARD); + + FREETMPS; + LEAVE; + } + } - if (!next) - next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); + prev = SvRV (current); + SvRV (current) = next; /* free this only after the transfer */ + LOCK; + free_coro_mortal (); + UNLOCK; coro_mortal = prev; - SV_CORO (prev, "Coro::schedule"); - - SvRV (GvSV (coro_current)) = next; - SV_CORO (next, "Coro::schedule"); + ta->prev = SvSTATE (prev); + ta->next = SvSTATE (next); + ta->flags = TRANSFER_SAVE_ALL; +} +static void +prepare_cede (struct transfer_args *ta) +{ + LOCK; + coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); UNLOCK; - coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), - TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); + prepare_schedule (ta); } static void -api_cede (void) +api_schedule (void) { dTHX; + struct transfer_args ta; - LOCK; - coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); - UNLOCK; + prepare_schedule (&ta); + TRANSFER (ta); +} - api_schedule (); +static void +api_cede (void) +{ + dTHX; + struct transfer_args ta; + + prepare_cede (&ta); + TRANSFER (ta); } MODULE = Coro::State PACKAGE = Coro::State -PROTOTYPES: ENABLE +PROTOTYPES: DISABLE BOOT: -{ /* {} necessary for stoopid perl-5.6.x */ +{ #ifdef USE_ITHREADS MUTEX_INIT (&coro_mutex); #endif BOOT_PAGESIZE; - 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); 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)); 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->args = (AV *)SvREFCNT_inc (SvRV (args)); - /*coro->mainstack = 0; *//*actual work is done inside transfer */ - /*coro->stack = 0;*/ + 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)); - 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"); - coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); - SPAGAIN; - -void -DESTROY(coro) - Coro::State coro +_set_stacklevel (...) + ALIAS: + Coro::State::transfer = 1 + Coro::schedule = 2 + Coro::cede = 3 + Coro::Cont::yield = 4 CODE: +{ + struct transfer_args ta; - if (coro->mainstack && coro->mainstack != main_mainstack) + switch (ix) { - struct coro temp; + case 0: + ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); + ta.next = 0; + ta.flags = TRANSFER_SET_STACKLEVEL; + break; + + case 1: + 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))); + break; + + case 2: + prepare_schedule (&ta); + break; + + case 3: + prepare_cede (&ta); + break; - PUTBACK; - SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); - LOAD (aTHX_ coro); - SPAGAIN; + case 4: + { + SV *yieldstack; + SV *sv; + AV *defav = GvAV (PL_defgv); + + 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)); + ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0)); + ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0)); + ta.flags = 0; + SvREFCNT_dec (sv); + } + break; - destroy_stacks (aTHX); + } - LOAD ((&temp)); /* this will get rid of defsv etc.. */ - SPAGAIN; + TRANSFER (ta); +} - coro->mainstack = 0; - } +void +_clone_state_from (SV *dst, SV *src) + CODE: +{ + struct coro *coro_src = SvSTATE (src); - deallocate_stack (coro); - SvREFCNT_dec (coro->args); - Safefree (coro); + sv_unmagic (SvRV (dst), PERL_MAGIC_ext); + + ++coro_src->refcnt; + sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; +} void -_exit(code) +_exit (code) int code PROTOTYPE: $ CODE: _exit (code); -MODULE = Coro::State PACKAGE = Coro::Cont - -void -yield(...) - PROTOTYPE: @ - CODE: - 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 ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); - next = SvSTATE ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); - SvREFCNT_dec (sv); +int +cctx_count () + CODE: + RETVAL = cctx_count; + OUTPUT: + RETVAL - coro_state_transfer (aTHX_ prev, next, 0); +int +cctx_idle () + CODE: + RETVAL = cctx_idle; + OUTPUT: + RETVAL MODULE = Coro::State PACKAGE = Coro 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); @@ -1072,40 +1218,44 @@ } } -#if !PERL_MICRO +int +prio (Coro::State coro, int newprio = 0) + ALIAS: + nice = 1 + CODE: +{ + RETVAL = coro->prio; + + if (items > 1) + { + if (ix) + newprio += coro->prio; + + if (newprio < PRIO_MIN) newprio = PRIO_MIN; + if (newprio > PRIO_MAX) newprio = PRIO_MAX; + + coro->prio = newprio; + } +} void -ready(self) - SV * self +ready (SV *self) PROTOTYPE: $ CODE: api_ready (self); -#endif - int -nready(...) +nready (...) PROTOTYPE: CODE: RETVAL = coro_nready; OUTPUT: RETVAL -void -schedule(...) - PROTOTYPE: - CODE: - api_schedule (); - -void -cede(...) - PROTOTYPE: - CODE: - api_cede (); +MODULE = Coro::State PACKAGE = Coro::AIO -# and these are hacks SV * -_aio_get_state () +_get_state () CODE: { struct { @@ -1126,7 +1276,7 @@ RETVAL void -_aio_set_state (char *data_) +_set_state (char *data_) PROTOTYPE: $ CODE: {