… | |
… | |
55 | |
55 | |
56 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
56 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
57 | (PERL_REVISION > (a) \ |
57 | (PERL_REVISION > (a) \ |
58 | || (PERL_REVISION == (a) \ |
58 | || (PERL_REVISION == (a) \ |
59 | && (PERL_VERSION > (b) \ |
59 | && (PERL_VERSION > (b) \ |
60 | || (PERL_VERSION == (b) && PERLSUBVERSION >= (c))))) |
60 | || (PERL_VERSION == (b) && PERL_SUBVERSION >= (c))))) |
61 | |
61 | |
62 | #if !PERL_VERSION_ATLEAST (5,6,0) |
62 | #if !PERL_VERSION_ATLEAST (5,6,0) |
63 | # ifndef PL_ppaddr |
63 | # ifndef PL_ppaddr |
64 | # define PL_ppaddr ppaddr |
64 | # define PL_ppaddr ppaddr |
65 | # endif |
65 | # endif |
… | |
… | |
98 | # define newSV(l) NEWSV(0,l) |
98 | # define newSV(l) NEWSV(0,l) |
99 | #endif |
99 | #endif |
100 | #ifndef CvISXSUB_on |
100 | #ifndef CvISXSUB_on |
101 | # define CvISXSUB_on(cv) (void)cv |
101 | # define CvISXSUB_on(cv) (void)cv |
102 | #endif |
102 | #endif |
|
|
103 | #ifndef CvISXSUB |
|
|
104 | # define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE) |
|
|
105 | #endif |
103 | |
106 | |
104 | /* 5.8.7 */ |
107 | /* 5.8.7 */ |
105 | #ifndef SvRV_set |
108 | #ifndef SvRV_set |
106 | # define SvRV_set(s,v) SvRV(s) = (v) |
109 | # define SvRV_set(s,v) SvRV(s) = (v) |
107 | #endif |
110 | #endif |
… | |
… | |
176 | static HV *hv_sig; /* %SIG */ |
179 | static HV *hv_sig; /* %SIG */ |
177 | |
180 | |
178 | /* async_pool helper stuff */ |
181 | /* async_pool helper stuff */ |
179 | static SV *sv_pool_rss; |
182 | static SV *sv_pool_rss; |
180 | static SV *sv_pool_size; |
183 | static SV *sv_pool_size; |
|
|
184 | static SV *sv_async_pool_idle; |
181 | static AV *av_async_pool; |
185 | static AV *av_async_pool; |
|
|
186 | static SV *sv_Coro; |
|
|
187 | static CV *cv_pool_handler; |
|
|
188 | static CV *cv_coro_state_new; |
182 | |
189 | |
183 | /* Coro::AnyEvent */ |
190 | /* Coro::AnyEvent */ |
184 | static SV *sv_activity; |
191 | static SV *sv_activity; |
185 | |
192 | |
186 | static struct coro_cctx *cctx_first; |
193 | static struct coro_cctx *cctx_first; |
… | |
… | |
246 | /* state data */ |
253 | /* state data */ |
247 | struct CoroSLF slf_frame; /* saved slf frame */ |
254 | struct CoroSLF slf_frame; /* saved slf frame */ |
248 | AV *mainstack; |
255 | AV *mainstack; |
249 | perl_slots *slot; /* basically the saved sp */ |
256 | perl_slots *slot; /* basically the saved sp */ |
250 | |
257 | |
|
|
258 | CV *startcv; /* the CV to execute */ |
251 | AV *args; /* data associated with this coroutine (initial args) */ |
259 | AV *args; /* data associated with this coroutine (initial args) */ |
252 | int refcnt; /* coroutines are refcounted, yes */ |
260 | int refcnt; /* coroutines are refcounted, yes */ |
253 | int flags; /* CF_ flags */ |
261 | int flags; /* CF_ flags */ |
254 | HV *hv; /* the perl hash associated with this coro, if any */ |
262 | HV *hv; /* the perl hash associated with this coro, if any */ |
255 | void (*on_destroy)(pTHX_ struct coro *coro); |
263 | void (*on_destroy)(pTHX_ struct coro *coro); |
256 | |
264 | |
257 | /* statistics */ |
265 | /* statistics */ |
258 | int usecount; /* number of transfers to this coro */ |
266 | int usecount; /* number of transfers to this coro */ |
259 | |
267 | |
… | |
… | |
262 | SV *except; /* exception to be thrown */ |
270 | SV *except; /* exception to be thrown */ |
263 | SV *rouse_cb; |
271 | SV *rouse_cb; |
264 | |
272 | |
265 | /* async_pool */ |
273 | /* async_pool */ |
266 | SV *saved_deffh; |
274 | SV *saved_deffh; |
|
|
275 | SV *invoke_cb; |
|
|
276 | AV *invoke_av; |
267 | |
277 | |
268 | /* linked list */ |
278 | /* linked list */ |
269 | struct coro *next, *prev; |
279 | struct coro *next, *prev; |
270 | }; |
280 | }; |
271 | |
281 | |
… | |
… | |
288 | |
298 | |
289 | /* for Coro.pm */ |
299 | /* for Coro.pm */ |
290 | static SV *coro_current; |
300 | static SV *coro_current; |
291 | static SV *coro_readyhook; |
301 | static SV *coro_readyhook; |
292 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
302 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
|
|
303 | static CV *cv_coro_run, *cv_coro_terminate; |
293 | static struct coro *coro_first; |
304 | static struct coro *coro_first; |
294 | #define coro_nready coroapi.nready |
305 | #define coro_nready coroapi.nready |
295 | |
306 | |
296 | /** lowlevel stuff **********************************************************/ |
307 | /** lowlevel stuff **********************************************************/ |
297 | |
308 | |
… | |
… | |
321 | #if PERL_VERSION_ATLEAST (5,10,0) |
332 | #if PERL_VERSION_ATLEAST (5,10,0) |
322 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
333 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
323 | get_hv (name, create); |
334 | get_hv (name, create); |
324 | #endif |
335 | #endif |
325 | return get_hv (name, create); |
336 | return get_hv (name, create); |
|
|
337 | } |
|
|
338 | |
|
|
339 | /* may croak */ |
|
|
340 | INLINE CV * |
|
|
341 | coro_sv_2cv (pTHX_ SV *sv) |
|
|
342 | { |
|
|
343 | HV *st; |
|
|
344 | GV *gvp; |
|
|
345 | return sv_2cv (sv, &st, &gvp, 0); |
326 | } |
346 | } |
327 | |
347 | |
328 | static AV * |
348 | static AV * |
329 | coro_clone_padlist (pTHX_ CV *cv) |
349 | coro_clone_padlist (pTHX_ CV *cv) |
330 | { |
350 | { |
… | |
… | |
676 | #if !PERL_VERSION_ATLEAST (5,10,0) |
696 | #if !PERL_VERSION_ATLEAST (5,10,0) |
677 | Safefree (PL_retstack); |
697 | Safefree (PL_retstack); |
678 | #endif |
698 | #endif |
679 | } |
699 | } |
680 | |
700 | |
|
|
701 | #define CORO_RSS \ |
|
|
702 | rss += sizeof (SYM (curstackinfo)); \ |
|
|
703 | rss += (SYM (curstackinfo->si_cxmax) + 1) * sizeof (PERL_CONTEXT); \ |
|
|
704 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (SYM (curstack))) * sizeof (SV *); \ |
|
|
705 | rss += SYM (tmps_max) * sizeof (SV *); \ |
|
|
706 | rss += (SYM (markstack_max) - SYM (markstack_ptr)) * sizeof (I32); \ |
|
|
707 | rss += SYM (scopestack_max) * sizeof (I32); \ |
|
|
708 | rss += SYM (savestack_max) * sizeof (ANY); |
|
|
709 | |
681 | static size_t |
710 | static size_t |
682 | coro_rss (pTHX_ struct coro *coro) |
711 | coro_rss (pTHX_ struct coro *coro) |
683 | { |
712 | { |
684 | size_t rss = sizeof (*coro); |
713 | size_t rss = sizeof (*coro); |
685 | |
714 | |
686 | if (coro->mainstack) |
715 | if (coro->mainstack) |
687 | { |
716 | { |
688 | perl_slots tmp_slot; |
|
|
689 | perl_slots *slot; |
|
|
690 | |
|
|
691 | if (coro->flags & CF_RUNNING) |
717 | if (coro->flags & CF_RUNNING) |
692 | { |
718 | { |
693 | slot = &tmp_slot; |
719 | #define SYM(sym) PL_ ## sym |
694 | |
720 | CORO_RSS; |
695 | #define VAR(name,type) slot->name = PL_ ## name; |
|
|
696 | # include "state.h" |
|
|
697 | #undef VAR |
721 | #undef SYM |
698 | } |
722 | } |
699 | else |
723 | else |
700 | slot = coro->slot; |
|
|
701 | |
|
|
702 | if (slot) |
|
|
703 | { |
724 | { |
704 | rss += sizeof (slot->curstackinfo); |
725 | #define SYM(sym) coro->slot->sym |
705 | rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
726 | CORO_RSS; |
706 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); |
727 | #undef SYM |
707 | rss += slot->tmps_max * sizeof (SV *); |
|
|
708 | rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); |
|
|
709 | rss += slot->scopestack_max * sizeof (I32); |
|
|
710 | rss += slot->savestack_max * sizeof (ANY); |
|
|
711 | |
|
|
712 | #if !PERL_VERSION_ATLEAST (5,10,0) |
|
|
713 | rss += slot->retstack_max * sizeof (OP *); |
|
|
714 | #endif |
|
|
715 | } |
728 | } |
716 | } |
729 | } |
717 | |
730 | |
718 | return rss; |
731 | return rss; |
719 | } |
732 | } |
… | |
… | |
861 | dSP; |
874 | dSP; |
862 | UNOP myop; |
875 | UNOP myop; |
863 | |
876 | |
864 | Zero (&myop, 1, UNOP); |
877 | Zero (&myop, 1, UNOP); |
865 | myop.op_next = Nullop; |
878 | myop.op_next = Nullop; |
|
|
879 | myop.op_type = OP_ENTERSUB; |
866 | myop.op_flags = OPf_WANT_VOID; |
880 | myop.op_flags = OPf_WANT_VOID; |
867 | |
881 | |
868 | PUSHMARK (SP); |
882 | PUSHMARK (SP); |
869 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
883 | PUSHs ((SV *)coro->startcv); |
870 | PUTBACK; |
884 | PUTBACK; |
871 | PL_op = (OP *)&myop; |
885 | PL_op = (OP *)&myop; |
872 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
886 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
873 | SPAGAIN; |
|
|
874 | } |
887 | } |
875 | |
888 | |
876 | /* this newly created coroutine might be run on an existing cctx which most |
889 | /* this newly created coroutine might be run on an existing cctx which most |
877 | * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. |
890 | * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. |
878 | */ |
891 | */ |
… | |
… | |
881 | |
894 | |
882 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
895 | /* 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; |
896 | coro_setup_op.op_next = PL_op; |
884 | coro_setup_op.op_type = OP_CUSTOM; |
897 | coro_setup_op.op_type = OP_CUSTOM; |
885 | coro_setup_op.op_ppaddr = pp_slf; |
898 | coro_setup_op.op_ppaddr = pp_slf; |
886 | /* no flags required, as an init function won't be called */ |
899 | /* no flags etc. required, as an init function won't be called */ |
887 | |
900 | |
888 | PL_op = (OP *)&coro_setup_op; |
901 | PL_op = (OP *)&coro_setup_op; |
889 | |
902 | |
890 | /* copy throw, in case it was set before coro_setup */ |
903 | /* copy throw, in case it was set before coro_setup */ |
891 | CORO_THROW = coro->except; |
904 | CORO_THROW = coro->except; |
… | |
… | |
919 | SvREFCNT_dec (GvSV (irsgv)); |
932 | SvREFCNT_dec (GvSV (irsgv)); |
920 | |
933 | |
921 | SvREFCNT_dec (PL_diehook); |
934 | SvREFCNT_dec (PL_diehook); |
922 | SvREFCNT_dec (PL_warnhook); |
935 | SvREFCNT_dec (PL_warnhook); |
923 | |
936 | |
924 | SvREFCNT_dec (CORO_THROW); |
|
|
925 | SvREFCNT_dec (coro->saved_deffh); |
937 | SvREFCNT_dec (coro->saved_deffh); |
926 | SvREFCNT_dec (coro->rouse_cb); |
938 | SvREFCNT_dec (coro->rouse_cb); |
|
|
939 | SvREFCNT_dec (coro->invoke_cb); |
|
|
940 | SvREFCNT_dec (coro->invoke_av); |
927 | |
941 | |
928 | coro_destruct_stacks (aTHX); |
942 | coro_destruct_stacks (aTHX); |
929 | } |
943 | } |
930 | |
944 | |
931 | INLINE void |
945 | INLINE void |
… | |
… | |
1018 | SAVETMPS; |
1032 | SAVETMPS; |
1019 | EXTEND (SP, 3); |
1033 | EXTEND (SP, 3); |
1020 | PUSHMARK (SP); |
1034 | PUSHMARK (SP); |
1021 | PUSHs (&PL_sv_yes); |
1035 | PUSHs (&PL_sv_yes); |
1022 | PUSHs (fullname); |
1036 | PUSHs (fullname); |
1023 | PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
1037 | PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
1024 | PUTBACK; |
1038 | PUTBACK; |
1025 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
1039 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
1026 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1040 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1027 | SPAGAIN; |
1041 | SPAGAIN; |
1028 | FREETMPS; |
1042 | FREETMPS; |
… | |
… | |
1134 | transfer_tail (aTHX); |
1148 | transfer_tail (aTHX); |
1135 | |
1149 | |
1136 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1150 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1137 | PL_restartop = PL_op; |
1151 | PL_restartop = PL_op; |
1138 | perl_run (PL_curinterp); |
1152 | perl_run (PL_curinterp); |
|
|
1153 | /* |
|
|
1154 | * Unfortunately, there is no way to get at the return values of the |
|
|
1155 | * coro body here, as perl_run destroys these |
|
|
1156 | */ |
1139 | |
1157 | |
1140 | /* |
1158 | /* |
1141 | * If perl-run returns we assume exit() was being called or the coro |
1159 | * If perl-run returns we assume exit() was being called or the coro |
1142 | * fell off the end, which seems to be the only valid (non-bug) |
1160 | * fell off the end, which seems to be the only valid (non-bug) |
1143 | * reason for perl_run to return. We try to exit by jumping to the |
1161 | * reason for perl_run to return. We try to exit by jumping to the |
… | |
… | |
1435 | |
1453 | |
1436 | coro->slot = 0; |
1454 | coro->slot = 0; |
1437 | } |
1455 | } |
1438 | |
1456 | |
1439 | cctx_destroy (coro->cctx); |
1457 | cctx_destroy (coro->cctx); |
|
|
1458 | SvREFCNT_dec (coro->startcv); |
1440 | SvREFCNT_dec (coro->args); |
1459 | SvREFCNT_dec (coro->args); |
|
|
1460 | SvREFCNT_dec (CORO_THROW); |
1441 | |
1461 | |
1442 | if (coro->next) coro->next->prev = coro->prev; |
1462 | if (coro->next) coro->next->prev = coro->prev; |
1443 | if (coro->prev) coro->prev->next = coro->next; |
1463 | if (coro->prev) coro->prev->next = coro->next; |
1444 | if (coro == coro_first) coro_first = coro->next; |
1464 | if (coro == coro_first) coro_first = coro->next; |
1445 | |
1465 | |
… | |
… | |
1592 | api_is_ready (pTHX_ SV *coro_sv) |
1612 | api_is_ready (pTHX_ SV *coro_sv) |
1593 | { |
1613 | { |
1594 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1614 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1595 | } |
1615 | } |
1596 | |
1616 | |
|
|
1617 | /* expects to own a reference to next->hv */ |
1597 | INLINE void |
1618 | INLINE void |
|
|
1619 | prepare_cede_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
|
|
1620 | { |
|
|
1621 | SV *prev_sv = SvRV (coro_current); |
|
|
1622 | |
|
|
1623 | ta->prev = SvSTATE_hv (prev_sv); |
|
|
1624 | ta->next = next; |
|
|
1625 | |
|
|
1626 | TRANSFER_CHECK (*ta); |
|
|
1627 | |
|
|
1628 | SvRV_set (coro_current, next->hv); |
|
|
1629 | |
|
|
1630 | free_coro_mortal (aTHX); |
|
|
1631 | coro_mortal = prev_sv; |
|
|
1632 | } |
|
|
1633 | |
|
|
1634 | static void |
1598 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1635 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1599 | { |
1636 | { |
1600 | SV *prev_sv, *next_sv; |
|
|
1601 | |
|
|
1602 | for (;;) |
1637 | for (;;) |
1603 | { |
1638 | { |
1604 | next_sv = coro_deq (aTHX); |
1639 | SV *next_sv = coro_deq (aTHX); |
1605 | |
1640 | |
1606 | /* nothing to schedule: call the idle handler */ |
|
|
1607 | if (expect_false (!next_sv)) |
1641 | if (expect_false (!next_sv)) |
1608 | { |
1642 | { |
|
|
1643 | /* nothing to schedule: call the idle handler */ |
1609 | dSP; |
1644 | dSP; |
1610 | |
1645 | |
1611 | ENTER; |
1646 | ENTER; |
1612 | SAVETMPS; |
1647 | SAVETMPS; |
1613 | |
1648 | |
… | |
… | |
1615 | PUTBACK; |
1650 | PUTBACK; |
1616 | call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); |
1651 | call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); |
1617 | |
1652 | |
1618 | FREETMPS; |
1653 | FREETMPS; |
1619 | LEAVE; |
1654 | LEAVE; |
1620 | continue; |
|
|
1621 | } |
1655 | } |
1622 | |
1656 | else |
1623 | ta->next = SvSTATE_hv (next_sv); |
|
|
1624 | |
|
|
1625 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1626 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
|
|
1627 | { |
1657 | { |
1628 | SvREFCNT_dec (next_sv); |
1658 | struct coro *next = SvSTATE_hv (next_sv); |
|
|
1659 | |
|
|
1660 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1661 | if (expect_false (next->flags & CF_DESTROYED)) |
1629 | /* coro_nready has already been taken care of by destroy */ |
1662 | SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ |
1630 | continue; |
1663 | else |
|
|
1664 | { |
|
|
1665 | next->flags &= ~CF_READY; |
|
|
1666 | --coro_nready; |
|
|
1667 | |
|
|
1668 | return prepare_cede_to (aTHX_ ta, next); |
|
|
1669 | } |
1631 | } |
1670 | } |
1632 | |
|
|
1633 | --coro_nready; |
|
|
1634 | break; |
|
|
1635 | } |
1671 | } |
1636 | |
|
|
1637 | /* free this only after the transfer */ |
|
|
1638 | prev_sv = SvRV (coro_current); |
|
|
1639 | ta->prev = SvSTATE_hv (prev_sv); |
|
|
1640 | TRANSFER_CHECK (*ta); |
|
|
1641 | assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); |
|
|
1642 | ta->next->flags &= ~CF_READY; |
|
|
1643 | SvRV_set (coro_current, next_sv); |
|
|
1644 | |
|
|
1645 | free_coro_mortal (aTHX); |
|
|
1646 | coro_mortal = prev_sv; |
|
|
1647 | } |
1672 | } |
1648 | |
1673 | |
1649 | INLINE void |
1674 | INLINE void |
1650 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1675 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1651 | { |
1676 | { |
… | |
… | |
1731 | coro->slot->runops = RUNOPS_DEFAULT; |
1756 | coro->slot->runops = RUNOPS_DEFAULT; |
1732 | } |
1757 | } |
1733 | } |
1758 | } |
1734 | |
1759 | |
1735 | /*****************************************************************************/ |
1760 | /*****************************************************************************/ |
|
|
1761 | /* async pool handler */ |
|
|
1762 | |
|
|
1763 | static int |
|
|
1764 | slf_check_pool_handler (pTHX_ struct CoroSLF *frame) |
|
|
1765 | { |
|
|
1766 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1767 | struct coro *coro = (struct coro *)frame->data; |
|
|
1768 | |
|
|
1769 | if (!coro->invoke_cb) |
|
|
1770 | return 1; /* loop till we have invoke */ |
|
|
1771 | else |
|
|
1772 | { |
|
|
1773 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
1774 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
|
|
1775 | |
|
|
1776 | coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv); |
|
|
1777 | |
|
|
1778 | { |
|
|
1779 | dSP; |
|
|
1780 | XPUSHs (sv_2mortal (coro->invoke_cb)); coro->invoke_cb = 0; |
|
|
1781 | PUTBACK; |
|
|
1782 | } |
|
|
1783 | |
|
|
1784 | SvREFCNT_dec (GvAV (PL_defgv)); |
|
|
1785 | GvAV (PL_defgv) = coro->invoke_av; |
|
|
1786 | coro->invoke_av = 0; |
|
|
1787 | |
|
|
1788 | return 0; |
|
|
1789 | } |
|
|
1790 | } |
|
|
1791 | |
|
|
1792 | static void |
|
|
1793 | slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1794 | { |
|
|
1795 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1796 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
|
|
1797 | |
|
|
1798 | if (expect_true (coro->saved_deffh)) |
|
|
1799 | { |
|
|
1800 | /* subsequent iteration */ |
|
|
1801 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
|
|
1802 | coro->saved_deffh = 0; |
|
|
1803 | |
|
|
1804 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
|
|
1805 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
1806 | { |
|
|
1807 | coro->invoke_cb = SvREFCNT_inc_NN ((SV *)cv_coro_terminate); |
|
|
1808 | coro->invoke_av = newAV (); |
|
|
1809 | |
|
|
1810 | frame->prepare = prepare_nop; |
|
|
1811 | } |
|
|
1812 | else |
|
|
1813 | { |
|
|
1814 | av_clear (GvAV (PL_defgv)); |
|
|
1815 | hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0); |
|
|
1816 | |
|
|
1817 | coro->prio = 0; |
|
|
1818 | |
|
|
1819 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
|
|
1820 | api_trace (aTHX_ coro_current, 0); |
|
|
1821 | |
|
|
1822 | frame->prepare = prepare_schedule; |
|
|
1823 | av_push (av_async_pool, SvREFCNT_inc (hv)); |
|
|
1824 | } |
|
|
1825 | } |
|
|
1826 | else |
|
|
1827 | { |
|
|
1828 | /* first iteration, simply fall through */ |
|
|
1829 | frame->prepare = prepare_nop; |
|
|
1830 | } |
|
|
1831 | |
|
|
1832 | frame->check = slf_check_pool_handler; |
|
|
1833 | frame->data = (void *)coro; |
|
|
1834 | } |
|
|
1835 | |
|
|
1836 | /*****************************************************************************/ |
1736 | /* rouse callback */ |
1837 | /* rouse callback */ |
1737 | |
1838 | |
1738 | #define CORO_MAGIC_type_rouse PERL_MAGIC_ext |
1839 | #define CORO_MAGIC_type_rouse PERL_MAGIC_ext |
1739 | |
1840 | |
1740 | static void |
1841 | static void |
… | |
… | |
1744 | SV *data = (SV *)GENSUB_ARG; |
1845 | SV *data = (SV *)GENSUB_ARG; |
1745 | |
1846 | |
1746 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
1847 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
1747 | { |
1848 | { |
1748 | /* first call, set args */ |
1849 | /* first call, set args */ |
1749 | int i; |
|
|
1750 | AV *av = newAV (); |
1850 | AV *av = newAV (); |
1751 | SV *coro = SvRV (data); |
1851 | SV *coro = SvRV (data); |
1752 | |
1852 | |
1753 | SvRV_set (data, (SV *)av); |
1853 | SvRV_set (data, (SV *)av); |
1754 | api_ready (aTHX_ coro); |
1854 | api_ready (aTHX_ coro); |
… | |
… | |
2174 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2274 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2175 | AvARRAY (av)[1] = count_sv; |
2275 | AvARRAY (av)[1] = count_sv; |
2176 | cb = av_shift (av); |
2276 | cb = av_shift (av); |
2177 | |
2277 | |
2178 | if (SvOBJECT (cb)) |
2278 | if (SvOBJECT (cb)) |
|
|
2279 | { |
2179 | api_ready (aTHX_ cb); |
2280 | api_ready (aTHX_ cb); |
2180 | else |
2281 | --count; |
2181 | croak ("callbacks not yet supported"); |
2282 | } |
|
|
2283 | else if (SvTYPE (cb) == SVt_PVCV) |
|
|
2284 | { |
|
|
2285 | dSP; |
|
|
2286 | PUSHMARK (SP); |
|
|
2287 | XPUSHs (sv_2mortal (newRV_inc ((SV *)av))); |
|
|
2288 | PUTBACK; |
|
|
2289 | call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR); |
|
|
2290 | } |
2182 | |
2291 | |
2183 | SvREFCNT_dec (cb); |
2292 | SvREFCNT_dec (cb); |
2184 | |
|
|
2185 | --count; |
|
|
2186 | } |
2293 | } |
2187 | } |
2294 | } |
2188 | |
2295 | |
2189 | static void |
2296 | static void |
2190 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
2297 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
… | |
… | |
2272 | } |
2379 | } |
2273 | |
2380 | |
2274 | static void |
2381 | static void |
2275 | slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2382 | slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2276 | { |
2383 | { |
|
|
2384 | if (items >= 2) |
|
|
2385 | { |
|
|
2386 | /* callback form */ |
|
|
2387 | AV *av = (AV *)SvRV (arg [0]); |
|
|
2388 | CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); |
|
|
2389 | |
|
|
2390 | av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); |
|
|
2391 | |
|
|
2392 | if (SvIVX (AvARRAY (av)[0]) > 0) |
|
|
2393 | coro_semaphore_adjust (aTHX_ av, 0); |
|
|
2394 | |
|
|
2395 | frame->prepare = prepare_nop; |
|
|
2396 | frame->check = slf_check_nop; |
|
|
2397 | } |
|
|
2398 | else |
|
|
2399 | { |
2277 | slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); |
2400 | slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); |
2278 | frame->check = slf_check_semaphore_wait; |
2401 | frame->check = slf_check_semaphore_wait; |
|
|
2402 | } |
2279 | } |
2403 | } |
2280 | |
2404 | |
2281 | /* signal */ |
2405 | /* signal */ |
2282 | |
2406 | |
2283 | static void |
2407 | static void |
… | |
… | |
2355 | dXSARGS; |
2479 | dXSARGS; |
2356 | AV *state = (AV *)GENSUB_ARG; |
2480 | AV *state = (AV *)GENSUB_ARG; |
2357 | SV *coro = av_pop (state); |
2481 | SV *coro = av_pop (state); |
2358 | SV *data_sv = newSV (sizeof (struct io_state)); |
2482 | SV *data_sv = newSV (sizeof (struct io_state)); |
2359 | |
2483 | |
2360 | av_extend (state, items); |
2484 | av_extend (state, items - 1); |
2361 | |
2485 | |
2362 | sv_upgrade (data_sv, SVt_PV); |
2486 | sv_upgrade (data_sv, SVt_PV); |
2363 | SvCUR_set (data_sv, sizeof (struct io_state)); |
2487 | SvCUR_set (data_sv, sizeof (struct io_state)); |
2364 | SvPOK_only (data_sv); |
2488 | SvPOK_only (data_sv); |
2365 | |
2489 | |
… | |
… | |
2575 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
2699 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
2576 | } |
2700 | } |
2577 | |
2701 | |
2578 | SV * |
2702 | SV * |
2579 | new (char *klass, ...) |
2703 | new (char *klass, ...) |
|
|
2704 | ALIAS: |
|
|
2705 | Coro::new = 1 |
2580 | CODE: |
2706 | CODE: |
2581 | { |
2707 | { |
2582 | struct coro *coro; |
2708 | struct coro *coro; |
2583 | MAGIC *mg; |
2709 | MAGIC *mg; |
2584 | HV *hv; |
2710 | HV *hv; |
|
|
2711 | CV *cb; |
2585 | int i; |
2712 | int i; |
|
|
2713 | |
|
|
2714 | if (items > 1) |
|
|
2715 | { |
|
|
2716 | cb = coro_sv_2cv (aTHX_ ST (1)); |
|
|
2717 | |
|
|
2718 | if (!ix) |
|
|
2719 | { |
|
|
2720 | if (CvISXSUB (cb)) |
|
|
2721 | croak ("Coro::State doesn't support XS functions as coroutine start, caught"); |
|
|
2722 | |
|
|
2723 | if (!CvROOT (cb)) |
|
|
2724 | croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught"); |
|
|
2725 | } |
|
|
2726 | } |
2586 | |
2727 | |
2587 | Newz (0, coro, 1, struct coro); |
2728 | Newz (0, coro, 1, struct coro); |
2588 | coro->args = newAV (); |
2729 | coro->args = newAV (); |
2589 | coro->flags = CF_NEW; |
2730 | coro->flags = CF_NEW; |
2590 | |
2731 | |
… | |
… | |
2595 | coro->hv = hv = newHV (); |
2736 | coro->hv = hv = newHV (); |
2596 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
2737 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
2597 | mg->mg_flags |= MGf_DUP; |
2738 | mg->mg_flags |= MGf_DUP; |
2598 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
2739 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
2599 | |
2740 | |
|
|
2741 | if (items > 1) |
|
|
2742 | { |
2600 | av_extend (coro->args, items - 1); |
2743 | av_extend (coro->args, items - 1 + ix - 1); |
|
|
2744 | |
|
|
2745 | if (ix) |
|
|
2746 | { |
|
|
2747 | av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb)); |
|
|
2748 | cb = cv_coro_run; |
|
|
2749 | } |
|
|
2750 | |
|
|
2751 | coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb); |
|
|
2752 | |
2601 | for (i = 1; i < items; i++) |
2753 | for (i = 2; i < items; i++) |
2602 | av_push (coro->args, newSVsv (ST (i))); |
2754 | av_push (coro->args, newSVsv (ST (i))); |
|
|
2755 | } |
2603 | } |
2756 | } |
2604 | OUTPUT: |
2757 | OUTPUT: |
2605 | RETVAL |
2758 | RETVAL |
2606 | |
2759 | |
2607 | void |
2760 | void |
… | |
… | |
2805 | |
2958 | |
2806 | BOOT: |
2959 | BOOT: |
2807 | { |
2960 | { |
2808 | int i; |
2961 | int i; |
2809 | |
2962 | |
2810 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
2963 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
2811 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
2964 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
2812 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
2965 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
2813 | |
2966 | cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); |
|
|
2967 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
2814 | coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); |
2968 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
2815 | SvREADONLY_on (coro_current); |
2969 | |
|
|
2970 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
|
|
2971 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
|
|
2972 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
|
|
2973 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
2816 | |
2974 | |
2817 | coro_stash = gv_stashpv ("Coro", TRUE); |
2975 | coro_stash = gv_stashpv ("Coro", TRUE); |
2818 | |
2976 | |
2819 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
2977 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
2820 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
2978 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
… | |
… | |
2909 | CODE: |
3067 | CODE: |
2910 | RETVAL = coro_nready; |
3068 | RETVAL = coro_nready; |
2911 | OUTPUT: |
3069 | OUTPUT: |
2912 | RETVAL |
3070 | RETVAL |
2913 | |
3071 | |
2914 | # for async_pool speedup |
|
|
2915 | void |
3072 | void |
2916 | _pool_1 (SV *cb) |
3073 | _pool_handler (...) |
2917 | CODE: |
3074 | CODE: |
2918 | { |
3075 | CORO_EXECUTE_SLF_XS (slf_init_pool_handler); |
2919 | HV *hv = (HV *)SvRV (coro_current); |
|
|
2920 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
|
|
2921 | AV *defav = GvAV (PL_defgv); |
|
|
2922 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
|
|
2923 | AV *invoke_av; |
|
|
2924 | int i, len; |
|
|
2925 | |
3076 | |
2926 | if (!invoke) |
3077 | void |
|
|
3078 | async_pool (SV *cv, ...) |
|
|
3079 | PROTOTYPE: &@ |
|
|
3080 | PPCODE: |
|
|
3081 | { |
|
|
3082 | HV *hv = (HV *)av_pop (av_async_pool); |
|
|
3083 | AV *av = newAV (); |
|
|
3084 | SV *cb = ST (0); |
|
|
3085 | int i; |
|
|
3086 | |
|
|
3087 | av_extend (av, items - 2); |
|
|
3088 | for (i = 1; i < items; ++i) |
|
|
3089 | av_push (av, SvREFCNT_inc_NN (ST (i))); |
|
|
3090 | |
|
|
3091 | if ((SV *)hv == &PL_sv_undef) |
2927 | { |
3092 | { |
2928 | SV *old = PL_diehook; |
3093 | PUSHMARK (SP); |
2929 | PL_diehook = 0; |
3094 | EXTEND (SP, 2); |
2930 | SvREFCNT_dec (old); |
3095 | PUSHs (sv_Coro); |
2931 | croak ("\3async_pool terminate\2\n"); |
3096 | PUSHs ((SV *)cv_pool_handler); |
|
|
3097 | PUTBACK; |
|
|
3098 | call_sv ((SV *)cv_coro_state_new, G_SCALAR); |
|
|
3099 | SPAGAIN; |
|
|
3100 | |
|
|
3101 | hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); |
2932 | } |
3102 | } |
2933 | |
3103 | |
2934 | SvREFCNT_dec (coro->saved_deffh); |
|
|
2935 | coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv); |
|
|
2936 | |
|
|
2937 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
2938 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
|
|
2939 | |
|
|
2940 | invoke_av = (AV *)SvRV (invoke); |
|
|
2941 | len = av_len (invoke_av); |
|
|
2942 | |
|
|
2943 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
|
|
2944 | |
|
|
2945 | if (len > 0) |
|
|
2946 | { |
3104 | { |
2947 | av_fill (defav, len - 1); |
3105 | struct coro *coro = SvSTATE_hv (hv); |
2948 | for (i = 0; i < len; ++i) |
3106 | |
2949 | av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); |
3107 | assert (!coro->invoke_cb); |
|
|
3108 | assert (!coro->invoke_av); |
|
|
3109 | coro->invoke_cb = SvREFCNT_inc (cb); |
|
|
3110 | coro->invoke_av = av; |
2950 | } |
3111 | } |
2951 | } |
|
|
2952 | |
3112 | |
2953 | void |
3113 | api_ready (aTHX_ (SV *)hv); |
2954 | _pool_2 (SV *cb) |
|
|
2955 | CODE: |
|
|
2956 | { |
|
|
2957 | HV *hv = (HV *)SvRV (coro_current); |
|
|
2958 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
|
|
2959 | |
3114 | |
2960 | sv_setsv (cb, &PL_sv_undef); |
3115 | if (GIMME_V != G_VOID) |
2961 | |
3116 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); |
2962 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
3117 | else |
2963 | coro->saved_deffh = 0; |
|
|
2964 | |
|
|
2965 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
|
|
2966 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
2967 | { |
|
|
2968 | SV *old = PL_diehook; |
|
|
2969 | PL_diehook = 0; |
|
|
2970 | SvREFCNT_dec (old); |
3118 | SvREFCNT_dec (hv); |
2971 | croak ("\3async_pool terminate\2\n"); |
|
|
2972 | } |
|
|
2973 | |
|
|
2974 | av_clear (GvAV (PL_defgv)); |
|
|
2975 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
2976 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
|
|
2977 | |
|
|
2978 | coro->prio = 0; |
|
|
2979 | |
|
|
2980 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
|
|
2981 | api_trace (aTHX_ coro_current, 0); |
|
|
2982 | |
|
|
2983 | av_push (av_async_pool, newSVsv (coro_current)); |
|
|
2984 | } |
3119 | } |
2985 | |
3120 | |
2986 | SV * |
3121 | SV * |
2987 | rouse_cb () |
3122 | rouse_cb () |
2988 | PROTOTYPE: |
3123 | PROTOTYPE: |
… | |
… | |
2990 | RETVAL = coro_new_rouse_cb (aTHX); |
3125 | RETVAL = coro_new_rouse_cb (aTHX); |
2991 | OUTPUT: |
3126 | OUTPUT: |
2992 | RETVAL |
3127 | RETVAL |
2993 | |
3128 | |
2994 | void |
3129 | void |
2995 | rouse_wait (SV *cb = 0) |
3130 | rouse_wait (...) |
2996 | PROTOTYPE: ;$ |
3131 | PROTOTYPE: ;$ |
2997 | PPCODE: |
3132 | PPCODE: |
2998 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
3133 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
2999 | |
3134 | |
3000 | |
3135 | |
… | |
… | |
3037 | adjust = 1 |
3172 | adjust = 1 |
3038 | CODE: |
3173 | CODE: |
3039 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
3174 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
3040 | |
3175 | |
3041 | void |
3176 | void |
3042 | down (SV *self) |
3177 | down (...) |
3043 | CODE: |
3178 | CODE: |
3044 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
3179 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
3045 | |
3180 | |
3046 | void |
3181 | void |
3047 | wait (SV *self) |
3182 | wait (...) |
3048 | CODE: |
3183 | CODE: |
3049 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait); |
3184 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait); |
3050 | |
3185 | |
3051 | void |
3186 | void |
3052 | try (SV *self) |
3187 | try (SV *self) |
… | |
… | |
3095 | ); |
3230 | ); |
3096 | OUTPUT: |
3231 | OUTPUT: |
3097 | RETVAL |
3232 | RETVAL |
3098 | |
3233 | |
3099 | void |
3234 | void |
3100 | wait (SV *self) |
3235 | wait (...) |
3101 | CODE: |
3236 | CODE: |
3102 | CORO_EXECUTE_SLF_XS (slf_init_signal_wait); |
3237 | CORO_EXECUTE_SLF_XS (slf_init_signal_wait); |
3103 | |
3238 | |
3104 | void |
3239 | void |
3105 | broadcast (SV *self) |
3240 | broadcast (SV *self) |
… | |
… | |
3162 | |
3297 | |
3163 | void |
3298 | void |
3164 | _register (char *target, char *proto, SV *req) |
3299 | _register (char *target, char *proto, SV *req) |
3165 | CODE: |
3300 | CODE: |
3166 | { |
3301 | { |
3167 | HV *st; |
3302 | CV *req_cv = coro_sv_2cv (aTHX_ req); |
3168 | GV *gvp; |
|
|
3169 | CV *req_cv = sv_2cv (req, &st, &gvp, 0); |
|
|
3170 | /* newXSproto doesn't return the CV on 5.8 */ |
3303 | /* newXSproto doesn't return the CV on 5.8 */ |
3171 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
3304 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
3172 | sv_setpv ((SV *)slf_cv, proto); |
3305 | sv_setpv ((SV *)slf_cv, proto); |
3173 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
3306 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
3174 | } |
3307 | } |