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.318 by root, Thu Nov 20 07:02:43 2008 UTC vs.
Revision 1.327 by root, Mon Nov 24 06:07:16 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 {
1031 runops_proc_t old_runops = PL_runops;
1032 dSP; 1046 dSP;
1033 GV *gv = CvGV (cx->blk_sub.cv); 1047 GV *gv = CvGV (cx->blk_sub.cv);
1034 SV *fullname = sv_2mortal (newSV (0)); 1048 SV *fullname = sv_2mortal (newSV (0));
1035 1049
1036 if (isGV (gv)) 1050 if (isGV (gv))
1054 } 1068 }
1055 1069
1056 oldcxix = cxstack_ix; 1070 oldcxix = cxstack_ix;
1057 } 1071 }
1058 1072
1059 if (cctx->flags & CC_TRACE_LINE) 1073 if (cctx_current->flags & CC_TRACE_LINE)
1060 { 1074 {
1061 dSP; 1075 dSP;
1062 1076
1063 PL_runops = RUNOPS_DEFAULT; 1077 PL_runops = RUNOPS_DEFAULT;
1064 ENTER; 1078 ENTER;
1083 1097
1084 TAINT_NOT; 1098 TAINT_NOT;
1085 return 0; 1099 return 0;
1086} 1100}
1087 1101
1088static struct coro_cctx *cctx_ssl_cctx;
1089static struct CoroSLF cctx_ssl_frame; 1102static struct CoroSLF cctx_ssl_frame;
1090 1103
1091static void 1104static void
1092slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) 1105slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1093{ 1106{
1094 ta->prev = (struct coro *)cctx_ssl_cctx;
1095 ta->next = 0; 1107 ta->prev = 0;
1096} 1108}
1097 1109
1098static int 1110static int
1099slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) 1111slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1100{ 1112{
1103 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 */
1104} 1116}
1105 1117
1106/* 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 */
1107static void NOINLINE 1119static void NOINLINE
1108cctx_prepare (pTHX_ coro_cctx *cctx) 1120cctx_prepare (pTHX)
1109{ 1121{
1110 PL_top_env = &PL_start_env; 1122 PL_top_env = &PL_start_env;
1111 1123
1112 if (cctx->flags & CC_TRACE) 1124 if (cctx_current->flags & CC_TRACE)
1113 PL_runops = runops_trace; 1125 PL_runops = runops_trace;
1114 1126
1115 /* 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
1116 * that can lead to creation of a new cctx */ 1128 * that can lead to creation of a new cctx */
1117 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)",
1118 slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); 1130 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1119 1131
1120 /* 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 */
1121 cctx_ssl_cctx = cctx;
1122 cctx_ssl_frame = slf_frame; 1133 cctx_ssl_frame = slf_frame;
1123 1134
1124 slf_frame.prepare = slf_prepare_set_stacklevel; 1135 slf_frame.prepare = slf_prepare_set_stacklevel;
1125 slf_frame.check = slf_check_set_stacklevel; 1136 slf_frame.check = slf_check_set_stacklevel;
1126} 1137}
1149 /* normally we would need to skip the entersub here */ 1160 /* normally we would need to skip the entersub here */
1150 /* 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 */
1151 /* PL_nop = PL_nop->op_next */ 1162 /* PL_nop = PL_nop->op_next */
1152 1163
1153 /* inject a fake subroutine call to cctx_init */ 1164 /* inject a fake subroutine call to cctx_init */
1154 cctx_prepare (aTHX_ (coro_cctx *)arg); 1165 cctx_prepare (aTHX);
1155 1166
1156 /* cctx_run is the alternative tail of transfer() */ 1167 /* cctx_run is the alternative tail of transfer() */
1157 transfer_tail (aTHX); 1168 transfer_tail (aTHX);
1158 1169
1159 /* 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 */
1254cctx_destroy (coro_cctx *cctx) 1265cctx_destroy (coro_cctx *cctx)
1255{ 1266{
1256 if (!cctx) 1267 if (!cctx)
1257 return; 1268 return;
1258 1269
1270 assert (cctx != cctx_current);//D temporary
1271
1259 --cctx_count; 1272 --cctx_count;
1260 coro_destroy (&cctx->cctx); 1273 coro_destroy (&cctx->cctx);
1261 1274
1262 /* coro_transfer creates new, empty cctx's */ 1275 /* coro_transfer creates new, empty cctx's */
1263 if (cctx->sptr) 1276 if (cctx->sptr)
1326 /* TODO: throwing up here is considered harmful */ 1339 /* TODO: throwing up here is considered harmful */
1327 1340
1328 if (expect_true (prev != next)) 1341 if (expect_true (prev != next))
1329 { 1342 {
1330 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1343 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1331 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,");
1332 1345
1333 if (expect_false (next->flags & CF_RUNNING)) 1346 if (expect_false (next->flags & CF_RUNNING))
1334 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,");
1335 1348
1336 if (expect_false (next->flags & CF_DESTROYED)) 1349 if (expect_false (next->flags & CF_DESTROYED))
1348transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1361transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1349{ 1362{
1350 dSTACKLEVEL; 1363 dSTACKLEVEL;
1351 1364
1352 /* sometimes transfer is only called to set idle_sp */ 1365 /* sometimes transfer is only called to set idle_sp */
1353 if (expect_false (!next)) 1366 if (expect_false (!prev))
1354 { 1367 {
1355 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1368 cctx_current->idle_sp = STACKLEVEL;
1356 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 */
1357 } 1370 }
1358 else if (expect_true (prev != next)) 1371 else if (expect_true (prev != next))
1359 { 1372 {
1360 coro_cctx *prev__cctx; 1373 coro_cctx *cctx_prev;
1361 1374
1362 if (expect_false (prev->flags & CF_NEW)) 1375 if (expect_false (prev->flags & CF_NEW))
1363 { 1376 {
1364 /* create a new empty/source context */ 1377 /* create a new empty/source context */
1365 prev->cctx = cctx_new_empty ();
1366 prev->flags &= ~CF_NEW; 1378 prev->flags &= ~CF_NEW;
1367 prev->flags |= CF_RUNNING; 1379 prev->flags |= CF_RUNNING;
1368 } 1380 }
1369 1381
1370 prev->flags &= ~CF_RUNNING; 1382 prev->flags &= ~CF_RUNNING;
1381 coro_setup (aTHX_ next); 1393 coro_setup (aTHX_ next);
1382 } 1394 }
1383 else 1395 else
1384 load_perl (aTHX_ next); 1396 load_perl (aTHX_ next);
1385 1397
1386 prev__cctx = prev->cctx; 1398 assert (!prev->cctx);//D temporary
1387 1399
1388 /* possibly untie and reuse the cctx */ 1400 /* possibly untie and reuse the cctx */
1389 if (expect_true ( 1401 if (expect_true (
1390 prev__cctx->idle_sp == STACKLEVEL 1402 cctx_current->idle_sp == STACKLEVEL
1391 && !(prev__cctx->flags & CC_TRACE) 1403 && !(cctx_current->flags & CC_TRACE)
1392 && !force_cctx 1404 && !force_cctx
1393 )) 1405 ))
1394 { 1406 {
1395 /* 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 */
1396 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));
1397 1409
1398 prev->cctx = 0;
1399
1400 /* 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. */
1401 /* 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 */
1402 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1412 if (expect_false (CCTX_EXPIRED (cctx_current)))
1403 if (!next->cctx) 1413 if (expect_true (!next->cctx))
1404 next->cctx = cctx_get (aTHX); 1414 next->cctx = cctx_get (aTHX);
1405 1415
1406 cctx_put (prev__cctx); 1416 cctx_put (cctx_current);
1407 } 1417 }
1418 else
1419 prev->cctx = cctx_current;
1408 1420
1409 ++next->usecount; 1421 ++next->usecount;
1410 1422
1411 if (expect_true (!next->cctx)) 1423 cctx_prev = cctx_current;
1412 next->cctx = cctx_get (aTHX); 1424 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1413 1425
1414 if (expect_false (prev__cctx != next->cctx)) 1426 next->cctx = 0;
1427
1428 if (expect_false (cctx_prev != cctx_current))
1415 { 1429 {
1416 prev__cctx->top_env = PL_top_env; 1430 cctx_prev->top_env = PL_top_env;
1417 PL_top_env = next->cctx->top_env; 1431 PL_top_env = cctx_current->top_env;
1418 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1432 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1419 } 1433 }
1420 1434
1421 transfer_tail (aTHX); 1435 transfer_tail (aTHX);
1422 } 1436 }
1423} 1437}
1578{ 1592{
1579 struct coro *coro; 1593 struct coro *coro;
1580 SV *sv_hook; 1594 SV *sv_hook;
1581 void (*xs_hook)(void); 1595 void (*xs_hook)(void);
1582 1596
1583 if (SvROK (coro_sv))
1584 coro_sv = SvRV (coro_sv);
1585
1586 coro = SvSTATE (coro_sv); 1597 coro = SvSTATE (coro_sv);
1587 1598
1588 if (coro->flags & CF_READY) 1599 if (coro->flags & CF_READY)
1589 return 0; 1600 return 0;
1590 1601
1657 else 1668 else
1658 { 1669 {
1659 next->flags &= ~CF_READY; 1670 next->flags &= ~CF_READY;
1660 --coro_nready; 1671 --coro_nready;
1661 1672
1662 return prepare_schedule_to (aTHX_ ta, next); 1673 prepare_schedule_to (aTHX_ ta, next);
1674 break;
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 {
2250 } 2275 }
2251 else 2276 else
2252 slf_argc = 0; 2277 slf_argc = 0;
2253 2278
2254 PL_op->op_ppaddr = pp_slf; 2279 PL_op->op_ppaddr = pp_slf;
2255 PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ 2280 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2256 2281
2257 PL_op = (OP *)&slf_restore; 2282 PL_op = (OP *)&slf_restore;
2258} 2283}
2259 2284
2260/*****************************************************************************/ 2285/*****************************************************************************/
2724 XSRETURN_EMPTY; 2749 XSRETURN_EMPTY;
2725} 2750}
2726 2751
2727/*****************************************************************************/ 2752/*****************************************************************************/
2728 2753
2754#if CORO_CLONE
2755# include "clone.c"
2756#endif
2757
2729MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 2758MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2730 2759
2731PROTOTYPES: DISABLE 2760PROTOTYPES: DISABLE
2732 2761
2733BOOT: 2762BOOT:
2737 coro_thx = PERL_GET_CONTEXT; 2766 coro_thx = PERL_GET_CONTEXT;
2738# endif 2767# endif
2739#endif 2768#endif
2740 BOOT_PAGESIZE; 2769 BOOT_PAGESIZE;
2741 2770
2771 cctx_current = cctx_new_empty ();
2772
2742 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 2773 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
2743 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 2774 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
2744 2775
2745 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; 2776 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
2746 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; 2777 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
2765 2796
2766 { 2797 {
2767 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); 2798 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf)));
2768 2799
2769 if (!PL_custom_op_names) PL_custom_op_names = newHV (); 2800 if (!PL_custom_op_names) PL_custom_op_names = newHV ();
2770 hv_store_ent (PL_custom_op_names, slf, 2801 hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0);
2771 newSVpv ("coro_slf", 0), 0);
2772 2802
2773 if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); 2803 if (!PL_custom_op_descs) PL_custom_op_descs = newHV ();
2774 hv_store_ent (PL_custom_op_descs, slf, 2804 hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0);
2775 newSVpv ("coro schedule like function", 0), 0);
2776 } 2805 }
2777 2806
2778 coroapi.ver = CORO_API_VERSION; 2807 coroapi.ver = CORO_API_VERSION;
2779 coroapi.rev = CORO_API_REVISION; 2808 coroapi.rev = CORO_API_REVISION;
2780 2809
2873void 2902void
2874_exit (int code) 2903_exit (int code)
2875 PROTOTYPE: $ 2904 PROTOTYPE: $
2876 CODE: 2905 CODE:
2877 _exit (code); 2906 _exit (code);
2907
2908SV *
2909clone (Coro::State coro)
2910 CODE:
2911{
2912#if CORO_CLONE
2913 struct coro *ncoro = coro_clone (coro);
2914 MAGIC *mg;
2915 /* TODO: too much duplication */
2916 ncoro->hv = newHV ();
2917 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
2918 mg->mg_flags |= MGf_DUP;
2919 RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv));
2920#else
2921 croak ("Coro::State->clone has not been configured into this installation of Coro, realised");
2922#endif
2923}
2924 OUTPUT:
2925 RETVAL
2878 2926
2879int 2927int
2880cctx_stacksize (int new_stacksize = 0) 2928cctx_stacksize (int new_stacksize = 0)
2881 PROTOTYPE: ;$ 2929 PROTOTYPE: ;$
2882 CODE: 2930 CODE:
3003 3051
3004SV * 3052SV *
3005has_cctx (Coro::State coro) 3053has_cctx (Coro::State coro)
3006 PROTOTYPE: $ 3054 PROTOTYPE: $
3007 CODE: 3055 CODE:
3008 RETVAL = boolSV (!!coro->cctx); 3056 /* maybe manage the running flag differently */
3057 RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING));
3009 OUTPUT: 3058 OUTPUT:
3010 RETVAL 3059 RETVAL
3011 3060
3012int 3061int
3013is_traced (Coro::State coro) 3062is_traced (Coro::State coro)
3033 3082
3034void 3083void
3035force_cctx () 3084force_cctx ()
3036 PROTOTYPE: 3085 PROTOTYPE:
3037 CODE: 3086 CODE:
3038 SvSTATE_current->cctx->idle_sp = 0; 3087 cctx_current->idle_sp = 0;
3039 3088
3040void 3089void
3041swap_defsv (Coro::State self) 3090swap_defsv (Coro::State self)
3042 PROTOTYPE: $ 3091 PROTOTYPE: $
3043 ALIAS: 3092 ALIAS:
3066 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3115 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3067 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3116 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
3068 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 3117 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3069 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3118 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3070 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3119 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3120 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3071 3121
3072 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3122 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
3073 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3123 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3074 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); 3124 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3075 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); 3125 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