|
|
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 |
… | |
… | |
231 | CV *startcv; /* the CV to execute */ |
241 | CV *startcv; /* the CV to execute */ |
232 | AV *args; /* data associated with this coroutine (initial args) */ |
242 | AV *args; /* data associated with this coroutine (initial args) */ |
233 | int refcnt; /* coroutines are refcounted, yes */ |
243 | int refcnt; /* coroutines are refcounted, yes */ |
234 | int flags; /* CF_ flags */ |
244 | int flags; /* CF_ flags */ |
235 | HV *hv; /* the perl hash associated with this coro, if any */ |
245 | HV *hv; /* the perl hash associated with this coro, if any */ |
236 | void (*on_destroy)(pTHX_ struct coro *coro); |
246 | void (*on_destroy)(pTHX_ struct coro *coro); /* for temporary use by xs in critical sections */ |
237 | |
247 | |
238 | /* statistics */ |
248 | /* statistics */ |
239 | int usecount; /* number of transfers to this coro */ |
249 | int usecount; /* number of transfers to this coro */ |
240 | |
250 | |
241 | /* coro process data */ |
251 | /* coro process data */ |
… | |
… | |
249 | AV *invoke_av; |
259 | AV *invoke_av; |
250 | |
260 | |
251 | /* on_enter/on_leave */ |
261 | /* on_enter/on_leave */ |
252 | AV *on_enter; |
262 | AV *on_enter; |
253 | AV *on_leave; |
263 | AV *on_leave; |
|
|
264 | |
|
|
265 | /* swap_sv */ |
|
|
266 | AV *swap_sv; |
254 | |
267 | |
255 | /* times */ |
268 | /* times */ |
256 | coro_ts t_cpu, t_real; |
269 | coro_ts t_cpu, t_real; |
257 | |
270 | |
258 | /* linked list */ |
271 | /* linked list */ |
… | |
… | |
301 | /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */ |
314 | /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */ |
302 | PL_op->op_flags |= OPf_STACKED; |
315 | PL_op->op_flags |= OPf_STACKED; |
303 | PL_op->op_private = 0; |
316 | PL_op->op_private = 0; |
304 | return PL_ppaddr [OP_ENTERSUB](aTHX); |
317 | return PL_ppaddr [OP_ENTERSUB](aTHX); |
305 | } |
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 |
306 | |
371 | |
307 | /** lowlevel stuff **********************************************************/ |
372 | /** lowlevel stuff **********************************************************/ |
308 | |
373 | |
309 | static SV * |
374 | static SV * |
310 | coro_get_sv (pTHX_ const char *name, int create) |
375 | coro_get_sv (pTHX_ const char *name, int create) |
… | |
… | |
550 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
615 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
551 | } |
616 | } |
552 | |
617 | |
553 | /** load & save, init *******************************************************/ |
618 | /** load & save, init *******************************************************/ |
554 | |
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 | |
555 | static void |
671 | static void |
556 | on_enterleave_call (pTHX_ SV *cb); |
672 | on_enterleave_call (pTHX_ SV *cb); |
557 | |
673 | |
558 | static void |
674 | static void |
559 | load_perl (pTHX_ Coro__State c) |
675 | load_perl (pTHX_ Coro__State c) |
… | |
… | |
605 | int i; |
721 | int i; |
606 | |
722 | |
607 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
723 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
608 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
724 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
609 | } |
725 | } |
|
|
726 | |
|
|
727 | SWAP_SVS (c); |
610 | } |
728 | } |
611 | |
729 | |
612 | static void |
730 | static void |
613 | save_perl (pTHX_ Coro__State c) |
731 | save_perl (pTHX_ Coro__State c) |
614 | { |
732 | { |
|
|
733 | SWAP_SVS (c); |
|
|
734 | |
615 | if (expect_false (c->on_leave)) |
735 | if (expect_false (c->on_leave)) |
616 | { |
736 | { |
617 | int i; |
737 | int i; |
618 | |
738 | |
619 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
739 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
… | |
… | |
926 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
1046 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
927 | { |
1047 | { |
928 | return 1; |
1048 | return 1; |
929 | } |
1049 | } |
930 | |
1050 | |
931 | static UNOP coro_setup_op; |
1051 | static UNOP init_perl_op; |
932 | |
1052 | |
933 | 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 */ |
934 | coro_setup (pTHX_ struct coro *coro) |
1054 | init_perl (pTHX_ struct coro *coro) |
935 | { |
1055 | { |
936 | /* |
1056 | /* |
937 | * emulate part of the perl startup here. |
1057 | * emulate part of the perl startup here. |
938 | */ |
1058 | */ |
939 | coro_init_stacks (aTHX); |
1059 | coro_init_stacks (aTHX); |
… | |
… | |
946 | PL_comppad_name_fill = 0; |
1066 | PL_comppad_name_fill = 0; |
947 | PL_comppad_name_floor = 0; |
1067 | PL_comppad_name_floor = 0; |
948 | PL_curpm = 0; |
1068 | PL_curpm = 0; |
949 | PL_curpad = 0; |
1069 | PL_curpad = 0; |
950 | PL_localizing = 0; |
1070 | PL_localizing = 0; |
951 | PL_dirty = 0; |
|
|
952 | PL_restartop = 0; |
1071 | PL_restartop = 0; |
953 | #if PERL_VERSION_ATLEAST (5,10,0) |
1072 | #if PERL_VERSION_ATLEAST (5,10,0) |
954 | PL_parser = 0; |
1073 | PL_parser = 0; |
955 | #endif |
1074 | #endif |
956 | PL_hints = 0; |
1075 | PL_hints = 0; |
… | |
… | |
988 | */ |
1107 | */ |
989 | 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 */ |
990 | 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 */ |
991 | |
1110 | |
992 | /* 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 */ |
993 | coro_setup_op.op_next = PL_op; |
1112 | init_perl_op.op_next = PL_op; |
994 | coro_setup_op.op_type = OP_ENTERSUB; |
1113 | init_perl_op.op_type = OP_ENTERSUB; |
995 | coro_setup_op.op_ppaddr = pp_slf; |
1114 | init_perl_op.op_ppaddr = pp_slf; |
996 | /* 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 */ |
997 | |
1116 | |
998 | PL_op = (OP *)&coro_setup_op; |
1117 | PL_op = (OP *)&init_perl_op; |
999 | |
1118 | |
1000 | /* copy throw, in case it was set before coro_setup */ |
1119 | /* copy throw, in case it was set before init_perl */ |
1001 | CORO_THROW = coro->except; |
1120 | CORO_THROW = coro->except; |
|
|
1121 | |
|
|
1122 | SWAP_SVS (coro); |
1002 | |
1123 | |
1003 | if (expect_false (enable_times)) |
1124 | if (expect_false (enable_times)) |
1004 | { |
1125 | { |
1005 | coro_times_update (); |
1126 | coro_times_update (); |
1006 | coro_times_sub (coro); |
1127 | coro_times_sub (coro); |
… | |
… | |
1027 | dounwind (-1); |
1148 | dounwind (-1); |
1028 | } |
1149 | } |
1029 | } |
1150 | } |
1030 | |
1151 | |
1031 | static void |
1152 | static void |
1032 | coro_destruct_perl (pTHX_ struct coro *coro) |
1153 | destroy_perl (pTHX_ struct coro *coro) |
1033 | { |
1154 | { |
1034 | SV *svf [9]; |
1155 | SV *svf [9]; |
1035 | |
1156 | |
1036 | { |
1157 | { |
1037 | struct coro *current = SvSTATE_current; |
1158 | struct coro *current = SvSTATE_current; |
… | |
… | |
1041 | save_perl (aTHX_ current); |
1162 | save_perl (aTHX_ current); |
1042 | load_perl (aTHX_ coro); |
1163 | load_perl (aTHX_ coro); |
1043 | |
1164 | |
1044 | coro_unwind_stacks (aTHX); |
1165 | coro_unwind_stacks (aTHX); |
1045 | coro_destruct_stacks (aTHX); |
1166 | coro_destruct_stacks (aTHX); |
|
|
1167 | |
|
|
1168 | /* restore swapped sv's */ |
|
|
1169 | SWAP_SVS (coro); |
1046 | |
1170 | |
1047 | // now save some sv's to be free'd later |
1171 | // now save some sv's to be free'd later |
1048 | svf [0] = GvSV (PL_defgv); |
1172 | svf [0] = GvSV (PL_defgv); |
1049 | svf [1] = (SV *)GvAV (PL_defgv); |
1173 | svf [1] = (SV *)GvAV (PL_defgv); |
1050 | svf [2] = GvSV (PL_errgv); |
1174 | svf [2] = GvSV (PL_errgv); |
… | |
… | |
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 |
… | |
… | |
2331 | if (PL_op->op_ppaddr != PL_ppaddr [OP_ENTERSUB] |
2459 | if (PL_op->op_ppaddr != PL_ppaddr [OP_ENTERSUB] |
2332 | && PL_op->op_ppaddr != pp_slf) |
2460 | && PL_op->op_ppaddr != pp_slf) |
2333 | croak ("FATAL: Coro SLF calls can only be made normally, not via goto or any other means, caught"); |
2461 | croak ("FATAL: Coro SLF calls can only be made normally, not via goto or any other means, caught"); |
2334 | |
2462 | |
2335 | CvFLAGS (cv) |= CVf_SLF; |
2463 | CvFLAGS (cv) |= CVf_SLF; |
2336 | CvNODEBUG_on (cv); |
|
|
2337 | CvXSUBANY (cv).any_ptr = (void *)init_cb; |
2464 | CvXSUBANY (cv).any_ptr = (void *)init_cb; |
2338 | slf_cv = cv; |
2465 | slf_cv = cv; |
2339 | |
2466 | |
2340 | /* we patch the op, and then re-run the whole call */ |
2467 | /* we patch the op, and then re-run the whole call */ |
2341 | /* we have to put the same argument on the stack for this to work */ |
2468 | /* we have to put the same argument on the stack for this to work */ |
… | |
… | |
2350 | if (PL_op->op_flags & OPf_STACKED) |
2477 | if (PL_op->op_flags & OPf_STACKED) |
2351 | { |
2478 | { |
2352 | if (items > slf_arga) |
2479 | if (items > slf_arga) |
2353 | { |
2480 | { |
2354 | slf_arga = items; |
2481 | slf_arga = items; |
2355 | free (slf_argv); |
2482 | Safefree (slf_argv); |
2356 | slf_argv = malloc (slf_arga * sizeof (SV *)); |
2483 | New (0, slf_argv, slf_arga, SV *); |
2357 | } |
2484 | } |
2358 | |
2485 | |
2359 | slf_argc = items; |
2486 | slf_argc = items; |
2360 | |
2487 | |
2361 | for (i = 0; i < items; ++i) |
2488 | for (i = 0; i < items; ++i) |
… | |
… | |
2722 | { |
2849 | { |
2723 | SV *cb_cv = s_get_cv_croak (arg [1]); |
2850 | SV *cb_cv = s_get_cv_croak (arg [1]); |
2724 | av_push (av, SvREFCNT_inc_NN (cb_cv)); |
2851 | av_push (av, SvREFCNT_inc_NN (cb_cv)); |
2725 | |
2852 | |
2726 | if (SvIVX (AvARRAY (av)[0])) |
2853 | if (SvIVX (AvARRAY (av)[0])) |
2727 | coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */ |
2854 | coro_signal_wake (aTHX_ av, 1); /* must be the only waiter */ |
2728 | |
2855 | |
2729 | frame->prepare = prepare_nop; |
2856 | frame->prepare = prepare_nop; |
2730 | frame->check = slf_check_nop; |
2857 | frame->check = slf_check_nop; |
2731 | } |
2858 | } |
2732 | else if (SvIVX (AvARRAY (av)[0])) |
2859 | else if (SvIVX (AvARRAY (av)[0])) |
… | |
… | |
2894 | /* now call the AIO function - we assume our request is uncancelable */ |
3021 | /* now call the AIO function - we assume our request is uncancelable */ |
2895 | PUTBACK; |
3022 | PUTBACK; |
2896 | call_sv ((SV *)req, G_VOID | G_DISCARD); |
3023 | call_sv ((SV *)req, G_VOID | G_DISCARD); |
2897 | } |
3024 | } |
2898 | |
3025 | |
2899 | /* 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 */ |
2900 | frame->data = (void *)state; |
3027 | frame->data = (void *)state; |
2901 | frame->prepare = prepare_schedule; |
3028 | frame->prepare = prepare_schedule; |
2902 | frame->check = slf_check_aio_req; |
3029 | frame->check = slf_check_aio_req; |
2903 | } |
3030 | } |
2904 | |
3031 | |
… | |
… | |
3035 | coroapi.prepare_nop = prepare_nop; |
3162 | coroapi.prepare_nop = prepare_nop; |
3036 | coroapi.prepare_schedule = prepare_schedule; |
3163 | coroapi.prepare_schedule = prepare_schedule; |
3037 | coroapi.prepare_cede = prepare_cede; |
3164 | coroapi.prepare_cede = prepare_cede; |
3038 | coroapi.prepare_cede_notself = prepare_cede_notself; |
3165 | coroapi.prepare_cede_notself = prepare_cede_notself; |
3039 | |
3166 | |
3040 | { |
3167 | time_init (aTHX); |
3041 | SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
|
|
3042 | |
|
|
3043 | if (!svp) croak ("Time::HiRes is required"); |
|
|
3044 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); |
|
|
3045 | |
|
|
3046 | nvtime = INT2PTR (double (*)(), SvIV (*svp)); |
|
|
3047 | |
|
|
3048 | svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0); |
|
|
3049 | u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp)); |
|
|
3050 | } |
|
|
3051 | |
3168 | |
3052 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
3169 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
3053 | } |
3170 | } |
3054 | |
3171 | |
3055 | SV * |
3172 | SV * |
… | |
… | |
3203 | RETVAL = boolSV (coro->flags & ix); |
3320 | RETVAL = boolSV (coro->flags & ix); |
3204 | OUTPUT: |
3321 | OUTPUT: |
3205 | RETVAL |
3322 | RETVAL |
3206 | |
3323 | |
3207 | void |
3324 | void |
3208 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
3325 | throw (Coro::State self, SV *exception = &PL_sv_undef) |
3209 | PROTOTYPE: $;$ |
3326 | PROTOTYPE: $;$ |
3210 | CODE: |
3327 | CODE: |
3211 | { |
3328 | { |
3212 | struct coro *current = SvSTATE_current; |
3329 | struct coro *current = SvSTATE_current; |
3213 | SV **throwp = self == current ? &CORO_THROW : &self->except; |
3330 | SV **exceptionp = self == current ? &CORO_THROW : &self->except; |
3214 | SvREFCNT_dec (*throwp); |
3331 | SvREFCNT_dec (*exceptionp); |
3215 | SvGETMAGIC (throw); |
3332 | SvGETMAGIC (exception); |
3216 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
3333 | *exceptionp = SvOK (exception) ? newSVsv (exception) : 0; |
3217 | } |
3334 | } |
3218 | |
3335 | |
3219 | void |
3336 | void |
3220 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3337 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3221 | PROTOTYPE: $;$ |
3338 | PROTOTYPE: $;$ |
… | |
… | |
3315 | PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9))); |
3432 | PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9))); |
3316 | |
3433 | |
3317 | if (expect_false (current == self)) |
3434 | if (expect_false (current == self)) |
3318 | coro_times_sub (SvSTATE (coro_current)); |
3435 | coro_times_sub (SvSTATE (coro_current)); |
3319 | } |
3436 | } |
|
|
3437 | |
|
|
3438 | void |
|
|
3439 | swap_sv (Coro::State coro, SV *sv, SV *swapsv) |
|
|
3440 | CODE: |
|
|
3441 | { |
|
|
3442 | struct coro *current = SvSTATE_current; |
|
|
3443 | |
|
|
3444 | if (current == coro) |
|
|
3445 | SWAP_SVS (current); |
|
|
3446 | |
|
|
3447 | if (!coro->swap_sv) |
|
|
3448 | coro->swap_sv = newAV (); |
|
|
3449 | |
|
|
3450 | av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (sv ))); |
|
|
3451 | av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (swapsv))); |
|
|
3452 | |
|
|
3453 | if (current == coro) |
|
|
3454 | SWAP_SVS (current); |
|
|
3455 | } |
|
|
3456 | |
3320 | |
3457 | |
3321 | MODULE = Coro::State PACKAGE = Coro |
3458 | MODULE = Coro::State PACKAGE = Coro |
3322 | |
3459 | |
3323 | BOOT: |
3460 | BOOT: |
3324 | { |
3461 | { |
… | |
… | |
3781 | { |
3918 | { |
3782 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
3919 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
3783 | coro_old_pp_sselect = 0; |
3920 | coro_old_pp_sselect = 0; |
3784 | } |
3921 | } |
3785 | |
3922 | |
3786 | |
|
|