ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.xs
(Generate patch)

Comparing Coro/Coro/State.xs (file contents):
Revision 1.317 by root, Thu Nov 20 06:28:52 2008 UTC vs.
Revision 1.328 by root, Tue Nov 25 09:49:43 2008 UTC

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
170static AV *main_mainstack; /* used to differentiate between $main and others */ 173static AV *main_mainstack; /* used to differentiate between $main and others */
171static JMPENV *main_top_env; 174static JMPENV *main_top_env;
172static HV *coro_state_stash, *coro_stash; 175static HV *coro_state_stash, *coro_stash;
173static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ 176static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
174 177
178static AV *av_destroy; /* destruction queue */
179static SV *sv_manager; /* the manager coro */
180static SV *sv_idle; /* $Coro::idle */
181
175static GV *irsgv; /* $/ */ 182static GV *irsgv; /* $/ */
176static GV *stdoutgv; /* *STDOUT */ 183static GV *stdoutgv; /* *STDOUT */
177static SV *rv_diehook; 184static SV *rv_diehook;
178static SV *rv_warnhook; 185static SV *rv_warnhook;
179static HV *hv_sig; /* %SIG */ 186static HV *hv_sig; /* %SIG */
180 187
181/* async_pool helper stuff */ 188/* async_pool helper stuff */
182static SV *sv_pool_rss; 189static SV *sv_pool_rss;
183static SV *sv_pool_size; 190static SV *sv_pool_size;
184static SV *sv_async_pool_idle; 191static SV *sv_async_pool_idle; /* description string */
185static AV *av_async_pool; 192static AV *av_async_pool; /* idle pool */
186static SV *sv_Coro; 193static SV *sv_Coro; /* class string */
187static CV *cv_pool_handler; 194static CV *cv_pool_handler;
188static CV *cv_coro_state_new; 195static CV *cv_coro_state_new;
189 196
190/* Coro::AnyEvent */ 197/* Coro::AnyEvent */
191static SV *sv_activity; 198static SV *sv_activity;
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
234coro_cctx *cctx_current; /* the currently running cctx */
235
236/*****************************************************************************/
237
227enum { 238enum {
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
348static AV * 360static AV *
349coro_clone_padlist (pTHX_ CV *cv) 361coro_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 ();
474 CV *cp = Perl_cv_clone (aTHX_ cv); 486 CV *cp = Perl_cv_clone (aTHX_ cv);
475 CvPADLIST (cv) = CvPADLIST (cp); 487 CvPADLIST (cv) = CvPADLIST (cp);
476 CvPADLIST (cp) = 0; 488 CvPADLIST (cp) = 0;
477 SvREFCNT_dec (cp); 489 SvREFCNT_dec (cp);
478#else 490#else
479 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 491 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv);
480#endif 492#endif
481 } 493 }
482} 494}
483 495
484static void 496static void
506 perl_slots *slot = c->slot; 518 perl_slots *slot = c->slot;
507 c->slot = 0; 519 c->slot = 0;
508 520
509 PL_mainstack = c->mainstack; 521 PL_mainstack = c->mainstack;
510 522
511 GvSV (PL_defgv) = slot->defsv; 523 GvSV (PL_defgv) = slot->defsv;
512 GvAV (PL_defgv) = slot->defav; 524 GvAV (PL_defgv) = slot->defav;
513 GvSV (PL_errgv) = slot->errsv; 525 GvSV (PL_errgv) = slot->errsv;
514 GvSV (irsgv) = slot->irsgv; 526 GvSV (irsgv) = slot->irsgv;
527 GvHV (PL_hintgv) = slot->hinthv;
515 528
516 #define VAR(name,type) PL_ ## name = slot->name; 529 #define VAR(name,type) PL_ ## name = slot->name;
517 # include "state.h" 530 # include "state.h"
518 #undef VAR 531 #undef VAR
519 532
605 c->mainstack = PL_mainstack; 618 c->mainstack = PL_mainstack;
606 619
607 { 620 {
608 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 621 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
609 622
610 slot->defav = GvAV (PL_defgv); 623 slot->defav = GvAV (PL_defgv);
611 slot->defsv = DEFSV; 624 slot->defsv = DEFSV;
612 slot->errsv = ERRSV; 625 slot->errsv = ERRSV;
613 slot->irsgv = GvSV (irsgv); 626 slot->irsgv = GvSV (irsgv);
627 slot->hinthv = GvHV (PL_hintgv);
614 628
615 #define VAR(name,type) slot->name = PL_ ## name; 629 #define VAR(name,type) slot->name = PL_ ## name;
616 # include "state.h" 630 # include "state.h"
617 #undef VAR 631 #undef VAR
618 } 632 }
834slf_check_nop (pTHX_ struct CoroSLF *frame) 848slf_check_nop (pTHX_ struct CoroSLF *frame)
835{ 849{
836 return 0; 850 return 0;
837} 851}
838 852
853static int
854slf_check_repeat (pTHX_ struct CoroSLF *frame)
855{
856 return 1;
857}
858
839static UNOP coro_setup_op; 859static UNOP coro_setup_op;
840 860
841static void NOINLINE /* noinline to keep it out of the transfer fast path */ 861static void NOINLINE /* noinline to keep it out of the transfer fast path */
842coro_setup (pTHX_ struct coro *coro) 862coro_setup (pTHX_ struct coro *coro)
843{ 863{
848 868
849 PL_runops = RUNOPS_DEFAULT; 869 PL_runops = RUNOPS_DEFAULT;
850 PL_curcop = &PL_compiling; 870 PL_curcop = &PL_compiling;
851 PL_in_eval = EVAL_NULL; 871 PL_in_eval = EVAL_NULL;
852 PL_comppad = 0; 872 PL_comppad = 0;
873 PL_comppad_name = 0;
874 PL_comppad_name_fill = 0;
875 PL_comppad_name_floor = 0;
853 PL_curpm = 0; 876 PL_curpm = 0;
854 PL_curpad = 0; 877 PL_curpad = 0;
855 PL_localizing = 0; 878 PL_localizing = 0;
856 PL_dirty = 0; 879 PL_dirty = 0;
857 PL_restartop = 0; 880 PL_restartop = 0;
858#if PERL_VERSION_ATLEAST (5,10,0) 881#if PERL_VERSION_ATLEAST (5,10,0)
859 PL_parser = 0; 882 PL_parser = 0;
860#endif 883#endif
884 PL_hints = 0;
861 885
862 /* recreate the die/warn hooks */ 886 /* recreate the die/warn hooks */
863 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 );
864 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);
865 889
866 GvSV (PL_defgv) = newSV (0); 890 GvSV (PL_defgv) = newSV (0);
867 GvAV (PL_defgv) = coro->args; coro->args = 0; 891 GvAV (PL_defgv) = coro->args; coro->args = 0;
868 GvSV (PL_errgv) = newSV (0); 892 GvSV (PL_errgv) = newSV (0);
869 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;
870 PL_rs = newSVsv (GvSV (irsgv)); 895 PL_rs = newSVsv (GvSV (irsgv));
871 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 896 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
872 897
873 { 898 {
874 dSP; 899 dSP;
892 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 */
893 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 */
894 919
895 /* 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 */
896 coro_setup_op.op_next = PL_op; 921 coro_setup_op.op_next = PL_op;
897 coro_setup_op.op_type = OP_CUSTOM; 922 coro_setup_op.op_type = OP_ENTERSUB;
898 coro_setup_op.op_ppaddr = pp_slf; 923 coro_setup_op.op_ppaddr = pp_slf;
899 /* 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 */
900 925
901 PL_op = (OP *)&coro_setup_op; 926 PL_op = (OP *)&coro_setup_op;
902 927
928 SvREFCNT_dec (GvAV (PL_defgv)); 953 SvREFCNT_dec (GvAV (PL_defgv));
929 SvREFCNT_dec (GvSV (PL_errgv)); 954 SvREFCNT_dec (GvSV (PL_errgv));
930 SvREFCNT_dec (PL_defoutgv); 955 SvREFCNT_dec (PL_defoutgv);
931 SvREFCNT_dec (PL_rs); 956 SvREFCNT_dec (PL_rs);
932 SvREFCNT_dec (GvSV (irsgv)); 957 SvREFCNT_dec (GvSV (irsgv));
958 SvREFCNT_dec (GvHV (PL_hintgv));
933 959
934 SvREFCNT_dec (PL_diehook); 960 SvREFCNT_dec (PL_diehook);
935 SvREFCNT_dec (PL_warnhook); 961 SvREFCNT_dec (PL_warnhook);
936 962
937 SvREFCNT_dec (coro->saved_deffh); 963 SvREFCNT_dec (coro->saved_deffh);
955static int 981static int
956runops_trace (pTHX) 982runops_trace (pTHX)
957{ 983{
958 COP *oldcop = 0; 984 COP *oldcop = 0;
959 int oldcxix = -2; 985 int oldcxix = -2;
960 struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */
961 coro_cctx *cctx = coro->cctx;
962 986
963 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 987 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
964 { 988 {
965 PERL_ASYNC_CHECK (); 989 PERL_ASYNC_CHECK ();
966 990
967 if (cctx->flags & CC_TRACE_ALL) 991 if (cctx_current->flags & CC_TRACE_ALL)
968 { 992 {
969 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)
970 { 994 {
971 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 995 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
972 SV **bot, **top; 996 SV **bot, **top;
973 AV *av = newAV (); /* return values */ 997 AV *av = newAV (); /* return values */
974 SV **cb; 998 SV **cb;
1011 1035
1012 if (PL_curcop != &PL_compiling) 1036 if (PL_curcop != &PL_compiling)
1013 { 1037 {
1014 SV **cb; 1038 SV **cb;
1015 1039
1016 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) 1040 if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB)
1017 { 1041 {
1018 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1042 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1019 1043
1020 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 1044 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
1021 { 1045 {
1022 runops_proc_t old_runops = PL_runops;
1023 dSP; 1046 dSP;
1024 GV *gv = CvGV (cx->blk_sub.cv); 1047 GV *gv = CvGV (cx->blk_sub.cv);
1025 SV *fullname = sv_2mortal (newSV (0)); 1048 SV *fullname = sv_2mortal (newSV (0));
1026 1049
1027 if (isGV (gv)) 1050 if (isGV (gv))
1045 } 1068 }
1046 1069
1047 oldcxix = cxstack_ix; 1070 oldcxix = cxstack_ix;
1048 } 1071 }
1049 1072
1050 if (cctx->flags & CC_TRACE_LINE) 1073 if (cctx_current->flags & CC_TRACE_LINE)
1051 { 1074 {
1052 dSP; 1075 dSP;
1053 1076
1054 PL_runops = RUNOPS_DEFAULT; 1077 PL_runops = RUNOPS_DEFAULT;
1055 ENTER; 1078 ENTER;
1074 1097
1075 TAINT_NOT; 1098 TAINT_NOT;
1076 return 0; 1099 return 0;
1077} 1100}
1078 1101
1079static struct coro_cctx *cctx_ssl_cctx;
1080static struct CoroSLF cctx_ssl_frame; 1102static struct CoroSLF cctx_ssl_frame;
1081 1103
1082static void 1104static void
1083slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) 1105slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1084{ 1106{
1085 ta->prev = (struct coro *)cctx_ssl_cctx;
1086 ta->next = 0; 1107 ta->prev = 0;
1087} 1108}
1088 1109
1089static int 1110static int
1090slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) 1111slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1091{ 1112{
1094 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 */
1095} 1116}
1096 1117
1097/* 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 */
1098static void NOINLINE 1119static void NOINLINE
1099cctx_prepare (pTHX_ coro_cctx *cctx) 1120cctx_prepare (pTHX)
1100{ 1121{
1101 PL_top_env = &PL_start_env; 1122 PL_top_env = &PL_start_env;
1102 1123
1103 if (cctx->flags & CC_TRACE) 1124 if (cctx_current->flags & CC_TRACE)
1104 PL_runops = runops_trace; 1125 PL_runops = runops_trace;
1105 1126
1106 /* 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
1107 * that can lead to creation of a new cctx */ 1128 * that can lead to creation of a new cctx */
1108 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)",
1109 slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); 1130 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1110 1131
1111 /* 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 */
1112 cctx_ssl_cctx = cctx;
1113 cctx_ssl_frame = slf_frame; 1133 cctx_ssl_frame = slf_frame;
1114 1134
1115 slf_frame.prepare = slf_prepare_set_stacklevel; 1135 slf_frame.prepare = slf_prepare_set_stacklevel;
1116 slf_frame.check = slf_check_set_stacklevel; 1136 slf_frame.check = slf_check_set_stacklevel;
1117} 1137}
1140 /* normally we would need to skip the entersub here */ 1160 /* normally we would need to skip the entersub here */
1141 /* 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 */
1142 /* PL_nop = PL_nop->op_next */ 1162 /* PL_nop = PL_nop->op_next */
1143 1163
1144 /* inject a fake subroutine call to cctx_init */ 1164 /* inject a fake subroutine call to cctx_init */
1145 cctx_prepare (aTHX_ (coro_cctx *)arg); 1165 cctx_prepare (aTHX);
1146 1166
1147 /* cctx_run is the alternative tail of transfer() */ 1167 /* cctx_run is the alternative tail of transfer() */
1148 transfer_tail (aTHX); 1168 transfer_tail (aTHX);
1149 1169
1150 /* 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 */
1158 /* 1178 /*
1159 * If perl-run returns we assume exit() was being called or the coro 1179 * 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) 1180 * 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 1181 * 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" 1182 * bootstrap-time "top" top_env, as we cannot restore the "main"
1163 * coroutine as Coro has no such concept 1183 * coroutine as Coro has no such concept.
1184 * This actually isn't valid with the pthread backend, but OSes requiring
1185 * that backend are too broken to do it in a standards-compliant way.
1164 */ 1186 */
1165 PL_top_env = main_top_env; 1187 PL_top_env = main_top_env;
1166 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1188 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1167 } 1189 }
1168} 1190}
1245cctx_destroy (coro_cctx *cctx) 1267cctx_destroy (coro_cctx *cctx)
1246{ 1268{
1247 if (!cctx) 1269 if (!cctx)
1248 return; 1270 return;
1249 1271
1272 assert (cctx != cctx_current);//D temporary
1273
1250 --cctx_count; 1274 --cctx_count;
1251 coro_destroy (&cctx->cctx); 1275 coro_destroy (&cctx->cctx);
1252 1276
1253 /* coro_transfer creates new, empty cctx's */ 1277 /* coro_transfer creates new, empty cctx's */
1254 if (cctx->sptr) 1278 if (cctx->sptr)
1317 /* TODO: throwing up here is considered harmful */ 1341 /* TODO: throwing up here is considered harmful */
1318 1342
1319 if (expect_true (prev != next)) 1343 if (expect_true (prev != next))
1320 { 1344 {
1321 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1345 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,"); 1346 croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,");
1323 1347
1324 if (expect_false (next->flags & CF_RUNNING)) 1348 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,"); 1349 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1326 1350
1327 if (expect_false (next->flags & CF_DESTROYED)) 1351 if (expect_false (next->flags & CF_DESTROYED))
1339transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1363transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1340{ 1364{
1341 dSTACKLEVEL; 1365 dSTACKLEVEL;
1342 1366
1343 /* sometimes transfer is only called to set idle_sp */ 1367 /* sometimes transfer is only called to set idle_sp */
1344 if (expect_false (!next)) 1368 if (expect_false (!prev))
1345 { 1369 {
1346 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1370 cctx_current->idle_sp = STACKLEVEL;
1347 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1371 assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1348 } 1372 }
1349 else if (expect_true (prev != next)) 1373 else if (expect_true (prev != next))
1350 { 1374 {
1351 coro_cctx *prev__cctx; 1375 coro_cctx *cctx_prev;
1352 1376
1353 if (expect_false (prev->flags & CF_NEW)) 1377 if (expect_false (prev->flags & CF_NEW))
1354 { 1378 {
1355 /* create a new empty/source context */ 1379 /* create a new empty/source context */
1356 prev->cctx = cctx_new_empty ();
1357 prev->flags &= ~CF_NEW; 1380 prev->flags &= ~CF_NEW;
1358 prev->flags |= CF_RUNNING; 1381 prev->flags |= CF_RUNNING;
1359 } 1382 }
1360 1383
1361 prev->flags &= ~CF_RUNNING; 1384 prev->flags &= ~CF_RUNNING;
1372 coro_setup (aTHX_ next); 1395 coro_setup (aTHX_ next);
1373 } 1396 }
1374 else 1397 else
1375 load_perl (aTHX_ next); 1398 load_perl (aTHX_ next);
1376 1399
1377 prev__cctx = prev->cctx; 1400 assert (!prev->cctx);//D temporary
1378 1401
1379 /* possibly untie and reuse the cctx */ 1402 /* possibly untie and reuse the cctx */
1380 if (expect_true ( 1403 if (expect_true (
1381 prev__cctx->idle_sp == STACKLEVEL 1404 cctx_current->idle_sp == STACKLEVEL
1382 && !(prev__cctx->flags & CC_TRACE) 1405 && !(cctx_current->flags & CC_TRACE)
1383 && !force_cctx 1406 && !force_cctx
1384 )) 1407 ))
1385 { 1408 {
1386 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1409 /* 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)); 1410 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te));
1388 1411
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 */ 1412 /* 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 */ 1413 /* without this the next cctx_get might destroy the running cctx while still in use */
1393 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1414 if (expect_false (CCTX_EXPIRED (cctx_current)))
1394 if (!next->cctx) 1415 if (expect_true (!next->cctx))
1395 next->cctx = cctx_get (aTHX); 1416 next->cctx = cctx_get (aTHX);
1396 1417
1397 cctx_put (prev__cctx); 1418 cctx_put (cctx_current);
1398 } 1419 }
1420 else
1421 prev->cctx = cctx_current;
1399 1422
1400 ++next->usecount; 1423 ++next->usecount;
1401 1424
1402 if (expect_true (!next->cctx)) 1425 cctx_prev = cctx_current;
1403 next->cctx = cctx_get (aTHX); 1426 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1404 1427
1405 if (expect_false (prev__cctx != next->cctx)) 1428 next->cctx = 0;
1429
1430 if (expect_false (cctx_prev != cctx_current))
1406 { 1431 {
1407 prev__cctx->top_env = PL_top_env; 1432 cctx_prev->top_env = PL_top_env;
1408 PL_top_env = next->cctx->top_env; 1433 PL_top_env = cctx_current->top_env;
1409 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1434 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1410 } 1435 }
1411 1436
1412 transfer_tail (aTHX); 1437 transfer_tail (aTHX);
1413 } 1438 }
1414} 1439}
1569{ 1594{
1570 struct coro *coro; 1595 struct coro *coro;
1571 SV *sv_hook; 1596 SV *sv_hook;
1572 void (*xs_hook)(void); 1597 void (*xs_hook)(void);
1573 1598
1574 if (SvROK (coro_sv))
1575 coro_sv = SvRV (coro_sv);
1576
1577 coro = SvSTATE (coro_sv); 1599 coro = SvSTATE (coro_sv);
1578 1600
1579 if (coro->flags & CF_READY) 1601 if (coro->flags & CF_READY)
1580 return 0; 1602 return 0;
1581 1603
1648 else 1670 else
1649 { 1671 {
1650 next->flags &= ~CF_READY; 1672 next->flags &= ~CF_READY;
1651 --coro_nready; 1673 --coro_nready;
1652 1674
1653 return prepare_schedule_to (aTHX_ ta, next); 1675 prepare_schedule_to (aTHX_ ta, next);
1676 break;
1654 } 1677 }
1655 } 1678 }
1656 else 1679 else
1657 { 1680 {
1658 /* nothing to schedule: call the idle handler */ 1681 /* nothing to schedule: call the idle handler */
1682 if (SvROK (sv_idle)
1683 && SvOBJECT (SvRV (sv_idle)))
1684 {
1685 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1686 api_ready (aTHX_ SvRV (sv_idle));
1687 --coro_nready;
1688 }
1689 else
1690 {
1659 dSP; 1691 dSP;
1660 1692
1661 ENTER; 1693 ENTER;
1662 SAVETMPS; 1694 SAVETMPS;
1663 1695
1664 PUSHMARK (SP); 1696 PUSHMARK (SP);
1665 PUTBACK; 1697 PUTBACK;
1666 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); 1698 call_sv (sv_idle, G_VOID | G_DISCARD);
1667 1699
1668 FREETMPS; 1700 FREETMPS;
1669 LEAVE; 1701 LEAVE;
1702 }
1670 } 1703 }
1671 } 1704 }
1672} 1705}
1673 1706
1674INLINE void 1707INLINE void
1745static void 1778static void
1746api_trace (pTHX_ SV *coro_sv, int flags) 1779api_trace (pTHX_ SV *coro_sv, int flags)
1747{ 1780{
1748 struct coro *coro = SvSTATE (coro_sv); 1781 struct coro *coro = SvSTATE (coro_sv);
1749 1782
1783 if (coro->flags & CF_RUNNING)
1784 croak ("cannot enable tracing on a running coroutine, caught");
1785
1750 if (flags & CC_TRACE) 1786 if (flags & CC_TRACE)
1751 { 1787 {
1752 if (!coro->cctx) 1788 if (!coro->cctx)
1753 coro->cctx = cctx_new_run (); 1789 coro->cctx = cctx_new_run ();
1754 else if (!(coro->cctx->flags & CC_TRACE)) 1790 else if (!(coro->cctx->flags & CC_TRACE))
1755 croak ("cannot enable tracing on coroutine with custom stack,"); 1791 croak ("cannot enable tracing on coroutine with custom stack, caught");
1756 1792
1757 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1793 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1758 } 1794 }
1759 else if (coro->cctx && coro->cctx->flags & CC_TRACE) 1795 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1760 { 1796 {
1763 if (coro->flags & CF_RUNNING) 1799 if (coro->flags & CF_RUNNING)
1764 PL_runops = RUNOPS_DEFAULT; 1800 PL_runops = RUNOPS_DEFAULT;
1765 else 1801 else
1766 coro->slot->runops = RUNOPS_DEFAULT; 1802 coro->slot->runops = RUNOPS_DEFAULT;
1767 } 1803 }
1804}
1805
1806static void
1807coro_call_on_destroy (pTHX_ struct coro *coro)
1808{
1809 SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0);
1810 SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0);
1811
1812 if (on_destroyp)
1813 {
1814 AV *on_destroy = (AV *)SvRV (*on_destroyp);
1815
1816 while (AvFILLp (on_destroy) >= 0)
1817 {
1818 dSP; /* don't disturb outer sp */
1819 SV *cb = av_pop (on_destroy);
1820
1821 PUSHMARK (SP);
1822
1823 if (statusp)
1824 {
1825 int i;
1826 AV *status = (AV *)SvRV (*statusp);
1827 EXTEND (SP, AvFILLp (status) + 1);
1828
1829 for (i = 0; i <= AvFILLp (status); ++i)
1830 PUSHs (AvARRAY (status)[i]);
1831 }
1832
1833 PUTBACK;
1834 call_sv (sv_2mortal (cb), G_VOID | G_DISCARD);
1835 }
1836 }
1837}
1838
1839static void
1840slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1841{
1842 int i;
1843 HV *hv = (HV *)SvRV (coro_current);
1844 AV *av = newAV ();
1845
1846 av_extend (av, items - 1);
1847 for (i = 0; i < items; ++i)
1848 av_push (av, SvREFCNT_inc_NN (arg [i]));
1849
1850 hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0);
1851
1852 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1853 api_ready (aTHX_ sv_manager);
1854
1855 frame->prepare = prepare_schedule;
1856 frame->check = slf_check_repeat;
1768} 1857}
1769 1858
1770/*****************************************************************************/ 1859/*****************************************************************************/
1771/* async pool handler */ 1860/* async pool handler */
1772 1861
2188 } 2277 }
2189 else 2278 else
2190 slf_argc = 0; 2279 slf_argc = 0;
2191 2280
2192 PL_op->op_ppaddr = pp_slf; 2281 PL_op->op_ppaddr = pp_slf;
2193 PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ 2282 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2194 2283
2195 PL_op = (OP *)&slf_restore; 2284 PL_op = (OP *)&slf_restore;
2196} 2285}
2197 2286
2198/*****************************************************************************/ 2287/*****************************************************************************/
2662 XSRETURN_EMPTY; 2751 XSRETURN_EMPTY;
2663} 2752}
2664 2753
2665/*****************************************************************************/ 2754/*****************************************************************************/
2666 2755
2756#if CORO_CLONE
2757# include "clone.c"
2758#endif
2759
2667MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 2760MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2668 2761
2669PROTOTYPES: DISABLE 2762PROTOTYPES: DISABLE
2670 2763
2671BOOT: 2764BOOT:
2675 coro_thx = PERL_GET_CONTEXT; 2768 coro_thx = PERL_GET_CONTEXT;
2676# endif 2769# endif
2677#endif 2770#endif
2678 BOOT_PAGESIZE; 2771 BOOT_PAGESIZE;
2679 2772
2773 cctx_current = cctx_new_empty ();
2774
2680 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 2775 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
2681 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 2776 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
2682 2777
2683 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; 2778 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
2684 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; 2779 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
2703 2798
2704 { 2799 {
2705 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); 2800 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf)));
2706 2801
2707 if (!PL_custom_op_names) PL_custom_op_names = newHV (); 2802 if (!PL_custom_op_names) PL_custom_op_names = newHV ();
2708 hv_store_ent (PL_custom_op_names, slf, 2803 hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0);
2709 newSVpv ("coro_slf", 0), 0);
2710 2804
2711 if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); 2805 if (!PL_custom_op_descs) PL_custom_op_descs = newHV ();
2712 hv_store_ent (PL_custom_op_descs, slf, 2806 hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0);
2713 newSVpv ("coro schedule like function", 0), 0);
2714 } 2807 }
2715 2808
2716 coroapi.ver = CORO_API_VERSION; 2809 coroapi.ver = CORO_API_VERSION;
2717 coroapi.rev = CORO_API_REVISION; 2810 coroapi.rev = CORO_API_REVISION;
2718 2811
2811void 2904void
2812_exit (int code) 2905_exit (int code)
2813 PROTOTYPE: $ 2906 PROTOTYPE: $
2814 CODE: 2907 CODE:
2815 _exit (code); 2908 _exit (code);
2909
2910SV *
2911clone (Coro::State coro)
2912 CODE:
2913{
2914#if CORO_CLONE
2915 struct coro *ncoro = coro_clone (coro);
2916 MAGIC *mg;
2917 /* TODO: too much duplication */
2918 ncoro->hv = newHV ();
2919 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
2920 mg->mg_flags |= MGf_DUP;
2921 RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv));
2922#else
2923 croak ("Coro::State->clone has not been configured into this installation of Coro, realised");
2924#endif
2925}
2926 OUTPUT:
2927 RETVAL
2816 2928
2817int 2929int
2818cctx_stacksize (int new_stacksize = 0) 2930cctx_stacksize (int new_stacksize = 0)
2819 PROTOTYPE: ;$ 2931 PROTOTYPE: ;$
2820 CODE: 2932 CODE:
2941 3053
2942SV * 3054SV *
2943has_cctx (Coro::State coro) 3055has_cctx (Coro::State coro)
2944 PROTOTYPE: $ 3056 PROTOTYPE: $
2945 CODE: 3057 CODE:
2946 RETVAL = boolSV (!!coro->cctx); 3058 /* maybe manage the running flag differently */
3059 RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING));
2947 OUTPUT: 3060 OUTPUT:
2948 RETVAL 3061 RETVAL
2949 3062
2950int 3063int
2951is_traced (Coro::State coro) 3064is_traced (Coro::State coro)
2971 3084
2972void 3085void
2973force_cctx () 3086force_cctx ()
2974 PROTOTYPE: 3087 PROTOTYPE:
2975 CODE: 3088 CODE:
2976 SvSTATE_current->cctx->idle_sp = 0; 3089 cctx_current->idle_sp = 0;
2977 3090
2978void 3091void
2979swap_defsv (Coro::State self) 3092swap_defsv (Coro::State self)
2980 PROTOTYPE: $ 3093 PROTOTYPE: $
2981 ALIAS: 3094 ALIAS:
2996 3109
2997BOOT: 3110BOOT:
2998{ 3111{
2999 int i; 3112 int i;
3000 3113
3001 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3002 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3114 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3003 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3115 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3004 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3116 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD);
3005 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3117 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3006 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3118 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
3119 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3120 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3121 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3122 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3007 3123
3008 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3124 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
3009 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3125 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3010 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); 3126 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3011 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); 3127 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);
3039 SvREADONLY_on (sv); 3155 SvREADONLY_on (sv);
3040 } 3156 }
3041} 3157}
3042 3158
3043void 3159void
3160terminate (...)
3161 CODE:
3162 CORO_EXECUTE_SLF_XS (slf_init_terminate);
3163
3164void
3044schedule (...) 3165schedule (...)
3045 CODE: 3166 CODE:
3046 CORO_EXECUTE_SLF_XS (slf_init_schedule); 3167 CORO_EXECUTE_SLF_XS (slf_init_schedule);
3047 3168
3048void 3169void
3062 3183
3063void 3184void
3064cede_notself (...) 3185cede_notself (...)
3065 CODE: 3186 CODE:
3066 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3187 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3188
3189void
3190_cancel (Coro::State self)
3191 CODE:
3192 coro_state_destroy (aTHX_ self);
3193 coro_call_on_destroy (aTHX_ self);
3067 3194
3068void 3195void
3069_set_current (SV *current) 3196_set_current (SV *current)
3070 PROTOTYPE: $ 3197 PROTOTYPE: $
3071 CODE: 3198 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines