|
|
1 | #define NDEBUG 1 |
|
|
2 | |
1 | #include "libcoro/coro.c" |
3 | #include "libcoro/coro.c" |
2 | |
4 | |
3 | #define PERL_NO_GET_CONTEXT |
5 | #define PERL_NO_GET_CONTEXT |
4 | #define PERL_EXT |
6 | #define PERL_EXT |
5 | |
7 | |
… | |
… | |
11 | #include "schmorp.h" |
13 | #include "schmorp.h" |
12 | |
14 | |
13 | #include <stdio.h> |
15 | #include <stdio.h> |
14 | #include <errno.h> |
16 | #include <errno.h> |
15 | #include <assert.h> |
17 | #include <assert.h> |
|
|
18 | |
|
|
19 | #ifndef SVs_PADSTALE |
|
|
20 | # define SVs_PADSTALE 0 |
|
|
21 | #endif |
16 | |
22 | |
17 | #ifdef WIN32 |
23 | #ifdef WIN32 |
18 | # undef setjmp |
24 | # undef setjmp |
19 | # undef longjmp |
25 | # undef longjmp |
20 | # undef _exit |
26 | # undef _exit |
… | |
… | |
142 | static SV *sv_pool_size; |
148 | static SV *sv_pool_size; |
143 | static SV *sv_async_pool_idle; /* description string */ |
149 | static SV *sv_async_pool_idle; /* description string */ |
144 | static AV *av_async_pool; /* idle pool */ |
150 | static AV *av_async_pool; /* idle pool */ |
145 | static SV *sv_Coro; /* class string */ |
151 | static SV *sv_Coro; /* class string */ |
146 | static CV *cv_pool_handler; |
152 | static CV *cv_pool_handler; |
147 | static CV *cv_coro_state_new; |
|
|
148 | |
153 | |
149 | /* Coro::AnyEvent */ |
154 | /* Coro::AnyEvent */ |
150 | static SV *sv_activity; |
155 | static SV *sv_activity; |
151 | |
156 | |
152 | /* enable processtime/realtime profiling */ |
157 | /* enable processtime/realtime profiling */ |
… | |
… | |
251 | |
256 | |
252 | /* on_enter/on_leave */ |
257 | /* on_enter/on_leave */ |
253 | AV *on_enter; |
258 | AV *on_enter; |
254 | AV *on_leave; |
259 | AV *on_leave; |
255 | |
260 | |
|
|
261 | /* swap_sv */ |
|
|
262 | AV *swap_sv; |
|
|
263 | |
256 | /* times */ |
264 | /* times */ |
257 | coro_ts t_cpu, t_real; |
265 | coro_ts t_cpu, t_real; |
258 | |
266 | |
259 | /* linked list */ |
267 | /* linked list */ |
260 | struct coro *next, *prev; |
268 | struct coro *next, *prev; |
… | |
… | |
282 | static SV *coro_readyhook; |
290 | static SV *coro_readyhook; |
283 | static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ |
291 | static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ |
284 | static CV *cv_coro_run, *cv_coro_terminate; |
292 | static CV *cv_coro_run, *cv_coro_terminate; |
285 | static struct coro *coro_first; |
293 | static struct coro *coro_first; |
286 | #define coro_nready coroapi.nready |
294 | #define coro_nready coroapi.nready |
|
|
295 | |
|
|
296 | /** Coro::Select ************************************************************/ |
|
|
297 | |
|
|
298 | static OP *(*coro_old_pp_sselect) (pTHX); |
|
|
299 | static SV *coro_select_select; |
|
|
300 | |
|
|
301 | /* horrible hack, but if it works... */ |
|
|
302 | static OP * |
|
|
303 | coro_pp_sselect (pTHX) |
|
|
304 | { |
|
|
305 | dSP; |
|
|
306 | PUSHMARK (SP - 4); /* fake argument list */ |
|
|
307 | XPUSHs (coro_select_select); |
|
|
308 | PUTBACK; |
|
|
309 | |
|
|
310 | /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */ |
|
|
311 | PL_op->op_flags |= OPf_STACKED; |
|
|
312 | PL_op->op_private = 0; |
|
|
313 | return PL_ppaddr [OP_ENTERSUB](aTHX); |
|
|
314 | } |
287 | |
315 | |
288 | /** lowlevel stuff **********************************************************/ |
316 | /** lowlevel stuff **********************************************************/ |
289 | |
317 | |
290 | static SV * |
318 | static SV * |
291 | coro_get_sv (pTHX_ const char *name, int create) |
319 | coro_get_sv (pTHX_ const char *name, int create) |
… | |
… | |
531 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
559 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
532 | } |
560 | } |
533 | |
561 | |
534 | /** load & save, init *******************************************************/ |
562 | /** load & save, init *******************************************************/ |
535 | |
563 | |
|
|
564 | /* swap sv heads, at least logically */ |
|
|
565 | static void |
|
|
566 | swap_svs (pTHX_ Coro__State c) |
|
|
567 | { |
|
|
568 | int i; |
|
|
569 | |
|
|
570 | for (i = 0; i <= AvFILLp (c->swap_sv); ) |
|
|
571 | { |
|
|
572 | SV *a = AvARRAY (c->swap_sv)[i++]; |
|
|
573 | SV *b = AvARRAY (c->swap_sv)[i++]; |
|
|
574 | |
|
|
575 | const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */ |
|
|
576 | SV tmp; |
|
|
577 | |
|
|
578 | /* swap sv_any */ |
|
|
579 | SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp); |
|
|
580 | |
|
|
581 | /* swap sv_flags */ |
|
|
582 | SvFLAGS (&tmp) = SvFLAGS (a); |
|
|
583 | SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep); |
|
|
584 | SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep); |
|
|
585 | |
|
|
586 | #if PERL_VERSION_ATLEAST (5,10,0) |
|
|
587 | /* perl 5.10 complicates this _quite_ a bit, but it also is |
|
|
588 | * is much faster, so no quarrels here. alternatively, we could |
|
|
589 | * sv_upgrade to avoid this. |
|
|
590 | */ |
|
|
591 | { |
|
|
592 | /* swap sv_u */ |
|
|
593 | tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u; |
|
|
594 | |
|
|
595 | /* if SvANY points to the head, we need to adjust the pointers, |
|
|
596 | * as the pointer for a still points to b, and maybe vice versa. |
|
|
597 | */ |
|
|
598 | #define svany_in_head(type) \ |
|
|
599 | (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type))) |
|
|
600 | |
|
|
601 | if (svany_in_head (SvTYPE (a))) |
|
|
602 | SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a); |
|
|
603 | |
|
|
604 | if (svany_in_head (SvTYPE (b))) |
|
|
605 | SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b); |
|
|
606 | } |
|
|
607 | #endif |
|
|
608 | } |
|
|
609 | } |
|
|
610 | |
|
|
611 | #define SWAP_SVS(coro) \ |
|
|
612 | if (expect_false ((coro)->swap_sv)) \ |
|
|
613 | swap_svs (aTHX_ (coro)) |
|
|
614 | |
536 | static void |
615 | static void |
537 | on_enterleave_call (pTHX_ SV *cb); |
616 | on_enterleave_call (pTHX_ SV *cb); |
538 | |
617 | |
539 | static void |
618 | static void |
540 | load_perl (pTHX_ Coro__State c) |
619 | load_perl (pTHX_ Coro__State c) |
… | |
… | |
586 | int i; |
665 | int i; |
587 | |
666 | |
588 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
667 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
589 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
668 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
590 | } |
669 | } |
|
|
670 | |
|
|
671 | SWAP_SVS (c); |
591 | } |
672 | } |
592 | |
673 | |
593 | static void |
674 | static void |
594 | save_perl (pTHX_ Coro__State c) |
675 | save_perl (pTHX_ Coro__State c) |
595 | { |
676 | { |
|
|
677 | SWAP_SVS (c); |
|
|
678 | |
596 | if (expect_false (c->on_leave)) |
679 | if (expect_false (c->on_leave)) |
597 | { |
680 | { |
598 | int i; |
681 | int i; |
599 | |
682 | |
600 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
683 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
… | |
… | |
907 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
990 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
908 | { |
991 | { |
909 | return 1; |
992 | return 1; |
910 | } |
993 | } |
911 | |
994 | |
912 | static UNOP coro_setup_op; |
995 | static UNOP init_perl_op; |
913 | |
996 | |
914 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
997 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
915 | coro_setup (pTHX_ struct coro *coro) |
998 | init_perl (pTHX_ struct coro *coro) |
916 | { |
999 | { |
917 | /* |
1000 | /* |
918 | * emulate part of the perl startup here. |
1001 | * emulate part of the perl startup here. |
919 | */ |
1002 | */ |
920 | coro_init_stacks (aTHX); |
1003 | coro_init_stacks (aTHX); |
… | |
… | |
969 | */ |
1052 | */ |
970 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
1053 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
971 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
1054 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
972 | |
1055 | |
973 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
1056 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
974 | coro_setup_op.op_next = PL_op; |
1057 | init_perl_op.op_next = PL_op; |
975 | coro_setup_op.op_type = OP_ENTERSUB; |
1058 | init_perl_op.op_type = OP_ENTERSUB; |
976 | coro_setup_op.op_ppaddr = pp_slf; |
1059 | init_perl_op.op_ppaddr = pp_slf; |
977 | /* no flags etc. required, as an init function won't be called */ |
1060 | /* no flags etc. required, as an init function won't be called */ |
978 | |
1061 | |
979 | PL_op = (OP *)&coro_setup_op; |
1062 | PL_op = (OP *)&init_perl_op; |
980 | |
1063 | |
981 | /* copy throw, in case it was set before coro_setup */ |
1064 | /* copy throw, in case it was set before init_perl */ |
982 | CORO_THROW = coro->except; |
1065 | CORO_THROW = coro->except; |
|
|
1066 | |
|
|
1067 | SWAP_SVS (coro); |
983 | |
1068 | |
984 | if (expect_false (enable_times)) |
1069 | if (expect_false (enable_times)) |
985 | { |
1070 | { |
986 | coro_times_update (); |
1071 | coro_times_update (); |
987 | coro_times_sub (coro); |
1072 | coro_times_sub (coro); |
… | |
… | |
1008 | dounwind (-1); |
1093 | dounwind (-1); |
1009 | } |
1094 | } |
1010 | } |
1095 | } |
1011 | |
1096 | |
1012 | static void |
1097 | static void |
1013 | coro_destruct_perl (pTHX_ struct coro *coro) |
1098 | destroy_perl (pTHX_ struct coro *coro) |
1014 | { |
1099 | { |
1015 | SV *svf [9]; |
1100 | SV *svf [9]; |
1016 | |
1101 | |
1017 | { |
1102 | { |
1018 | struct coro *current = SvSTATE_current; |
1103 | struct coro *current = SvSTATE_current; |
… | |
… | |
1022 | save_perl (aTHX_ current); |
1107 | save_perl (aTHX_ current); |
1023 | load_perl (aTHX_ coro); |
1108 | load_perl (aTHX_ coro); |
1024 | |
1109 | |
1025 | coro_unwind_stacks (aTHX); |
1110 | coro_unwind_stacks (aTHX); |
1026 | coro_destruct_stacks (aTHX); |
1111 | coro_destruct_stacks (aTHX); |
|
|
1112 | |
|
|
1113 | /* restore swapped sv's */ |
|
|
1114 | SWAP_SVS (coro); |
1027 | |
1115 | |
1028 | // now save some sv's to be free'd later |
1116 | // now save some sv's to be free'd later |
1029 | svf [0] = GvSV (PL_defgv); |
1117 | svf [0] = GvSV (PL_defgv); |
1030 | svf [1] = (SV *)GvAV (PL_defgv); |
1118 | svf [1] = (SV *)GvAV (PL_defgv); |
1031 | svf [2] = GvSV (PL_errgv); |
1119 | svf [2] = GvSV (PL_errgv); |
… | |
… | |
1255 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1343 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1256 | PL_restartop = PL_op; |
1344 | PL_restartop = PL_op; |
1257 | perl_run (PL_curinterp); |
1345 | perl_run (PL_curinterp); |
1258 | /* |
1346 | /* |
1259 | * Unfortunately, there is no way to get at the return values of the |
1347 | * Unfortunately, there is no way to get at the return values of the |
1260 | * coro body here, as perl_run destroys these |
1348 | * coro body here, as perl_run destroys these. Likewise, we cannot catch |
|
|
1349 | * runtime errors here, as this is just a random interpreter, not a thread. |
1261 | */ |
1350 | */ |
1262 | |
1351 | |
1263 | /* |
1352 | /* |
1264 | * If perl-run returns we assume exit() was being called or the coro |
1353 | * If perl-run returns we assume exit() was being called or the coro |
1265 | * fell off the end, which seems to be the only valid (non-bug) |
1354 | * fell off the end, which seems to be the only valid (non-bug) |
… | |
… | |
1472 | if (expect_false (next->flags & CF_NEW)) |
1561 | if (expect_false (next->flags & CF_NEW)) |
1473 | { |
1562 | { |
1474 | /* need to start coroutine */ |
1563 | /* need to start coroutine */ |
1475 | next->flags &= ~CF_NEW; |
1564 | next->flags &= ~CF_NEW; |
1476 | /* setup coroutine call */ |
1565 | /* setup coroutine call */ |
1477 | coro_setup (aTHX_ next); |
1566 | init_perl (aTHX_ next); |
1478 | } |
1567 | } |
1479 | else |
1568 | else |
1480 | load_perl (aTHX_ next); |
1569 | load_perl (aTHX_ next); |
1481 | |
1570 | |
1482 | /* possibly untie and reuse the cctx */ |
1571 | /* possibly untie and reuse the cctx */ |
… | |
… | |
1545 | |
1634 | |
1546 | if (coro->mainstack |
1635 | if (coro->mainstack |
1547 | && coro->mainstack != main_mainstack |
1636 | && coro->mainstack != main_mainstack |
1548 | && coro->slot |
1637 | && coro->slot |
1549 | && !PL_dirty) |
1638 | && !PL_dirty) |
1550 | coro_destruct_perl (aTHX_ coro); |
1639 | destroy_perl (aTHX_ coro); |
|
|
1640 | |
|
|
1641 | if (coro->next) coro->next->prev = coro->prev; |
|
|
1642 | if (coro->prev) coro->prev->next = coro->next; |
|
|
1643 | if (coro == coro_first) coro_first = coro->next; |
1551 | |
1644 | |
1552 | cctx_destroy (coro->cctx); |
1645 | cctx_destroy (coro->cctx); |
1553 | SvREFCNT_dec (coro->startcv); |
1646 | SvREFCNT_dec (coro->startcv); |
1554 | SvREFCNT_dec (coro->args); |
1647 | SvREFCNT_dec (coro->args); |
|
|
1648 | SvREFCNT_dec (coro->swap_sv); |
1555 | SvREFCNT_dec (CORO_THROW); |
1649 | SvREFCNT_dec (CORO_THROW); |
1556 | |
|
|
1557 | if (coro->next) coro->next->prev = coro->prev; |
|
|
1558 | if (coro->prev) coro->prev->next = coro->next; |
|
|
1559 | if (coro == coro_first) coro_first = coro->next; |
|
|
1560 | |
1650 | |
1561 | return 1; |
1651 | return 1; |
1562 | } |
1652 | } |
1563 | |
1653 | |
1564 | static int |
1654 | static int |
… | |
… | |
1648 | } |
1738 | } |
1649 | |
1739 | |
1650 | return 0; |
1740 | return 0; |
1651 | } |
1741 | } |
1652 | |
1742 | |
|
|
1743 | static void |
|
|
1744 | invoke_sv_ready_hook_helper (void) |
|
|
1745 | { |
|
|
1746 | dTHX; |
|
|
1747 | dSP; |
|
|
1748 | |
|
|
1749 | ENTER; |
|
|
1750 | SAVETMPS; |
|
|
1751 | |
|
|
1752 | PUSHMARK (SP); |
|
|
1753 | PUTBACK; |
|
|
1754 | call_sv (coro_readyhook, G_VOID | G_DISCARD); |
|
|
1755 | |
|
|
1756 | FREETMPS; |
|
|
1757 | LEAVE; |
|
|
1758 | } |
|
|
1759 | |
1653 | static int |
1760 | static int |
1654 | api_ready (pTHX_ SV *coro_sv) |
1761 | api_ready (pTHX_ SV *coro_sv) |
1655 | { |
1762 | { |
1656 | struct coro *coro; |
|
|
1657 | SV *sv_hook; |
|
|
1658 | void (*xs_hook)(void); |
|
|
1659 | |
|
|
1660 | coro = SvSTATE (coro_sv); |
1763 | struct coro *coro = SvSTATE (coro_sv); |
1661 | |
1764 | |
1662 | if (coro->flags & CF_READY) |
1765 | if (coro->flags & CF_READY) |
1663 | return 0; |
1766 | return 0; |
1664 | |
1767 | |
1665 | coro->flags |= CF_READY; |
1768 | coro->flags |= CF_READY; |
1666 | |
1769 | |
1667 | sv_hook = coro_nready ? 0 : coro_readyhook; |
|
|
1668 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
|
|
1669 | |
|
|
1670 | coro_enq (aTHX_ coro); |
1770 | coro_enq (aTHX_ coro); |
1671 | ++coro_nready; |
|
|
1672 | |
1771 | |
1673 | if (sv_hook) |
1772 | if (!coro_nready++) |
1674 | { |
1773 | if (coroapi.readyhook) |
1675 | dSP; |
1774 | coroapi.readyhook (); |
1676 | |
|
|
1677 | ENTER; |
|
|
1678 | SAVETMPS; |
|
|
1679 | |
|
|
1680 | PUSHMARK (SP); |
|
|
1681 | PUTBACK; |
|
|
1682 | call_sv (sv_hook, G_VOID | G_DISCARD); |
|
|
1683 | |
|
|
1684 | FREETMPS; |
|
|
1685 | LEAVE; |
|
|
1686 | } |
|
|
1687 | |
|
|
1688 | if (xs_hook) |
|
|
1689 | xs_hook (); |
|
|
1690 | |
1775 | |
1691 | return 1; |
1776 | return 1; |
1692 | } |
1777 | } |
1693 | |
1778 | |
1694 | static int |
1779 | static int |
… | |
… | |
1739 | { |
1824 | { |
1740 | /* nothing to schedule: call the idle handler */ |
1825 | /* nothing to schedule: call the idle handler */ |
1741 | if (SvROK (sv_idle) |
1826 | if (SvROK (sv_idle) |
1742 | && SvOBJECT (SvRV (sv_idle))) |
1827 | && SvOBJECT (SvRV (sv_idle))) |
1743 | { |
1828 | { |
|
|
1829 | if (SvRV (sv_idle) == SvRV (coro_current)) |
|
|
1830 | croak ("FATAL: $Coro::IDLE blocked itself - did you try to block inside an event loop callback? Caught"); |
|
|
1831 | |
1744 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
1832 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
1745 | api_ready (aTHX_ SvRV (sv_idle)); |
1833 | api_ready (aTHX_ SvRV (sv_idle)); |
1746 | --coro_nready; |
1834 | --coro_nready; |
1747 | } |
1835 | } |
1748 | else |
1836 | else |
1749 | { |
1837 | { |
|
|
1838 | /* TODO: deprecated, remove, cannot work reliably *//*D*/ |
1750 | dSP; |
1839 | dSP; |
1751 | |
1840 | |
1752 | ENTER; |
1841 | ENTER; |
1753 | SAVETMPS; |
1842 | SAVETMPS; |
1754 | |
1843 | |
… | |
… | |
1900 | { |
1989 | { |
1901 | int i; |
1990 | int i; |
1902 | HV *hv = (HV *)SvRV (coro_current); |
1991 | HV *hv = (HV *)SvRV (coro_current); |
1903 | AV *av = newAV (); |
1992 | AV *av = newAV (); |
1904 | |
1993 | |
|
|
1994 | /* items are actually not so common, so optimise for this case */ |
|
|
1995 | if (items) |
|
|
1996 | { |
1905 | av_extend (av, items - 1); |
1997 | av_extend (av, items - 1); |
|
|
1998 | |
1906 | for (i = 0; i < items; ++i) |
1999 | for (i = 0; i < items; ++i) |
1907 | av_push (av, SvREFCNT_inc_NN (arg [i])); |
2000 | av_push (av, SvREFCNT_inc_NN (arg [i])); |
|
|
2001 | } |
1908 | |
2002 | |
1909 | hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); |
2003 | hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); |
1910 | |
2004 | |
1911 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
2005 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
1912 | api_ready (aTHX_ sv_manager); |
2006 | api_ready (aTHX_ sv_manager); |
… | |
… | |
2328 | if (PL_op->op_flags & OPf_STACKED) |
2422 | if (PL_op->op_flags & OPf_STACKED) |
2329 | { |
2423 | { |
2330 | if (items > slf_arga) |
2424 | if (items > slf_arga) |
2331 | { |
2425 | { |
2332 | slf_arga = items; |
2426 | slf_arga = items; |
2333 | free (slf_argv); |
2427 | Safefree (slf_argv); |
2334 | slf_argv = malloc (slf_arga * sizeof (SV *)); |
2428 | New (0, slf_argv, slf_arga, SV *); |
2335 | } |
2429 | } |
2336 | |
2430 | |
2337 | slf_argc = items; |
2431 | slf_argc = items; |
2338 | |
2432 | |
2339 | for (i = 0; i < items; ++i) |
2433 | for (i = 0; i < items; ++i) |
… | |
… | |
2894 | |
2988 | |
2895 | #if CORO_CLONE |
2989 | #if CORO_CLONE |
2896 | # include "clone.c" |
2990 | # include "clone.c" |
2897 | #endif |
2991 | #endif |
2898 | |
2992 | |
|
|
2993 | /*****************************************************************************/ |
|
|
2994 | |
|
|
2995 | static SV * |
|
|
2996 | coro_new (pTHX_ HV *stash, SV **argv, int argc, int is_coro) |
|
|
2997 | { |
|
|
2998 | SV *coro_sv; |
|
|
2999 | struct coro *coro; |
|
|
3000 | MAGIC *mg; |
|
|
3001 | HV *hv; |
|
|
3002 | SV *cb; |
|
|
3003 | int i; |
|
|
3004 | |
|
|
3005 | if (argc > 0) |
|
|
3006 | { |
|
|
3007 | cb = s_get_cv_croak (argv [0]); |
|
|
3008 | |
|
|
3009 | if (!is_coro) |
|
|
3010 | { |
|
|
3011 | if (CvISXSUB (cb)) |
|
|
3012 | croak ("Coro::State doesn't support XS functions as coroutine start, caught"); |
|
|
3013 | |
|
|
3014 | if (!CvROOT (cb)) |
|
|
3015 | croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught"); |
|
|
3016 | } |
|
|
3017 | } |
|
|
3018 | |
|
|
3019 | Newz (0, coro, 1, struct coro); |
|
|
3020 | coro->args = newAV (); |
|
|
3021 | coro->flags = CF_NEW; |
|
|
3022 | |
|
|
3023 | if (coro_first) coro_first->prev = coro; |
|
|
3024 | coro->next = coro_first; |
|
|
3025 | coro_first = coro; |
|
|
3026 | |
|
|
3027 | coro->hv = hv = newHV (); |
|
|
3028 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
|
|
3029 | mg->mg_flags |= MGf_DUP; |
|
|
3030 | coro_sv = sv_bless (newRV_noinc ((SV *)hv), stash); |
|
|
3031 | |
|
|
3032 | if (argc > 0) |
|
|
3033 | { |
|
|
3034 | av_extend (coro->args, argc + is_coro - 1); |
|
|
3035 | |
|
|
3036 | if (is_coro) |
|
|
3037 | { |
|
|
3038 | av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb)); |
|
|
3039 | cb = (SV *)cv_coro_run; |
|
|
3040 | } |
|
|
3041 | |
|
|
3042 | coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb); |
|
|
3043 | |
|
|
3044 | for (i = 1; i < argc; i++) |
|
|
3045 | av_push (coro->args, newSVsv (argv [i])); |
|
|
3046 | } |
|
|
3047 | |
|
|
3048 | return coro_sv; |
|
|
3049 | } |
|
|
3050 | |
2899 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
3051 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2900 | |
3052 | |
2901 | PROTOTYPES: DISABLE |
3053 | PROTOTYPES: DISABLE |
2902 | |
3054 | |
2903 | BOOT: |
3055 | BOOT: |
… | |
… | |
2971 | |
3123 | |
2972 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
3124 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
2973 | } |
3125 | } |
2974 | |
3126 | |
2975 | SV * |
3127 | SV * |
2976 | new (char *klass, ...) |
3128 | new (SV *klass, ...) |
2977 | ALIAS: |
3129 | ALIAS: |
2978 | Coro::new = 1 |
3130 | Coro::new = 1 |
2979 | CODE: |
3131 | CODE: |
2980 | { |
3132 | RETVAL = coro_new (aTHX_ ix ? coro_stash : coro_state_stash, &ST (1), items - 1, ix); |
2981 | struct coro *coro; |
3133 | OUTPUT: |
2982 | MAGIC *mg; |
|
|
2983 | HV *hv; |
|
|
2984 | SV *cb; |
|
|
2985 | int i; |
|
|
2986 | |
|
|
2987 | if (items > 1) |
|
|
2988 | { |
|
|
2989 | cb = s_get_cv_croak (ST (1)); |
|
|
2990 | |
|
|
2991 | if (!ix) |
|
|
2992 | { |
|
|
2993 | if (CvISXSUB (cb)) |
|
|
2994 | croak ("Coro::State doesn't support XS functions as coroutine start, caught"); |
|
|
2995 | |
|
|
2996 | if (!CvROOT (cb)) |
|
|
2997 | croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught"); |
|
|
2998 | } |
|
|
2999 | } |
|
|
3000 | |
|
|
3001 | Newz (0, coro, 1, struct coro); |
|
|
3002 | coro->args = newAV (); |
|
|
3003 | coro->flags = CF_NEW; |
|
|
3004 | |
|
|
3005 | if (coro_first) coro_first->prev = coro; |
|
|
3006 | coro->next = coro_first; |
|
|
3007 | coro_first = coro; |
|
|
3008 | |
|
|
3009 | coro->hv = hv = newHV (); |
|
|
3010 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
|
|
3011 | mg->mg_flags |= MGf_DUP; |
|
|
3012 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
|
|
3013 | |
|
|
3014 | if (items > 1) |
|
|
3015 | { |
|
|
3016 | av_extend (coro->args, items - 1 + ix - 1); |
|
|
3017 | |
|
|
3018 | if (ix) |
|
|
3019 | { |
|
|
3020 | av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb)); |
|
|
3021 | cb = (SV *)cv_coro_run; |
|
|
3022 | } |
|
|
3023 | |
|
|
3024 | coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb); |
|
|
3025 | |
|
|
3026 | for (i = 2; i < items; i++) |
|
|
3027 | av_push (coro->args, newSVsv (ST (i))); |
|
|
3028 | } |
|
|
3029 | } |
|
|
3030 | OUTPUT: |
|
|
3031 | RETVAL |
3134 | RETVAL |
3032 | |
3135 | |
3033 | void |
3136 | void |
3034 | transfer (...) |
3137 | transfer (...) |
3035 | PROTOTYPE: $$ |
3138 | PROTOTYPE: $$ |
… | |
… | |
3172 | RETVAL = boolSV (coro->flags & ix); |
3275 | RETVAL = boolSV (coro->flags & ix); |
3173 | OUTPUT: |
3276 | OUTPUT: |
3174 | RETVAL |
3277 | RETVAL |
3175 | |
3278 | |
3176 | void |
3279 | void |
3177 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
3280 | throw (Coro::State self, SV *exception = &PL_sv_undef) |
3178 | PROTOTYPE: $;$ |
3281 | PROTOTYPE: $;$ |
3179 | CODE: |
3282 | CODE: |
3180 | { |
3283 | { |
3181 | struct coro *current = SvSTATE_current; |
3284 | struct coro *current = SvSTATE_current; |
3182 | SV **throwp = self == current ? &CORO_THROW : &self->except; |
3285 | SV **exceptionp = self == current ? &CORO_THROW : &self->except; |
3183 | SvREFCNT_dec (*throwp); |
3286 | SvREFCNT_dec (*exceptionp); |
3184 | SvGETMAGIC (throw); |
3287 | SvGETMAGIC (exception); |
3185 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
3288 | *exceptionp = SvOK (exception) ? newSVsv (exception) : 0; |
3186 | } |
3289 | } |
3187 | |
3290 | |
3188 | void |
3291 | void |
3189 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3292 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3190 | PROTOTYPE: $;$ |
3293 | PROTOTYPE: $;$ |
… | |
… | |
3285 | |
3388 | |
3286 | if (expect_false (current == self)) |
3389 | if (expect_false (current == self)) |
3287 | coro_times_sub (SvSTATE (coro_current)); |
3390 | coro_times_sub (SvSTATE (coro_current)); |
3288 | } |
3391 | } |
3289 | |
3392 | |
|
|
3393 | void |
|
|
3394 | swap_sv (Coro::State coro, SV *sv, SV *swapsv) |
|
|
3395 | CODE: |
|
|
3396 | { |
|
|
3397 | struct coro *current = SvSTATE_current; |
|
|
3398 | |
|
|
3399 | if (current == coro) |
|
|
3400 | SWAP_SVS (current); |
|
|
3401 | |
|
|
3402 | if (!coro->swap_sv) |
|
|
3403 | coro->swap_sv = newAV (); |
|
|
3404 | |
|
|
3405 | av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (sv ))); |
|
|
3406 | av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (swapsv))); |
|
|
3407 | |
|
|
3408 | if (current == coro) |
|
|
3409 | SWAP_SVS (current); |
|
|
3410 | } |
|
|
3411 | |
|
|
3412 | |
3290 | MODULE = Coro::State PACKAGE = Coro |
3413 | MODULE = Coro::State PACKAGE = Coro |
3291 | |
3414 | |
3292 | BOOT: |
3415 | BOOT: |
3293 | { |
3416 | { |
3294 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3417 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
… | |
… | |
3302 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
3425 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
3303 | |
3426 | |
3304 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3427 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3305 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3428 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3306 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3429 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3307 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
3430 | CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */ |
3308 | |
3431 | |
3309 | coro_stash = gv_stashpv ("Coro", TRUE); |
3432 | coro_stash = gv_stashpv ("Coro", TRUE); |
3310 | |
3433 | |
3311 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); |
3434 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); |
3312 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); |
3435 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); |
3313 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); |
3436 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); |
… | |
… | |
3331 | sv_setiv (sv, (IV)&coroapi); |
3454 | sv_setiv (sv, (IV)&coroapi); |
3332 | SvREADONLY_on (sv); |
3455 | SvREADONLY_on (sv); |
3333 | } |
3456 | } |
3334 | } |
3457 | } |
3335 | |
3458 | |
|
|
3459 | SV * |
|
|
3460 | async (...) |
|
|
3461 | PROTOTYPE: &@ |
|
|
3462 | CODE: |
|
|
3463 | RETVAL = coro_new (aTHX_ coro_stash, &ST (0), items, 1); |
|
|
3464 | api_ready (aTHX_ RETVAL); |
|
|
3465 | OUTPUT: |
|
|
3466 | RETVAL |
|
|
3467 | |
3336 | void |
3468 | void |
3337 | terminate (...) |
3469 | terminate (...) |
3338 | CODE: |
3470 | CODE: |
3339 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
3471 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
3340 | |
3472 | |
… | |
… | |
3374 | _set_readyhook (SV *hook) |
3506 | _set_readyhook (SV *hook) |
3375 | PROTOTYPE: $ |
3507 | PROTOTYPE: $ |
3376 | CODE: |
3508 | CODE: |
3377 | SvREFCNT_dec (coro_readyhook); |
3509 | SvREFCNT_dec (coro_readyhook); |
3378 | SvGETMAGIC (hook); |
3510 | SvGETMAGIC (hook); |
|
|
3511 | if (SvOK (hook)) |
|
|
3512 | { |
3379 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
3513 | coro_readyhook = newSVsv (hook); |
|
|
3514 | CORO_READYHOOK = invoke_sv_ready_hook_helper; |
|
|
3515 | } |
|
|
3516 | else |
|
|
3517 | { |
|
|
3518 | coro_readyhook = 0; |
|
|
3519 | CORO_READYHOOK = 0; |
|
|
3520 | } |
3380 | |
3521 | |
3381 | int |
3522 | int |
3382 | prio (Coro::State coro, int newprio = 0) |
3523 | prio (Coro::State coro, int newprio = 0) |
3383 | PROTOTYPE: $;$ |
3524 | PROTOTYPE: $;$ |
3384 | ALIAS: |
3525 | ALIAS: |
… | |
… | |
3448 | for (i = 1; i < items; ++i) |
3589 | for (i = 1; i < items; ++i) |
3449 | av_push (av, SvREFCNT_inc_NN (ST (i))); |
3590 | av_push (av, SvREFCNT_inc_NN (ST (i))); |
3450 | |
3591 | |
3451 | if ((SV *)hv == &PL_sv_undef) |
3592 | if ((SV *)hv == &PL_sv_undef) |
3452 | { |
3593 | { |
3453 | PUSHMARK (SP); |
3594 | SV *sv = coro_new (aTHX_ coro_stash, (SV **)&cv_pool_handler, 1, 1); |
3454 | EXTEND (SP, 2); |
|
|
3455 | PUSHs (sv_Coro); |
|
|
3456 | PUSHs ((SV *)cv_pool_handler); |
|
|
3457 | PUTBACK; |
|
|
3458 | call_sv ((SV *)cv_coro_state_new, G_SCALAR); |
|
|
3459 | SPAGAIN; |
|
|
3460 | |
|
|
3461 | hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); |
3595 | hv = (HV *)SvREFCNT_inc_NN (SvRV (sv)); |
|
|
3596 | SvREFCNT_dec (sv); |
3462 | } |
3597 | } |
3463 | |
3598 | |
3464 | { |
3599 | { |
3465 | struct coro *coro = SvSTATE_hv (hv); |
3600 | struct coro *coro = SvSTATE_hv (hv); |
3466 | |
3601 | |
… | |
… | |
3717 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
3852 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
3718 | sv_setpv ((SV *)slf_cv, proto); |
3853 | sv_setpv ((SV *)slf_cv, proto); |
3719 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
3854 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
3720 | } |
3855 | } |
3721 | |
3856 | |
|
|
3857 | MODULE = Coro::State PACKAGE = Coro::Select |
|
|
3858 | |
|
|
3859 | void |
|
|
3860 | patch_pp_sselect () |
|
|
3861 | CODE: |
|
|
3862 | if (!coro_old_pp_sselect) |
|
|
3863 | { |
|
|
3864 | coro_select_select = (SV *)get_cv ("Coro::Select::select", 0); |
|
|
3865 | coro_old_pp_sselect = PL_ppaddr [OP_SSELECT]; |
|
|
3866 | PL_ppaddr [OP_SSELECT] = coro_pp_sselect; |
|
|
3867 | } |
|
|
3868 | |
|
|
3869 | void |
|
|
3870 | unpatch_pp_sselect () |
|
|
3871 | CODE: |
|
|
3872 | if (coro_old_pp_sselect) |
|
|
3873 | { |
|
|
3874 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
|
|
3875 | coro_old_pp_sselect = 0; |
|
|
3876 | } |
|
|
3877 | |
|
|
3878 | |