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