… | |
… | |
21 | |
21 | |
22 | #ifndef SVs_PADSTALE |
22 | #ifndef SVs_PADSTALE |
23 | # define SVs_PADSTALE 0 |
23 | # define SVs_PADSTALE 0 |
24 | #endif |
24 | #endif |
25 | |
25 | |
26 | #ifdef WIN32 |
26 | #if defined(_WIN32) |
|
|
27 | # undef HAS_GETTIMEOFDAY |
27 | # undef setjmp |
28 | # undef setjmp |
28 | # undef longjmp |
29 | # undef longjmp |
29 | # undef _exit |
30 | # undef _exit |
30 | # define setjmp _setjmp /* deep magic */ |
31 | # define setjmp _setjmp /* deep magic */ |
31 | #else |
32 | #else |
… | |
… | |
125 | static void (*u2time)(pTHX_ UV ret[2]); |
126 | static void (*u2time)(pTHX_ UV ret[2]); |
126 | |
127 | |
127 | /* we hijack an hopefully unused CV flag for our purposes */ |
128 | /* we hijack an hopefully unused CV flag for our purposes */ |
128 | #define CVf_SLF 0x4000 |
129 | #define CVf_SLF 0x4000 |
129 | static OP *pp_slf (pTHX); |
130 | static OP *pp_slf (pTHX); |
|
|
131 | static void slf_destroy (pTHX_ struct coro *coro); |
130 | |
132 | |
131 | static U32 cctx_gen; |
133 | static U32 cctx_gen; |
132 | static size_t cctx_stacksize = CORO_STACKSIZE; |
134 | static size_t cctx_stacksize = CORO_STACKSIZE; |
133 | static struct CoroAPI coroapi; |
135 | static struct CoroAPI coroapi; |
134 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
136 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
… | |
… | |
195 | int valgrind_id; |
197 | int valgrind_id; |
196 | #endif |
198 | #endif |
197 | unsigned char flags; |
199 | unsigned char flags; |
198 | } coro_cctx; |
200 | } coro_cctx; |
199 | |
201 | |
200 | coro_cctx *cctx_current; /* the currently running cctx */ |
202 | static coro_cctx *cctx_current; /* the currently running cctx */ |
201 | |
203 | |
202 | /*****************************************************************************/ |
204 | /*****************************************************************************/ |
203 | |
205 | |
204 | enum { |
206 | enum { |
205 | CF_RUNNING = 0x0001, /* coroutine is running */ |
207 | CF_RUNNING = 0x0001, /* coroutine is running */ |
206 | CF_READY = 0x0002, /* coroutine is ready */ |
208 | CF_READY = 0x0002, /* coroutine is ready */ |
207 | CF_NEW = 0x0004, /* has never been switched to */ |
209 | CF_NEW = 0x0004, /* has never been switched to */ |
208 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
210 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
209 | CF_SUSPENDED = 0x0010, /* coroutine can't be scheduled */ |
211 | CF_SUSPENDED = 0x0010, /* coroutine can't be scheduled */ |
|
|
212 | CF_NOCANCEL = 0x0020, /* cannot cancel, set slf_frame.data to 1 (hackish) */ |
210 | }; |
213 | }; |
211 | |
214 | |
212 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
215 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
213 | typedef struct |
216 | typedef struct |
214 | { |
217 | { |
… | |
… | |
240 | CV *startcv; /* the CV to execute */ |
243 | CV *startcv; /* the CV to execute */ |
241 | AV *args; /* data associated with this coroutine (initial args) */ |
244 | AV *args; /* data associated with this coroutine (initial args) */ |
242 | int refcnt; /* coroutines are refcounted, yes */ |
245 | int refcnt; /* coroutines are refcounted, yes */ |
243 | int flags; /* CF_ flags */ |
246 | int flags; /* CF_ flags */ |
244 | HV *hv; /* the perl hash associated with this coro, if any */ |
247 | HV *hv; /* the perl hash associated with this coro, if any */ |
245 | void (*on_destroy)(pTHX_ struct coro *coro); |
|
|
246 | |
248 | |
247 | /* statistics */ |
249 | /* statistics */ |
248 | int usecount; /* number of transfers to this coro */ |
250 | int usecount; /* number of transfers to this coro */ |
249 | |
251 | |
250 | /* coro process data */ |
252 | /* coro process data */ |
… | |
… | |
274 | typedef struct coro *Coro__State; |
276 | typedef struct coro *Coro__State; |
275 | typedef struct coro *Coro__State_or_hashref; |
277 | typedef struct coro *Coro__State_or_hashref; |
276 | |
278 | |
277 | /* the following variables are effectively part of the perl context */ |
279 | /* the following variables are effectively part of the perl context */ |
278 | /* and get copied between struct coro and these variables */ |
280 | /* and get copied between struct coro and these variables */ |
279 | /* the mainr easonw e don't support windows process emulation */ |
281 | /* the main reason we don't support windows process emulation */ |
280 | static struct CoroSLF slf_frame; /* the current slf frame */ |
282 | static struct CoroSLF slf_frame; /* the current slf frame */ |
281 | |
283 | |
282 | /** Coro ********************************************************************/ |
284 | /** Coro ********************************************************************/ |
283 | |
285 | |
284 | #define CORO_PRIO_MAX 3 |
286 | #define CORO_PRIO_MAX 3 |
… | |
… | |
290 | |
292 | |
291 | /* for Coro.pm */ |
293 | /* for Coro.pm */ |
292 | static SV *coro_current; |
294 | static SV *coro_current; |
293 | static SV *coro_readyhook; |
295 | static SV *coro_readyhook; |
294 | static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ |
296 | static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ |
295 | static CV *cv_coro_run, *cv_coro_terminate; |
297 | static CV *cv_coro_run; |
296 | static struct coro *coro_first; |
298 | static struct coro *coro_first; |
297 | #define coro_nready coroapi.nready |
299 | #define coro_nready coroapi.nready |
298 | |
300 | |
299 | /** Coro::Select ************************************************************/ |
301 | /** Coro::Select ************************************************************/ |
300 | |
302 | |
… | |
… | |
313 | /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */ |
315 | /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */ |
314 | PL_op->op_flags |= OPf_STACKED; |
316 | PL_op->op_flags |= OPf_STACKED; |
315 | PL_op->op_private = 0; |
317 | PL_op->op_private = 0; |
316 | return PL_ppaddr [OP_ENTERSUB](aTHX); |
318 | return PL_ppaddr [OP_ENTERSUB](aTHX); |
317 | } |
319 | } |
|
|
320 | |
|
|
321 | /** time stuff **************************************************************/ |
|
|
322 | |
|
|
323 | #ifdef HAS_GETTIMEOFDAY |
|
|
324 | |
|
|
325 | static void |
|
|
326 | coro_u2time (pTHX_ UV ret[2]) |
|
|
327 | { |
|
|
328 | struct timeval tv; |
|
|
329 | gettimeofday (&tv, 0); |
|
|
330 | |
|
|
331 | ret [0] = tv.tv_sec; |
|
|
332 | ret [1] = tv.tv_usec; |
|
|
333 | } |
|
|
334 | |
|
|
335 | static double |
|
|
336 | coro_nvtime () |
|
|
337 | { |
|
|
338 | struct timeval tv; |
|
|
339 | gettimeofday (&tv, 0); |
|
|
340 | |
|
|
341 | return tv.tv_sec + tv.tv_usec * 1e-6; |
|
|
342 | } |
|
|
343 | |
|
|
344 | static void |
|
|
345 | time_init (pTHX) |
|
|
346 | { |
|
|
347 | nvtime = coro_nvtime; |
|
|
348 | u2time = coro_u2time; |
|
|
349 | } |
|
|
350 | |
|
|
351 | #else |
|
|
352 | |
|
|
353 | static void |
|
|
354 | time_init (pTHX) |
|
|
355 | { |
|
|
356 | SV **svp; |
|
|
357 | |
|
|
358 | require_pv ("Time/HiRes.pm"); |
|
|
359 | |
|
|
360 | svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
|
|
361 | |
|
|
362 | if (!svp) croak ("Time::HiRes is required, but missing. Caught"); |
|
|
363 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer. Caught"); |
|
|
364 | |
|
|
365 | nvtime = INT2PTR (double (*)(), SvIV (*svp)); |
|
|
366 | |
|
|
367 | svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0); |
|
|
368 | u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp)); |
|
|
369 | } |
|
|
370 | |
|
|
371 | #endif |
318 | |
372 | |
319 | /** lowlevel stuff **********************************************************/ |
373 | /** lowlevel stuff **********************************************************/ |
320 | |
374 | |
321 | static SV * |
375 | static SV * |
322 | coro_get_sv (pTHX_ const char *name, int create) |
376 | coro_get_sv (pTHX_ const char *name, int create) |
… | |
… | |
586 | SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep); |
640 | SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep); |
587 | SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep); |
641 | SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep); |
588 | |
642 | |
589 | #if PERL_VERSION_ATLEAST (5,10,0) |
643 | #if PERL_VERSION_ATLEAST (5,10,0) |
590 | /* perl 5.10 complicates this _quite_ a bit, but it also is |
644 | /* perl 5.10 complicates this _quite_ a bit, but it also is |
591 | * is much faster, so no quarrels here. alternatively, we could |
645 | * much faster, so no quarrels here. alternatively, we could |
592 | * sv_upgrade to avoid this. |
646 | * sv_upgrade to avoid this. |
593 | */ |
647 | */ |
594 | { |
648 | { |
595 | /* swap sv_u */ |
649 | /* swap sv_u */ |
596 | tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u; |
650 | tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u; |
… | |
… | |
1013 | PL_comppad_name_fill = 0; |
1067 | PL_comppad_name_fill = 0; |
1014 | PL_comppad_name_floor = 0; |
1068 | PL_comppad_name_floor = 0; |
1015 | PL_curpm = 0; |
1069 | PL_curpm = 0; |
1016 | PL_curpad = 0; |
1070 | PL_curpad = 0; |
1017 | PL_localizing = 0; |
1071 | PL_localizing = 0; |
1018 | PL_dirty = 0; |
|
|
1019 | PL_restartop = 0; |
1072 | PL_restartop = 0; |
1020 | #if PERL_VERSION_ATLEAST (5,10,0) |
1073 | #if PERL_VERSION_ATLEAST (5,10,0) |
1021 | PL_parser = 0; |
1074 | PL_parser = 0; |
1022 | #endif |
1075 | #endif |
1023 | PL_hints = 0; |
1076 | PL_hints = 0; |
… | |
… | |
1101 | destroy_perl (pTHX_ struct coro *coro) |
1154 | destroy_perl (pTHX_ struct coro *coro) |
1102 | { |
1155 | { |
1103 | SV *svf [9]; |
1156 | SV *svf [9]; |
1104 | |
1157 | |
1105 | { |
1158 | { |
|
|
1159 | SV *old_current = SvRV (coro_current); |
1106 | struct coro *current = SvSTATE_current; |
1160 | struct coro *current = SvSTATE (old_current); |
1107 | |
1161 | |
1108 | assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack)); |
1162 | assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack)); |
1109 | |
1163 | |
1110 | save_perl (aTHX_ current); |
1164 | save_perl (aTHX_ current); |
|
|
1165 | |
|
|
1166 | /* this will cause transfer_check to croak on block*/ |
|
|
1167 | SvRV_set (coro_current, (SV *)coro->hv); |
|
|
1168 | |
1111 | load_perl (aTHX_ coro); |
1169 | load_perl (aTHX_ coro); |
1112 | |
1170 | |
1113 | coro_unwind_stacks (aTHX); |
1171 | coro_unwind_stacks (aTHX); |
1114 | coro_destruct_stacks (aTHX); |
1172 | coro_destruct_stacks (aTHX); |
1115 | |
1173 | |
… | |
… | |
1125 | svf [5] = GvSV (irsgv); |
1183 | svf [5] = GvSV (irsgv); |
1126 | svf [6] = (SV *)GvHV (PL_hintgv); |
1184 | svf [6] = (SV *)GvHV (PL_hintgv); |
1127 | svf [7] = PL_diehook; |
1185 | svf [7] = PL_diehook; |
1128 | svf [8] = PL_warnhook; |
1186 | svf [8] = PL_warnhook; |
1129 | assert (9 == sizeof (svf) / sizeof (*svf)); |
1187 | assert (9 == sizeof (svf) / sizeof (*svf)); |
|
|
1188 | |
|
|
1189 | SvRV_set (coro_current, old_current); |
1130 | |
1190 | |
1131 | load_perl (aTHX_ current); |
1191 | load_perl (aTHX_ current); |
1132 | } |
1192 | } |
1133 | |
1193 | |
1134 | { |
1194 | { |
… | |
… | |
1444 | cctx_destroy (coro_cctx *cctx) |
1504 | cctx_destroy (coro_cctx *cctx) |
1445 | { |
1505 | { |
1446 | if (!cctx) |
1506 | if (!cctx) |
1447 | return; |
1507 | return; |
1448 | |
1508 | |
1449 | assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary? |
1509 | assert (("FATAL: tried to destroy current cctx", cctx != cctx_current)); |
1450 | |
1510 | |
1451 | --cctx_count; |
1511 | --cctx_count; |
1452 | coro_destroy (&cctx->cctx); |
1512 | coro_destroy (&cctx->cctx); |
1453 | |
1513 | |
1454 | /* coro_transfer creates new, empty cctx's */ |
1514 | /* coro_transfer creates new, empty cctx's */ |
… | |
… | |
1613 | #define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx)) |
1673 | #define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx)) |
1614 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
1674 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
1615 | |
1675 | |
1616 | /** high level stuff ********************************************************/ |
1676 | /** high level stuff ********************************************************/ |
1617 | |
1677 | |
1618 | static int |
1678 | static void |
1619 | coro_state_destroy (pTHX_ struct coro *coro) |
1679 | coro_state_destroy (pTHX_ struct coro *coro) |
1620 | { |
1680 | { |
1621 | if (coro->flags & CF_DESTROYED) |
1681 | if (coro->flags & CF_DESTROYED) |
1622 | return 0; |
1682 | return; |
1623 | |
1683 | |
1624 | if (coro->on_destroy && !PL_dirty) |
|
|
1625 | coro->on_destroy (aTHX_ coro); |
1684 | slf_destroy (aTHX_ coro); |
1626 | |
1685 | |
1627 | coro->flags |= CF_DESTROYED; |
1686 | coro->flags |= CF_DESTROYED; |
1628 | |
1687 | |
1629 | if (coro->flags & CF_READY) |
1688 | if (coro->flags & CF_READY) |
1630 | { |
1689 | { |
… | |
… | |
1648 | cctx_destroy (coro->cctx); |
1707 | cctx_destroy (coro->cctx); |
1649 | SvREFCNT_dec (coro->startcv); |
1708 | SvREFCNT_dec (coro->startcv); |
1650 | SvREFCNT_dec (coro->args); |
1709 | SvREFCNT_dec (coro->args); |
1651 | SvREFCNT_dec (coro->swap_sv); |
1710 | SvREFCNT_dec (coro->swap_sv); |
1652 | SvREFCNT_dec (CORO_THROW); |
1711 | SvREFCNT_dec (CORO_THROW); |
1653 | |
|
|
1654 | return 1; |
|
|
1655 | } |
1712 | } |
1656 | |
1713 | |
1657 | static int |
1714 | static int |
1658 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
1715 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
1659 | { |
1716 | { |
… | |
… | |
1956 | |
2013 | |
1957 | static void |
2014 | static void |
1958 | coro_call_on_destroy (pTHX_ struct coro *coro) |
2015 | coro_call_on_destroy (pTHX_ struct coro *coro) |
1959 | { |
2016 | { |
1960 | SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0); |
2017 | SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0); |
1961 | SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0); |
|
|
1962 | |
2018 | |
1963 | if (on_destroyp) |
2019 | if (on_destroyp) |
1964 | { |
2020 | { |
1965 | AV *on_destroy = (AV *)SvRV (*on_destroyp); |
2021 | SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0); |
|
|
2022 | AV *on_destroy = sv_2mortal (SvREFCNT_inc ((AV *)SvRV (*on_destroyp))); |
|
|
2023 | AV *status = statusp ? sv_2mortal (SvREFCNT_inc ((AV *)SvRV (*statusp))) : 0; |
1966 | |
2024 | |
1967 | while (AvFILLp (on_destroy) >= 0) |
2025 | while (AvFILLp (on_destroy) >= 0) |
1968 | { |
2026 | { |
1969 | dSP; /* don't disturb outer sp */ |
2027 | dSP; /* don't disturb outer sp */ |
1970 | SV *cb = av_pop (on_destroy); |
2028 | SV *cb = av_pop (on_destroy); |
… | |
… | |
1972 | PUSHMARK (SP); |
2030 | PUSHMARK (SP); |
1973 | |
2031 | |
1974 | if (statusp) |
2032 | if (statusp) |
1975 | { |
2033 | { |
1976 | int i; |
2034 | int i; |
1977 | AV *status = (AV *)SvRV (*statusp); |
|
|
1978 | EXTEND (SP, AvFILLp (status) + 1); |
2035 | EXTEND (SP, AvFILLp (status) + 1); |
1979 | |
2036 | |
1980 | for (i = 0; i <= AvFILLp (status); ++i) |
2037 | for (i = 0; i <= AvFILLp (status); ++i) |
1981 | PUSHs (AvARRAY (status)[i]); |
2038 | PUSHs (AvARRAY (status)[i]); |
1982 | } |
2039 | } |
… | |
… | |
1986 | } |
2043 | } |
1987 | } |
2044 | } |
1988 | } |
2045 | } |
1989 | |
2046 | |
1990 | static void |
2047 | static void |
1991 | slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2048 | coro_set_status (HV *coro_hv, SV **arg, int items) |
1992 | { |
2049 | { |
1993 | int i; |
|
|
1994 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1995 | AV *av = newAV (); |
2050 | AV *av = newAV (); |
1996 | |
2051 | |
1997 | /* items are actually not so common, so optimise for this case */ |
2052 | /* items are actually not so common, so optimise for this case */ |
1998 | if (items) |
2053 | if (items) |
1999 | { |
2054 | { |
|
|
2055 | int i; |
|
|
2056 | |
2000 | av_extend (av, items - 1); |
2057 | av_extend (av, items - 1); |
2001 | |
2058 | |
2002 | for (i = 0; i < items; ++i) |
2059 | for (i = 0; i < items; ++i) |
2003 | av_push (av, SvREFCNT_inc_NN (arg [i])); |
2060 | av_push (av, SvREFCNT_inc_NN (arg [i])); |
2004 | } |
2061 | } |
2005 | |
2062 | |
2006 | hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); |
2063 | hv_store (coro_hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); |
|
|
2064 | } |
2007 | |
2065 | |
|
|
2066 | static void |
|
|
2067 | slf_init_terminate_cancel_common (pTHX_ struct CoroSLF *frame, HV *coro_hv) |
|
|
2068 | { |
2008 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
2069 | av_push (av_destroy, (SV *)newRV_inc ((SV *)coro_hv)); /* RVinc for perl */ |
2009 | api_ready (aTHX_ sv_manager); |
2070 | api_ready (aTHX_ sv_manager); |
2010 | |
2071 | |
2011 | frame->prepare = prepare_schedule; |
2072 | frame->prepare = prepare_schedule; |
2012 | frame->check = slf_check_repeat; |
2073 | frame->check = slf_check_repeat; |
2013 | |
2074 | |
2014 | /* as a minor optimisation, we could unwind all stacks here */ |
2075 | /* as a minor optimisation, we could unwind all stacks here */ |
2015 | /* but that puts extra pressure on pp_slf, and is not worth much */ |
2076 | /* but that puts extra pressure on pp_slf, and is not worth much */ |
2016 | /*coro_unwind_stacks (aTHX);*/ |
2077 | /*coro_unwind_stacks (aTHX);*/ |
|
|
2078 | } |
|
|
2079 | |
|
|
2080 | static void |
|
|
2081 | slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2082 | { |
|
|
2083 | HV *coro_hv = (HV *)SvRV (coro_current); |
|
|
2084 | |
|
|
2085 | coro_set_status (coro_hv, arg, items); |
|
|
2086 | slf_init_terminate_cancel_common (frame, coro_hv); |
|
|
2087 | } |
|
|
2088 | |
|
|
2089 | static void |
|
|
2090 | slf_init_cancel (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2091 | { |
|
|
2092 | HV *coro_hv; |
|
|
2093 | struct coro *coro; |
|
|
2094 | |
|
|
2095 | if (items <= 0) |
|
|
2096 | croak ("Coro::cancel called without coro object,"); |
|
|
2097 | |
|
|
2098 | coro = SvSTATE (arg [0]); |
|
|
2099 | coro_hv = coro->hv; |
|
|
2100 | |
|
|
2101 | coro_set_status (coro_hv, arg + 1, items - 1); |
|
|
2102 | |
|
|
2103 | if (expect_false (coro->flags & CF_NOCANCEL)) |
|
|
2104 | { |
|
|
2105 | /* coro currently busy cancelling something, so just notify it */ |
|
|
2106 | coro->slf_frame.data = (void *)coro; |
|
|
2107 | |
|
|
2108 | frame->prepare = prepare_nop; |
|
|
2109 | frame->check = slf_check_nop; |
|
|
2110 | } |
|
|
2111 | else if (coro_hv == (HV *)SvRV (coro_current)) |
|
|
2112 | { |
|
|
2113 | /* cancelling the current coro is allowed, and equals terminate */ |
|
|
2114 | slf_init_terminate_cancel_common (frame, coro_hv); |
|
|
2115 | } |
|
|
2116 | else |
|
|
2117 | { |
|
|
2118 | struct coro *self = SvSTATE_current; |
|
|
2119 | |
|
|
2120 | /* otherwise we cancel directly, purely for speed reasons |
|
|
2121 | * unfortunately, this requires some magic trickery, as |
|
|
2122 | * somebody else could cancel us, so we have to fight the cancellation. |
|
|
2123 | * this is ugly, and hopefully fully worth the extra speed. |
|
|
2124 | * besides, I can't get the slow-but-safe version working... |
|
|
2125 | */ |
|
|
2126 | slf_frame.data = 0; |
|
|
2127 | self->flags |= CF_NOCANCEL; |
|
|
2128 | |
|
|
2129 | coro_state_destroy (aTHX_ coro); |
|
|
2130 | coro_call_on_destroy (aTHX_ coro); |
|
|
2131 | |
|
|
2132 | self->flags &= ~CF_NOCANCEL; |
|
|
2133 | |
|
|
2134 | if (slf_frame.data) |
|
|
2135 | { |
|
|
2136 | /* while we were busy we have been cancelled, so terminate */ |
|
|
2137 | slf_init_terminate_cancel_common (frame, self->hv); |
|
|
2138 | } |
|
|
2139 | else |
|
|
2140 | { |
|
|
2141 | frame->prepare = prepare_nop; |
|
|
2142 | frame->check = slf_check_nop; |
|
|
2143 | } |
|
|
2144 | } |
|
|
2145 | } |
|
|
2146 | |
|
|
2147 | static int |
|
|
2148 | slf_check_safe_cancel (pTHX_ struct CoroSLF *frame) |
|
|
2149 | { |
|
|
2150 | frame->prepare = 0; |
|
|
2151 | coro_unwind_stacks (); |
|
|
2152 | |
|
|
2153 | slf_init_terminate_cancel_common (aTHX_ frame, (HV *)SvRV (coro_current)); |
|
|
2154 | |
|
|
2155 | return 1; |
|
|
2156 | } |
|
|
2157 | |
|
|
2158 | static int |
|
|
2159 | safe_cancel (pTHX_ struct coro *coro, SV **arg, int items) |
|
|
2160 | { |
|
|
2161 | if (coro->cctx) |
|
|
2162 | croak ("coro inside C callback, unable to cancel at this time, caught"); |
|
|
2163 | |
|
|
2164 | if (coro->flags & CF_NEW) |
|
|
2165 | { |
|
|
2166 | coro_set_status (coro->hv, arg, items); |
|
|
2167 | coro_state_destroy (aTHX_ coro); |
|
|
2168 | coro_call_on_destroy (aTHX_ coro); |
|
|
2169 | } |
|
|
2170 | else |
|
|
2171 | { |
|
|
2172 | if (!coro->slf_frame.prepare) |
|
|
2173 | croak ("coro outside an SLF function, unable to cancel at this time, caught"); |
|
|
2174 | |
|
|
2175 | slf_destroy (coro); |
|
|
2176 | |
|
|
2177 | coro_set_status (coro->hv, arg, items); |
|
|
2178 | coro->slf_frame.prepare = prepare_nop; |
|
|
2179 | coro->slf_frame.check = slf_check_safe_cancel; |
|
|
2180 | |
|
|
2181 | api_ready (aTHX_ coro->hv); |
|
|
2182 | } |
|
|
2183 | |
|
|
2184 | return 1; |
2017 | } |
2185 | } |
2018 | |
2186 | |
2019 | /*****************************************************************************/ |
2187 | /*****************************************************************************/ |
2020 | /* async pool handler */ |
2188 | /* async pool handler */ |
2021 | |
2189 | |
… | |
… | |
2061 | coro->saved_deffh = 0; |
2229 | coro->saved_deffh = 0; |
2062 | |
2230 | |
2063 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
2231 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
2064 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
2232 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
2065 | { |
2233 | { |
2066 | coro->invoke_cb = SvREFCNT_inc_NN ((SV *)cv_coro_terminate); |
2234 | slf_init_terminate_cancel_common (aTHX_ frame, hv); |
2067 | coro->invoke_av = newAV (); |
2235 | return; |
2068 | |
|
|
2069 | frame->prepare = prepare_nop; |
|
|
2070 | } |
2236 | } |
2071 | else |
2237 | else |
2072 | { |
2238 | { |
2073 | av_clear (GvAV (PL_defgv)); |
2239 | av_clear (GvAV (PL_defgv)); |
2074 | hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0); |
2240 | hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0); |
… | |
… | |
2297 | static void |
2463 | static void |
2298 | slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2464 | slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2299 | { |
2465 | { |
2300 | frame->prepare = prepare_cede_notself; |
2466 | frame->prepare = prepare_cede_notself; |
2301 | frame->check = slf_check_nop; |
2467 | frame->check = slf_check_nop; |
|
|
2468 | } |
|
|
2469 | |
|
|
2470 | /* "undo"/cancel a running slf call - used when cancelling a coro, mainly */ |
|
|
2471 | static void |
|
|
2472 | slf_destroy (pTHX_ struct coro *coro) |
|
|
2473 | { |
|
|
2474 | /* this callback is reserved for slf functions needing to do cleanup */ |
|
|
2475 | if (coro->slf_frame.destroy && coro->slf_frame.prepare && !PL_dirty) |
|
|
2476 | coro->slf_frame.destroy (aTHX_ coro); |
|
|
2477 | |
|
|
2478 | /* |
|
|
2479 | * The on_destroy above most likely is from an SLF call. |
|
|
2480 | * Since by definition the SLF call will not finish when we destroy |
|
|
2481 | * the coro, we will have to force-finish it here, otherwise |
|
|
2482 | * cleanup functions cannot call SLF functions. |
|
|
2483 | */ |
|
|
2484 | coro->slf_frame.prepare = 0; |
2302 | } |
2485 | } |
2303 | |
2486 | |
2304 | /* |
2487 | /* |
2305 | * these not obviously related functions are all rolled into one |
2488 | * these not obviously related functions are all rolled into one |
2306 | * function to increase chances that they all will call transfer with the same |
2489 | * function to increase chances that they all will call transfer with the same |
… | |
… | |
2632 | SvREFCNT_dec (cb); |
2815 | SvREFCNT_dec (cb); |
2633 | } |
2816 | } |
2634 | } |
2817 | } |
2635 | |
2818 | |
2636 | static void |
2819 | static void |
2637 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
2820 | coro_semaphore_destroy (pTHX_ struct coro *coro) |
2638 | { |
2821 | { |
2639 | /* call $sem->adjust (0) to possibly wake up some other waiters */ |
2822 | /* call $sem->adjust (0) to possibly wake up some other waiters */ |
2640 | coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); |
2823 | coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); |
2641 | } |
2824 | } |
2642 | |
2825 | |
… | |
… | |
2649 | /* if we are about to throw, don't actually acquire the lock, just throw */ |
2832 | /* if we are about to throw, don't actually acquire the lock, just throw */ |
2650 | if (CORO_THROW) |
2833 | if (CORO_THROW) |
2651 | return 0; |
2834 | return 0; |
2652 | else if (SvIVX (count_sv) > 0) |
2835 | else if (SvIVX (count_sv) > 0) |
2653 | { |
2836 | { |
2654 | SvSTATE_current->on_destroy = 0; |
2837 | frame->destroy = 0; |
2655 | |
2838 | |
2656 | if (acquire) |
2839 | if (acquire) |
2657 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2840 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2658 | else |
2841 | else |
2659 | coro_semaphore_adjust (aTHX_ av, 0); |
2842 | coro_semaphore_adjust (aTHX_ av, 0); |
… | |
… | |
2702 | { |
2885 | { |
2703 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2886 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2704 | |
2887 | |
2705 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
2888 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
2706 | frame->prepare = prepare_schedule; |
2889 | frame->prepare = prepare_schedule; |
2707 | |
|
|
2708 | /* to avoid race conditions when a woken-up coro gets terminated */ |
2890 | /* to avoid race conditions when a woken-up coro gets terminated */ |
2709 | /* we arrange for a temporary on_destroy that calls adjust (0) */ |
2891 | /* we arrange for a temporary on_destroy that calls adjust (0) */ |
2710 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
2892 | frame->destroy = coro_semaphore_destroy; |
2711 | } |
2893 | } |
2712 | } |
2894 | } |
2713 | |
2895 | |
2714 | static void |
2896 | static void |
2715 | slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2897 | slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
… | |
… | |
3110 | coroapi.prepare_nop = prepare_nop; |
3292 | coroapi.prepare_nop = prepare_nop; |
3111 | coroapi.prepare_schedule = prepare_schedule; |
3293 | coroapi.prepare_schedule = prepare_schedule; |
3112 | coroapi.prepare_cede = prepare_cede; |
3294 | coroapi.prepare_cede = prepare_cede; |
3113 | coroapi.prepare_cede_notself = prepare_cede_notself; |
3295 | coroapi.prepare_cede_notself = prepare_cede_notself; |
3114 | |
3296 | |
3115 | { |
3297 | time_init (aTHX); |
3116 | SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
|
|
3117 | |
|
|
3118 | if (!svp) croak ("Time::HiRes is required"); |
|
|
3119 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); |
|
|
3120 | |
|
|
3121 | nvtime = INT2PTR (double (*)(), SvIV (*svp)); |
|
|
3122 | |
|
|
3123 | svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0); |
|
|
3124 | u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp)); |
|
|
3125 | } |
|
|
3126 | |
3298 | |
3127 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
3299 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
3128 | } |
3300 | } |
3129 | |
3301 | |
3130 | SV * |
3302 | SV * |
… | |
… | |
3139 | void |
3311 | void |
3140 | transfer (...) |
3312 | transfer (...) |
3141 | PROTOTYPE: $$ |
3313 | PROTOTYPE: $$ |
3142 | CODE: |
3314 | CODE: |
3143 | CORO_EXECUTE_SLF_XS (slf_init_transfer); |
3315 | CORO_EXECUTE_SLF_XS (slf_init_transfer); |
3144 | |
|
|
3145 | bool |
|
|
3146 | _destroy (SV *coro_sv) |
|
|
3147 | CODE: |
|
|
3148 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
|
|
3149 | OUTPUT: |
|
|
3150 | RETVAL |
|
|
3151 | |
3316 | |
3152 | void |
3317 | void |
3153 | _exit (int code) |
3318 | _exit (int code) |
3154 | PROTOTYPE: $ |
3319 | PROTOTYPE: $ |
3155 | CODE: |
3320 | CODE: |
… | |
… | |
3231 | CODE: |
3396 | CODE: |
3232 | { |
3397 | { |
3233 | if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) |
3398 | if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) |
3234 | { |
3399 | { |
3235 | struct coro *current = SvSTATE_current; |
3400 | struct coro *current = SvSTATE_current; |
|
|
3401 | struct CoroSLF slf_save; |
3236 | |
3402 | |
3237 | if (current != coro) |
3403 | if (current != coro) |
3238 | { |
3404 | { |
3239 | PUTBACK; |
3405 | PUTBACK; |
3240 | save_perl (aTHX_ current); |
3406 | save_perl (aTHX_ current); |
3241 | load_perl (aTHX_ coro); |
3407 | load_perl (aTHX_ coro); |
|
|
3408 | /* the coro is most likely in an active SLF call. |
|
|
3409 | * while not strictly required (the code we execute is |
|
|
3410 | * not allowed to call any SLF functions), it's cleaner |
|
|
3411 | * to reinitialise the slf_frame and restore it later. |
|
|
3412 | * This might one day allow us to actually do SLF calls |
|
|
3413 | * from code executed here. |
|
|
3414 | */ |
|
|
3415 | slf_save = slf_frame; |
|
|
3416 | slf_frame.prepare = 0; |
3242 | SPAGAIN; |
3417 | SPAGAIN; |
3243 | } |
3418 | } |
3244 | |
3419 | |
3245 | PUSHSTACK; |
3420 | PUSHSTACK; |
3246 | |
3421 | |
… | |
… | |
3256 | SPAGAIN; |
3431 | SPAGAIN; |
3257 | |
3432 | |
3258 | if (current != coro) |
3433 | if (current != coro) |
3259 | { |
3434 | { |
3260 | PUTBACK; |
3435 | PUTBACK; |
|
|
3436 | slf_frame = slf_save; |
3261 | save_perl (aTHX_ coro); |
3437 | save_perl (aTHX_ coro); |
3262 | load_perl (aTHX_ current); |
3438 | load_perl (aTHX_ current); |
3263 | SPAGAIN; |
3439 | SPAGAIN; |
3264 | } |
3440 | } |
3265 | } |
3441 | } |
… | |
… | |
3351 | |
3527 | |
3352 | void |
3528 | void |
3353 | cancel (Coro::State self) |
3529 | cancel (Coro::State self) |
3354 | CODE: |
3530 | CODE: |
3355 | coro_state_destroy (aTHX_ self); |
3531 | coro_state_destroy (aTHX_ self); |
3356 | coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */ |
|
|
3357 | |
|
|
3358 | |
3532 | |
3359 | SV * |
3533 | SV * |
3360 | enable_times (int enabled = enable_times) |
3534 | enable_times (int enabled = enable_times) |
3361 | CODE: |
3535 | CODE: |
3362 | { |
3536 | { |
… | |
… | |
3418 | BOOT: |
3592 | BOOT: |
3419 | { |
3593 | { |
3420 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3594 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3421 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3595 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3422 | cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); |
3596 | cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); |
3423 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
|
|
3424 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
3597 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
3425 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
3598 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
3426 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
3599 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
3427 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
3600 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
3428 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
3601 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
… | |
… | |
3467 | api_ready (aTHX_ RETVAL); |
3640 | api_ready (aTHX_ RETVAL); |
3468 | OUTPUT: |
3641 | OUTPUT: |
3469 | RETVAL |
3642 | RETVAL |
3470 | |
3643 | |
3471 | void |
3644 | void |
|
|
3645 | _destroy (Coro::State coro) |
|
|
3646 | CODE: |
|
|
3647 | /* used by the manager thread */ |
|
|
3648 | coro_state_destroy (aTHX_ coro); |
|
|
3649 | coro_call_on_destroy (aTHX_ coro); |
|
|
3650 | |
|
|
3651 | void |
3472 | terminate (...) |
3652 | terminate (...) |
3473 | CODE: |
3653 | CODE: |
3474 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
3654 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
|
|
3655 | |
|
|
3656 | void |
|
|
3657 | cancel (...) |
|
|
3658 | CODE: |
|
|
3659 | CORO_EXECUTE_SLF_XS (slf_init_cancel); |
|
|
3660 | |
|
|
3661 | int |
|
|
3662 | safe_cancel (Coro::State self, ...) |
|
|
3663 | C_ARGS: aTHX_ self, &ST (1), items - 1 |
3475 | |
3664 | |
3476 | void |
3665 | void |
3477 | schedule (...) |
3666 | schedule (...) |
3478 | CODE: |
3667 | CODE: |
3479 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
3668 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
… | |
… | |
3876 | { |
4065 | { |
3877 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
4066 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
3878 | coro_old_pp_sselect = 0; |
4067 | coro_old_pp_sselect = 0; |
3879 | } |
4068 | } |
3880 | |
4069 | |
3881 | |
|
|