--- Coro/Coro/State.xs 2008/11/15 18:40:55 1.277 +++ Coro/Coro/State.xs 2008/11/17 07:14:50 1.288 @@ -97,6 +97,9 @@ #ifndef newSV # define newSV(l) NEWSV(0,l) #endif +#ifndef CvISXSUB_on +# define CvISXSUB_on(cv) (void)cv +#endif /* 5.8.7 */ #ifndef SvRV_set @@ -144,36 +147,11 @@ #include "CoroAPI.h" #ifdef USE_ITHREADS - -static perl_mutex coro_lock; -# define LOCK do { MUTEX_LOCK (&coro_lock); } while (0) -# define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0) # if CORO_PTHREAD static void *coro_thx; # endif - -#else - -# define LOCK (void)0 -# define UNLOCK (void)0 - #endif -# undef LOCK -# define LOCK (void)0 -# undef UNLOCK -# define UNLOCK (void)0 - -/* helper storage struct for Coro::AIO */ -struct io_state -{ - AV *res; - int errorno; - I32 laststype; /* U16 in 5.10.0 */ - int laststatval; - Stat_t statcache; -}; - static double (*nvtime)(); /* so why doesn't it take void? */ static U32 cctx_gen; @@ -183,7 +161,6 @@ static JMPENV *main_top_env; static HV *coro_state_stash, *coro_stash; static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ -static volatile struct coro *transfer_next; static GV *irsgv; /* $/ */ static GV *stdoutgv; /* *STDOUT */ @@ -259,7 +236,7 @@ /* the C coroutine allocated to this perl coroutine, if any */ coro_cctx *cctx; - /* process data */ + /* state data */ struct CoroSLF slf_frame; /* saved slf frame */ AV *mainstack; perl_slots *slot; /* basically the saved sp */ @@ -268,6 +245,7 @@ int refcnt; /* coroutines are refcounted, yes */ int flags; /* CF_ flags */ HV *hv; /* the perl hash associated with this coro, if any */ + void (*on_destroy)(pTHX_ struct coro *coro); /* statistics */ int usecount; /* number of transfers to this coro */ @@ -286,7 +264,11 @@ typedef struct coro *Coro__State; typedef struct coro *Coro__State_or_hashref; +/* the following variables are effectively part of the perl context */ +/* and get copied between struct coro and these variables */ +/* the mainr easonw e don't support windows process emulation */ static struct CoroSLF slf_frame; /* the current slf frame */ +static SV *coro_throw; /** Coro ********************************************************************/ @@ -397,7 +379,7 @@ return 0; } -#define CORO_MAGIC_type_cv PERL_MAGIC_ext +#define CORO_MAGIC_type_cv 26 #define CORO_MAGIC_type_state PERL_MAGIC_ext static MGVTBL coro_cv_vtbl = { @@ -405,15 +387,18 @@ coro_cv_free }; -#define CORO_MAGIC(sv, type) \ - SvMAGIC (sv) \ - ? SvMAGIC (sv)->mg_type == type \ - ? SvMAGIC (sv) \ - : mg_find (sv, type) \ - : 0 +#define CORO_MAGIC_NN(sv, type) \ + (expect_true (SvMAGIC (sv)->mg_type == type) \ + ? SvMAGIC (sv) \ + : mg_find (sv, type)) + +#define CORO_MAGIC(sv, type) \ + (expect_true (SvMAGIC (sv)) \ + ? CORO_MAGIC_NN (sv, type) \ + : 0) -#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) -#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) +#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) +#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) INLINE struct coro * SvSTATE_ (pTHX_ SV *coro) @@ -441,6 +426,10 @@ #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) +/* faster than SvSTATE, but expects a coroutine hv */ +#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr) +#define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) + /* the next two functions merely cache the padlists */ static void get_padlist (pTHX_ CV *cv) @@ -517,12 +506,14 @@ PUTBACK; } - slf_frame = c->slf_frame; + slf_frame = c->slf_frame; + coro_throw = c->throw; } static void save_perl (pTHX_ Coro__State c) { + c->throw = coro_throw; c->slf_frame = slf_frame; { @@ -825,7 +816,7 @@ return 0; } -static void +static void NOINLINE /* noinline to keep it out of the transfer fast path */ coro_setup (pTHX_ struct coro *coro) { /* @@ -878,6 +869,8 @@ */ slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ + + coro_throw = coro->throw; } static void @@ -911,7 +904,7 @@ SvREFCNT_dec (PL_warnhook); SvREFCNT_dec (coro->saved_deffh); - SvREFCNT_dec (coro->throw); + SvREFCNT_dec (coro_throw); coro_destruct_stacks (aTHX); } @@ -931,7 +924,7 @@ { COP *oldcop = 0; int oldcxix = -2; - struct coro *coro = SvSTATE (coro_current); /* trace cctx is tied to specific coro */ + struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */ coro_cctx *cctx = coro->cctx; while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) @@ -1090,21 +1083,7 @@ INLINE void transfer_tail (pTHX) { - struct coro *next = (struct coro *)transfer_next; - assert (!(transfer_next = 0)); /* just used for the side effect when asserts are enabled */ - assert (("FATAL: next coroutine was zero in transfer_tail (please report)", next)); - free_coro_mortal (aTHX); - UNLOCK; - - if (expect_false (next->throw)) - { - SV *exception = sv_2mortal (next->throw); - - next->throw = 0; - sv_setsv (ERRSV, exception); - croak (0); - } } /* @@ -1129,7 +1108,6 @@ cctx_prepare (aTHX_ (coro_cctx *)arg); /* cctx_run is the alternative tail of transfer() */ - /* TODO: throwing an exception here might be deadly, VERIFY */ transfer_tail (aTHX); /* somebody or something will hit me for both perl_run and PL_restartop */ @@ -1295,6 +1273,8 @@ static void transfer_check (pTHX_ struct coro *prev, struct coro *next) { + /* TODO: throwing up here is considered harmful */ + if (expect_true (prev != next)) { if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) @@ -1314,7 +1294,7 @@ } /* always use the TRANSFER macro */ -static void NOINLINE +static void NOINLINE /* noinline so we have a fixed stackframe */ transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) { dSTACKLEVEL; @@ -1340,8 +1320,6 @@ prev->flags &= ~CF_RUNNING; next->flags |= CF_RUNNING; - LOCK; - /* first get rid of the old state */ save_perl (aTHX_ prev); @@ -1383,9 +1361,6 @@ if (expect_true (!next->cctx)) next->cctx = cctx_get (aTHX); - assert (("FATAL: transfer_next already nonzero in Coro (please report)", !transfer_next)); - transfer_next = next; - if (expect_false (prev__cctx != next->cctx)) { prev__cctx->top_env = PL_top_env; @@ -1408,15 +1383,16 @@ if (coro->flags & CF_DESTROYED) return 0; + if (coro->on_destroy) + coro->on_destroy (aTHX_ coro); + 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 */ @@ -1504,13 +1480,13 @@ /** Coro ********************************************************************/ -static void -coro_enq (pTHX_ SV *coro_sv) +INLINE void +coro_enq (pTHX_ struct coro *coro) { - av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); + av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv)); } -static SV * +INLINE SV * coro_deq (pTHX) { int prio; @@ -1539,16 +1515,12 @@ coro->flags |= CF_READY; - LOCK; - sv_hook = coro_nready ? 0 : coro_readyhook; xs_hook = coro_nready ? 0 : coroapi.readyhook; - coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv)); + coro_enq (aTHX_ coro); ++coro_nready; - UNLOCK; - if (sv_hook) { dSP; @@ -1584,14 +1556,12 @@ for (;;) { - LOCK; next_sv = coro_deq (aTHX); /* nothing to schedule: call the idle handler */ if (expect_false (!next_sv)) { dSP; - UNLOCK; ENTER; SAVETMPS; @@ -1606,34 +1576,30 @@ continue; } - ta->next = SvSTATE (next_sv); + ta->next = SvSTATE_hv (next_sv); /* cannot transfer to destroyed coros, skip and look for next */ if (expect_false (ta->next->flags & CF_DESTROYED)) { - UNLOCK; SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ continue; } --coro_nready; - UNLOCK; break; } /* free this only after the transfer */ prev_sv = SvRV (coro_current); - ta->prev = SvSTATE (prev_sv); + ta->prev = SvSTATE_hv (prev_sv); TRANSFER_CHECK (*ta); assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); ta->next->flags &= ~CF_READY; SvRV_set (coro_current, next_sv); - LOCK; free_coro_mortal (aTHX); coro_mortal = prev_sv; - UNLOCK; } INLINE void @@ -1722,107 +1688,14 @@ } } -#if 0 -static int -coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) -{ - AV *padlist; - AV *av = (AV *)mg->mg_obj; - - abort (); - - return 0; -} - -static MGVTBL coro_gensub_vtbl = { - 0, 0, 0, 0, - coro_gensub_free -}; -#endif - -/*****************************************************************************/ -/* PerlIO::cede */ - -typedef struct -{ - PerlIOBuf base; - NV next, every; -} PerlIOCede; - -static IV -PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) -{ - PerlIOCede *self = PerlIOSelf (f, PerlIOCede); - - self->every = SvCUR (arg) ? SvNV (arg) : 0.01; - self->next = nvtime () + self->every; - - return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab); -} - -static SV * -PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags) -{ - PerlIOCede *self = PerlIOSelf (f, PerlIOCede); - - return newSVnv (self->every); -} - -static IV -PerlIOCede_flush (pTHX_ PerlIO *f) -{ - PerlIOCede *self = PerlIOSelf (f, PerlIOCede); - double now = nvtime (); - - if (now >= self->next) - { - api_cede (aTHX); - self->next = now + self->every; - } - - return PerlIOBuf_flush (aTHX_ f); -} - -static PerlIO_funcs PerlIO_cede = -{ - sizeof(PerlIO_funcs), - "cede", - sizeof(PerlIOCede), - PERLIO_K_DESTRUCT | PERLIO_K_RAW, - PerlIOCede_pushed, - PerlIOBuf_popped, - PerlIOBuf_open, - PerlIOBase_binmode, - PerlIOCede_getarg, - PerlIOBase_fileno, - PerlIOBuf_dup, - PerlIOBuf_read, - PerlIOBuf_unread, - PerlIOBuf_write, - PerlIOBuf_seek, - PerlIOBuf_tell, - PerlIOBuf_close, - PerlIOCede_flush, - PerlIOBuf_fill, - PerlIOBase_eof, - PerlIOBase_error, - PerlIOBase_clearerr, - PerlIOBase_setlinebuf, - PerlIOBuf_get_base, - PerlIOBuf_bufsiz, - PerlIOBuf_get_ptr, - PerlIOBuf_get_cnt, - PerlIOBuf_set_ptrcnt, -}; - /*****************************************************************************/ - -static const CV *slf_cv; /* for quick consistency check */ +/* schedule-like-function opcode (SLF) */ static UNOP slf_restore; /* restore stack as entersub did, for first-re-run */ -static SV *slf_arg0; -static SV *slf_arg1; -static SV *slf_arg2; +static const CV *slf_cv; +static SV **slf_argv; +static int slf_argc, slf_arga; /* count, allocated */ +static I32 slf_ax; /* top of stack, for restore */ /* this restores the stack in the case we patched the entersub, to */ /* recreate the stack frame as perl will on following calls */ @@ -1830,14 +1703,16 @@ static OP * pp_restore (pTHX) { - dSP; + int i; + SV **SP = PL_stack_base + slf_ax; PUSHMARK (SP); - EXTEND (SP, 3); - if (slf_arg0) PUSHs (sv_2mortal (slf_arg0)); - if (slf_arg1) PUSHs (sv_2mortal (slf_arg1)); - if (slf_arg2) PUSHs (sv_2mortal (slf_arg2)); + EXTEND (SP, slf_argc + 1); + + for (i = 0; i < slf_argc; ++i) + PUSHs (sv_2mortal (slf_argv [i])); + PUSHs ((SV *)CvGV (slf_cv)); RETURNOP (slf_restore.op_first); @@ -1850,7 +1725,7 @@ } static void -slf_init_set_stacklevel (pTHX_ struct CoroSLF *frame, SV **arg, int items) +slf_init_set_stacklevel (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) { assert (("FATAL: set_stacklevel needs the coro cctx as sole argument", items == 1)); @@ -1868,7 +1743,7 @@ } static void -slf_init_transfer (pTHX_ struct CoroSLF *frame, SV **arg, int items) +slf_init_transfer (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) { if (items != 2) croak ("Coro::State::transfer (prev, next) expects two arguments, not %d,", items); @@ -1879,21 +1754,21 @@ } static void -slf_init_schedule (pTHX_ struct CoroSLF *frame, SV **arg, int items) +slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) { frame->prepare = prepare_schedule; frame->check = slf_check_nop; } static void -slf_init_cede (pTHX_ struct CoroSLF *frame, SV **arg, int items) +slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) { frame->prepare = prepare_cede; frame->check = slf_check_nop; } static void -slf_init_cede_notself (pTHX_ struct CoroSLF *frame, SV **arg, int items) +slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) { frame->prepare = prepare_cede_notself; frame->check = slf_check_nop; @@ -1926,12 +1801,11 @@ /* do a quick consistency check on the "function" object, and if it isn't */ /* for us, divert to the real entersub */ - if (SvTYPE (gv) != SVt_PVGV || !(CvFLAGS (GvCV (gv)) & CVf_SLF)) + if (SvTYPE (gv) != SVt_PVGV + || !GvCV (gv) + || !(CvFLAGS (GvCV (gv)) & CVf_SLF)) return PL_ppaddr[OP_ENTERSUB](aTHX); - /* pop args */ - SP = PL_stack_base + POPMARK; - if (!(PL_op->op_flags & OPf_STACKED)) { /* ampersand-form of call, use @_ instead of stack */ @@ -1940,12 +1814,17 @@ items = AvFILLp (av) + 1; } - PUTBACK; + /* now call the init function, which needs to set up slf_frame */ + ((coro_slf_cb)CvXSUBANY (GvCV (gv)).any_ptr) + (aTHX_ &slf_frame, GvCV (gv), arg, items); + + /* pop args */ + SP = PL_stack_base + POPMARK; - ((coro_slf_cb)CvXSUBANY (GvCV (gv)).any_ptr) (aTHX_ &slf_frame, arg, items); + PUTBACK; } - /* now interpret the slf_frame */ + /* now that we have a slf_frame, interpret it! */ /* we use a callback system not to make the code needlessly */ /* complicated, but so we can run multiple perl coros from one cctx */ @@ -1960,13 +1839,14 @@ } while (slf_frame.check (aTHX_ &slf_frame)); + slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ + + /* return value handling - mostly like entersub */ { dSP; SV **bot = PL_stack_base + checkmark; int gimme = GIMME_V; - slf_frame.prepare = 0; /* signal pp_slf that we need a new frame */ - /* make sure we put something on the stack in scalar context */ if (gimme == G_SCALAR) { @@ -1979,21 +1859,32 @@ PUTBACK; } + /* exception handling */ + if (expect_false (coro_throw)) + { + SV *exception = sv_2mortal (coro_throw); + + coro_throw = 0; + sv_setsv (ERRSV, exception); + croak (0); + } + return NORMAL; } static void -api_execute_slf (pTHX_ CV *cv, coro_slf_cb init_cb, SV **arg, int items) +api_execute_slf (pTHX_ CV *cv, coro_slf_cb init_cb, I32 ax) { + int i; + SV **arg = PL_stack_base + ax; + int items = PL_stack_sp - arg + 1; + assert (("FATAL: SLF call with illegal CV value", !CvANON (cv))); if (PL_op->op_ppaddr != PL_ppaddr [OP_ENTERSUB] && PL_op->op_ppaddr != pp_slf) croak ("FATAL: Coro SLF calls can only be made normally, not via goto or any other means, caught"); - if (items > 3) - croak ("Coro only supports up to three arguments to SLF functions currently (not %d), caught", items); - CvFLAGS (cv) |= CVf_SLF; CvXSUBANY (cv).any_ptr = (void *)init_cb; slf_cv = cv; @@ -2001,21 +1892,150 @@ /* we patch the op, and then re-run the whole call */ /* we have to put the same argument on the stack for this to work */ /* and this will be done by pp_restore */ - slf_restore.op_next = (OP *)&slf_restore; - slf_restore.op_type = OP_NULL; + slf_restore.op_next = (OP *)&slf_restore; + slf_restore.op_type = OP_CUSTOM; slf_restore.op_ppaddr = pp_restore; - slf_restore.op_first = PL_op; + slf_restore.op_first = PL_op; - slf_arg0 = items > 0 ? SvREFCNT_inc (arg [0]) : 0; - slf_arg1 = items > 1 ? SvREFCNT_inc (arg [1]) : 0; - slf_arg2 = items > 2 ? SvREFCNT_inc (arg [2]) : 0; + slf_ax = ax - 1; /* undo the ax++ inside dAXMARK */ + + if (PL_op->op_flags & OPf_STACKED) + { + if (items > slf_arga) + { + slf_arga = items; + free (slf_argv); + slf_argv = malloc (slf_arga * sizeof (SV *)); + } + + slf_argc = items; + + for (i = 0; i < items; ++i) + slf_argv [i] = SvREFCNT_inc (arg [i]); + } + else + slf_argc = 0; PL_op->op_ppaddr = pp_slf; + PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ PL_op = (OP *)&slf_restore; } /*****************************************************************************/ +/* PerlIO::cede */ + +typedef struct +{ + PerlIOBuf base; + NV next, every; +} PerlIOCede; + +static IV +PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) +{ + PerlIOCede *self = PerlIOSelf (f, PerlIOCede); + + self->every = SvCUR (arg) ? SvNV (arg) : 0.01; + self->next = nvtime () + self->every; + + return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab); +} + +static SV * +PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags) +{ + PerlIOCede *self = PerlIOSelf (f, PerlIOCede); + + return newSVnv (self->every); +} + +static IV +PerlIOCede_flush (pTHX_ PerlIO *f) +{ + PerlIOCede *self = PerlIOSelf (f, PerlIOCede); + double now = nvtime (); + + if (now >= self->next) + { + api_cede (aTHX); + self->next = now + self->every; + } + + return PerlIOBuf_flush (aTHX_ f); +} + +static PerlIO_funcs PerlIO_cede = +{ + sizeof(PerlIO_funcs), + "cede", + sizeof(PerlIOCede), + PERLIO_K_DESTRUCT | PERLIO_K_RAW, + PerlIOCede_pushed, + PerlIOBuf_popped, + PerlIOBuf_open, + PerlIOBase_binmode, + PerlIOCede_getarg, + PerlIOBase_fileno, + PerlIOBuf_dup, + PerlIOBuf_read, + PerlIOBuf_unread, + PerlIOBuf_write, + PerlIOBuf_seek, + PerlIOBuf_tell, + PerlIOBuf_close, + PerlIOCede_flush, + PerlIOBuf_fill, + PerlIOBase_eof, + PerlIOBase_error, + PerlIOBase_clearerr, + PerlIOBase_setlinebuf, + PerlIOBuf_get_base, + PerlIOBuf_bufsiz, + PerlIOBuf_get_ptr, + PerlIOBuf_get_cnt, + PerlIOBuf_set_ptrcnt, +}; + +/*****************************************************************************/ +/* Coro::Semaphore */ + +static void +coro_semaphore_adjust (pTHX_ AV *av, IV adjust) +{ + SV *count_sv = AvARRAY (av)[0]; + IV count = SvIVX (count_sv); + + count += adjust; + SvIVX (count_sv) = count; + + /* now wake up as many waiters as are expected to lock */ + while (count > 0 && AvFILLp (av) > 0) + { + SV *cb; + + /* swap first two elements so we can shift a waiter */ + AvARRAY (av)[0] = AvARRAY (av)[1]; + AvARRAY (av)[1] = count_sv; + cb = av_shift (av); + + if (SvOBJECT (cb)) + api_ready (aTHX_ cb); + else + croak ("callbacks not yet supported"); + + SvREFCNT_dec (cb); + + --count; + } +} + +static void +coro_semaphore_on_destroy (pTHX_ struct coro *coro) +{ + /* call $sem->adjust (0) to possibly wake up some other waiters */ + coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); +} static int slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) @@ -2025,6 +2045,7 @@ if (SvIVX (count_sv) > 0) { + SvSTATE_current->on_destroy = 0; SvIVX (count_sv) = SvIVX (count_sv) - 1; return 0; } @@ -2045,7 +2066,7 @@ } static void -slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, SV **arg, int items) +slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) { AV *av = (AV *)SvRV (arg [0]); @@ -2053,6 +2074,7 @@ { frame->data = (void *)av; frame->prepare = prepare_nop; + SvSTATE_current->on_destroy = coro_semaphore_on_destroy; } else { @@ -2060,12 +2082,201 @@ frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); frame->prepare = prepare_schedule; + + /* to avoid race conditions when a woken-up coro gets terminated */ + /* we arrange for a temporary on_destroy that calls adjust (0) */ + assert (!SvSTATE_current->on_destroy);//D + SvSTATE_current->on_destroy = coro_semaphore_on_destroy; } frame->check = slf_check_semaphore_down; } +/*****************************************************************************/ +/* gensub: simple closure generation utility */ + +#define GENSUB_ARG CvXSUBANY (cv).any_ptr + +/* create a closure from XS, returns a code reference */ +/* the arg can be accessed via GENSUB_ARG from the callback */ +/* the callback must use dXSARGS/XSRETURN */ +static SV * +gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg) +{ + CV *cv = (CV *)newSV (0); + + sv_upgrade ((SV *)cv, SVt_PVCV); + + CvANON_on (cv); + CvISXSUB_on (cv); + CvXSUB (cv) = xsub; + GENSUB_ARG = arg; + + return newRV_noinc ((SV *)cv); +} + +/*****************************************************************************/ +/* Coro::AIO */ + +#define CORO_MAGIC_type_aio PERL_MAGIC_ext + +/* helper storage struct */ +struct io_state +{ + int errorno; + I32 laststype; /* U16 in 5.10.0 */ + int laststatval; + Stat_t statcache; +}; + +static void +coro_aio_callback (pTHX_ CV *cv) +{ + dXSARGS; + AV *state = (AV *)GENSUB_ARG; + SV *coro = av_pop (state); + SV *data_sv = newSV (sizeof (struct io_state)); + + av_extend (state, items); + + sv_upgrade (data_sv, SVt_PV); + SvCUR_set (data_sv, sizeof (struct io_state)); + SvPOK_only (data_sv); + + { + struct io_state *data = (struct io_state *)SvPVX (data_sv); + + data->errorno = errno; + data->laststype = PL_laststype; + data->laststatval = PL_laststatval; + data->statcache = PL_statcache; + } + + /* now build the result vector out of all the parameters and the data_sv */ + { + int i; + + for (i = 0; i < items; ++i) + av_push (state, SvREFCNT_inc_NN (ST (i))); + } + + av_push (state, data_sv); + + api_ready (aTHX_ coro); + SvREFCNT_dec (coro); + SvREFCNT_dec ((AV *)state); +} + +static int +slf_check_aio_req (pTHX_ struct CoroSLF *frame) +{ + AV *state = (AV *)frame->data; + + /* one element that is an RV? repeat! */ + if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) + return 1; + + /* restore status */ + { + SV *data_sv = av_pop (state); + struct io_state *data = (struct io_state *)SvPVX (data_sv); + + errno = data->errorno; + PL_laststype = data->laststype; + PL_laststatval = data->laststatval; + PL_statcache = data->statcache; + + SvREFCNT_dec (data_sv); + } + + /* push result values */ + { + dSP; + int i; + + EXTEND (SP, AvFILLp (state) + 1); + for (i = 0; i <= AvFILLp (state); ++i) + PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (state)[i]))); + + PUTBACK; + } + + return 0; +} + +static void +slf_init_aio_req (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) +{ + AV *state = (AV *)sv_2mortal ((SV *)newAV ()); + SV *coro_hv = SvRV (coro_current); + struct coro *coro = SvSTATE_hv (coro_hv); + + /* put our coroutine id on the state arg */ + av_push (state, SvREFCNT_inc_NN (coro_hv)); + + /* first see whether we have a non-zero priority and set it as AIO prio */ + if (coro->prio) + { + dSP; + + static SV *prio_cv; + static SV *prio_sv; + + if (expect_false (!prio_cv)) + { + prio_cv = (SV *)get_cv ("IO::AIO::aioreq_pri", 0); + prio_sv = newSViv (0); + } + + PUSHMARK (SP); + sv_setiv (prio_sv, coro->prio); + XPUSHs (prio_sv); + + PUTBACK; + call_sv (prio_cv, G_VOID | G_DISCARD); + } + + /* now call the original request */ + { + dSP; + CV *req = (CV *)CORO_MAGIC_NN ((SV *)cv, CORO_MAGIC_type_aio)->mg_obj; + int i; + + PUSHMARK (SP); + + /* first push all args to the stack */ + EXTEND (SP, items + 1); + + for (i = 0; i < items; ++i) + PUSHs (arg [i]); + + /* now push the callback closure */ + PUSHs (sv_2mortal (gensub (aTHX_ coro_aio_callback, (void *)SvREFCNT_inc_NN ((SV *)state)))); + + /* now call the AIO function - we assume our request is uncancelable */ + PUTBACK; + call_sv ((SV *)req, G_VOID | G_DISCARD); + } + + /* now that the requets is going, we loop toll we have a result */ + frame->data = (void *)state; + frame->prepare = prepare_schedule; + frame->check = slf_check_aio_req; +} + +static void +coro_aio_req_xs (pTHX_ CV *cv) +{ + dXSARGS; + + CORO_EXECUTE_SLF_XS (slf_init_aio_req); + + XSRETURN_EMPTY; +} + +/*****************************************************************************/ + MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ PROTOTYPES: DISABLE @@ -2073,7 +2284,6 @@ BOOT: { #ifdef USE_ITHREADS - MUTEX_INIT (&coro_lock); # if CORO_PTHREAD coro_thx = PERL_GET_CONTEXT; # endif @@ -2104,6 +2314,18 @@ while (main_top_env->je_prev) main_top_env = main_top_env->je_prev; + { + SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); + + if (!PL_custom_op_names) PL_custom_op_names = newHV (); + hv_store_ent (PL_custom_op_names, slf, + newSVpv ("coro_slf", 0), 0); + + if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); + hv_store_ent (PL_custom_op_descs, slf, + newSVpv ("coro schedule like function", 0), 0); + } + coroapi.ver = CORO_API_VERSION; coroapi.rev = CORO_API_REVISION; @@ -2160,13 +2382,13 @@ void _set_stacklevel (...) CODE: - api_execute_slf (aTHX_ cv, slf_init_set_stacklevel, &ST (0), items); + CORO_EXECUTE_SLF_XS (slf_init_set_stacklevel); void transfer (...) PROTOTYPE: $$ CODE: - api_execute_slf (aTHX_ cv, slf_init_transfer, &ST (0), items); + CORO_EXECUTE_SLF_XS (slf_init_transfer); bool _destroy (SV *coro_sv) @@ -2294,8 +2516,12 @@ throw (Coro::State self, SV *throw = &PL_sv_undef) PROTOTYPE: $;$ CODE: - SvREFCNT_dec (self->throw); - self->throw = SvOK (throw) ? newSVsv (throw) : 0; +{ + struct coro *current = SvSTATE_current; + SV **throwp = self == current ? &coro_throw : &self->throw; + SvREFCNT_dec (*throwp); + *throwp = SvOK (throw) ? newSVsv (throw) : 0; +} void api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) @@ -2336,8 +2562,7 @@ force_cctx () PROTOTYPE: CODE: - struct coro *coro = SvSTATE (coro_current); - coro->cctx->idle_sp = 0; + SvSTATE_current->cctx->idle_sp = 0; void swap_defsv (Coro::State self) @@ -2355,6 +2580,7 @@ SV *tmp = *src; *src = *dst; *dst = tmp; } + MODULE = Coro::State PACKAGE = Coro BOOT: @@ -2400,17 +2626,17 @@ void schedule (...) CODE: - api_execute_slf (aTHX_ cv, slf_init_schedule, &ST (0), 0); + CORO_EXECUTE_SLF_XS (slf_init_schedule); void cede (...) CODE: - api_execute_slf (aTHX_ cv, slf_init_cede, &ST (0), 0); + CORO_EXECUTE_SLF_XS (slf_init_cede); void cede_notself (...) CODE: - api_execute_slf (aTHX_ cv, slf_init_cede_notself, &ST (0), 0); + CORO_EXECUTE_SLF_XS (slf_init_cede_notself); void _set_current (SV *current) @@ -2423,10 +2649,8 @@ _set_readyhook (SV *hook) PROTOTYPE: $ CODE: - LOCK; SvREFCNT_dec (coro_readyhook); coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; - UNLOCK; int prio (Coro::State coro, int newprio = 0) @@ -2472,8 +2696,8 @@ _pool_1 (SV *cb) CODE: { - struct coro *coro = SvSTATE (coro_current); HV *hv = (HV *)SvRV (coro_current); + struct coro *coro = SvSTATE_hv ((SV *)hv); AV *defav = GvAV (PL_defgv); SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); AV *invoke_av; @@ -2510,7 +2734,8 @@ _pool_2 (SV *cb) CODE: { - struct coro *coro = SvSTATE (coro_current); + HV *hv = (HV *)SvRV (coro_current); + struct coro *coro = SvSTATE_hv ((SV *)hv); sv_setsv (cb, &PL_sv_undef); @@ -2527,7 +2752,7 @@ } av_clear (GvAV (PL_defgv)); - hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, + hv_store (hv, "desc", sizeof ("desc") - 1, newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); coro->prio = 0; @@ -2538,142 +2763,13 @@ av_push (av_async_pool, newSVsv (coro_current)); } -#if 0 - -void -_generator_call (...) - PROTOTYPE: @ - PPCODE: - fprintf (stderr, "call %p\n", CvXSUBANY(cv).any_ptr); - xxxx - abort (); - -SV * -gensub (SV *sub, ...) - PROTOTYPE: &;@ - CODE: -{ - struct coro *coro; - MAGIC *mg; - CV *xcv; - CV *ncv = (CV *)newSV_type (SVt_PVCV); - int i; - - CvGV (ncv) = CvGV (cv); - CvFILE (ncv) = CvFILE (cv); - - Newz (0, coro, 1, struct coro); - coro->args = newAV (); - coro->flags = CF_NEW; - - av_extend (coro->args, items - 1); - for (i = 1; i < items; i++) - av_push (coro->args, newSVsv (ST (i))); - - CvISXSUB_on (ncv); - CvXSUBANY (ncv).any_ptr = (void *)coro; - - xcv = GvCV (gv_fetchpv ("Coro::_generator_call", 0, SVt_PVCV)); - - CvXSUB (ncv) = CvXSUB (xcv); - CvANON_on (ncv); - - mg = sv_magicext ((SV *)ncv, 0, CORO_MAGIC_type_state, &coro_gensub_vtbl, (char *)coro, 0); - RETVAL = newRV_noinc ((SV *)ncv); -} - OUTPUT: - RETVAL - -#endif - - -MODULE = Coro::State PACKAGE = Coro::AIO - -void -_get_state (SV *self) - PROTOTYPE: $ - PPCODE: -{ - AV *defav = GvAV (PL_defgv); - AV *av = newAV (); - int i; - SV *data_sv = newSV (sizeof (struct io_state)); - struct io_state *data = (struct io_state *)SvPVX (data_sv); - SvCUR_set (data_sv, sizeof (struct io_state)); - SvPOK_only (data_sv); - - data->errorno = errno; - data->laststype = PL_laststype; - data->laststatval = PL_laststatval; - data->statcache = PL_statcache; - - av_extend (av, AvFILLp (defav) + 1 + 1); - - for (i = 0; i <= AvFILLp (defav); ++i) - av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i])); - - av_push (av, data_sv); - - XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); - - api_ready (aTHX_ self); -} - -void -_set_state (SV *state) - PROTOTYPE: $ - PPCODE: -{ - AV *av = (AV *)SvRV (state); - struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]); - int i; - - errno = data->errorno; - PL_laststype = data->laststype; - PL_laststatval = data->laststatval; - PL_statcache = data->statcache; - - EXTEND (SP, AvFILLp (av)); - for (i = 0; i < AvFILLp (av); ++i) - PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i]))); -} - - -MODULE = Coro::State PACKAGE = Coro::AnyEvent - -BOOT: - sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE); - -void -_schedule (...) - CODE: -{ - static int incede; - - api_cede_notself (aTHX); - - ++incede; - while (coro_nready >= incede && api_cede (aTHX)) - ; - - sv_setsv (sv_activity, &PL_sv_undef); - if (coro_nready >= incede) - { - PUSHMARK (SP); - PUTBACK; - call_pv ("Coro::AnyEvent::_activity", G_DISCARD | G_EVAL); - SPAGAIN; - } - - --incede; -} - MODULE = Coro::State PACKAGE = PerlIO::cede BOOT: PerlIO_define_layer (aTHX_ &PerlIO_cede); + MODULE = Coro::State PACKAGE = Coro::Semaphore SV * @@ -2700,37 +2796,12 @@ ALIAS: adjust = 1 CODE: -{ - AV *av = (AV *)SvRV (self); - SV *count_sv = AvARRAY (av)[0]; - IV count = SvIVX (count_sv); - - count += ix ? adjust : 1; - SvIVX (count_sv) = count; - - /* now wake up as many waiters as possible */ - while (count > 0 && AvFILLp (av) >= count) - { - SV *cb; - - /* swap first two elements so we can shift a waiter */ - AvARRAY (av)[0] = AvARRAY (av)[1]; - AvARRAY (av)[1] = count_sv; - cb = av_shift (av); - - if (SvOBJECT (cb)) - api_ready (aTHX_ cb); - else - croak ("callbacks not yet supported"); - - SvREFCNT_dec (cb); - } -} + coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); void down (SV *self) CODE: - api_execute_slf (aTHX_ cv, slf_init_semaphore_down, &ST (0), 1); + CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); void try (SV *self) @@ -2767,3 +2838,49 @@ } } + +MODULE = Coro::State PACKAGE = Coro::AnyEvent + +BOOT: + sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE); + +void +_schedule (...) + CODE: +{ + static int incede; + + api_cede_notself (aTHX); + + ++incede; + while (coro_nready >= incede && api_cede (aTHX)) + ; + + sv_setsv (sv_activity, &PL_sv_undef); + if (coro_nready >= incede) + { + PUSHMARK (SP); + PUTBACK; + call_pv ("Coro::AnyEvent::_activity", G_DISCARD | G_EVAL); + SPAGAIN; + } + + --incede; +} + + +MODULE = Coro::State PACKAGE = Coro::AIO + +void +_register (char *target, char *proto, SV *req) + CODE: +{ + HV *st; + GV *gvp; + CV *req_cv = sv_2cv (req, &st, &gvp, 0); + /* newXSproto doesn't return the CV on 5.8 */ + CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); + sv_setpv ((SV *)slf_cv, proto); + sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); +} +