|
|
1 | /* this works around a bug in mingw32 providing a non-working setjmp */ |
|
|
2 | #define USE_NO_MINGW_SETJMP_TWO_ARGS |
|
|
3 | |
|
|
4 | #define NDEBUG 1 |
|
|
5 | |
1 | #include "libcoro/coro.c" |
6 | #include "libcoro/coro.c" |
2 | |
7 | |
3 | #define PERL_NO_GET_CONTEXT |
8 | #define PERL_NO_GET_CONTEXT |
4 | #define PERL_EXT |
9 | #define PERL_EXT |
5 | |
10 | |
… | |
… | |
12 | |
17 | |
13 | #include <stdio.h> |
18 | #include <stdio.h> |
14 | #include <errno.h> |
19 | #include <errno.h> |
15 | #include <assert.h> |
20 | #include <assert.h> |
16 | |
21 | |
17 | #ifdef WIN32 |
22 | #ifndef SVs_PADSTALE |
|
|
23 | # define SVs_PADSTALE 0 |
|
|
24 | #endif |
|
|
25 | |
|
|
26 | #if defined(_WIN32) |
|
|
27 | # undef HAS_GETTIMEOFDAY |
18 | # undef setjmp |
28 | # undef setjmp |
19 | # undef longjmp |
29 | # undef longjmp |
20 | # undef _exit |
30 | # undef _exit |
21 | # define setjmp _setjmp /* deep magic */ |
31 | # define setjmp _setjmp /* deep magic */ |
22 | #else |
32 | #else |
… | |
… | |
142 | static SV *sv_pool_size; |
152 | static SV *sv_pool_size; |
143 | static SV *sv_async_pool_idle; /* description string */ |
153 | static SV *sv_async_pool_idle; /* description string */ |
144 | static AV *av_async_pool; /* idle pool */ |
154 | static AV *av_async_pool; /* idle pool */ |
145 | static SV *sv_Coro; /* class string */ |
155 | static SV *sv_Coro; /* class string */ |
146 | static CV *cv_pool_handler; |
156 | static CV *cv_pool_handler; |
147 | static CV *cv_coro_state_new; |
|
|
148 | |
157 | |
149 | /* Coro::AnyEvent */ |
158 | /* Coro::AnyEvent */ |
150 | static SV *sv_activity; |
159 | static SV *sv_activity; |
151 | |
160 | |
152 | /* enable processtime/realtime profiling */ |
161 | /* enable processtime/realtime profiling */ |
… | |
… | |
232 | CV *startcv; /* the CV to execute */ |
241 | CV *startcv; /* the CV to execute */ |
233 | AV *args; /* data associated with this coroutine (initial args) */ |
242 | AV *args; /* data associated with this coroutine (initial args) */ |
234 | int refcnt; /* coroutines are refcounted, yes */ |
243 | int refcnt; /* coroutines are refcounted, yes */ |
235 | int flags; /* CF_ flags */ |
244 | int flags; /* CF_ flags */ |
236 | HV *hv; /* the perl hash associated with this coro, if any */ |
245 | HV *hv; /* the perl hash associated with this coro, if any */ |
237 | void (*on_destroy)(pTHX_ struct coro *coro); |
246 | void (*on_destroy)(pTHX_ struct coro *coro); /* for temporary use by xs in critical sections */ |
238 | |
247 | |
239 | /* statistics */ |
248 | /* statistics */ |
240 | int usecount; /* number of transfers to this coro */ |
249 | int usecount; /* number of transfers to this coro */ |
241 | |
250 | |
242 | /* coro process data */ |
251 | /* coro process data */ |
… | |
… | |
250 | AV *invoke_av; |
259 | AV *invoke_av; |
251 | |
260 | |
252 | /* on_enter/on_leave */ |
261 | /* on_enter/on_leave */ |
253 | AV *on_enter; |
262 | AV *on_enter; |
254 | AV *on_leave; |
263 | AV *on_leave; |
|
|
264 | |
|
|
265 | /* swap_sv */ |
|
|
266 | AV *swap_sv; |
255 | |
267 | |
256 | /* times */ |
268 | /* times */ |
257 | coro_ts t_cpu, t_real; |
269 | coro_ts t_cpu, t_real; |
258 | |
270 | |
259 | /* linked list */ |
271 | /* linked list */ |
… | |
… | |
290 | static OP *(*coro_old_pp_sselect) (pTHX); |
302 | static OP *(*coro_old_pp_sselect) (pTHX); |
291 | static SV *coro_select_select; |
303 | static SV *coro_select_select; |
292 | |
304 | |
293 | /* horrible hack, but if it works... */ |
305 | /* horrible hack, but if it works... */ |
294 | static OP * |
306 | static OP * |
295 | coro_pp_sselect (aTHX) |
307 | coro_pp_sselect (pTHX) |
296 | { |
308 | { |
297 | dSP; |
309 | dSP; |
298 | PUSHMARK (SP - 4); /* fake argument list */ |
310 | PUSHMARK (SP - 4); /* fake argument list */ |
299 | XPUSHs (coro_select_select); |
311 | XPUSHs (coro_select_select); |
300 | PUTBACK; |
312 | PUTBACK; |
… | |
… | |
302 | /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */ |
314 | /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */ |
303 | PL_op->op_flags |= OPf_STACKED; |
315 | PL_op->op_flags |= OPf_STACKED; |
304 | PL_op->op_private = 0; |
316 | PL_op->op_private = 0; |
305 | return PL_ppaddr [OP_ENTERSUB](aTHX); |
317 | return PL_ppaddr [OP_ENTERSUB](aTHX); |
306 | } |
318 | } |
|
|
319 | |
|
|
320 | /** time stuff **************************************************************/ |
|
|
321 | |
|
|
322 | #ifdef HAS_GETTIMEOFDAY |
|
|
323 | |
|
|
324 | static void |
|
|
325 | coro_u2time (pTHX_ UV ret[2]) |
|
|
326 | { |
|
|
327 | struct timeval tv; |
|
|
328 | gettimeofday (&tv, 0); |
|
|
329 | |
|
|
330 | ret [0] = tv.tv_sec; |
|
|
331 | ret [1] = tv.tv_usec; |
|
|
332 | } |
|
|
333 | |
|
|
334 | static double |
|
|
335 | coro_nvtime () |
|
|
336 | { |
|
|
337 | struct timeval tv; |
|
|
338 | gettimeofday (&tv, 0); |
|
|
339 | |
|
|
340 | return tv.tv_sec + tv.tv_usec * 1e-6; |
|
|
341 | } |
|
|
342 | |
|
|
343 | static void |
|
|
344 | time_init (pTHX) |
|
|
345 | { |
|
|
346 | nvtime = coro_nvtime; |
|
|
347 | u2time = coro_u2time; |
|
|
348 | } |
|
|
349 | |
|
|
350 | #else |
|
|
351 | |
|
|
352 | static void |
|
|
353 | time_init (pTHX) |
|
|
354 | { |
|
|
355 | SV **svp; |
|
|
356 | |
|
|
357 | require_pv ("Time/HiRes.pm"); |
|
|
358 | |
|
|
359 | svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
|
|
360 | |
|
|
361 | if (!svp) croak ("Time::HiRes is required, but missing."); |
|
|
362 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); |
|
|
363 | |
|
|
364 | nvtime = INT2PTR (double (*)(), SvIV (*svp)); |
|
|
365 | |
|
|
366 | svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0); |
|
|
367 | u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp)); |
|
|
368 | } |
|
|
369 | |
|
|
370 | #endif |
307 | |
371 | |
308 | /** lowlevel stuff **********************************************************/ |
372 | /** lowlevel stuff **********************************************************/ |
309 | |
373 | |
310 | static SV * |
374 | static SV * |
311 | coro_get_sv (pTHX_ const char *name, int create) |
375 | coro_get_sv (pTHX_ const char *name, int create) |
… | |
… | |
551 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
615 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
552 | } |
616 | } |
553 | |
617 | |
554 | /** load & save, init *******************************************************/ |
618 | /** load & save, init *******************************************************/ |
555 | |
619 | |
|
|
620 | /* swap sv heads, at least logically */ |
|
|
621 | static void |
|
|
622 | swap_svs (pTHX_ Coro__State c) |
|
|
623 | { |
|
|
624 | int i; |
|
|
625 | |
|
|
626 | for (i = 0; i <= AvFILLp (c->swap_sv); ) |
|
|
627 | { |
|
|
628 | SV *a = AvARRAY (c->swap_sv)[i++]; |
|
|
629 | SV *b = AvARRAY (c->swap_sv)[i++]; |
|
|
630 | |
|
|
631 | const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */ |
|
|
632 | SV tmp; |
|
|
633 | |
|
|
634 | /* swap sv_any */ |
|
|
635 | SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp); |
|
|
636 | |
|
|
637 | /* swap sv_flags */ |
|
|
638 | SvFLAGS (&tmp) = SvFLAGS (a); |
|
|
639 | SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep); |
|
|
640 | SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep); |
|
|
641 | |
|
|
642 | #if PERL_VERSION_ATLEAST (5,10,0) |
|
|
643 | /* perl 5.10 complicates this _quite_ a bit, but it also is |
|
|
644 | * much faster, so no quarrels here. alternatively, we could |
|
|
645 | * sv_upgrade to avoid this. |
|
|
646 | */ |
|
|
647 | { |
|
|
648 | /* swap sv_u */ |
|
|
649 | tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u; |
|
|
650 | |
|
|
651 | /* if SvANY points to the head, we need to adjust the pointers, |
|
|
652 | * as the pointer for a still points to b, and maybe vice versa. |
|
|
653 | */ |
|
|
654 | #define svany_in_head(type) \ |
|
|
655 | (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type))) |
|
|
656 | |
|
|
657 | if (svany_in_head (SvTYPE (a))) |
|
|
658 | SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a); |
|
|
659 | |
|
|
660 | if (svany_in_head (SvTYPE (b))) |
|
|
661 | SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b); |
|
|
662 | } |
|
|
663 | #endif |
|
|
664 | } |
|
|
665 | } |
|
|
666 | |
|
|
667 | #define SWAP_SVS(coro) \ |
|
|
668 | if (expect_false ((coro)->swap_sv)) \ |
|
|
669 | swap_svs (aTHX_ (coro)) |
|
|
670 | |
556 | static void |
671 | static void |
557 | on_enterleave_call (pTHX_ SV *cb); |
672 | on_enterleave_call (pTHX_ SV *cb); |
558 | |
673 | |
559 | static void |
674 | static void |
560 | load_perl (pTHX_ Coro__State c) |
675 | load_perl (pTHX_ Coro__State c) |
… | |
… | |
606 | int i; |
721 | int i; |
607 | |
722 | |
608 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
723 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
609 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
724 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
610 | } |
725 | } |
|
|
726 | |
|
|
727 | SWAP_SVS (c); |
611 | } |
728 | } |
612 | |
729 | |
613 | static void |
730 | static void |
614 | save_perl (pTHX_ Coro__State c) |
731 | save_perl (pTHX_ Coro__State c) |
615 | { |
732 | { |
|
|
733 | SWAP_SVS (c); |
|
|
734 | |
616 | if (expect_false (c->on_leave)) |
735 | if (expect_false (c->on_leave)) |
617 | { |
736 | { |
618 | int i; |
737 | int i; |
619 | |
738 | |
620 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
739 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
… | |
… | |
927 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
1046 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
928 | { |
1047 | { |
929 | return 1; |
1048 | return 1; |
930 | } |
1049 | } |
931 | |
1050 | |
932 | static UNOP coro_setup_op; |
1051 | static UNOP init_perl_op; |
933 | |
1052 | |
934 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
1053 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
935 | coro_setup (pTHX_ struct coro *coro) |
1054 | init_perl (pTHX_ struct coro *coro) |
936 | { |
1055 | { |
937 | /* |
1056 | /* |
938 | * emulate part of the perl startup here. |
1057 | * emulate part of the perl startup here. |
939 | */ |
1058 | */ |
940 | coro_init_stacks (aTHX); |
1059 | coro_init_stacks (aTHX); |
… | |
… | |
947 | PL_comppad_name_fill = 0; |
1066 | PL_comppad_name_fill = 0; |
948 | PL_comppad_name_floor = 0; |
1067 | PL_comppad_name_floor = 0; |
949 | PL_curpm = 0; |
1068 | PL_curpm = 0; |
950 | PL_curpad = 0; |
1069 | PL_curpad = 0; |
951 | PL_localizing = 0; |
1070 | PL_localizing = 0; |
952 | PL_dirty = 0; |
|
|
953 | PL_restartop = 0; |
1071 | PL_restartop = 0; |
954 | #if PERL_VERSION_ATLEAST (5,10,0) |
1072 | #if PERL_VERSION_ATLEAST (5,10,0) |
955 | PL_parser = 0; |
1073 | PL_parser = 0; |
956 | #endif |
1074 | #endif |
957 | PL_hints = 0; |
1075 | PL_hints = 0; |
… | |
… | |
989 | */ |
1107 | */ |
990 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
1108 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
991 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
1109 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
992 | |
1110 | |
993 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
1111 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
994 | coro_setup_op.op_next = PL_op; |
1112 | init_perl_op.op_next = PL_op; |
995 | coro_setup_op.op_type = OP_ENTERSUB; |
1113 | init_perl_op.op_type = OP_ENTERSUB; |
996 | coro_setup_op.op_ppaddr = pp_slf; |
1114 | init_perl_op.op_ppaddr = pp_slf; |
997 | /* no flags etc. required, as an init function won't be called */ |
1115 | /* no flags etc. required, as an init function won't be called */ |
998 | |
1116 | |
999 | PL_op = (OP *)&coro_setup_op; |
1117 | PL_op = (OP *)&init_perl_op; |
1000 | |
1118 | |
1001 | /* copy throw, in case it was set before coro_setup */ |
1119 | /* copy throw, in case it was set before init_perl */ |
1002 | CORO_THROW = coro->except; |
1120 | CORO_THROW = coro->except; |
|
|
1121 | |
|
|
1122 | SWAP_SVS (coro); |
1003 | |
1123 | |
1004 | if (expect_false (enable_times)) |
1124 | if (expect_false (enable_times)) |
1005 | { |
1125 | { |
1006 | coro_times_update (); |
1126 | coro_times_update (); |
1007 | coro_times_sub (coro); |
1127 | coro_times_sub (coro); |
… | |
… | |
1028 | dounwind (-1); |
1148 | dounwind (-1); |
1029 | } |
1149 | } |
1030 | } |
1150 | } |
1031 | |
1151 | |
1032 | static void |
1152 | static void |
1033 | coro_destruct_perl (pTHX_ struct coro *coro) |
1153 | destroy_perl (pTHX_ struct coro *coro) |
1034 | { |
1154 | { |
1035 | SV *svf [9]; |
1155 | SV *svf [9]; |
1036 | |
1156 | |
1037 | { |
1157 | { |
1038 | struct coro *current = SvSTATE_current; |
1158 | struct coro *current = SvSTATE_current; |
… | |
… | |
1042 | save_perl (aTHX_ current); |
1162 | save_perl (aTHX_ current); |
1043 | load_perl (aTHX_ coro); |
1163 | load_perl (aTHX_ coro); |
1044 | |
1164 | |
1045 | coro_unwind_stacks (aTHX); |
1165 | coro_unwind_stacks (aTHX); |
1046 | coro_destruct_stacks (aTHX); |
1166 | coro_destruct_stacks (aTHX); |
|
|
1167 | |
|
|
1168 | /* restore swapped sv's */ |
|
|
1169 | SWAP_SVS (coro); |
1047 | |
1170 | |
1048 | // now save some sv's to be free'd later |
1171 | // now save some sv's to be free'd later |
1049 | svf [0] = GvSV (PL_defgv); |
1172 | svf [0] = GvSV (PL_defgv); |
1050 | svf [1] = (SV *)GvAV (PL_defgv); |
1173 | svf [1] = (SV *)GvAV (PL_defgv); |
1051 | svf [2] = GvSV (PL_errgv); |
1174 | svf [2] = GvSV (PL_errgv); |
… | |
… | |
1275 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1398 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1276 | PL_restartop = PL_op; |
1399 | PL_restartop = PL_op; |
1277 | perl_run (PL_curinterp); |
1400 | perl_run (PL_curinterp); |
1278 | /* |
1401 | /* |
1279 | * Unfortunately, there is no way to get at the return values of the |
1402 | * Unfortunately, there is no way to get at the return values of the |
1280 | * coro body here, as perl_run destroys these |
1403 | * coro body here, as perl_run destroys these. Likewise, we cannot catch |
|
|
1404 | * runtime errors here, as this is just a random interpreter, not a thread. |
1281 | */ |
1405 | */ |
1282 | |
1406 | |
1283 | /* |
1407 | /* |
1284 | * If perl-run returns we assume exit() was being called or the coro |
1408 | * If perl-run returns we assume exit() was being called or the coro |
1285 | * fell off the end, which seems to be the only valid (non-bug) |
1409 | * fell off the end, which seems to be the only valid (non-bug) |
… | |
… | |
1492 | if (expect_false (next->flags & CF_NEW)) |
1616 | if (expect_false (next->flags & CF_NEW)) |
1493 | { |
1617 | { |
1494 | /* need to start coroutine */ |
1618 | /* need to start coroutine */ |
1495 | next->flags &= ~CF_NEW; |
1619 | next->flags &= ~CF_NEW; |
1496 | /* setup coroutine call */ |
1620 | /* setup coroutine call */ |
1497 | coro_setup (aTHX_ next); |
1621 | init_perl (aTHX_ next); |
1498 | } |
1622 | } |
1499 | else |
1623 | else |
1500 | load_perl (aTHX_ next); |
1624 | load_perl (aTHX_ next); |
1501 | |
1625 | |
1502 | /* possibly untie and reuse the cctx */ |
1626 | /* possibly untie and reuse the cctx */ |
… | |
… | |
1565 | |
1689 | |
1566 | if (coro->mainstack |
1690 | if (coro->mainstack |
1567 | && coro->mainstack != main_mainstack |
1691 | && coro->mainstack != main_mainstack |
1568 | && coro->slot |
1692 | && coro->slot |
1569 | && !PL_dirty) |
1693 | && !PL_dirty) |
1570 | coro_destruct_perl (aTHX_ coro); |
1694 | destroy_perl (aTHX_ coro); |
|
|
1695 | |
|
|
1696 | if (coro->next) coro->next->prev = coro->prev; |
|
|
1697 | if (coro->prev) coro->prev->next = coro->next; |
|
|
1698 | if (coro == coro_first) coro_first = coro->next; |
1571 | |
1699 | |
1572 | cctx_destroy (coro->cctx); |
1700 | cctx_destroy (coro->cctx); |
1573 | SvREFCNT_dec (coro->startcv); |
1701 | SvREFCNT_dec (coro->startcv); |
1574 | SvREFCNT_dec (coro->args); |
1702 | SvREFCNT_dec (coro->args); |
|
|
1703 | SvREFCNT_dec (coro->swap_sv); |
1575 | SvREFCNT_dec (CORO_THROW); |
1704 | SvREFCNT_dec (CORO_THROW); |
1576 | |
|
|
1577 | if (coro->next) coro->next->prev = coro->prev; |
|
|
1578 | if (coro->prev) coro->prev->next = coro->next; |
|
|
1579 | if (coro == coro_first) coro_first = coro->next; |
|
|
1580 | |
1705 | |
1581 | return 1; |
1706 | return 1; |
1582 | } |
1707 | } |
1583 | |
1708 | |
1584 | static int |
1709 | static int |
… | |
… | |
1754 | { |
1879 | { |
1755 | /* nothing to schedule: call the idle handler */ |
1880 | /* nothing to schedule: call the idle handler */ |
1756 | if (SvROK (sv_idle) |
1881 | if (SvROK (sv_idle) |
1757 | && SvOBJECT (SvRV (sv_idle))) |
1882 | && SvOBJECT (SvRV (sv_idle))) |
1758 | { |
1883 | { |
|
|
1884 | if (SvRV (sv_idle) == SvRV (coro_current)) |
|
|
1885 | croak ("FATAL: $Coro::IDLE blocked itself - did you try to block inside an event loop callback? Caught"); |
|
|
1886 | |
1759 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
1887 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
1760 | api_ready (aTHX_ SvRV (sv_idle)); |
1888 | api_ready (aTHX_ SvRV (sv_idle)); |
1761 | --coro_nready; |
1889 | --coro_nready; |
1762 | } |
1890 | } |
1763 | else |
1891 | else |
1764 | { |
1892 | { |
|
|
1893 | /* TODO: deprecated, remove, cannot work reliably *//*D*/ |
1765 | dSP; |
1894 | dSP; |
1766 | |
1895 | |
1767 | ENTER; |
1896 | ENTER; |
1768 | SAVETMPS; |
1897 | SAVETMPS; |
1769 | |
1898 | |
… | |
… | |
1879 | |
2008 | |
1880 | static void |
2009 | static void |
1881 | coro_call_on_destroy (pTHX_ struct coro *coro) |
2010 | coro_call_on_destroy (pTHX_ struct coro *coro) |
1882 | { |
2011 | { |
1883 | SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0); |
2012 | SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0); |
1884 | SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0); |
|
|
1885 | |
2013 | |
1886 | if (on_destroyp) |
2014 | if (on_destroyp) |
1887 | { |
2015 | { |
1888 | AV *on_destroy = (AV *)SvRV (*on_destroyp); |
2016 | SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0); |
|
|
2017 | AV *on_destroy = sv_2mortal (SvREFCNT_inc ((AV *)SvRV (*on_destroyp))); |
|
|
2018 | AV *status = statusp ? sv_2mortal (SvREFCNT_inc ((AV *)SvRV (*statusp))) : 0; |
1889 | |
2019 | |
1890 | while (AvFILLp (on_destroy) >= 0) |
2020 | while (AvFILLp (on_destroy) >= 0) |
1891 | { |
2021 | { |
1892 | dSP; /* don't disturb outer sp */ |
2022 | dSP; /* don't disturb outer sp */ |
1893 | SV *cb = av_pop (on_destroy); |
2023 | SV *cb = av_pop (on_destroy); |
… | |
… | |
1895 | PUSHMARK (SP); |
2025 | PUSHMARK (SP); |
1896 | |
2026 | |
1897 | if (statusp) |
2027 | if (statusp) |
1898 | { |
2028 | { |
1899 | int i; |
2029 | int i; |
1900 | AV *status = (AV *)SvRV (*statusp); |
|
|
1901 | EXTEND (SP, AvFILLp (status) + 1); |
2030 | EXTEND (SP, AvFILLp (status) + 1); |
1902 | |
2031 | |
1903 | for (i = 0; i <= AvFILLp (status); ++i) |
2032 | for (i = 0; i <= AvFILLp (status); ++i) |
1904 | PUSHs (AvARRAY (status)[i]); |
2033 | PUSHs (AvARRAY (status)[i]); |
1905 | } |
2034 | } |
… | |
… | |
1915 | { |
2044 | { |
1916 | int i; |
2045 | int i; |
1917 | HV *hv = (HV *)SvRV (coro_current); |
2046 | HV *hv = (HV *)SvRV (coro_current); |
1918 | AV *av = newAV (); |
2047 | AV *av = newAV (); |
1919 | |
2048 | |
|
|
2049 | /* items are actually not so common, so optimise for this case */ |
|
|
2050 | if (items) |
|
|
2051 | { |
1920 | av_extend (av, items - 1); |
2052 | av_extend (av, items - 1); |
|
|
2053 | |
1921 | for (i = 0; i < items; ++i) |
2054 | for (i = 0; i < items; ++i) |
1922 | av_push (av, SvREFCNT_inc_NN (arg [i])); |
2055 | av_push (av, SvREFCNT_inc_NN (arg [i])); |
|
|
2056 | } |
1923 | |
2057 | |
1924 | hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); |
2058 | hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); |
1925 | |
2059 | |
1926 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
2060 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
1927 | api_ready (aTHX_ sv_manager); |
2061 | api_ready (aTHX_ sv_manager); |
… | |
… | |
2343 | if (PL_op->op_flags & OPf_STACKED) |
2477 | if (PL_op->op_flags & OPf_STACKED) |
2344 | { |
2478 | { |
2345 | if (items > slf_arga) |
2479 | if (items > slf_arga) |
2346 | { |
2480 | { |
2347 | slf_arga = items; |
2481 | slf_arga = items; |
2348 | free (slf_argv); |
2482 | Safefree (slf_argv); |
2349 | slf_argv = malloc (slf_arga * sizeof (SV *)); |
2483 | New (0, slf_argv, slf_arga, SV *); |
2350 | } |
2484 | } |
2351 | |
2485 | |
2352 | slf_argc = items; |
2486 | slf_argc = items; |
2353 | |
2487 | |
2354 | for (i = 0; i < items; ++i) |
2488 | for (i = 0; i < items; ++i) |
… | |
… | |
2715 | { |
2849 | { |
2716 | SV *cb_cv = s_get_cv_croak (arg [1]); |
2850 | SV *cb_cv = s_get_cv_croak (arg [1]); |
2717 | av_push (av, SvREFCNT_inc_NN (cb_cv)); |
2851 | av_push (av, SvREFCNT_inc_NN (cb_cv)); |
2718 | |
2852 | |
2719 | if (SvIVX (AvARRAY (av)[0])) |
2853 | if (SvIVX (AvARRAY (av)[0])) |
2720 | coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */ |
2854 | coro_signal_wake (aTHX_ av, 1); /* must be the only waiter */ |
2721 | |
2855 | |
2722 | frame->prepare = prepare_nop; |
2856 | frame->prepare = prepare_nop; |
2723 | frame->check = slf_check_nop; |
2857 | frame->check = slf_check_nop; |
2724 | } |
2858 | } |
2725 | else if (SvIVX (AvARRAY (av)[0])) |
2859 | else if (SvIVX (AvARRAY (av)[0])) |
… | |
… | |
2887 | /* now call the AIO function - we assume our request is uncancelable */ |
3021 | /* now call the AIO function - we assume our request is uncancelable */ |
2888 | PUTBACK; |
3022 | PUTBACK; |
2889 | call_sv ((SV *)req, G_VOID | G_DISCARD); |
3023 | call_sv ((SV *)req, G_VOID | G_DISCARD); |
2890 | } |
3024 | } |
2891 | |
3025 | |
2892 | /* now that the requets is going, we loop toll we have a result */ |
3026 | /* now that the request is going, we loop till we have a result */ |
2893 | frame->data = (void *)state; |
3027 | frame->data = (void *)state; |
2894 | frame->prepare = prepare_schedule; |
3028 | frame->prepare = prepare_schedule; |
2895 | frame->check = slf_check_aio_req; |
3029 | frame->check = slf_check_aio_req; |
2896 | } |
3030 | } |
2897 | |
3031 | |
… | |
… | |
2908 | /*****************************************************************************/ |
3042 | /*****************************************************************************/ |
2909 | |
3043 | |
2910 | #if CORO_CLONE |
3044 | #if CORO_CLONE |
2911 | # include "clone.c" |
3045 | # include "clone.c" |
2912 | #endif |
3046 | #endif |
|
|
3047 | |
|
|
3048 | /*****************************************************************************/ |
|
|
3049 | |
|
|
3050 | static SV * |
|
|
3051 | coro_new (pTHX_ HV *stash, SV **argv, int argc, int is_coro) |
|
|
3052 | { |
|
|
3053 | SV *coro_sv; |
|
|
3054 | struct coro *coro; |
|
|
3055 | MAGIC *mg; |
|
|
3056 | HV *hv; |
|
|
3057 | SV *cb; |
|
|
3058 | int i; |
|
|
3059 | |
|
|
3060 | if (argc > 0) |
|
|
3061 | { |
|
|
3062 | cb = s_get_cv_croak (argv [0]); |
|
|
3063 | |
|
|
3064 | if (!is_coro) |
|
|
3065 | { |
|
|
3066 | if (CvISXSUB (cb)) |
|
|
3067 | croak ("Coro::State doesn't support XS functions as coroutine start, caught"); |
|
|
3068 | |
|
|
3069 | if (!CvROOT (cb)) |
|
|
3070 | croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught"); |
|
|
3071 | } |
|
|
3072 | } |
|
|
3073 | |
|
|
3074 | Newz (0, coro, 1, struct coro); |
|
|
3075 | coro->args = newAV (); |
|
|
3076 | coro->flags = CF_NEW; |
|
|
3077 | |
|
|
3078 | if (coro_first) coro_first->prev = coro; |
|
|
3079 | coro->next = coro_first; |
|
|
3080 | coro_first = coro; |
|
|
3081 | |
|
|
3082 | coro->hv = hv = newHV (); |
|
|
3083 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
|
|
3084 | mg->mg_flags |= MGf_DUP; |
|
|
3085 | coro_sv = sv_bless (newRV_noinc ((SV *)hv), stash); |
|
|
3086 | |
|
|
3087 | if (argc > 0) |
|
|
3088 | { |
|
|
3089 | av_extend (coro->args, argc + is_coro - 1); |
|
|
3090 | |
|
|
3091 | if (is_coro) |
|
|
3092 | { |
|
|
3093 | av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb)); |
|
|
3094 | cb = (SV *)cv_coro_run; |
|
|
3095 | } |
|
|
3096 | |
|
|
3097 | coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb); |
|
|
3098 | |
|
|
3099 | for (i = 1; i < argc; i++) |
|
|
3100 | av_push (coro->args, newSVsv (argv [i])); |
|
|
3101 | } |
|
|
3102 | |
|
|
3103 | return coro_sv; |
|
|
3104 | } |
2913 | |
3105 | |
2914 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
3106 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2915 | |
3107 | |
2916 | PROTOTYPES: DISABLE |
3108 | PROTOTYPES: DISABLE |
2917 | |
3109 | |
… | |
… | |
2970 | coroapi.prepare_nop = prepare_nop; |
3162 | coroapi.prepare_nop = prepare_nop; |
2971 | coroapi.prepare_schedule = prepare_schedule; |
3163 | coroapi.prepare_schedule = prepare_schedule; |
2972 | coroapi.prepare_cede = prepare_cede; |
3164 | coroapi.prepare_cede = prepare_cede; |
2973 | coroapi.prepare_cede_notself = prepare_cede_notself; |
3165 | coroapi.prepare_cede_notself = prepare_cede_notself; |
2974 | |
3166 | |
2975 | { |
3167 | time_init (aTHX); |
2976 | SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
|
|
2977 | |
|
|
2978 | if (!svp) croak ("Time::HiRes is required"); |
|
|
2979 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); |
|
|
2980 | |
|
|
2981 | nvtime = INT2PTR (double (*)(), SvIV (*svp)); |
|
|
2982 | |
|
|
2983 | svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0); |
|
|
2984 | u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp)); |
|
|
2985 | } |
|
|
2986 | |
3168 | |
2987 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
3169 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
2988 | } |
3170 | } |
2989 | |
3171 | |
2990 | SV * |
3172 | SV * |
2991 | new (char *klass, ...) |
3173 | new (SV *klass, ...) |
2992 | ALIAS: |
3174 | ALIAS: |
2993 | Coro::new = 1 |
3175 | Coro::new = 1 |
2994 | CODE: |
3176 | CODE: |
2995 | { |
3177 | RETVAL = coro_new (aTHX_ ix ? coro_stash : coro_state_stash, &ST (1), items - 1, ix); |
2996 | struct coro *coro; |
3178 | OUTPUT: |
2997 | MAGIC *mg; |
|
|
2998 | HV *hv; |
|
|
2999 | SV *cb; |
|
|
3000 | int i; |
|
|
3001 | |
|
|
3002 | if (items > 1) |
|
|
3003 | { |
|
|
3004 | cb = s_get_cv_croak (ST (1)); |
|
|
3005 | |
|
|
3006 | if (!ix) |
|
|
3007 | { |
|
|
3008 | if (CvISXSUB (cb)) |
|
|
3009 | croak ("Coro::State doesn't support XS functions as coroutine start, caught"); |
|
|
3010 | |
|
|
3011 | if (!CvROOT (cb)) |
|
|
3012 | croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught"); |
|
|
3013 | } |
|
|
3014 | } |
|
|
3015 | |
|
|
3016 | Newz (0, coro, 1, struct coro); |
|
|
3017 | coro->args = newAV (); |
|
|
3018 | coro->flags = CF_NEW; |
|
|
3019 | |
|
|
3020 | if (coro_first) coro_first->prev = coro; |
|
|
3021 | coro->next = coro_first; |
|
|
3022 | coro_first = coro; |
|
|
3023 | |
|
|
3024 | coro->hv = hv = newHV (); |
|
|
3025 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
|
|
3026 | mg->mg_flags |= MGf_DUP; |
|
|
3027 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
|
|
3028 | |
|
|
3029 | if (items > 1) |
|
|
3030 | { |
|
|
3031 | av_extend (coro->args, items - 1 + ix - 1); |
|
|
3032 | |
|
|
3033 | if (ix) |
|
|
3034 | { |
|
|
3035 | av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb)); |
|
|
3036 | cb = (SV *)cv_coro_run; |
|
|
3037 | } |
|
|
3038 | |
|
|
3039 | coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb); |
|
|
3040 | |
|
|
3041 | for (i = 2; i < items; i++) |
|
|
3042 | av_push (coro->args, newSVsv (ST (i))); |
|
|
3043 | } |
|
|
3044 | } |
|
|
3045 | OUTPUT: |
|
|
3046 | RETVAL |
3179 | RETVAL |
3047 | |
3180 | |
3048 | void |
3181 | void |
3049 | transfer (...) |
3182 | transfer (...) |
3050 | PROTOTYPE: $$ |
3183 | PROTOTYPE: $$ |
… | |
… | |
3187 | RETVAL = boolSV (coro->flags & ix); |
3320 | RETVAL = boolSV (coro->flags & ix); |
3188 | OUTPUT: |
3321 | OUTPUT: |
3189 | RETVAL |
3322 | RETVAL |
3190 | |
3323 | |
3191 | void |
3324 | void |
3192 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
3325 | throw (Coro::State self, SV *exception = &PL_sv_undef) |
3193 | PROTOTYPE: $;$ |
3326 | PROTOTYPE: $;$ |
3194 | CODE: |
3327 | CODE: |
3195 | { |
3328 | { |
3196 | struct coro *current = SvSTATE_current; |
3329 | struct coro *current = SvSTATE_current; |
3197 | SV **throwp = self == current ? &CORO_THROW : &self->except; |
3330 | SV **exceptionp = self == current ? &CORO_THROW : &self->except; |
3198 | SvREFCNT_dec (*throwp); |
3331 | SvREFCNT_dec (*exceptionp); |
3199 | SvGETMAGIC (throw); |
3332 | SvGETMAGIC (exception); |
3200 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
3333 | *exceptionp = SvOK (exception) ? newSVsv (exception) : 0; |
3201 | } |
3334 | } |
3202 | |
3335 | |
3203 | void |
3336 | void |
3204 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3337 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3205 | PROTOTYPE: $;$ |
3338 | PROTOTYPE: $;$ |
… | |
… | |
3262 | cancel (Coro::State self) |
3395 | cancel (Coro::State self) |
3263 | CODE: |
3396 | CODE: |
3264 | coro_state_destroy (aTHX_ self); |
3397 | coro_state_destroy (aTHX_ self); |
3265 | coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */ |
3398 | coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */ |
3266 | |
3399 | |
3267 | |
|
|
3268 | SV * |
3400 | SV * |
3269 | enable_times (int enabled = enable_times) |
3401 | enable_times (int enabled = enable_times) |
3270 | CODE: |
3402 | CODE: |
3271 | { |
3403 | { |
3272 | RETVAL = boolSV (enable_times); |
3404 | RETVAL = boolSV (enable_times); |
… | |
… | |
3299 | PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9))); |
3431 | PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9))); |
3300 | |
3432 | |
3301 | if (expect_false (current == self)) |
3433 | if (expect_false (current == self)) |
3302 | coro_times_sub (SvSTATE (coro_current)); |
3434 | coro_times_sub (SvSTATE (coro_current)); |
3303 | } |
3435 | } |
|
|
3436 | |
|
|
3437 | void |
|
|
3438 | swap_sv (Coro::State coro, SV *sv, SV *swapsv) |
|
|
3439 | CODE: |
|
|
3440 | { |
|
|
3441 | struct coro *current = SvSTATE_current; |
|
|
3442 | |
|
|
3443 | if (current == coro) |
|
|
3444 | SWAP_SVS (current); |
|
|
3445 | |
|
|
3446 | if (!coro->swap_sv) |
|
|
3447 | coro->swap_sv = newAV (); |
|
|
3448 | |
|
|
3449 | av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (sv ))); |
|
|
3450 | av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (swapsv))); |
|
|
3451 | |
|
|
3452 | if (current == coro) |
|
|
3453 | SWAP_SVS (current); |
|
|
3454 | } |
|
|
3455 | |
3304 | |
3456 | |
3305 | MODULE = Coro::State PACKAGE = Coro |
3457 | MODULE = Coro::State PACKAGE = Coro |
3306 | |
3458 | |
3307 | BOOT: |
3459 | BOOT: |
3308 | { |
3460 | { |
… | |
… | |
3317 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
3469 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
3318 | |
3470 | |
3319 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3471 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3320 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3472 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3321 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3473 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3322 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
3474 | CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */ |
3323 | |
3475 | |
3324 | coro_stash = gv_stashpv ("Coro", TRUE); |
3476 | coro_stash = gv_stashpv ("Coro", TRUE); |
3325 | |
3477 | |
3326 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); |
3478 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); |
3327 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); |
3479 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); |
3328 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); |
3480 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); |
… | |
… | |
3345 | /*GCoroAPI = &coroapi;*/ |
3497 | /*GCoroAPI = &coroapi;*/ |
3346 | sv_setiv (sv, (IV)&coroapi); |
3498 | sv_setiv (sv, (IV)&coroapi); |
3347 | SvREADONLY_on (sv); |
3499 | SvREADONLY_on (sv); |
3348 | } |
3500 | } |
3349 | } |
3501 | } |
|
|
3502 | |
|
|
3503 | SV * |
|
|
3504 | async (...) |
|
|
3505 | PROTOTYPE: &@ |
|
|
3506 | CODE: |
|
|
3507 | RETVAL = coro_new (aTHX_ coro_stash, &ST (0), items, 1); |
|
|
3508 | api_ready (aTHX_ RETVAL); |
|
|
3509 | OUTPUT: |
|
|
3510 | RETVAL |
3350 | |
3511 | |
3351 | void |
3512 | void |
3352 | terminate (...) |
3513 | terminate (...) |
3353 | CODE: |
3514 | CODE: |
3354 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
3515 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
… | |
… | |
3472 | for (i = 1; i < items; ++i) |
3633 | for (i = 1; i < items; ++i) |
3473 | av_push (av, SvREFCNT_inc_NN (ST (i))); |
3634 | av_push (av, SvREFCNT_inc_NN (ST (i))); |
3474 | |
3635 | |
3475 | if ((SV *)hv == &PL_sv_undef) |
3636 | if ((SV *)hv == &PL_sv_undef) |
3476 | { |
3637 | { |
3477 | PUSHMARK (SP); |
3638 | SV *sv = coro_new (aTHX_ coro_stash, (SV **)&cv_pool_handler, 1, 1); |
3478 | EXTEND (SP, 2); |
|
|
3479 | PUSHs (sv_Coro); |
|
|
3480 | PUSHs ((SV *)cv_pool_handler); |
|
|
3481 | PUTBACK; |
|
|
3482 | call_sv ((SV *)cv_coro_state_new, G_SCALAR); |
|
|
3483 | SPAGAIN; |
|
|
3484 | |
|
|
3485 | hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); |
3639 | hv = (HV *)SvREFCNT_inc_NN (SvRV (sv)); |
|
|
3640 | SvREFCNT_dec (sv); |
3486 | } |
3641 | } |
3487 | |
3642 | |
3488 | { |
3643 | { |
3489 | struct coro *coro = SvSTATE_hv (hv); |
3644 | struct coro *coro = SvSTATE_hv (hv); |
3490 | |
3645 | |
… | |
… | |
3762 | { |
3917 | { |
3763 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
3918 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
3764 | coro_old_pp_sselect = 0; |
3919 | coro_old_pp_sselect = 0; |
3765 | } |
3920 | } |
3766 | |
3921 | |
3767 | |
|
|