… | |
… | |
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 |
|
|
106 | #ifndef Newx |
|
|
107 | # define Newx(ptr,nitems,type) New (0,ptr,nitems,type) |
|
|
108 | #endif |
103 | |
109 | |
104 | /* 5.8.7 */ |
110 | /* 5.8.7 */ |
105 | #ifndef SvRV_set |
111 | #ifndef SvRV_set |
106 | # define SvRV_set(s,v) SvRV(s) = (v) |
112 | # define SvRV_set(s,v) SvRV(s) = (v) |
107 | #endif |
113 | #endif |
… | |
… | |
167 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
173 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
168 | static JMPENV *main_top_env; |
174 | static JMPENV *main_top_env; |
169 | static HV *coro_state_stash, *coro_stash; |
175 | static HV *coro_state_stash, *coro_stash; |
170 | 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 */ |
171 | |
177 | |
|
|
178 | static AV *av_destroy; /* destruction queue */ |
|
|
179 | static SV *sv_manager; /* the manager coro */ |
|
|
180 | static SV *sv_idle; /* $Coro::idle */ |
|
|
181 | |
172 | static GV *irsgv; /* $/ */ |
182 | static GV *irsgv; /* $/ */ |
173 | static GV *stdoutgv; /* *STDOUT */ |
183 | static GV *stdoutgv; /* *STDOUT */ |
174 | static SV *rv_diehook; |
184 | static SV *rv_diehook; |
175 | static SV *rv_warnhook; |
185 | static SV *rv_warnhook; |
176 | static HV *hv_sig; /* %SIG */ |
186 | static HV *hv_sig; /* %SIG */ |
177 | |
187 | |
178 | /* async_pool helper stuff */ |
188 | /* async_pool helper stuff */ |
179 | static SV *sv_pool_rss; |
189 | static SV *sv_pool_rss; |
180 | static SV *sv_pool_size; |
190 | static SV *sv_pool_size; |
181 | static SV *sv_async_pool_idle; |
191 | static SV *sv_async_pool_idle; /* description string */ |
182 | static AV *av_async_pool; |
192 | static AV *av_async_pool; /* idle pool */ |
183 | static SV *sv_Coro; |
193 | static SV *sv_Coro; /* class string */ |
184 | static CV *cv_pool_handler; |
194 | static CV *cv_pool_handler; |
185 | static CV *cv_coro_new; |
195 | static CV *cv_coro_state_new; |
186 | |
196 | |
187 | /* Coro::AnyEvent */ |
197 | /* Coro::AnyEvent */ |
188 | static SV *sv_activity; |
198 | static SV *sv_activity; |
189 | |
199 | |
190 | static struct coro_cctx *cctx_first; |
200 | static struct coro_cctx *cctx_first; |
… | |
… | |
219 | int valgrind_id; |
229 | int valgrind_id; |
220 | #endif |
230 | #endif |
221 | unsigned char flags; |
231 | unsigned char flags; |
222 | } coro_cctx; |
232 | } coro_cctx; |
223 | |
233 | |
|
|
234 | coro_cctx *cctx_current; /* the currently running cctx */ |
|
|
235 | |
|
|
236 | /*****************************************************************************/ |
|
|
237 | |
224 | enum { |
238 | enum { |
225 | CF_RUNNING = 0x0001, /* coroutine is running */ |
239 | CF_RUNNING = 0x0001, /* coroutine is running */ |
226 | CF_READY = 0x0002, /* coroutine is ready */ |
240 | CF_READY = 0x0002, /* coroutine is ready */ |
227 | CF_NEW = 0x0004, /* has never been switched to */ |
241 | CF_NEW = 0x0004, /* has never been switched to */ |
228 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
242 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
… | |
… | |
233 | { |
247 | { |
234 | SV *defsv; |
248 | SV *defsv; |
235 | AV *defav; |
249 | AV *defav; |
236 | SV *errsv; |
250 | SV *errsv; |
237 | SV *irsgv; |
251 | SV *irsgv; |
|
|
252 | HV *hinthv; |
238 | #define VAR(name,type) type name; |
253 | #define VAR(name,type) type name; |
239 | # include "state.h" |
254 | # include "state.h" |
240 | #undef VAR |
255 | #undef VAR |
241 | } perl_slots; |
256 | } perl_slots; |
242 | |
257 | |
… | |
… | |
341 | GV *gvp; |
356 | GV *gvp; |
342 | return sv_2cv (sv, &st, &gvp, 0); |
357 | return sv_2cv (sv, &st, &gvp, 0); |
343 | } |
358 | } |
344 | |
359 | |
345 | static AV * |
360 | static AV * |
346 | coro_clone_padlist (pTHX_ CV *cv) |
361 | coro_derive_padlist (pTHX_ CV *cv) |
347 | { |
362 | { |
348 | AV *padlist = CvPADLIST (cv); |
363 | AV *padlist = CvPADLIST (cv); |
349 | AV *newpadlist, *newpad; |
364 | AV *newpadlist, *newpad; |
350 | |
365 | |
351 | newpadlist = newAV (); |
366 | newpadlist = newAV (); |
… | |
… | |
471 | CV *cp = Perl_cv_clone (aTHX_ cv); |
486 | CV *cp = Perl_cv_clone (aTHX_ cv); |
472 | CvPADLIST (cv) = CvPADLIST (cp); |
487 | CvPADLIST (cv) = CvPADLIST (cp); |
473 | CvPADLIST (cp) = 0; |
488 | CvPADLIST (cp) = 0; |
474 | SvREFCNT_dec (cp); |
489 | SvREFCNT_dec (cp); |
475 | #else |
490 | #else |
476 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
491 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
477 | #endif |
492 | #endif |
478 | } |
493 | } |
479 | } |
494 | } |
480 | |
495 | |
481 | static void |
496 | static void |
… | |
… | |
503 | perl_slots *slot = c->slot; |
518 | perl_slots *slot = c->slot; |
504 | c->slot = 0; |
519 | c->slot = 0; |
505 | |
520 | |
506 | PL_mainstack = c->mainstack; |
521 | PL_mainstack = c->mainstack; |
507 | |
522 | |
508 | GvSV (PL_defgv) = slot->defsv; |
523 | GvSV (PL_defgv) = slot->defsv; |
509 | GvAV (PL_defgv) = slot->defav; |
524 | GvAV (PL_defgv) = slot->defav; |
510 | GvSV (PL_errgv) = slot->errsv; |
525 | GvSV (PL_errgv) = slot->errsv; |
511 | GvSV (irsgv) = slot->irsgv; |
526 | GvSV (irsgv) = slot->irsgv; |
|
|
527 | GvHV (PL_hintgv) = slot->hinthv; |
512 | |
528 | |
513 | #define VAR(name,type) PL_ ## name = slot->name; |
529 | #define VAR(name,type) PL_ ## name = slot->name; |
514 | # include "state.h" |
530 | # include "state.h" |
515 | #undef VAR |
531 | #undef VAR |
516 | |
532 | |
… | |
… | |
602 | c->mainstack = PL_mainstack; |
618 | c->mainstack = PL_mainstack; |
603 | |
619 | |
604 | { |
620 | { |
605 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
621 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
606 | |
622 | |
607 | slot->defav = GvAV (PL_defgv); |
623 | slot->defav = GvAV (PL_defgv); |
608 | slot->defsv = DEFSV; |
624 | slot->defsv = DEFSV; |
609 | slot->errsv = ERRSV; |
625 | slot->errsv = ERRSV; |
610 | slot->irsgv = GvSV (irsgv); |
626 | slot->irsgv = GvSV (irsgv); |
|
|
627 | slot->hinthv = GvHV (PL_hintgv); |
611 | |
628 | |
612 | #define VAR(name,type) slot->name = PL_ ## name; |
629 | #define VAR(name,type) slot->name = PL_ ## name; |
613 | # include "state.h" |
630 | # include "state.h" |
614 | #undef VAR |
631 | #undef VAR |
615 | } |
632 | } |
… | |
… | |
831 | slf_check_nop (pTHX_ struct CoroSLF *frame) |
848 | slf_check_nop (pTHX_ struct CoroSLF *frame) |
832 | { |
849 | { |
833 | return 0; |
850 | return 0; |
834 | } |
851 | } |
835 | |
852 | |
|
|
853 | static int |
|
|
854 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
|
|
855 | { |
|
|
856 | return 1; |
|
|
857 | } |
|
|
858 | |
836 | static UNOP coro_setup_op; |
859 | static UNOP coro_setup_op; |
837 | |
860 | |
838 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
861 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
839 | coro_setup (pTHX_ struct coro *coro) |
862 | coro_setup (pTHX_ struct coro *coro) |
840 | { |
863 | { |
… | |
… | |
845 | |
868 | |
846 | PL_runops = RUNOPS_DEFAULT; |
869 | PL_runops = RUNOPS_DEFAULT; |
847 | PL_curcop = &PL_compiling; |
870 | PL_curcop = &PL_compiling; |
848 | PL_in_eval = EVAL_NULL; |
871 | PL_in_eval = EVAL_NULL; |
849 | PL_comppad = 0; |
872 | PL_comppad = 0; |
|
|
873 | PL_comppad_name = 0; |
|
|
874 | PL_comppad_name_fill = 0; |
|
|
875 | PL_comppad_name_floor = 0; |
850 | PL_curpm = 0; |
876 | PL_curpm = 0; |
851 | PL_curpad = 0; |
877 | PL_curpad = 0; |
852 | PL_localizing = 0; |
878 | PL_localizing = 0; |
853 | PL_dirty = 0; |
879 | PL_dirty = 0; |
854 | PL_restartop = 0; |
880 | PL_restartop = 0; |
855 | #if PERL_VERSION_ATLEAST (5,10,0) |
881 | #if PERL_VERSION_ATLEAST (5,10,0) |
856 | PL_parser = 0; |
882 | PL_parser = 0; |
857 | #endif |
883 | #endif |
|
|
884 | PL_hints = 0; |
858 | |
885 | |
859 | /* recreate the die/warn hooks */ |
886 | /* recreate the die/warn hooks */ |
860 | PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); |
887 | PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); |
861 | PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); |
888 | PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); |
862 | |
889 | |
863 | GvSV (PL_defgv) = newSV (0); |
890 | GvSV (PL_defgv) = newSV (0); |
864 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
891 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
865 | GvSV (PL_errgv) = newSV (0); |
892 | GvSV (PL_errgv) = newSV (0); |
866 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
893 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
|
|
894 | GvHV (PL_hintgv) = 0; |
867 | PL_rs = newSVsv (GvSV (irsgv)); |
895 | PL_rs = newSVsv (GvSV (irsgv)); |
868 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
896 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
869 | |
897 | |
870 | { |
898 | { |
871 | dSP; |
899 | dSP; |
… | |
… | |
889 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
917 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
890 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
918 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
891 | |
919 | |
892 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
920 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
893 | coro_setup_op.op_next = PL_op; |
921 | coro_setup_op.op_next = PL_op; |
894 | coro_setup_op.op_type = OP_CUSTOM; |
922 | coro_setup_op.op_type = OP_ENTERSUB; |
895 | coro_setup_op.op_ppaddr = pp_slf; |
923 | coro_setup_op.op_ppaddr = pp_slf; |
896 | /* no flags etc. required, as an init function won't be called */ |
924 | /* no flags etc. required, as an init function won't be called */ |
897 | |
925 | |
898 | PL_op = (OP *)&coro_setup_op; |
926 | PL_op = (OP *)&coro_setup_op; |
899 | |
927 | |
… | |
… | |
925 | SvREFCNT_dec (GvAV (PL_defgv)); |
953 | SvREFCNT_dec (GvAV (PL_defgv)); |
926 | SvREFCNT_dec (GvSV (PL_errgv)); |
954 | SvREFCNT_dec (GvSV (PL_errgv)); |
927 | SvREFCNT_dec (PL_defoutgv); |
955 | SvREFCNT_dec (PL_defoutgv); |
928 | SvREFCNT_dec (PL_rs); |
956 | SvREFCNT_dec (PL_rs); |
929 | SvREFCNT_dec (GvSV (irsgv)); |
957 | SvREFCNT_dec (GvSV (irsgv)); |
|
|
958 | SvREFCNT_dec (GvHV (PL_hintgv)); |
930 | |
959 | |
931 | SvREFCNT_dec (PL_diehook); |
960 | SvREFCNT_dec (PL_diehook); |
932 | SvREFCNT_dec (PL_warnhook); |
961 | SvREFCNT_dec (PL_warnhook); |
933 | |
962 | |
934 | SvREFCNT_dec (coro->saved_deffh); |
963 | SvREFCNT_dec (coro->saved_deffh); |
… | |
… | |
952 | static int |
981 | static int |
953 | runops_trace (pTHX) |
982 | runops_trace (pTHX) |
954 | { |
983 | { |
955 | COP *oldcop = 0; |
984 | COP *oldcop = 0; |
956 | int oldcxix = -2; |
985 | int oldcxix = -2; |
957 | struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */ |
|
|
958 | coro_cctx *cctx = coro->cctx; |
|
|
959 | |
986 | |
960 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
987 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
961 | { |
988 | { |
962 | PERL_ASYNC_CHECK (); |
989 | PERL_ASYNC_CHECK (); |
963 | |
990 | |
964 | if (cctx->flags & CC_TRACE_ALL) |
991 | if (cctx_current->flags & CC_TRACE_ALL) |
965 | { |
992 | { |
966 | if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) |
993 | if (PL_op->op_type == OP_LEAVESUB && cctx_current->flags & CC_TRACE_SUB) |
967 | { |
994 | { |
968 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
995 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
969 | SV **bot, **top; |
996 | SV **bot, **top; |
970 | AV *av = newAV (); /* return values */ |
997 | AV *av = newAV (); /* return values */ |
971 | SV **cb; |
998 | SV **cb; |
… | |
… | |
1008 | |
1035 | |
1009 | if (PL_curcop != &PL_compiling) |
1036 | if (PL_curcop != &PL_compiling) |
1010 | { |
1037 | { |
1011 | SV **cb; |
1038 | SV **cb; |
1012 | |
1039 | |
1013 | if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) |
1040 | if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB) |
1014 | { |
1041 | { |
1015 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1042 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1016 | |
1043 | |
1017 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1044 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1018 | { |
1045 | { |
1019 | runops_proc_t old_runops = PL_runops; |
|
|
1020 | dSP; |
1046 | dSP; |
1021 | GV *gv = CvGV (cx->blk_sub.cv); |
1047 | GV *gv = CvGV (cx->blk_sub.cv); |
1022 | SV *fullname = sv_2mortal (newSV (0)); |
1048 | SV *fullname = sv_2mortal (newSV (0)); |
1023 | |
1049 | |
1024 | if (isGV (gv)) |
1050 | if (isGV (gv)) |
… | |
… | |
1042 | } |
1068 | } |
1043 | |
1069 | |
1044 | oldcxix = cxstack_ix; |
1070 | oldcxix = cxstack_ix; |
1045 | } |
1071 | } |
1046 | |
1072 | |
1047 | if (cctx->flags & CC_TRACE_LINE) |
1073 | if (cctx_current->flags & CC_TRACE_LINE) |
1048 | { |
1074 | { |
1049 | dSP; |
1075 | dSP; |
1050 | |
1076 | |
1051 | PL_runops = RUNOPS_DEFAULT; |
1077 | PL_runops = RUNOPS_DEFAULT; |
1052 | ENTER; |
1078 | ENTER; |
… | |
… | |
1071 | |
1097 | |
1072 | TAINT_NOT; |
1098 | TAINT_NOT; |
1073 | return 0; |
1099 | return 0; |
1074 | } |
1100 | } |
1075 | |
1101 | |
1076 | static struct coro_cctx *cctx_ssl_cctx; |
|
|
1077 | static struct CoroSLF cctx_ssl_frame; |
1102 | static struct CoroSLF cctx_ssl_frame; |
1078 | |
1103 | |
1079 | static void |
1104 | static void |
1080 | slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) |
1105 | slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) |
1081 | { |
1106 | { |
1082 | ta->prev = (struct coro *)cctx_ssl_cctx; |
|
|
1083 | ta->next = 0; |
1107 | ta->prev = 0; |
1084 | } |
1108 | } |
1085 | |
1109 | |
1086 | static int |
1110 | static int |
1087 | slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) |
1111 | slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) |
1088 | { |
1112 | { |
… | |
… | |
1091 | return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ |
1115 | return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ |
1092 | } |
1116 | } |
1093 | |
1117 | |
1094 | /* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ |
1118 | /* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ |
1095 | static void NOINLINE |
1119 | static void NOINLINE |
1096 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1120 | cctx_prepare (pTHX) |
1097 | { |
1121 | { |
1098 | PL_top_env = &PL_start_env; |
1122 | PL_top_env = &PL_start_env; |
1099 | |
1123 | |
1100 | if (cctx->flags & CC_TRACE) |
1124 | if (cctx_current->flags & CC_TRACE) |
1101 | PL_runops = runops_trace; |
1125 | PL_runops = runops_trace; |
1102 | |
1126 | |
1103 | /* we already must be executing an SLF op, there is no other valid way |
1127 | /* we already must be executing an SLF op, there is no other valid way |
1104 | * that can lead to creation of a new cctx */ |
1128 | * that can lead to creation of a new cctx */ |
1105 | assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", |
1129 | assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", |
1106 | slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); |
1130 | slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); |
1107 | |
1131 | |
1108 | /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ |
1132 | /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ |
1109 | cctx_ssl_cctx = cctx; |
|
|
1110 | cctx_ssl_frame = slf_frame; |
1133 | cctx_ssl_frame = slf_frame; |
1111 | |
1134 | |
1112 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1135 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1113 | slf_frame.check = slf_check_set_stacklevel; |
1136 | slf_frame.check = slf_check_set_stacklevel; |
1114 | } |
1137 | } |
… | |
… | |
1137 | /* normally we would need to skip the entersub here */ |
1160 | /* normally we would need to skip the entersub here */ |
1138 | /* not doing so will re-execute it, which is exactly what we want */ |
1161 | /* not doing so will re-execute it, which is exactly what we want */ |
1139 | /* PL_nop = PL_nop->op_next */ |
1162 | /* PL_nop = PL_nop->op_next */ |
1140 | |
1163 | |
1141 | /* inject a fake subroutine call to cctx_init */ |
1164 | /* inject a fake subroutine call to cctx_init */ |
1142 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1165 | cctx_prepare (aTHX); |
1143 | |
1166 | |
1144 | /* cctx_run is the alternative tail of transfer() */ |
1167 | /* cctx_run is the alternative tail of transfer() */ |
1145 | transfer_tail (aTHX); |
1168 | transfer_tail (aTHX); |
1146 | |
1169 | |
1147 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1170 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
… | |
… | |
1242 | cctx_destroy (coro_cctx *cctx) |
1265 | cctx_destroy (coro_cctx *cctx) |
1243 | { |
1266 | { |
1244 | if (!cctx) |
1267 | if (!cctx) |
1245 | return; |
1268 | return; |
1246 | |
1269 | |
|
|
1270 | assert (cctx != cctx_current);//D temporary |
|
|
1271 | |
1247 | --cctx_count; |
1272 | --cctx_count; |
1248 | coro_destroy (&cctx->cctx); |
1273 | coro_destroy (&cctx->cctx); |
1249 | |
1274 | |
1250 | /* coro_transfer creates new, empty cctx's */ |
1275 | /* coro_transfer creates new, empty cctx's */ |
1251 | if (cctx->sptr) |
1276 | if (cctx->sptr) |
… | |
… | |
1314 | /* TODO: throwing up here is considered harmful */ |
1339 | /* TODO: throwing up here is considered harmful */ |
1315 | |
1340 | |
1316 | if (expect_true (prev != next)) |
1341 | if (expect_true (prev != next)) |
1317 | { |
1342 | { |
1318 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1343 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1319 | croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,"); |
1344 | croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,"); |
1320 | |
1345 | |
1321 | if (expect_false (next->flags & CF_RUNNING)) |
1346 | if (expect_false (next->flags & CF_RUNNING)) |
1322 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); |
1347 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); |
1323 | |
1348 | |
1324 | if (expect_false (next->flags & CF_DESTROYED)) |
1349 | if (expect_false (next->flags & CF_DESTROYED)) |
… | |
… | |
1336 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1361 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1337 | { |
1362 | { |
1338 | dSTACKLEVEL; |
1363 | dSTACKLEVEL; |
1339 | |
1364 | |
1340 | /* sometimes transfer is only called to set idle_sp */ |
1365 | /* sometimes transfer is only called to set idle_sp */ |
1341 | if (expect_false (!next)) |
1366 | if (expect_false (!prev)) |
1342 | { |
1367 | { |
1343 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1368 | cctx_current->idle_sp = STACKLEVEL; |
1344 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1369 | assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1345 | } |
1370 | } |
1346 | else if (expect_true (prev != next)) |
1371 | else if (expect_true (prev != next)) |
1347 | { |
1372 | { |
1348 | coro_cctx *prev__cctx; |
1373 | coro_cctx *cctx_prev; |
1349 | |
1374 | |
1350 | if (expect_false (prev->flags & CF_NEW)) |
1375 | if (expect_false (prev->flags & CF_NEW)) |
1351 | { |
1376 | { |
1352 | /* create a new empty/source context */ |
1377 | /* create a new empty/source context */ |
1353 | prev->cctx = cctx_new_empty (); |
|
|
1354 | prev->flags &= ~CF_NEW; |
1378 | prev->flags &= ~CF_NEW; |
1355 | prev->flags |= CF_RUNNING; |
1379 | prev->flags |= CF_RUNNING; |
1356 | } |
1380 | } |
1357 | |
1381 | |
1358 | prev->flags &= ~CF_RUNNING; |
1382 | prev->flags &= ~CF_RUNNING; |
… | |
… | |
1369 | coro_setup (aTHX_ next); |
1393 | coro_setup (aTHX_ next); |
1370 | } |
1394 | } |
1371 | else |
1395 | else |
1372 | load_perl (aTHX_ next); |
1396 | load_perl (aTHX_ next); |
1373 | |
1397 | |
1374 | prev__cctx = prev->cctx; |
1398 | assert (!prev->cctx);//D temporary |
1375 | |
1399 | |
1376 | /* possibly untie and reuse the cctx */ |
1400 | /* possibly untie and reuse the cctx */ |
1377 | if (expect_true ( |
1401 | if (expect_true ( |
1378 | prev__cctx->idle_sp == STACKLEVEL |
1402 | cctx_current->idle_sp == STACKLEVEL |
1379 | && !(prev__cctx->flags & CC_TRACE) |
1403 | && !(cctx_current->flags & CC_TRACE) |
1380 | && !force_cctx |
1404 | && !force_cctx |
1381 | )) |
1405 | )) |
1382 | { |
1406 | { |
1383 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1407 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1384 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); |
1408 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te)); |
1385 | |
1409 | |
1386 | prev->cctx = 0; |
|
|
1387 | |
|
|
1388 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1410 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */ |
1389 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
1411 | /* without this the next cctx_get might destroy the running cctx while still in use */ |
1390 | if (expect_false (CCTX_EXPIRED (prev__cctx))) |
1412 | if (expect_false (CCTX_EXPIRED (cctx_current))) |
1391 | if (!next->cctx) |
1413 | if (expect_true (!next->cctx)) |
1392 | next->cctx = cctx_get (aTHX); |
1414 | next->cctx = cctx_get (aTHX); |
1393 | |
1415 | |
1394 | cctx_put (prev__cctx); |
1416 | cctx_put (cctx_current); |
1395 | } |
1417 | } |
|
|
1418 | else |
|
|
1419 | prev->cctx = cctx_current; |
1396 | |
1420 | |
1397 | ++next->usecount; |
1421 | ++next->usecount; |
1398 | |
1422 | |
1399 | if (expect_true (!next->cctx)) |
1423 | cctx_prev = cctx_current; |
1400 | next->cctx = cctx_get (aTHX); |
1424 | cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX); |
1401 | |
1425 | |
1402 | if (expect_false (prev__cctx != next->cctx)) |
1426 | next->cctx = 0; |
|
|
1427 | |
|
|
1428 | if (expect_false (cctx_prev != cctx_current)) |
1403 | { |
1429 | { |
1404 | prev__cctx->top_env = PL_top_env; |
1430 | cctx_prev->top_env = PL_top_env; |
1405 | PL_top_env = next->cctx->top_env; |
1431 | PL_top_env = cctx_current->top_env; |
1406 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1432 | coro_transfer (&cctx_prev->cctx, &cctx_current->cctx); |
1407 | } |
1433 | } |
1408 | |
1434 | |
1409 | transfer_tail (aTHX); |
1435 | transfer_tail (aTHX); |
1410 | } |
1436 | } |
1411 | } |
1437 | } |
… | |
… | |
1566 | { |
1592 | { |
1567 | struct coro *coro; |
1593 | struct coro *coro; |
1568 | SV *sv_hook; |
1594 | SV *sv_hook; |
1569 | void (*xs_hook)(void); |
1595 | void (*xs_hook)(void); |
1570 | |
1596 | |
1571 | if (SvROK (coro_sv)) |
|
|
1572 | coro_sv = SvRV (coro_sv); |
|
|
1573 | |
|
|
1574 | coro = SvSTATE (coro_sv); |
1597 | coro = SvSTATE (coro_sv); |
1575 | |
1598 | |
1576 | if (coro->flags & CF_READY) |
1599 | if (coro->flags & CF_READY) |
1577 | return 0; |
1600 | return 0; |
1578 | |
1601 | |
… | |
… | |
1609 | api_is_ready (pTHX_ SV *coro_sv) |
1632 | api_is_ready (pTHX_ SV *coro_sv) |
1610 | { |
1633 | { |
1611 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1634 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1612 | } |
1635 | } |
1613 | |
1636 | |
|
|
1637 | /* expects to own a reference to next->hv */ |
1614 | INLINE void |
1638 | INLINE void |
1615 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1639 | prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
1616 | { |
1640 | { |
1617 | SV *prev_sv, *next_sv; |
|
|
1618 | |
|
|
1619 | for (;;) |
|
|
1620 | { |
|
|
1621 | next_sv = coro_deq (aTHX); |
|
|
1622 | |
|
|
1623 | /* nothing to schedule: call the idle handler */ |
|
|
1624 | if (expect_false (!next_sv)) |
|
|
1625 | { |
|
|
1626 | dSP; |
|
|
1627 | |
|
|
1628 | ENTER; |
|
|
1629 | SAVETMPS; |
|
|
1630 | |
|
|
1631 | PUSHMARK (SP); |
|
|
1632 | PUTBACK; |
|
|
1633 | call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); |
|
|
1634 | |
|
|
1635 | FREETMPS; |
|
|
1636 | LEAVE; |
|
|
1637 | continue; |
|
|
1638 | } |
|
|
1639 | |
|
|
1640 | ta->next = SvSTATE_hv (next_sv); |
|
|
1641 | |
|
|
1642 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1643 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
|
|
1644 | { |
|
|
1645 | SvREFCNT_dec (next_sv); |
|
|
1646 | /* coro_nready has already been taken care of by destroy */ |
|
|
1647 | continue; |
|
|
1648 | } |
|
|
1649 | |
|
|
1650 | --coro_nready; |
|
|
1651 | break; |
|
|
1652 | } |
|
|
1653 | |
|
|
1654 | /* free this only after the transfer */ |
|
|
1655 | prev_sv = SvRV (coro_current); |
1641 | SV *prev_sv = SvRV (coro_current); |
|
|
1642 | |
1656 | ta->prev = SvSTATE_hv (prev_sv); |
1643 | ta->prev = SvSTATE_hv (prev_sv); |
|
|
1644 | ta->next = next; |
|
|
1645 | |
1657 | TRANSFER_CHECK (*ta); |
1646 | TRANSFER_CHECK (*ta); |
1658 | assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); |
1647 | |
1659 | ta->next->flags &= ~CF_READY; |
|
|
1660 | SvRV_set (coro_current, next_sv); |
1648 | SvRV_set (coro_current, (SV *)next->hv); |
1661 | |
1649 | |
1662 | free_coro_mortal (aTHX); |
1650 | free_coro_mortal (aTHX); |
1663 | coro_mortal = prev_sv; |
1651 | coro_mortal = prev_sv; |
1664 | } |
1652 | } |
1665 | |
1653 | |
|
|
1654 | static void |
|
|
1655 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
|
|
1656 | { |
|
|
1657 | for (;;) |
|
|
1658 | { |
|
|
1659 | SV *next_sv = coro_deq (aTHX); |
|
|
1660 | |
|
|
1661 | if (expect_true (next_sv)) |
|
|
1662 | { |
|
|
1663 | struct coro *next = SvSTATE_hv (next_sv); |
|
|
1664 | |
|
|
1665 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1666 | if (expect_false (next->flags & CF_DESTROYED)) |
|
|
1667 | SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ |
|
|
1668 | else |
|
|
1669 | { |
|
|
1670 | next->flags &= ~CF_READY; |
|
|
1671 | --coro_nready; |
|
|
1672 | |
|
|
1673 | prepare_schedule_to (aTHX_ ta, next); |
|
|
1674 | break; |
|
|
1675 | } |
|
|
1676 | } |
|
|
1677 | else |
|
|
1678 | { |
|
|
1679 | /* nothing to schedule: call the idle handler */ |
|
|
1680 | if (SvROK (sv_idle) |
|
|
1681 | && SvOBJECT (SvRV (sv_idle))) |
|
|
1682 | { |
|
|
1683 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
|
|
1684 | api_ready (aTHX_ SvRV (sv_idle)); |
|
|
1685 | --coro_nready; |
|
|
1686 | } |
|
|
1687 | else |
|
|
1688 | { |
|
|
1689 | dSP; |
|
|
1690 | |
|
|
1691 | ENTER; |
|
|
1692 | SAVETMPS; |
|
|
1693 | |
|
|
1694 | PUSHMARK (SP); |
|
|
1695 | PUTBACK; |
|
|
1696 | call_sv (sv_idle, G_VOID | G_DISCARD); |
|
|
1697 | |
|
|
1698 | FREETMPS; |
|
|
1699 | LEAVE; |
|
|
1700 | } |
|
|
1701 | } |
|
|
1702 | } |
|
|
1703 | } |
|
|
1704 | |
1666 | INLINE void |
1705 | INLINE void |
1667 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1706 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1668 | { |
1707 | { |
1669 | api_ready (aTHX_ coro_current); |
1708 | api_ready (aTHX_ coro_current); |
1670 | prepare_schedule (aTHX_ ta); |
1709 | prepare_schedule (aTHX_ ta); |
… | |
… | |
1689 | { |
1728 | { |
1690 | struct coro_transfer_args ta; |
1729 | struct coro_transfer_args ta; |
1691 | |
1730 | |
1692 | prepare_schedule (aTHX_ &ta); |
1731 | prepare_schedule (aTHX_ &ta); |
1693 | TRANSFER (ta, 1); |
1732 | TRANSFER (ta, 1); |
|
|
1733 | } |
|
|
1734 | |
|
|
1735 | static void |
|
|
1736 | api_schedule_to (pTHX_ SV *coro_sv) |
|
|
1737 | { |
|
|
1738 | struct coro_transfer_args ta; |
|
|
1739 | struct coro *next = SvSTATE (coro_sv); |
|
|
1740 | |
|
|
1741 | SvREFCNT_inc_NN (coro_sv); |
|
|
1742 | prepare_schedule_to (aTHX_ &ta, next); |
1694 | } |
1743 | } |
1695 | |
1744 | |
1696 | static int |
1745 | static int |
1697 | api_cede (pTHX) |
1746 | api_cede (pTHX) |
1698 | { |
1747 | { |
… | |
… | |
1727 | static void |
1776 | static void |
1728 | api_trace (pTHX_ SV *coro_sv, int flags) |
1777 | api_trace (pTHX_ SV *coro_sv, int flags) |
1729 | { |
1778 | { |
1730 | struct coro *coro = SvSTATE (coro_sv); |
1779 | struct coro *coro = SvSTATE (coro_sv); |
1731 | |
1780 | |
|
|
1781 | if (coro->flags & CF_RUNNING) |
|
|
1782 | croak ("cannot enable tracing on a running coroutine, caught"); |
|
|
1783 | |
1732 | if (flags & CC_TRACE) |
1784 | if (flags & CC_TRACE) |
1733 | { |
1785 | { |
1734 | if (!coro->cctx) |
1786 | if (!coro->cctx) |
1735 | coro->cctx = cctx_new_run (); |
1787 | coro->cctx = cctx_new_run (); |
1736 | else if (!(coro->cctx->flags & CC_TRACE)) |
1788 | else if (!(coro->cctx->flags & CC_TRACE)) |
1737 | croak ("cannot enable tracing on coroutine with custom stack,"); |
1789 | croak ("cannot enable tracing on coroutine with custom stack, caught"); |
1738 | |
1790 | |
1739 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1791 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1740 | } |
1792 | } |
1741 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
1793 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
1742 | { |
1794 | { |
… | |
… | |
1745 | if (coro->flags & CF_RUNNING) |
1797 | if (coro->flags & CF_RUNNING) |
1746 | PL_runops = RUNOPS_DEFAULT; |
1798 | PL_runops = RUNOPS_DEFAULT; |
1747 | else |
1799 | else |
1748 | coro->slot->runops = RUNOPS_DEFAULT; |
1800 | coro->slot->runops = RUNOPS_DEFAULT; |
1749 | } |
1801 | } |
|
|
1802 | } |
|
|
1803 | |
|
|
1804 | static void |
|
|
1805 | coro_call_on_destroy (pTHX_ struct coro *coro) |
|
|
1806 | { |
|
|
1807 | SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0); |
|
|
1808 | SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0); |
|
|
1809 | |
|
|
1810 | if (on_destroyp) |
|
|
1811 | { |
|
|
1812 | AV *on_destroy = (AV *)SvRV (*on_destroyp); |
|
|
1813 | |
|
|
1814 | while (AvFILLp (on_destroy) >= 0) |
|
|
1815 | { |
|
|
1816 | dSP; /* don't disturb outer sp */ |
|
|
1817 | SV *cb = av_pop (on_destroy); |
|
|
1818 | |
|
|
1819 | PUSHMARK (SP); |
|
|
1820 | |
|
|
1821 | if (statusp) |
|
|
1822 | { |
|
|
1823 | int i; |
|
|
1824 | AV *status = (AV *)SvRV (*statusp); |
|
|
1825 | EXTEND (SP, AvFILLp (status) + 1); |
|
|
1826 | |
|
|
1827 | for (i = 0; i <= AvFILLp (status); ++i) |
|
|
1828 | PUSHs (AvARRAY (status)[i]); |
|
|
1829 | } |
|
|
1830 | |
|
|
1831 | PUTBACK; |
|
|
1832 | call_sv (sv_2mortal (cb), G_VOID | G_DISCARD); |
|
|
1833 | } |
|
|
1834 | } |
|
|
1835 | } |
|
|
1836 | |
|
|
1837 | static void |
|
|
1838 | slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1839 | { |
|
|
1840 | int i; |
|
|
1841 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1842 | AV *av = newAV (); |
|
|
1843 | |
|
|
1844 | av_extend (av, items - 1); |
|
|
1845 | for (i = 0; i < items; ++i) |
|
|
1846 | av_push (av, SvREFCNT_inc_NN (arg [i])); |
|
|
1847 | |
|
|
1848 | hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); |
|
|
1849 | |
|
|
1850 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
|
|
1851 | api_ready (aTHX_ sv_manager); |
|
|
1852 | |
|
|
1853 | frame->prepare = prepare_schedule; |
|
|
1854 | frame->check = slf_check_repeat; |
1750 | } |
1855 | } |
1751 | |
1856 | |
1752 | /*****************************************************************************/ |
1857 | /*****************************************************************************/ |
1753 | /* async pool handler */ |
1858 | /* async pool handler */ |
1754 | |
1859 | |
… | |
… | |
1987 | static void |
2092 | static void |
1988 | slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2093 | slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1989 | { |
2094 | { |
1990 | frame->prepare = prepare_schedule; |
2095 | frame->prepare = prepare_schedule; |
1991 | frame->check = slf_check_nop; |
2096 | frame->check = slf_check_nop; |
|
|
2097 | } |
|
|
2098 | |
|
|
2099 | static void |
|
|
2100 | slf_prepare_schedule_to (pTHX_ struct coro_transfer_args *ta) |
|
|
2101 | { |
|
|
2102 | struct coro *next = (struct coro *)slf_frame.data; |
|
|
2103 | |
|
|
2104 | SvREFCNT_inc_NN (next->hv); |
|
|
2105 | prepare_schedule_to (aTHX_ ta, next); |
|
|
2106 | } |
|
|
2107 | |
|
|
2108 | static void |
|
|
2109 | slf_init_schedule_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2110 | { |
|
|
2111 | if (!items) |
|
|
2112 | croak ("Coro::schedule_to expects a coroutine argument, caught"); |
|
|
2113 | |
|
|
2114 | frame->data = (void *)SvSTATE (arg [0]); |
|
|
2115 | frame->prepare = slf_prepare_schedule_to; |
|
|
2116 | frame->check = slf_check_nop; |
|
|
2117 | } |
|
|
2118 | |
|
|
2119 | static void |
|
|
2120 | slf_init_cede_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2121 | { |
|
|
2122 | api_ready (aTHX_ SvRV (coro_current)); |
|
|
2123 | |
|
|
2124 | slf_init_schedule_to (aTHX_ frame, cv, arg, items); |
1992 | } |
2125 | } |
1993 | |
2126 | |
1994 | static void |
2127 | static void |
1995 | slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2128 | slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1996 | { |
2129 | { |
… | |
… | |
2142 | } |
2275 | } |
2143 | else |
2276 | else |
2144 | slf_argc = 0; |
2277 | slf_argc = 0; |
2145 | |
2278 | |
2146 | PL_op->op_ppaddr = pp_slf; |
2279 | PL_op->op_ppaddr = pp_slf; |
2147 | PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ |
2280 | /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ |
2148 | |
2281 | |
2149 | PL_op = (OP *)&slf_restore; |
2282 | PL_op = (OP *)&slf_restore; |
2150 | } |
2283 | } |
2151 | |
2284 | |
2152 | /*****************************************************************************/ |
2285 | /*****************************************************************************/ |
… | |
… | |
2616 | XSRETURN_EMPTY; |
2749 | XSRETURN_EMPTY; |
2617 | } |
2750 | } |
2618 | |
2751 | |
2619 | /*****************************************************************************/ |
2752 | /*****************************************************************************/ |
2620 | |
2753 | |
|
|
2754 | #if CORO_CLONE |
|
|
2755 | # include "clone.c" |
|
|
2756 | #endif |
|
|
2757 | |
2621 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2758 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2622 | |
2759 | |
2623 | PROTOTYPES: DISABLE |
2760 | PROTOTYPES: DISABLE |
2624 | |
2761 | |
2625 | BOOT: |
2762 | BOOT: |
… | |
… | |
2629 | coro_thx = PERL_GET_CONTEXT; |
2766 | coro_thx = PERL_GET_CONTEXT; |
2630 | # endif |
2767 | # endif |
2631 | #endif |
2768 | #endif |
2632 | BOOT_PAGESIZE; |
2769 | BOOT_PAGESIZE; |
2633 | |
2770 | |
|
|
2771 | cctx_current = cctx_new_empty (); |
|
|
2772 | |
2634 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
2773 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
2635 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
2774 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
2636 | |
2775 | |
2637 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
2776 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
2638 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
2777 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
… | |
… | |
2657 | |
2796 | |
2658 | { |
2797 | { |
2659 | SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); |
2798 | SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); |
2660 | |
2799 | |
2661 | if (!PL_custom_op_names) PL_custom_op_names = newHV (); |
2800 | if (!PL_custom_op_names) PL_custom_op_names = newHV (); |
2662 | hv_store_ent (PL_custom_op_names, slf, |
2801 | hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0); |
2663 | newSVpv ("coro_slf", 0), 0); |
|
|
2664 | |
2802 | |
2665 | if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); |
2803 | if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); |
2666 | hv_store_ent (PL_custom_op_descs, slf, |
2804 | hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0); |
2667 | newSVpv ("coro schedule like function", 0), 0); |
|
|
2668 | } |
2805 | } |
2669 | |
2806 | |
2670 | coroapi.ver = CORO_API_VERSION; |
2807 | coroapi.ver = CORO_API_VERSION; |
2671 | coroapi.rev = CORO_API_REVISION; |
2808 | coroapi.rev = CORO_API_REVISION; |
2672 | |
2809 | |
… | |
… | |
2765 | void |
2902 | void |
2766 | _exit (int code) |
2903 | _exit (int code) |
2767 | PROTOTYPE: $ |
2904 | PROTOTYPE: $ |
2768 | CODE: |
2905 | CODE: |
2769 | _exit (code); |
2906 | _exit (code); |
|
|
2907 | |
|
|
2908 | SV * |
|
|
2909 | clone (Coro::State coro) |
|
|
2910 | CODE: |
|
|
2911 | { |
|
|
2912 | #if CORO_CLONE |
|
|
2913 | struct coro *ncoro = coro_clone (coro); |
|
|
2914 | MAGIC *mg; |
|
|
2915 | /* TODO: too much duplication */ |
|
|
2916 | ncoro->hv = newHV (); |
|
|
2917 | mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); |
|
|
2918 | mg->mg_flags |= MGf_DUP; |
|
|
2919 | RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv)); |
|
|
2920 | #else |
|
|
2921 | croak ("Coro::State->clone has not been configured into this installation of Coro, realised"); |
|
|
2922 | #endif |
|
|
2923 | } |
|
|
2924 | OUTPUT: |
|
|
2925 | RETVAL |
2770 | |
2926 | |
2771 | int |
2927 | int |
2772 | cctx_stacksize (int new_stacksize = 0) |
2928 | cctx_stacksize (int new_stacksize = 0) |
2773 | PROTOTYPE: ;$ |
2929 | PROTOTYPE: ;$ |
2774 | CODE: |
2930 | CODE: |
… | |
… | |
2895 | |
3051 | |
2896 | SV * |
3052 | SV * |
2897 | has_cctx (Coro::State coro) |
3053 | has_cctx (Coro::State coro) |
2898 | PROTOTYPE: $ |
3054 | PROTOTYPE: $ |
2899 | CODE: |
3055 | CODE: |
2900 | RETVAL = boolSV (!!coro->cctx); |
3056 | /* maybe manage the running flag differently */ |
|
|
3057 | RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING)); |
2901 | OUTPUT: |
3058 | OUTPUT: |
2902 | RETVAL |
3059 | RETVAL |
2903 | |
3060 | |
2904 | int |
3061 | int |
2905 | is_traced (Coro::State coro) |
3062 | is_traced (Coro::State coro) |
… | |
… | |
2925 | |
3082 | |
2926 | void |
3083 | void |
2927 | force_cctx () |
3084 | force_cctx () |
2928 | PROTOTYPE: |
3085 | PROTOTYPE: |
2929 | CODE: |
3086 | CODE: |
2930 | SvSTATE_current->cctx->idle_sp = 0; |
3087 | cctx_current->idle_sp = 0; |
2931 | |
3088 | |
2932 | void |
3089 | void |
2933 | swap_defsv (Coro::State self) |
3090 | swap_defsv (Coro::State self) |
2934 | PROTOTYPE: $ |
3091 | PROTOTYPE: $ |
2935 | ALIAS: |
3092 | ALIAS: |
… | |
… | |
2950 | |
3107 | |
2951 | BOOT: |
3108 | BOOT: |
2952 | { |
3109 | { |
2953 | int i; |
3110 | int i; |
2954 | |
3111 | |
2955 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
|
|
2956 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3112 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
2957 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3113 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
2958 | cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); |
3114 | cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); |
2959 | cv_coro_terminate = get_cv ( "Coro::terminate", GV_ADD); |
3115 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
2960 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); |
3116 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
2961 | SvREADONLY_on (coro_current); |
3117 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
|
|
3118 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
|
|
3119 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
|
|
3120 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
2962 | |
3121 | |
2963 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3122 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
2964 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3123 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
2965 | cv_pool_handler = get_cv ("Coro::_pool_handler", 0); SvREADONLY_on (cv_pool_handler); |
3124 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
2966 | cv_coro_new = get_cv ("Coro::new", 0); SvREADONLY_on (cv_coro_new); |
3125 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
2967 | |
3126 | |
2968 | coro_stash = gv_stashpv ("Coro", TRUE); |
3127 | coro_stash = gv_stashpv ("Coro", TRUE); |
2969 | |
3128 | |
2970 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
3129 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
2971 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
3130 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
… | |
… | |
2979 | |
3138 | |
2980 | { |
3139 | { |
2981 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
3140 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
2982 | |
3141 | |
2983 | coroapi.schedule = api_schedule; |
3142 | coroapi.schedule = api_schedule; |
|
|
3143 | coroapi.schedule_to = api_schedule_to; |
2984 | coroapi.cede = api_cede; |
3144 | coroapi.cede = api_cede; |
2985 | coroapi.cede_notself = api_cede_notself; |
3145 | coroapi.cede_notself = api_cede_notself; |
2986 | coroapi.ready = api_ready; |
3146 | coroapi.ready = api_ready; |
2987 | coroapi.is_ready = api_is_ready; |
3147 | coroapi.is_ready = api_is_ready; |
2988 | coroapi.nready = coro_nready; |
3148 | coroapi.nready = coro_nready; |
… | |
… | |
2993 | SvREADONLY_on (sv); |
3153 | SvREADONLY_on (sv); |
2994 | } |
3154 | } |
2995 | } |
3155 | } |
2996 | |
3156 | |
2997 | void |
3157 | void |
|
|
3158 | terminate (...) |
|
|
3159 | CODE: |
|
|
3160 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
|
|
3161 | |
|
|
3162 | void |
2998 | schedule (...) |
3163 | schedule (...) |
2999 | CODE: |
3164 | CODE: |
3000 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
3165 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
|
|
3166 | |
|
|
3167 | void |
|
|
3168 | schedule_to (...) |
|
|
3169 | CODE: |
|
|
3170 | CORO_EXECUTE_SLF_XS (slf_init_schedule_to); |
|
|
3171 | |
|
|
3172 | void |
|
|
3173 | cede_to (...) |
|
|
3174 | CODE: |
|
|
3175 | CORO_EXECUTE_SLF_XS (slf_init_cede_to); |
3001 | |
3176 | |
3002 | void |
3177 | void |
3003 | cede (...) |
3178 | cede (...) |
3004 | CODE: |
3179 | CODE: |
3005 | CORO_EXECUTE_SLF_XS (slf_init_cede); |
3180 | CORO_EXECUTE_SLF_XS (slf_init_cede); |
3006 | |
3181 | |
3007 | void |
3182 | void |
3008 | cede_notself (...) |
3183 | cede_notself (...) |
3009 | CODE: |
3184 | CODE: |
3010 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
3185 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
|
|
3186 | |
|
|
3187 | void |
|
|
3188 | _cancel (Coro::State self) |
|
|
3189 | CODE: |
|
|
3190 | coro_state_destroy (aTHX_ self); |
|
|
3191 | coro_call_on_destroy (aTHX_ self); |
3011 | |
3192 | |
3012 | void |
3193 | void |
3013 | _set_current (SV *current) |
3194 | _set_current (SV *current) |
3014 | PROTOTYPE: $ |
3195 | PROTOTYPE: $ |
3015 | CODE: |
3196 | CODE: |
… | |
… | |
3086 | PUSHMARK (SP); |
3267 | PUSHMARK (SP); |
3087 | EXTEND (SP, 2); |
3268 | EXTEND (SP, 2); |
3088 | PUSHs (sv_Coro); |
3269 | PUSHs (sv_Coro); |
3089 | PUSHs ((SV *)cv_pool_handler); |
3270 | PUSHs ((SV *)cv_pool_handler); |
3090 | PUTBACK; |
3271 | PUTBACK; |
3091 | call_sv ((SV *)cv_coro_new, G_SCALAR); |
3272 | call_sv ((SV *)cv_coro_state_new, G_SCALAR); |
3092 | SPAGAIN; |
3273 | SPAGAIN; |
3093 | |
3274 | |
3094 | hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); |
3275 | hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); |
3095 | } |
3276 | } |
3096 | |
3277 | |