… | |
… | |
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 | { |
… | |
… | |
449 | else |
472 | else |
450 | { |
473 | { |
451 | #if CORO_PREFER_PERL_FUNCTIONS |
474 | #if CORO_PREFER_PERL_FUNCTIONS |
452 | /* this is probably cleaner? but also slower! */ |
475 | /* this is probably cleaner? but also slower! */ |
453 | /* in practise, it seems to be less stable */ |
476 | /* in practise, it seems to be less stable */ |
454 | CV *cp = Perl_cv_clone (cv); |
477 | CV *cp = Perl_cv_clone (aTHX_ cv); |
455 | CvPADLIST (cv) = CvPADLIST (cp); |
478 | CvPADLIST (cv) = CvPADLIST (cp); |
456 | CvPADLIST (cp) = 0; |
479 | CvPADLIST (cp) = 0; |
457 | SvREFCNT_dec (cp); |
480 | SvREFCNT_dec (cp); |
458 | #else |
481 | #else |
459 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
482 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
… | |
… | |
603 | * of perl.c:init_stacks, except that it uses less memory |
626 | * of perl.c:init_stacks, except that it uses less memory |
604 | * on the (sometimes correct) assumption that coroutines do |
627 | * on the (sometimes correct) assumption that coroutines do |
605 | * not usually need a lot of stackspace. |
628 | * not usually need a lot of stackspace. |
606 | */ |
629 | */ |
607 | #if CORO_PREFER_PERL_FUNCTIONS |
630 | #if CORO_PREFER_PERL_FUNCTIONS |
608 | # define coro_init_stacks init_stacks |
631 | # define coro_init_stacks(thx) init_stacks () |
609 | #else |
632 | #else |
610 | static void |
633 | static void |
611 | coro_init_stacks (pTHX) |
634 | coro_init_stacks (pTHX) |
612 | { |
635 | { |
613 | PL_curstackinfo = new_stackinfo(32, 8); |
636 | PL_curstackinfo = new_stackinfo(32, 8); |
… | |
… | |
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 | */ |
… | |
… | |
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_CUSTOM; |
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); |
1755 | SvREFCNT_dec (coro); |
1927 | SvREFCNT_dec (coro); |
1756 | |
1928 | |
|
|
1929 | /* better take a full copy of the arguments */ |
1757 | while (items--) |
1930 | while (items--) |
1758 | av_store (av, items, SvREFCNT_inc_NN (ST (items))); |
1931 | av_store (av, items, newSVsv (ST (items))); |
1759 | } |
1932 | } |
1760 | |
1933 | |
1761 | XSRETURN_EMPTY; |
1934 | XSRETURN_EMPTY; |
1762 | } |
1935 | } |
1763 | |
1936 | |
… | |
… | |
1778 | AV *av = (AV *)SvRV (data); |
1951 | AV *av = (AV *)SvRV (data); |
1779 | int i; |
1952 | int i; |
1780 | |
1953 | |
1781 | EXTEND (SP, AvFILLp (av) + 1); |
1954 | EXTEND (SP, AvFILLp (av) + 1); |
1782 | for (i = 0; i <= AvFILLp (av); ++i) |
1955 | for (i = 0; i <= AvFILLp (av); ++i) |
1783 | PUSHs (AvARRAY (av)[i]); |
1956 | PUSHs (sv_2mortal (AvARRAY (av)[i])); |
|
|
1957 | |
|
|
1958 | /* we have stolen the elements, so ste length to zero and free */ |
|
|
1959 | AvFILLp (av) = -1; |
|
|
1960 | av_undef (av); |
1784 | |
1961 | |
1785 | PUTBACK; |
1962 | PUTBACK; |
1786 | } |
1963 | } |
1787 | |
1964 | |
1788 | return 0; |
1965 | return 0; |
… | |
… | |
1828 | struct coro *coro = SvSTATE_hv (hv); |
2005 | struct coro *coro = SvSTATE_hv (hv); |
1829 | SV *data = newRV_inc ((SV *)hv); |
2006 | SV *data = newRV_inc ((SV *)hv); |
1830 | SV *cb = gensub (aTHX_ coro_rouse_callback, (void *)data); |
2007 | SV *cb = gensub (aTHX_ coro_rouse_callback, (void *)data); |
1831 | |
2008 | |
1832 | sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0); |
2009 | sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0); |
|
|
2010 | SvREFCNT_dec (data); /* magicext increases the refcount */ |
1833 | |
2011 | |
1834 | SvREFCNT_dec (coro->rouse_cb); |
2012 | SvREFCNT_dec (coro->rouse_cb); |
1835 | coro->rouse_cb = SvREFCNT_inc_NN (cb); |
2013 | coro->rouse_cb = SvREFCNT_inc_NN (cb); |
1836 | |
2014 | |
1837 | return cb; |
2015 | return cb; |
… | |
… | |
1889 | static void |
2067 | static void |
1890 | 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) |
1891 | { |
2069 | { |
1892 | frame->prepare = prepare_schedule; |
2070 | frame->prepare = prepare_schedule; |
1893 | 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); |
1894 | } |
2100 | } |
1895 | |
2101 | |
1896 | static void |
2102 | static void |
1897 | 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) |
1898 | { |
2104 | { |
… | |
… | |
2137 | SV **ary; |
2343 | SV **ary; |
2138 | |
2344 | |
2139 | /* unfortunately, building manually saves memory */ |
2345 | /* unfortunately, building manually saves memory */ |
2140 | Newx (ary, 2, SV *); |
2346 | Newx (ary, 2, SV *); |
2141 | AvALLOC (av) = ary; |
2347 | AvALLOC (av) = ary; |
2142 | AvARRAY (av) = ary; |
2348 | /*AvARRAY (av) = ary;*/ |
|
|
2349 | SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ |
2143 | AvMAX (av) = 1; |
2350 | AvMAX (av) = 1; |
2144 | AvFILLp (av) = 0; |
2351 | AvFILLp (av) = 0; |
2145 | ary [0] = newSViv (count); |
2352 | ary [0] = newSViv (count); |
2146 | |
2353 | |
2147 | return newRV_noinc ((SV *)av); |
2354 | return newRV_noinc ((SV *)av); |
… | |
… | |
2167 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2374 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2168 | AvARRAY (av)[1] = count_sv; |
2375 | AvARRAY (av)[1] = count_sv; |
2169 | cb = av_shift (av); |
2376 | cb = av_shift (av); |
2170 | |
2377 | |
2171 | if (SvOBJECT (cb)) |
2378 | if (SvOBJECT (cb)) |
|
|
2379 | { |
2172 | api_ready (aTHX_ cb); |
2380 | api_ready (aTHX_ cb); |
2173 | else |
2381 | --count; |
2174 | croak ("callbacks not yet supported"); |
2382 | } |
|
|
2383 | else if (SvTYPE (cb) == SVt_PVCV) |
|
|
2384 | { |
|
|
2385 | dSP; |
|
|
2386 | PUSHMARK (SP); |
|
|
2387 | XPUSHs (sv_2mortal (newRV_inc ((SV *)av))); |
|
|
2388 | PUTBACK; |
|
|
2389 | call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR); |
|
|
2390 | } |
2175 | |
2391 | |
2176 | SvREFCNT_dec (cb); |
2392 | SvREFCNT_dec (cb); |
2177 | |
|
|
2178 | --count; |
|
|
2179 | } |
2393 | } |
2180 | } |
2394 | } |
2181 | |
2395 | |
2182 | static void |
2396 | static void |
2183 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
2397 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
… | |
… | |
2265 | } |
2479 | } |
2266 | |
2480 | |
2267 | static void |
2481 | static void |
2268 | slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2482 | slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2269 | { |
2483 | { |
|
|
2484 | if (items >= 2) |
|
|
2485 | { |
|
|
2486 | /* callback form */ |
|
|
2487 | AV *av = (AV *)SvRV (arg [0]); |
|
|
2488 | CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); |
|
|
2489 | |
|
|
2490 | av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); |
|
|
2491 | |
|
|
2492 | if (SvIVX (AvARRAY (av)[0]) > 0) |
|
|
2493 | coro_semaphore_adjust (aTHX_ av, 0); |
|
|
2494 | |
|
|
2495 | frame->prepare = prepare_nop; |
|
|
2496 | frame->check = slf_check_nop; |
|
|
2497 | } |
|
|
2498 | else |
|
|
2499 | { |
2270 | slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); |
2500 | slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); |
2271 | frame->check = slf_check_semaphore_wait; |
2501 | frame->check = slf_check_semaphore_wait; |
|
|
2502 | } |
2272 | } |
2503 | } |
2273 | |
2504 | |
2274 | /* signal */ |
2505 | /* signal */ |
2275 | |
2506 | |
2276 | static void |
2507 | static void |
… | |
… | |
2348 | dXSARGS; |
2579 | dXSARGS; |
2349 | AV *state = (AV *)GENSUB_ARG; |
2580 | AV *state = (AV *)GENSUB_ARG; |
2350 | SV *coro = av_pop (state); |
2581 | SV *coro = av_pop (state); |
2351 | SV *data_sv = newSV (sizeof (struct io_state)); |
2582 | SV *data_sv = newSV (sizeof (struct io_state)); |
2352 | |
2583 | |
2353 | av_extend (state, items); |
2584 | av_extend (state, items - 1); |
2354 | |
2585 | |
2355 | sv_upgrade (data_sv, SVt_PV); |
2586 | sv_upgrade (data_sv, SVt_PV); |
2356 | SvCUR_set (data_sv, sizeof (struct io_state)); |
2587 | SvCUR_set (data_sv, sizeof (struct io_state)); |
2357 | SvPOK_only (data_sv); |
2588 | SvPOK_only (data_sv); |
2358 | |
2589 | |
… | |
… | |
2534 | |
2765 | |
2535 | { |
2766 | { |
2536 | SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); |
2767 | SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); |
2537 | |
2768 | |
2538 | if (!PL_custom_op_names) PL_custom_op_names = newHV (); |
2769 | if (!PL_custom_op_names) PL_custom_op_names = newHV (); |
2539 | hv_store_ent (PL_custom_op_names, slf, |
2770 | hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0); |
2540 | newSVpv ("coro_slf", 0), 0); |
|
|
2541 | |
2771 | |
2542 | if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); |
2772 | if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); |
2543 | hv_store_ent (PL_custom_op_descs, slf, |
2773 | hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0); |
2544 | newSVpv ("coro schedule like function", 0), 0); |
|
|
2545 | } |
2774 | } |
2546 | |
2775 | |
2547 | coroapi.ver = CORO_API_VERSION; |
2776 | coroapi.ver = CORO_API_VERSION; |
2548 | coroapi.rev = CORO_API_REVISION; |
2777 | coroapi.rev = CORO_API_REVISION; |
2549 | |
2778 | |
… | |
… | |
2568 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
2797 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
2569 | } |
2798 | } |
2570 | |
2799 | |
2571 | SV * |
2800 | SV * |
2572 | new (char *klass, ...) |
2801 | new (char *klass, ...) |
|
|
2802 | ALIAS: |
|
|
2803 | Coro::new = 1 |
2573 | CODE: |
2804 | CODE: |
2574 | { |
2805 | { |
2575 | struct coro *coro; |
2806 | struct coro *coro; |
2576 | MAGIC *mg; |
2807 | MAGIC *mg; |
2577 | HV *hv; |
2808 | HV *hv; |
|
|
2809 | CV *cb; |
2578 | 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 | } |
2579 | |
2825 | |
2580 | Newz (0, coro, 1, struct coro); |
2826 | Newz (0, coro, 1, struct coro); |
2581 | coro->args = newAV (); |
2827 | coro->args = newAV (); |
2582 | coro->flags = CF_NEW; |
2828 | coro->flags = CF_NEW; |
2583 | |
2829 | |
… | |
… | |
2588 | coro->hv = hv = newHV (); |
2834 | coro->hv = hv = newHV (); |
2589 | 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); |
2590 | mg->mg_flags |= MGf_DUP; |
2836 | mg->mg_flags |= MGf_DUP; |
2591 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
2837 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
2592 | |
2838 | |
|
|
2839 | if (items > 1) |
|
|
2840 | { |
2593 | 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 | |
2594 | for (i = 1; i < items; i++) |
2851 | for (i = 2; i < items; i++) |
2595 | av_push (coro->args, newSVsv (ST (i))); |
2852 | av_push (coro->args, newSVsv (ST (i))); |
|
|
2853 | } |
2596 | } |
2854 | } |
2597 | OUTPUT: |
2855 | OUTPUT: |
2598 | RETVAL |
2856 | RETVAL |
2599 | |
2857 | |
2600 | void |
2858 | void |
… | |
… | |
2798 | |
3056 | |
2799 | BOOT: |
3057 | BOOT: |
2800 | { |
3058 | { |
2801 | int i; |
3059 | int i; |
2802 | |
3060 | |
2803 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
|
|
2804 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3061 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
2805 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3062 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
2806 | |
3063 | cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); |
|
|
3064 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
2807 | coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); |
3065 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
2808 | 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); |
2809 | |
3074 | |
2810 | coro_stash = gv_stashpv ("Coro", TRUE); |
3075 | coro_stash = gv_stashpv ("Coro", TRUE); |
2811 | |
3076 | |
2812 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
3077 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
2813 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
3078 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
… | |
… | |
2821 | |
3086 | |
2822 | { |
3087 | { |
2823 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
3088 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
2824 | |
3089 | |
2825 | coroapi.schedule = api_schedule; |
3090 | coroapi.schedule = api_schedule; |
|
|
3091 | coroapi.schedule_to = api_schedule_to; |
2826 | coroapi.cede = api_cede; |
3092 | coroapi.cede = api_cede; |
2827 | coroapi.cede_notself = api_cede_notself; |
3093 | coroapi.cede_notself = api_cede_notself; |
2828 | coroapi.ready = api_ready; |
3094 | coroapi.ready = api_ready; |
2829 | coroapi.is_ready = api_is_ready; |
3095 | coroapi.is_ready = api_is_ready; |
2830 | coroapi.nready = coro_nready; |
3096 | coroapi.nready = coro_nready; |
… | |
… | |
2835 | SvREADONLY_on (sv); |
3101 | SvREADONLY_on (sv); |
2836 | } |
3102 | } |
2837 | } |
3103 | } |
2838 | |
3104 | |
2839 | void |
3105 | void |
|
|
3106 | terminate (...) |
|
|
3107 | CODE: |
|
|
3108 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
|
|
3109 | |
|
|
3110 | void |
2840 | schedule (...) |
3111 | schedule (...) |
2841 | CODE: |
3112 | CODE: |
2842 | 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); |
2843 | |
3124 | |
2844 | void |
3125 | void |
2845 | cede (...) |
3126 | cede (...) |
2846 | CODE: |
3127 | CODE: |
2847 | CORO_EXECUTE_SLF_XS (slf_init_cede); |
3128 | CORO_EXECUTE_SLF_XS (slf_init_cede); |
2848 | |
3129 | |
2849 | void |
3130 | void |
2850 | cede_notself (...) |
3131 | cede_notself (...) |
2851 | CODE: |
3132 | CODE: |
2852 | 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); |
2853 | |
3140 | |
2854 | void |
3141 | void |
2855 | _set_current (SV *current) |
3142 | _set_current (SV *current) |
2856 | PROTOTYPE: $ |
3143 | PROTOTYPE: $ |
2857 | CODE: |
3144 | CODE: |
… | |
… | |
2902 | CODE: |
3189 | CODE: |
2903 | RETVAL = coro_nready; |
3190 | RETVAL = coro_nready; |
2904 | OUTPUT: |
3191 | OUTPUT: |
2905 | RETVAL |
3192 | RETVAL |
2906 | |
3193 | |
2907 | # for async_pool speedup |
|
|
2908 | void |
3194 | void |
2909 | _pool_1 (SV *cb) |
3195 | _pool_handler (...) |
2910 | CODE: |
3196 | CODE: |
2911 | { |
3197 | CORO_EXECUTE_SLF_XS (slf_init_pool_handler); |
2912 | HV *hv = (HV *)SvRV (coro_current); |
|
|
2913 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
|
|
2914 | AV *defav = GvAV (PL_defgv); |
|
|
2915 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
|
|
2916 | AV *invoke_av; |
|
|
2917 | int i, len; |
|
|
2918 | |
3198 | |
2919 | 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) |
2920 | { |
3214 | { |
2921 | SV *old = PL_diehook; |
3215 | PUSHMARK (SP); |
2922 | PL_diehook = 0; |
3216 | EXTEND (SP, 2); |
2923 | SvREFCNT_dec (old); |
3217 | PUSHs (sv_Coro); |
2924 | 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)); |
2925 | } |
3224 | } |
2926 | |
3225 | |
2927 | SvREFCNT_dec (coro->saved_deffh); |
|
|
2928 | coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv); |
|
|
2929 | |
|
|
2930 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
2931 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
|
|
2932 | |
|
|
2933 | invoke_av = (AV *)SvRV (invoke); |
|
|
2934 | len = av_len (invoke_av); |
|
|
2935 | |
|
|
2936 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
|
|
2937 | |
|
|
2938 | if (len > 0) |
|
|
2939 | { |
3226 | { |
2940 | av_fill (defav, len - 1); |
3227 | struct coro *coro = SvSTATE_hv (hv); |
2941 | for (i = 0; i < len; ++i) |
3228 | |
2942 | 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; |
2943 | } |
3233 | } |
2944 | } |
|
|
2945 | |
3234 | |
2946 | void |
3235 | api_ready (aTHX_ (SV *)hv); |
2947 | _pool_2 (SV *cb) |
|
|
2948 | CODE: |
|
|
2949 | { |
|
|
2950 | HV *hv = (HV *)SvRV (coro_current); |
|
|
2951 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
|
|
2952 | |
3236 | |
2953 | sv_setsv (cb, &PL_sv_undef); |
3237 | if (GIMME_V != G_VOID) |
2954 | |
3238 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); |
2955 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
3239 | else |
2956 | coro->saved_deffh = 0; |
|
|
2957 | |
|
|
2958 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
|
|
2959 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
2960 | { |
|
|
2961 | SV *old = PL_diehook; |
|
|
2962 | PL_diehook = 0; |
|
|
2963 | SvREFCNT_dec (old); |
3240 | SvREFCNT_dec (hv); |
2964 | croak ("\3async_pool terminate\2\n"); |
|
|
2965 | } |
|
|
2966 | |
|
|
2967 | av_clear (GvAV (PL_defgv)); |
|
|
2968 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
2969 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
|
|
2970 | |
|
|
2971 | coro->prio = 0; |
|
|
2972 | |
|
|
2973 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
|
|
2974 | api_trace (aTHX_ coro_current, 0); |
|
|
2975 | |
|
|
2976 | av_push (av_async_pool, newSVsv (coro_current)); |
|
|
2977 | } |
3241 | } |
2978 | |
3242 | |
2979 | SV * |
3243 | SV * |
2980 | rouse_cb () |
3244 | rouse_cb () |
2981 | PROTOTYPE: |
3245 | PROTOTYPE: |
… | |
… | |
2983 | RETVAL = coro_new_rouse_cb (aTHX); |
3247 | RETVAL = coro_new_rouse_cb (aTHX); |
2984 | OUTPUT: |
3248 | OUTPUT: |
2985 | RETVAL |
3249 | RETVAL |
2986 | |
3250 | |
2987 | void |
3251 | void |
2988 | rouse_wait (SV *cb = 0) |
3252 | rouse_wait (...) |
2989 | PROTOTYPE: ;$ |
3253 | PROTOTYPE: ;$ |
2990 | PPCODE: |
3254 | PPCODE: |
2991 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
3255 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
2992 | |
3256 | |
2993 | |
3257 | |
… | |
… | |
3030 | adjust = 1 |
3294 | adjust = 1 |
3031 | CODE: |
3295 | CODE: |
3032 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
3296 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
3033 | |
3297 | |
3034 | void |
3298 | void |
3035 | down (SV *self) |
3299 | down (...) |
3036 | CODE: |
3300 | CODE: |
3037 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
3301 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
3038 | |
3302 | |
3039 | void |
3303 | void |
3040 | wait (SV *self) |
3304 | wait (...) |
3041 | CODE: |
3305 | CODE: |
3042 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait); |
3306 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait); |
3043 | |
3307 | |
3044 | void |
3308 | void |
3045 | try (SV *self) |
3309 | try (SV *self) |
… | |
… | |
3088 | ); |
3352 | ); |
3089 | OUTPUT: |
3353 | OUTPUT: |
3090 | RETVAL |
3354 | RETVAL |
3091 | |
3355 | |
3092 | void |
3356 | void |
3093 | wait (SV *self) |
3357 | wait (...) |
3094 | CODE: |
3358 | CODE: |
3095 | CORO_EXECUTE_SLF_XS (slf_init_signal_wait); |
3359 | CORO_EXECUTE_SLF_XS (slf_init_signal_wait); |
3096 | |
3360 | |
3097 | void |
3361 | void |
3098 | broadcast (SV *self) |
3362 | broadcast (SV *self) |
… | |
… | |
3155 | |
3419 | |
3156 | void |
3420 | void |
3157 | _register (char *target, char *proto, SV *req) |
3421 | _register (char *target, char *proto, SV *req) |
3158 | CODE: |
3422 | CODE: |
3159 | { |
3423 | { |
3160 | HV *st; |
3424 | CV *req_cv = coro_sv_2cv (aTHX_ req); |
3161 | GV *gvp; |
|
|
3162 | CV *req_cv = sv_2cv (req, &st, &gvp, 0); |
|
|
3163 | /* newXSproto doesn't return the CV on 5.8 */ |
3425 | /* newXSproto doesn't return the CV on 5.8 */ |
3164 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
3426 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
3165 | sv_setpv ((SV *)slf_cv, proto); |
3427 | sv_setpv ((SV *)slf_cv, proto); |
3166 | 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); |
3167 | } |
3429 | } |