… | |
… | |
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 | { |
… | |
… | |
220 | #define VAR(name,type) type name; |
223 | #define VAR(name,type) type name; |
221 | # include "state.h" |
224 | # include "state.h" |
222 | #undef VAR |
225 | #undef VAR |
223 | } perl_slots; |
226 | } perl_slots; |
224 | |
227 | |
|
|
228 | // how many context stack entries do we need for perl_slots |
225 | #define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) |
229 | #define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) |
226 | |
230 | |
227 | /* this is a structure representing a perl-level coroutine */ |
231 | /* this is a structure representing a perl-level coroutine */ |
228 | struct coro { |
232 | struct coro { |
229 | /* the C coroutine allocated to this perl coroutine, if any */ |
233 | /* the C coroutine allocated to this perl coroutine, if any */ |
… | |
… | |
240 | CV *startcv; /* the CV to execute */ |
244 | CV *startcv; /* the CV to execute */ |
241 | AV *args; /* data associated with this coroutine (initial args) */ |
245 | AV *args; /* data associated with this coroutine (initial args) */ |
242 | int refcnt; /* coroutines are refcounted, yes */ |
246 | int refcnt; /* coroutines are refcounted, yes */ |
243 | int flags; /* CF_ flags */ |
247 | int flags; /* CF_ flags */ |
244 | HV *hv; /* the perl hash associated with this coro, if any */ |
248 | HV *hv; /* the perl hash associated with this coro, if any */ |
245 | void (*on_destroy)(pTHX_ struct coro *coro); |
|
|
246 | |
249 | |
247 | /* statistics */ |
250 | /* statistics */ |
248 | int usecount; /* number of transfers to this coro */ |
251 | int usecount; /* number of transfers to this coro */ |
249 | |
252 | |
250 | /* coro process data */ |
253 | /* coro process data */ |
… | |
… | |
274 | typedef struct coro *Coro__State; |
277 | typedef struct coro *Coro__State; |
275 | typedef struct coro *Coro__State_or_hashref; |
278 | typedef struct coro *Coro__State_or_hashref; |
276 | |
279 | |
277 | /* the following variables are effectively part of the perl context */ |
280 | /* the following variables are effectively part of the perl context */ |
278 | /* and get copied between struct coro and these variables */ |
281 | /* and get copied between struct coro and these variables */ |
279 | /* the mainr easonw e don't support windows process emulation */ |
282 | /* the main reason we don't support windows process emulation */ |
280 | static struct CoroSLF slf_frame; /* the current slf frame */ |
283 | static struct CoroSLF slf_frame; /* the current slf frame */ |
281 | |
284 | |
282 | /** Coro ********************************************************************/ |
285 | /** Coro ********************************************************************/ |
283 | |
286 | |
284 | #define CORO_PRIO_MAX 3 |
287 | #define CORO_PRIO_MAX 3 |
… | |
… | |
290 | |
293 | |
291 | /* for Coro.pm */ |
294 | /* for Coro.pm */ |
292 | static SV *coro_current; |
295 | static SV *coro_current; |
293 | static SV *coro_readyhook; |
296 | static SV *coro_readyhook; |
294 | static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ |
297 | static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ |
295 | static CV *cv_coro_run, *cv_coro_terminate; |
298 | static CV *cv_coro_run; |
296 | static struct coro *coro_first; |
299 | static struct coro *coro_first; |
297 | #define coro_nready coroapi.nready |
300 | #define coro_nready coroapi.nready |
298 | |
301 | |
299 | /** Coro::Select ************************************************************/ |
302 | /** Coro::Select ************************************************************/ |
300 | |
303 | |
… | |
… | |
313 | /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */ |
316 | /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */ |
314 | PL_op->op_flags |= OPf_STACKED; |
317 | PL_op->op_flags |= OPf_STACKED; |
315 | PL_op->op_private = 0; |
318 | PL_op->op_private = 0; |
316 | return PL_ppaddr [OP_ENTERSUB](aTHX); |
319 | return PL_ppaddr [OP_ENTERSUB](aTHX); |
317 | } |
320 | } |
|
|
321 | |
|
|
322 | /** time stuff **************************************************************/ |
|
|
323 | |
|
|
324 | #ifdef HAS_GETTIMEOFDAY |
|
|
325 | |
|
|
326 | static void |
|
|
327 | coro_u2time (pTHX_ UV ret[2]) |
|
|
328 | { |
|
|
329 | struct timeval tv; |
|
|
330 | gettimeofday (&tv, 0); |
|
|
331 | |
|
|
332 | ret [0] = tv.tv_sec; |
|
|
333 | ret [1] = tv.tv_usec; |
|
|
334 | } |
|
|
335 | |
|
|
336 | static double |
|
|
337 | coro_nvtime () |
|
|
338 | { |
|
|
339 | struct timeval tv; |
|
|
340 | gettimeofday (&tv, 0); |
|
|
341 | |
|
|
342 | return tv.tv_sec + tv.tv_usec * 1e-6; |
|
|
343 | } |
|
|
344 | |
|
|
345 | static void |
|
|
346 | time_init (pTHX) |
|
|
347 | { |
|
|
348 | nvtime = coro_nvtime; |
|
|
349 | u2time = coro_u2time; |
|
|
350 | } |
|
|
351 | |
|
|
352 | #else |
|
|
353 | |
|
|
354 | static void |
|
|
355 | time_init (pTHX) |
|
|
356 | { |
|
|
357 | SV **svp; |
|
|
358 | |
|
|
359 | require_pv ("Time/HiRes.pm"); |
|
|
360 | |
|
|
361 | svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
|
|
362 | |
|
|
363 | if (!svp) croak ("Time::HiRes is required, but missing. Caught"); |
|
|
364 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer. Caught"); |
|
|
365 | |
|
|
366 | nvtime = INT2PTR (double (*)(), SvIV (*svp)); |
|
|
367 | |
|
|
368 | svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0); |
|
|
369 | u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp)); |
|
|
370 | } |
|
|
371 | |
|
|
372 | #endif |
318 | |
373 | |
319 | /** lowlevel stuff **********************************************************/ |
374 | /** lowlevel stuff **********************************************************/ |
320 | |
375 | |
321 | static SV * |
376 | static SV * |
322 | coro_get_sv (pTHX_ const char *name, int create) |
377 | coro_get_sv (pTHX_ const char *name, int create) |
… | |
… | |
586 | SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep); |
641 | SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep); |
587 | SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep); |
642 | SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep); |
588 | |
643 | |
589 | #if PERL_VERSION_ATLEAST (5,10,0) |
644 | #if PERL_VERSION_ATLEAST (5,10,0) |
590 | /* perl 5.10 complicates this _quite_ a bit, but it also is |
645 | /* perl 5.10 complicates this _quite_ a bit, but it also is |
591 | * is much faster, so no quarrels here. alternatively, we could |
646 | * much faster, so no quarrels here. alternatively, we could |
592 | * sv_upgrade to avoid this. |
647 | * sv_upgrade to avoid this. |
593 | */ |
648 | */ |
594 | { |
649 | { |
595 | /* swap sv_u */ |
650 | /* swap sv_u */ |
596 | tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u; |
651 | tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u; |
… | |
… | |
744 | |
799 | |
745 | PUTBACK; |
800 | PUTBACK; |
746 | } |
801 | } |
747 | |
802 | |
748 | /* allocate some space on the context stack for our purposes */ |
803 | /* allocate some space on the context stack for our purposes */ |
749 | /* we manually unroll here, as usually 2 slots is enough */ |
804 | if (expect_false (cxstack_ix + SLOT_COUNT >= cxstack_max)) |
750 | if (SLOT_COUNT >= 1) CXINC; |
|
|
751 | if (SLOT_COUNT >= 2) CXINC; |
|
|
752 | if (SLOT_COUNT >= 3) CXINC; |
|
|
753 | { |
805 | { |
754 | unsigned int i; |
806 | unsigned int i; |
|
|
807 | |
755 | for (i = 3; i < SLOT_COUNT; ++i) |
808 | for (i = 0; i < SLOT_COUNT; ++i) |
756 | CXINC; |
809 | CXINC; |
757 | } |
810 | |
758 | cxstack_ix -= SLOT_COUNT; /* undo allocation */ |
811 | cxstack_ix -= SLOT_COUNT; /* undo allocation */ |
|
|
812 | } |
759 | |
813 | |
760 | c->mainstack = PL_mainstack; |
814 | c->mainstack = PL_mainstack; |
761 | |
815 | |
762 | { |
816 | { |
763 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
817 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
… | |
… | |
784 | # define coro_init_stacks(thx) init_stacks () |
838 | # define coro_init_stacks(thx) init_stacks () |
785 | #else |
839 | #else |
786 | static void |
840 | static void |
787 | coro_init_stacks (pTHX) |
841 | coro_init_stacks (pTHX) |
788 | { |
842 | { |
789 | PL_curstackinfo = new_stackinfo(32, 8); |
843 | PL_curstackinfo = new_stackinfo(32, 4 + SLOT_COUNT); /* 3 is minimum due to perl rounding down in scope.c:GROW() */ |
790 | PL_curstackinfo->si_type = PERLSI_MAIN; |
844 | PL_curstackinfo->si_type = PERLSI_MAIN; |
791 | PL_curstack = PL_curstackinfo->si_stack; |
845 | PL_curstack = PL_curstackinfo->si_stack; |
792 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
846 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
793 | |
847 | |
794 | PL_stack_base = AvARRAY(PL_curstack); |
848 | PL_stack_base = AvARRAY(PL_curstack); |
… | |
… | |
1100 | destroy_perl (pTHX_ struct coro *coro) |
1154 | destroy_perl (pTHX_ struct coro *coro) |
1101 | { |
1155 | { |
1102 | SV *svf [9]; |
1156 | SV *svf [9]; |
1103 | |
1157 | |
1104 | { |
1158 | { |
|
|
1159 | SV *old_current = SvRV (coro_current); |
1105 | struct coro *current = SvSTATE_current; |
1160 | struct coro *current = SvSTATE (old_current); |
1106 | |
1161 | |
1107 | 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)); |
1108 | |
1163 | |
1109 | 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 | |
1110 | load_perl (aTHX_ coro); |
1169 | load_perl (aTHX_ coro); |
1111 | |
1170 | |
1112 | coro_unwind_stacks (aTHX); |
1171 | coro_unwind_stacks (aTHX); |
1113 | coro_destruct_stacks (aTHX); |
1172 | coro_destruct_stacks (aTHX); |
1114 | |
1173 | |
… | |
… | |
1124 | svf [5] = GvSV (irsgv); |
1183 | svf [5] = GvSV (irsgv); |
1125 | svf [6] = (SV *)GvHV (PL_hintgv); |
1184 | svf [6] = (SV *)GvHV (PL_hintgv); |
1126 | svf [7] = PL_diehook; |
1185 | svf [7] = PL_diehook; |
1127 | svf [8] = PL_warnhook; |
1186 | svf [8] = PL_warnhook; |
1128 | assert (9 == sizeof (svf) / sizeof (*svf)); |
1187 | assert (9 == sizeof (svf) / sizeof (*svf)); |
|
|
1188 | |
|
|
1189 | SvRV_set (coro_current, old_current); |
1129 | |
1190 | |
1130 | load_perl (aTHX_ current); |
1191 | load_perl (aTHX_ current); |
1131 | } |
1192 | } |
1132 | |
1193 | |
1133 | { |
1194 | { |
… | |
… | |
1443 | cctx_destroy (coro_cctx *cctx) |
1504 | cctx_destroy (coro_cctx *cctx) |
1444 | { |
1505 | { |
1445 | if (!cctx) |
1506 | if (!cctx) |
1446 | return; |
1507 | return; |
1447 | |
1508 | |
1448 | assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary? |
1509 | assert (("FATAL: tried to destroy current cctx", cctx != cctx_current)); |
1449 | |
1510 | |
1450 | --cctx_count; |
1511 | --cctx_count; |
1451 | coro_destroy (&cctx->cctx); |
1512 | coro_destroy (&cctx->cctx); |
1452 | |
1513 | |
1453 | /* coro_transfer creates new, empty cctx's */ |
1514 | /* coro_transfer creates new, empty cctx's */ |
… | |
… | |
1612 | #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)) |
1613 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
1674 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
1614 | |
1675 | |
1615 | /** high level stuff ********************************************************/ |
1676 | /** high level stuff ********************************************************/ |
1616 | |
1677 | |
1617 | static int |
1678 | static void |
1618 | coro_state_destroy (pTHX_ struct coro *coro) |
1679 | coro_state_destroy (pTHX_ struct coro *coro) |
1619 | { |
1680 | { |
1620 | if (coro->flags & CF_DESTROYED) |
1681 | if (coro->flags & CF_DESTROYED) |
1621 | return 0; |
1682 | return; |
1622 | |
1683 | |
1623 | if (coro->on_destroy && !PL_dirty) |
|
|
1624 | coro->on_destroy (aTHX_ coro); |
1684 | slf_destroy (aTHX_ coro); |
1625 | |
1685 | |
1626 | coro->flags |= CF_DESTROYED; |
1686 | coro->flags |= CF_DESTROYED; |
1627 | |
1687 | |
1628 | if (coro->flags & CF_READY) |
1688 | if (coro->flags & CF_READY) |
1629 | { |
1689 | { |
… | |
… | |
1647 | cctx_destroy (coro->cctx); |
1707 | cctx_destroy (coro->cctx); |
1648 | SvREFCNT_dec (coro->startcv); |
1708 | SvREFCNT_dec (coro->startcv); |
1649 | SvREFCNT_dec (coro->args); |
1709 | SvREFCNT_dec (coro->args); |
1650 | SvREFCNT_dec (coro->swap_sv); |
1710 | SvREFCNT_dec (coro->swap_sv); |
1651 | SvREFCNT_dec (CORO_THROW); |
1711 | SvREFCNT_dec (CORO_THROW); |
1652 | |
|
|
1653 | return 1; |
|
|
1654 | } |
1712 | } |
1655 | |
1713 | |
1656 | static int |
1714 | static int |
1657 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
1715 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
1658 | { |
1716 | { |
… | |
… | |
1955 | |
2013 | |
1956 | static void |
2014 | static void |
1957 | coro_call_on_destroy (pTHX_ struct coro *coro) |
2015 | coro_call_on_destroy (pTHX_ struct coro *coro) |
1958 | { |
2016 | { |
1959 | 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); |
1960 | SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0); |
|
|
1961 | |
2018 | |
1962 | if (on_destroyp) |
2019 | if (on_destroyp) |
1963 | { |
2020 | { |
1964 | 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; |
1965 | |
2024 | |
1966 | while (AvFILLp (on_destroy) >= 0) |
2025 | while (AvFILLp (on_destroy) >= 0) |
1967 | { |
2026 | { |
1968 | dSP; /* don't disturb outer sp */ |
2027 | dSP; /* don't disturb outer sp */ |
1969 | SV *cb = av_pop (on_destroy); |
2028 | SV *cb = av_pop (on_destroy); |
… | |
… | |
1971 | PUSHMARK (SP); |
2030 | PUSHMARK (SP); |
1972 | |
2031 | |
1973 | if (statusp) |
2032 | if (statusp) |
1974 | { |
2033 | { |
1975 | int i; |
2034 | int i; |
1976 | AV *status = (AV *)SvRV (*statusp); |
|
|
1977 | EXTEND (SP, AvFILLp (status) + 1); |
2035 | EXTEND (SP, AvFILLp (status) + 1); |
1978 | |
2036 | |
1979 | for (i = 0; i <= AvFILLp (status); ++i) |
2037 | for (i = 0; i <= AvFILLp (status); ++i) |
1980 | PUSHs (AvARRAY (status)[i]); |
2038 | PUSHs (AvARRAY (status)[i]); |
1981 | } |
2039 | } |
… | |
… | |
1985 | } |
2043 | } |
1986 | } |
2044 | } |
1987 | } |
2045 | } |
1988 | |
2046 | |
1989 | static void |
2047 | static void |
1990 | slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2048 | coro_set_status (pTHX_ HV *coro_hv, SV **arg, int items) |
1991 | { |
2049 | { |
1992 | int i; |
|
|
1993 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1994 | AV *av = newAV (); |
2050 | AV *av = newAV (); |
1995 | |
2051 | |
1996 | /* items are actually not so common, so optimise for this case */ |
2052 | /* items are actually not so common, so optimise for this case */ |
1997 | if (items) |
2053 | if (items) |
1998 | { |
2054 | { |
|
|
2055 | int i; |
|
|
2056 | |
1999 | av_extend (av, items - 1); |
2057 | av_extend (av, items - 1); |
2000 | |
2058 | |
2001 | for (i = 0; i < items; ++i) |
2059 | for (i = 0; i < items; ++i) |
2002 | av_push (av, SvREFCNT_inc_NN (arg [i])); |
2060 | av_push (av, SvREFCNT_inc_NN (arg [i])); |
2003 | } |
2061 | } |
2004 | |
2062 | |
2005 | 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 | } |
2006 | |
2065 | |
|
|
2066 | static void |
|
|
2067 | slf_init_terminate_cancel_common (pTHX_ struct CoroSLF *frame, HV *coro_hv) |
|
|
2068 | { |
2007 | 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 */ |
2008 | api_ready (aTHX_ sv_manager); |
2070 | api_ready (aTHX_ sv_manager); |
2009 | |
2071 | |
2010 | frame->prepare = prepare_schedule; |
2072 | frame->prepare = prepare_schedule; |
2011 | frame->check = slf_check_repeat; |
2073 | frame->check = slf_check_repeat; |
2012 | |
2074 | |
2013 | /* as a minor optimisation, we could unwind all stacks here */ |
2075 | /* as a minor optimisation, we could unwind all stacks here */ |
2014 | /* 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 */ |
2015 | /*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 (aTHX_ coro_hv, arg, items); |
|
|
2086 | slf_init_terminate_cancel_common (aTHX_ 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 (aTHX_ 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 (aTHX_ 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 (aTHX_ 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 (aTHX); |
|
|
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 (aTHX_ 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 (aTHX_ coro); |
|
|
2176 | |
|
|
2177 | coro_set_status (aTHX_ 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; |
2016 | } |
2185 | } |
2017 | |
2186 | |
2018 | /*****************************************************************************/ |
2187 | /*****************************************************************************/ |
2019 | /* async pool handler */ |
2188 | /* async pool handler */ |
2020 | |
2189 | |
… | |
… | |
2060 | coro->saved_deffh = 0; |
2229 | coro->saved_deffh = 0; |
2061 | |
2230 | |
2062 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
2231 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
2063 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
2232 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
2064 | { |
2233 | { |
2065 | coro->invoke_cb = SvREFCNT_inc_NN ((SV *)cv_coro_terminate); |
2234 | slf_init_terminate_cancel_common (aTHX_ frame, hv); |
2066 | coro->invoke_av = newAV (); |
2235 | return; |
2067 | |
|
|
2068 | frame->prepare = prepare_nop; |
|
|
2069 | } |
2236 | } |
2070 | else |
2237 | else |
2071 | { |
2238 | { |
2072 | av_clear (GvAV (PL_defgv)); |
2239 | av_clear (GvAV (PL_defgv)); |
2073 | 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); |
… | |
… | |
2296 | static void |
2463 | static void |
2297 | 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) |
2298 | { |
2465 | { |
2299 | frame->prepare = prepare_cede_notself; |
2466 | frame->prepare = prepare_cede_notself; |
2300 | 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; |
2301 | } |
2485 | } |
2302 | |
2486 | |
2303 | /* |
2487 | /* |
2304 | * these not obviously related functions are all rolled into one |
2488 | * these not obviously related functions are all rolled into one |
2305 | * 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 |
… | |
… | |
2631 | SvREFCNT_dec (cb); |
2815 | SvREFCNT_dec (cb); |
2632 | } |
2816 | } |
2633 | } |
2817 | } |
2634 | |
2818 | |
2635 | static void |
2819 | static void |
2636 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
2820 | coro_semaphore_destroy (pTHX_ struct coro *coro) |
2637 | { |
2821 | { |
2638 | /* call $sem->adjust (0) to possibly wake up some other waiters */ |
2822 | /* call $sem->adjust (0) to possibly wake up some other waiters */ |
2639 | coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); |
2823 | coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); |
2640 | } |
2824 | } |
2641 | |
2825 | |
… | |
… | |
2648 | /* 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 */ |
2649 | if (CORO_THROW) |
2833 | if (CORO_THROW) |
2650 | return 0; |
2834 | return 0; |
2651 | else if (SvIVX (count_sv) > 0) |
2835 | else if (SvIVX (count_sv) > 0) |
2652 | { |
2836 | { |
2653 | SvSTATE_current->on_destroy = 0; |
2837 | frame->destroy = 0; |
2654 | |
2838 | |
2655 | if (acquire) |
2839 | if (acquire) |
2656 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2840 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2657 | else |
2841 | else |
2658 | coro_semaphore_adjust (aTHX_ av, 0); |
2842 | coro_semaphore_adjust (aTHX_ av, 0); |
… | |
… | |
2701 | { |
2885 | { |
2702 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2886 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2703 | |
2887 | |
2704 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
2888 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
2705 | frame->prepare = prepare_schedule; |
2889 | frame->prepare = prepare_schedule; |
2706 | |
|
|
2707 | /* to avoid race conditions when a woken-up coro gets terminated */ |
2890 | /* to avoid race conditions when a woken-up coro gets terminated */ |
2708 | /* we arrange for a temporary on_destroy that calls adjust (0) */ |
2891 | /* we arrange for a temporary on_destroy that calls adjust (0) */ |
2709 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
2892 | frame->destroy = coro_semaphore_destroy; |
2710 | } |
2893 | } |
2711 | } |
2894 | } |
2712 | |
2895 | |
2713 | static void |
2896 | static void |
2714 | 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) |
… | |
… | |
3109 | coroapi.prepare_nop = prepare_nop; |
3292 | coroapi.prepare_nop = prepare_nop; |
3110 | coroapi.prepare_schedule = prepare_schedule; |
3293 | coroapi.prepare_schedule = prepare_schedule; |
3111 | coroapi.prepare_cede = prepare_cede; |
3294 | coroapi.prepare_cede = prepare_cede; |
3112 | coroapi.prepare_cede_notself = prepare_cede_notself; |
3295 | coroapi.prepare_cede_notself = prepare_cede_notself; |
3113 | |
3296 | |
3114 | { |
3297 | time_init (aTHX); |
3115 | SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
|
|
3116 | |
|
|
3117 | if (!svp) croak ("Time::HiRes is required"); |
|
|
3118 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); |
|
|
3119 | |
|
|
3120 | nvtime = INT2PTR (double (*)(), SvIV (*svp)); |
|
|
3121 | |
|
|
3122 | svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0); |
|
|
3123 | u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp)); |
|
|
3124 | } |
|
|
3125 | |
3298 | |
3126 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
3299 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
3127 | } |
3300 | } |
3128 | |
3301 | |
3129 | SV * |
3302 | SV * |
… | |
… | |
3138 | void |
3311 | void |
3139 | transfer (...) |
3312 | transfer (...) |
3140 | PROTOTYPE: $$ |
3313 | PROTOTYPE: $$ |
3141 | CODE: |
3314 | CODE: |
3142 | CORO_EXECUTE_SLF_XS (slf_init_transfer); |
3315 | CORO_EXECUTE_SLF_XS (slf_init_transfer); |
3143 | |
|
|
3144 | bool |
|
|
3145 | _destroy (SV *coro_sv) |
|
|
3146 | CODE: |
|
|
3147 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
|
|
3148 | OUTPUT: |
|
|
3149 | RETVAL |
|
|
3150 | |
3316 | |
3151 | void |
3317 | void |
3152 | _exit (int code) |
3318 | _exit (int code) |
3153 | PROTOTYPE: $ |
3319 | PROTOTYPE: $ |
3154 | CODE: |
3320 | CODE: |
… | |
… | |
3230 | CODE: |
3396 | CODE: |
3231 | { |
3397 | { |
3232 | if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) |
3398 | if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) |
3233 | { |
3399 | { |
3234 | struct coro *current = SvSTATE_current; |
3400 | struct coro *current = SvSTATE_current; |
|
|
3401 | struct CoroSLF slf_save; |
3235 | |
3402 | |
3236 | if (current != coro) |
3403 | if (current != coro) |
3237 | { |
3404 | { |
3238 | PUTBACK; |
3405 | PUTBACK; |
3239 | save_perl (aTHX_ current); |
3406 | save_perl (aTHX_ current); |
3240 | 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; |
3241 | SPAGAIN; |
3417 | SPAGAIN; |
3242 | } |
3418 | } |
3243 | |
3419 | |
3244 | PUSHSTACK; |
3420 | PUSHSTACK; |
3245 | |
3421 | |
… | |
… | |
3255 | SPAGAIN; |
3431 | SPAGAIN; |
3256 | |
3432 | |
3257 | if (current != coro) |
3433 | if (current != coro) |
3258 | { |
3434 | { |
3259 | PUTBACK; |
3435 | PUTBACK; |
|
|
3436 | slf_frame = slf_save; |
3260 | save_perl (aTHX_ coro); |
3437 | save_perl (aTHX_ coro); |
3261 | load_perl (aTHX_ current); |
3438 | load_perl (aTHX_ current); |
3262 | SPAGAIN; |
3439 | SPAGAIN; |
3263 | } |
3440 | } |
3264 | } |
3441 | } |
… | |
… | |
3350 | |
3527 | |
3351 | void |
3528 | void |
3352 | cancel (Coro::State self) |
3529 | cancel (Coro::State self) |
3353 | CODE: |
3530 | CODE: |
3354 | coro_state_destroy (aTHX_ self); |
3531 | coro_state_destroy (aTHX_ self); |
3355 | coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */ |
|
|
3356 | |
|
|
3357 | |
3532 | |
3358 | SV * |
3533 | SV * |
3359 | enable_times (int enabled = enable_times) |
3534 | enable_times (int enabled = enable_times) |
3360 | CODE: |
3535 | CODE: |
3361 | { |
3536 | { |
… | |
… | |
3417 | BOOT: |
3592 | BOOT: |
3418 | { |
3593 | { |
3419 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3594 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3420 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3595 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3421 | cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); |
3596 | cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); |
3422 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
|
|
3423 | 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); |
3424 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
3598 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
3425 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
3599 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
3426 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
3600 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
3427 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
3601 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
… | |
… | |
3466 | api_ready (aTHX_ RETVAL); |
3640 | api_ready (aTHX_ RETVAL); |
3467 | OUTPUT: |
3641 | OUTPUT: |
3468 | RETVAL |
3642 | RETVAL |
3469 | |
3643 | |
3470 | 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 |
3471 | terminate (...) |
3652 | terminate (...) |
3472 | CODE: |
3653 | CODE: |
3473 | 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 |
3474 | |
3664 | |
3475 | void |
3665 | void |
3476 | schedule (...) |
3666 | schedule (...) |
3477 | CODE: |
3667 | CODE: |
3478 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
3668 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |