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.319 by root, Thu Nov 20 07:25:01 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
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
175static AV *av_destroy; /* destruction queue */ 178static AV *av_destroy; /* destruction queue */
176static SV *sv_manager; /* the manager coro */ 179static SV *sv_manager; /* the manager coro */
180static SV *sv_idle; /* $Coro::idle */
177 181
178static GV *irsgv; /* $/ */ 182static GV *irsgv; /* $/ */
179static GV *stdoutgv; /* *STDOUT */ 183static GV *stdoutgv; /* *STDOUT */
180static SV *rv_diehook; 184static SV *rv_diehook;
181static SV *rv_warnhook; 185static SV *rv_warnhook;
225 int valgrind_id; 229 int valgrind_id;
226#endif 230#endif
227 unsigned char flags; 231 unsigned char flags;
228} coro_cctx; 232} coro_cctx;
229 233
234coro_cctx *cctx_current; /* the currently running cctx */
235
236/*****************************************************************************/
237
230enum { 238enum {
231 CF_RUNNING = 0x0001, /* coroutine is running */ 239 CF_RUNNING = 0x0001, /* coroutine is running */
232 CF_READY = 0x0002, /* coroutine is ready */ 240 CF_READY = 0x0002, /* coroutine is ready */
233 CF_NEW = 0x0004, /* has never been switched to */ 241 CF_NEW = 0x0004, /* has never been switched to */
234 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 242 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
239{ 247{
240 SV *defsv; 248 SV *defsv;
241 AV *defav; 249 AV *defav;
242 SV *errsv; 250 SV *errsv;
243 SV *irsgv; 251 SV *irsgv;
252 HV *hinthv;
244#define VAR(name,type) type name; 253#define VAR(name,type) type name;
245# include "state.h" 254# include "state.h"
246#undef VAR 255#undef VAR
247} perl_slots; 256} perl_slots;
248 257
347 GV *gvp; 356 GV *gvp;
348 return sv_2cv (sv, &st, &gvp, 0); 357 return sv_2cv (sv, &st, &gvp, 0);
349} 358}
350 359
351static AV * 360static AV *
352coro_clone_padlist (pTHX_ CV *cv) 361coro_derive_padlist (pTHX_ CV *cv)
353{ 362{
354 AV *padlist = CvPADLIST (cv); 363 AV *padlist = CvPADLIST (cv);
355 AV *newpadlist, *newpad; 364 AV *newpadlist, *newpad;
356 365
357 newpadlist = newAV (); 366 newpadlist = newAV ();
477 CV *cp = Perl_cv_clone (aTHX_ cv); 486 CV *cp = Perl_cv_clone (aTHX_ cv);
478 CvPADLIST (cv) = CvPADLIST (cp); 487 CvPADLIST (cv) = CvPADLIST (cp);
479 CvPADLIST (cp) = 0; 488 CvPADLIST (cp) = 0;
480 SvREFCNT_dec (cp); 489 SvREFCNT_dec (cp);
481#else 490#else
482 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 491 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv);
483#endif 492#endif
484 } 493 }
485} 494}
486 495
487static void 496static void
509 perl_slots *slot = c->slot; 518 perl_slots *slot = c->slot;
510 c->slot = 0; 519 c->slot = 0;
511 520
512 PL_mainstack = c->mainstack; 521 PL_mainstack = c->mainstack;
513 522
514 GvSV (PL_defgv) = slot->defsv; 523 GvSV (PL_defgv) = slot->defsv;
515 GvAV (PL_defgv) = slot->defav; 524 GvAV (PL_defgv) = slot->defav;
516 GvSV (PL_errgv) = slot->errsv; 525 GvSV (PL_errgv) = slot->errsv;
517 GvSV (irsgv) = slot->irsgv; 526 GvSV (irsgv) = slot->irsgv;
527 GvHV (PL_hintgv) = slot->hinthv;
518 528
519 #define VAR(name,type) PL_ ## name = slot->name; 529 #define VAR(name,type) PL_ ## name = slot->name;
520 # include "state.h" 530 # include "state.h"
521 #undef VAR 531 #undef VAR
522 532
608 c->mainstack = PL_mainstack; 618 c->mainstack = PL_mainstack;
609 619
610 { 620 {
611 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 621 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
612 622
613 slot->defav = GvAV (PL_defgv); 623 slot->defav = GvAV (PL_defgv);
614 slot->defsv = DEFSV; 624 slot->defsv = DEFSV;
615 slot->errsv = ERRSV; 625 slot->errsv = ERRSV;
616 slot->irsgv = GvSV (irsgv); 626 slot->irsgv = GvSV (irsgv);
627 slot->hinthv = GvHV (PL_hintgv);
617 628
618 #define VAR(name,type) slot->name = PL_ ## name; 629 #define VAR(name,type) slot->name = PL_ ## name;
619 # include "state.h" 630 # include "state.h"
620 #undef VAR 631 #undef VAR
621 } 632 }
857 868
858 PL_runops = RUNOPS_DEFAULT; 869 PL_runops = RUNOPS_DEFAULT;
859 PL_curcop = &PL_compiling; 870 PL_curcop = &PL_compiling;
860 PL_in_eval = EVAL_NULL; 871 PL_in_eval = EVAL_NULL;
861 PL_comppad = 0; 872 PL_comppad = 0;
873 PL_comppad_name = 0;
874 PL_comppad_name_fill = 0;
875 PL_comppad_name_floor = 0;
862 PL_curpm = 0; 876 PL_curpm = 0;
863 PL_curpad = 0; 877 PL_curpad = 0;
864 PL_localizing = 0; 878 PL_localizing = 0;
865 PL_dirty = 0; 879 PL_dirty = 0;
866 PL_restartop = 0; 880 PL_restartop = 0;
867#if PERL_VERSION_ATLEAST (5,10,0) 881#if PERL_VERSION_ATLEAST (5,10,0)
868 PL_parser = 0; 882 PL_parser = 0;
869#endif 883#endif
884 PL_hints = 0;
870 885
871 /* recreate the die/warn hooks */ 886 /* recreate the die/warn hooks */
872 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 );
873 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);
874 889
875 GvSV (PL_defgv) = newSV (0); 890 GvSV (PL_defgv) = newSV (0);
876 GvAV (PL_defgv) = coro->args; coro->args = 0; 891 GvAV (PL_defgv) = coro->args; coro->args = 0;
877 GvSV (PL_errgv) = newSV (0); 892 GvSV (PL_errgv) = newSV (0);
878 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;
879 PL_rs = newSVsv (GvSV (irsgv)); 895 PL_rs = newSVsv (GvSV (irsgv));
880 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 896 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
881 897
882 { 898 {
883 dSP; 899 dSP;
901 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 */
902 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 */
903 919
904 /* 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 */
905 coro_setup_op.op_next = PL_op; 921 coro_setup_op.op_next = PL_op;
906 coro_setup_op.op_type = OP_CUSTOM; 922 coro_setup_op.op_type = OP_ENTERSUB;
907 coro_setup_op.op_ppaddr = pp_slf; 923 coro_setup_op.op_ppaddr = pp_slf;
908 /* 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 */
909 925
910 PL_op = (OP *)&coro_setup_op; 926 PL_op = (OP *)&coro_setup_op;
911 927
937 SvREFCNT_dec (GvAV (PL_defgv)); 953 SvREFCNT_dec (GvAV (PL_defgv));
938 SvREFCNT_dec (GvSV (PL_errgv)); 954 SvREFCNT_dec (GvSV (PL_errgv));
939 SvREFCNT_dec (PL_defoutgv); 955 SvREFCNT_dec (PL_defoutgv);
940 SvREFCNT_dec (PL_rs); 956 SvREFCNT_dec (PL_rs);
941 SvREFCNT_dec (GvSV (irsgv)); 957 SvREFCNT_dec (GvSV (irsgv));
958 SvREFCNT_dec (GvHV (PL_hintgv));
942 959
943 SvREFCNT_dec (PL_diehook); 960 SvREFCNT_dec (PL_diehook);
944 SvREFCNT_dec (PL_warnhook); 961 SvREFCNT_dec (PL_warnhook);
945 962
946 SvREFCNT_dec (coro->saved_deffh); 963 SvREFCNT_dec (coro->saved_deffh);
964static int 981static int
965runops_trace (pTHX) 982runops_trace (pTHX)
966{ 983{
967 COP *oldcop = 0; 984 COP *oldcop = 0;
968 int oldcxix = -2; 985 int oldcxix = -2;
969 struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */
970 coro_cctx *cctx = coro->cctx;
971 986
972 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 987 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
973 { 988 {
974 PERL_ASYNC_CHECK (); 989 PERL_ASYNC_CHECK ();
975 990
976 if (cctx->flags & CC_TRACE_ALL) 991 if (cctx_current->flags & CC_TRACE_ALL)
977 { 992 {
978 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)
979 { 994 {
980 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 995 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
981 SV **bot, **top; 996 SV **bot, **top;
982 AV *av = newAV (); /* return values */ 997 AV *av = newAV (); /* return values */
983 SV **cb; 998 SV **cb;
1020 1035
1021 if (PL_curcop != &PL_compiling) 1036 if (PL_curcop != &PL_compiling)
1022 { 1037 {
1023 SV **cb; 1038 SV **cb;
1024 1039
1025 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) 1040 if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB)
1026 { 1041 {
1027 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1042 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1028 1043
1029 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 1044 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
1030 { 1045 {
1053 } 1068 }
1054 1069
1055 oldcxix = cxstack_ix; 1070 oldcxix = cxstack_ix;
1056 } 1071 }
1057 1072
1058 if (cctx->flags & CC_TRACE_LINE) 1073 if (cctx_current->flags & CC_TRACE_LINE)
1059 { 1074 {
1060 dSP; 1075 dSP;
1061 1076
1062 PL_runops = RUNOPS_DEFAULT; 1077 PL_runops = RUNOPS_DEFAULT;
1063 ENTER; 1078 ENTER;
1082 1097
1083 TAINT_NOT; 1098 TAINT_NOT;
1084 return 0; 1099 return 0;
1085} 1100}
1086 1101
1087static struct coro_cctx *cctx_ssl_cctx;
1088static struct CoroSLF cctx_ssl_frame; 1102static struct CoroSLF cctx_ssl_frame;
1089 1103
1090static void 1104static void
1091slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) 1105slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1092{ 1106{
1093 ta->prev = (struct coro *)cctx_ssl_cctx;
1094 ta->next = 0; 1107 ta->prev = 0;
1095} 1108}
1096 1109
1097static int 1110static int
1098slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) 1111slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1099{ 1112{
1102 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 */
1103} 1116}
1104 1117
1105/* 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 */
1106static void NOINLINE 1119static void NOINLINE
1107cctx_prepare (pTHX_ coro_cctx *cctx) 1120cctx_prepare (pTHX)
1108{ 1121{
1109 PL_top_env = &PL_start_env; 1122 PL_top_env = &PL_start_env;
1110 1123
1111 if (cctx->flags & CC_TRACE) 1124 if (cctx_current->flags & CC_TRACE)
1112 PL_runops = runops_trace; 1125 PL_runops = runops_trace;
1113 1126
1114 /* 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
1115 * that can lead to creation of a new cctx */ 1128 * that can lead to creation of a new cctx */
1116 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)",
1117 slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); 1130 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1118 1131
1119 /* 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 */
1120 cctx_ssl_cctx = cctx;
1121 cctx_ssl_frame = slf_frame; 1133 cctx_ssl_frame = slf_frame;
1122 1134
1123 slf_frame.prepare = slf_prepare_set_stacklevel; 1135 slf_frame.prepare = slf_prepare_set_stacklevel;
1124 slf_frame.check = slf_check_set_stacklevel; 1136 slf_frame.check = slf_check_set_stacklevel;
1125} 1137}
1148 /* normally we would need to skip the entersub here */ 1160 /* normally we would need to skip the entersub here */
1149 /* 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 */
1150 /* PL_nop = PL_nop->op_next */ 1162 /* PL_nop = PL_nop->op_next */
1151 1163
1152 /* inject a fake subroutine call to cctx_init */ 1164 /* inject a fake subroutine call to cctx_init */
1153 cctx_prepare (aTHX_ (coro_cctx *)arg); 1165 cctx_prepare (aTHX);
1154 1166
1155 /* cctx_run is the alternative tail of transfer() */ 1167 /* cctx_run is the alternative tail of transfer() */
1156 transfer_tail (aTHX); 1168 transfer_tail (aTHX);
1157 1169
1158 /* 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 */
1166 /* 1178 /*
1167 * 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
1168 * 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)
1169 * 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
1170 * bootstrap-time "top" top_env, as we cannot restore the "main" 1182 * bootstrap-time "top" top_env, as we cannot restore the "main"
1171 * 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.
1172 */ 1186 */
1173 PL_top_env = main_top_env; 1187 PL_top_env = main_top_env;
1174 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 */
1175 } 1189 }
1176} 1190}
1253cctx_destroy (coro_cctx *cctx) 1267cctx_destroy (coro_cctx *cctx)
1254{ 1268{
1255 if (!cctx) 1269 if (!cctx)
1256 return; 1270 return;
1257 1271
1272 assert (cctx != cctx_current);//D temporary
1273
1258 --cctx_count; 1274 --cctx_count;
1259 coro_destroy (&cctx->cctx); 1275 coro_destroy (&cctx->cctx);
1260 1276
1261 /* coro_transfer creates new, empty cctx's */ 1277 /* coro_transfer creates new, empty cctx's */
1262 if (cctx->sptr) 1278 if (cctx->sptr)
1325 /* TODO: throwing up here is considered harmful */ 1341 /* TODO: throwing up here is considered harmful */
1326 1342
1327 if (expect_true (prev != next)) 1343 if (expect_true (prev != next))
1328 { 1344 {
1329 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1345 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1330 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,");
1331 1347
1332 if (expect_false (next->flags & CF_RUNNING)) 1348 if (expect_false (next->flags & CF_RUNNING))
1333 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,");
1334 1350
1335 if (expect_false (next->flags & CF_DESTROYED)) 1351 if (expect_false (next->flags & CF_DESTROYED))
1347transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1363transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1348{ 1364{
1349 dSTACKLEVEL; 1365 dSTACKLEVEL;
1350 1366
1351 /* sometimes transfer is only called to set idle_sp */ 1367 /* sometimes transfer is only called to set idle_sp */
1352 if (expect_false (!next)) 1368 if (expect_false (!prev))
1353 { 1369 {
1354 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1370 cctx_current->idle_sp = STACKLEVEL;
1355 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 */
1356 } 1372 }
1357 else if (expect_true (prev != next)) 1373 else if (expect_true (prev != next))
1358 { 1374 {
1359 coro_cctx *prev__cctx; 1375 coro_cctx *cctx_prev;
1360 1376
1361 if (expect_false (prev->flags & CF_NEW)) 1377 if (expect_false (prev->flags & CF_NEW))
1362 { 1378 {
1363 /* create a new empty/source context */ 1379 /* create a new empty/source context */
1364 prev->cctx = cctx_new_empty ();
1365 prev->flags &= ~CF_NEW; 1380 prev->flags &= ~CF_NEW;
1366 prev->flags |= CF_RUNNING; 1381 prev->flags |= CF_RUNNING;
1367 } 1382 }
1368 1383
1369 prev->flags &= ~CF_RUNNING; 1384 prev->flags &= ~CF_RUNNING;
1380 coro_setup (aTHX_ next); 1395 coro_setup (aTHX_ next);
1381 } 1396 }
1382 else 1397 else
1383 load_perl (aTHX_ next); 1398 load_perl (aTHX_ next);
1384 1399
1385 prev__cctx = prev->cctx; 1400 assert (!prev->cctx);//D temporary
1386 1401
1387 /* possibly untie and reuse the cctx */ 1402 /* possibly untie and reuse the cctx */
1388 if (expect_true ( 1403 if (expect_true (
1389 prev__cctx->idle_sp == STACKLEVEL 1404 cctx_current->idle_sp == STACKLEVEL
1390 && !(prev__cctx->flags & CC_TRACE) 1405 && !(cctx_current->flags & CC_TRACE)
1391 && !force_cctx 1406 && !force_cctx
1392 )) 1407 ))
1393 { 1408 {
1394 /* 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 */
1395 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));
1396 1411
1397 prev->cctx = 0;
1398
1399 /* 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. */
1400 /* 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 */
1401 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1414 if (expect_false (CCTX_EXPIRED (cctx_current)))
1402 if (!next->cctx) 1415 if (expect_true (!next->cctx))
1403 next->cctx = cctx_get (aTHX); 1416 next->cctx = cctx_get (aTHX);
1404 1417
1405 cctx_put (prev__cctx); 1418 cctx_put (cctx_current);
1406 } 1419 }
1420 else
1421 prev->cctx = cctx_current;
1407 1422
1408 ++next->usecount; 1423 ++next->usecount;
1409 1424
1410 if (expect_true (!next->cctx)) 1425 cctx_prev = cctx_current;
1411 next->cctx = cctx_get (aTHX); 1426 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1412 1427
1413 if (expect_false (prev__cctx != next->cctx)) 1428 next->cctx = 0;
1429
1430 if (expect_false (cctx_prev != cctx_current))
1414 { 1431 {
1415 prev__cctx->top_env = PL_top_env; 1432 cctx_prev->top_env = PL_top_env;
1416 PL_top_env = next->cctx->top_env; 1433 PL_top_env = cctx_current->top_env;
1417 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1434 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1418 } 1435 }
1419 1436
1420 transfer_tail (aTHX); 1437 transfer_tail (aTHX);
1421 } 1438 }
1422} 1439}
1577{ 1594{
1578 struct coro *coro; 1595 struct coro *coro;
1579 SV *sv_hook; 1596 SV *sv_hook;
1580 void (*xs_hook)(void); 1597 void (*xs_hook)(void);
1581 1598
1582 if (SvROK (coro_sv))
1583 coro_sv = SvRV (coro_sv);
1584
1585 coro = SvSTATE (coro_sv); 1599 coro = SvSTATE (coro_sv);
1586 1600
1587 if (coro->flags & CF_READY) 1601 if (coro->flags & CF_READY)
1588 return 0; 1602 return 0;
1589 1603
1663 } 1677 }
1664 } 1678 }
1665 else 1679 else
1666 { 1680 {
1667 /* 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 {
1668 dSP; 1691 dSP;
1669 1692
1670 ENTER; 1693 ENTER;
1671 SAVETMPS; 1694 SAVETMPS;
1672 1695
1673 PUSHMARK (SP); 1696 PUSHMARK (SP);
1674 PUTBACK; 1697 PUTBACK;
1675 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); 1698 call_sv (sv_idle, G_VOID | G_DISCARD);
1676 1699
1677 FREETMPS; 1700 FREETMPS;
1678 LEAVE; 1701 LEAVE;
1702 }
1679 } 1703 }
1680 } 1704 }
1681} 1705}
1682 1706
1683INLINE void 1707INLINE void
1754static void 1778static void
1755api_trace (pTHX_ SV *coro_sv, int flags) 1779api_trace (pTHX_ SV *coro_sv, int flags)
1756{ 1780{
1757 struct coro *coro = SvSTATE (coro_sv); 1781 struct coro *coro = SvSTATE (coro_sv);
1758 1782
1783 if (coro->flags & CF_RUNNING)
1784 croak ("cannot enable tracing on a running coroutine, caught");
1785
1759 if (flags & CC_TRACE) 1786 if (flags & CC_TRACE)
1760 { 1787 {
1761 if (!coro->cctx) 1788 if (!coro->cctx)
1762 coro->cctx = cctx_new_run (); 1789 coro->cctx = cctx_new_run ();
1763 else if (!(coro->cctx->flags & CC_TRACE)) 1790 else if (!(coro->cctx->flags & CC_TRACE))
1764 croak ("cannot enable tracing on coroutine with custom stack,"); 1791 croak ("cannot enable tracing on coroutine with custom stack, caught");
1765 1792
1766 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1793 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1767 } 1794 }
1768 else if (coro->cctx && coro->cctx->flags & CC_TRACE) 1795 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1769 { 1796 {
2250 } 2277 }
2251 else 2278 else
2252 slf_argc = 0; 2279 slf_argc = 0;
2253 2280
2254 PL_op->op_ppaddr = pp_slf; 2281 PL_op->op_ppaddr = pp_slf;
2255 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 */
2256 2283
2257 PL_op = (OP *)&slf_restore; 2284 PL_op = (OP *)&slf_restore;
2258} 2285}
2259 2286
2260/*****************************************************************************/ 2287/*****************************************************************************/
2724 XSRETURN_EMPTY; 2751 XSRETURN_EMPTY;
2725} 2752}
2726 2753
2727/*****************************************************************************/ 2754/*****************************************************************************/
2728 2755
2756#if CORO_CLONE
2757# include "clone.c"
2758#endif
2759
2729MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 2760MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2730 2761
2731PROTOTYPES: DISABLE 2762PROTOTYPES: DISABLE
2732 2763
2733BOOT: 2764BOOT:
2736# if CORO_PTHREAD 2767# if CORO_PTHREAD
2737 coro_thx = PERL_GET_CONTEXT; 2768 coro_thx = PERL_GET_CONTEXT;
2738# endif 2769# endif
2739#endif 2770#endif
2740 BOOT_PAGESIZE; 2771 BOOT_PAGESIZE;
2772
2773 cctx_current = cctx_new_empty ();
2741 2774
2742 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 2775 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
2743 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 2776 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
2744 2777
2745 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;
2871void 2904void
2872_exit (int code) 2905_exit (int code)
2873 PROTOTYPE: $ 2906 PROTOTYPE: $
2874 CODE: 2907 CODE:
2875 _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
2876 2928
2877int 2929int
2878cctx_stacksize (int new_stacksize = 0) 2930cctx_stacksize (int new_stacksize = 0)
2879 PROTOTYPE: ;$ 2931 PROTOTYPE: ;$
2880 CODE: 2932 CODE:
3001 3053
3002SV * 3054SV *
3003has_cctx (Coro::State coro) 3055has_cctx (Coro::State coro)
3004 PROTOTYPE: $ 3056 PROTOTYPE: $
3005 CODE: 3057 CODE:
3006 RETVAL = boolSV (!!coro->cctx); 3058 /* maybe manage the running flag differently */
3059 RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING));
3007 OUTPUT: 3060 OUTPUT:
3008 RETVAL 3061 RETVAL
3009 3062
3010int 3063int
3011is_traced (Coro::State coro) 3064is_traced (Coro::State coro)
3031 3084
3032void 3085void
3033force_cctx () 3086force_cctx ()
3034 PROTOTYPE: 3087 PROTOTYPE:
3035 CODE: 3088 CODE:
3036 SvSTATE_current->cctx->idle_sp = 0; 3089 cctx_current->idle_sp = 0;
3037 3090
3038void 3091void
3039swap_defsv (Coro::State self) 3092swap_defsv (Coro::State self)
3040 PROTOTYPE: $ 3093 PROTOTYPE: $
3041 ALIAS: 3094 ALIAS:
3064 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3117 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3065 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);
3066 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 3119 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3067 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3120 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3068 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3121 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3122 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3069 3123
3070 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);
3071 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3125 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3072 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); 3126 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3073 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); 3127 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines