|
|
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; |
… | |
… | |
290 | static OP *(*coro_old_pp_sselect) (pTHX); |
298 | static OP *(*coro_old_pp_sselect) (pTHX); |
291 | static SV *coro_select_select; |
299 | static SV *coro_select_select; |
292 | |
300 | |
293 | /* horrible hack, but if it works... */ |
301 | /* horrible hack, but if it works... */ |
294 | static OP * |
302 | static OP * |
295 | coro_pp_sselect (aTHX) |
303 | coro_pp_sselect (pTHX) |
296 | { |
304 | { |
297 | dSP; |
305 | dSP; |
298 | PUSHMARK (SP - 4); /* fake argument list */ |
306 | PUSHMARK (SP - 4); /* fake argument list */ |
299 | XPUSHs (coro_select_select); |
307 | XPUSHs (coro_select_select); |
300 | PUTBACK; |
308 | PUTBACK; |
… | |
… | |
551 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
559 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
552 | } |
560 | } |
553 | |
561 | |
554 | /** load & save, init *******************************************************/ |
562 | /** load & save, init *******************************************************/ |
555 | |
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 | |
556 | static void |
615 | static void |
557 | on_enterleave_call (pTHX_ SV *cb); |
616 | on_enterleave_call (pTHX_ SV *cb); |
558 | |
617 | |
559 | static void |
618 | static void |
560 | load_perl (pTHX_ Coro__State c) |
619 | load_perl (pTHX_ Coro__State c) |
… | |
… | |
606 | int i; |
665 | int i; |
607 | |
666 | |
608 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
667 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
609 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
668 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
610 | } |
669 | } |
|
|
670 | |
|
|
671 | SWAP_SVS (c); |
611 | } |
672 | } |
612 | |
673 | |
613 | static void |
674 | static void |
614 | save_perl (pTHX_ Coro__State c) |
675 | save_perl (pTHX_ Coro__State c) |
615 | { |
676 | { |
|
|
677 | SWAP_SVS (c); |
|
|
678 | |
616 | if (expect_false (c->on_leave)) |
679 | if (expect_false (c->on_leave)) |
617 | { |
680 | { |
618 | int i; |
681 | int i; |
619 | |
682 | |
620 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
683 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
… | |
… | |
927 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
990 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
928 | { |
991 | { |
929 | return 1; |
992 | return 1; |
930 | } |
993 | } |
931 | |
994 | |
932 | static UNOP coro_setup_op; |
995 | static UNOP init_perl_op; |
933 | |
996 | |
934 | 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 */ |
935 | coro_setup (pTHX_ struct coro *coro) |
998 | init_perl (pTHX_ struct coro *coro) |
936 | { |
999 | { |
937 | /* |
1000 | /* |
938 | * emulate part of the perl startup here. |
1001 | * emulate part of the perl startup here. |
939 | */ |
1002 | */ |
940 | coro_init_stacks (aTHX); |
1003 | coro_init_stacks (aTHX); |
… | |
… | |
989 | */ |
1052 | */ |
990 | 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 */ |
991 | 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 */ |
992 | |
1055 | |
993 | /* 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 */ |
994 | coro_setup_op.op_next = PL_op; |
1057 | init_perl_op.op_next = PL_op; |
995 | coro_setup_op.op_type = OP_ENTERSUB; |
1058 | init_perl_op.op_type = OP_ENTERSUB; |
996 | coro_setup_op.op_ppaddr = pp_slf; |
1059 | init_perl_op.op_ppaddr = pp_slf; |
997 | /* 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 */ |
998 | |
1061 | |
999 | PL_op = (OP *)&coro_setup_op; |
1062 | PL_op = (OP *)&init_perl_op; |
1000 | |
1063 | |
1001 | /* copy throw, in case it was set before coro_setup */ |
1064 | /* copy throw, in case it was set before init_perl */ |
1002 | CORO_THROW = coro->except; |
1065 | CORO_THROW = coro->except; |
|
|
1066 | |
|
|
1067 | SWAP_SVS (coro); |
1003 | |
1068 | |
1004 | if (expect_false (enable_times)) |
1069 | if (expect_false (enable_times)) |
1005 | { |
1070 | { |
1006 | coro_times_update (); |
1071 | coro_times_update (); |
1007 | coro_times_sub (coro); |
1072 | coro_times_sub (coro); |
… | |
… | |
1028 | dounwind (-1); |
1093 | dounwind (-1); |
1029 | } |
1094 | } |
1030 | } |
1095 | } |
1031 | |
1096 | |
1032 | static void |
1097 | static void |
1033 | coro_destruct_perl (pTHX_ struct coro *coro) |
1098 | destroy_perl (pTHX_ struct coro *coro) |
1034 | { |
1099 | { |
1035 | SV *svf [9]; |
1100 | SV *svf [9]; |
1036 | |
1101 | |
1037 | { |
1102 | { |
1038 | struct coro *current = SvSTATE_current; |
1103 | struct coro *current = SvSTATE_current; |
… | |
… | |
1042 | save_perl (aTHX_ current); |
1107 | save_perl (aTHX_ current); |
1043 | load_perl (aTHX_ coro); |
1108 | load_perl (aTHX_ coro); |
1044 | |
1109 | |
1045 | coro_unwind_stacks (aTHX); |
1110 | coro_unwind_stacks (aTHX); |
1046 | coro_destruct_stacks (aTHX); |
1111 | coro_destruct_stacks (aTHX); |
|
|
1112 | |
|
|
1113 | /* restore swapped sv's */ |
|
|
1114 | SWAP_SVS (coro); |
1047 | |
1115 | |
1048 | // now save some sv's to be free'd later |
1116 | // now save some sv's to be free'd later |
1049 | svf [0] = GvSV (PL_defgv); |
1117 | svf [0] = GvSV (PL_defgv); |
1050 | svf [1] = (SV *)GvAV (PL_defgv); |
1118 | svf [1] = (SV *)GvAV (PL_defgv); |
1051 | svf [2] = GvSV (PL_errgv); |
1119 | svf [2] = GvSV (PL_errgv); |
… | |
… | |
1275 | /* 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 */ |
1276 | PL_restartop = PL_op; |
1344 | PL_restartop = PL_op; |
1277 | perl_run (PL_curinterp); |
1345 | perl_run (PL_curinterp); |
1278 | /* |
1346 | /* |
1279 | * 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 |
1280 | * 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. |
1281 | */ |
1350 | */ |
1282 | |
1351 | |
1283 | /* |
1352 | /* |
1284 | * 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 |
1285 | * 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) |
… | |
… | |
1492 | if (expect_false (next->flags & CF_NEW)) |
1561 | if (expect_false (next->flags & CF_NEW)) |
1493 | { |
1562 | { |
1494 | /* need to start coroutine */ |
1563 | /* need to start coroutine */ |
1495 | next->flags &= ~CF_NEW; |
1564 | next->flags &= ~CF_NEW; |
1496 | /* setup coroutine call */ |
1565 | /* setup coroutine call */ |
1497 | coro_setup (aTHX_ next); |
1566 | init_perl (aTHX_ next); |
1498 | } |
1567 | } |
1499 | else |
1568 | else |
1500 | load_perl (aTHX_ next); |
1569 | load_perl (aTHX_ next); |
1501 | |
1570 | |
1502 | /* possibly untie and reuse the cctx */ |
1571 | /* possibly untie and reuse the cctx */ |
… | |
… | |
1565 | |
1634 | |
1566 | if (coro->mainstack |
1635 | if (coro->mainstack |
1567 | && coro->mainstack != main_mainstack |
1636 | && coro->mainstack != main_mainstack |
1568 | && coro->slot |
1637 | && coro->slot |
1569 | && !PL_dirty) |
1638 | && !PL_dirty) |
1570 | 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; |
1571 | |
1644 | |
1572 | cctx_destroy (coro->cctx); |
1645 | cctx_destroy (coro->cctx); |
1573 | SvREFCNT_dec (coro->startcv); |
1646 | SvREFCNT_dec (coro->startcv); |
1574 | SvREFCNT_dec (coro->args); |
1647 | SvREFCNT_dec (coro->args); |
|
|
1648 | SvREFCNT_dec (coro->swap_sv); |
1575 | SvREFCNT_dec (CORO_THROW); |
1649 | 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 | |
1650 | |
1581 | return 1; |
1651 | return 1; |
1582 | } |
1652 | } |
1583 | |
1653 | |
1584 | static int |
1654 | static int |
… | |
… | |
1668 | } |
1738 | } |
1669 | |
1739 | |
1670 | return 0; |
1740 | return 0; |
1671 | } |
1741 | } |
1672 | |
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 | |
1673 | static int |
1760 | static int |
1674 | api_ready (pTHX_ SV *coro_sv) |
1761 | api_ready (pTHX_ SV *coro_sv) |
1675 | { |
1762 | { |
1676 | struct coro *coro; |
|
|
1677 | SV *sv_hook; |
|
|
1678 | void (*xs_hook)(void); |
|
|
1679 | |
|
|
1680 | coro = SvSTATE (coro_sv); |
1763 | struct coro *coro = SvSTATE (coro_sv); |
1681 | |
1764 | |
1682 | if (coro->flags & CF_READY) |
1765 | if (coro->flags & CF_READY) |
1683 | return 0; |
1766 | return 0; |
1684 | |
1767 | |
1685 | coro->flags |= CF_READY; |
1768 | coro->flags |= CF_READY; |
1686 | |
1769 | |
1687 | sv_hook = coro_nready ? 0 : coro_readyhook; |
|
|
1688 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
|
|
1689 | |
|
|
1690 | coro_enq (aTHX_ coro); |
1770 | coro_enq (aTHX_ coro); |
1691 | ++coro_nready; |
|
|
1692 | |
1771 | |
1693 | if (sv_hook) |
1772 | if (!coro_nready++) |
1694 | { |
1773 | if (coroapi.readyhook) |
1695 | dSP; |
1774 | coroapi.readyhook (); |
1696 | |
|
|
1697 | ENTER; |
|
|
1698 | SAVETMPS; |
|
|
1699 | |
|
|
1700 | PUSHMARK (SP); |
|
|
1701 | PUTBACK; |
|
|
1702 | call_sv (sv_hook, G_VOID | G_DISCARD); |
|
|
1703 | |
|
|
1704 | FREETMPS; |
|
|
1705 | LEAVE; |
|
|
1706 | } |
|
|
1707 | |
|
|
1708 | if (xs_hook) |
|
|
1709 | xs_hook (); |
|
|
1710 | |
1775 | |
1711 | return 1; |
1776 | return 1; |
1712 | } |
1777 | } |
1713 | |
1778 | |
1714 | static int |
1779 | static int |
… | |
… | |
1759 | { |
1824 | { |
1760 | /* nothing to schedule: call the idle handler */ |
1825 | /* nothing to schedule: call the idle handler */ |
1761 | if (SvROK (sv_idle) |
1826 | if (SvROK (sv_idle) |
1762 | && SvOBJECT (SvRV (sv_idle))) |
1827 | && SvOBJECT (SvRV (sv_idle))) |
1763 | { |
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 | |
1764 | ++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 */ |
1765 | api_ready (aTHX_ SvRV (sv_idle)); |
1833 | api_ready (aTHX_ SvRV (sv_idle)); |
1766 | --coro_nready; |
1834 | --coro_nready; |
1767 | } |
1835 | } |
1768 | else |
1836 | else |
1769 | { |
1837 | { |
|
|
1838 | /* TODO: deprecated, remove, cannot work reliably *//*D*/ |
1770 | dSP; |
1839 | dSP; |
1771 | |
1840 | |
1772 | ENTER; |
1841 | ENTER; |
1773 | SAVETMPS; |
1842 | SAVETMPS; |
1774 | |
1843 | |
… | |
… | |
1920 | { |
1989 | { |
1921 | int i; |
1990 | int i; |
1922 | HV *hv = (HV *)SvRV (coro_current); |
1991 | HV *hv = (HV *)SvRV (coro_current); |
1923 | AV *av = newAV (); |
1992 | AV *av = newAV (); |
1924 | |
1993 | |
|
|
1994 | /* items are actually not so common, so optimise for this case */ |
|
|
1995 | if (items) |
|
|
1996 | { |
1925 | av_extend (av, items - 1); |
1997 | av_extend (av, items - 1); |
|
|
1998 | |
1926 | for (i = 0; i < items; ++i) |
1999 | for (i = 0; i < items; ++i) |
1927 | av_push (av, SvREFCNT_inc_NN (arg [i])); |
2000 | av_push (av, SvREFCNT_inc_NN (arg [i])); |
|
|
2001 | } |
1928 | |
2002 | |
1929 | 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); |
1930 | |
2004 | |
1931 | 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 */ |
1932 | api_ready (aTHX_ sv_manager); |
2006 | api_ready (aTHX_ sv_manager); |
… | |
… | |
2348 | if (PL_op->op_flags & OPf_STACKED) |
2422 | if (PL_op->op_flags & OPf_STACKED) |
2349 | { |
2423 | { |
2350 | if (items > slf_arga) |
2424 | if (items > slf_arga) |
2351 | { |
2425 | { |
2352 | slf_arga = items; |
2426 | slf_arga = items; |
2353 | free (slf_argv); |
2427 | Safefree (slf_argv); |
2354 | slf_argv = malloc (slf_arga * sizeof (SV *)); |
2428 | New (0, slf_argv, slf_arga, SV *); |
2355 | } |
2429 | } |
2356 | |
2430 | |
2357 | slf_argc = items; |
2431 | slf_argc = items; |
2358 | |
2432 | |
2359 | for (i = 0; i < items; ++i) |
2433 | for (i = 0; i < items; ++i) |
… | |
… | |
2892 | /* now call the AIO function - we assume our request is uncancelable */ |
2966 | /* now call the AIO function - we assume our request is uncancelable */ |
2893 | PUTBACK; |
2967 | PUTBACK; |
2894 | call_sv ((SV *)req, G_VOID | G_DISCARD); |
2968 | call_sv ((SV *)req, G_VOID | G_DISCARD); |
2895 | } |
2969 | } |
2896 | |
2970 | |
2897 | /* now that the requets is going, we loop toll we have a result */ |
2971 | /* now that the request is going, we loop till we have a result */ |
2898 | frame->data = (void *)state; |
2972 | frame->data = (void *)state; |
2899 | frame->prepare = prepare_schedule; |
2973 | frame->prepare = prepare_schedule; |
2900 | frame->check = slf_check_aio_req; |
2974 | frame->check = slf_check_aio_req; |
2901 | } |
2975 | } |
2902 | |
2976 | |
… | |
… | |
2913 | /*****************************************************************************/ |
2987 | /*****************************************************************************/ |
2914 | |
2988 | |
2915 | #if CORO_CLONE |
2989 | #if CORO_CLONE |
2916 | # include "clone.c" |
2990 | # include "clone.c" |
2917 | #endif |
2991 | #endif |
|
|
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 | } |
2918 | |
3050 | |
2919 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
3051 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2920 | |
3052 | |
2921 | PROTOTYPES: DISABLE |
3053 | PROTOTYPES: DISABLE |
2922 | |
3054 | |
… | |
… | |
2991 | |
3123 | |
2992 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
3124 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
2993 | } |
3125 | } |
2994 | |
3126 | |
2995 | SV * |
3127 | SV * |
2996 | new (char *klass, ...) |
3128 | new (SV *klass, ...) |
2997 | ALIAS: |
3129 | ALIAS: |
2998 | Coro::new = 1 |
3130 | Coro::new = 1 |
2999 | CODE: |
3131 | CODE: |
3000 | { |
3132 | RETVAL = coro_new (aTHX_ ix ? coro_stash : coro_state_stash, &ST (1), items - 1, ix); |
3001 | struct coro *coro; |
3133 | OUTPUT: |
3002 | MAGIC *mg; |
|
|
3003 | HV *hv; |
|
|
3004 | SV *cb; |
|
|
3005 | int i; |
|
|
3006 | |
|
|
3007 | if (items > 1) |
|
|
3008 | { |
|
|
3009 | cb = s_get_cv_croak (ST (1)); |
|
|
3010 | |
|
|
3011 | if (!ix) |
|
|
3012 | { |
|
|
3013 | if (CvISXSUB (cb)) |
|
|
3014 | croak ("Coro::State doesn't support XS functions as coroutine start, caught"); |
|
|
3015 | |
|
|
3016 | if (!CvROOT (cb)) |
|
|
3017 | croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught"); |
|
|
3018 | } |
|
|
3019 | } |
|
|
3020 | |
|
|
3021 | Newz (0, coro, 1, struct coro); |
|
|
3022 | coro->args = newAV (); |
|
|
3023 | coro->flags = CF_NEW; |
|
|
3024 | |
|
|
3025 | if (coro_first) coro_first->prev = coro; |
|
|
3026 | coro->next = coro_first; |
|
|
3027 | coro_first = coro; |
|
|
3028 | |
|
|
3029 | coro->hv = hv = newHV (); |
|
|
3030 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
|
|
3031 | mg->mg_flags |= MGf_DUP; |
|
|
3032 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
|
|
3033 | |
|
|
3034 | if (items > 1) |
|
|
3035 | { |
|
|
3036 | av_extend (coro->args, items - 1 + ix - 1); |
|
|
3037 | |
|
|
3038 | if (ix) |
|
|
3039 | { |
|
|
3040 | av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb)); |
|
|
3041 | cb = (SV *)cv_coro_run; |
|
|
3042 | } |
|
|
3043 | |
|
|
3044 | coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb); |
|
|
3045 | |
|
|
3046 | for (i = 2; i < items; i++) |
|
|
3047 | av_push (coro->args, newSVsv (ST (i))); |
|
|
3048 | } |
|
|
3049 | } |
|
|
3050 | OUTPUT: |
|
|
3051 | RETVAL |
3134 | RETVAL |
3052 | |
3135 | |
3053 | void |
3136 | void |
3054 | transfer (...) |
3137 | transfer (...) |
3055 | PROTOTYPE: $$ |
3138 | PROTOTYPE: $$ |
… | |
… | |
3192 | RETVAL = boolSV (coro->flags & ix); |
3275 | RETVAL = boolSV (coro->flags & ix); |
3193 | OUTPUT: |
3276 | OUTPUT: |
3194 | RETVAL |
3277 | RETVAL |
3195 | |
3278 | |
3196 | void |
3279 | void |
3197 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
3280 | throw (Coro::State self, SV *exception = &PL_sv_undef) |
3198 | PROTOTYPE: $;$ |
3281 | PROTOTYPE: $;$ |
3199 | CODE: |
3282 | CODE: |
3200 | { |
3283 | { |
3201 | struct coro *current = SvSTATE_current; |
3284 | struct coro *current = SvSTATE_current; |
3202 | SV **throwp = self == current ? &CORO_THROW : &self->except; |
3285 | SV **exceptionp = self == current ? &CORO_THROW : &self->except; |
3203 | SvREFCNT_dec (*throwp); |
3286 | SvREFCNT_dec (*exceptionp); |
3204 | SvGETMAGIC (throw); |
3287 | SvGETMAGIC (exception); |
3205 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
3288 | *exceptionp = SvOK (exception) ? newSVsv (exception) : 0; |
3206 | } |
3289 | } |
3207 | |
3290 | |
3208 | void |
3291 | void |
3209 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3292 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3210 | PROTOTYPE: $;$ |
3293 | PROTOTYPE: $;$ |
… | |
… | |
3305 | |
3388 | |
3306 | if (expect_false (current == self)) |
3389 | if (expect_false (current == self)) |
3307 | coro_times_sub (SvSTATE (coro_current)); |
3390 | coro_times_sub (SvSTATE (coro_current)); |
3308 | } |
3391 | } |
3309 | |
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 | |
3310 | MODULE = Coro::State PACKAGE = Coro |
3413 | MODULE = Coro::State PACKAGE = Coro |
3311 | |
3414 | |
3312 | BOOT: |
3415 | BOOT: |
3313 | { |
3416 | { |
3314 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3417 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
… | |
… | |
3322 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
3425 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
3323 | |
3426 | |
3324 | 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); |
3325 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3428 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3326 | 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); |
3327 | 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 */ |
3328 | |
3431 | |
3329 | coro_stash = gv_stashpv ("Coro", TRUE); |
3432 | coro_stash = gv_stashpv ("Coro", TRUE); |
3330 | |
3433 | |
3331 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); |
3434 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); |
3332 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); |
3435 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); |
3333 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); |
3436 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); |
… | |
… | |
3351 | sv_setiv (sv, (IV)&coroapi); |
3454 | sv_setiv (sv, (IV)&coroapi); |
3352 | SvREADONLY_on (sv); |
3455 | SvREADONLY_on (sv); |
3353 | } |
3456 | } |
3354 | } |
3457 | } |
3355 | |
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 | |
3356 | void |
3468 | void |
3357 | terminate (...) |
3469 | terminate (...) |
3358 | CODE: |
3470 | CODE: |
3359 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
3471 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
3360 | |
3472 | |
… | |
… | |
3394 | _set_readyhook (SV *hook) |
3506 | _set_readyhook (SV *hook) |
3395 | PROTOTYPE: $ |
3507 | PROTOTYPE: $ |
3396 | CODE: |
3508 | CODE: |
3397 | SvREFCNT_dec (coro_readyhook); |
3509 | SvREFCNT_dec (coro_readyhook); |
3398 | SvGETMAGIC (hook); |
3510 | SvGETMAGIC (hook); |
|
|
3511 | if (SvOK (hook)) |
|
|
3512 | { |
3399 | 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 | } |
3400 | |
3521 | |
3401 | int |
3522 | int |
3402 | prio (Coro::State coro, int newprio = 0) |
3523 | prio (Coro::State coro, int newprio = 0) |
3403 | PROTOTYPE: $;$ |
3524 | PROTOTYPE: $;$ |
3404 | ALIAS: |
3525 | ALIAS: |
… | |
… | |
3468 | for (i = 1; i < items; ++i) |
3589 | for (i = 1; i < items; ++i) |
3469 | av_push (av, SvREFCNT_inc_NN (ST (i))); |
3590 | av_push (av, SvREFCNT_inc_NN (ST (i))); |
3470 | |
3591 | |
3471 | if ((SV *)hv == &PL_sv_undef) |
3592 | if ((SV *)hv == &PL_sv_undef) |
3472 | { |
3593 | { |
3473 | PUSHMARK (SP); |
3594 | SV *sv = coro_new (aTHX_ coro_stash, (SV **)&cv_pool_handler, 1, 1); |
3474 | EXTEND (SP, 2); |
|
|
3475 | PUSHs (sv_Coro); |
|
|
3476 | PUSHs ((SV *)cv_pool_handler); |
|
|
3477 | PUTBACK; |
|
|
3478 | call_sv ((SV *)cv_coro_state_new, G_SCALAR); |
|
|
3479 | SPAGAIN; |
|
|
3480 | |
|
|
3481 | hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); |
3595 | hv = (HV *)SvREFCNT_inc_NN (SvRV (sv)); |
|
|
3596 | SvREFCNT_dec (sv); |
3482 | } |
3597 | } |
3483 | |
3598 | |
3484 | { |
3599 | { |
3485 | struct coro *coro = SvSTATE_hv (hv); |
3600 | struct coro *coro = SvSTATE_hv (hv); |
3486 | |
3601 | |