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.320 by root, Fri Nov 21 07:29:04 2008 UTC vs.
Revision 1.330 by root, Wed Nov 26 09:29:06 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 ();
377 if (SvREFCNT (padlist)) 386 if (SvREFCNT (padlist))
378 { 387 {
379 I32 i = AvFILLp (padlist); 388 I32 i = AvFILLp (padlist);
380 while (i >= 0) 389 while (i >= 0)
381 { 390 {
382 SV **svp = av_fetch (padlist, i--, FALSE); 391 SV **svp = AvARRAY (padlist)[i--];
383 if (svp) 392 if (svp)
384 { 393 {
385 SV *sv; 394 AvREAL_on (*svp);
386 while (&PL_sv_undef != (sv = av_pop ((AV *)*svp)))
387 SvREFCNT_dec (sv); 395 av_undef (*svp);
388
389 SvREFCNT_dec (*svp); 396 SvREFCNT_dec (*svp);
390 } 397 }
391 } 398 }
392 399
393 SvREFCNT_dec ((SV*)padlist); 400 SvREFCNT_dec ((SV*)padlist);
477 CV *cp = Perl_cv_clone (aTHX_ cv); 484 CV *cp = Perl_cv_clone (aTHX_ cv);
478 CvPADLIST (cv) = CvPADLIST (cp); 485 CvPADLIST (cv) = CvPADLIST (cp);
479 CvPADLIST (cp) = 0; 486 CvPADLIST (cp) = 0;
480 SvREFCNT_dec (cp); 487 SvREFCNT_dec (cp);
481#else 488#else
482 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 489 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv);
483#endif 490#endif
484 } 491 }
485} 492}
486 493
487static void 494static void
494 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); 501 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
495 502
496 av = (AV *)mg->mg_obj; 503 av = (AV *)mg->mg_obj;
497 504
498 if (expect_false (AvFILLp (av) >= AvMAX (av))) 505 if (expect_false (AvFILLp (av) >= AvMAX (av)))
499 av_extend (av, AvMAX (av) + 1); 506 av_extend (av, AvFILLp (av) + 1);
500 507
501 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 508 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
502} 509}
503 510
504/** load & save, init *******************************************************/ 511/** load & save, init *******************************************************/
509 perl_slots *slot = c->slot; 516 perl_slots *slot = c->slot;
510 c->slot = 0; 517 c->slot = 0;
511 518
512 PL_mainstack = c->mainstack; 519 PL_mainstack = c->mainstack;
513 520
514 GvSV (PL_defgv) = slot->defsv; 521 GvSV (PL_defgv) = slot->defsv;
515 GvAV (PL_defgv) = slot->defav; 522 GvAV (PL_defgv) = slot->defav;
516 GvSV (PL_errgv) = slot->errsv; 523 GvSV (PL_errgv) = slot->errsv;
517 GvSV (irsgv) = slot->irsgv; 524 GvSV (irsgv) = slot->irsgv;
525 GvHV (PL_hintgv) = slot->hinthv;
518 526
519 #define VAR(name,type) PL_ ## name = slot->name; 527 #define VAR(name,type) PL_ ## name = slot->name;
520 # include "state.h" 528 # include "state.h"
521 #undef VAR 529 #undef VAR
522 530
608 c->mainstack = PL_mainstack; 616 c->mainstack = PL_mainstack;
609 617
610 { 618 {
611 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 619 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
612 620
613 slot->defav = GvAV (PL_defgv); 621 slot->defav = GvAV (PL_defgv);
614 slot->defsv = DEFSV; 622 slot->defsv = DEFSV;
615 slot->errsv = ERRSV; 623 slot->errsv = ERRSV;
616 slot->irsgv = GvSV (irsgv); 624 slot->irsgv = GvSV (irsgv);
625 slot->hinthv = GvHV (PL_hintgv);
617 626
618 #define VAR(name,type) slot->name = PL_ ## name; 627 #define VAR(name,type) slot->name = PL_ ## name;
619 # include "state.h" 628 # include "state.h"
620 #undef VAR 629 #undef VAR
621 } 630 }
857 866
858 PL_runops = RUNOPS_DEFAULT; 867 PL_runops = RUNOPS_DEFAULT;
859 PL_curcop = &PL_compiling; 868 PL_curcop = &PL_compiling;
860 PL_in_eval = EVAL_NULL; 869 PL_in_eval = EVAL_NULL;
861 PL_comppad = 0; 870 PL_comppad = 0;
871 PL_comppad_name = 0;
872 PL_comppad_name_fill = 0;
873 PL_comppad_name_floor = 0;
862 PL_curpm = 0; 874 PL_curpm = 0;
863 PL_curpad = 0; 875 PL_curpad = 0;
864 PL_localizing = 0; 876 PL_localizing = 0;
865 PL_dirty = 0; 877 PL_dirty = 0;
866 PL_restartop = 0; 878 PL_restartop = 0;
867#if PERL_VERSION_ATLEAST (5,10,0) 879#if PERL_VERSION_ATLEAST (5,10,0)
868 PL_parser = 0; 880 PL_parser = 0;
869#endif 881#endif
882 PL_hints = 0;
870 883
871 /* recreate the die/warn hooks */ 884 /* recreate the die/warn hooks */
872 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); 885 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); 886 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook);
874 887
875 GvSV (PL_defgv) = newSV (0); 888 GvSV (PL_defgv) = newSV (0);
876 GvAV (PL_defgv) = coro->args; coro->args = 0; 889 GvAV (PL_defgv) = coro->args; coro->args = 0;
877 GvSV (PL_errgv) = newSV (0); 890 GvSV (PL_errgv) = newSV (0);
878 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 891 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
892 GvHV (PL_hintgv) = 0;
879 PL_rs = newSVsv (GvSV (irsgv)); 893 PL_rs = newSVsv (GvSV (irsgv));
880 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 894 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
881 895
882 { 896 {
883 dSP; 897 dSP;
937 SvREFCNT_dec (GvAV (PL_defgv)); 951 SvREFCNT_dec (GvAV (PL_defgv));
938 SvREFCNT_dec (GvSV (PL_errgv)); 952 SvREFCNT_dec (GvSV (PL_errgv));
939 SvREFCNT_dec (PL_defoutgv); 953 SvREFCNT_dec (PL_defoutgv);
940 SvREFCNT_dec (PL_rs); 954 SvREFCNT_dec (PL_rs);
941 SvREFCNT_dec (GvSV (irsgv)); 955 SvREFCNT_dec (GvSV (irsgv));
956 SvREFCNT_dec (GvHV (PL_hintgv));
942 957
943 SvREFCNT_dec (PL_diehook); 958 SvREFCNT_dec (PL_diehook);
944 SvREFCNT_dec (PL_warnhook); 959 SvREFCNT_dec (PL_warnhook);
945 960
946 SvREFCNT_dec (coro->saved_deffh); 961 SvREFCNT_dec (coro->saved_deffh);
964static int 979static int
965runops_trace (pTHX) 980runops_trace (pTHX)
966{ 981{
967 COP *oldcop = 0; 982 COP *oldcop = 0;
968 int oldcxix = -2; 983 int oldcxix = -2;
969 struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */
970 coro_cctx *cctx = coro->cctx;
971 984
972 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 985 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
973 { 986 {
974 PERL_ASYNC_CHECK (); 987 PERL_ASYNC_CHECK ();
975 988
976 if (cctx->flags & CC_TRACE_ALL) 989 if (cctx_current->flags & CC_TRACE_ALL)
977 { 990 {
978 if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) 991 if (PL_op->op_type == OP_LEAVESUB && cctx_current->flags & CC_TRACE_SUB)
979 { 992 {
980 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 993 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
981 SV **bot, **top; 994 SV **bot, **top;
982 AV *av = newAV (); /* return values */ 995 AV *av = newAV (); /* return values */
983 SV **cb; 996 SV **cb;
1020 1033
1021 if (PL_curcop != &PL_compiling) 1034 if (PL_curcop != &PL_compiling)
1022 { 1035 {
1023 SV **cb; 1036 SV **cb;
1024 1037
1025 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) 1038 if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB)
1026 { 1039 {
1027 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1040 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1028 1041
1029 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 1042 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
1030 { 1043 {
1053 } 1066 }
1054 1067
1055 oldcxix = cxstack_ix; 1068 oldcxix = cxstack_ix;
1056 } 1069 }
1057 1070
1058 if (cctx->flags & CC_TRACE_LINE) 1071 if (cctx_current->flags & CC_TRACE_LINE)
1059 { 1072 {
1060 dSP; 1073 dSP;
1061 1074
1062 PL_runops = RUNOPS_DEFAULT; 1075 PL_runops = RUNOPS_DEFAULT;
1063 ENTER; 1076 ENTER;
1082 1095
1083 TAINT_NOT; 1096 TAINT_NOT;
1084 return 0; 1097 return 0;
1085} 1098}
1086 1099
1087static struct coro_cctx *cctx_ssl_cctx;
1088static struct CoroSLF cctx_ssl_frame; 1100static struct CoroSLF cctx_ssl_frame;
1089 1101
1090static void 1102static void
1091slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) 1103slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1092{ 1104{
1093 ta->prev = (struct coro *)cctx_ssl_cctx;
1094 ta->next = 0; 1105 ta->prev = 0;
1095} 1106}
1096 1107
1097static int 1108static int
1098slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) 1109slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1099{ 1110{
1102 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ 1113 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1103} 1114}
1104 1115
1105/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ 1116/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1106static void NOINLINE 1117static void NOINLINE
1107cctx_prepare (pTHX_ coro_cctx *cctx) 1118cctx_prepare (pTHX)
1108{ 1119{
1109 PL_top_env = &PL_start_env; 1120 PL_top_env = &PL_start_env;
1110 1121
1111 if (cctx->flags & CC_TRACE) 1122 if (cctx_current->flags & CC_TRACE)
1112 PL_runops = runops_trace; 1123 PL_runops = runops_trace;
1113 1124
1114 /* we already must be executing an SLF op, there is no other valid way 1125 /* we already must be executing an SLF op, there is no other valid way
1115 * that can lead to creation of a new cctx */ 1126 * that can lead to creation of a new cctx */
1116 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", 1127 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)",
1117 slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); 1128 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1118 1129
1119 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ 1130 /* 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; 1131 cctx_ssl_frame = slf_frame;
1122 1132
1123 slf_frame.prepare = slf_prepare_set_stacklevel; 1133 slf_frame.prepare = slf_prepare_set_stacklevel;
1124 slf_frame.check = slf_check_set_stacklevel; 1134 slf_frame.check = slf_check_set_stacklevel;
1125} 1135}
1148 /* normally we would need to skip the entersub here */ 1158 /* normally we would need to skip the entersub here */
1149 /* not doing so will re-execute it, which is exactly what we want */ 1159 /* not doing so will re-execute it, which is exactly what we want */
1150 /* PL_nop = PL_nop->op_next */ 1160 /* PL_nop = PL_nop->op_next */
1151 1161
1152 /* inject a fake subroutine call to cctx_init */ 1162 /* inject a fake subroutine call to cctx_init */
1153 cctx_prepare (aTHX_ (coro_cctx *)arg); 1163 cctx_prepare (aTHX);
1154 1164
1155 /* cctx_run is the alternative tail of transfer() */ 1165 /* cctx_run is the alternative tail of transfer() */
1156 transfer_tail (aTHX); 1166 transfer_tail (aTHX);
1157 1167
1158 /* somebody or something will hit me for both perl_run and PL_restartop */ 1168 /* somebody or something will hit me for both perl_run and PL_restartop */
1166 /* 1176 /*
1167 * If perl-run returns we assume exit() was being called or the coro 1177 * 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) 1178 * 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 1179 * 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" 1180 * bootstrap-time "top" top_env, as we cannot restore the "main"
1171 * coroutine as Coro has no such concept 1181 * coroutine as Coro has no such concept.
1182 * This actually isn't valid with the pthread backend, but OSes requiring
1183 * that backend are too broken to do it in a standards-compliant way.
1172 */ 1184 */
1173 PL_top_env = main_top_env; 1185 PL_top_env = main_top_env;
1174 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1186 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1175 } 1187 }
1176} 1188}
1253cctx_destroy (coro_cctx *cctx) 1265cctx_destroy (coro_cctx *cctx)
1254{ 1266{
1255 if (!cctx) 1267 if (!cctx)
1256 return; 1268 return;
1257 1269
1270 assert (cctx != cctx_current);//D temporary
1271
1258 --cctx_count; 1272 --cctx_count;
1259 coro_destroy (&cctx->cctx); 1273 coro_destroy (&cctx->cctx);
1260 1274
1261 /* coro_transfer creates new, empty cctx's */ 1275 /* coro_transfer creates new, empty cctx's */
1262 if (cctx->sptr) 1276 if (cctx->sptr)
1325 /* TODO: throwing up here is considered harmful */ 1339 /* TODO: throwing up here is considered harmful */
1326 1340
1327 if (expect_true (prev != next)) 1341 if (expect_true (prev != next))
1328 { 1342 {
1329 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1343 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,"); 1344 croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,");
1331 1345
1332 if (expect_false (next->flags & CF_RUNNING)) 1346 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,"); 1347 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1334 1348
1335 if (expect_false (next->flags & CF_DESTROYED)) 1349 if (expect_false (next->flags & CF_DESTROYED))
1347transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1361transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1348{ 1362{
1349 dSTACKLEVEL; 1363 dSTACKLEVEL;
1350 1364
1351 /* sometimes transfer is only called to set idle_sp */ 1365 /* sometimes transfer is only called to set idle_sp */
1352 if (expect_false (!next)) 1366 if (expect_false (!prev))
1353 { 1367 {
1354 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1368 cctx_current->idle_sp = STACKLEVEL;
1355 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 */
1356 } 1370 }
1357 else if (expect_true (prev != next)) 1371 else if (expect_true (prev != next))
1358 { 1372 {
1359 coro_cctx *prev__cctx; 1373 coro_cctx *cctx_prev;
1360 1374
1361 if (expect_false (prev->flags & CF_NEW)) 1375 if (expect_false (prev->flags & CF_NEW))
1362 { 1376 {
1363 /* create a new empty/source context */ 1377 /* create a new empty/source context */
1364 prev->cctx = cctx_new_empty ();
1365 prev->flags &= ~CF_NEW; 1378 prev->flags &= ~CF_NEW;
1366 prev->flags |= CF_RUNNING; 1379 prev->flags |= CF_RUNNING;
1367 } 1380 }
1368 1381
1369 prev->flags &= ~CF_RUNNING; 1382 prev->flags &= ~CF_RUNNING;
1380 coro_setup (aTHX_ next); 1393 coro_setup (aTHX_ next);
1381 } 1394 }
1382 else 1395 else
1383 load_perl (aTHX_ next); 1396 load_perl (aTHX_ next);
1384 1397
1385 prev__cctx = prev->cctx; 1398 assert (!prev->cctx);//D temporary
1386 1399
1387 /* possibly untie and reuse the cctx */ 1400 /* possibly untie and reuse the cctx */
1388 if (expect_true ( 1401 if (expect_true (
1389 prev__cctx->idle_sp == STACKLEVEL 1402 cctx_current->idle_sp == STACKLEVEL
1390 && !(prev__cctx->flags & CC_TRACE) 1403 && !(cctx_current->flags & CC_TRACE)
1391 && !force_cctx 1404 && !force_cctx
1392 )) 1405 ))
1393 { 1406 {
1394 /* 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 */
1395 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));
1396 1409
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 */ 1410 /* 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 */ 1411 /* without this the next cctx_get might destroy the running cctx while still in use */
1401 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1412 if (expect_false (CCTX_EXPIRED (cctx_current)))
1402 if (!next->cctx) 1413 if (expect_true (!next->cctx))
1403 next->cctx = cctx_get (aTHX); 1414 next->cctx = cctx_get (aTHX);
1404 1415
1405 cctx_put (prev__cctx); 1416 cctx_put (cctx_current);
1406 } 1417 }
1418 else
1419 prev->cctx = cctx_current;
1407 1420
1408 ++next->usecount; 1421 ++next->usecount;
1409 1422
1410 if (expect_true (!next->cctx)) 1423 cctx_prev = cctx_current;
1411 next->cctx = cctx_get (aTHX); 1424 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1412 1425
1413 if (expect_false (prev__cctx != next->cctx)) 1426 next->cctx = 0;
1427
1428 if (expect_false (cctx_prev != cctx_current))
1414 { 1429 {
1415 prev__cctx->top_env = PL_top_env; 1430 cctx_prev->top_env = PL_top_env;
1416 PL_top_env = next->cctx->top_env; 1431 PL_top_env = cctx_current->top_env;
1417 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1432 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1418 } 1433 }
1419 1434
1420 transfer_tail (aTHX); 1435 transfer_tail (aTHX);
1421 } 1436 }
1422} 1437}
1577{ 1592{
1578 struct coro *coro; 1593 struct coro *coro;
1579 SV *sv_hook; 1594 SV *sv_hook;
1580 void (*xs_hook)(void); 1595 void (*xs_hook)(void);
1581 1596
1582 if (SvROK (coro_sv))
1583 coro_sv = SvRV (coro_sv);
1584
1585 coro = SvSTATE (coro_sv); 1597 coro = SvSTATE (coro_sv);
1586 1598
1587 if (coro->flags & CF_READY) 1599 if (coro->flags & CF_READY)
1588 return 0; 1600 return 0;
1589 1601
1663 } 1675 }
1664 } 1676 }
1665 else 1677 else
1666 { 1678 {
1667 /* nothing to schedule: call the idle handler */ 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 {
1668 dSP; 1689 dSP;
1669 1690
1670 ENTER; 1691 ENTER;
1671 SAVETMPS; 1692 SAVETMPS;
1672 1693
1673 PUSHMARK (SP); 1694 PUSHMARK (SP);
1674 PUTBACK; 1695 PUTBACK;
1675 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); 1696 call_sv (sv_idle, G_VOID | G_DISCARD);
1676 1697
1677 FREETMPS; 1698 FREETMPS;
1678 LEAVE; 1699 LEAVE;
1700 }
1679 } 1701 }
1680 } 1702 }
1681} 1703}
1682 1704
1683INLINE void 1705INLINE void
1754static void 1776static void
1755api_trace (pTHX_ SV *coro_sv, int flags) 1777api_trace (pTHX_ SV *coro_sv, int flags)
1756{ 1778{
1757 struct coro *coro = SvSTATE (coro_sv); 1779 struct coro *coro = SvSTATE (coro_sv);
1758 1780
1781 if (coro->flags & CF_RUNNING)
1782 croak ("cannot enable tracing on a running coroutine, caught");
1783
1759 if (flags & CC_TRACE) 1784 if (flags & CC_TRACE)
1760 { 1785 {
1761 if (!coro->cctx) 1786 if (!coro->cctx)
1762 coro->cctx = cctx_new_run (); 1787 coro->cctx = cctx_new_run ();
1763 else if (!(coro->cctx->flags & CC_TRACE)) 1788 else if (!(coro->cctx->flags & CC_TRACE))
1764 croak ("cannot enable tracing on coroutine with custom stack,"); 1789 croak ("cannot enable tracing on coroutine with custom stack, caught");
1765 1790
1766 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1791 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1767 } 1792 }
1768 else if (coro->cctx && coro->cctx->flags & CC_TRACE) 1793 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1769 { 1794 {
2343 SV **ary; 2368 SV **ary;
2344 2369
2345 /* unfortunately, building manually saves memory */ 2370 /* unfortunately, building manually saves memory */
2346 Newx (ary, 2, SV *); 2371 Newx (ary, 2, SV *);
2347 AvALLOC (av) = ary; 2372 AvALLOC (av) = ary;
2373#if PERL_VERSION_ATLEAST (5,10,0)
2348 /*AvARRAY (av) = ary;*/ 2374 AvARRAY (av) = ary;
2375#else
2349 SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ 2376 /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */
2377 /* -DDEBUGGING flags this as a bug, despite it perfectly working */
2378 SvPVX ((SV *)av) = (char *)ary;
2379#endif
2350 AvMAX (av) = 1; 2380 AvMAX (av) = 1;
2351 AvFILLp (av) = 0; 2381 AvFILLp (av) = 0;
2352 ary [0] = newSViv (count); 2382 ary [0] = newSViv (count);
2353 2383
2354 return newRV_noinc ((SV *)av); 2384 return newRV_noinc ((SV *)av);
2724 XSRETURN_EMPTY; 2754 XSRETURN_EMPTY;
2725} 2755}
2726 2756
2727/*****************************************************************************/ 2757/*****************************************************************************/
2728 2758
2759#if CORO_CLONE
2760# include "clone.c"
2761#endif
2762
2729MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 2763MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2730 2764
2731PROTOTYPES: DISABLE 2765PROTOTYPES: DISABLE
2732 2766
2733BOOT: 2767BOOT:
2736# if CORO_PTHREAD 2770# if CORO_PTHREAD
2737 coro_thx = PERL_GET_CONTEXT; 2771 coro_thx = PERL_GET_CONTEXT;
2738# endif 2772# endif
2739#endif 2773#endif
2740 BOOT_PAGESIZE; 2774 BOOT_PAGESIZE;
2775
2776 cctx_current = cctx_new_empty ();
2741 2777
2742 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 2778 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
2743 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 2779 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
2744 2780
2745 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; 2781 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
2871void 2907void
2872_exit (int code) 2908_exit (int code)
2873 PROTOTYPE: $ 2909 PROTOTYPE: $
2874 CODE: 2910 CODE:
2875 _exit (code); 2911 _exit (code);
2912
2913SV *
2914clone (Coro::State coro)
2915 CODE:
2916{
2917#if CORO_CLONE
2918 struct coro *ncoro = coro_clone (coro);
2919 MAGIC *mg;
2920 /* TODO: too much duplication */
2921 ncoro->hv = newHV ();
2922 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
2923 mg->mg_flags |= MGf_DUP;
2924 RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv));
2925#else
2926 croak ("Coro::State->clone has not been configured into this installation of Coro, realised");
2927#endif
2928}
2929 OUTPUT:
2930 RETVAL
2876 2931
2877int 2932int
2878cctx_stacksize (int new_stacksize = 0) 2933cctx_stacksize (int new_stacksize = 0)
2879 PROTOTYPE: ;$ 2934 PROTOTYPE: ;$
2880 CODE: 2935 CODE:
3001 3056
3002SV * 3057SV *
3003has_cctx (Coro::State coro) 3058has_cctx (Coro::State coro)
3004 PROTOTYPE: $ 3059 PROTOTYPE: $
3005 CODE: 3060 CODE:
3006 RETVAL = boolSV (!!coro->cctx); 3061 /* maybe manage the running flag differently */
3062 RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING));
3007 OUTPUT: 3063 OUTPUT:
3008 RETVAL 3064 RETVAL
3009 3065
3010int 3066int
3011is_traced (Coro::State coro) 3067is_traced (Coro::State coro)
3031 3087
3032void 3088void
3033force_cctx () 3089force_cctx ()
3034 PROTOTYPE: 3090 PROTOTYPE:
3035 CODE: 3091 CODE:
3036 SvSTATE_current->cctx->idle_sp = 0; 3092 cctx_current->idle_sp = 0;
3037 3093
3038void 3094void
3039swap_defsv (Coro::State self) 3095swap_defsv (Coro::State self)
3040 PROTOTYPE: $ 3096 PROTOTYPE: $
3041 ALIAS: 3097 ALIAS:
3064 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3120 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3065 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3121 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); 3122 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3067 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3123 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3068 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3124 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3125 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3069 3126
3070 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3127 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); 3128 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); 3129 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); 3130 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