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.330 by root, Wed Nov 26 09:29:06 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
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
382 if (SvREFCNT (padlist)) 386 if (SvREFCNT (padlist))
383 { 387 {
384 I32 i = AvFILLp (padlist); 388 I32 i = AvFILLp (padlist);
385 while (i >= 0) 389 while (i >= 0)
386 { 390 {
387 SV **svp = av_fetch (padlist, i--, FALSE); 391 SV **svp = AvARRAY (padlist)[i--];
388 if (svp) 392 if (svp)
389 { 393 {
390 SV *sv; 394 AvREAL_on (*svp);
391 while (&PL_sv_undef != (sv = av_pop ((AV *)*svp)))
392 SvREFCNT_dec (sv); 395 av_undef (*svp);
393
394 SvREFCNT_dec (*svp); 396 SvREFCNT_dec (*svp);
395 } 397 }
396 } 398 }
397 399
398 SvREFCNT_dec ((SV*)padlist); 400 SvREFCNT_dec ((SV*)padlist);
499 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);
500 502
501 av = (AV *)mg->mg_obj; 503 av = (AV *)mg->mg_obj;
502 504
503 if (expect_false (AvFILLp (av) >= AvMAX (av))) 505 if (expect_false (AvFILLp (av) >= AvMAX (av)))
504 av_extend (av, AvMAX (av) + 1); 506 av_extend (av, AvFILLp (av) + 1);
505 507
506 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 508 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
507} 509}
508 510
509/** load & save, init *******************************************************/ 511/** load & save, init *******************************************************/
514 perl_slots *slot = c->slot; 516 perl_slots *slot = c->slot;
515 c->slot = 0; 517 c->slot = 0;
516 518
517 PL_mainstack = c->mainstack; 519 PL_mainstack = c->mainstack;
518 520
519 GvSV (PL_defgv) = slot->defsv; 521 GvSV (PL_defgv) = slot->defsv;
520 GvAV (PL_defgv) = slot->defav; 522 GvAV (PL_defgv) = slot->defav;
521 GvSV (PL_errgv) = slot->errsv; 523 GvSV (PL_errgv) = slot->errsv;
522 GvSV (irsgv) = slot->irsgv; 524 GvSV (irsgv) = slot->irsgv;
525 GvHV (PL_hintgv) = slot->hinthv;
523 526
524 #define VAR(name,type) PL_ ## name = slot->name; 527 #define VAR(name,type) PL_ ## name = slot->name;
525 # include "state.h" 528 # include "state.h"
526 #undef VAR 529 #undef VAR
527 530
613 c->mainstack = PL_mainstack; 616 c->mainstack = PL_mainstack;
614 617
615 { 618 {
616 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 619 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
617 620
618 slot->defav = GvAV (PL_defgv); 621 slot->defav = GvAV (PL_defgv);
619 slot->defsv = DEFSV; 622 slot->defsv = DEFSV;
620 slot->errsv = ERRSV; 623 slot->errsv = ERRSV;
621 slot->irsgv = GvSV (irsgv); 624 slot->irsgv = GvSV (irsgv);
625 slot->hinthv = GvHV (PL_hintgv);
622 626
623 #define VAR(name,type) slot->name = PL_ ## name; 627 #define VAR(name,type) slot->name = PL_ ## name;
624 # include "state.h" 628 # include "state.h"
625 #undef VAR 629 #undef VAR
626 } 630 }
862 866
863 PL_runops = RUNOPS_DEFAULT; 867 PL_runops = RUNOPS_DEFAULT;
864 PL_curcop = &PL_compiling; 868 PL_curcop = &PL_compiling;
865 PL_in_eval = EVAL_NULL; 869 PL_in_eval = EVAL_NULL;
866 PL_comppad = 0; 870 PL_comppad = 0;
871 PL_comppad_name = 0;
872 PL_comppad_name_fill = 0;
873 PL_comppad_name_floor = 0;
867 PL_curpm = 0; 874 PL_curpm = 0;
868 PL_curpad = 0; 875 PL_curpad = 0;
869 PL_localizing = 0; 876 PL_localizing = 0;
870 PL_dirty = 0; 877 PL_dirty = 0;
871 PL_restartop = 0; 878 PL_restartop = 0;
872#if PERL_VERSION_ATLEAST (5,10,0) 879#if PERL_VERSION_ATLEAST (5,10,0)
873 PL_parser = 0; 880 PL_parser = 0;
874#endif 881#endif
882 PL_hints = 0;
875 883
876 /* recreate the die/warn hooks */ 884 /* recreate the die/warn hooks */
877 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 );
878 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);
879 887
880 GvSV (PL_defgv) = newSV (0); 888 GvSV (PL_defgv) = newSV (0);
881 GvAV (PL_defgv) = coro->args; coro->args = 0; 889 GvAV (PL_defgv) = coro->args; coro->args = 0;
882 GvSV (PL_errgv) = newSV (0); 890 GvSV (PL_errgv) = newSV (0);
883 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;
884 PL_rs = newSVsv (GvSV (irsgv)); 893 PL_rs = newSVsv (GvSV (irsgv));
885 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 894 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
886 895
887 { 896 {
888 dSP; 897 dSP;
942 SvREFCNT_dec (GvAV (PL_defgv)); 951 SvREFCNT_dec (GvAV (PL_defgv));
943 SvREFCNT_dec (GvSV (PL_errgv)); 952 SvREFCNT_dec (GvSV (PL_errgv));
944 SvREFCNT_dec (PL_defoutgv); 953 SvREFCNT_dec (PL_defoutgv);
945 SvREFCNT_dec (PL_rs); 954 SvREFCNT_dec (PL_rs);
946 SvREFCNT_dec (GvSV (irsgv)); 955 SvREFCNT_dec (GvSV (irsgv));
956 SvREFCNT_dec (GvHV (PL_hintgv));
947 957
948 SvREFCNT_dec (PL_diehook); 958 SvREFCNT_dec (PL_diehook);
949 SvREFCNT_dec (PL_warnhook); 959 SvREFCNT_dec (PL_warnhook);
950 960
951 SvREFCNT_dec (coro->saved_deffh); 961 SvREFCNT_dec (coro->saved_deffh);
969static int 979static int
970runops_trace (pTHX) 980runops_trace (pTHX)
971{ 981{
972 COP *oldcop = 0; 982 COP *oldcop = 0;
973 int oldcxix = -2; 983 int oldcxix = -2;
974 struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */
975 coro_cctx *cctx = coro->cctx;
976 984
977 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 985 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
978 { 986 {
979 PERL_ASYNC_CHECK (); 987 PERL_ASYNC_CHECK ();
980 988
981 if (cctx->flags & CC_TRACE_ALL) 989 if (cctx_current->flags & CC_TRACE_ALL)
982 { 990 {
983 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)
984 { 992 {
985 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 993 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
986 SV **bot, **top; 994 SV **bot, **top;
987 AV *av = newAV (); /* return values */ 995 AV *av = newAV (); /* return values */
988 SV **cb; 996 SV **cb;
1025 1033
1026 if (PL_curcop != &PL_compiling) 1034 if (PL_curcop != &PL_compiling)
1027 { 1035 {
1028 SV **cb; 1036 SV **cb;
1029 1037
1030 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) 1038 if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB)
1031 { 1039 {
1032 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1040 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1033 1041
1034 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 1042 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
1035 { 1043 {
1058 } 1066 }
1059 1067
1060 oldcxix = cxstack_ix; 1068 oldcxix = cxstack_ix;
1061 } 1069 }
1062 1070
1063 if (cctx->flags & CC_TRACE_LINE) 1071 if (cctx_current->flags & CC_TRACE_LINE)
1064 { 1072 {
1065 dSP; 1073 dSP;
1066 1074
1067 PL_runops = RUNOPS_DEFAULT; 1075 PL_runops = RUNOPS_DEFAULT;
1068 ENTER; 1076 ENTER;
1087 1095
1088 TAINT_NOT; 1096 TAINT_NOT;
1089 return 0; 1097 return 0;
1090} 1098}
1091 1099
1092static struct coro_cctx *cctx_ssl_cctx;
1093static struct CoroSLF cctx_ssl_frame; 1100static struct CoroSLF cctx_ssl_frame;
1094 1101
1095static void 1102static void
1096slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) 1103slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1097{ 1104{
1098 ta->prev = (struct coro *)cctx_ssl_cctx;
1099 ta->next = 0; 1105 ta->prev = 0;
1100} 1106}
1101 1107
1102static int 1108static int
1103slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) 1109slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1104{ 1110{
1107 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 */
1108} 1114}
1109 1115
1110/* 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 */
1111static void NOINLINE 1117static void NOINLINE
1112cctx_prepare (pTHX_ coro_cctx *cctx) 1118cctx_prepare (pTHX)
1113{ 1119{
1114 PL_top_env = &PL_start_env; 1120 PL_top_env = &PL_start_env;
1115 1121
1116 if (cctx->flags & CC_TRACE) 1122 if (cctx_current->flags & CC_TRACE)
1117 PL_runops = runops_trace; 1123 PL_runops = runops_trace;
1118 1124
1119 /* 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
1120 * that can lead to creation of a new cctx */ 1126 * that can lead to creation of a new cctx */
1121 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)",
1122 slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); 1128 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1123 1129
1124 /* 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 */
1125 cctx_ssl_cctx = cctx;
1126 cctx_ssl_frame = slf_frame; 1131 cctx_ssl_frame = slf_frame;
1127 1132
1128 slf_frame.prepare = slf_prepare_set_stacklevel; 1133 slf_frame.prepare = slf_prepare_set_stacklevel;
1129 slf_frame.check = slf_check_set_stacklevel; 1134 slf_frame.check = slf_check_set_stacklevel;
1130} 1135}
1153 /* normally we would need to skip the entersub here */ 1158 /* normally we would need to skip the entersub here */
1154 /* 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 */
1155 /* PL_nop = PL_nop->op_next */ 1160 /* PL_nop = PL_nop->op_next */
1156 1161
1157 /* inject a fake subroutine call to cctx_init */ 1162 /* inject a fake subroutine call to cctx_init */
1158 cctx_prepare (aTHX_ (coro_cctx *)arg); 1163 cctx_prepare (aTHX);
1159 1164
1160 /* cctx_run is the alternative tail of transfer() */ 1165 /* cctx_run is the alternative tail of transfer() */
1161 transfer_tail (aTHX); 1166 transfer_tail (aTHX);
1162 1167
1163 /* 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 */
1171 /* 1176 /*
1172 * 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
1173 * 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)
1174 * 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
1175 * bootstrap-time "top" top_env, as we cannot restore the "main" 1180 * bootstrap-time "top" top_env, as we cannot restore the "main"
1176 * 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.
1177 */ 1184 */
1178 PL_top_env = main_top_env; 1185 PL_top_env = main_top_env;
1179 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 */
1180 } 1187 }
1181} 1188}
1258cctx_destroy (coro_cctx *cctx) 1265cctx_destroy (coro_cctx *cctx)
1259{ 1266{
1260 if (!cctx) 1267 if (!cctx)
1261 return; 1268 return;
1262 1269
1270 assert (cctx != cctx_current);//D temporary
1271
1263 --cctx_count; 1272 --cctx_count;
1264 coro_destroy (&cctx->cctx); 1273 coro_destroy (&cctx->cctx);
1265 1274
1266 /* coro_transfer creates new, empty cctx's */ 1275 /* coro_transfer creates new, empty cctx's */
1267 if (cctx->sptr) 1276 if (cctx->sptr)
1330 /* TODO: throwing up here is considered harmful */ 1339 /* TODO: throwing up here is considered harmful */
1331 1340
1332 if (expect_true (prev != next)) 1341 if (expect_true (prev != next))
1333 { 1342 {
1334 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1343 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,"); 1344 croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,");
1336 1345
1337 if (expect_false (next->flags & CF_RUNNING)) 1346 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,"); 1347 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1339 1348
1340 if (expect_false (next->flags & CF_DESTROYED)) 1349 if (expect_false (next->flags & CF_DESTROYED))
1352transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1361transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1353{ 1362{
1354 dSTACKLEVEL; 1363 dSTACKLEVEL;
1355 1364
1356 /* sometimes transfer is only called to set idle_sp */ 1365 /* sometimes transfer is only called to set idle_sp */
1357 if (expect_false (!next)) 1366 if (expect_false (!prev))
1358 { 1367 {
1359 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1368 cctx_current->idle_sp = STACKLEVEL;
1360 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 */
1361 } 1370 }
1362 else if (expect_true (prev != next)) 1371 else if (expect_true (prev != next))
1363 { 1372 {
1364 coro_cctx *prev__cctx; 1373 coro_cctx *cctx_prev;
1365 1374
1366 if (expect_false (prev->flags & CF_NEW)) 1375 if (expect_false (prev->flags & CF_NEW))
1367 { 1376 {
1368 /* create a new empty/source context */ 1377 /* create a new empty/source context */
1369 prev->cctx = cctx_new_empty ();
1370 prev->flags &= ~CF_NEW; 1378 prev->flags &= ~CF_NEW;
1371 prev->flags |= CF_RUNNING; 1379 prev->flags |= CF_RUNNING;
1372 } 1380 }
1373 1381
1374 prev->flags &= ~CF_RUNNING; 1382 prev->flags &= ~CF_RUNNING;
1385 coro_setup (aTHX_ next); 1393 coro_setup (aTHX_ next);
1386 } 1394 }
1387 else 1395 else
1388 load_perl (aTHX_ next); 1396 load_perl (aTHX_ next);
1389 1397
1390 prev__cctx = prev->cctx; 1398 assert (!prev->cctx);//D temporary
1391 1399
1392 /* possibly untie and reuse the cctx */ 1400 /* possibly untie and reuse the cctx */
1393 if (expect_true ( 1401 if (expect_true (
1394 prev__cctx->idle_sp == STACKLEVEL 1402 cctx_current->idle_sp == STACKLEVEL
1395 && !(prev__cctx->flags & CC_TRACE) 1403 && !(cctx_current->flags & CC_TRACE)
1396 && !force_cctx 1404 && !force_cctx
1397 )) 1405 ))
1398 { 1406 {
1399 /* 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 */
1400 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));
1401 1409
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 */ 1410 /* 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 */ 1411 /* without this the next cctx_get might destroy the running cctx while still in use */
1406 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1412 if (expect_false (CCTX_EXPIRED (cctx_current)))
1407 if (!next->cctx) 1413 if (expect_true (!next->cctx))
1408 next->cctx = cctx_get (aTHX); 1414 next->cctx = cctx_get (aTHX);
1409 1415
1410 cctx_put (prev__cctx); 1416 cctx_put (cctx_current);
1411 } 1417 }
1418 else
1419 prev->cctx = cctx_current;
1412 1420
1413 ++next->usecount; 1421 ++next->usecount;
1414 1422
1415 if (expect_true (!next->cctx)) 1423 cctx_prev = cctx_current;
1416 next->cctx = cctx_get (aTHX); 1424 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1417 1425
1418 if (expect_false (prev__cctx != next->cctx)) 1426 next->cctx = 0;
1427
1428 if (expect_false (cctx_prev != cctx_current))
1419 { 1429 {
1420 prev__cctx->top_env = PL_top_env; 1430 cctx_prev->top_env = PL_top_env;
1421 PL_top_env = next->cctx->top_env; 1431 PL_top_env = cctx_current->top_env;
1422 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1432 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1423 } 1433 }
1424 1434
1425 transfer_tail (aTHX); 1435 transfer_tail (aTHX);
1426 } 1436 }
1427} 1437}
1582{ 1592{
1583 struct coro *coro; 1593 struct coro *coro;
1584 SV *sv_hook; 1594 SV *sv_hook;
1585 void (*xs_hook)(void); 1595 void (*xs_hook)(void);
1586 1596
1587 if (SvROK (coro_sv))
1588 coro_sv = SvRV (coro_sv);
1589
1590 coro = SvSTATE (coro_sv); 1597 coro = SvSTATE (coro_sv);
1591 1598
1592 if (coro->flags & CF_READY) 1599 if (coro->flags & CF_READY)
1593 return 0; 1600 return 0;
1594 1601
1668 } 1675 }
1669 } 1676 }
1670 else 1677 else
1671 { 1678 {
1672 /* 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 {
1673 dSP; 1689 dSP;
1674 1690
1675 ENTER; 1691 ENTER;
1676 SAVETMPS; 1692 SAVETMPS;
1677 1693
1678 PUSHMARK (SP); 1694 PUSHMARK (SP);
1679 PUTBACK; 1695 PUTBACK;
1680 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); 1696 call_sv (sv_idle, G_VOID | G_DISCARD);
1681 1697
1682 FREETMPS; 1698 FREETMPS;
1683 LEAVE; 1699 LEAVE;
1700 }
1684 } 1701 }
1685 } 1702 }
1686} 1703}
1687 1704
1688INLINE void 1705INLINE void
1759static void 1776static void
1760api_trace (pTHX_ SV *coro_sv, int flags) 1777api_trace (pTHX_ SV *coro_sv, int flags)
1761{ 1778{
1762 struct coro *coro = SvSTATE (coro_sv); 1779 struct coro *coro = SvSTATE (coro_sv);
1763 1780
1781 if (coro->flags & CF_RUNNING)
1782 croak ("cannot enable tracing on a running coroutine, caught");
1783
1764 if (flags & CC_TRACE) 1784 if (flags & CC_TRACE)
1765 { 1785 {
1766 if (!coro->cctx) 1786 if (!coro->cctx)
1767 coro->cctx = cctx_new_run (); 1787 coro->cctx = cctx_new_run ();
1768 else if (!(coro->cctx->flags & CC_TRACE)) 1788 else if (!(coro->cctx->flags & CC_TRACE))
1769 croak ("cannot enable tracing on coroutine with custom stack,"); 1789 croak ("cannot enable tracing on coroutine with custom stack, caught");
1770 1790
1771 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1791 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1772 } 1792 }
1773 else if (coro->cctx && coro->cctx->flags & CC_TRACE) 1793 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1774 { 1794 {
2348 SV **ary; 2368 SV **ary;
2349 2369
2350 /* unfortunately, building manually saves memory */ 2370 /* unfortunately, building manually saves memory */
2351 Newx (ary, 2, SV *); 2371 Newx (ary, 2, SV *);
2352 AvALLOC (av) = ary; 2372 AvALLOC (av) = ary;
2373#if PERL_VERSION_ATLEAST (5,10,0)
2353 /*AvARRAY (av) = ary;*/ 2374 AvARRAY (av) = ary;
2375#else
2354 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
2355 AvMAX (av) = 1; 2380 AvMAX (av) = 1;
2356 AvFILLp (av) = 0; 2381 AvFILLp (av) = 0;
2357 ary [0] = newSViv (count); 2382 ary [0] = newSViv (count);
2358 2383
2359 return newRV_noinc ((SV *)av); 2384 return newRV_noinc ((SV *)av);
2746 coro_thx = PERL_GET_CONTEXT; 2771 coro_thx = PERL_GET_CONTEXT;
2747# endif 2772# endif
2748#endif 2773#endif
2749 BOOT_PAGESIZE; 2774 BOOT_PAGESIZE;
2750 2775
2776 cctx_current = cctx_new_empty ();
2777
2751 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 2778 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
2752 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 2779 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
2753 2780
2754 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;
2755 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; 2782 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
2881_exit (int code) 2908_exit (int code)
2882 PROTOTYPE: $ 2909 PROTOTYPE: $
2883 CODE: 2910 CODE:
2884 _exit (code); 2911 _exit (code);
2885 2912
2886#if CORO_CLONE
2887
2888SV * 2913SV *
2889clone (Coro::State coro) 2914clone (Coro::State coro)
2890 CODE: 2915 CODE:
2891{ 2916{
2917#if CORO_CLONE
2892 struct coro *ncoro = coro_clone (coro); 2918 struct coro *ncoro = coro_clone (coro);
2893 MAGIC *mg; 2919 MAGIC *mg;
2894 /* TODO: too much duplication */ 2920 /* TODO: too much duplication */
2895 ncoro->hv = newHV (); 2921 ncoro->hv = newHV ();
2896 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); 2922 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
2897 mg->mg_flags |= MGf_DUP; 2923 mg->mg_flags |= MGf_DUP;
2898 RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv)); 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
2899} 2928}
2900 OUTPUT: 2929 OUTPUT:
2901 RETVAL 2930 RETVAL
2902
2903#endif
2904 2931
2905int 2932int
2906cctx_stacksize (int new_stacksize = 0) 2933cctx_stacksize (int new_stacksize = 0)
2907 PROTOTYPE: ;$ 2934 PROTOTYPE: ;$
2908 CODE: 2935 CODE:
3029 3056
3030SV * 3057SV *
3031has_cctx (Coro::State coro) 3058has_cctx (Coro::State coro)
3032 PROTOTYPE: $ 3059 PROTOTYPE: $
3033 CODE: 3060 CODE:
3034 RETVAL = boolSV (!!coro->cctx); 3061 /* maybe manage the running flag differently */
3062 RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING));
3035 OUTPUT: 3063 OUTPUT:
3036 RETVAL 3064 RETVAL
3037 3065
3038int 3066int
3039is_traced (Coro::State coro) 3067is_traced (Coro::State coro)
3059 3087
3060void 3088void
3061force_cctx () 3089force_cctx ()
3062 PROTOTYPE: 3090 PROTOTYPE:
3063 CODE: 3091 CODE:
3064 SvSTATE_current->cctx->idle_sp = 0; 3092 cctx_current->idle_sp = 0;
3065 3093
3066void 3094void
3067swap_defsv (Coro::State self) 3095swap_defsv (Coro::State self)
3068 PROTOTYPE: $ 3096 PROTOTYPE: $
3069 ALIAS: 3097 ALIAS:
3092 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3120 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3093 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);
3094 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 3122 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3095 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3123 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3096 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);
3097 3126
3098 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);
3099 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3128 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); 3129 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); 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