… | |
… | |
16 | |
16 | |
17 | #ifdef WIN32 |
17 | #ifdef WIN32 |
18 | # undef setjmp |
18 | # undef setjmp |
19 | # undef longjmp |
19 | # undef longjmp |
20 | # undef _exit |
20 | # undef _exit |
21 | # define setjmp _setjmp // deep magic, don't ask |
21 | # define setjmp _setjmp /* deep magic */ |
22 | #else |
22 | #else |
23 | # include <inttypes.h> /* most portable stdint.h */ |
23 | # include <inttypes.h> /* most portable stdint.h */ |
24 | #endif |
24 | #endif |
25 | |
25 | |
26 | #ifdef HAVE_MMAP |
26 | #ifdef HAVE_MMAP |
… | |
… | |
120 | #endif |
120 | #endif |
121 | |
121 | |
122 | /* The next macros try to return the current stack pointer, in an as |
122 | /* The next macros try to return the current stack pointer, in an as |
123 | * portable way as possible. */ |
123 | * portable way as possible. */ |
124 | #if __GNUC__ >= 4 |
124 | #if __GNUC__ >= 4 |
|
|
125 | # define dSTACKLEVEL int stacklevel_dummy |
125 | # define dSTACKLEVEL void *stacklevel = __builtin_frame_address (0) |
126 | # define STACKLEVEL __builtin_frame_address (0) |
126 | #else |
127 | #else |
127 | # define dSTACKLEVEL volatile void *stacklevel = (volatile void *)&stacklevel |
128 | # define dSTACKLEVEL volatile void *stacklevel |
|
|
129 | # define STACKLEVEL ((void *)&stacklevel) |
128 | #endif |
130 | #endif |
129 | |
131 | |
130 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
132 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
131 | |
133 | |
132 | #if __GNUC__ >= 3 |
134 | #if __GNUC__ >= 3 |
… | |
… | |
143 | #define expect_true(expr) expect ((expr) != 0, 1) |
145 | #define expect_true(expr) expect ((expr) != 0, 1) |
144 | |
146 | |
145 | #define NOINLINE attribute ((noinline)) |
147 | #define NOINLINE attribute ((noinline)) |
146 | |
148 | |
147 | #include "CoroAPI.h" |
149 | #include "CoroAPI.h" |
|
|
150 | #define GCoroAPI (&coroapi) /* very sneaky */ |
148 | |
151 | |
149 | #ifdef USE_ITHREADS |
152 | #ifdef USE_ITHREADS |
150 | # if CORO_PTHREAD |
153 | # if CORO_PTHREAD |
151 | static void *coro_thx; |
154 | static void *coro_thx; |
152 | # endif |
155 | # endif |
153 | #endif |
156 | #endif |
154 | |
157 | |
155 | static double (*nvtime)(); /* so why doesn't it take void? */ |
158 | static double (*nvtime)(); /* so why doesn't it take void? */ |
|
|
159 | |
|
|
160 | /* we hijack an hopefully unused CV flag for our purposes */ |
|
|
161 | #define CVf_SLF 0x4000 |
|
|
162 | static OP *pp_slf (pTHX); |
156 | |
163 | |
157 | static U32 cctx_gen; |
164 | static U32 cctx_gen; |
158 | static size_t cctx_stacksize = CORO_STACKSIZE; |
165 | static size_t cctx_stacksize = CORO_STACKSIZE; |
159 | static struct CoroAPI coroapi; |
166 | static struct CoroAPI coroapi; |
160 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
167 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
… | |
… | |
250 | /* statistics */ |
257 | /* statistics */ |
251 | int usecount; /* number of transfers to this coro */ |
258 | int usecount; /* number of transfers to this coro */ |
252 | |
259 | |
253 | /* coro process data */ |
260 | /* coro process data */ |
254 | int prio; |
261 | int prio; |
255 | SV *throw; /* exception to be thrown */ |
262 | SV *except; /* exception to be thrown */ |
|
|
263 | SV *rouse_cb; |
256 | |
264 | |
257 | /* async_pool */ |
265 | /* async_pool */ |
258 | SV *saved_deffh; |
266 | SV *saved_deffh; |
259 | |
267 | |
260 | /* linked list */ |
268 | /* linked list */ |
… | |
… | |
266 | |
274 | |
267 | /* the following variables are effectively part of the perl context */ |
275 | /* the following variables are effectively part of the perl context */ |
268 | /* and get copied between struct coro and these variables */ |
276 | /* and get copied between struct coro and these variables */ |
269 | /* the mainr easonw e don't support windows process emulation */ |
277 | /* the mainr easonw e don't support windows process emulation */ |
270 | static struct CoroSLF slf_frame; /* the current slf frame */ |
278 | static struct CoroSLF slf_frame; /* the current slf frame */ |
271 | static SV *coro_throw; |
|
|
272 | |
279 | |
273 | /** Coro ********************************************************************/ |
280 | /** Coro ********************************************************************/ |
274 | |
281 | |
275 | #define PRIO_MAX 3 |
282 | #define PRIO_MAX 3 |
276 | #define PRIO_HIGH 1 |
283 | #define PRIO_HIGH 1 |
… | |
… | |
505 | |
512 | |
506 | PUTBACK; |
513 | PUTBACK; |
507 | } |
514 | } |
508 | |
515 | |
509 | slf_frame = c->slf_frame; |
516 | slf_frame = c->slf_frame; |
510 | coro_throw = c->throw; |
517 | CORO_THROW = c->except; |
511 | } |
518 | } |
512 | |
519 | |
513 | static void |
520 | static void |
514 | save_perl (pTHX_ Coro__State c) |
521 | save_perl (pTHX_ Coro__State c) |
515 | { |
522 | { |
516 | c->throw = coro_throw; |
523 | c->except = CORO_THROW; |
517 | c->slf_frame = slf_frame; |
524 | c->slf_frame = slf_frame; |
518 | |
525 | |
519 | { |
526 | { |
520 | dSP; |
527 | dSP; |
521 | I32 cxix = cxstack_ix; |
528 | I32 cxix = cxstack_ix; |
… | |
… | |
814 | slf_check_nop (pTHX_ struct CoroSLF *frame) |
821 | slf_check_nop (pTHX_ struct CoroSLF *frame) |
815 | { |
822 | { |
816 | return 0; |
823 | return 0; |
817 | } |
824 | } |
818 | |
825 | |
|
|
826 | static UNOP coro_setup_op; |
|
|
827 | |
819 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
828 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
820 | coro_setup (pTHX_ struct coro *coro) |
829 | coro_setup (pTHX_ struct coro *coro) |
821 | { |
830 | { |
822 | /* |
831 | /* |
823 | * emulate part of the perl startup here. |
832 | * emulate part of the perl startup here. |
… | |
… | |
851 | { |
860 | { |
852 | dSP; |
861 | dSP; |
853 | UNOP myop; |
862 | UNOP myop; |
854 | |
863 | |
855 | Zero (&myop, 1, UNOP); |
864 | Zero (&myop, 1, UNOP); |
856 | myop.op_next = Nullop; |
865 | myop.op_next = Nullop; |
857 | myop.op_flags = OPf_WANT_VOID; |
866 | myop.op_flags = OPf_WANT_VOID; |
858 | |
867 | |
859 | PUSHMARK (SP); |
868 | PUSHMARK (SP); |
860 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
869 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
861 | PUTBACK; |
870 | PUTBACK; |
… | |
… | |
868 | * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. |
877 | * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. |
869 | */ |
878 | */ |
870 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
879 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
871 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
880 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
872 | |
881 | |
873 | coro_throw = coro->throw; |
882 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
|
|
883 | coro_setup_op.op_next = PL_op; |
|
|
884 | coro_setup_op.op_type = OP_CUSTOM; |
|
|
885 | coro_setup_op.op_ppaddr = pp_slf; |
|
|
886 | /* no flags required, as an init function won't be called */ |
|
|
887 | |
|
|
888 | PL_op = (OP *)&coro_setup_op; |
|
|
889 | |
|
|
890 | /* copy throw, in case it was set before coro_setup */ |
|
|
891 | CORO_THROW = coro->except; |
874 | } |
892 | } |
875 | |
893 | |
876 | static void |
894 | static void |
877 | coro_destruct (pTHX_ struct coro *coro) |
895 | coro_destruct (pTHX_ struct coro *coro) |
878 | { |
896 | { |
… | |
… | |
901 | SvREFCNT_dec (GvSV (irsgv)); |
919 | SvREFCNT_dec (GvSV (irsgv)); |
902 | |
920 | |
903 | SvREFCNT_dec (PL_diehook); |
921 | SvREFCNT_dec (PL_diehook); |
904 | SvREFCNT_dec (PL_warnhook); |
922 | SvREFCNT_dec (PL_warnhook); |
905 | |
923 | |
|
|
924 | SvREFCNT_dec (CORO_THROW); |
906 | SvREFCNT_dec (coro->saved_deffh); |
925 | SvREFCNT_dec (coro->saved_deffh); |
907 | SvREFCNT_dec (coro_throw); |
926 | SvREFCNT_dec (coro->rouse_cb); |
908 | |
927 | |
909 | coro_destruct_stacks (aTHX); |
928 | coro_destruct_stacks (aTHX); |
910 | } |
929 | } |
911 | |
930 | |
912 | INLINE void |
931 | INLINE void |
… | |
… | |
1041 | |
1060 | |
1042 | TAINT_NOT; |
1061 | TAINT_NOT; |
1043 | return 0; |
1062 | return 0; |
1044 | } |
1063 | } |
1045 | |
1064 | |
|
|
1065 | static struct coro_cctx *cctx_ssl_cctx; |
|
|
1066 | static struct CoroSLF cctx_ssl_frame; |
|
|
1067 | |
1046 | static void |
1068 | static void |
1047 | prepare_set_stacklevel (struct coro_transfer_args *ta, struct coro_cctx *cctx) |
1069 | slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) |
1048 | { |
1070 | { |
1049 | ta->prev = (struct coro *)cctx; |
1071 | ta->prev = (struct coro *)cctx_ssl_cctx; |
1050 | ta->next = 0; |
1072 | ta->next = 0; |
1051 | } |
1073 | } |
1052 | |
1074 | |
1053 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
1075 | static int |
1054 | /* _cctx_init should be careful, as it could be called at almost any time */ |
1076 | slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) |
1055 | /* during execution of a perl program */ |
1077 | { |
1056 | /* also initialises PL_top_env */ |
1078 | *frame = cctx_ssl_frame; |
|
|
1079 | |
|
|
1080 | return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ |
|
|
1081 | } |
|
|
1082 | |
|
|
1083 | /* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ |
1057 | static void NOINLINE |
1084 | static void NOINLINE |
1058 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1085 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1059 | { |
1086 | { |
1060 | dSP; |
|
|
1061 | UNOP myop; |
|
|
1062 | |
|
|
1063 | PL_top_env = &PL_start_env; |
1087 | PL_top_env = &PL_start_env; |
1064 | |
1088 | |
1065 | if (cctx->flags & CC_TRACE) |
1089 | if (cctx->flags & CC_TRACE) |
1066 | PL_runops = runops_trace; |
1090 | PL_runops = runops_trace; |
1067 | |
1091 | |
1068 | Zero (&myop, 1, UNOP); |
1092 | /* we already must be executing an SLF op, there is no other valid way |
1069 | myop.op_next = PL_op; |
1093 | * that can lead to creation of a new cctx */ |
1070 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
1094 | assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", |
|
|
1095 | slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); |
1071 | |
1096 | |
1072 | PUSHMARK (SP); |
1097 | /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ |
1073 | EXTEND (SP, 2); |
1098 | cctx_ssl_cctx = cctx; |
1074 | PUSHs (sv_2mortal (newSViv ((IV)cctx))); |
1099 | cctx_ssl_frame = slf_frame; |
1075 | PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
1100 | |
1076 | PUTBACK; |
1101 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1077 | PL_op = (OP *)&myop; |
1102 | slf_frame.check = slf_check_set_stacklevel; |
1078 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
|
|
1079 | SPAGAIN; |
|
|
1080 | } |
1103 | } |
1081 | |
1104 | |
1082 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1105 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1083 | INLINE void |
1106 | INLINE void |
1084 | transfer_tail (pTHX) |
1107 | transfer_tail (pTHX) |
… | |
… | |
1300 | dSTACKLEVEL; |
1323 | dSTACKLEVEL; |
1301 | |
1324 | |
1302 | /* sometimes transfer is only called to set idle_sp */ |
1325 | /* sometimes transfer is only called to set idle_sp */ |
1303 | if (expect_false (!next)) |
1326 | if (expect_false (!next)) |
1304 | { |
1327 | { |
1305 | ((coro_cctx *)prev)->idle_sp = (void *)stacklevel; |
1328 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1306 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1329 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1307 | } |
1330 | } |
1308 | else if (expect_true (prev != next)) |
1331 | else if (expect_true (prev != next)) |
1309 | { |
1332 | { |
1310 | coro_cctx *prev__cctx; |
1333 | coro_cctx *prev__cctx; |
… | |
… | |
1335 | |
1358 | |
1336 | prev__cctx = prev->cctx; |
1359 | prev__cctx = prev->cctx; |
1337 | |
1360 | |
1338 | /* possibly untie and reuse the cctx */ |
1361 | /* possibly untie and reuse the cctx */ |
1339 | if (expect_true ( |
1362 | if (expect_true ( |
1340 | prev__cctx->idle_sp == (void *)stacklevel |
1363 | prev__cctx->idle_sp == STACKLEVEL |
1341 | && !(prev__cctx->flags & CC_TRACE) |
1364 | && !(prev__cctx->flags & CC_TRACE) |
1342 | && !force_cctx |
1365 | && !force_cctx |
1343 | )) |
1366 | )) |
1344 | { |
1367 | { |
1345 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1368 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
… | |
… | |
1476 | |
1499 | |
1477 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1500 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1478 | TRANSFER (ta, 1); |
1501 | TRANSFER (ta, 1); |
1479 | } |
1502 | } |
1480 | |
1503 | |
|
|
1504 | /*****************************************************************************/ |
|
|
1505 | /* gensub: simple closure generation utility */ |
|
|
1506 | |
|
|
1507 | #define GENSUB_ARG CvXSUBANY (cv).any_ptr |
|
|
1508 | |
|
|
1509 | /* create a closure from XS, returns a code reference */ |
|
|
1510 | /* the arg can be accessed via GENSUB_ARG from the callback */ |
|
|
1511 | /* the callback must use dXSARGS/XSRETURN */ |
|
|
1512 | static SV * |
|
|
1513 | gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg) |
|
|
1514 | { |
|
|
1515 | CV *cv = (CV *)newSV (0); |
|
|
1516 | |
|
|
1517 | sv_upgrade ((SV *)cv, SVt_PVCV); |
|
|
1518 | |
|
|
1519 | CvANON_on (cv); |
|
|
1520 | CvISXSUB_on (cv); |
|
|
1521 | CvXSUB (cv) = xsub; |
|
|
1522 | GENSUB_ARG = arg; |
|
|
1523 | |
|
|
1524 | return newRV_noinc ((SV *)cv); |
|
|
1525 | } |
|
|
1526 | |
1481 | /** Coro ********************************************************************/ |
1527 | /** Coro ********************************************************************/ |
1482 | |
1528 | |
1483 | INLINE void |
1529 | INLINE void |
1484 | coro_enq (pTHX_ struct coro *coro) |
1530 | coro_enq (pTHX_ struct coro *coro) |
1485 | { |
1531 | { |
… | |
… | |
1528 | ENTER; |
1574 | ENTER; |
1529 | SAVETMPS; |
1575 | SAVETMPS; |
1530 | |
1576 | |
1531 | PUSHMARK (SP); |
1577 | PUSHMARK (SP); |
1532 | PUTBACK; |
1578 | PUTBACK; |
1533 | call_sv (sv_hook, G_DISCARD); |
1579 | call_sv (sv_hook, G_VOID | G_DISCARD); |
1534 | SPAGAIN; |
|
|
1535 | |
1580 | |
1536 | FREETMPS; |
1581 | FREETMPS; |
1537 | LEAVE; |
1582 | LEAVE; |
1538 | } |
1583 | } |
1539 | |
1584 | |
… | |
… | |
1566 | ENTER; |
1611 | ENTER; |
1567 | SAVETMPS; |
1612 | SAVETMPS; |
1568 | |
1613 | |
1569 | PUSHMARK (SP); |
1614 | PUSHMARK (SP); |
1570 | PUTBACK; |
1615 | PUTBACK; |
1571 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
1616 | call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); |
1572 | SPAGAIN; |
|
|
1573 | |
1617 | |
1574 | FREETMPS; |
1618 | FREETMPS; |
1575 | LEAVE; |
1619 | LEAVE; |
1576 | continue; |
1620 | continue; |
1577 | } |
1621 | } |
… | |
… | |
1687 | coro->slot->runops = RUNOPS_DEFAULT; |
1731 | coro->slot->runops = RUNOPS_DEFAULT; |
1688 | } |
1732 | } |
1689 | } |
1733 | } |
1690 | |
1734 | |
1691 | /*****************************************************************************/ |
1735 | /*****************************************************************************/ |
|
|
1736 | /* rouse callback */ |
|
|
1737 | |
|
|
1738 | #define CORO_MAGIC_type_rouse PERL_MAGIC_ext |
|
|
1739 | |
|
|
1740 | static void |
|
|
1741 | coro_rouse_callback (pTHX_ CV *cv) |
|
|
1742 | { |
|
|
1743 | dXSARGS; |
|
|
1744 | SV *data = (SV *)GENSUB_ARG; |
|
|
1745 | |
|
|
1746 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
|
|
1747 | { |
|
|
1748 | /* first call, set args */ |
|
|
1749 | int i; |
|
|
1750 | AV *av = newAV (); |
|
|
1751 | SV *coro = SvRV (data); |
|
|
1752 | |
|
|
1753 | SvRV_set (data, (SV *)av); |
|
|
1754 | api_ready (aTHX_ coro); |
|
|
1755 | SvREFCNT_dec (coro); |
|
|
1756 | |
|
|
1757 | /* better take a full copy of the arguments */ |
|
|
1758 | while (items--) |
|
|
1759 | av_store (av, items, newSVsv (ST (items))); |
|
|
1760 | } |
|
|
1761 | |
|
|
1762 | XSRETURN_EMPTY; |
|
|
1763 | } |
|
|
1764 | |
|
|
1765 | static int |
|
|
1766 | slf_check_rouse_wait (pTHX_ struct CoroSLF *frame) |
|
|
1767 | { |
|
|
1768 | SV *data = (SV *)frame->data; |
|
|
1769 | |
|
|
1770 | if (CORO_THROW) |
|
|
1771 | return 0; |
|
|
1772 | |
|
|
1773 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
|
|
1774 | return 1; |
|
|
1775 | |
|
|
1776 | /* now push all results on the stack */ |
|
|
1777 | { |
|
|
1778 | dSP; |
|
|
1779 | AV *av = (AV *)SvRV (data); |
|
|
1780 | int i; |
|
|
1781 | |
|
|
1782 | EXTEND (SP, AvFILLp (av) + 1); |
|
|
1783 | for (i = 0; i <= AvFILLp (av); ++i) |
|
|
1784 | PUSHs (sv_2mortal (AvARRAY (av)[i])); |
|
|
1785 | |
|
|
1786 | /* we have stolen the elements, so ste length to zero and free */ |
|
|
1787 | AvFILLp (av) = -1; |
|
|
1788 | av_undef (av); |
|
|
1789 | |
|
|
1790 | PUTBACK; |
|
|
1791 | } |
|
|
1792 | |
|
|
1793 | return 0; |
|
|
1794 | } |
|
|
1795 | |
|
|
1796 | static void |
|
|
1797 | slf_init_rouse_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1798 | { |
|
|
1799 | SV *cb; |
|
|
1800 | |
|
|
1801 | if (items) |
|
|
1802 | cb = arg [0]; |
|
|
1803 | else |
|
|
1804 | { |
|
|
1805 | struct coro *coro = SvSTATE_current; |
|
|
1806 | |
|
|
1807 | if (!coro->rouse_cb) |
|
|
1808 | croak ("Coro::rouse_wait called without rouse callback, and no default rouse callback found either,"); |
|
|
1809 | |
|
|
1810 | cb = sv_2mortal (coro->rouse_cb); |
|
|
1811 | coro->rouse_cb = 0; |
|
|
1812 | } |
|
|
1813 | |
|
|
1814 | if (!SvROK (cb) |
|
|
1815 | || SvTYPE (SvRV (cb)) != SVt_PVCV |
|
|
1816 | || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) |
|
|
1817 | croak ("Coro::rouse_wait called with illegal callback argument,"); |
|
|
1818 | |
|
|
1819 | { |
|
|
1820 | CV *cv = (CV *)SvRV (cb); /* for GENSUB_ARG */ |
|
|
1821 | SV *data = (SV *)GENSUB_ARG; |
|
|
1822 | |
|
|
1823 | frame->data = (void *)data; |
|
|
1824 | frame->prepare = SvTYPE (SvRV (data)) == SVt_PVAV ? prepare_nop : prepare_schedule; |
|
|
1825 | frame->check = slf_check_rouse_wait; |
|
|
1826 | } |
|
|
1827 | } |
|
|
1828 | |
|
|
1829 | static SV * |
|
|
1830 | coro_new_rouse_cb (pTHX) |
|
|
1831 | { |
|
|
1832 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1833 | struct coro *coro = SvSTATE_hv (hv); |
|
|
1834 | SV *data = newRV_inc ((SV *)hv); |
|
|
1835 | SV *cb = gensub (aTHX_ coro_rouse_callback, (void *)data); |
|
|
1836 | |
|
|
1837 | sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0); |
|
|
1838 | SvREFCNT_dec (data); /* magicext increases the refcount */ |
|
|
1839 | |
|
|
1840 | SvREFCNT_dec (coro->rouse_cb); |
|
|
1841 | coro->rouse_cb = SvREFCNT_inc_NN (cb); |
|
|
1842 | |
|
|
1843 | return cb; |
|
|
1844 | } |
|
|
1845 | |
|
|
1846 | /*****************************************************************************/ |
1692 | /* schedule-like-function opcode (SLF) */ |
1847 | /* schedule-like-function opcode (SLF) */ |
1693 | |
1848 | |
1694 | static UNOP slf_restore; /* restore stack as entersub did, for first-re-run */ |
1849 | static UNOP slf_restore; /* restore stack as entersub did, for first-re-run */ |
1695 | static const CV *slf_cv; |
1850 | static const CV *slf_cv; |
1696 | static SV **slf_argv; |
1851 | static SV **slf_argv; |
… | |
… | |
1717 | |
1872 | |
1718 | RETURNOP (slf_restore.op_first); |
1873 | RETURNOP (slf_restore.op_first); |
1719 | } |
1874 | } |
1720 | |
1875 | |
1721 | static void |
1876 | static void |
1722 | slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) |
|
|
1723 | { |
|
|
1724 | prepare_set_stacklevel (ta, (struct coro_cctx *)slf_frame.data); |
|
|
1725 | } |
|
|
1726 | |
|
|
1727 | static void |
|
|
1728 | slf_init_set_stacklevel (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1729 | { |
|
|
1730 | assert (("FATAL: set_stacklevel needs the coro cctx as sole argument", items == 1)); |
|
|
1731 | |
|
|
1732 | frame->prepare = slf_prepare_set_stacklevel; |
|
|
1733 | frame->check = slf_check_nop; |
|
|
1734 | frame->data = (void *)SvIV (arg [0]); |
|
|
1735 | } |
|
|
1736 | |
|
|
1737 | static void |
|
|
1738 | slf_prepare_transfer (pTHX_ struct coro_transfer_args *ta) |
1877 | slf_prepare_transfer (pTHX_ struct coro_transfer_args *ta) |
1739 | { |
1878 | { |
1740 | SV **arg = (SV **)slf_frame.data; |
1879 | SV **arg = (SV **)slf_frame.data; |
1741 | |
1880 | |
1742 | prepare_transfer (aTHX_ ta, arg [0], arg [1]); |
1881 | prepare_transfer (aTHX_ ta, arg [0], arg [1]); |
… | |
… | |
1771 | slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1910 | slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1772 | { |
1911 | { |
1773 | frame->prepare = prepare_cede_notself; |
1912 | frame->prepare = prepare_cede_notself; |
1774 | frame->check = slf_check_nop; |
1913 | frame->check = slf_check_nop; |
1775 | } |
1914 | } |
1776 | |
|
|
1777 | /* we hijack an hopefully unused CV flag for our purposes */ |
|
|
1778 | #define CVf_SLF 0x4000 |
|
|
1779 | |
1915 | |
1780 | /* |
1916 | /* |
1781 | * these not obviously related functions are all rolled into one |
1917 | * these not obviously related functions are all rolled into one |
1782 | * function to increase chances that they all will call transfer with the same |
1918 | * function to increase chances that they all will call transfer with the same |
1783 | * stack offset |
1919 | * stack offset |
… | |
… | |
1839 | } |
1975 | } |
1840 | while (slf_frame.check (aTHX_ &slf_frame)); |
1976 | while (slf_frame.check (aTHX_ &slf_frame)); |
1841 | |
1977 | |
1842 | slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ |
1978 | slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ |
1843 | |
1979 | |
1844 | /* return value handling - mostly like entersub */ |
|
|
1845 | { |
|
|
1846 | dSP; |
|
|
1847 | SV **bot = PL_stack_base + checkmark; |
|
|
1848 | int gimme = GIMME_V; |
|
|
1849 | |
|
|
1850 | /* make sure we put something on the stack in scalar context */ |
|
|
1851 | if (gimme == G_SCALAR) |
|
|
1852 | { |
|
|
1853 | if (sp == bot) |
|
|
1854 | XPUSHs (&PL_sv_undef); |
|
|
1855 | |
|
|
1856 | SP = bot + 1; |
|
|
1857 | } |
|
|
1858 | |
|
|
1859 | PUTBACK; |
|
|
1860 | } |
|
|
1861 | |
|
|
1862 | /* exception handling */ |
1980 | /* exception handling */ |
1863 | if (expect_false (coro_throw)) |
1981 | if (expect_false (CORO_THROW)) |
1864 | { |
1982 | { |
1865 | SV *exception = sv_2mortal (coro_throw); |
1983 | SV *exception = sv_2mortal (CORO_THROW); |
1866 | |
1984 | |
1867 | coro_throw = 0; |
1985 | CORO_THROW = 0; |
1868 | sv_setsv (ERRSV, exception); |
1986 | sv_setsv (ERRSV, exception); |
1869 | croak (0); |
1987 | croak (0); |
|
|
1988 | } |
|
|
1989 | |
|
|
1990 | /* return value handling - mostly like entersub */ |
|
|
1991 | /* make sure we put something on the stack in scalar context */ |
|
|
1992 | if (GIMME_V == G_SCALAR) |
|
|
1993 | { |
|
|
1994 | dSP; |
|
|
1995 | SV **bot = PL_stack_base + checkmark; |
|
|
1996 | |
|
|
1997 | if (sp == bot) /* too few, push undef */ |
|
|
1998 | bot [1] = &PL_sv_undef; |
|
|
1999 | else if (sp != bot + 1) /* too many, take last one */ |
|
|
2000 | bot [1] = *sp; |
|
|
2001 | |
|
|
2002 | SP = bot + 1; |
|
|
2003 | |
|
|
2004 | PUTBACK; |
1870 | } |
2005 | } |
1871 | |
2006 | |
1872 | return NORMAL; |
2007 | return NORMAL; |
1873 | } |
2008 | } |
1874 | |
2009 | |
… | |
… | |
1996 | PerlIOBuf_get_cnt, |
2131 | PerlIOBuf_get_cnt, |
1997 | PerlIOBuf_set_ptrcnt, |
2132 | PerlIOBuf_set_ptrcnt, |
1998 | }; |
2133 | }; |
1999 | |
2134 | |
2000 | /*****************************************************************************/ |
2135 | /*****************************************************************************/ |
|
|
2136 | /* Coro::Semaphore & Coro::Signal */ |
|
|
2137 | |
|
|
2138 | static SV * |
|
|
2139 | coro_waitarray_new (pTHX_ int count) |
|
|
2140 | { |
|
|
2141 | /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ |
|
|
2142 | AV *av = newAV (); |
|
|
2143 | SV **ary; |
|
|
2144 | |
|
|
2145 | /* unfortunately, building manually saves memory */ |
|
|
2146 | Newx (ary, 2, SV *); |
|
|
2147 | AvALLOC (av) = ary; |
|
|
2148 | SvPV_set ((SV *)av, (char *)ary); /* 5.8.8 needs this syntax instead of AvARRAY = ary */ |
|
|
2149 | AvMAX (av) = 1; |
|
|
2150 | AvFILLp (av) = 0; |
|
|
2151 | ary [0] = newSViv (count); |
|
|
2152 | |
|
|
2153 | return newRV_noinc ((SV *)av); |
|
|
2154 | } |
|
|
2155 | |
2001 | /* Coro::Semaphore */ |
2156 | /* semaphore */ |
2002 | |
2157 | |
2003 | static void |
2158 | static void |
2004 | coro_semaphore_adjust (pTHX_ AV *av, IV adjust) |
2159 | coro_semaphore_adjust (pTHX_ AV *av, IV adjust) |
2005 | { |
2160 | { |
2006 | SV *count_sv = AvARRAY (av)[0]; |
2161 | SV *count_sv = AvARRAY (av)[0]; |
… | |
… | |
2036 | /* call $sem->adjust (0) to possibly wake up some other waiters */ |
2191 | /* call $sem->adjust (0) to possibly wake up some other waiters */ |
2037 | coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); |
2192 | coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); |
2038 | } |
2193 | } |
2039 | |
2194 | |
2040 | static int |
2195 | static int |
2041 | slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) |
2196 | slf_check_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, int acquire) |
2042 | { |
2197 | { |
2043 | AV *av = (AV *)frame->data; |
2198 | AV *av = (AV *)frame->data; |
2044 | SV *count_sv = AvARRAY (av)[0]; |
2199 | SV *count_sv = AvARRAY (av)[0]; |
2045 | |
2200 | |
|
|
2201 | /* if we are about to throw, don't actually acquire the lock, just throw */ |
|
|
2202 | if (CORO_THROW) |
|
|
2203 | return 0; |
2046 | if (SvIVX (count_sv) > 0) |
2204 | else if (SvIVX (count_sv) > 0) |
2047 | { |
2205 | { |
2048 | SvSTATE_current->on_destroy = 0; |
2206 | SvSTATE_current->on_destroy = 0; |
|
|
2207 | |
|
|
2208 | if (acquire) |
2049 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2209 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
|
|
2210 | else |
|
|
2211 | coro_semaphore_adjust (aTHX_ av, 0); |
|
|
2212 | |
2050 | return 0; |
2213 | return 0; |
2051 | } |
2214 | } |
2052 | else |
2215 | else |
2053 | { |
2216 | { |
2054 | int i; |
2217 | int i; |
… | |
… | |
2063 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2226 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2064 | return 1; |
2227 | return 1; |
2065 | } |
2228 | } |
2066 | } |
2229 | } |
2067 | |
2230 | |
2068 | static void |
2231 | static int |
|
|
2232 | slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) |
|
|
2233 | { |
|
|
2234 | return slf_check_semaphore_down_or_wait (aTHX_ frame, 1); |
|
|
2235 | } |
|
|
2236 | |
|
|
2237 | static int |
|
|
2238 | slf_check_semaphore_wait (pTHX_ struct CoroSLF *frame) |
|
|
2239 | { |
|
|
2240 | return slf_check_semaphore_down_or_wait (aTHX_ frame, 0); |
|
|
2241 | } |
|
|
2242 | |
|
|
2243 | static void |
2069 | slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2244 | slf_init_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2070 | { |
2245 | { |
2071 | AV *av = (AV *)SvRV (arg [0]); |
2246 | AV *av = (AV *)SvRV (arg [0]); |
2072 | |
2247 | |
2073 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2248 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2074 | { |
2249 | { |
2075 | frame->data = (void *)av; |
2250 | frame->data = (void *)av; |
2076 | frame->prepare = prepare_nop; |
2251 | frame->prepare = prepare_nop; |
2077 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
|
|
2078 | } |
2252 | } |
2079 | else |
2253 | else |
2080 | { |
2254 | { |
2081 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2255 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2082 | |
2256 | |
2083 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
2257 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
2084 | frame->prepare = prepare_schedule; |
2258 | frame->prepare = prepare_schedule; |
2085 | |
2259 | |
2086 | /* to avoid race conditions when a woken-up coro gets terminated */ |
2260 | /* to avoid race conditions when a woken-up coro gets terminated */ |
2087 | /* we arrange for a temporary on_destroy that calls adjust (0) */ |
2261 | /* we arrange for a temporary on_destroy that calls adjust (0) */ |
2088 | assert (!SvSTATE_current->on_destroy);//D |
|
|
2089 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
2262 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
2090 | } |
2263 | } |
|
|
2264 | } |
2091 | |
2265 | |
|
|
2266 | static void |
|
|
2267 | slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2268 | { |
|
|
2269 | slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); |
2092 | frame->check = slf_check_semaphore_down; |
2270 | frame->check = slf_check_semaphore_down; |
2093 | |
|
|
2094 | } |
2271 | } |
2095 | |
2272 | |
2096 | /*****************************************************************************/ |
2273 | static void |
2097 | /* gensub: simple closure generation utility */ |
2274 | slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2098 | |
|
|
2099 | #define GENSUB_ARG CvXSUBANY (cv).any_ptr |
|
|
2100 | |
|
|
2101 | /* create a closure from XS, returns a code reference */ |
|
|
2102 | /* the arg can be accessed via GENSUB_ARG from the callback */ |
|
|
2103 | /* the callback must use dXSARGS/XSRETURN */ |
|
|
2104 | static SV * |
|
|
2105 | gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg) |
|
|
2106 | { |
2275 | { |
2107 | CV *cv = (CV *)newSV (0); |
2276 | slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); |
|
|
2277 | frame->check = slf_check_semaphore_wait; |
|
|
2278 | } |
2108 | |
2279 | |
2109 | sv_upgrade ((SV *)cv, SVt_PVCV); |
2280 | /* signal */ |
2110 | |
2281 | |
2111 | CvANON_on (cv); |
2282 | static void |
2112 | CvISXSUB_on (cv); |
2283 | coro_signal_wake (pTHX_ AV *av, int count) |
2113 | CvXSUB (cv) = xsub; |
2284 | { |
2114 | GENSUB_ARG = arg; |
2285 | SvIVX (AvARRAY (av)[0]) = 0; |
2115 | |
2286 | |
2116 | return newRV_noinc ((SV *)cv); |
2287 | /* now signal count waiters */ |
|
|
2288 | while (count > 0 && AvFILLp (av) > 0) |
|
|
2289 | { |
|
|
2290 | SV *cb; |
|
|
2291 | |
|
|
2292 | /* swap first two elements so we can shift a waiter */ |
|
|
2293 | cb = AvARRAY (av)[0]; |
|
|
2294 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
|
|
2295 | AvARRAY (av)[1] = cb; |
|
|
2296 | |
|
|
2297 | cb = av_shift (av); |
|
|
2298 | |
|
|
2299 | api_ready (aTHX_ cb); |
|
|
2300 | sv_setiv (cb, 0); /* signal waiter */ |
|
|
2301 | SvREFCNT_dec (cb); |
|
|
2302 | |
|
|
2303 | --count; |
|
|
2304 | } |
|
|
2305 | } |
|
|
2306 | |
|
|
2307 | static int |
|
|
2308 | slf_check_signal_wait (pTHX_ struct CoroSLF *frame) |
|
|
2309 | { |
|
|
2310 | /* if we are about to throw, also stop waiting */ |
|
|
2311 | return SvROK ((SV *)frame->data) && !CORO_THROW; |
|
|
2312 | } |
|
|
2313 | |
|
|
2314 | static void |
|
|
2315 | slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2316 | { |
|
|
2317 | AV *av = (AV *)SvRV (arg [0]); |
|
|
2318 | |
|
|
2319 | if (SvIVX (AvARRAY (av)[0])) |
|
|
2320 | { |
|
|
2321 | SvIVX (AvARRAY (av)[0]) = 0; |
|
|
2322 | frame->prepare = prepare_nop; |
|
|
2323 | frame->check = slf_check_nop; |
|
|
2324 | } |
|
|
2325 | else |
|
|
2326 | { |
|
|
2327 | SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */ |
|
|
2328 | |
|
|
2329 | av_push (av, waiter); |
|
|
2330 | |
|
|
2331 | frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */ |
|
|
2332 | frame->prepare = prepare_schedule; |
|
|
2333 | frame->check = slf_check_signal_wait; |
|
|
2334 | } |
2117 | } |
2335 | } |
2118 | |
2336 | |
2119 | /*****************************************************************************/ |
2337 | /*****************************************************************************/ |
2120 | /* Coro::AIO */ |
2338 | /* Coro::AIO */ |
2121 | |
2339 | |
… | |
… | |
2170 | |
2388 | |
2171 | static int |
2389 | static int |
2172 | slf_check_aio_req (pTHX_ struct CoroSLF *frame) |
2390 | slf_check_aio_req (pTHX_ struct CoroSLF *frame) |
2173 | { |
2391 | { |
2174 | AV *state = (AV *)frame->data; |
2392 | AV *state = (AV *)frame->data; |
|
|
2393 | |
|
|
2394 | /* if we are about to throw, return early */ |
|
|
2395 | /* this does not cancel the aio request, but at least */ |
|
|
2396 | /* it quickly returns */ |
|
|
2397 | if (CORO_THROW) |
|
|
2398 | return 0; |
2175 | |
2399 | |
2176 | /* one element that is an RV? repeat! */ |
2400 | /* one element that is an RV? repeat! */ |
2177 | if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) |
2401 | if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) |
2178 | return 1; |
2402 | return 1; |
2179 | |
2403 | |
… | |
… | |
2376 | for (i = 1; i < items; i++) |
2600 | for (i = 1; i < items; i++) |
2377 | av_push (coro->args, newSVsv (ST (i))); |
2601 | av_push (coro->args, newSVsv (ST (i))); |
2378 | } |
2602 | } |
2379 | OUTPUT: |
2603 | OUTPUT: |
2380 | RETVAL |
2604 | RETVAL |
2381 | |
|
|
2382 | void |
|
|
2383 | _set_stacklevel (...) |
|
|
2384 | CODE: |
|
|
2385 | CORO_EXECUTE_SLF_XS (slf_init_set_stacklevel); |
|
|
2386 | |
2605 | |
2387 | void |
2606 | void |
2388 | transfer (...) |
2607 | transfer (...) |
2389 | PROTOTYPE: $$ |
2608 | PROTOTYPE: $$ |
2390 | CODE: |
2609 | CODE: |
… | |
… | |
2516 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
2735 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
2517 | PROTOTYPE: $;$ |
2736 | PROTOTYPE: $;$ |
2518 | CODE: |
2737 | CODE: |
2519 | { |
2738 | { |
2520 | struct coro *current = SvSTATE_current; |
2739 | struct coro *current = SvSTATE_current; |
2521 | SV **throwp = self == current ? &coro_throw : &self->throw; |
2740 | SV **throwp = self == current ? &CORO_THROW : &self->except; |
2522 | SvREFCNT_dec (*throwp); |
2741 | SvREFCNT_dec (*throwp); |
2523 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
2742 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
2524 | } |
2743 | } |
2525 | |
2744 | |
2526 | void |
2745 | void |
… | |
… | |
2615 | coroapi.ready = api_ready; |
2834 | coroapi.ready = api_ready; |
2616 | coroapi.is_ready = api_is_ready; |
2835 | coroapi.is_ready = api_is_ready; |
2617 | coroapi.nready = coro_nready; |
2836 | coroapi.nready = coro_nready; |
2618 | coroapi.current = coro_current; |
2837 | coroapi.current = coro_current; |
2619 | |
2838 | |
2620 | GCoroAPI = &coroapi; |
2839 | /*GCoroAPI = &coroapi;*/ |
2621 | sv_setiv (sv, (IV)&coroapi); |
2840 | sv_setiv (sv, (IV)&coroapi); |
2622 | SvREADONLY_on (sv); |
2841 | SvREADONLY_on (sv); |
2623 | } |
2842 | } |
2624 | } |
2843 | } |
2625 | |
2844 | |
… | |
… | |
2761 | api_trace (aTHX_ coro_current, 0); |
2980 | api_trace (aTHX_ coro_current, 0); |
2762 | |
2981 | |
2763 | av_push (av_async_pool, newSVsv (coro_current)); |
2982 | av_push (av_async_pool, newSVsv (coro_current)); |
2764 | } |
2983 | } |
2765 | |
2984 | |
|
|
2985 | SV * |
|
|
2986 | rouse_cb () |
|
|
2987 | PROTOTYPE: |
|
|
2988 | CODE: |
|
|
2989 | RETVAL = coro_new_rouse_cb (aTHX); |
|
|
2990 | OUTPUT: |
|
|
2991 | RETVAL |
|
|
2992 | |
|
|
2993 | void |
|
|
2994 | rouse_wait (SV *cb = 0) |
|
|
2995 | PROTOTYPE: ;$ |
|
|
2996 | PPCODE: |
|
|
2997 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
|
|
2998 | |
2766 | |
2999 | |
2767 | MODULE = Coro::State PACKAGE = PerlIO::cede |
3000 | MODULE = Coro::State PACKAGE = PerlIO::cede |
2768 | |
3001 | |
2769 | BOOT: |
3002 | BOOT: |
2770 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
3003 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
2771 | |
3004 | |
2772 | |
3005 | |
2773 | MODULE = Coro::State PACKAGE = Coro::Semaphore |
3006 | MODULE = Coro::State PACKAGE = Coro::Semaphore |
2774 | |
3007 | |
2775 | SV * |
3008 | SV * |
2776 | new (SV *klass, SV *count_ = 0) |
3009 | new (SV *klass, SV *count = 0) |
2777 | CODE: |
3010 | CODE: |
2778 | { |
3011 | RETVAL = sv_bless ( |
2779 | /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ |
3012 | coro_waitarray_new (aTHX_ count && SvOK (count) ? SvIV (count) : 1), |
2780 | AV *av = newAV (); |
3013 | GvSTASH (CvGV (cv)) |
2781 | av_push (av, newSViv (count_ && SvOK (count_) ? SvIV (count_) : 1)); |
3014 | ); |
2782 | RETVAL = sv_bless (newRV_noinc ((SV *)av), GvSTASH (CvGV (cv))); |
3015 | OUTPUT: |
2783 | } |
3016 | RETVAL |
|
|
3017 | |
|
|
3018 | # helper for Coro::Channel |
|
|
3019 | SV * |
|
|
3020 | _alloc (int count) |
|
|
3021 | CODE: |
|
|
3022 | RETVAL = coro_waitarray_new (aTHX_ count); |
2784 | OUTPUT: |
3023 | OUTPUT: |
2785 | RETVAL |
3024 | RETVAL |
2786 | |
3025 | |
2787 | SV * |
3026 | SV * |
2788 | count (SV *self) |
3027 | count (SV *self) |
… | |
… | |
2800 | |
3039 | |
2801 | void |
3040 | void |
2802 | down (SV *self) |
3041 | down (SV *self) |
2803 | CODE: |
3042 | CODE: |
2804 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
3043 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
|
|
3044 | |
|
|
3045 | void |
|
|
3046 | wait (SV *self) |
|
|
3047 | CODE: |
|
|
3048 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait); |
2805 | |
3049 | |
2806 | void |
3050 | void |
2807 | try (SV *self) |
3051 | try (SV *self) |
2808 | PPCODE: |
3052 | PPCODE: |
2809 | { |
3053 | { |
… | |
… | |
2821 | XSRETURN_NO; |
3065 | XSRETURN_NO; |
2822 | } |
3066 | } |
2823 | |
3067 | |
2824 | void |
3068 | void |
2825 | waiters (SV *self) |
3069 | waiters (SV *self) |
2826 | CODE: |
3070 | PPCODE: |
2827 | { |
3071 | { |
2828 | AV *av = (AV *)SvRV (self); |
3072 | AV *av = (AV *)SvRV (self); |
|
|
3073 | int wcount = AvFILLp (av) + 1 - 1; |
2829 | |
3074 | |
2830 | if (GIMME_V == G_SCALAR) |
3075 | if (GIMME_V == G_SCALAR) |
2831 | XPUSHs (sv_2mortal (newSVsv (AvARRAY (av)[0]))); |
3076 | XPUSHs (sv_2mortal (newSViv (wcount))); |
2832 | else |
3077 | else |
2833 | { |
3078 | { |
2834 | int i; |
3079 | int i; |
2835 | EXTEND (SP, AvFILLp (av) + 1 - 1); |
3080 | EXTEND (SP, wcount); |
2836 | for (i = 1; i <= AvFILLp (av); ++i) |
3081 | for (i = 1; i <= wcount; ++i) |
2837 | PUSHs (newSVsv (AvARRAY (av)[i])); |
3082 | PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); |
2838 | } |
3083 | } |
2839 | } |
3084 | } |
|
|
3085 | |
|
|
3086 | MODULE = Coro::State PACKAGE = Coro::Signal |
|
|
3087 | |
|
|
3088 | SV * |
|
|
3089 | new (SV *klass) |
|
|
3090 | CODE: |
|
|
3091 | RETVAL = sv_bless ( |
|
|
3092 | coro_waitarray_new (aTHX_ 0), |
|
|
3093 | GvSTASH (CvGV (cv)) |
|
|
3094 | ); |
|
|
3095 | OUTPUT: |
|
|
3096 | RETVAL |
|
|
3097 | |
|
|
3098 | void |
|
|
3099 | wait (SV *self) |
|
|
3100 | CODE: |
|
|
3101 | CORO_EXECUTE_SLF_XS (slf_init_signal_wait); |
|
|
3102 | |
|
|
3103 | void |
|
|
3104 | broadcast (SV *self) |
|
|
3105 | CODE: |
|
|
3106 | { |
|
|
3107 | AV *av = (AV *)SvRV (self); |
|
|
3108 | coro_signal_wake (aTHX_ av, AvFILLp (av)); |
|
|
3109 | } |
|
|
3110 | |
|
|
3111 | void |
|
|
3112 | send (SV *self) |
|
|
3113 | CODE: |
|
|
3114 | { |
|
|
3115 | AV *av = (AV *)SvRV (self); |
|
|
3116 | |
|
|
3117 | if (AvFILLp (av)) |
|
|
3118 | coro_signal_wake (aTHX_ av, 1); |
|
|
3119 | else |
|
|
3120 | SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */ |
|
|
3121 | } |
|
|
3122 | |
|
|
3123 | IV |
|
|
3124 | awaited (SV *self) |
|
|
3125 | CODE: |
|
|
3126 | RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1; |
|
|
3127 | OUTPUT: |
|
|
3128 | RETVAL |
2840 | |
3129 | |
2841 | |
3130 | |
2842 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
3131 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2843 | |
3132 | |
2844 | BOOT: |
3133 | BOOT: |
… | |
… | |
2859 | sv_setsv (sv_activity, &PL_sv_undef); |
3148 | sv_setsv (sv_activity, &PL_sv_undef); |
2860 | if (coro_nready >= incede) |
3149 | if (coro_nready >= incede) |
2861 | { |
3150 | { |
2862 | PUSHMARK (SP); |
3151 | PUSHMARK (SP); |
2863 | PUTBACK; |
3152 | PUTBACK; |
2864 | call_pv ("Coro::AnyEvent::_activity", G_DISCARD | G_EVAL); |
3153 | call_pv ("Coro::AnyEvent::_activity", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
2865 | SPAGAIN; |
|
|
2866 | } |
3154 | } |
2867 | |
3155 | |
2868 | --incede; |
3156 | --incede; |
2869 | } |
3157 | } |
2870 | |
3158 | |