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.321 by root, Sat Nov 22 02:09:54 2008 UTC vs.
Revision 1.331 by root, Thu Nov 27 12:00:59 2008 UTC

1#define CORO_CLONE 1 //D
2
3#include "libcoro/coro.c" 1#include "libcoro/coro.c"
4 2
5#define PERL_NO_GET_CONTEXT 3#define PERL_NO_GET_CONTEXT
6#define PERL_EXT 4#define PERL_EXT
7 5
135#else 133#else
136# define dSTACKLEVEL volatile void *stacklevel 134# define dSTACKLEVEL volatile void *stacklevel
137# define STACKLEVEL ((void *)&stacklevel) 135# define STACKLEVEL ((void *)&stacklevel)
138#endif 136#endif
139 137
140#define IN_DESTRUCT (PL_main_cv == Nullcv) 138#define IN_DESTRUCT PL_dirty
141 139
142#if __GNUC__ >= 3 140#if __GNUC__ >= 3
143# define attribute(x) __attribute__(x) 141# define attribute(x) __attribute__(x)
144# define expect(expr,value) __builtin_expect ((expr),(value)) 142# define expect(expr,value) __builtin_expect ((expr),(value))
145# define INLINE static inline 143# define INLINE static inline
177static HV *coro_state_stash, *coro_stash; 175static HV *coro_state_stash, *coro_stash;
178static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ 176static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
179 177
180static AV *av_destroy; /* destruction queue */ 178static AV *av_destroy; /* destruction queue */
181static SV *sv_manager; /* the manager coro */ 179static SV *sv_manager; /* the manager coro */
180static SV *sv_idle; /* $Coro::idle */
182 181
183static GV *irsgv; /* $/ */ 182static GV *irsgv; /* $/ */
184static GV *stdoutgv; /* *STDOUT */ 183static GV *stdoutgv; /* *STDOUT */
185static SV *rv_diehook; 184static SV *rv_diehook;
186static SV *rv_warnhook; 185static SV *rv_warnhook;
230 int valgrind_id; 229 int valgrind_id;
231#endif 230#endif
232 unsigned char flags; 231 unsigned char flags;
233} coro_cctx; 232} coro_cctx;
234 233
234coro_cctx *cctx_current; /* the currently running cctx */
235
236/*****************************************************************************/
237
235enum { 238enum {
236 CF_RUNNING = 0x0001, /* coroutine is running */ 239 CF_RUNNING = 0x0001, /* coroutine is running */
237 CF_READY = 0x0002, /* coroutine is ready */ 240 CF_READY = 0x0002, /* coroutine is ready */
238 CF_NEW = 0x0004, /* has never been switched to */ 241 CF_NEW = 0x0004, /* has never been switched to */
239 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 242 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
244{ 247{
245 SV *defsv; 248 SV *defsv;
246 AV *defav; 249 AV *defav;
247 SV *errsv; 250 SV *errsv;
248 SV *irsgv; 251 SV *irsgv;
252 HV *hinthv;
249#define VAR(name,type) type name; 253#define VAR(name,type) type name;
250# include "state.h" 254# include "state.h"
251#undef VAR 255#undef VAR
252} perl_slots; 256} perl_slots;
253 257
377 381
378static void 382static void
379free_padlist (pTHX_ AV *padlist) 383free_padlist (pTHX_ AV *padlist)
380{ 384{
381 /* may be during global destruction */ 385 /* may be during global destruction */
382 if (SvREFCNT (padlist)) 386 if (!IN_DESTRUCT)
383 { 387 {
384 I32 i = AvFILLp (padlist); 388 I32 i = AvFILLp (padlist);
389
385 while (i >= 0) 390 while (i >= 0)
386 { 391 {
387 SV **svp = av_fetch (padlist, i--, FALSE); 392 /* we try to be extra-careful here */
388 if (svp) 393 AV *av = (AV *)AvARRAY (padlist)[i--];
389 { 394
390 SV *sv; 395 I32 i = AvFILLp (av);
391 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;
392 SvREFCNT_dec (sv); 401 SvREFCNT_dec (av);
393
394 SvREFCNT_dec (*svp);
395 }
396 } 402 }
397 403
404 AvFILLp (padlist) = -1;
398 SvREFCNT_dec ((SV*)padlist); 405 SvREFCNT_dec ((SV*)padlist);
399 } 406 }
400} 407}
401 408
402static int 409static int
499 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);
500 507
501 av = (AV *)mg->mg_obj; 508 av = (AV *)mg->mg_obj;
502 509
503 if (expect_false (AvFILLp (av) >= AvMAX (av))) 510 if (expect_false (AvFILLp (av) >= AvMAX (av)))
504 av_extend (av, AvMAX (av) + 1); 511 av_extend (av, AvFILLp (av) + 1);
505 512
506 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 513 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
507} 514}
508 515
509/** load & save, init *******************************************************/ 516/** load & save, init *******************************************************/
514 perl_slots *slot = c->slot; 521 perl_slots *slot = c->slot;
515 c->slot = 0; 522 c->slot = 0;
516 523
517 PL_mainstack = c->mainstack; 524 PL_mainstack = c->mainstack;
518 525
519 GvSV (PL_defgv) = slot->defsv; 526 GvSV (PL_defgv) = slot->defsv;
520 GvAV (PL_defgv) = slot->defav; 527 GvAV (PL_defgv) = slot->defav;
521 GvSV (PL_errgv) = slot->errsv; 528 GvSV (PL_errgv) = slot->errsv;
522 GvSV (irsgv) = slot->irsgv; 529 GvSV (irsgv) = slot->irsgv;
530 GvHV (PL_hintgv) = slot->hinthv;
523 531
524 #define VAR(name,type) PL_ ## name = slot->name; 532 #define VAR(name,type) PL_ ## name = slot->name;
525 # include "state.h" 533 # include "state.h"
526 #undef VAR 534 #undef VAR
527 535
613 c->mainstack = PL_mainstack; 621 c->mainstack = PL_mainstack;
614 622
615 { 623 {
616 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 624 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
617 625
618 slot->defav = GvAV (PL_defgv); 626 slot->defav = GvAV (PL_defgv);
619 slot->defsv = DEFSV; 627 slot->defsv = DEFSV;
620 slot->errsv = ERRSV; 628 slot->errsv = ERRSV;
621 slot->irsgv = GvSV (irsgv); 629 slot->irsgv = GvSV (irsgv);
630 slot->hinthv = GvHV (PL_hintgv);
622 631
623 #define VAR(name,type) slot->name = PL_ ## name; 632 #define VAR(name,type) slot->name = PL_ ## name;
624 # include "state.h" 633 # include "state.h"
625 #undef VAR 634 #undef VAR
626 } 635 }
862 871
863 PL_runops = RUNOPS_DEFAULT; 872 PL_runops = RUNOPS_DEFAULT;
864 PL_curcop = &PL_compiling; 873 PL_curcop = &PL_compiling;
865 PL_in_eval = EVAL_NULL; 874 PL_in_eval = EVAL_NULL;
866 PL_comppad = 0; 875 PL_comppad = 0;
876 PL_comppad_name = 0;
877 PL_comppad_name_fill = 0;
878 PL_comppad_name_floor = 0;
867 PL_curpm = 0; 879 PL_curpm = 0;
868 PL_curpad = 0; 880 PL_curpad = 0;
869 PL_localizing = 0; 881 PL_localizing = 0;
870 PL_dirty = 0; 882 PL_dirty = 0;
871 PL_restartop = 0; 883 PL_restartop = 0;
872#if PERL_VERSION_ATLEAST (5,10,0) 884#if PERL_VERSION_ATLEAST (5,10,0)
873 PL_parser = 0; 885 PL_parser = 0;
874#endif 886#endif
887 PL_hints = 0;
875 888
876 /* recreate the die/warn hooks */ 889 /* recreate the die/warn hooks */
877 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 );
878 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);
879 892
880 GvSV (PL_defgv) = newSV (0); 893 GvSV (PL_defgv) = newSV (0);
881 GvAV (PL_defgv) = coro->args; coro->args = 0; 894 GvAV (PL_defgv) = coro->args; coro->args = 0;
882 GvSV (PL_errgv) = newSV (0); 895 GvSV (PL_errgv) = newSV (0);
883 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;
884 PL_rs = newSVsv (GvSV (irsgv)); 898 PL_rs = newSVsv (GvSV (irsgv));
885 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 899 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
886 900
887 { 901 {
888 dSP; 902 dSP;
942 SvREFCNT_dec (GvAV (PL_defgv)); 956 SvREFCNT_dec (GvAV (PL_defgv));
943 SvREFCNT_dec (GvSV (PL_errgv)); 957 SvREFCNT_dec (GvSV (PL_errgv));
944 SvREFCNT_dec (PL_defoutgv); 958 SvREFCNT_dec (PL_defoutgv);
945 SvREFCNT_dec (PL_rs); 959 SvREFCNT_dec (PL_rs);
946 SvREFCNT_dec (GvSV (irsgv)); 960 SvREFCNT_dec (GvSV (irsgv));
961 SvREFCNT_dec (GvHV (PL_hintgv));
947 962
948 SvREFCNT_dec (PL_diehook); 963 SvREFCNT_dec (PL_diehook);
949 SvREFCNT_dec (PL_warnhook); 964 SvREFCNT_dec (PL_warnhook);
950 965
951 SvREFCNT_dec (coro->saved_deffh); 966 SvREFCNT_dec (coro->saved_deffh);
969static int 984static int
970runops_trace (pTHX) 985runops_trace (pTHX)
971{ 986{
972 COP *oldcop = 0; 987 COP *oldcop = 0;
973 int oldcxix = -2; 988 int oldcxix = -2;
974 struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */
975 coro_cctx *cctx = coro->cctx;
976 989
977 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 990 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
978 { 991 {
979 PERL_ASYNC_CHECK (); 992 PERL_ASYNC_CHECK ();
980 993
981 if (cctx->flags & CC_TRACE_ALL) 994 if (cctx_current->flags & CC_TRACE_ALL)
982 { 995 {
983 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)
984 { 997 {
985 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 998 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
986 SV **bot, **top; 999 SV **bot, **top;
987 AV *av = newAV (); /* return values */ 1000 AV *av = newAV (); /* return values */
988 SV **cb; 1001 SV **cb;
1025 1038
1026 if (PL_curcop != &PL_compiling) 1039 if (PL_curcop != &PL_compiling)
1027 { 1040 {
1028 SV **cb; 1041 SV **cb;
1029 1042
1030 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) 1043 if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB)
1031 { 1044 {
1032 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1045 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1033 1046
1034 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 1047 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
1035 { 1048 {
1058 } 1071 }
1059 1072
1060 oldcxix = cxstack_ix; 1073 oldcxix = cxstack_ix;
1061 } 1074 }
1062 1075
1063 if (cctx->flags & CC_TRACE_LINE) 1076 if (cctx_current->flags & CC_TRACE_LINE)
1064 { 1077 {
1065 dSP; 1078 dSP;
1066 1079
1067 PL_runops = RUNOPS_DEFAULT; 1080 PL_runops = RUNOPS_DEFAULT;
1068 ENTER; 1081 ENTER;
1087 1100
1088 TAINT_NOT; 1101 TAINT_NOT;
1089 return 0; 1102 return 0;
1090} 1103}
1091 1104
1092static struct coro_cctx *cctx_ssl_cctx;
1093static struct CoroSLF cctx_ssl_frame; 1105static struct CoroSLF cctx_ssl_frame;
1094 1106
1095static void 1107static void
1096slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) 1108slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1097{ 1109{
1098 ta->prev = (struct coro *)cctx_ssl_cctx;
1099 ta->next = 0; 1110 ta->prev = 0;
1100} 1111}
1101 1112
1102static int 1113static int
1103slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) 1114slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1104{ 1115{
1107 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 */
1108} 1119}
1109 1120
1110/* 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 */
1111static void NOINLINE 1122static void NOINLINE
1112cctx_prepare (pTHX_ coro_cctx *cctx) 1123cctx_prepare (pTHX)
1113{ 1124{
1114 PL_top_env = &PL_start_env; 1125 PL_top_env = &PL_start_env;
1115 1126
1116 if (cctx->flags & CC_TRACE) 1127 if (cctx_current->flags & CC_TRACE)
1117 PL_runops = runops_trace; 1128 PL_runops = runops_trace;
1118 1129
1119 /* 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
1120 * that can lead to creation of a new cctx */ 1131 * that can lead to creation of a new cctx */
1121 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)",
1122 slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); 1133 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1123 1134
1124 /* 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 */
1125 cctx_ssl_cctx = cctx;
1126 cctx_ssl_frame = slf_frame; 1136 cctx_ssl_frame = slf_frame;
1127 1137
1128 slf_frame.prepare = slf_prepare_set_stacklevel; 1138 slf_frame.prepare = slf_prepare_set_stacklevel;
1129 slf_frame.check = slf_check_set_stacklevel; 1139 slf_frame.check = slf_check_set_stacklevel;
1130} 1140}
1153 /* normally we would need to skip the entersub here */ 1163 /* normally we would need to skip the entersub here */
1154 /* 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 */
1155 /* PL_nop = PL_nop->op_next */ 1165 /* PL_nop = PL_nop->op_next */
1156 1166
1157 /* inject a fake subroutine call to cctx_init */ 1167 /* inject a fake subroutine call to cctx_init */
1158 cctx_prepare (aTHX_ (coro_cctx *)arg); 1168 cctx_prepare (aTHX);
1159 1169
1160 /* cctx_run is the alternative tail of transfer() */ 1170 /* cctx_run is the alternative tail of transfer() */
1161 transfer_tail (aTHX); 1171 transfer_tail (aTHX);
1162 1172
1163 /* 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 */
1171 /* 1181 /*
1172 * 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
1173 * 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)
1174 * 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
1175 * bootstrap-time "top" top_env, as we cannot restore the "main" 1185 * bootstrap-time "top" top_env, as we cannot restore the "main"
1176 * 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.
1177 */ 1189 */
1178 PL_top_env = main_top_env; 1190 PL_top_env = main_top_env;
1179 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 */
1180 } 1192 }
1181} 1193}
1258cctx_destroy (coro_cctx *cctx) 1270cctx_destroy (coro_cctx *cctx)
1259{ 1271{
1260 if (!cctx) 1272 if (!cctx)
1261 return; 1273 return;
1262 1274
1275 assert (cctx != cctx_current);//D temporary
1276
1263 --cctx_count; 1277 --cctx_count;
1264 coro_destroy (&cctx->cctx); 1278 coro_destroy (&cctx->cctx);
1265 1279
1266 /* coro_transfer creates new, empty cctx's */ 1280 /* coro_transfer creates new, empty cctx's */
1267 if (cctx->sptr) 1281 if (cctx->sptr)
1330 /* TODO: throwing up here is considered harmful */ 1344 /* TODO: throwing up here is considered harmful */
1331 1345
1332 if (expect_true (prev != next)) 1346 if (expect_true (prev != next))
1333 { 1347 {
1334 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1348 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1335 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,");
1336 1350
1337 if (expect_false (next->flags & CF_RUNNING)) 1351 if (expect_false (next->flags & CF_RUNNING))
1338 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,");
1339 1353
1340 if (expect_false (next->flags & CF_DESTROYED)) 1354 if (expect_false (next->flags & CF_DESTROYED))
1352transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1366transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1353{ 1367{
1354 dSTACKLEVEL; 1368 dSTACKLEVEL;
1355 1369
1356 /* sometimes transfer is only called to set idle_sp */ 1370 /* sometimes transfer is only called to set idle_sp */
1357 if (expect_false (!next)) 1371 if (expect_false (!prev))
1358 { 1372 {
1359 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1373 cctx_current->idle_sp = STACKLEVEL;
1360 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 */
1361 } 1375 }
1362 else if (expect_true (prev != next)) 1376 else if (expect_true (prev != next))
1363 { 1377 {
1364 coro_cctx *prev__cctx; 1378 coro_cctx *cctx_prev;
1365 1379
1366 if (expect_false (prev->flags & CF_NEW)) 1380 if (expect_false (prev->flags & CF_NEW))
1367 { 1381 {
1368 /* create a new empty/source context */ 1382 /* create a new empty/source context */
1369 prev->cctx = cctx_new_empty ();
1370 prev->flags &= ~CF_NEW; 1383 prev->flags &= ~CF_NEW;
1371 prev->flags |= CF_RUNNING; 1384 prev->flags |= CF_RUNNING;
1372 } 1385 }
1373 1386
1374 prev->flags &= ~CF_RUNNING; 1387 prev->flags &= ~CF_RUNNING;
1385 coro_setup (aTHX_ next); 1398 coro_setup (aTHX_ next);
1386 } 1399 }
1387 else 1400 else
1388 load_perl (aTHX_ next); 1401 load_perl (aTHX_ next);
1389 1402
1390 prev__cctx = prev->cctx; 1403 assert (!prev->cctx);//D temporary
1391 1404
1392 /* possibly untie and reuse the cctx */ 1405 /* possibly untie and reuse the cctx */
1393 if (expect_true ( 1406 if (expect_true (
1394 prev__cctx->idle_sp == STACKLEVEL 1407 cctx_current->idle_sp == STACKLEVEL
1395 && !(prev__cctx->flags & CC_TRACE) 1408 && !(cctx_current->flags & CC_TRACE)
1396 && !force_cctx 1409 && !force_cctx
1397 )) 1410 ))
1398 { 1411 {
1399 /* 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 */
1400 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));
1401 1414
1402 prev->cctx = 0;
1403
1404 /* 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. */
1405 /* 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 */
1406 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1417 if (expect_false (CCTX_EXPIRED (cctx_current)))
1407 if (!next->cctx) 1418 if (expect_true (!next->cctx))
1408 next->cctx = cctx_get (aTHX); 1419 next->cctx = cctx_get (aTHX);
1409 1420
1410 cctx_put (prev__cctx); 1421 cctx_put (cctx_current);
1411 } 1422 }
1423 else
1424 prev->cctx = cctx_current;
1412 1425
1413 ++next->usecount; 1426 ++next->usecount;
1414 1427
1415 if (expect_true (!next->cctx)) 1428 cctx_prev = cctx_current;
1416 next->cctx = cctx_get (aTHX); 1429 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1417 1430
1418 if (expect_false (prev__cctx != next->cctx)) 1431 next->cctx = 0;
1432
1433 if (expect_false (cctx_prev != cctx_current))
1419 { 1434 {
1420 prev__cctx->top_env = PL_top_env; 1435 cctx_prev->top_env = PL_top_env;
1421 PL_top_env = next->cctx->top_env; 1436 PL_top_env = cctx_current->top_env;
1422 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1437 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1423 } 1438 }
1424 1439
1425 transfer_tail (aTHX); 1440 transfer_tail (aTHX);
1426 } 1441 }
1427} 1442}
1582{ 1597{
1583 struct coro *coro; 1598 struct coro *coro;
1584 SV *sv_hook; 1599 SV *sv_hook;
1585 void (*xs_hook)(void); 1600 void (*xs_hook)(void);
1586 1601
1587 if (SvROK (coro_sv))
1588 coro_sv = SvRV (coro_sv);
1589
1590 coro = SvSTATE (coro_sv); 1602 coro = SvSTATE (coro_sv);
1591 1603
1592 if (coro->flags & CF_READY) 1604 if (coro->flags & CF_READY)
1593 return 0; 1605 return 0;
1594 1606
1668 } 1680 }
1669 } 1681 }
1670 else 1682 else
1671 { 1683 {
1672 /* 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 {
1673 dSP; 1694 dSP;
1674 1695
1675 ENTER; 1696 ENTER;
1676 SAVETMPS; 1697 SAVETMPS;
1677 1698
1678 PUSHMARK (SP); 1699 PUSHMARK (SP);
1679 PUTBACK; 1700 PUTBACK;
1680 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); 1701 call_sv (sv_idle, G_VOID | G_DISCARD);
1681 1702
1682 FREETMPS; 1703 FREETMPS;
1683 LEAVE; 1704 LEAVE;
1705 }
1684 } 1706 }
1685 } 1707 }
1686} 1708}
1687 1709
1688INLINE void 1710INLINE void
1759static void 1781static void
1760api_trace (pTHX_ SV *coro_sv, int flags) 1782api_trace (pTHX_ SV *coro_sv, int flags)
1761{ 1783{
1762 struct coro *coro = SvSTATE (coro_sv); 1784 struct coro *coro = SvSTATE (coro_sv);
1763 1785
1786 if (coro->flags & CF_RUNNING)
1787 croak ("cannot enable tracing on a running coroutine, caught");
1788
1764 if (flags & CC_TRACE) 1789 if (flags & CC_TRACE)
1765 { 1790 {
1766 if (!coro->cctx) 1791 if (!coro->cctx)
1767 coro->cctx = cctx_new_run (); 1792 coro->cctx = cctx_new_run ();
1768 else if (!(coro->cctx->flags & CC_TRACE)) 1793 else if (!(coro->cctx->flags & CC_TRACE))
1769 croak ("cannot enable tracing on coroutine with custom stack,"); 1794 croak ("cannot enable tracing on coroutine with custom stack, caught");
1770 1795
1771 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1796 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1772 } 1797 }
1773 else if (coro->cctx && coro->cctx->flags & CC_TRACE) 1798 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1774 { 1799 {
2348 SV **ary; 2373 SV **ary;
2349 2374
2350 /* unfortunately, building manually saves memory */ 2375 /* unfortunately, building manually saves memory */
2351 Newx (ary, 2, SV *); 2376 Newx (ary, 2, SV *);
2352 AvALLOC (av) = ary; 2377 AvALLOC (av) = ary;
2378#if PERL_VERSION_ATLEAST (5,10,0)
2353 /*AvARRAY (av) = ary;*/ 2379 AvARRAY (av) = ary;
2380#else
2354 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
2355 AvMAX (av) = 1; 2385 AvMAX (av) = 1;
2356 AvFILLp (av) = 0; 2386 AvFILLp (av) = 0;
2357 ary [0] = newSViv (count); 2387 ary [0] = newSViv (count);
2358 2388
2359 return newRV_noinc ((SV *)av); 2389 return newRV_noinc ((SV *)av);
2746 coro_thx = PERL_GET_CONTEXT; 2776 coro_thx = PERL_GET_CONTEXT;
2747# endif 2777# endif
2748#endif 2778#endif
2749 BOOT_PAGESIZE; 2779 BOOT_PAGESIZE;
2750 2780
2781 cctx_current = cctx_new_empty ();
2782
2751 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 2783 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
2752 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 2784 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
2753 2785
2754 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;
2755 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; 2787 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
2881_exit (int code) 2913_exit (int code)
2882 PROTOTYPE: $ 2914 PROTOTYPE: $
2883 CODE: 2915 CODE:
2884 _exit (code); 2916 _exit (code);
2885 2917
2886#if CORO_CLONE
2887
2888SV * 2918SV *
2889clone (Coro::State coro) 2919clone (Coro::State coro)
2890 CODE: 2920 CODE:
2891{ 2921{
2922#if CORO_CLONE
2892 struct coro *ncoro = coro_clone (coro); 2923 struct coro *ncoro = coro_clone (coro);
2893 MAGIC *mg; 2924 MAGIC *mg;
2894 /* TODO: too much duplication */ 2925 /* TODO: too much duplication */
2895 ncoro->hv = newHV (); 2926 ncoro->hv = newHV ();
2896 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); 2927 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
2897 mg->mg_flags |= MGf_DUP; 2928 mg->mg_flags |= MGf_DUP;
2898 RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv)); 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
2899} 2933}
2900 OUTPUT: 2934 OUTPUT:
2901 RETVAL 2935 RETVAL
2902
2903#endif
2904 2936
2905int 2937int
2906cctx_stacksize (int new_stacksize = 0) 2938cctx_stacksize (int new_stacksize = 0)
2907 PROTOTYPE: ;$ 2939 PROTOTYPE: ;$
2908 CODE: 2940 CODE:
3029 3061
3030SV * 3062SV *
3031has_cctx (Coro::State coro) 3063has_cctx (Coro::State coro)
3032 PROTOTYPE: $ 3064 PROTOTYPE: $
3033 CODE: 3065 CODE:
3034 RETVAL = boolSV (!!coro->cctx); 3066 /* maybe manage the running flag differently */
3067 RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING));
3035 OUTPUT: 3068 OUTPUT:
3036 RETVAL 3069 RETVAL
3037 3070
3038int 3071int
3039is_traced (Coro::State coro) 3072is_traced (Coro::State coro)
3059 3092
3060void 3093void
3061force_cctx () 3094force_cctx ()
3062 PROTOTYPE: 3095 PROTOTYPE:
3063 CODE: 3096 CODE:
3064 SvSTATE_current->cctx->idle_sp = 0; 3097 cctx_current->idle_sp = 0;
3065 3098
3066void 3099void
3067swap_defsv (Coro::State self) 3100swap_defsv (Coro::State self)
3068 PROTOTYPE: $ 3101 PROTOTYPE: $
3069 ALIAS: 3102 ALIAS:
3092 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3125 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3093 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);
3094 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 3127 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3095 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3128 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3096 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);
3097 3131
3098 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);
3099 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3133 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3100 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);
3101 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