… | |
… | |
101 | # define CvISXSUB_on(cv) (void)cv |
101 | # define CvISXSUB_on(cv) (void)cv |
102 | #endif |
102 | #endif |
103 | #ifndef CvISXSUB |
103 | #ifndef CvISXSUB |
104 | # define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE) |
104 | # define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE) |
105 | #endif |
105 | #endif |
|
|
106 | #ifndef Newx |
|
|
107 | # define Newx(ptr,nitems,type) New (0,ptr,nitems,type) |
|
|
108 | #endif |
106 | |
109 | |
107 | /* 5.8.7 */ |
110 | /* 5.8.7 */ |
108 | #ifndef SvRV_set |
111 | #ifndef SvRV_set |
109 | # define SvRV_set(s,v) SvRV(s) = (v) |
112 | # define SvRV_set(s,v) SvRV(s) = (v) |
110 | #endif |
113 | #endif |
… | |
… | |
130 | #else |
133 | #else |
131 | # define dSTACKLEVEL volatile void *stacklevel |
134 | # define dSTACKLEVEL volatile void *stacklevel |
132 | # define STACKLEVEL ((void *)&stacklevel) |
135 | # define STACKLEVEL ((void *)&stacklevel) |
133 | #endif |
136 | #endif |
134 | |
137 | |
135 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
138 | #define IN_DESTRUCT PL_dirty |
136 | |
139 | |
137 | #if __GNUC__ >= 3 |
140 | #if __GNUC__ >= 3 |
138 | # define attribute(x) __attribute__(x) |
141 | # define attribute(x) __attribute__(x) |
139 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
142 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
140 | # define INLINE static inline |
143 | # define INLINE static inline |
… | |
… | |
170 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
173 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
171 | static JMPENV *main_top_env; |
174 | static JMPENV *main_top_env; |
172 | static HV *coro_state_stash, *coro_stash; |
175 | static HV *coro_state_stash, *coro_stash; |
173 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
176 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
174 | |
177 | |
|
|
178 | static AV *av_destroy; /* destruction queue */ |
|
|
179 | static SV *sv_manager; /* the manager coro */ |
|
|
180 | static SV *sv_idle; /* $Coro::idle */ |
|
|
181 | |
175 | static GV *irsgv; /* $/ */ |
182 | static GV *irsgv; /* $/ */ |
176 | static GV *stdoutgv; /* *STDOUT */ |
183 | static GV *stdoutgv; /* *STDOUT */ |
177 | static SV *rv_diehook; |
184 | static SV *rv_diehook; |
178 | static SV *rv_warnhook; |
185 | static SV *rv_warnhook; |
179 | static HV *hv_sig; /* %SIG */ |
186 | static HV *hv_sig; /* %SIG */ |
180 | |
187 | |
181 | /* async_pool helper stuff */ |
188 | /* async_pool helper stuff */ |
182 | static SV *sv_pool_rss; |
189 | static SV *sv_pool_rss; |
183 | static SV *sv_pool_size; |
190 | static SV *sv_pool_size; |
184 | static SV *sv_async_pool_idle; |
191 | static SV *sv_async_pool_idle; /* description string */ |
185 | static AV *av_async_pool; |
192 | static AV *av_async_pool; /* idle pool */ |
186 | static SV *sv_Coro; |
193 | static SV *sv_Coro; /* class string */ |
187 | static CV *cv_pool_handler; |
194 | static CV *cv_pool_handler; |
188 | static CV *cv_coro_new; |
195 | static CV *cv_coro_state_new; |
189 | |
196 | |
190 | /* Coro::AnyEvent */ |
197 | /* Coro::AnyEvent */ |
191 | static SV *sv_activity; |
198 | static SV *sv_activity; |
192 | |
199 | |
193 | static struct coro_cctx *cctx_first; |
200 | static struct coro_cctx *cctx_first; |
… | |
… | |
222 | int valgrind_id; |
229 | int valgrind_id; |
223 | #endif |
230 | #endif |
224 | unsigned char flags; |
231 | unsigned char flags; |
225 | } coro_cctx; |
232 | } coro_cctx; |
226 | |
233 | |
|
|
234 | coro_cctx *cctx_current; /* the currently running cctx */ |
|
|
235 | |
|
|
236 | /*****************************************************************************/ |
|
|
237 | |
227 | enum { |
238 | enum { |
228 | CF_RUNNING = 0x0001, /* coroutine is running */ |
239 | CF_RUNNING = 0x0001, /* coroutine is running */ |
229 | CF_READY = 0x0002, /* coroutine is ready */ |
240 | CF_READY = 0x0002, /* coroutine is ready */ |
230 | CF_NEW = 0x0004, /* has never been switched to */ |
241 | CF_NEW = 0x0004, /* has never been switched to */ |
231 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
242 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
… | |
… | |
236 | { |
247 | { |
237 | SV *defsv; |
248 | SV *defsv; |
238 | AV *defav; |
249 | AV *defav; |
239 | SV *errsv; |
250 | SV *errsv; |
240 | SV *irsgv; |
251 | SV *irsgv; |
|
|
252 | HV *hinthv; |
241 | #define VAR(name,type) type name; |
253 | #define VAR(name,type) type name; |
242 | # include "state.h" |
254 | # include "state.h" |
243 | #undef VAR |
255 | #undef VAR |
244 | } perl_slots; |
256 | } perl_slots; |
245 | |
257 | |
… | |
… | |
344 | GV *gvp; |
356 | GV *gvp; |
345 | return sv_2cv (sv, &st, &gvp, 0); |
357 | return sv_2cv (sv, &st, &gvp, 0); |
346 | } |
358 | } |
347 | |
359 | |
348 | static AV * |
360 | static AV * |
349 | coro_clone_padlist (pTHX_ CV *cv) |
361 | coro_derive_padlist (pTHX_ CV *cv) |
350 | { |
362 | { |
351 | AV *padlist = CvPADLIST (cv); |
363 | AV *padlist = CvPADLIST (cv); |
352 | AV *newpadlist, *newpad; |
364 | AV *newpadlist, *newpad; |
353 | |
365 | |
354 | newpadlist = newAV (); |
366 | newpadlist = newAV (); |
… | |
… | |
369 | |
381 | |
370 | static void |
382 | static void |
371 | free_padlist (pTHX_ AV *padlist) |
383 | free_padlist (pTHX_ AV *padlist) |
372 | { |
384 | { |
373 | /* may be during global destruction */ |
385 | /* may be during global destruction */ |
374 | if (SvREFCNT (padlist)) |
386 | if (!IN_DESTRUCT) |
375 | { |
387 | { |
376 | I32 i = AvFILLp (padlist); |
388 | I32 i = AvFILLp (padlist); |
|
|
389 | |
377 | while (i >= 0) |
390 | while (i >= 0) |
378 | { |
391 | { |
379 | SV **svp = av_fetch (padlist, i--, FALSE); |
392 | /* we try to be extra-careful here */ |
380 | if (svp) |
393 | AV *av = (AV *)AvARRAY (padlist)[i--]; |
381 | { |
394 | |
382 | SV *sv; |
395 | I32 i = AvFILLp (av); |
383 | while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) |
396 | |
|
|
397 | while (i >= 0) |
|
|
398 | SvREFCNT_dec (AvARRAY (av)[i--]); |
|
|
399 | |
|
|
400 | AvFILLp (av) = -1; |
384 | SvREFCNT_dec (sv); |
401 | SvREFCNT_dec (av); |
385 | |
|
|
386 | SvREFCNT_dec (*svp); |
|
|
387 | } |
|
|
388 | } |
402 | } |
389 | |
403 | |
|
|
404 | AvFILLp (padlist) = -1; |
390 | SvREFCNT_dec ((SV*)padlist); |
405 | SvREFCNT_dec ((SV*)padlist); |
391 | } |
406 | } |
392 | } |
407 | } |
393 | |
408 | |
394 | static int |
409 | static int |
… | |
… | |
474 | CV *cp = Perl_cv_clone (aTHX_ cv); |
489 | CV *cp = Perl_cv_clone (aTHX_ cv); |
475 | CvPADLIST (cv) = CvPADLIST (cp); |
490 | CvPADLIST (cv) = CvPADLIST (cp); |
476 | CvPADLIST (cp) = 0; |
491 | CvPADLIST (cp) = 0; |
477 | SvREFCNT_dec (cp); |
492 | SvREFCNT_dec (cp); |
478 | #else |
493 | #else |
479 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
494 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
480 | #endif |
495 | #endif |
481 | } |
496 | } |
482 | } |
497 | } |
483 | |
498 | |
484 | static void |
499 | static void |
… | |
… | |
491 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
506 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
492 | |
507 | |
493 | av = (AV *)mg->mg_obj; |
508 | av = (AV *)mg->mg_obj; |
494 | |
509 | |
495 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
510 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
496 | av_extend (av, AvMAX (av) + 1); |
511 | av_extend (av, AvFILLp (av) + 1); |
497 | |
512 | |
498 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
513 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
499 | } |
514 | } |
500 | |
515 | |
501 | /** load & save, init *******************************************************/ |
516 | /** load & save, init *******************************************************/ |
… | |
… | |
506 | perl_slots *slot = c->slot; |
521 | perl_slots *slot = c->slot; |
507 | c->slot = 0; |
522 | c->slot = 0; |
508 | |
523 | |
509 | PL_mainstack = c->mainstack; |
524 | PL_mainstack = c->mainstack; |
510 | |
525 | |
511 | GvSV (PL_defgv) = slot->defsv; |
526 | GvSV (PL_defgv) = slot->defsv; |
512 | GvAV (PL_defgv) = slot->defav; |
527 | GvAV (PL_defgv) = slot->defav; |
513 | GvSV (PL_errgv) = slot->errsv; |
528 | GvSV (PL_errgv) = slot->errsv; |
514 | GvSV (irsgv) = slot->irsgv; |
529 | GvSV (irsgv) = slot->irsgv; |
|
|
530 | GvHV (PL_hintgv) = slot->hinthv; |
515 | |
531 | |
516 | #define VAR(name,type) PL_ ## name = slot->name; |
532 | #define VAR(name,type) PL_ ## name = slot->name; |
517 | # include "state.h" |
533 | # include "state.h" |
518 | #undef VAR |
534 | #undef VAR |
519 | |
535 | |
… | |
… | |
605 | c->mainstack = PL_mainstack; |
621 | c->mainstack = PL_mainstack; |
606 | |
622 | |
607 | { |
623 | { |
608 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
624 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
609 | |
625 | |
610 | slot->defav = GvAV (PL_defgv); |
626 | slot->defav = GvAV (PL_defgv); |
611 | slot->defsv = DEFSV; |
627 | slot->defsv = DEFSV; |
612 | slot->errsv = ERRSV; |
628 | slot->errsv = ERRSV; |
613 | slot->irsgv = GvSV (irsgv); |
629 | slot->irsgv = GvSV (irsgv); |
|
|
630 | slot->hinthv = GvHV (PL_hintgv); |
614 | |
631 | |
615 | #define VAR(name,type) slot->name = PL_ ## name; |
632 | #define VAR(name,type) slot->name = PL_ ## name; |
616 | # include "state.h" |
633 | # include "state.h" |
617 | #undef VAR |
634 | #undef VAR |
618 | } |
635 | } |
… | |
… | |
834 | slf_check_nop (pTHX_ struct CoroSLF *frame) |
851 | slf_check_nop (pTHX_ struct CoroSLF *frame) |
835 | { |
852 | { |
836 | return 0; |
853 | return 0; |
837 | } |
854 | } |
838 | |
855 | |
|
|
856 | static int |
|
|
857 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
|
|
858 | { |
|
|
859 | return 1; |
|
|
860 | } |
|
|
861 | |
839 | static UNOP coro_setup_op; |
862 | static UNOP coro_setup_op; |
840 | |
863 | |
841 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
864 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
842 | coro_setup (pTHX_ struct coro *coro) |
865 | coro_setup (pTHX_ struct coro *coro) |
843 | { |
866 | { |
… | |
… | |
848 | |
871 | |
849 | PL_runops = RUNOPS_DEFAULT; |
872 | PL_runops = RUNOPS_DEFAULT; |
850 | PL_curcop = &PL_compiling; |
873 | PL_curcop = &PL_compiling; |
851 | PL_in_eval = EVAL_NULL; |
874 | PL_in_eval = EVAL_NULL; |
852 | PL_comppad = 0; |
875 | PL_comppad = 0; |
|
|
876 | PL_comppad_name = 0; |
|
|
877 | PL_comppad_name_fill = 0; |
|
|
878 | PL_comppad_name_floor = 0; |
853 | PL_curpm = 0; |
879 | PL_curpm = 0; |
854 | PL_curpad = 0; |
880 | PL_curpad = 0; |
855 | PL_localizing = 0; |
881 | PL_localizing = 0; |
856 | PL_dirty = 0; |
882 | PL_dirty = 0; |
857 | PL_restartop = 0; |
883 | PL_restartop = 0; |
858 | #if PERL_VERSION_ATLEAST (5,10,0) |
884 | #if PERL_VERSION_ATLEAST (5,10,0) |
859 | PL_parser = 0; |
885 | PL_parser = 0; |
860 | #endif |
886 | #endif |
|
|
887 | PL_hints = 0; |
861 | |
888 | |
862 | /* recreate the die/warn hooks */ |
889 | /* recreate the die/warn hooks */ |
863 | PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); |
890 | PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); |
864 | PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); |
891 | PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); |
865 | |
892 | |
866 | GvSV (PL_defgv) = newSV (0); |
893 | GvSV (PL_defgv) = newSV (0); |
867 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
894 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
868 | GvSV (PL_errgv) = newSV (0); |
895 | GvSV (PL_errgv) = newSV (0); |
869 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
896 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
|
|
897 | GvHV (PL_hintgv) = 0; |
870 | PL_rs = newSVsv (GvSV (irsgv)); |
898 | PL_rs = newSVsv (GvSV (irsgv)); |
871 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
899 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
872 | |
900 | |
873 | { |
901 | { |
874 | dSP; |
902 | dSP; |
… | |
… | |
892 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
920 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
893 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
921 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
894 | |
922 | |
895 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
923 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
896 | coro_setup_op.op_next = PL_op; |
924 | coro_setup_op.op_next = PL_op; |
897 | coro_setup_op.op_type = OP_CUSTOM; |
925 | coro_setup_op.op_type = OP_ENTERSUB; |
898 | coro_setup_op.op_ppaddr = pp_slf; |
926 | coro_setup_op.op_ppaddr = pp_slf; |
899 | /* no flags etc. required, as an init function won't be called */ |
927 | /* no flags etc. required, as an init function won't be called */ |
900 | |
928 | |
901 | PL_op = (OP *)&coro_setup_op; |
929 | PL_op = (OP *)&coro_setup_op; |
902 | |
930 | |
… | |
… | |
928 | SvREFCNT_dec (GvAV (PL_defgv)); |
956 | SvREFCNT_dec (GvAV (PL_defgv)); |
929 | SvREFCNT_dec (GvSV (PL_errgv)); |
957 | SvREFCNT_dec (GvSV (PL_errgv)); |
930 | SvREFCNT_dec (PL_defoutgv); |
958 | SvREFCNT_dec (PL_defoutgv); |
931 | SvREFCNT_dec (PL_rs); |
959 | SvREFCNT_dec (PL_rs); |
932 | SvREFCNT_dec (GvSV (irsgv)); |
960 | SvREFCNT_dec (GvSV (irsgv)); |
|
|
961 | SvREFCNT_dec (GvHV (PL_hintgv)); |
933 | |
962 | |
934 | SvREFCNT_dec (PL_diehook); |
963 | SvREFCNT_dec (PL_diehook); |
935 | SvREFCNT_dec (PL_warnhook); |
964 | SvREFCNT_dec (PL_warnhook); |
936 | |
965 | |
937 | SvREFCNT_dec (coro->saved_deffh); |
966 | SvREFCNT_dec (coro->saved_deffh); |
… | |
… | |
955 | static int |
984 | static int |
956 | runops_trace (pTHX) |
985 | runops_trace (pTHX) |
957 | { |
986 | { |
958 | COP *oldcop = 0; |
987 | COP *oldcop = 0; |
959 | int oldcxix = -2; |
988 | int oldcxix = -2; |
960 | struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */ |
|
|
961 | coro_cctx *cctx = coro->cctx; |
|
|
962 | |
989 | |
963 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
990 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
964 | { |
991 | { |
965 | PERL_ASYNC_CHECK (); |
992 | PERL_ASYNC_CHECK (); |
966 | |
993 | |
967 | if (cctx->flags & CC_TRACE_ALL) |
994 | if (cctx_current->flags & CC_TRACE_ALL) |
968 | { |
995 | { |
969 | if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) |
996 | if (PL_op->op_type == OP_LEAVESUB && cctx_current->flags & CC_TRACE_SUB) |
970 | { |
997 | { |
971 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
998 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
972 | SV **bot, **top; |
999 | SV **bot, **top; |
973 | AV *av = newAV (); /* return values */ |
1000 | AV *av = newAV (); /* return values */ |
974 | SV **cb; |
1001 | SV **cb; |
… | |
… | |
1011 | |
1038 | |
1012 | if (PL_curcop != &PL_compiling) |
1039 | if (PL_curcop != &PL_compiling) |
1013 | { |
1040 | { |
1014 | SV **cb; |
1041 | SV **cb; |
1015 | |
1042 | |
1016 | if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) |
1043 | if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB) |
1017 | { |
1044 | { |
1018 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1045 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1019 | |
1046 | |
1020 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1047 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1021 | { |
1048 | { |
1022 | runops_proc_t old_runops = PL_runops; |
|
|
1023 | dSP; |
1049 | dSP; |
1024 | GV *gv = CvGV (cx->blk_sub.cv); |
1050 | GV *gv = CvGV (cx->blk_sub.cv); |
1025 | SV *fullname = sv_2mortal (newSV (0)); |
1051 | SV *fullname = sv_2mortal (newSV (0)); |
1026 | |
1052 | |
1027 | if (isGV (gv)) |
1053 | if (isGV (gv)) |
… | |
… | |
1045 | } |
1071 | } |
1046 | |
1072 | |
1047 | oldcxix = cxstack_ix; |
1073 | oldcxix = cxstack_ix; |
1048 | } |
1074 | } |
1049 | |
1075 | |
1050 | if (cctx->flags & CC_TRACE_LINE) |
1076 | if (cctx_current->flags & CC_TRACE_LINE) |
1051 | { |
1077 | { |
1052 | dSP; |
1078 | dSP; |
1053 | |
1079 | |
1054 | PL_runops = RUNOPS_DEFAULT; |
1080 | PL_runops = RUNOPS_DEFAULT; |
1055 | ENTER; |
1081 | ENTER; |
… | |
… | |
1074 | |
1100 | |
1075 | TAINT_NOT; |
1101 | TAINT_NOT; |
1076 | return 0; |
1102 | return 0; |
1077 | } |
1103 | } |
1078 | |
1104 | |
1079 | static struct coro_cctx *cctx_ssl_cctx; |
|
|
1080 | static struct CoroSLF cctx_ssl_frame; |
1105 | static struct CoroSLF cctx_ssl_frame; |
1081 | |
1106 | |
1082 | static void |
1107 | static void |
1083 | slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) |
1108 | slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) |
1084 | { |
1109 | { |
1085 | ta->prev = (struct coro *)cctx_ssl_cctx; |
|
|
1086 | ta->next = 0; |
1110 | ta->prev = 0; |
1087 | } |
1111 | } |
1088 | |
1112 | |
1089 | static int |
1113 | static int |
1090 | slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) |
1114 | slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) |
1091 | { |
1115 | { |
… | |
… | |
1094 | return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ |
1118 | return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ |
1095 | } |
1119 | } |
1096 | |
1120 | |
1097 | /* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ |
1121 | /* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ |
1098 | static void NOINLINE |
1122 | static void NOINLINE |
1099 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1123 | cctx_prepare (pTHX) |
1100 | { |
1124 | { |
1101 | PL_top_env = &PL_start_env; |
1125 | PL_top_env = &PL_start_env; |
1102 | |
1126 | |
1103 | if (cctx->flags & CC_TRACE) |
1127 | if (cctx_current->flags & CC_TRACE) |
1104 | PL_runops = runops_trace; |
1128 | PL_runops = runops_trace; |
1105 | |
1129 | |
1106 | /* we already must be executing an SLF op, there is no other valid way |
1130 | /* we already must be executing an SLF op, there is no other valid way |
1107 | * that can lead to creation of a new cctx */ |
1131 | * that can lead to creation of a new cctx */ |
1108 | assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", |
1132 | assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", |
1109 | slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); |
1133 | slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); |
1110 | |
1134 | |
1111 | /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ |
1135 | /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ |
1112 | cctx_ssl_cctx = cctx; |
|
|
1113 | cctx_ssl_frame = slf_frame; |
1136 | cctx_ssl_frame = slf_frame; |
1114 | |
1137 | |
1115 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1138 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1116 | slf_frame.check = slf_check_set_stacklevel; |
1139 | slf_frame.check = slf_check_set_stacklevel; |
1117 | } |
1140 | } |
… | |
… | |
1140 | /* normally we would need to skip the entersub here */ |
1163 | /* normally we would need to skip the entersub here */ |
1141 | /* not doing so will re-execute it, which is exactly what we want */ |
1164 | /* not doing so will re-execute it, which is exactly what we want */ |
1142 | /* PL_nop = PL_nop->op_next */ |
1165 | /* PL_nop = PL_nop->op_next */ |
1143 | |
1166 | |
1144 | /* inject a fake subroutine call to cctx_init */ |
1167 | /* inject a fake subroutine call to cctx_init */ |
1145 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1168 | cctx_prepare (aTHX); |
1146 | |
1169 | |
1147 | /* cctx_run is the alternative tail of transfer() */ |
1170 | /* cctx_run is the alternative tail of transfer() */ |
1148 | transfer_tail (aTHX); |
1171 | transfer_tail (aTHX); |
1149 | |
1172 | |
1150 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1173 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
… | |
… | |
1158 | /* |
1181 | /* |
1159 | * If perl-run returns we assume exit() was being called or the coro |
1182 | * If perl-run returns we assume exit() was being called or the coro |
1160 | * fell off the end, which seems to be the only valid (non-bug) |
1183 | * fell off the end, which seems to be the only valid (non-bug) |
1161 | * reason for perl_run to return. We try to exit by jumping to the |
1184 | * reason for perl_run to return. We try to exit by jumping to the |
1162 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1185 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1163 | * coroutine as Coro has no such concept |
1186 | * coroutine as Coro has no such concept. |
|
|
1187 | * This actually isn't valid with the pthread backend, but OSes requiring |
|
|
1188 | * that backend are too broken to do it in a standards-compliant way. |
1164 | */ |
1189 | */ |
1165 | PL_top_env = main_top_env; |
1190 | PL_top_env = main_top_env; |
1166 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1191 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1167 | } |
1192 | } |
1168 | } |
1193 | } |
… | |
… | |
1245 | cctx_destroy (coro_cctx *cctx) |
1270 | cctx_destroy (coro_cctx *cctx) |
1246 | { |
1271 | { |
1247 | if (!cctx) |
1272 | if (!cctx) |
1248 | return; |
1273 | return; |
1249 | |
1274 | |
|
|
1275 | assert (cctx != cctx_current);//D temporary |
|
|
1276 | |
1250 | --cctx_count; |
1277 | --cctx_count; |
1251 | coro_destroy (&cctx->cctx); |
1278 | coro_destroy (&cctx->cctx); |
1252 | |
1279 | |
1253 | /* coro_transfer creates new, empty cctx's */ |
1280 | /* coro_transfer creates new, empty cctx's */ |
1254 | if (cctx->sptr) |
1281 | if (cctx->sptr) |
… | |
… | |
1317 | /* TODO: throwing up here is considered harmful */ |
1344 | /* TODO: throwing up here is considered harmful */ |
1318 | |
1345 | |
1319 | if (expect_true (prev != next)) |
1346 | if (expect_true (prev != next)) |
1320 | { |
1347 | { |
1321 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1348 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1322 | croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,"); |
1349 | croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,"); |
1323 | |
1350 | |
1324 | if (expect_false (next->flags & CF_RUNNING)) |
1351 | if (expect_false (next->flags & CF_RUNNING)) |
1325 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); |
1352 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); |
1326 | |
1353 | |
1327 | if (expect_false (next->flags & CF_DESTROYED)) |
1354 | if (expect_false (next->flags & CF_DESTROYED)) |
… | |
… | |
1339 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1366 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1340 | { |
1367 | { |
1341 | dSTACKLEVEL; |
1368 | dSTACKLEVEL; |
1342 | |
1369 | |
1343 | /* sometimes transfer is only called to set idle_sp */ |
1370 | /* sometimes transfer is only called to set idle_sp */ |
1344 | if (expect_false (!next)) |
1371 | if (expect_false (!prev)) |
1345 | { |
1372 | { |
1346 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1373 | cctx_current->idle_sp = STACKLEVEL; |
1347 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1374 | assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1348 | } |
1375 | } |
1349 | else if (expect_true (prev != next)) |
1376 | else if (expect_true (prev != next)) |
1350 | { |
1377 | { |
1351 | coro_cctx *prev__cctx; |
1378 | coro_cctx *cctx_prev; |
1352 | |
1379 | |
1353 | if (expect_false (prev->flags & CF_NEW)) |
1380 | if (expect_false (prev->flags & CF_NEW)) |
1354 | { |
1381 | { |
1355 | /* create a new empty/source context */ |
1382 | /* create a new empty/source context */ |
1356 | prev->cctx = cctx_new_empty (); |
|
|
1357 | prev->flags &= ~CF_NEW; |
1383 | prev->flags &= ~CF_NEW; |
1358 | prev->flags |= CF_RUNNING; |
1384 | prev->flags |= CF_RUNNING; |
1359 | } |
1385 | } |
1360 | |
1386 | |
1361 | prev->flags &= ~CF_RUNNING; |
1387 | prev->flags &= ~CF_RUNNING; |
… | |
… | |
1372 | coro_setup (aTHX_ next); |
1398 | coro_setup (aTHX_ next); |
1373 | } |
1399 | } |
1374 | else |
1400 | else |
1375 | load_perl (aTHX_ next); |
1401 | load_perl (aTHX_ next); |
1376 | |
1402 | |
1377 | prev__cctx = prev->cctx; |
1403 | assert (!prev->cctx);//D temporary |
1378 | |
1404 | |
1379 | /* possibly untie and reuse the cctx */ |
1405 | /* possibly untie and reuse the cctx */ |
1380 | if (expect_true ( |
1406 | if (expect_true ( |
1381 | prev__cctx->idle_sp == STACKLEVEL |
1407 | cctx_current->idle_sp == STACKLEVEL |
1382 | && !(prev__cctx->flags & CC_TRACE) |
1408 | && !(cctx_current->flags & CC_TRACE) |
1383 | && !force_cctx |
1409 | && !force_cctx |
1384 | )) |
1410 | )) |
1385 | { |
1411 | { |
1386 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1412 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1387 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); |
1413 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te)); |
1388 | |
1414 | |
1389 | prev->cctx = 0; |
|
|
1390 | |
|
|
1391 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1415 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */ |
1392 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
1416 | /* without this the next cctx_get might destroy the running cctx while still in use */ |
1393 | if (expect_false (CCTX_EXPIRED (prev__cctx))) |
1417 | if (expect_false (CCTX_EXPIRED (cctx_current))) |
1394 | if (!next->cctx) |
1418 | if (expect_true (!next->cctx)) |
1395 | next->cctx = cctx_get (aTHX); |
1419 | next->cctx = cctx_get (aTHX); |
1396 | |
1420 | |
1397 | cctx_put (prev__cctx); |
1421 | cctx_put (cctx_current); |
1398 | } |
1422 | } |
|
|
1423 | else |
|
|
1424 | prev->cctx = cctx_current; |
1399 | |
1425 | |
1400 | ++next->usecount; |
1426 | ++next->usecount; |
1401 | |
1427 | |
1402 | if (expect_true (!next->cctx)) |
1428 | cctx_prev = cctx_current; |
1403 | next->cctx = cctx_get (aTHX); |
1429 | cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX); |
1404 | |
1430 | |
1405 | if (expect_false (prev__cctx != next->cctx)) |
1431 | next->cctx = 0; |
|
|
1432 | |
|
|
1433 | if (expect_false (cctx_prev != cctx_current)) |
1406 | { |
1434 | { |
1407 | prev__cctx->top_env = PL_top_env; |
1435 | cctx_prev->top_env = PL_top_env; |
1408 | PL_top_env = next->cctx->top_env; |
1436 | PL_top_env = cctx_current->top_env; |
1409 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1437 | coro_transfer (&cctx_prev->cctx, &cctx_current->cctx); |
1410 | } |
1438 | } |
1411 | |
1439 | |
1412 | transfer_tail (aTHX); |
1440 | transfer_tail (aTHX); |
1413 | } |
1441 | } |
1414 | } |
1442 | } |
… | |
… | |
1569 | { |
1597 | { |
1570 | struct coro *coro; |
1598 | struct coro *coro; |
1571 | SV *sv_hook; |
1599 | SV *sv_hook; |
1572 | void (*xs_hook)(void); |
1600 | void (*xs_hook)(void); |
1573 | |
1601 | |
1574 | if (SvROK (coro_sv)) |
|
|
1575 | coro_sv = SvRV (coro_sv); |
|
|
1576 | |
|
|
1577 | coro = SvSTATE (coro_sv); |
1602 | coro = SvSTATE (coro_sv); |
1578 | |
1603 | |
1579 | if (coro->flags & CF_READY) |
1604 | if (coro->flags & CF_READY) |
1580 | return 0; |
1605 | return 0; |
1581 | |
1606 | |
… | |
… | |
1612 | api_is_ready (pTHX_ SV *coro_sv) |
1637 | api_is_ready (pTHX_ SV *coro_sv) |
1613 | { |
1638 | { |
1614 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1639 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1615 | } |
1640 | } |
1616 | |
1641 | |
|
|
1642 | /* expects to own a reference to next->hv */ |
1617 | INLINE void |
1643 | INLINE void |
1618 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1644 | prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
1619 | { |
1645 | { |
1620 | SV *prev_sv, *next_sv; |
|
|
1621 | |
|
|
1622 | for (;;) |
|
|
1623 | { |
|
|
1624 | next_sv = coro_deq (aTHX); |
|
|
1625 | |
|
|
1626 | /* nothing to schedule: call the idle handler */ |
|
|
1627 | if (expect_false (!next_sv)) |
|
|
1628 | { |
|
|
1629 | dSP; |
|
|
1630 | |
|
|
1631 | ENTER; |
|
|
1632 | SAVETMPS; |
|
|
1633 | |
|
|
1634 | PUSHMARK (SP); |
|
|
1635 | PUTBACK; |
|
|
1636 | call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); |
|
|
1637 | |
|
|
1638 | FREETMPS; |
|
|
1639 | LEAVE; |
|
|
1640 | continue; |
|
|
1641 | } |
|
|
1642 | |
|
|
1643 | ta->next = SvSTATE_hv (next_sv); |
|
|
1644 | |
|
|
1645 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1646 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
|
|
1647 | { |
|
|
1648 | SvREFCNT_dec (next_sv); |
|
|
1649 | /* coro_nready has already been taken care of by destroy */ |
|
|
1650 | continue; |
|
|
1651 | } |
|
|
1652 | |
|
|
1653 | --coro_nready; |
|
|
1654 | break; |
|
|
1655 | } |
|
|
1656 | |
|
|
1657 | /* free this only after the transfer */ |
|
|
1658 | prev_sv = SvRV (coro_current); |
1646 | SV *prev_sv = SvRV (coro_current); |
|
|
1647 | |
1659 | ta->prev = SvSTATE_hv (prev_sv); |
1648 | ta->prev = SvSTATE_hv (prev_sv); |
|
|
1649 | ta->next = next; |
|
|
1650 | |
1660 | TRANSFER_CHECK (*ta); |
1651 | TRANSFER_CHECK (*ta); |
1661 | assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); |
1652 | |
1662 | ta->next->flags &= ~CF_READY; |
|
|
1663 | SvRV_set (coro_current, next_sv); |
1653 | SvRV_set (coro_current, (SV *)next->hv); |
1664 | |
1654 | |
1665 | free_coro_mortal (aTHX); |
1655 | free_coro_mortal (aTHX); |
1666 | coro_mortal = prev_sv; |
1656 | coro_mortal = prev_sv; |
1667 | } |
1657 | } |
1668 | |
1658 | |
|
|
1659 | static void |
|
|
1660 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
|
|
1661 | { |
|
|
1662 | for (;;) |
|
|
1663 | { |
|
|
1664 | SV *next_sv = coro_deq (aTHX); |
|
|
1665 | |
|
|
1666 | if (expect_true (next_sv)) |
|
|
1667 | { |
|
|
1668 | struct coro *next = SvSTATE_hv (next_sv); |
|
|
1669 | |
|
|
1670 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1671 | if (expect_false (next->flags & CF_DESTROYED)) |
|
|
1672 | SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ |
|
|
1673 | else |
|
|
1674 | { |
|
|
1675 | next->flags &= ~CF_READY; |
|
|
1676 | --coro_nready; |
|
|
1677 | |
|
|
1678 | prepare_schedule_to (aTHX_ ta, next); |
|
|
1679 | break; |
|
|
1680 | } |
|
|
1681 | } |
|
|
1682 | else |
|
|
1683 | { |
|
|
1684 | /* nothing to schedule: call the idle handler */ |
|
|
1685 | if (SvROK (sv_idle) |
|
|
1686 | && SvOBJECT (SvRV (sv_idle))) |
|
|
1687 | { |
|
|
1688 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
|
|
1689 | api_ready (aTHX_ SvRV (sv_idle)); |
|
|
1690 | --coro_nready; |
|
|
1691 | } |
|
|
1692 | else |
|
|
1693 | { |
|
|
1694 | dSP; |
|
|
1695 | |
|
|
1696 | ENTER; |
|
|
1697 | SAVETMPS; |
|
|
1698 | |
|
|
1699 | PUSHMARK (SP); |
|
|
1700 | PUTBACK; |
|
|
1701 | call_sv (sv_idle, G_VOID | G_DISCARD); |
|
|
1702 | |
|
|
1703 | FREETMPS; |
|
|
1704 | LEAVE; |
|
|
1705 | } |
|
|
1706 | } |
|
|
1707 | } |
|
|
1708 | } |
|
|
1709 | |
1669 | INLINE void |
1710 | INLINE void |
1670 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1711 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1671 | { |
1712 | { |
1672 | api_ready (aTHX_ coro_current); |
1713 | api_ready (aTHX_ coro_current); |
1673 | prepare_schedule (aTHX_ ta); |
1714 | prepare_schedule (aTHX_ ta); |
… | |
… | |
1692 | { |
1733 | { |
1693 | struct coro_transfer_args ta; |
1734 | struct coro_transfer_args ta; |
1694 | |
1735 | |
1695 | prepare_schedule (aTHX_ &ta); |
1736 | prepare_schedule (aTHX_ &ta); |
1696 | TRANSFER (ta, 1); |
1737 | TRANSFER (ta, 1); |
|
|
1738 | } |
|
|
1739 | |
|
|
1740 | static void |
|
|
1741 | api_schedule_to (pTHX_ SV *coro_sv) |
|
|
1742 | { |
|
|
1743 | struct coro_transfer_args ta; |
|
|
1744 | struct coro *next = SvSTATE (coro_sv); |
|
|
1745 | |
|
|
1746 | SvREFCNT_inc_NN (coro_sv); |
|
|
1747 | prepare_schedule_to (aTHX_ &ta, next); |
1697 | } |
1748 | } |
1698 | |
1749 | |
1699 | static int |
1750 | static int |
1700 | api_cede (pTHX) |
1751 | api_cede (pTHX) |
1701 | { |
1752 | { |
… | |
… | |
1730 | static void |
1781 | static void |
1731 | api_trace (pTHX_ SV *coro_sv, int flags) |
1782 | api_trace (pTHX_ SV *coro_sv, int flags) |
1732 | { |
1783 | { |
1733 | struct coro *coro = SvSTATE (coro_sv); |
1784 | struct coro *coro = SvSTATE (coro_sv); |
1734 | |
1785 | |
|
|
1786 | if (coro->flags & CF_RUNNING) |
|
|
1787 | croak ("cannot enable tracing on a running coroutine, caught"); |
|
|
1788 | |
1735 | if (flags & CC_TRACE) |
1789 | if (flags & CC_TRACE) |
1736 | { |
1790 | { |
1737 | if (!coro->cctx) |
1791 | if (!coro->cctx) |
1738 | coro->cctx = cctx_new_run (); |
1792 | coro->cctx = cctx_new_run (); |
1739 | else if (!(coro->cctx->flags & CC_TRACE)) |
1793 | else if (!(coro->cctx->flags & CC_TRACE)) |
1740 | croak ("cannot enable tracing on coroutine with custom stack,"); |
1794 | croak ("cannot enable tracing on coroutine with custom stack, caught"); |
1741 | |
1795 | |
1742 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1796 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1743 | } |
1797 | } |
1744 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
1798 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
1745 | { |
1799 | { |
… | |
… | |
1748 | if (coro->flags & CF_RUNNING) |
1802 | if (coro->flags & CF_RUNNING) |
1749 | PL_runops = RUNOPS_DEFAULT; |
1803 | PL_runops = RUNOPS_DEFAULT; |
1750 | else |
1804 | else |
1751 | coro->slot->runops = RUNOPS_DEFAULT; |
1805 | coro->slot->runops = RUNOPS_DEFAULT; |
1752 | } |
1806 | } |
|
|
1807 | } |
|
|
1808 | |
|
|
1809 | static void |
|
|
1810 | coro_call_on_destroy (pTHX_ struct coro *coro) |
|
|
1811 | { |
|
|
1812 | SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0); |
|
|
1813 | SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0); |
|
|
1814 | |
|
|
1815 | if (on_destroyp) |
|
|
1816 | { |
|
|
1817 | AV *on_destroy = (AV *)SvRV (*on_destroyp); |
|
|
1818 | |
|
|
1819 | while (AvFILLp (on_destroy) >= 0) |
|
|
1820 | { |
|
|
1821 | dSP; /* don't disturb outer sp */ |
|
|
1822 | SV *cb = av_pop (on_destroy); |
|
|
1823 | |
|
|
1824 | PUSHMARK (SP); |
|
|
1825 | |
|
|
1826 | if (statusp) |
|
|
1827 | { |
|
|
1828 | int i; |
|
|
1829 | AV *status = (AV *)SvRV (*statusp); |
|
|
1830 | EXTEND (SP, AvFILLp (status) + 1); |
|
|
1831 | |
|
|
1832 | for (i = 0; i <= AvFILLp (status); ++i) |
|
|
1833 | PUSHs (AvARRAY (status)[i]); |
|
|
1834 | } |
|
|
1835 | |
|
|
1836 | PUTBACK; |
|
|
1837 | call_sv (sv_2mortal (cb), G_VOID | G_DISCARD); |
|
|
1838 | } |
|
|
1839 | } |
|
|
1840 | } |
|
|
1841 | |
|
|
1842 | static void |
|
|
1843 | slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1844 | { |
|
|
1845 | int i; |
|
|
1846 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1847 | AV *av = newAV (); |
|
|
1848 | |
|
|
1849 | av_extend (av, items - 1); |
|
|
1850 | for (i = 0; i < items; ++i) |
|
|
1851 | av_push (av, SvREFCNT_inc_NN (arg [i])); |
|
|
1852 | |
|
|
1853 | hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); |
|
|
1854 | |
|
|
1855 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
|
|
1856 | api_ready (aTHX_ sv_manager); |
|
|
1857 | |
|
|
1858 | frame->prepare = prepare_schedule; |
|
|
1859 | frame->check = slf_check_repeat; |
1753 | } |
1860 | } |
1754 | |
1861 | |
1755 | /*****************************************************************************/ |
1862 | /*****************************************************************************/ |
1756 | /* async pool handler */ |
1863 | /* async pool handler */ |
1757 | |
1864 | |
… | |
… | |
1990 | static void |
2097 | static void |
1991 | slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2098 | slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1992 | { |
2099 | { |
1993 | frame->prepare = prepare_schedule; |
2100 | frame->prepare = prepare_schedule; |
1994 | frame->check = slf_check_nop; |
2101 | frame->check = slf_check_nop; |
|
|
2102 | } |
|
|
2103 | |
|
|
2104 | static void |
|
|
2105 | slf_prepare_schedule_to (pTHX_ struct coro_transfer_args *ta) |
|
|
2106 | { |
|
|
2107 | struct coro *next = (struct coro *)slf_frame.data; |
|
|
2108 | |
|
|
2109 | SvREFCNT_inc_NN (next->hv); |
|
|
2110 | prepare_schedule_to (aTHX_ ta, next); |
|
|
2111 | } |
|
|
2112 | |
|
|
2113 | static void |
|
|
2114 | slf_init_schedule_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2115 | { |
|
|
2116 | if (!items) |
|
|
2117 | croak ("Coro::schedule_to expects a coroutine argument, caught"); |
|
|
2118 | |
|
|
2119 | frame->data = (void *)SvSTATE (arg [0]); |
|
|
2120 | frame->prepare = slf_prepare_schedule_to; |
|
|
2121 | frame->check = slf_check_nop; |
|
|
2122 | } |
|
|
2123 | |
|
|
2124 | static void |
|
|
2125 | slf_init_cede_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2126 | { |
|
|
2127 | api_ready (aTHX_ SvRV (coro_current)); |
|
|
2128 | |
|
|
2129 | slf_init_schedule_to (aTHX_ frame, cv, arg, items); |
1995 | } |
2130 | } |
1996 | |
2131 | |
1997 | static void |
2132 | static void |
1998 | slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2133 | slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1999 | { |
2134 | { |
… | |
… | |
2145 | } |
2280 | } |
2146 | else |
2281 | else |
2147 | slf_argc = 0; |
2282 | slf_argc = 0; |
2148 | |
2283 | |
2149 | PL_op->op_ppaddr = pp_slf; |
2284 | PL_op->op_ppaddr = pp_slf; |
2150 | PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ |
2285 | /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ |
2151 | |
2286 | |
2152 | PL_op = (OP *)&slf_restore; |
2287 | PL_op = (OP *)&slf_restore; |
2153 | } |
2288 | } |
2154 | |
2289 | |
2155 | /*****************************************************************************/ |
2290 | /*****************************************************************************/ |
… | |
… | |
2238 | SV **ary; |
2373 | SV **ary; |
2239 | |
2374 | |
2240 | /* unfortunately, building manually saves memory */ |
2375 | /* unfortunately, building manually saves memory */ |
2241 | Newx (ary, 2, SV *); |
2376 | Newx (ary, 2, SV *); |
2242 | AvALLOC (av) = ary; |
2377 | AvALLOC (av) = ary; |
|
|
2378 | #if PERL_VERSION_ATLEAST (5,10,0) |
2243 | /*AvARRAY (av) = ary;*/ |
2379 | AvARRAY (av) = ary; |
|
|
2380 | #else |
2244 | SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ |
2381 | /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */ |
|
|
2382 | /* -DDEBUGGING flags this as a bug, despite it perfectly working */ |
|
|
2383 | SvPVX ((SV *)av) = (char *)ary; |
|
|
2384 | #endif |
2245 | AvMAX (av) = 1; |
2385 | AvMAX (av) = 1; |
2246 | AvFILLp (av) = 0; |
2386 | AvFILLp (av) = 0; |
2247 | ary [0] = newSViv (count); |
2387 | ary [0] = newSViv (count); |
2248 | |
2388 | |
2249 | return newRV_noinc ((SV *)av); |
2389 | return newRV_noinc ((SV *)av); |
… | |
… | |
2619 | XSRETURN_EMPTY; |
2759 | XSRETURN_EMPTY; |
2620 | } |
2760 | } |
2621 | |
2761 | |
2622 | /*****************************************************************************/ |
2762 | /*****************************************************************************/ |
2623 | |
2763 | |
|
|
2764 | #if CORO_CLONE |
|
|
2765 | # include "clone.c" |
|
|
2766 | #endif |
|
|
2767 | |
2624 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2768 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2625 | |
2769 | |
2626 | PROTOTYPES: DISABLE |
2770 | PROTOTYPES: DISABLE |
2627 | |
2771 | |
2628 | BOOT: |
2772 | BOOT: |
… | |
… | |
2632 | coro_thx = PERL_GET_CONTEXT; |
2776 | coro_thx = PERL_GET_CONTEXT; |
2633 | # endif |
2777 | # endif |
2634 | #endif |
2778 | #endif |
2635 | BOOT_PAGESIZE; |
2779 | BOOT_PAGESIZE; |
2636 | |
2780 | |
|
|
2781 | cctx_current = cctx_new_empty (); |
|
|
2782 | |
2637 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
2783 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
2638 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
2784 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
2639 | |
2785 | |
2640 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
2786 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
2641 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
2787 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
… | |
… | |
2660 | |
2806 | |
2661 | { |
2807 | { |
2662 | SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); |
2808 | SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); |
2663 | |
2809 | |
2664 | if (!PL_custom_op_names) PL_custom_op_names = newHV (); |
2810 | if (!PL_custom_op_names) PL_custom_op_names = newHV (); |
2665 | hv_store_ent (PL_custom_op_names, slf, |
2811 | hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0); |
2666 | newSVpv ("coro_slf", 0), 0); |
|
|
2667 | |
2812 | |
2668 | if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); |
2813 | if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); |
2669 | hv_store_ent (PL_custom_op_descs, slf, |
2814 | hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0); |
2670 | newSVpv ("coro schedule like function", 0), 0); |
|
|
2671 | } |
2815 | } |
2672 | |
2816 | |
2673 | coroapi.ver = CORO_API_VERSION; |
2817 | coroapi.ver = CORO_API_VERSION; |
2674 | coroapi.rev = CORO_API_REVISION; |
2818 | coroapi.rev = CORO_API_REVISION; |
2675 | |
2819 | |
… | |
… | |
2768 | void |
2912 | void |
2769 | _exit (int code) |
2913 | _exit (int code) |
2770 | PROTOTYPE: $ |
2914 | PROTOTYPE: $ |
2771 | CODE: |
2915 | CODE: |
2772 | _exit (code); |
2916 | _exit (code); |
|
|
2917 | |
|
|
2918 | SV * |
|
|
2919 | clone (Coro::State coro) |
|
|
2920 | CODE: |
|
|
2921 | { |
|
|
2922 | #if CORO_CLONE |
|
|
2923 | struct coro *ncoro = coro_clone (coro); |
|
|
2924 | MAGIC *mg; |
|
|
2925 | /* TODO: too much duplication */ |
|
|
2926 | ncoro->hv = newHV (); |
|
|
2927 | mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); |
|
|
2928 | mg->mg_flags |= MGf_DUP; |
|
|
2929 | RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv)); |
|
|
2930 | #else |
|
|
2931 | croak ("Coro::State->clone has not been configured into this installation of Coro, realised"); |
|
|
2932 | #endif |
|
|
2933 | } |
|
|
2934 | OUTPUT: |
|
|
2935 | RETVAL |
2773 | |
2936 | |
2774 | int |
2937 | int |
2775 | cctx_stacksize (int new_stacksize = 0) |
2938 | cctx_stacksize (int new_stacksize = 0) |
2776 | PROTOTYPE: ;$ |
2939 | PROTOTYPE: ;$ |
2777 | CODE: |
2940 | CODE: |
… | |
… | |
2898 | |
3061 | |
2899 | SV * |
3062 | SV * |
2900 | has_cctx (Coro::State coro) |
3063 | has_cctx (Coro::State coro) |
2901 | PROTOTYPE: $ |
3064 | PROTOTYPE: $ |
2902 | CODE: |
3065 | CODE: |
2903 | RETVAL = boolSV (!!coro->cctx); |
3066 | /* maybe manage the running flag differently */ |
|
|
3067 | RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING)); |
2904 | OUTPUT: |
3068 | OUTPUT: |
2905 | RETVAL |
3069 | RETVAL |
2906 | |
3070 | |
2907 | int |
3071 | int |
2908 | is_traced (Coro::State coro) |
3072 | is_traced (Coro::State coro) |
… | |
… | |
2928 | |
3092 | |
2929 | void |
3093 | void |
2930 | force_cctx () |
3094 | force_cctx () |
2931 | PROTOTYPE: |
3095 | PROTOTYPE: |
2932 | CODE: |
3096 | CODE: |
2933 | SvSTATE_current->cctx->idle_sp = 0; |
3097 | cctx_current->idle_sp = 0; |
2934 | |
3098 | |
2935 | void |
3099 | void |
2936 | swap_defsv (Coro::State self) |
3100 | swap_defsv (Coro::State self) |
2937 | PROTOTYPE: $ |
3101 | PROTOTYPE: $ |
2938 | ALIAS: |
3102 | ALIAS: |
… | |
… | |
2953 | |
3117 | |
2954 | BOOT: |
3118 | BOOT: |
2955 | { |
3119 | { |
2956 | int i; |
3120 | int i; |
2957 | |
3121 | |
2958 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
|
|
2959 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3122 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
2960 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3123 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
2961 | cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); |
3124 | cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); |
2962 | cv_coro_terminate = get_cv ( "Coro::terminate", GV_ADD); |
3125 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
2963 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); |
3126 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
2964 | SvREADONLY_on (coro_current); |
3127 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
|
|
3128 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
|
|
3129 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
|
|
3130 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
2965 | |
3131 | |
2966 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3132 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
2967 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3133 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
2968 | cv_pool_handler = get_cv ("Coro::_pool_handler", 0); SvREADONLY_on (cv_pool_handler); |
3134 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
2969 | cv_coro_new = get_cv ("Coro::new", 0); SvREADONLY_on (cv_coro_new); |
3135 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
2970 | |
3136 | |
2971 | coro_stash = gv_stashpv ("Coro", TRUE); |
3137 | coro_stash = gv_stashpv ("Coro", TRUE); |
2972 | |
3138 | |
2973 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
3139 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
2974 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
3140 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
… | |
… | |
2982 | |
3148 | |
2983 | { |
3149 | { |
2984 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
3150 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
2985 | |
3151 | |
2986 | coroapi.schedule = api_schedule; |
3152 | coroapi.schedule = api_schedule; |
|
|
3153 | coroapi.schedule_to = api_schedule_to; |
2987 | coroapi.cede = api_cede; |
3154 | coroapi.cede = api_cede; |
2988 | coroapi.cede_notself = api_cede_notself; |
3155 | coroapi.cede_notself = api_cede_notself; |
2989 | coroapi.ready = api_ready; |
3156 | coroapi.ready = api_ready; |
2990 | coroapi.is_ready = api_is_ready; |
3157 | coroapi.is_ready = api_is_ready; |
2991 | coroapi.nready = coro_nready; |
3158 | coroapi.nready = coro_nready; |
… | |
… | |
2996 | SvREADONLY_on (sv); |
3163 | SvREADONLY_on (sv); |
2997 | } |
3164 | } |
2998 | } |
3165 | } |
2999 | |
3166 | |
3000 | void |
3167 | void |
|
|
3168 | terminate (...) |
|
|
3169 | CODE: |
|
|
3170 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
|
|
3171 | |
|
|
3172 | void |
3001 | schedule (...) |
3173 | schedule (...) |
3002 | CODE: |
3174 | CODE: |
3003 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
3175 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
|
|
3176 | |
|
|
3177 | void |
|
|
3178 | schedule_to (...) |
|
|
3179 | CODE: |
|
|
3180 | CORO_EXECUTE_SLF_XS (slf_init_schedule_to); |
|
|
3181 | |
|
|
3182 | void |
|
|
3183 | cede_to (...) |
|
|
3184 | CODE: |
|
|
3185 | CORO_EXECUTE_SLF_XS (slf_init_cede_to); |
3004 | |
3186 | |
3005 | void |
3187 | void |
3006 | cede (...) |
3188 | cede (...) |
3007 | CODE: |
3189 | CODE: |
3008 | CORO_EXECUTE_SLF_XS (slf_init_cede); |
3190 | CORO_EXECUTE_SLF_XS (slf_init_cede); |
3009 | |
3191 | |
3010 | void |
3192 | void |
3011 | cede_notself (...) |
3193 | cede_notself (...) |
3012 | CODE: |
3194 | CODE: |
3013 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
3195 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
|
|
3196 | |
|
|
3197 | void |
|
|
3198 | _cancel (Coro::State self) |
|
|
3199 | CODE: |
|
|
3200 | coro_state_destroy (aTHX_ self); |
|
|
3201 | coro_call_on_destroy (aTHX_ self); |
3014 | |
3202 | |
3015 | void |
3203 | void |
3016 | _set_current (SV *current) |
3204 | _set_current (SV *current) |
3017 | PROTOTYPE: $ |
3205 | PROTOTYPE: $ |
3018 | CODE: |
3206 | CODE: |
… | |
… | |
3089 | PUSHMARK (SP); |
3277 | PUSHMARK (SP); |
3090 | EXTEND (SP, 2); |
3278 | EXTEND (SP, 2); |
3091 | PUSHs (sv_Coro); |
3279 | PUSHs (sv_Coro); |
3092 | PUSHs ((SV *)cv_pool_handler); |
3280 | PUSHs ((SV *)cv_pool_handler); |
3093 | PUTBACK; |
3281 | PUTBACK; |
3094 | call_sv ((SV *)cv_coro_new, G_SCALAR); |
3282 | call_sv ((SV *)cv_coro_state_new, G_SCALAR); |
3095 | SPAGAIN; |
3283 | SPAGAIN; |
3096 | |
3284 | |
3097 | hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); |
3285 | hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); |
3098 | } |
3286 | } |
3099 | |
3287 | |