|
|
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 | |
… | |
… | |
11 | #include "schmorp.h" |
16 | #include "schmorp.h" |
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> |
|
|
21 | |
|
|
22 | #ifndef SVs_PADSTALE |
|
|
23 | # define SVs_PADSTALE 0 |
|
|
24 | #endif |
16 | |
25 | |
17 | #ifdef WIN32 |
26 | #ifdef WIN32 |
18 | # undef setjmp |
27 | # undef setjmp |
19 | # undef longjmp |
28 | # undef longjmp |
20 | # undef _exit |
29 | # undef _exit |
… | |
… | |
142 | static SV *sv_pool_size; |
151 | static SV *sv_pool_size; |
143 | static SV *sv_async_pool_idle; /* description string */ |
152 | static SV *sv_async_pool_idle; /* description string */ |
144 | static AV *av_async_pool; /* idle pool */ |
153 | static AV *av_async_pool; /* idle pool */ |
145 | static SV *sv_Coro; /* class string */ |
154 | static SV *sv_Coro; /* class string */ |
146 | static CV *cv_pool_handler; |
155 | static CV *cv_pool_handler; |
147 | static CV *cv_coro_state_new; |
|
|
148 | |
156 | |
149 | /* Coro::AnyEvent */ |
157 | /* Coro::AnyEvent */ |
150 | static SV *sv_activity; |
158 | static SV *sv_activity; |
151 | |
159 | |
152 | /* enable processtime/realtime profiling */ |
160 | /* enable processtime/realtime profiling */ |
… | |
… | |
232 | CV *startcv; /* the CV to execute */ |
240 | CV *startcv; /* the CV to execute */ |
233 | AV *args; /* data associated with this coroutine (initial args) */ |
241 | AV *args; /* data associated with this coroutine (initial args) */ |
234 | int refcnt; /* coroutines are refcounted, yes */ |
242 | int refcnt; /* coroutines are refcounted, yes */ |
235 | int flags; /* CF_ flags */ |
243 | int flags; /* CF_ flags */ |
236 | HV *hv; /* the perl hash associated with this coro, if any */ |
244 | HV *hv; /* the perl hash associated with this coro, if any */ |
237 | void (*on_destroy)(pTHX_ struct coro *coro); |
245 | void (*on_destroy)(pTHX_ struct coro *coro); /* for temporary use by xs in critical sections */ |
238 | |
246 | |
239 | /* statistics */ |
247 | /* statistics */ |
240 | int usecount; /* number of transfers to this coro */ |
248 | int usecount; /* number of transfers to this coro */ |
241 | |
249 | |
242 | /* coro process data */ |
250 | /* coro process data */ |
… | |
… | |
250 | AV *invoke_av; |
258 | AV *invoke_av; |
251 | |
259 | |
252 | /* on_enter/on_leave */ |
260 | /* on_enter/on_leave */ |
253 | AV *on_enter; |
261 | AV *on_enter; |
254 | AV *on_leave; |
262 | AV *on_leave; |
|
|
263 | |
|
|
264 | /* swap_sv */ |
|
|
265 | AV *swap_sv; |
255 | |
266 | |
256 | /* times */ |
267 | /* times */ |
257 | coro_ts t_cpu, t_real; |
268 | coro_ts t_cpu, t_real; |
258 | |
269 | |
259 | /* linked list */ |
270 | /* linked list */ |
… | |
… | |
290 | static OP *(*coro_old_pp_sselect) (pTHX); |
301 | static OP *(*coro_old_pp_sselect) (pTHX); |
291 | static SV *coro_select_select; |
302 | static SV *coro_select_select; |
292 | |
303 | |
293 | /* horrible hack, but if it works... */ |
304 | /* horrible hack, but if it works... */ |
294 | static OP * |
305 | static OP * |
295 | coro_pp_sselect (aTHX) |
306 | coro_pp_sselect (pTHX) |
296 | { |
307 | { |
297 | dSP; |
308 | dSP; |
298 | PUSHMARK (SP - 4); /* fake argument list */ |
309 | PUSHMARK (SP - 4); /* fake argument list */ |
299 | XPUSHs (coro_select_select); |
310 | XPUSHs (coro_select_select); |
300 | PUTBACK; |
311 | PUTBACK; |
… | |
… | |
302 | /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */ |
313 | /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */ |
303 | PL_op->op_flags |= OPf_STACKED; |
314 | PL_op->op_flags |= OPf_STACKED; |
304 | PL_op->op_private = 0; |
315 | PL_op->op_private = 0; |
305 | return PL_ppaddr [OP_ENTERSUB](aTHX); |
316 | return PL_ppaddr [OP_ENTERSUB](aTHX); |
306 | } |
317 | } |
|
|
318 | |
|
|
319 | /** time stuff **************************************************************/ |
|
|
320 | |
|
|
321 | #ifdef HAS_GETTIMEOFDAY |
|
|
322 | |
|
|
323 | static void |
|
|
324 | coro_u2time (pTHX_ UV ret[2]) |
|
|
325 | { |
|
|
326 | struct timeval tv; |
|
|
327 | gettimeofday (&tv, 0); |
|
|
328 | |
|
|
329 | ret [0] = tv.tv_sec; |
|
|
330 | ret [1] = tv.tv_usec; |
|
|
331 | } |
|
|
332 | |
|
|
333 | static double |
|
|
334 | coro_nvtime () |
|
|
335 | { |
|
|
336 | struct timeval tv; |
|
|
337 | gettimeofday (&tv, 0); |
|
|
338 | |
|
|
339 | return tv.tv_sec + tv.tv_usec * 1e-6; |
|
|
340 | } |
|
|
341 | |
|
|
342 | static void |
|
|
343 | time_init (void) |
|
|
344 | { |
|
|
345 | nvtime = coro_nvtime; |
|
|
346 | u2time = coro_u2time; |
|
|
347 | } |
|
|
348 | |
|
|
349 | #else |
|
|
350 | |
|
|
351 | static void |
|
|
352 | time_init (void) |
|
|
353 | { |
|
|
354 | SV **svp; |
|
|
355 | |
|
|
356 | require_pv ("Time::HiRes"); |
|
|
357 | |
|
|
358 | svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
|
|
359 | |
|
|
360 | if (!svp) croak ("Time::HiRes is required"); |
|
|
361 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); |
|
|
362 | |
|
|
363 | nvtime = INT2PTR (double (*)(), SvIV (*svp)); |
|
|
364 | |
|
|
365 | svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0); |
|
|
366 | u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp)); |
|
|
367 | } |
|
|
368 | |
|
|
369 | #endif |
307 | |
370 | |
308 | /** lowlevel stuff **********************************************************/ |
371 | /** lowlevel stuff **********************************************************/ |
309 | |
372 | |
310 | static SV * |
373 | static SV * |
311 | coro_get_sv (pTHX_ const char *name, int create) |
374 | coro_get_sv (pTHX_ const char *name, int create) |
… | |
… | |
551 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
614 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
552 | } |
615 | } |
553 | |
616 | |
554 | /** load & save, init *******************************************************/ |
617 | /** load & save, init *******************************************************/ |
555 | |
618 | |
|
|
619 | /* swap sv heads, at least logically */ |
|
|
620 | static void |
|
|
621 | swap_svs (pTHX_ Coro__State c) |
|
|
622 | { |
|
|
623 | int i; |
|
|
624 | |
|
|
625 | for (i = 0; i <= AvFILLp (c->swap_sv); ) |
|
|
626 | { |
|
|
627 | SV *a = AvARRAY (c->swap_sv)[i++]; |
|
|
628 | SV *b = AvARRAY (c->swap_sv)[i++]; |
|
|
629 | |
|
|
630 | const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */ |
|
|
631 | SV tmp; |
|
|
632 | |
|
|
633 | /* swap sv_any */ |
|
|
634 | SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp); |
|
|
635 | |
|
|
636 | /* swap sv_flags */ |
|
|
637 | SvFLAGS (&tmp) = SvFLAGS (a); |
|
|
638 | SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep); |
|
|
639 | SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep); |
|
|
640 | |
|
|
641 | #if PERL_VERSION_ATLEAST (5,10,0) |
|
|
642 | /* perl 5.10 complicates this _quite_ a bit, but it also is |
|
|
643 | * much faster, so no quarrels here. alternatively, we could |
|
|
644 | * sv_upgrade to avoid this. |
|
|
645 | */ |
|
|
646 | { |
|
|
647 | /* swap sv_u */ |
|
|
648 | tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u; |
|
|
649 | |
|
|
650 | /* if SvANY points to the head, we need to adjust the pointers, |
|
|
651 | * as the pointer for a still points to b, and maybe vice versa. |
|
|
652 | */ |
|
|
653 | #define svany_in_head(type) \ |
|
|
654 | (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type))) |
|
|
655 | |
|
|
656 | if (svany_in_head (SvTYPE (a))) |
|
|
657 | SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a); |
|
|
658 | |
|
|
659 | if (svany_in_head (SvTYPE (b))) |
|
|
660 | SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b); |
|
|
661 | } |
|
|
662 | #endif |
|
|
663 | } |
|
|
664 | } |
|
|
665 | |
|
|
666 | #define SWAP_SVS(coro) \ |
|
|
667 | if (expect_false ((coro)->swap_sv)) \ |
|
|
668 | swap_svs (aTHX_ (coro)) |
|
|
669 | |
556 | static void |
670 | static void |
557 | on_enterleave_call (pTHX_ SV *cb); |
671 | on_enterleave_call (pTHX_ SV *cb); |
558 | |
672 | |
559 | static void |
673 | static void |
560 | load_perl (pTHX_ Coro__State c) |
674 | load_perl (pTHX_ Coro__State c) |
… | |
… | |
606 | int i; |
720 | int i; |
607 | |
721 | |
608 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
722 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
609 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
723 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
610 | } |
724 | } |
|
|
725 | |
|
|
726 | SWAP_SVS (c); |
611 | } |
727 | } |
612 | |
728 | |
613 | static void |
729 | static void |
614 | save_perl (pTHX_ Coro__State c) |
730 | save_perl (pTHX_ Coro__State c) |
615 | { |
731 | { |
|
|
732 | SWAP_SVS (c); |
|
|
733 | |
616 | if (expect_false (c->on_leave)) |
734 | if (expect_false (c->on_leave)) |
617 | { |
735 | { |
618 | int i; |
736 | int i; |
619 | |
737 | |
620 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
738 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
… | |
… | |
927 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
1045 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
928 | { |
1046 | { |
929 | return 1; |
1047 | return 1; |
930 | } |
1048 | } |
931 | |
1049 | |
932 | static UNOP coro_setup_op; |
1050 | static UNOP init_perl_op; |
933 | |
1051 | |
934 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
1052 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
935 | coro_setup (pTHX_ struct coro *coro) |
1053 | init_perl (pTHX_ struct coro *coro) |
936 | { |
1054 | { |
937 | /* |
1055 | /* |
938 | * emulate part of the perl startup here. |
1056 | * emulate part of the perl startup here. |
939 | */ |
1057 | */ |
940 | coro_init_stacks (aTHX); |
1058 | coro_init_stacks (aTHX); |
… | |
… | |
947 | PL_comppad_name_fill = 0; |
1065 | PL_comppad_name_fill = 0; |
948 | PL_comppad_name_floor = 0; |
1066 | PL_comppad_name_floor = 0; |
949 | PL_curpm = 0; |
1067 | PL_curpm = 0; |
950 | PL_curpad = 0; |
1068 | PL_curpad = 0; |
951 | PL_localizing = 0; |
1069 | PL_localizing = 0; |
952 | PL_dirty = 0; |
|
|
953 | PL_restartop = 0; |
1070 | PL_restartop = 0; |
954 | #if PERL_VERSION_ATLEAST (5,10,0) |
1071 | #if PERL_VERSION_ATLEAST (5,10,0) |
955 | PL_parser = 0; |
1072 | PL_parser = 0; |
956 | #endif |
1073 | #endif |
957 | PL_hints = 0; |
1074 | PL_hints = 0; |
… | |
… | |
989 | */ |
1106 | */ |
990 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
1107 | 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 */ |
1108 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
992 | |
1109 | |
993 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
1110 | /* 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; |
1111 | init_perl_op.op_next = PL_op; |
995 | coro_setup_op.op_type = OP_ENTERSUB; |
1112 | init_perl_op.op_type = OP_ENTERSUB; |
996 | coro_setup_op.op_ppaddr = pp_slf; |
1113 | init_perl_op.op_ppaddr = pp_slf; |
997 | /* no flags etc. required, as an init function won't be called */ |
1114 | /* no flags etc. required, as an init function won't be called */ |
998 | |
1115 | |
999 | PL_op = (OP *)&coro_setup_op; |
1116 | PL_op = (OP *)&init_perl_op; |
1000 | |
1117 | |
1001 | /* copy throw, in case it was set before coro_setup */ |
1118 | /* copy throw, in case it was set before init_perl */ |
1002 | CORO_THROW = coro->except; |
1119 | CORO_THROW = coro->except; |
|
|
1120 | |
|
|
1121 | SWAP_SVS (coro); |
1003 | |
1122 | |
1004 | if (expect_false (enable_times)) |
1123 | if (expect_false (enable_times)) |
1005 | { |
1124 | { |
1006 | coro_times_update (); |
1125 | coro_times_update (); |
1007 | coro_times_sub (coro); |
1126 | coro_times_sub (coro); |
… | |
… | |
1028 | dounwind (-1); |
1147 | dounwind (-1); |
1029 | } |
1148 | } |
1030 | } |
1149 | } |
1031 | |
1150 | |
1032 | static void |
1151 | static void |
1033 | coro_destruct_perl (pTHX_ struct coro *coro) |
1152 | destroy_perl (pTHX_ struct coro *coro) |
1034 | { |
1153 | { |
1035 | SV *svf [9]; |
1154 | SV *svf [9]; |
1036 | |
1155 | |
1037 | { |
1156 | { |
1038 | struct coro *current = SvSTATE_current; |
1157 | struct coro *current = SvSTATE_current; |
… | |
… | |
1042 | save_perl (aTHX_ current); |
1161 | save_perl (aTHX_ current); |
1043 | load_perl (aTHX_ coro); |
1162 | load_perl (aTHX_ coro); |
1044 | |
1163 | |
1045 | coro_unwind_stacks (aTHX); |
1164 | coro_unwind_stacks (aTHX); |
1046 | coro_destruct_stacks (aTHX); |
1165 | coro_destruct_stacks (aTHX); |
|
|
1166 | |
|
|
1167 | /* restore swapped sv's */ |
|
|
1168 | SWAP_SVS (coro); |
1047 | |
1169 | |
1048 | // now save some sv's to be free'd later |
1170 | // now save some sv's to be free'd later |
1049 | svf [0] = GvSV (PL_defgv); |
1171 | svf [0] = GvSV (PL_defgv); |
1050 | svf [1] = (SV *)GvAV (PL_defgv); |
1172 | svf [1] = (SV *)GvAV (PL_defgv); |
1051 | svf [2] = GvSV (PL_errgv); |
1173 | svf [2] = GvSV (PL_errgv); |
… | |
… | |
1493 | if (expect_false (next->flags & CF_NEW)) |
1615 | if (expect_false (next->flags & CF_NEW)) |
1494 | { |
1616 | { |
1495 | /* need to start coroutine */ |
1617 | /* need to start coroutine */ |
1496 | next->flags &= ~CF_NEW; |
1618 | next->flags &= ~CF_NEW; |
1497 | /* setup coroutine call */ |
1619 | /* setup coroutine call */ |
1498 | coro_setup (aTHX_ next); |
1620 | init_perl (aTHX_ next); |
1499 | } |
1621 | } |
1500 | else |
1622 | else |
1501 | load_perl (aTHX_ next); |
1623 | load_perl (aTHX_ next); |
1502 | |
1624 | |
1503 | /* possibly untie and reuse the cctx */ |
1625 | /* possibly untie and reuse the cctx */ |
… | |
… | |
1566 | |
1688 | |
1567 | if (coro->mainstack |
1689 | if (coro->mainstack |
1568 | && coro->mainstack != main_mainstack |
1690 | && coro->mainstack != main_mainstack |
1569 | && coro->slot |
1691 | && coro->slot |
1570 | && !PL_dirty) |
1692 | && !PL_dirty) |
1571 | coro_destruct_perl (aTHX_ coro); |
1693 | destroy_perl (aTHX_ coro); |
|
|
1694 | |
|
|
1695 | if (coro->next) coro->next->prev = coro->prev; |
|
|
1696 | if (coro->prev) coro->prev->next = coro->next; |
|
|
1697 | if (coro == coro_first) coro_first = coro->next; |
1572 | |
1698 | |
1573 | cctx_destroy (coro->cctx); |
1699 | cctx_destroy (coro->cctx); |
1574 | SvREFCNT_dec (coro->startcv); |
1700 | SvREFCNT_dec (coro->startcv); |
1575 | SvREFCNT_dec (coro->args); |
1701 | SvREFCNT_dec (coro->args); |
|
|
1702 | SvREFCNT_dec (coro->swap_sv); |
1576 | SvREFCNT_dec (CORO_THROW); |
1703 | SvREFCNT_dec (CORO_THROW); |
1577 | |
|
|
1578 | if (coro->next) coro->next->prev = coro->prev; |
|
|
1579 | if (coro->prev) coro->prev->next = coro->next; |
|
|
1580 | if (coro == coro_first) coro_first = coro->next; |
|
|
1581 | |
1704 | |
1582 | return 1; |
1705 | return 1; |
1583 | } |
1706 | } |
1584 | |
1707 | |
1585 | static int |
1708 | static int |
… | |
… | |
1755 | { |
1878 | { |
1756 | /* nothing to schedule: call the idle handler */ |
1879 | /* nothing to schedule: call the idle handler */ |
1757 | if (SvROK (sv_idle) |
1880 | if (SvROK (sv_idle) |
1758 | && SvOBJECT (SvRV (sv_idle))) |
1881 | && SvOBJECT (SvRV (sv_idle))) |
1759 | { |
1882 | { |
|
|
1883 | if (SvRV (sv_idle) == SvRV (coro_current)) |
|
|
1884 | croak ("FATAL: $Coro::IDLE blocked itself - did you try to block inside an event loop callback? Caught"); |
|
|
1885 | |
1760 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
1886 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
1761 | api_ready (aTHX_ SvRV (sv_idle)); |
1887 | api_ready (aTHX_ SvRV (sv_idle)); |
1762 | --coro_nready; |
1888 | --coro_nready; |
1763 | } |
1889 | } |
1764 | else |
1890 | else |
1765 | { |
1891 | { |
|
|
1892 | /* TODO: deprecated, remove, cannot work reliably *//*D*/ |
1766 | dSP; |
1893 | dSP; |
1767 | |
1894 | |
1768 | ENTER; |
1895 | ENTER; |
1769 | SAVETMPS; |
1896 | SAVETMPS; |
1770 | |
1897 | |
… | |
… | |
2349 | if (PL_op->op_flags & OPf_STACKED) |
2476 | if (PL_op->op_flags & OPf_STACKED) |
2350 | { |
2477 | { |
2351 | if (items > slf_arga) |
2478 | if (items > slf_arga) |
2352 | { |
2479 | { |
2353 | slf_arga = items; |
2480 | slf_arga = items; |
2354 | free (slf_argv); |
2481 | Safefree (slf_argv); |
2355 | slf_argv = malloc (slf_arga * sizeof (SV *)); |
2482 | New (0, slf_argv, slf_arga, SV *); |
2356 | } |
2483 | } |
2357 | |
2484 | |
2358 | slf_argc = items; |
2485 | slf_argc = items; |
2359 | |
2486 | |
2360 | for (i = 0; i < items; ++i) |
2487 | for (i = 0; i < items; ++i) |
… | |
… | |
2721 | { |
2848 | { |
2722 | SV *cb_cv = s_get_cv_croak (arg [1]); |
2849 | SV *cb_cv = s_get_cv_croak (arg [1]); |
2723 | av_push (av, SvREFCNT_inc_NN (cb_cv)); |
2850 | av_push (av, SvREFCNT_inc_NN (cb_cv)); |
2724 | |
2851 | |
2725 | if (SvIVX (AvARRAY (av)[0])) |
2852 | if (SvIVX (AvARRAY (av)[0])) |
2726 | coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */ |
2853 | coro_signal_wake (aTHX_ av, 1); /* must be the only waiter */ |
2727 | |
2854 | |
2728 | frame->prepare = prepare_nop; |
2855 | frame->prepare = prepare_nop; |
2729 | frame->check = slf_check_nop; |
2856 | frame->check = slf_check_nop; |
2730 | } |
2857 | } |
2731 | else if (SvIVX (AvARRAY (av)[0])) |
2858 | else if (SvIVX (AvARRAY (av)[0])) |
… | |
… | |
2893 | /* now call the AIO function - we assume our request is uncancelable */ |
3020 | /* now call the AIO function - we assume our request is uncancelable */ |
2894 | PUTBACK; |
3021 | PUTBACK; |
2895 | call_sv ((SV *)req, G_VOID | G_DISCARD); |
3022 | call_sv ((SV *)req, G_VOID | G_DISCARD); |
2896 | } |
3023 | } |
2897 | |
3024 | |
2898 | /* now that the requets is going, we loop toll we have a result */ |
3025 | /* now that the request is going, we loop till we have a result */ |
2899 | frame->data = (void *)state; |
3026 | frame->data = (void *)state; |
2900 | frame->prepare = prepare_schedule; |
3027 | frame->prepare = prepare_schedule; |
2901 | frame->check = slf_check_aio_req; |
3028 | frame->check = slf_check_aio_req; |
2902 | } |
3029 | } |
2903 | |
3030 | |
… | |
… | |
2918 | #endif |
3045 | #endif |
2919 | |
3046 | |
2920 | /*****************************************************************************/ |
3047 | /*****************************************************************************/ |
2921 | |
3048 | |
2922 | static SV * |
3049 | static SV * |
2923 | coro_new (HV *stash, SV **argv, int argc, int is_coro) |
3050 | coro_new (pTHX_ HV *stash, SV **argv, int argc, int is_coro) |
2924 | { |
3051 | { |
2925 | SV *coro_sv; |
3052 | SV *coro_sv; |
2926 | struct coro *coro; |
3053 | struct coro *coro; |
2927 | MAGIC *mg; |
3054 | MAGIC *mg; |
2928 | HV *hv; |
3055 | HV *hv; |
… | |
… | |
3034 | coroapi.prepare_nop = prepare_nop; |
3161 | coroapi.prepare_nop = prepare_nop; |
3035 | coroapi.prepare_schedule = prepare_schedule; |
3162 | coroapi.prepare_schedule = prepare_schedule; |
3036 | coroapi.prepare_cede = prepare_cede; |
3163 | coroapi.prepare_cede = prepare_cede; |
3037 | coroapi.prepare_cede_notself = prepare_cede_notself; |
3164 | coroapi.prepare_cede_notself = prepare_cede_notself; |
3038 | |
3165 | |
3039 | { |
3166 | time_init (); |
3040 | SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
|
|
3041 | |
|
|
3042 | if (!svp) croak ("Time::HiRes is required"); |
|
|
3043 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); |
|
|
3044 | |
|
|
3045 | nvtime = INT2PTR (double (*)(), SvIV (*svp)); |
|
|
3046 | |
|
|
3047 | svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0); |
|
|
3048 | u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp)); |
|
|
3049 | } |
|
|
3050 | |
3167 | |
3051 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
3168 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
3052 | } |
3169 | } |
3053 | |
3170 | |
3054 | SV * |
3171 | SV * |
3055 | new (SV *klass, ...) |
3172 | new (SV *klass, ...) |
3056 | ALIAS: |
3173 | ALIAS: |
3057 | Coro::new = 1 |
3174 | Coro::new = 1 |
3058 | CODE: |
3175 | CODE: |
3059 | RETVAL = coro_new (ix ? coro_stash : coro_state_stash, &ST (1), items - 1, ix); |
3176 | RETVAL = coro_new (aTHX_ ix ? coro_stash : coro_state_stash, &ST (1), items - 1, ix); |
3060 | OUTPUT: |
3177 | OUTPUT: |
3061 | RETVAL |
3178 | RETVAL |
3062 | |
3179 | |
3063 | void |
3180 | void |
3064 | transfer (...) |
3181 | transfer (...) |
… | |
… | |
3202 | RETVAL = boolSV (coro->flags & ix); |
3319 | RETVAL = boolSV (coro->flags & ix); |
3203 | OUTPUT: |
3320 | OUTPUT: |
3204 | RETVAL |
3321 | RETVAL |
3205 | |
3322 | |
3206 | void |
3323 | void |
3207 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
3324 | throw (Coro::State self, SV *exception = &PL_sv_undef) |
3208 | PROTOTYPE: $;$ |
3325 | PROTOTYPE: $;$ |
3209 | CODE: |
3326 | CODE: |
3210 | { |
3327 | { |
3211 | struct coro *current = SvSTATE_current; |
3328 | struct coro *current = SvSTATE_current; |
3212 | SV **throwp = self == current ? &CORO_THROW : &self->except; |
3329 | SV **exceptionp = self == current ? &CORO_THROW : &self->except; |
3213 | SvREFCNT_dec (*throwp); |
3330 | SvREFCNT_dec (*exceptionp); |
3214 | SvGETMAGIC (throw); |
3331 | SvGETMAGIC (exception); |
3215 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
3332 | *exceptionp = SvOK (exception) ? newSVsv (exception) : 0; |
3216 | } |
3333 | } |
3217 | |
3334 | |
3218 | void |
3335 | void |
3219 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3336 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3220 | PROTOTYPE: $;$ |
3337 | PROTOTYPE: $;$ |
… | |
… | |
3315 | |
3432 | |
3316 | if (expect_false (current == self)) |
3433 | if (expect_false (current == self)) |
3317 | coro_times_sub (SvSTATE (coro_current)); |
3434 | coro_times_sub (SvSTATE (coro_current)); |
3318 | } |
3435 | } |
3319 | |
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 | |
|
|
3456 | |
3320 | MODULE = Coro::State PACKAGE = Coro |
3457 | MODULE = Coro::State PACKAGE = Coro |
3321 | |
3458 | |
3322 | BOOT: |
3459 | BOOT: |
3323 | { |
3460 | { |
3324 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3461 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
… | |
… | |
3332 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
3469 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
3333 | |
3470 | |
3334 | 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); |
3335 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3472 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3336 | 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); |
3337 | 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 */ |
3338 | |
3475 | |
3339 | coro_stash = gv_stashpv ("Coro", TRUE); |
3476 | coro_stash = gv_stashpv ("Coro", TRUE); |
3340 | |
3477 | |
3341 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); |
3478 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); |
3342 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); |
3479 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); |
3343 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); |
3480 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); |
… | |
… | |
3365 | |
3502 | |
3366 | SV * |
3503 | SV * |
3367 | async (...) |
3504 | async (...) |
3368 | PROTOTYPE: &@ |
3505 | PROTOTYPE: &@ |
3369 | CODE: |
3506 | CODE: |
3370 | RETVAL = coro_new (coro_stash, &ST (0), items, 1); |
3507 | RETVAL = coro_new (aTHX_ coro_stash, &ST (0), items, 1); |
3371 | api_ready (RETVAL); |
3508 | api_ready (aTHX_ RETVAL); |
3372 | OUTPUT: |
3509 | OUTPUT: |
3373 | RETVAL |
3510 | RETVAL |
3374 | |
3511 | |
3375 | void |
3512 | void |
3376 | terminate (...) |
3513 | terminate (...) |
… | |
… | |
3496 | for (i = 1; i < items; ++i) |
3633 | for (i = 1; i < items; ++i) |
3497 | av_push (av, SvREFCNT_inc_NN (ST (i))); |
3634 | av_push (av, SvREFCNT_inc_NN (ST (i))); |
3498 | |
3635 | |
3499 | if ((SV *)hv == &PL_sv_undef) |
3636 | if ((SV *)hv == &PL_sv_undef) |
3500 | { |
3637 | { |
3501 | PUSHMARK (SP); |
3638 | SV *sv = coro_new (aTHX_ coro_stash, (SV **)&cv_pool_handler, 1, 1); |
3502 | EXTEND (SP, 2); |
|
|
3503 | PUSHs (sv_Coro); |
|
|
3504 | PUSHs ((SV *)cv_pool_handler); |
|
|
3505 | PUTBACK; |
|
|
3506 | call_sv ((SV *)cv_coro_state_new, G_SCALAR); |
|
|
3507 | SPAGAIN; |
|
|
3508 | |
|
|
3509 | hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); |
3639 | hv = (HV *)SvREFCNT_inc_NN (SvRV (sv)); |
|
|
3640 | SvREFCNT_dec (sv); |
3510 | } |
3641 | } |
3511 | |
3642 | |
3512 | { |
3643 | { |
3513 | struct coro *coro = SvSTATE_hv (hv); |
3644 | struct coro *coro = SvSTATE_hv (hv); |
3514 | |
3645 | |
… | |
… | |
3786 | { |
3917 | { |
3787 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
3918 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
3788 | coro_old_pp_sselect = 0; |
3919 | coro_old_pp_sselect = 0; |
3789 | } |
3920 | } |
3790 | |
3921 | |
3791 | |
|
|