|
|
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 */ |
… | |
… | |
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; |
255 | |
263 | |
|
|
264 | /* swap_sv */ |
|
|
265 | AV *swap_sv; |
|
|
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 */ |
260 | struct coro *next, *prev; |
271 | struct coro *next, *prev; |
… | |
… | |
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; |
… | |
… | |
551 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
562 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
552 | } |
563 | } |
553 | |
564 | |
554 | /** load & save, init *******************************************************/ |
565 | /** load & save, init *******************************************************/ |
555 | |
566 | |
|
|
567 | /* swap sv heads, at least logically */ |
|
|
568 | static void |
|
|
569 | swap_svs (pTHX_ Coro__State c) |
|
|
570 | { |
|
|
571 | int i; |
|
|
572 | |
|
|
573 | for (i = 0; i <= AvFILLp (c->swap_sv); ) |
|
|
574 | { |
|
|
575 | SV *a = AvARRAY (c->swap_sv)[i++]; |
|
|
576 | SV *b = AvARRAY (c->swap_sv)[i++]; |
|
|
577 | |
|
|
578 | const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */ |
|
|
579 | SV tmp; |
|
|
580 | |
|
|
581 | /* swap sv_any */ |
|
|
582 | SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp); |
|
|
583 | |
|
|
584 | /* swap sv_flags */ |
|
|
585 | SvFLAGS (&tmp) = SvFLAGS (a); |
|
|
586 | SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep); |
|
|
587 | SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep); |
|
|
588 | |
|
|
589 | #if PERL_VERSION_ATLEAST (5,10,0) |
|
|
590 | /* perl 5.10 complicates this _quite_ a bit, but it also is |
|
|
591 | * is much faster, so no quarrels here. alternatively, we could |
|
|
592 | * sv_upgrade to avoid this. |
|
|
593 | */ |
|
|
594 | { |
|
|
595 | /* swap sv_u */ |
|
|
596 | tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u; |
|
|
597 | |
|
|
598 | /* if SvANY points to the head, we need to adjust the pointers, |
|
|
599 | * as the pointer for a still points to b, and maybe vice versa. |
|
|
600 | */ |
|
|
601 | #define svany_in_head(type) \ |
|
|
602 | (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type))) |
|
|
603 | |
|
|
604 | if (svany_in_head (SvTYPE (a))) |
|
|
605 | SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a); |
|
|
606 | |
|
|
607 | if (svany_in_head (SvTYPE (b))) |
|
|
608 | SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b); |
|
|
609 | } |
|
|
610 | #endif |
|
|
611 | } |
|
|
612 | } |
|
|
613 | |
|
|
614 | #define SWAP_SVS(coro) \ |
|
|
615 | if (expect_false ((coro)->swap_sv)) \ |
|
|
616 | swap_svs (aTHX_ (coro)) |
|
|
617 | |
556 | static void |
618 | static void |
557 | on_enterleave_call (pTHX_ SV *cb); |
619 | on_enterleave_call (pTHX_ SV *cb); |
558 | |
620 | |
559 | static void |
621 | static void |
560 | load_perl (pTHX_ Coro__State c) |
622 | load_perl (pTHX_ Coro__State c) |
… | |
… | |
606 | int i; |
668 | int i; |
607 | |
669 | |
608 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
670 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
609 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
671 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
610 | } |
672 | } |
|
|
673 | |
|
|
674 | SWAP_SVS (c); |
611 | } |
675 | } |
612 | |
676 | |
613 | static void |
677 | static void |
614 | save_perl (pTHX_ Coro__State c) |
678 | save_perl (pTHX_ Coro__State c) |
615 | { |
679 | { |
|
|
680 | SWAP_SVS (c); |
|
|
681 | |
616 | if (expect_false (c->on_leave)) |
682 | if (expect_false (c->on_leave)) |
617 | { |
683 | { |
618 | int i; |
684 | int i; |
619 | |
685 | |
620 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
686 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
… | |
… | |
927 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
993 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
928 | { |
994 | { |
929 | return 1; |
995 | return 1; |
930 | } |
996 | } |
931 | |
997 | |
932 | static UNOP coro_setup_op; |
998 | static UNOP init_perl_op; |
933 | |
999 | |
934 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
1000 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
935 | coro_setup (pTHX_ struct coro *coro) |
1001 | init_perl (pTHX_ struct coro *coro) |
936 | { |
1002 | { |
937 | /* |
1003 | /* |
938 | * emulate part of the perl startup here. |
1004 | * emulate part of the perl startup here. |
939 | */ |
1005 | */ |
940 | coro_init_stacks (aTHX); |
1006 | coro_init_stacks (aTHX); |
… | |
… | |
947 | PL_comppad_name_fill = 0; |
1013 | PL_comppad_name_fill = 0; |
948 | PL_comppad_name_floor = 0; |
1014 | PL_comppad_name_floor = 0; |
949 | PL_curpm = 0; |
1015 | PL_curpm = 0; |
950 | PL_curpad = 0; |
1016 | PL_curpad = 0; |
951 | PL_localizing = 0; |
1017 | PL_localizing = 0; |
952 | PL_dirty = 0; |
|
|
953 | PL_restartop = 0; |
1018 | PL_restartop = 0; |
954 | #if PERL_VERSION_ATLEAST (5,10,0) |
1019 | #if PERL_VERSION_ATLEAST (5,10,0) |
955 | PL_parser = 0; |
1020 | PL_parser = 0; |
956 | #endif |
1021 | #endif |
957 | PL_hints = 0; |
1022 | PL_hints = 0; |
… | |
… | |
989 | */ |
1054 | */ |
990 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
1055 | 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 */ |
1056 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
992 | |
1057 | |
993 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
1058 | /* 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; |
1059 | init_perl_op.op_next = PL_op; |
995 | coro_setup_op.op_type = OP_ENTERSUB; |
1060 | init_perl_op.op_type = OP_ENTERSUB; |
996 | coro_setup_op.op_ppaddr = pp_slf; |
1061 | init_perl_op.op_ppaddr = pp_slf; |
997 | /* no flags etc. required, as an init function won't be called */ |
1062 | /* no flags etc. required, as an init function won't be called */ |
998 | |
1063 | |
999 | PL_op = (OP *)&coro_setup_op; |
1064 | PL_op = (OP *)&init_perl_op; |
1000 | |
1065 | |
1001 | /* copy throw, in case it was set before coro_setup */ |
1066 | /* copy throw, in case it was set before init_perl */ |
1002 | CORO_THROW = coro->except; |
1067 | CORO_THROW = coro->except; |
|
|
1068 | |
|
|
1069 | SWAP_SVS (coro); |
1003 | |
1070 | |
1004 | if (expect_false (enable_times)) |
1071 | if (expect_false (enable_times)) |
1005 | { |
1072 | { |
1006 | coro_times_update (); |
1073 | coro_times_update (); |
1007 | coro_times_sub (coro); |
1074 | coro_times_sub (coro); |
… | |
… | |
1028 | dounwind (-1); |
1095 | dounwind (-1); |
1029 | } |
1096 | } |
1030 | } |
1097 | } |
1031 | |
1098 | |
1032 | static void |
1099 | static void |
1033 | coro_destruct_perl (pTHX_ struct coro *coro) |
1100 | destroy_perl (pTHX_ struct coro *coro) |
1034 | { |
1101 | { |
1035 | SV *svf [9]; |
1102 | SV *svf [9]; |
1036 | |
1103 | |
1037 | { |
1104 | { |
1038 | struct coro *current = SvSTATE_current; |
1105 | struct coro *current = SvSTATE_current; |
… | |
… | |
1042 | save_perl (aTHX_ current); |
1109 | save_perl (aTHX_ current); |
1043 | load_perl (aTHX_ coro); |
1110 | load_perl (aTHX_ coro); |
1044 | |
1111 | |
1045 | coro_unwind_stacks (aTHX); |
1112 | coro_unwind_stacks (aTHX); |
1046 | coro_destruct_stacks (aTHX); |
1113 | coro_destruct_stacks (aTHX); |
|
|
1114 | |
|
|
1115 | /* restore swapped sv's */ |
|
|
1116 | SWAP_SVS (coro); |
1047 | |
1117 | |
1048 | // now save some sv's to be free'd later |
1118 | // now save some sv's to be free'd later |
1049 | svf [0] = GvSV (PL_defgv); |
1119 | svf [0] = GvSV (PL_defgv); |
1050 | svf [1] = (SV *)GvAV (PL_defgv); |
1120 | svf [1] = (SV *)GvAV (PL_defgv); |
1051 | svf [2] = GvSV (PL_errgv); |
1121 | svf [2] = GvSV (PL_errgv); |
… | |
… | |
1275 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1345 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1276 | PL_restartop = PL_op; |
1346 | PL_restartop = PL_op; |
1277 | perl_run (PL_curinterp); |
1347 | perl_run (PL_curinterp); |
1278 | /* |
1348 | /* |
1279 | * Unfortunately, there is no way to get at the return values of the |
1349 | * Unfortunately, there is no way to get at the return values of the |
1280 | * coro body here, as perl_run destroys these |
1350 | * coro body here, as perl_run destroys these. Likewise, we cannot catch |
|
|
1351 | * runtime errors here, as this is just a random interpreter, not a thread. |
1281 | */ |
1352 | */ |
1282 | |
1353 | |
1283 | /* |
1354 | /* |
1284 | * If perl-run returns we assume exit() was being called or the coro |
1355 | * 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) |
1356 | * fell off the end, which seems to be the only valid (non-bug) |
… | |
… | |
1492 | if (expect_false (next->flags & CF_NEW)) |
1563 | if (expect_false (next->flags & CF_NEW)) |
1493 | { |
1564 | { |
1494 | /* need to start coroutine */ |
1565 | /* need to start coroutine */ |
1495 | next->flags &= ~CF_NEW; |
1566 | next->flags &= ~CF_NEW; |
1496 | /* setup coroutine call */ |
1567 | /* setup coroutine call */ |
1497 | coro_setup (aTHX_ next); |
1568 | init_perl (aTHX_ next); |
1498 | } |
1569 | } |
1499 | else |
1570 | else |
1500 | load_perl (aTHX_ next); |
1571 | load_perl (aTHX_ next); |
1501 | |
1572 | |
1502 | /* possibly untie and reuse the cctx */ |
1573 | /* possibly untie and reuse the cctx */ |
… | |
… | |
1565 | |
1636 | |
1566 | if (coro->mainstack |
1637 | if (coro->mainstack |
1567 | && coro->mainstack != main_mainstack |
1638 | && coro->mainstack != main_mainstack |
1568 | && coro->slot |
1639 | && coro->slot |
1569 | && !PL_dirty) |
1640 | && !PL_dirty) |
1570 | coro_destruct_perl (aTHX_ coro); |
1641 | destroy_perl (aTHX_ coro); |
|
|
1642 | |
|
|
1643 | if (coro->next) coro->next->prev = coro->prev; |
|
|
1644 | if (coro->prev) coro->prev->next = coro->next; |
|
|
1645 | if (coro == coro_first) coro_first = coro->next; |
1571 | |
1646 | |
1572 | cctx_destroy (coro->cctx); |
1647 | cctx_destroy (coro->cctx); |
1573 | SvREFCNT_dec (coro->startcv); |
1648 | SvREFCNT_dec (coro->startcv); |
1574 | SvREFCNT_dec (coro->args); |
1649 | SvREFCNT_dec (coro->args); |
|
|
1650 | SvREFCNT_dec (coro->swap_sv); |
1575 | SvREFCNT_dec (CORO_THROW); |
1651 | 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 | |
1652 | |
1581 | return 1; |
1653 | return 1; |
1582 | } |
1654 | } |
1583 | |
1655 | |
1584 | static int |
1656 | static int |
… | |
… | |
1668 | } |
1740 | } |
1669 | |
1741 | |
1670 | return 0; |
1742 | return 0; |
1671 | } |
1743 | } |
1672 | |
1744 | |
|
|
1745 | static void |
|
|
1746 | invoke_sv_ready_hook_helper (void) |
|
|
1747 | { |
|
|
1748 | dTHX; |
|
|
1749 | dSP; |
|
|
1750 | |
|
|
1751 | ENTER; |
|
|
1752 | SAVETMPS; |
|
|
1753 | |
|
|
1754 | PUSHMARK (SP); |
|
|
1755 | PUTBACK; |
|
|
1756 | call_sv (coro_readyhook, G_VOID | G_DISCARD); |
|
|
1757 | |
|
|
1758 | FREETMPS; |
|
|
1759 | LEAVE; |
|
|
1760 | } |
|
|
1761 | |
1673 | static int |
1762 | static int |
1674 | api_ready (pTHX_ SV *coro_sv) |
1763 | api_ready (pTHX_ SV *coro_sv) |
1675 | { |
1764 | { |
1676 | struct coro *coro; |
|
|
1677 | SV *sv_hook; |
|
|
1678 | void (*xs_hook)(void); |
|
|
1679 | |
|
|
1680 | coro = SvSTATE (coro_sv); |
1765 | struct coro *coro = SvSTATE (coro_sv); |
1681 | |
1766 | |
1682 | if (coro->flags & CF_READY) |
1767 | if (coro->flags & CF_READY) |
1683 | return 0; |
1768 | return 0; |
1684 | |
1769 | |
1685 | coro->flags |= CF_READY; |
1770 | coro->flags |= CF_READY; |
1686 | |
1771 | |
1687 | sv_hook = coro_nready ? 0 : coro_readyhook; |
|
|
1688 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
|
|
1689 | |
|
|
1690 | coro_enq (aTHX_ coro); |
1772 | coro_enq (aTHX_ coro); |
1691 | ++coro_nready; |
|
|
1692 | |
1773 | |
1693 | if (sv_hook) |
1774 | if (!coro_nready++) |
1694 | { |
1775 | if (coroapi.readyhook) |
1695 | dSP; |
1776 | 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 | |
1777 | |
1711 | return 1; |
1778 | return 1; |
1712 | } |
1779 | } |
1713 | |
1780 | |
1714 | static int |
1781 | static int |
… | |
… | |
1759 | { |
1826 | { |
1760 | /* nothing to schedule: call the idle handler */ |
1827 | /* nothing to schedule: call the idle handler */ |
1761 | if (SvROK (sv_idle) |
1828 | if (SvROK (sv_idle) |
1762 | && SvOBJECT (SvRV (sv_idle))) |
1829 | && SvOBJECT (SvRV (sv_idle))) |
1763 | { |
1830 | { |
|
|
1831 | if (SvRV (sv_idle) == SvRV (coro_current)) |
|
|
1832 | croak ("FATAL: $Coro::IDLE blocked itself - did you try to block inside an event loop callback? Caught"); |
|
|
1833 | |
1764 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
1834 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
1765 | api_ready (aTHX_ SvRV (sv_idle)); |
1835 | api_ready (aTHX_ SvRV (sv_idle)); |
1766 | --coro_nready; |
1836 | --coro_nready; |
1767 | } |
1837 | } |
1768 | else |
1838 | else |
1769 | { |
1839 | { |
|
|
1840 | /* TODO: deprecated, remove, cannot work reliably *//*D*/ |
1770 | dSP; |
1841 | dSP; |
1771 | |
1842 | |
1772 | ENTER; |
1843 | ENTER; |
1773 | SAVETMPS; |
1844 | SAVETMPS; |
1774 | |
1845 | |
… | |
… | |
1920 | { |
1991 | { |
1921 | int i; |
1992 | int i; |
1922 | HV *hv = (HV *)SvRV (coro_current); |
1993 | HV *hv = (HV *)SvRV (coro_current); |
1923 | AV *av = newAV (); |
1994 | AV *av = newAV (); |
1924 | |
1995 | |
|
|
1996 | /* items are actually not so common, so optimise for this case */ |
|
|
1997 | if (items) |
|
|
1998 | { |
1925 | av_extend (av, items - 1); |
1999 | av_extend (av, items - 1); |
|
|
2000 | |
1926 | for (i = 0; i < items; ++i) |
2001 | for (i = 0; i < items; ++i) |
1927 | av_push (av, SvREFCNT_inc_NN (arg [i])); |
2002 | av_push (av, SvREFCNT_inc_NN (arg [i])); |
|
|
2003 | } |
1928 | |
2004 | |
1929 | hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); |
2005 | hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); |
1930 | |
2006 | |
1931 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
2007 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
1932 | api_ready (aTHX_ sv_manager); |
2008 | api_ready (aTHX_ sv_manager); |
… | |
… | |
2348 | if (PL_op->op_flags & OPf_STACKED) |
2424 | if (PL_op->op_flags & OPf_STACKED) |
2349 | { |
2425 | { |
2350 | if (items > slf_arga) |
2426 | if (items > slf_arga) |
2351 | { |
2427 | { |
2352 | slf_arga = items; |
2428 | slf_arga = items; |
2353 | free (slf_argv); |
2429 | Safefree (slf_argv); |
2354 | slf_argv = malloc (slf_arga * sizeof (SV *)); |
2430 | New (0, slf_argv, slf_arga, SV *); |
2355 | } |
2431 | } |
2356 | |
2432 | |
2357 | slf_argc = items; |
2433 | slf_argc = items; |
2358 | |
2434 | |
2359 | for (i = 0; i < items; ++i) |
2435 | for (i = 0; i < items; ++i) |
… | |
… | |
2720 | { |
2796 | { |
2721 | SV *cb_cv = s_get_cv_croak (arg [1]); |
2797 | SV *cb_cv = s_get_cv_croak (arg [1]); |
2722 | av_push (av, SvREFCNT_inc_NN (cb_cv)); |
2798 | av_push (av, SvREFCNT_inc_NN (cb_cv)); |
2723 | |
2799 | |
2724 | if (SvIVX (AvARRAY (av)[0])) |
2800 | if (SvIVX (AvARRAY (av)[0])) |
2725 | coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */ |
2801 | coro_signal_wake (aTHX_ av, 1); /* must be the only waiter */ |
2726 | |
2802 | |
2727 | frame->prepare = prepare_nop; |
2803 | frame->prepare = prepare_nop; |
2728 | frame->check = slf_check_nop; |
2804 | frame->check = slf_check_nop; |
2729 | } |
2805 | } |
2730 | else if (SvIVX (AvARRAY (av)[0])) |
2806 | else if (SvIVX (AvARRAY (av)[0])) |
… | |
… | |
2892 | /* now call the AIO function - we assume our request is uncancelable */ |
2968 | /* now call the AIO function - we assume our request is uncancelable */ |
2893 | PUTBACK; |
2969 | PUTBACK; |
2894 | call_sv ((SV *)req, G_VOID | G_DISCARD); |
2970 | call_sv ((SV *)req, G_VOID | G_DISCARD); |
2895 | } |
2971 | } |
2896 | |
2972 | |
2897 | /* now that the requets is going, we loop toll we have a result */ |
2973 | /* now that the request is going, we loop till we have a result */ |
2898 | frame->data = (void *)state; |
2974 | frame->data = (void *)state; |
2899 | frame->prepare = prepare_schedule; |
2975 | frame->prepare = prepare_schedule; |
2900 | frame->check = slf_check_aio_req; |
2976 | frame->check = slf_check_aio_req; |
2901 | } |
2977 | } |
2902 | |
2978 | |
… | |
… | |
2913 | /*****************************************************************************/ |
2989 | /*****************************************************************************/ |
2914 | |
2990 | |
2915 | #if CORO_CLONE |
2991 | #if CORO_CLONE |
2916 | # include "clone.c" |
2992 | # include "clone.c" |
2917 | #endif |
2993 | #endif |
|
|
2994 | |
|
|
2995 | /*****************************************************************************/ |
|
|
2996 | |
|
|
2997 | static SV * |
|
|
2998 | coro_new (pTHX_ HV *stash, SV **argv, int argc, int is_coro) |
|
|
2999 | { |
|
|
3000 | SV *coro_sv; |
|
|
3001 | struct coro *coro; |
|
|
3002 | MAGIC *mg; |
|
|
3003 | HV *hv; |
|
|
3004 | SV *cb; |
|
|
3005 | int i; |
|
|
3006 | |
|
|
3007 | if (argc > 0) |
|
|
3008 | { |
|
|
3009 | cb = s_get_cv_croak (argv [0]); |
|
|
3010 | |
|
|
3011 | if (!is_coro) |
|
|
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 | coro_sv = sv_bless (newRV_noinc ((SV *)hv), stash); |
|
|
3033 | |
|
|
3034 | if (argc > 0) |
|
|
3035 | { |
|
|
3036 | av_extend (coro->args, argc + is_coro - 1); |
|
|
3037 | |
|
|
3038 | if (is_coro) |
|
|
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 = 1; i < argc; i++) |
|
|
3047 | av_push (coro->args, newSVsv (argv [i])); |
|
|
3048 | } |
|
|
3049 | |
|
|
3050 | return coro_sv; |
|
|
3051 | } |
2918 | |
3052 | |
2919 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
3053 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2920 | |
3054 | |
2921 | PROTOTYPES: DISABLE |
3055 | PROTOTYPES: DISABLE |
2922 | |
3056 | |
… | |
… | |
2991 | |
3125 | |
2992 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
3126 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
2993 | } |
3127 | } |
2994 | |
3128 | |
2995 | SV * |
3129 | SV * |
2996 | new (char *klass, ...) |
3130 | new (SV *klass, ...) |
2997 | ALIAS: |
3131 | ALIAS: |
2998 | Coro::new = 1 |
3132 | Coro::new = 1 |
2999 | CODE: |
3133 | CODE: |
3000 | { |
3134 | RETVAL = coro_new (aTHX_ ix ? coro_stash : coro_state_stash, &ST (1), items - 1, ix); |
3001 | struct coro *coro; |
3135 | 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 |
3136 | RETVAL |
3052 | |
3137 | |
3053 | void |
3138 | void |
3054 | transfer (...) |
3139 | transfer (...) |
3055 | PROTOTYPE: $$ |
3140 | PROTOTYPE: $$ |
… | |
… | |
3192 | RETVAL = boolSV (coro->flags & ix); |
3277 | RETVAL = boolSV (coro->flags & ix); |
3193 | OUTPUT: |
3278 | OUTPUT: |
3194 | RETVAL |
3279 | RETVAL |
3195 | |
3280 | |
3196 | void |
3281 | void |
3197 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
3282 | throw (Coro::State self, SV *exception = &PL_sv_undef) |
3198 | PROTOTYPE: $;$ |
3283 | PROTOTYPE: $;$ |
3199 | CODE: |
3284 | CODE: |
3200 | { |
3285 | { |
3201 | struct coro *current = SvSTATE_current; |
3286 | struct coro *current = SvSTATE_current; |
3202 | SV **throwp = self == current ? &CORO_THROW : &self->except; |
3287 | SV **exceptionp = self == current ? &CORO_THROW : &self->except; |
3203 | SvREFCNT_dec (*throwp); |
3288 | SvREFCNT_dec (*exceptionp); |
3204 | SvGETMAGIC (throw); |
3289 | SvGETMAGIC (exception); |
3205 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
3290 | *exceptionp = SvOK (exception) ? newSVsv (exception) : 0; |
3206 | } |
3291 | } |
3207 | |
3292 | |
3208 | void |
3293 | void |
3209 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3294 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3210 | PROTOTYPE: $;$ |
3295 | PROTOTYPE: $;$ |
… | |
… | |
3305 | |
3390 | |
3306 | if (expect_false (current == self)) |
3391 | if (expect_false (current == self)) |
3307 | coro_times_sub (SvSTATE (coro_current)); |
3392 | coro_times_sub (SvSTATE (coro_current)); |
3308 | } |
3393 | } |
3309 | |
3394 | |
|
|
3395 | void |
|
|
3396 | swap_sv (Coro::State coro, SV *sv, SV *swapsv) |
|
|
3397 | CODE: |
|
|
3398 | { |
|
|
3399 | struct coro *current = SvSTATE_current; |
|
|
3400 | |
|
|
3401 | if (current == coro) |
|
|
3402 | SWAP_SVS (current); |
|
|
3403 | |
|
|
3404 | if (!coro->swap_sv) |
|
|
3405 | coro->swap_sv = newAV (); |
|
|
3406 | |
|
|
3407 | av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (sv ))); |
|
|
3408 | av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (swapsv))); |
|
|
3409 | |
|
|
3410 | if (current == coro) |
|
|
3411 | SWAP_SVS (current); |
|
|
3412 | } |
|
|
3413 | |
|
|
3414 | |
3310 | MODULE = Coro::State PACKAGE = Coro |
3415 | MODULE = Coro::State PACKAGE = Coro |
3311 | |
3416 | |
3312 | BOOT: |
3417 | BOOT: |
3313 | { |
3418 | { |
3314 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3419 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
… | |
… | |
3322 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
3427 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
3323 | |
3428 | |
3324 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3429 | 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); |
3430 | 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); |
3431 | 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); |
3432 | CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */ |
3328 | |
3433 | |
3329 | coro_stash = gv_stashpv ("Coro", TRUE); |
3434 | coro_stash = gv_stashpv ("Coro", TRUE); |
3330 | |
3435 | |
3331 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); |
3436 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); |
3332 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); |
3437 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); |
3333 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); |
3438 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); |
… | |
… | |
3351 | sv_setiv (sv, (IV)&coroapi); |
3456 | sv_setiv (sv, (IV)&coroapi); |
3352 | SvREADONLY_on (sv); |
3457 | SvREADONLY_on (sv); |
3353 | } |
3458 | } |
3354 | } |
3459 | } |
3355 | |
3460 | |
|
|
3461 | SV * |
|
|
3462 | async (...) |
|
|
3463 | PROTOTYPE: &@ |
|
|
3464 | CODE: |
|
|
3465 | RETVAL = coro_new (aTHX_ coro_stash, &ST (0), items, 1); |
|
|
3466 | api_ready (aTHX_ RETVAL); |
|
|
3467 | OUTPUT: |
|
|
3468 | RETVAL |
|
|
3469 | |
3356 | void |
3470 | void |
3357 | terminate (...) |
3471 | terminate (...) |
3358 | CODE: |
3472 | CODE: |
3359 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
3473 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
3360 | |
3474 | |
… | |
… | |
3394 | _set_readyhook (SV *hook) |
3508 | _set_readyhook (SV *hook) |
3395 | PROTOTYPE: $ |
3509 | PROTOTYPE: $ |
3396 | CODE: |
3510 | CODE: |
3397 | SvREFCNT_dec (coro_readyhook); |
3511 | SvREFCNT_dec (coro_readyhook); |
3398 | SvGETMAGIC (hook); |
3512 | SvGETMAGIC (hook); |
|
|
3513 | if (SvOK (hook)) |
|
|
3514 | { |
3399 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
3515 | coro_readyhook = newSVsv (hook); |
|
|
3516 | CORO_READYHOOK = invoke_sv_ready_hook_helper; |
|
|
3517 | } |
|
|
3518 | else |
|
|
3519 | { |
|
|
3520 | coro_readyhook = 0; |
|
|
3521 | CORO_READYHOOK = 0; |
|
|
3522 | } |
3400 | |
3523 | |
3401 | int |
3524 | int |
3402 | prio (Coro::State coro, int newprio = 0) |
3525 | prio (Coro::State coro, int newprio = 0) |
3403 | PROTOTYPE: $;$ |
3526 | PROTOTYPE: $;$ |
3404 | ALIAS: |
3527 | ALIAS: |
… | |
… | |
3468 | for (i = 1; i < items; ++i) |
3591 | for (i = 1; i < items; ++i) |
3469 | av_push (av, SvREFCNT_inc_NN (ST (i))); |
3592 | av_push (av, SvREFCNT_inc_NN (ST (i))); |
3470 | |
3593 | |
3471 | if ((SV *)hv == &PL_sv_undef) |
3594 | if ((SV *)hv == &PL_sv_undef) |
3472 | { |
3595 | { |
3473 | PUSHMARK (SP); |
3596 | 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)); |
3597 | hv = (HV *)SvREFCNT_inc_NN (SvRV (sv)); |
|
|
3598 | SvREFCNT_dec (sv); |
3482 | } |
3599 | } |
3483 | |
3600 | |
3484 | { |
3601 | { |
3485 | struct coro *coro = SvSTATE_hv (hv); |
3602 | struct coro *coro = SvSTATE_hv (hv); |
3486 | |
3603 | |
… | |
… | |
3758 | { |
3875 | { |
3759 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
3876 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
3760 | coro_old_pp_sselect = 0; |
3877 | coro_old_pp_sselect = 0; |
3761 | } |
3878 | } |
3762 | |
3879 | |
3763 | |
|
|