… | |
… | |
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 |
… | |
… | |
167 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
170 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
168 | static JMPENV *main_top_env; |
171 | static JMPENV *main_top_env; |
169 | static HV *coro_state_stash, *coro_stash; |
172 | static HV *coro_state_stash, *coro_stash; |
170 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
173 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
171 | |
174 | |
|
|
175 | static AV *av_destroy; /* destruction queue */ |
|
|
176 | static SV *sv_manager; /* the manager coro */ |
|
|
177 | |
172 | static GV *irsgv; /* $/ */ |
178 | static GV *irsgv; /* $/ */ |
173 | static GV *stdoutgv; /* *STDOUT */ |
179 | static GV *stdoutgv; /* *STDOUT */ |
174 | static SV *rv_diehook; |
180 | static SV *rv_diehook; |
175 | static SV *rv_warnhook; |
181 | static SV *rv_warnhook; |
176 | static HV *hv_sig; /* %SIG */ |
182 | static HV *hv_sig; /* %SIG */ |
177 | |
183 | |
178 | /* async_pool helper stuff */ |
184 | /* async_pool helper stuff */ |
179 | static SV *sv_pool_rss; |
185 | static SV *sv_pool_rss; |
180 | static SV *sv_pool_size; |
186 | static SV *sv_pool_size; |
|
|
187 | static SV *sv_async_pool_idle; /* description string */ |
181 | static AV *av_async_pool; |
188 | static AV *av_async_pool; /* idle pool */ |
|
|
189 | static SV *sv_Coro; /* class string */ |
|
|
190 | static CV *cv_pool_handler; |
|
|
191 | static CV *cv_coro_state_new; |
182 | |
192 | |
183 | /* Coro::AnyEvent */ |
193 | /* Coro::AnyEvent */ |
184 | static SV *sv_activity; |
194 | static SV *sv_activity; |
185 | |
195 | |
186 | static struct coro_cctx *cctx_first; |
196 | static struct coro_cctx *cctx_first; |
… | |
… | |
246 | /* state data */ |
256 | /* state data */ |
247 | struct CoroSLF slf_frame; /* saved slf frame */ |
257 | struct CoroSLF slf_frame; /* saved slf frame */ |
248 | AV *mainstack; |
258 | AV *mainstack; |
249 | perl_slots *slot; /* basically the saved sp */ |
259 | perl_slots *slot; /* basically the saved sp */ |
250 | |
260 | |
|
|
261 | CV *startcv; /* the CV to execute */ |
251 | AV *args; /* data associated with this coroutine (initial args) */ |
262 | AV *args; /* data associated with this coroutine (initial args) */ |
252 | int refcnt; /* coroutines are refcounted, yes */ |
263 | int refcnt; /* coroutines are refcounted, yes */ |
253 | int flags; /* CF_ flags */ |
264 | int flags; /* CF_ flags */ |
254 | HV *hv; /* the perl hash associated with this coro, if any */ |
265 | HV *hv; /* the perl hash associated with this coro, if any */ |
255 | void (*on_destroy)(pTHX_ struct coro *coro); |
266 | void (*on_destroy)(pTHX_ struct coro *coro); |
256 | |
267 | |
257 | /* statistics */ |
268 | /* statistics */ |
258 | int usecount; /* number of transfers to this coro */ |
269 | int usecount; /* number of transfers to this coro */ |
259 | |
270 | |
… | |
… | |
262 | SV *except; /* exception to be thrown */ |
273 | SV *except; /* exception to be thrown */ |
263 | SV *rouse_cb; |
274 | SV *rouse_cb; |
264 | |
275 | |
265 | /* async_pool */ |
276 | /* async_pool */ |
266 | SV *saved_deffh; |
277 | SV *saved_deffh; |
|
|
278 | SV *invoke_cb; |
|
|
279 | AV *invoke_av; |
267 | |
280 | |
268 | /* linked list */ |
281 | /* linked list */ |
269 | struct coro *next, *prev; |
282 | struct coro *next, *prev; |
270 | }; |
283 | }; |
271 | |
284 | |
… | |
… | |
288 | |
301 | |
289 | /* for Coro.pm */ |
302 | /* for Coro.pm */ |
290 | static SV *coro_current; |
303 | static SV *coro_current; |
291 | static SV *coro_readyhook; |
304 | static SV *coro_readyhook; |
292 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
305 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
|
|
306 | static CV *cv_coro_run, *cv_coro_terminate; |
293 | static struct coro *coro_first; |
307 | static struct coro *coro_first; |
294 | #define coro_nready coroapi.nready |
308 | #define coro_nready coroapi.nready |
295 | |
309 | |
296 | /** lowlevel stuff **********************************************************/ |
310 | /** lowlevel stuff **********************************************************/ |
297 | |
311 | |
… | |
… | |
321 | #if PERL_VERSION_ATLEAST (5,10,0) |
335 | #if PERL_VERSION_ATLEAST (5,10,0) |
322 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
336 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
323 | get_hv (name, create); |
337 | get_hv (name, create); |
324 | #endif |
338 | #endif |
325 | return get_hv (name, create); |
339 | return get_hv (name, create); |
|
|
340 | } |
|
|
341 | |
|
|
342 | /* may croak */ |
|
|
343 | INLINE CV * |
|
|
344 | coro_sv_2cv (pTHX_ SV *sv) |
|
|
345 | { |
|
|
346 | HV *st; |
|
|
347 | GV *gvp; |
|
|
348 | return sv_2cv (sv, &st, &gvp, 0); |
326 | } |
349 | } |
327 | |
350 | |
328 | static AV * |
351 | static AV * |
329 | coro_clone_padlist (pTHX_ CV *cv) |
352 | coro_clone_padlist (pTHX_ CV *cv) |
330 | { |
353 | { |
… | |
… | |
676 | #if !PERL_VERSION_ATLEAST (5,10,0) |
699 | #if !PERL_VERSION_ATLEAST (5,10,0) |
677 | Safefree (PL_retstack); |
700 | Safefree (PL_retstack); |
678 | #endif |
701 | #endif |
679 | } |
702 | } |
680 | |
703 | |
|
|
704 | #define CORO_RSS \ |
|
|
705 | rss += sizeof (SYM (curstackinfo)); \ |
|
|
706 | rss += (SYM (curstackinfo->si_cxmax) + 1) * sizeof (PERL_CONTEXT); \ |
|
|
707 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (SYM (curstack))) * sizeof (SV *); \ |
|
|
708 | rss += SYM (tmps_max) * sizeof (SV *); \ |
|
|
709 | rss += (SYM (markstack_max) - SYM (markstack_ptr)) * sizeof (I32); \ |
|
|
710 | rss += SYM (scopestack_max) * sizeof (I32); \ |
|
|
711 | rss += SYM (savestack_max) * sizeof (ANY); |
|
|
712 | |
681 | static size_t |
713 | static size_t |
682 | coro_rss (pTHX_ struct coro *coro) |
714 | coro_rss (pTHX_ struct coro *coro) |
683 | { |
715 | { |
684 | size_t rss = sizeof (*coro); |
716 | size_t rss = sizeof (*coro); |
685 | |
717 | |
686 | if (coro->mainstack) |
718 | if (coro->mainstack) |
687 | { |
719 | { |
688 | perl_slots tmp_slot; |
|
|
689 | perl_slots *slot; |
|
|
690 | |
|
|
691 | if (coro->flags & CF_RUNNING) |
720 | if (coro->flags & CF_RUNNING) |
692 | { |
721 | { |
693 | slot = &tmp_slot; |
722 | #define SYM(sym) PL_ ## sym |
694 | |
723 | CORO_RSS; |
695 | #define VAR(name,type) slot->name = PL_ ## name; |
|
|
696 | # include "state.h" |
|
|
697 | #undef VAR |
724 | #undef SYM |
698 | } |
725 | } |
699 | else |
726 | else |
700 | slot = coro->slot; |
|
|
701 | |
|
|
702 | if (slot) |
|
|
703 | { |
727 | { |
704 | rss += sizeof (slot->curstackinfo); |
728 | #define SYM(sym) coro->slot->sym |
705 | rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
729 | CORO_RSS; |
706 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); |
730 | #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 | } |
731 | } |
716 | } |
732 | } |
717 | |
733 | |
718 | return rss; |
734 | return rss; |
719 | } |
735 | } |
… | |
… | |
819 | |
835 | |
820 | static int |
836 | static int |
821 | slf_check_nop (pTHX_ struct CoroSLF *frame) |
837 | slf_check_nop (pTHX_ struct CoroSLF *frame) |
822 | { |
838 | { |
823 | return 0; |
839 | return 0; |
|
|
840 | } |
|
|
841 | |
|
|
842 | static int |
|
|
843 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
|
|
844 | { |
|
|
845 | return 1; |
824 | } |
846 | } |
825 | |
847 | |
826 | static UNOP coro_setup_op; |
848 | static UNOP coro_setup_op; |
827 | |
849 | |
828 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
850 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
… | |
… | |
861 | dSP; |
883 | dSP; |
862 | UNOP myop; |
884 | UNOP myop; |
863 | |
885 | |
864 | Zero (&myop, 1, UNOP); |
886 | Zero (&myop, 1, UNOP); |
865 | myop.op_next = Nullop; |
887 | myop.op_next = Nullop; |
|
|
888 | myop.op_type = OP_ENTERSUB; |
866 | myop.op_flags = OPf_WANT_VOID; |
889 | myop.op_flags = OPf_WANT_VOID; |
867 | |
890 | |
868 | PUSHMARK (SP); |
891 | PUSHMARK (SP); |
869 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
892 | PUSHs ((SV *)coro->startcv); |
870 | PUTBACK; |
893 | PUTBACK; |
871 | PL_op = (OP *)&myop; |
894 | PL_op = (OP *)&myop; |
872 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
895 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
873 | SPAGAIN; |
|
|
874 | } |
896 | } |
875 | |
897 | |
876 | /* this newly created coroutine might be run on an existing cctx which most |
898 | /* 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. |
899 | * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. |
878 | */ |
900 | */ |
879 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
901 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
880 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
902 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
881 | |
903 | |
882 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
904 | /* 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; |
905 | coro_setup_op.op_next = PL_op; |
884 | coro_setup_op.op_type = OP_CUSTOM; |
906 | coro_setup_op.op_type = OP_ENTERSUB; |
885 | coro_setup_op.op_ppaddr = pp_slf; |
907 | coro_setup_op.op_ppaddr = pp_slf; |
886 | /* no flags required, as an init function won't be called */ |
908 | /* no flags etc. required, as an init function won't be called */ |
887 | |
909 | |
888 | PL_op = (OP *)&coro_setup_op; |
910 | PL_op = (OP *)&coro_setup_op; |
889 | |
911 | |
890 | /* copy throw, in case it was set before coro_setup */ |
912 | /* copy throw, in case it was set before coro_setup */ |
891 | CORO_THROW = coro->except; |
913 | CORO_THROW = coro->except; |
… | |
… | |
919 | SvREFCNT_dec (GvSV (irsgv)); |
941 | SvREFCNT_dec (GvSV (irsgv)); |
920 | |
942 | |
921 | SvREFCNT_dec (PL_diehook); |
943 | SvREFCNT_dec (PL_diehook); |
922 | SvREFCNT_dec (PL_warnhook); |
944 | SvREFCNT_dec (PL_warnhook); |
923 | |
945 | |
924 | SvREFCNT_dec (CORO_THROW); |
|
|
925 | SvREFCNT_dec (coro->saved_deffh); |
946 | SvREFCNT_dec (coro->saved_deffh); |
926 | SvREFCNT_dec (coro->rouse_cb); |
947 | SvREFCNT_dec (coro->rouse_cb); |
|
|
948 | SvREFCNT_dec (coro->invoke_cb); |
|
|
949 | SvREFCNT_dec (coro->invoke_av); |
927 | |
950 | |
928 | coro_destruct_stacks (aTHX); |
951 | coro_destruct_stacks (aTHX); |
929 | } |
952 | } |
930 | |
953 | |
931 | INLINE void |
954 | INLINE void |
… | |
… | |
1003 | { |
1026 | { |
1004 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1027 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1005 | |
1028 | |
1006 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1029 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1007 | { |
1030 | { |
1008 | runops_proc_t old_runops = PL_runops; |
|
|
1009 | dSP; |
1031 | dSP; |
1010 | GV *gv = CvGV (cx->blk_sub.cv); |
1032 | GV *gv = CvGV (cx->blk_sub.cv); |
1011 | SV *fullname = sv_2mortal (newSV (0)); |
1033 | SV *fullname = sv_2mortal (newSV (0)); |
1012 | |
1034 | |
1013 | if (isGV (gv)) |
1035 | if (isGV (gv)) |
… | |
… | |
1018 | SAVETMPS; |
1040 | SAVETMPS; |
1019 | EXTEND (SP, 3); |
1041 | EXTEND (SP, 3); |
1020 | PUSHMARK (SP); |
1042 | PUSHMARK (SP); |
1021 | PUSHs (&PL_sv_yes); |
1043 | PUSHs (&PL_sv_yes); |
1022 | PUSHs (fullname); |
1044 | PUSHs (fullname); |
1023 | PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
1045 | PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
1024 | PUTBACK; |
1046 | PUTBACK; |
1025 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
1047 | 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); |
1048 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1027 | SPAGAIN; |
1049 | SPAGAIN; |
1028 | FREETMPS; |
1050 | FREETMPS; |
… | |
… | |
1134 | transfer_tail (aTHX); |
1156 | transfer_tail (aTHX); |
1135 | |
1157 | |
1136 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1158 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1137 | PL_restartop = PL_op; |
1159 | PL_restartop = PL_op; |
1138 | perl_run (PL_curinterp); |
1160 | perl_run (PL_curinterp); |
|
|
1161 | /* |
|
|
1162 | * Unfortunately, there is no way to get at the return values of the |
|
|
1163 | * coro body here, as perl_run destroys these |
|
|
1164 | */ |
1139 | |
1165 | |
1140 | /* |
1166 | /* |
1141 | * If perl-run returns we assume exit() was being called or the coro |
1167 | * 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) |
1168 | * 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 |
1169 | * reason for perl_run to return. We try to exit by jumping to the |
… | |
… | |
1435 | |
1461 | |
1436 | coro->slot = 0; |
1462 | coro->slot = 0; |
1437 | } |
1463 | } |
1438 | |
1464 | |
1439 | cctx_destroy (coro->cctx); |
1465 | cctx_destroy (coro->cctx); |
|
|
1466 | SvREFCNT_dec (coro->startcv); |
1440 | SvREFCNT_dec (coro->args); |
1467 | SvREFCNT_dec (coro->args); |
|
|
1468 | SvREFCNT_dec (CORO_THROW); |
1441 | |
1469 | |
1442 | if (coro->next) coro->next->prev = coro->prev; |
1470 | if (coro->next) coro->next->prev = coro->prev; |
1443 | if (coro->prev) coro->prev->next = coro->next; |
1471 | if (coro->prev) coro->prev->next = coro->next; |
1444 | if (coro == coro_first) coro_first = coro->next; |
1472 | if (coro == coro_first) coro_first = coro->next; |
1445 | |
1473 | |
… | |
… | |
1592 | api_is_ready (pTHX_ SV *coro_sv) |
1620 | api_is_ready (pTHX_ SV *coro_sv) |
1593 | { |
1621 | { |
1594 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1622 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1595 | } |
1623 | } |
1596 | |
1624 | |
|
|
1625 | /* expects to own a reference to next->hv */ |
1597 | INLINE void |
1626 | INLINE void |
|
|
1627 | prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
|
|
1628 | { |
|
|
1629 | SV *prev_sv = SvRV (coro_current); |
|
|
1630 | |
|
|
1631 | ta->prev = SvSTATE_hv (prev_sv); |
|
|
1632 | ta->next = next; |
|
|
1633 | |
|
|
1634 | TRANSFER_CHECK (*ta); |
|
|
1635 | |
|
|
1636 | SvRV_set (coro_current, (SV *)next->hv); |
|
|
1637 | |
|
|
1638 | free_coro_mortal (aTHX); |
|
|
1639 | coro_mortal = prev_sv; |
|
|
1640 | } |
|
|
1641 | |
|
|
1642 | static void |
1598 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1643 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1599 | { |
1644 | { |
1600 | SV *prev_sv, *next_sv; |
|
|
1601 | |
|
|
1602 | for (;;) |
1645 | for (;;) |
1603 | { |
1646 | { |
1604 | next_sv = coro_deq (aTHX); |
1647 | SV *next_sv = coro_deq (aTHX); |
1605 | |
1648 | |
1606 | /* nothing to schedule: call the idle handler */ |
|
|
1607 | if (expect_false (!next_sv)) |
1649 | if (expect_true (next_sv)) |
1608 | { |
1650 | { |
|
|
1651 | struct coro *next = SvSTATE_hv (next_sv); |
|
|
1652 | |
|
|
1653 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1654 | if (expect_false (next->flags & CF_DESTROYED)) |
|
|
1655 | SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ |
|
|
1656 | else |
|
|
1657 | { |
|
|
1658 | next->flags &= ~CF_READY; |
|
|
1659 | --coro_nready; |
|
|
1660 | |
|
|
1661 | prepare_schedule_to (aTHX_ ta, next); |
|
|
1662 | break; |
|
|
1663 | } |
|
|
1664 | } |
|
|
1665 | else |
|
|
1666 | { |
|
|
1667 | /* nothing to schedule: call the idle handler */ |
1609 | dSP; |
1668 | dSP; |
1610 | |
1669 | |
1611 | ENTER; |
1670 | ENTER; |
1612 | SAVETMPS; |
1671 | SAVETMPS; |
1613 | |
1672 | |
… | |
… | |
1615 | PUTBACK; |
1674 | PUTBACK; |
1616 | call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); |
1675 | call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); |
1617 | |
1676 | |
1618 | FREETMPS; |
1677 | FREETMPS; |
1619 | LEAVE; |
1678 | LEAVE; |
1620 | continue; |
|
|
1621 | } |
1679 | } |
1622 | |
|
|
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 | { |
|
|
1628 | SvREFCNT_dec (next_sv); |
|
|
1629 | /* coro_nready has already been taken care of by destroy */ |
|
|
1630 | continue; |
|
|
1631 | } |
|
|
1632 | |
|
|
1633 | --coro_nready; |
|
|
1634 | break; |
|
|
1635 | } |
1680 | } |
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 | } |
1681 | } |
1648 | |
1682 | |
1649 | INLINE void |
1683 | INLINE void |
1650 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1684 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1651 | { |
1685 | { |
… | |
… | |
1672 | { |
1706 | { |
1673 | struct coro_transfer_args ta; |
1707 | struct coro_transfer_args ta; |
1674 | |
1708 | |
1675 | prepare_schedule (aTHX_ &ta); |
1709 | prepare_schedule (aTHX_ &ta); |
1676 | TRANSFER (ta, 1); |
1710 | TRANSFER (ta, 1); |
|
|
1711 | } |
|
|
1712 | |
|
|
1713 | static void |
|
|
1714 | api_schedule_to (pTHX_ SV *coro_sv) |
|
|
1715 | { |
|
|
1716 | struct coro_transfer_args ta; |
|
|
1717 | struct coro *next = SvSTATE (coro_sv); |
|
|
1718 | |
|
|
1719 | SvREFCNT_inc_NN (coro_sv); |
|
|
1720 | prepare_schedule_to (aTHX_ &ta, next); |
1677 | } |
1721 | } |
1678 | |
1722 | |
1679 | static int |
1723 | static int |
1680 | api_cede (pTHX) |
1724 | api_cede (pTHX) |
1681 | { |
1725 | { |
… | |
… | |
1730 | else |
1774 | else |
1731 | coro->slot->runops = RUNOPS_DEFAULT; |
1775 | coro->slot->runops = RUNOPS_DEFAULT; |
1732 | } |
1776 | } |
1733 | } |
1777 | } |
1734 | |
1778 | |
|
|
1779 | static void |
|
|
1780 | coro_call_on_destroy (pTHX_ struct coro *coro) |
|
|
1781 | { |
|
|
1782 | SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0); |
|
|
1783 | SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0); |
|
|
1784 | |
|
|
1785 | if (on_destroyp) |
|
|
1786 | { |
|
|
1787 | AV *on_destroy = (AV *)SvRV (*on_destroyp); |
|
|
1788 | |
|
|
1789 | while (AvFILLp (on_destroy) >= 0) |
|
|
1790 | { |
|
|
1791 | dSP; /* don't disturb outer sp */ |
|
|
1792 | SV *cb = av_pop (on_destroy); |
|
|
1793 | |
|
|
1794 | PUSHMARK (SP); |
|
|
1795 | |
|
|
1796 | if (statusp) |
|
|
1797 | { |
|
|
1798 | int i; |
|
|
1799 | AV *status = (AV *)SvRV (*statusp); |
|
|
1800 | EXTEND (SP, AvFILLp (status) + 1); |
|
|
1801 | |
|
|
1802 | for (i = 0; i <= AvFILLp (status); ++i) |
|
|
1803 | PUSHs (AvARRAY (status)[i]); |
|
|
1804 | } |
|
|
1805 | |
|
|
1806 | PUTBACK; |
|
|
1807 | call_sv (sv_2mortal (cb), G_VOID | G_DISCARD); |
|
|
1808 | } |
|
|
1809 | } |
|
|
1810 | } |
|
|
1811 | |
|
|
1812 | static void |
|
|
1813 | slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1814 | { |
|
|
1815 | int i; |
|
|
1816 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1817 | AV *av = newAV (); |
|
|
1818 | |
|
|
1819 | av_extend (av, items - 1); |
|
|
1820 | for (i = 0; i < items; ++i) |
|
|
1821 | av_push (av, SvREFCNT_inc_NN (arg [i])); |
|
|
1822 | |
|
|
1823 | hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); |
|
|
1824 | |
|
|
1825 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
|
|
1826 | api_ready (aTHX_ sv_manager); |
|
|
1827 | |
|
|
1828 | frame->prepare = prepare_schedule; |
|
|
1829 | frame->check = slf_check_repeat; |
|
|
1830 | } |
|
|
1831 | |
|
|
1832 | /*****************************************************************************/ |
|
|
1833 | /* async pool handler */ |
|
|
1834 | |
|
|
1835 | static int |
|
|
1836 | slf_check_pool_handler (pTHX_ struct CoroSLF *frame) |
|
|
1837 | { |
|
|
1838 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1839 | struct coro *coro = (struct coro *)frame->data; |
|
|
1840 | |
|
|
1841 | if (!coro->invoke_cb) |
|
|
1842 | return 1; /* loop till we have invoke */ |
|
|
1843 | else |
|
|
1844 | { |
|
|
1845 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
1846 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
|
|
1847 | |
|
|
1848 | coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv); |
|
|
1849 | |
|
|
1850 | { |
|
|
1851 | dSP; |
|
|
1852 | XPUSHs (sv_2mortal (coro->invoke_cb)); coro->invoke_cb = 0; |
|
|
1853 | PUTBACK; |
|
|
1854 | } |
|
|
1855 | |
|
|
1856 | SvREFCNT_dec (GvAV (PL_defgv)); |
|
|
1857 | GvAV (PL_defgv) = coro->invoke_av; |
|
|
1858 | coro->invoke_av = 0; |
|
|
1859 | |
|
|
1860 | return 0; |
|
|
1861 | } |
|
|
1862 | } |
|
|
1863 | |
|
|
1864 | static void |
|
|
1865 | slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1866 | { |
|
|
1867 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1868 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
|
|
1869 | |
|
|
1870 | if (expect_true (coro->saved_deffh)) |
|
|
1871 | { |
|
|
1872 | /* subsequent iteration */ |
|
|
1873 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
|
|
1874 | coro->saved_deffh = 0; |
|
|
1875 | |
|
|
1876 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
|
|
1877 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
1878 | { |
|
|
1879 | coro->invoke_cb = SvREFCNT_inc_NN ((SV *)cv_coro_terminate); |
|
|
1880 | coro->invoke_av = newAV (); |
|
|
1881 | |
|
|
1882 | frame->prepare = prepare_nop; |
|
|
1883 | } |
|
|
1884 | else |
|
|
1885 | { |
|
|
1886 | av_clear (GvAV (PL_defgv)); |
|
|
1887 | hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0); |
|
|
1888 | |
|
|
1889 | coro->prio = 0; |
|
|
1890 | |
|
|
1891 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
|
|
1892 | api_trace (aTHX_ coro_current, 0); |
|
|
1893 | |
|
|
1894 | frame->prepare = prepare_schedule; |
|
|
1895 | av_push (av_async_pool, SvREFCNT_inc (hv)); |
|
|
1896 | } |
|
|
1897 | } |
|
|
1898 | else |
|
|
1899 | { |
|
|
1900 | /* first iteration, simply fall through */ |
|
|
1901 | frame->prepare = prepare_nop; |
|
|
1902 | } |
|
|
1903 | |
|
|
1904 | frame->check = slf_check_pool_handler; |
|
|
1905 | frame->data = (void *)coro; |
|
|
1906 | } |
|
|
1907 | |
1735 | /*****************************************************************************/ |
1908 | /*****************************************************************************/ |
1736 | /* rouse callback */ |
1909 | /* rouse callback */ |
1737 | |
1910 | |
1738 | #define CORO_MAGIC_type_rouse PERL_MAGIC_ext |
1911 | #define CORO_MAGIC_type_rouse PERL_MAGIC_ext |
1739 | |
1912 | |
… | |
… | |
1744 | SV *data = (SV *)GENSUB_ARG; |
1917 | SV *data = (SV *)GENSUB_ARG; |
1745 | |
1918 | |
1746 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
1919 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
1747 | { |
1920 | { |
1748 | /* first call, set args */ |
1921 | /* first call, set args */ |
1749 | int i; |
|
|
1750 | AV *av = newAV (); |
1922 | AV *av = newAV (); |
1751 | SV *coro = SvRV (data); |
1923 | SV *coro = SvRV (data); |
1752 | |
1924 | |
1753 | SvRV_set (data, (SV *)av); |
1925 | SvRV_set (data, (SV *)av); |
1754 | api_ready (aTHX_ coro); |
1926 | api_ready (aTHX_ coro); |
… | |
… | |
1895 | static void |
2067 | static void |
1896 | slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2068 | slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1897 | { |
2069 | { |
1898 | frame->prepare = prepare_schedule; |
2070 | frame->prepare = prepare_schedule; |
1899 | frame->check = slf_check_nop; |
2071 | frame->check = slf_check_nop; |
|
|
2072 | } |
|
|
2073 | |
|
|
2074 | static void |
|
|
2075 | slf_prepare_schedule_to (pTHX_ struct coro_transfer_args *ta) |
|
|
2076 | { |
|
|
2077 | struct coro *next = (struct coro *)slf_frame.data; |
|
|
2078 | |
|
|
2079 | SvREFCNT_inc_NN (next->hv); |
|
|
2080 | prepare_schedule_to (aTHX_ ta, next); |
|
|
2081 | } |
|
|
2082 | |
|
|
2083 | static void |
|
|
2084 | slf_init_schedule_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2085 | { |
|
|
2086 | if (!items) |
|
|
2087 | croak ("Coro::schedule_to expects a coroutine argument, caught"); |
|
|
2088 | |
|
|
2089 | frame->data = (void *)SvSTATE (arg [0]); |
|
|
2090 | frame->prepare = slf_prepare_schedule_to; |
|
|
2091 | frame->check = slf_check_nop; |
|
|
2092 | } |
|
|
2093 | |
|
|
2094 | static void |
|
|
2095 | slf_init_cede_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2096 | { |
|
|
2097 | api_ready (aTHX_ SvRV (coro_current)); |
|
|
2098 | |
|
|
2099 | slf_init_schedule_to (aTHX_ frame, cv, arg, items); |
1900 | } |
2100 | } |
1901 | |
2101 | |
1902 | static void |
2102 | static void |
1903 | slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2103 | slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1904 | { |
2104 | { |
… | |
… | |
2050 | } |
2250 | } |
2051 | else |
2251 | else |
2052 | slf_argc = 0; |
2252 | slf_argc = 0; |
2053 | |
2253 | |
2054 | PL_op->op_ppaddr = pp_slf; |
2254 | PL_op->op_ppaddr = pp_slf; |
2055 | PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ |
2255 | /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ |
2056 | |
2256 | |
2057 | PL_op = (OP *)&slf_restore; |
2257 | PL_op = (OP *)&slf_restore; |
2058 | } |
2258 | } |
2059 | |
2259 | |
2060 | /*****************************************************************************/ |
2260 | /*****************************************************************************/ |
… | |
… | |
2283 | { |
2483 | { |
2284 | if (items >= 2) |
2484 | if (items >= 2) |
2285 | { |
2485 | { |
2286 | /* callback form */ |
2486 | /* callback form */ |
2287 | AV *av = (AV *)SvRV (arg [0]); |
2487 | AV *av = (AV *)SvRV (arg [0]); |
2288 | HV *st; |
2488 | CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); |
2289 | GV *gvp; |
|
|
2290 | CV *cb_cv = sv_2cv (arg [1], &st, &gvp, 0); |
|
|
2291 | |
2489 | |
2292 | av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); |
2490 | av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); |
2293 | |
2491 | |
2294 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2492 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2295 | coro_semaphore_adjust (aTHX_ av, 0); |
2493 | coro_semaphore_adjust (aTHX_ av, 0); |
… | |
… | |
2381 | dXSARGS; |
2579 | dXSARGS; |
2382 | AV *state = (AV *)GENSUB_ARG; |
2580 | AV *state = (AV *)GENSUB_ARG; |
2383 | SV *coro = av_pop (state); |
2581 | SV *coro = av_pop (state); |
2384 | SV *data_sv = newSV (sizeof (struct io_state)); |
2582 | SV *data_sv = newSV (sizeof (struct io_state)); |
2385 | |
2583 | |
2386 | av_extend (state, items); |
2584 | av_extend (state, items - 1); |
2387 | |
2585 | |
2388 | sv_upgrade (data_sv, SVt_PV); |
2586 | sv_upgrade (data_sv, SVt_PV); |
2389 | SvCUR_set (data_sv, sizeof (struct io_state)); |
2587 | SvCUR_set (data_sv, sizeof (struct io_state)); |
2390 | SvPOK_only (data_sv); |
2588 | SvPOK_only (data_sv); |
2391 | |
2589 | |
… | |
… | |
2567 | |
2765 | |
2568 | { |
2766 | { |
2569 | SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); |
2767 | SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); |
2570 | |
2768 | |
2571 | if (!PL_custom_op_names) PL_custom_op_names = newHV (); |
2769 | if (!PL_custom_op_names) PL_custom_op_names = newHV (); |
2572 | hv_store_ent (PL_custom_op_names, slf, |
2770 | hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0); |
2573 | newSVpv ("coro_slf", 0), 0); |
|
|
2574 | |
2771 | |
2575 | if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); |
2772 | if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); |
2576 | hv_store_ent (PL_custom_op_descs, slf, |
2773 | hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0); |
2577 | newSVpv ("coro schedule like function", 0), 0); |
|
|
2578 | } |
2774 | } |
2579 | |
2775 | |
2580 | coroapi.ver = CORO_API_VERSION; |
2776 | coroapi.ver = CORO_API_VERSION; |
2581 | coroapi.rev = CORO_API_REVISION; |
2777 | coroapi.rev = CORO_API_REVISION; |
2582 | |
2778 | |
… | |
… | |
2601 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
2797 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
2602 | } |
2798 | } |
2603 | |
2799 | |
2604 | SV * |
2800 | SV * |
2605 | new (char *klass, ...) |
2801 | new (char *klass, ...) |
|
|
2802 | ALIAS: |
|
|
2803 | Coro::new = 1 |
2606 | CODE: |
2804 | CODE: |
2607 | { |
2805 | { |
2608 | struct coro *coro; |
2806 | struct coro *coro; |
2609 | MAGIC *mg; |
2807 | MAGIC *mg; |
2610 | HV *hv; |
2808 | HV *hv; |
|
|
2809 | CV *cb; |
2611 | int i; |
2810 | int i; |
|
|
2811 | |
|
|
2812 | if (items > 1) |
|
|
2813 | { |
|
|
2814 | cb = coro_sv_2cv (aTHX_ ST (1)); |
|
|
2815 | |
|
|
2816 | if (!ix) |
|
|
2817 | { |
|
|
2818 | if (CvISXSUB (cb)) |
|
|
2819 | croak ("Coro::State doesn't support XS functions as coroutine start, caught"); |
|
|
2820 | |
|
|
2821 | if (!CvROOT (cb)) |
|
|
2822 | croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught"); |
|
|
2823 | } |
|
|
2824 | } |
2612 | |
2825 | |
2613 | Newz (0, coro, 1, struct coro); |
2826 | Newz (0, coro, 1, struct coro); |
2614 | coro->args = newAV (); |
2827 | coro->args = newAV (); |
2615 | coro->flags = CF_NEW; |
2828 | coro->flags = CF_NEW; |
2616 | |
2829 | |
… | |
… | |
2621 | coro->hv = hv = newHV (); |
2834 | coro->hv = hv = newHV (); |
2622 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
2835 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
2623 | mg->mg_flags |= MGf_DUP; |
2836 | mg->mg_flags |= MGf_DUP; |
2624 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
2837 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
2625 | |
2838 | |
|
|
2839 | if (items > 1) |
|
|
2840 | { |
2626 | av_extend (coro->args, items - 1); |
2841 | av_extend (coro->args, items - 1 + ix - 1); |
|
|
2842 | |
|
|
2843 | if (ix) |
|
|
2844 | { |
|
|
2845 | av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb)); |
|
|
2846 | cb = cv_coro_run; |
|
|
2847 | } |
|
|
2848 | |
|
|
2849 | coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb); |
|
|
2850 | |
2627 | for (i = 1; i < items; i++) |
2851 | for (i = 2; i < items; i++) |
2628 | av_push (coro->args, newSVsv (ST (i))); |
2852 | av_push (coro->args, newSVsv (ST (i))); |
|
|
2853 | } |
2629 | } |
2854 | } |
2630 | OUTPUT: |
2855 | OUTPUT: |
2631 | RETVAL |
2856 | RETVAL |
2632 | |
2857 | |
2633 | void |
2858 | void |
… | |
… | |
2831 | |
3056 | |
2832 | BOOT: |
3057 | BOOT: |
2833 | { |
3058 | { |
2834 | int i; |
3059 | int i; |
2835 | |
3060 | |
2836 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
|
|
2837 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3061 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
2838 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3062 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
2839 | |
3063 | cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); |
|
|
3064 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
2840 | coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); |
3065 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
2841 | SvREADONLY_on (coro_current); |
3066 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
|
|
3067 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
|
|
3068 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
|
|
3069 | |
|
|
3070 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
|
|
3071 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
|
|
3072 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
|
|
3073 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
2842 | |
3074 | |
2843 | coro_stash = gv_stashpv ("Coro", TRUE); |
3075 | coro_stash = gv_stashpv ("Coro", TRUE); |
2844 | |
3076 | |
2845 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
3077 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
2846 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
3078 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
… | |
… | |
2854 | |
3086 | |
2855 | { |
3087 | { |
2856 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
3088 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
2857 | |
3089 | |
2858 | coroapi.schedule = api_schedule; |
3090 | coroapi.schedule = api_schedule; |
|
|
3091 | coroapi.schedule_to = api_schedule_to; |
2859 | coroapi.cede = api_cede; |
3092 | coroapi.cede = api_cede; |
2860 | coroapi.cede_notself = api_cede_notself; |
3093 | coroapi.cede_notself = api_cede_notself; |
2861 | coroapi.ready = api_ready; |
3094 | coroapi.ready = api_ready; |
2862 | coroapi.is_ready = api_is_ready; |
3095 | coroapi.is_ready = api_is_ready; |
2863 | coroapi.nready = coro_nready; |
3096 | coroapi.nready = coro_nready; |
… | |
… | |
2868 | SvREADONLY_on (sv); |
3101 | SvREADONLY_on (sv); |
2869 | } |
3102 | } |
2870 | } |
3103 | } |
2871 | |
3104 | |
2872 | void |
3105 | void |
|
|
3106 | terminate (...) |
|
|
3107 | CODE: |
|
|
3108 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
|
|
3109 | |
|
|
3110 | void |
2873 | schedule (...) |
3111 | schedule (...) |
2874 | CODE: |
3112 | CODE: |
2875 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
3113 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
|
|
3114 | |
|
|
3115 | void |
|
|
3116 | schedule_to (...) |
|
|
3117 | CODE: |
|
|
3118 | CORO_EXECUTE_SLF_XS (slf_init_schedule_to); |
|
|
3119 | |
|
|
3120 | void |
|
|
3121 | cede_to (...) |
|
|
3122 | CODE: |
|
|
3123 | CORO_EXECUTE_SLF_XS (slf_init_cede_to); |
2876 | |
3124 | |
2877 | void |
3125 | void |
2878 | cede (...) |
3126 | cede (...) |
2879 | CODE: |
3127 | CODE: |
2880 | CORO_EXECUTE_SLF_XS (slf_init_cede); |
3128 | CORO_EXECUTE_SLF_XS (slf_init_cede); |
2881 | |
3129 | |
2882 | void |
3130 | void |
2883 | cede_notself (...) |
3131 | cede_notself (...) |
2884 | CODE: |
3132 | CODE: |
2885 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
3133 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
|
|
3134 | |
|
|
3135 | void |
|
|
3136 | _cancel (Coro::State self) |
|
|
3137 | CODE: |
|
|
3138 | coro_state_destroy (aTHX_ self); |
|
|
3139 | coro_call_on_destroy (aTHX_ self); |
2886 | |
3140 | |
2887 | void |
3141 | void |
2888 | _set_current (SV *current) |
3142 | _set_current (SV *current) |
2889 | PROTOTYPE: $ |
3143 | PROTOTYPE: $ |
2890 | CODE: |
3144 | CODE: |
… | |
… | |
2935 | CODE: |
3189 | CODE: |
2936 | RETVAL = coro_nready; |
3190 | RETVAL = coro_nready; |
2937 | OUTPUT: |
3191 | OUTPUT: |
2938 | RETVAL |
3192 | RETVAL |
2939 | |
3193 | |
2940 | # for async_pool speedup |
|
|
2941 | void |
3194 | void |
2942 | _pool_1 (SV *cb) |
3195 | _pool_handler (...) |
2943 | CODE: |
3196 | CODE: |
2944 | { |
3197 | CORO_EXECUTE_SLF_XS (slf_init_pool_handler); |
2945 | HV *hv = (HV *)SvRV (coro_current); |
|
|
2946 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
|
|
2947 | AV *defav = GvAV (PL_defgv); |
|
|
2948 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
|
|
2949 | AV *invoke_av; |
|
|
2950 | int i, len; |
|
|
2951 | |
3198 | |
2952 | if (!invoke) |
3199 | void |
|
|
3200 | async_pool (SV *cv, ...) |
|
|
3201 | PROTOTYPE: &@ |
|
|
3202 | PPCODE: |
|
|
3203 | { |
|
|
3204 | HV *hv = (HV *)av_pop (av_async_pool); |
|
|
3205 | AV *av = newAV (); |
|
|
3206 | SV *cb = ST (0); |
|
|
3207 | int i; |
|
|
3208 | |
|
|
3209 | av_extend (av, items - 2); |
|
|
3210 | for (i = 1; i < items; ++i) |
|
|
3211 | av_push (av, SvREFCNT_inc_NN (ST (i))); |
|
|
3212 | |
|
|
3213 | if ((SV *)hv == &PL_sv_undef) |
2953 | { |
3214 | { |
2954 | SV *old = PL_diehook; |
3215 | PUSHMARK (SP); |
2955 | PL_diehook = 0; |
3216 | EXTEND (SP, 2); |
2956 | SvREFCNT_dec (old); |
3217 | PUSHs (sv_Coro); |
2957 | croak ("\3async_pool terminate\2\n"); |
3218 | PUSHs ((SV *)cv_pool_handler); |
|
|
3219 | PUTBACK; |
|
|
3220 | call_sv ((SV *)cv_coro_state_new, G_SCALAR); |
|
|
3221 | SPAGAIN; |
|
|
3222 | |
|
|
3223 | hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); |
2958 | } |
3224 | } |
2959 | |
3225 | |
2960 | SvREFCNT_dec (coro->saved_deffh); |
|
|
2961 | coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv); |
|
|
2962 | |
|
|
2963 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
2964 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
|
|
2965 | |
|
|
2966 | invoke_av = (AV *)SvRV (invoke); |
|
|
2967 | len = av_len (invoke_av); |
|
|
2968 | |
|
|
2969 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
|
|
2970 | |
|
|
2971 | if (len > 0) |
|
|
2972 | { |
3226 | { |
2973 | av_fill (defav, len - 1); |
3227 | struct coro *coro = SvSTATE_hv (hv); |
2974 | for (i = 0; i < len; ++i) |
3228 | |
2975 | av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); |
3229 | assert (!coro->invoke_cb); |
|
|
3230 | assert (!coro->invoke_av); |
|
|
3231 | coro->invoke_cb = SvREFCNT_inc (cb); |
|
|
3232 | coro->invoke_av = av; |
2976 | } |
3233 | } |
2977 | } |
|
|
2978 | |
3234 | |
2979 | void |
3235 | api_ready (aTHX_ (SV *)hv); |
2980 | _pool_2 (SV *cb) |
|
|
2981 | CODE: |
|
|
2982 | { |
|
|
2983 | HV *hv = (HV *)SvRV (coro_current); |
|
|
2984 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
|
|
2985 | |
3236 | |
2986 | sv_setsv (cb, &PL_sv_undef); |
3237 | if (GIMME_V != G_VOID) |
2987 | |
3238 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); |
2988 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
3239 | else |
2989 | coro->saved_deffh = 0; |
|
|
2990 | |
|
|
2991 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
|
|
2992 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
2993 | { |
|
|
2994 | SV *old = PL_diehook; |
|
|
2995 | PL_diehook = 0; |
|
|
2996 | SvREFCNT_dec (old); |
3240 | SvREFCNT_dec (hv); |
2997 | croak ("\3async_pool terminate\2\n"); |
|
|
2998 | } |
|
|
2999 | |
|
|
3000 | av_clear (GvAV (PL_defgv)); |
|
|
3001 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
3002 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
|
|
3003 | |
|
|
3004 | coro->prio = 0; |
|
|
3005 | |
|
|
3006 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
|
|
3007 | api_trace (aTHX_ coro_current, 0); |
|
|
3008 | |
|
|
3009 | av_push (av_async_pool, newSVsv (coro_current)); |
|
|
3010 | } |
3241 | } |
3011 | |
3242 | |
3012 | SV * |
3243 | SV * |
3013 | rouse_cb () |
3244 | rouse_cb () |
3014 | PROTOTYPE: |
3245 | PROTOTYPE: |
… | |
… | |
3188 | |
3419 | |
3189 | void |
3420 | void |
3190 | _register (char *target, char *proto, SV *req) |
3421 | _register (char *target, char *proto, SV *req) |
3191 | CODE: |
3422 | CODE: |
3192 | { |
3423 | { |
3193 | HV *st; |
3424 | CV *req_cv = coro_sv_2cv (aTHX_ req); |
3194 | GV *gvp; |
|
|
3195 | CV *req_cv = sv_2cv (req, &st, &gvp, 0); |
|
|
3196 | /* newXSproto doesn't return the CV on 5.8 */ |
3425 | /* newXSproto doesn't return the CV on 5.8 */ |
3197 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
3426 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
3198 | sv_setpv ((SV *)slf_cv, proto); |
3427 | sv_setpv ((SV *)slf_cv, proto); |
3199 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
3428 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
3200 | } |
3429 | } |