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.332 by root, Thu Nov 27 12:20:31 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
351 HV *st; 355 HV *st;
352 GV *gvp; 356 GV *gvp;
353 return sv_2cv (sv, &st, &gvp, 0); 357 return sv_2cv (sv, &st, &gvp, 0);
354} 358}
355 359
360/*****************************************************************************/
361/* magic glue */
362
363#define CORO_MAGIC_type_cv 26
364#define CORO_MAGIC_type_state PERL_MAGIC_ext
365
366#define CORO_MAGIC_NN(sv, type) \
367 (expect_true (SvMAGIC (sv)->mg_type == type) \
368 ? SvMAGIC (sv) \
369 : mg_find (sv, type))
370
371#define CORO_MAGIC(sv, type) \
372 (expect_true (SvMAGIC (sv)) \
373 ? CORO_MAGIC_NN (sv, type) \
374 : 0)
375
376#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
377#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
378
379INLINE struct coro *
380SvSTATE_ (pTHX_ SV *coro)
381{
382 HV *stash;
383 MAGIC *mg;
384
385 if (SvROK (coro))
386 coro = SvRV (coro);
387
388 if (expect_false (SvTYPE (coro) != SVt_PVHV))
389 croak ("Coro::State object required");
390
391 stash = SvSTASH (coro);
392 if (expect_false (stash != coro_stash && stash != coro_state_stash))
393 {
394 /* very slow, but rare, check */
395 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
396 croak ("Coro::State object required");
397 }
398
399 mg = CORO_MAGIC_state (coro);
400 return (struct coro *)mg->mg_ptr;
401}
402
403#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
404
405/* faster than SvSTATE, but expects a coroutine hv */
406#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr)
407#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
408
409/*****************************************************************************/
410/* padlist management and caching */
411
356static AV * 412static AV *
357coro_derive_padlist (pTHX_ CV *cv) 413coro_derive_padlist (pTHX_ CV *cv)
358{ 414{
359 AV *padlist = CvPADLIST (cv); 415 AV *padlist = CvPADLIST (cv);
360 AV *newpadlist, *newpad; 416 AV *newpadlist, *newpad;
367 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 423 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
368#endif 424#endif
369 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 425 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
370 --AvFILLp (padlist); 426 --AvFILLp (padlist);
371 427
372 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); 428 av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0]));
373 av_store (newpadlist, 1, (SV *)newpad); 429 av_store (newpadlist, 1, (SV *)newpad);
374 430
375 return newpadlist; 431 return newpadlist;
376} 432}
377 433
378static void 434static void
379free_padlist (pTHX_ AV *padlist) 435free_padlist (pTHX_ AV *padlist)
380{ 436{
381 /* may be during global destruction */ 437 /* may be during global destruction */
382 if (SvREFCNT (padlist)) 438 if (!IN_DESTRUCT)
383 { 439 {
384 I32 i = AvFILLp (padlist); 440 I32 i = AvFILLp (padlist);
385 while (i >= 0) 441
442 while (i > 0) /* special-case index 0 */
386 { 443 {
387 SV **svp = av_fetch (padlist, i--, FALSE); 444 /* we try to be extra-careful here */
388 if (svp) 445 AV *av = (AV *)AvARRAY (padlist)[i--];
389 { 446 I32 j = AvFILLp (av);
390 SV *sv; 447
391 while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) 448 while (j >= 0)
449 SvREFCNT_dec (AvARRAY (av)[j--]);
450
451 AvFILLp (av) = -1;
392 SvREFCNT_dec (sv); 452 SvREFCNT_dec (av);
393
394 SvREFCNT_dec (*svp);
395 }
396 } 453 }
397 454
455 SvREFCNT_dec (AvARRAY (padlist)[0]);
456
457 AvFILLp (padlist) = -1;
398 SvREFCNT_dec ((SV*)padlist); 458 SvREFCNT_dec ((SV*)padlist);
399 } 459 }
400} 460}
401 461
402static int 462static int
411 471
412 SvREFCNT_dec (av); /* sv_magicext increased the refcount */ 472 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
413 473
414 return 0; 474 return 0;
415} 475}
416
417#define CORO_MAGIC_type_cv 26
418#define CORO_MAGIC_type_state PERL_MAGIC_ext
419 476
420static MGVTBL coro_cv_vtbl = { 477static MGVTBL coro_cv_vtbl = {
421 0, 0, 0, 0, 478 0, 0, 0, 0,
422 coro_cv_free 479 coro_cv_free
423}; 480};
424
425#define CORO_MAGIC_NN(sv, type) \
426 (expect_true (SvMAGIC (sv)->mg_type == type) \
427 ? SvMAGIC (sv) \
428 : mg_find (sv, type))
429
430#define CORO_MAGIC(sv, type) \
431 (expect_true (SvMAGIC (sv)) \
432 ? CORO_MAGIC_NN (sv, type) \
433 : 0)
434
435#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
436#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
437
438INLINE struct coro *
439SvSTATE_ (pTHX_ SV *coro)
440{
441 HV *stash;
442 MAGIC *mg;
443
444 if (SvROK (coro))
445 coro = SvRV (coro);
446
447 if (expect_false (SvTYPE (coro) != SVt_PVHV))
448 croak ("Coro::State object required");
449
450 stash = SvSTASH (coro);
451 if (expect_false (stash != coro_stash && stash != coro_state_stash))
452 {
453 /* very slow, but rare, check */
454 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
455 croak ("Coro::State object required");
456 }
457
458 mg = CORO_MAGIC_state (coro);
459 return (struct coro *)mg->mg_ptr;
460}
461
462#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
463
464/* faster than SvSTATE, but expects a coroutine hv */
465#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr)
466#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
467 481
468/* the next two functions merely cache the padlists */ 482/* the next two functions merely cache the padlists */
469static void 483static void
470get_padlist (pTHX_ CV *cv) 484get_padlist (pTHX_ CV *cv)
471{ 485{
499 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); 513 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
500 514
501 av = (AV *)mg->mg_obj; 515 av = (AV *)mg->mg_obj;
502 516
503 if (expect_false (AvFILLp (av) >= AvMAX (av))) 517 if (expect_false (AvFILLp (av) >= AvMAX (av)))
504 av_extend (av, AvMAX (av) + 1); 518 av_extend (av, AvFILLp (av) + 1);
505 519
506 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 520 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
507} 521}
508 522
509/** load & save, init *******************************************************/ 523/** load & save, init *******************************************************/
514 perl_slots *slot = c->slot; 528 perl_slots *slot = c->slot;
515 c->slot = 0; 529 c->slot = 0;
516 530
517 PL_mainstack = c->mainstack; 531 PL_mainstack = c->mainstack;
518 532
519 GvSV (PL_defgv) = slot->defsv; 533 GvSV (PL_defgv) = slot->defsv;
520 GvAV (PL_defgv) = slot->defav; 534 GvAV (PL_defgv) = slot->defav;
521 GvSV (PL_errgv) = slot->errsv; 535 GvSV (PL_errgv) = slot->errsv;
522 GvSV (irsgv) = slot->irsgv; 536 GvSV (irsgv) = slot->irsgv;
537 GvHV (PL_hintgv) = slot->hinthv;
523 538
524 #define VAR(name,type) PL_ ## name = slot->name; 539 #define VAR(name,type) PL_ ## name = slot->name;
525 # include "state.h" 540 # include "state.h"
526 #undef VAR 541 #undef VAR
527 542
613 c->mainstack = PL_mainstack; 628 c->mainstack = PL_mainstack;
614 629
615 { 630 {
616 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 631 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
617 632
618 slot->defav = GvAV (PL_defgv); 633 slot->defav = GvAV (PL_defgv);
619 slot->defsv = DEFSV; 634 slot->defsv = DEFSV;
620 slot->errsv = ERRSV; 635 slot->errsv = ERRSV;
621 slot->irsgv = GvSV (irsgv); 636 slot->irsgv = GvSV (irsgv);
637 slot->hinthv = GvHV (PL_hintgv);
622 638
623 #define VAR(name,type) slot->name = PL_ ## name; 639 #define VAR(name,type) slot->name = PL_ ## name;
624 # include "state.h" 640 # include "state.h"
625 #undef VAR 641 #undef VAR
626 } 642 }
862 878
863 PL_runops = RUNOPS_DEFAULT; 879 PL_runops = RUNOPS_DEFAULT;
864 PL_curcop = &PL_compiling; 880 PL_curcop = &PL_compiling;
865 PL_in_eval = EVAL_NULL; 881 PL_in_eval = EVAL_NULL;
866 PL_comppad = 0; 882 PL_comppad = 0;
883 PL_comppad_name = 0;
884 PL_comppad_name_fill = 0;
885 PL_comppad_name_floor = 0;
867 PL_curpm = 0; 886 PL_curpm = 0;
868 PL_curpad = 0; 887 PL_curpad = 0;
869 PL_localizing = 0; 888 PL_localizing = 0;
870 PL_dirty = 0; 889 PL_dirty = 0;
871 PL_restartop = 0; 890 PL_restartop = 0;
872#if PERL_VERSION_ATLEAST (5,10,0) 891#if PERL_VERSION_ATLEAST (5,10,0)
873 PL_parser = 0; 892 PL_parser = 0;
874#endif 893#endif
894 PL_hints = 0;
875 895
876 /* recreate the die/warn hooks */ 896 /* recreate the die/warn hooks */
877 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); 897 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); 898 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook);
879 899
880 GvSV (PL_defgv) = newSV (0); 900 GvSV (PL_defgv) = newSV (0);
881 GvAV (PL_defgv) = coro->args; coro->args = 0; 901 GvAV (PL_defgv) = coro->args; coro->args = 0;
882 GvSV (PL_errgv) = newSV (0); 902 GvSV (PL_errgv) = newSV (0);
883 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 903 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
904 GvHV (PL_hintgv) = 0;
884 PL_rs = newSVsv (GvSV (irsgv)); 905 PL_rs = newSVsv (GvSV (irsgv));
885 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 906 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
886 907
887 { 908 {
888 dSP; 909 dSP;
942 SvREFCNT_dec (GvAV (PL_defgv)); 963 SvREFCNT_dec (GvAV (PL_defgv));
943 SvREFCNT_dec (GvSV (PL_errgv)); 964 SvREFCNT_dec (GvSV (PL_errgv));
944 SvREFCNT_dec (PL_defoutgv); 965 SvREFCNT_dec (PL_defoutgv);
945 SvREFCNT_dec (PL_rs); 966 SvREFCNT_dec (PL_rs);
946 SvREFCNT_dec (GvSV (irsgv)); 967 SvREFCNT_dec (GvSV (irsgv));
968 SvREFCNT_dec (GvHV (PL_hintgv));
947 969
948 SvREFCNT_dec (PL_diehook); 970 SvREFCNT_dec (PL_diehook);
949 SvREFCNT_dec (PL_warnhook); 971 SvREFCNT_dec (PL_warnhook);
950 972
951 SvREFCNT_dec (coro->saved_deffh); 973 SvREFCNT_dec (coro->saved_deffh);
969static int 991static int
970runops_trace (pTHX) 992runops_trace (pTHX)
971{ 993{
972 COP *oldcop = 0; 994 COP *oldcop = 0;
973 int oldcxix = -2; 995 int oldcxix = -2;
974 struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */
975 coro_cctx *cctx = coro->cctx;
976 996
977 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 997 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
978 { 998 {
979 PERL_ASYNC_CHECK (); 999 PERL_ASYNC_CHECK ();
980 1000
981 if (cctx->flags & CC_TRACE_ALL) 1001 if (cctx_current->flags & CC_TRACE_ALL)
982 { 1002 {
983 if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) 1003 if (PL_op->op_type == OP_LEAVESUB && cctx_current->flags & CC_TRACE_SUB)
984 { 1004 {
985 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1005 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
986 SV **bot, **top; 1006 SV **bot, **top;
987 AV *av = newAV (); /* return values */ 1007 AV *av = newAV (); /* return values */
988 SV **cb; 1008 SV **cb;
1025 1045
1026 if (PL_curcop != &PL_compiling) 1046 if (PL_curcop != &PL_compiling)
1027 { 1047 {
1028 SV **cb; 1048 SV **cb;
1029 1049
1030 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) 1050 if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB)
1031 { 1051 {
1032 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1052 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1033 1053
1034 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 1054 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
1035 { 1055 {
1058 } 1078 }
1059 1079
1060 oldcxix = cxstack_ix; 1080 oldcxix = cxstack_ix;
1061 } 1081 }
1062 1082
1063 if (cctx->flags & CC_TRACE_LINE) 1083 if (cctx_current->flags & CC_TRACE_LINE)
1064 { 1084 {
1065 dSP; 1085 dSP;
1066 1086
1067 PL_runops = RUNOPS_DEFAULT; 1087 PL_runops = RUNOPS_DEFAULT;
1068 ENTER; 1088 ENTER;
1087 1107
1088 TAINT_NOT; 1108 TAINT_NOT;
1089 return 0; 1109 return 0;
1090} 1110}
1091 1111
1092static struct coro_cctx *cctx_ssl_cctx;
1093static struct CoroSLF cctx_ssl_frame; 1112static struct CoroSLF cctx_ssl_frame;
1094 1113
1095static void 1114static void
1096slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) 1115slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1097{ 1116{
1098 ta->prev = (struct coro *)cctx_ssl_cctx;
1099 ta->next = 0; 1117 ta->prev = 0;
1100} 1118}
1101 1119
1102static int 1120static int
1103slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) 1121slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1104{ 1122{
1107 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ 1125 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1108} 1126}
1109 1127
1110/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ 1128/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1111static void NOINLINE 1129static void NOINLINE
1112cctx_prepare (pTHX_ coro_cctx *cctx) 1130cctx_prepare (pTHX)
1113{ 1131{
1114 PL_top_env = &PL_start_env; 1132 PL_top_env = &PL_start_env;
1115 1133
1116 if (cctx->flags & CC_TRACE) 1134 if (cctx_current->flags & CC_TRACE)
1117 PL_runops = runops_trace; 1135 PL_runops = runops_trace;
1118 1136
1119 /* we already must be executing an SLF op, there is no other valid way 1137 /* we already must be executing an SLF op, there is no other valid way
1120 * that can lead to creation of a new cctx */ 1138 * that can lead to creation of a new cctx */
1121 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", 1139 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)",
1122 slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); 1140 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1123 1141
1124 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ 1142 /* 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; 1143 cctx_ssl_frame = slf_frame;
1127 1144
1128 slf_frame.prepare = slf_prepare_set_stacklevel; 1145 slf_frame.prepare = slf_prepare_set_stacklevel;
1129 slf_frame.check = slf_check_set_stacklevel; 1146 slf_frame.check = slf_check_set_stacklevel;
1130} 1147}
1153 /* normally we would need to skip the entersub here */ 1170 /* normally we would need to skip the entersub here */
1154 /* not doing so will re-execute it, which is exactly what we want */ 1171 /* not doing so will re-execute it, which is exactly what we want */
1155 /* PL_nop = PL_nop->op_next */ 1172 /* PL_nop = PL_nop->op_next */
1156 1173
1157 /* inject a fake subroutine call to cctx_init */ 1174 /* inject a fake subroutine call to cctx_init */
1158 cctx_prepare (aTHX_ (coro_cctx *)arg); 1175 cctx_prepare (aTHX);
1159 1176
1160 /* cctx_run is the alternative tail of transfer() */ 1177 /* cctx_run is the alternative tail of transfer() */
1161 transfer_tail (aTHX); 1178 transfer_tail (aTHX);
1162 1179
1163 /* somebody or something will hit me for both perl_run and PL_restartop */ 1180 /* somebody or something will hit me for both perl_run and PL_restartop */
1171 /* 1188 /*
1172 * If perl-run returns we assume exit() was being called or the coro 1189 * 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) 1190 * 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 1191 * 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" 1192 * bootstrap-time "top" top_env, as we cannot restore the "main"
1176 * coroutine as Coro has no such concept 1193 * coroutine as Coro has no such concept.
1194 * This actually isn't valid with the pthread backend, but OSes requiring
1195 * that backend are too broken to do it in a standards-compliant way.
1177 */ 1196 */
1178 PL_top_env = main_top_env; 1197 PL_top_env = main_top_env;
1179 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1198 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1180 } 1199 }
1181} 1200}
1258cctx_destroy (coro_cctx *cctx) 1277cctx_destroy (coro_cctx *cctx)
1259{ 1278{
1260 if (!cctx) 1279 if (!cctx)
1261 return; 1280 return;
1262 1281
1282 assert (cctx != cctx_current);//D temporary
1283
1263 --cctx_count; 1284 --cctx_count;
1264 coro_destroy (&cctx->cctx); 1285 coro_destroy (&cctx->cctx);
1265 1286
1266 /* coro_transfer creates new, empty cctx's */ 1287 /* coro_transfer creates new, empty cctx's */
1267 if (cctx->sptr) 1288 if (cctx->sptr)
1330 /* TODO: throwing up here is considered harmful */ 1351 /* TODO: throwing up here is considered harmful */
1331 1352
1332 if (expect_true (prev != next)) 1353 if (expect_true (prev != next))
1333 { 1354 {
1334 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1355 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,"); 1356 croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,");
1336 1357
1337 if (expect_false (next->flags & CF_RUNNING)) 1358 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,"); 1359 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1339 1360
1340 if (expect_false (next->flags & CF_DESTROYED)) 1361 if (expect_false (next->flags & CF_DESTROYED))
1352transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1373transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1353{ 1374{
1354 dSTACKLEVEL; 1375 dSTACKLEVEL;
1355 1376
1356 /* sometimes transfer is only called to set idle_sp */ 1377 /* sometimes transfer is only called to set idle_sp */
1357 if (expect_false (!next)) 1378 if (expect_false (!prev))
1358 { 1379 {
1359 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1380 cctx_current->idle_sp = STACKLEVEL;
1360 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1381 assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1361 } 1382 }
1362 else if (expect_true (prev != next)) 1383 else if (expect_true (prev != next))
1363 { 1384 {
1364 coro_cctx *prev__cctx; 1385 coro_cctx *cctx_prev;
1365 1386
1366 if (expect_false (prev->flags & CF_NEW)) 1387 if (expect_false (prev->flags & CF_NEW))
1367 { 1388 {
1368 /* create a new empty/source context */ 1389 /* create a new empty/source context */
1369 prev->cctx = cctx_new_empty ();
1370 prev->flags &= ~CF_NEW; 1390 prev->flags &= ~CF_NEW;
1371 prev->flags |= CF_RUNNING; 1391 prev->flags |= CF_RUNNING;
1372 } 1392 }
1373 1393
1374 prev->flags &= ~CF_RUNNING; 1394 prev->flags &= ~CF_RUNNING;
1385 coro_setup (aTHX_ next); 1405 coro_setup (aTHX_ next);
1386 } 1406 }
1387 else 1407 else
1388 load_perl (aTHX_ next); 1408 load_perl (aTHX_ next);
1389 1409
1390 prev__cctx = prev->cctx; 1410 assert (!prev->cctx);//D temporary
1391 1411
1392 /* possibly untie and reuse the cctx */ 1412 /* possibly untie and reuse the cctx */
1393 if (expect_true ( 1413 if (expect_true (
1394 prev__cctx->idle_sp == STACKLEVEL 1414 cctx_current->idle_sp == STACKLEVEL
1395 && !(prev__cctx->flags & CC_TRACE) 1415 && !(cctx_current->flags & CC_TRACE)
1396 && !force_cctx 1416 && !force_cctx
1397 )) 1417 ))
1398 { 1418 {
1399 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1419 /* 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)); 1420 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te));
1401 1421
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 */ 1422 /* 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 */ 1423 /* without this the next cctx_get might destroy the running cctx while still in use */
1406 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1424 if (expect_false (CCTX_EXPIRED (cctx_current)))
1407 if (!next->cctx) 1425 if (expect_true (!next->cctx))
1408 next->cctx = cctx_get (aTHX); 1426 next->cctx = cctx_get (aTHX);
1409 1427
1410 cctx_put (prev__cctx); 1428 cctx_put (cctx_current);
1411 } 1429 }
1430 else
1431 prev->cctx = cctx_current;
1412 1432
1413 ++next->usecount; 1433 ++next->usecount;
1414 1434
1415 if (expect_true (!next->cctx)) 1435 cctx_prev = cctx_current;
1416 next->cctx = cctx_get (aTHX); 1436 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1417 1437
1418 if (expect_false (prev__cctx != next->cctx)) 1438 next->cctx = 0;
1439
1440 if (expect_false (cctx_prev != cctx_current))
1419 { 1441 {
1420 prev__cctx->top_env = PL_top_env; 1442 cctx_prev->top_env = PL_top_env;
1421 PL_top_env = next->cctx->top_env; 1443 PL_top_env = cctx_current->top_env;
1422 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1444 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1423 } 1445 }
1424 1446
1425 transfer_tail (aTHX); 1447 transfer_tail (aTHX);
1426 } 1448 }
1427} 1449}
1582{ 1604{
1583 struct coro *coro; 1605 struct coro *coro;
1584 SV *sv_hook; 1606 SV *sv_hook;
1585 void (*xs_hook)(void); 1607 void (*xs_hook)(void);
1586 1608
1587 if (SvROK (coro_sv))
1588 coro_sv = SvRV (coro_sv);
1589
1590 coro = SvSTATE (coro_sv); 1609 coro = SvSTATE (coro_sv);
1591 1610
1592 if (coro->flags & CF_READY) 1611 if (coro->flags & CF_READY)
1593 return 0; 1612 return 0;
1594 1613
1668 } 1687 }
1669 } 1688 }
1670 else 1689 else
1671 { 1690 {
1672 /* nothing to schedule: call the idle handler */ 1691 /* nothing to schedule: call the idle handler */
1692 if (SvROK (sv_idle)
1693 && SvOBJECT (SvRV (sv_idle)))
1694 {
1695 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1696 api_ready (aTHX_ SvRV (sv_idle));
1697 --coro_nready;
1698 }
1699 else
1700 {
1673 dSP; 1701 dSP;
1674 1702
1675 ENTER; 1703 ENTER;
1676 SAVETMPS; 1704 SAVETMPS;
1677 1705
1678 PUSHMARK (SP); 1706 PUSHMARK (SP);
1679 PUTBACK; 1707 PUTBACK;
1680 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); 1708 call_sv (sv_idle, G_VOID | G_DISCARD);
1681 1709
1682 FREETMPS; 1710 FREETMPS;
1683 LEAVE; 1711 LEAVE;
1712 }
1684 } 1713 }
1685 } 1714 }
1686} 1715}
1687 1716
1688INLINE void 1717INLINE void
1759static void 1788static void
1760api_trace (pTHX_ SV *coro_sv, int flags) 1789api_trace (pTHX_ SV *coro_sv, int flags)
1761{ 1790{
1762 struct coro *coro = SvSTATE (coro_sv); 1791 struct coro *coro = SvSTATE (coro_sv);
1763 1792
1793 if (coro->flags & CF_RUNNING)
1794 croak ("cannot enable tracing on a running coroutine, caught");
1795
1764 if (flags & CC_TRACE) 1796 if (flags & CC_TRACE)
1765 { 1797 {
1766 if (!coro->cctx) 1798 if (!coro->cctx)
1767 coro->cctx = cctx_new_run (); 1799 coro->cctx = cctx_new_run ();
1768 else if (!(coro->cctx->flags & CC_TRACE)) 1800 else if (!(coro->cctx->flags & CC_TRACE))
1769 croak ("cannot enable tracing on coroutine with custom stack,"); 1801 croak ("cannot enable tracing on coroutine with custom stack, caught");
1770 1802
1771 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1803 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1772 } 1804 }
1773 else if (coro->cctx && coro->cctx->flags & CC_TRACE) 1805 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1774 { 1806 {
2348 SV **ary; 2380 SV **ary;
2349 2381
2350 /* unfortunately, building manually saves memory */ 2382 /* unfortunately, building manually saves memory */
2351 Newx (ary, 2, SV *); 2383 Newx (ary, 2, SV *);
2352 AvALLOC (av) = ary; 2384 AvALLOC (av) = ary;
2385#if PERL_VERSION_ATLEAST (5,10,0)
2353 /*AvARRAY (av) = ary;*/ 2386 AvARRAY (av) = ary;
2387#else
2354 SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ 2388 /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */
2389 /* -DDEBUGGING flags this as a bug, despite it perfectly working */
2390 SvPVX ((SV *)av) = (char *)ary;
2391#endif
2355 AvMAX (av) = 1; 2392 AvMAX (av) = 1;
2356 AvFILLp (av) = 0; 2393 AvFILLp (av) = 0;
2357 ary [0] = newSViv (count); 2394 ary [0] = newSViv (count);
2358 2395
2359 return newRV_noinc ((SV *)av); 2396 return newRV_noinc ((SV *)av);
2746 coro_thx = PERL_GET_CONTEXT; 2783 coro_thx = PERL_GET_CONTEXT;
2747# endif 2784# endif
2748#endif 2785#endif
2749 BOOT_PAGESIZE; 2786 BOOT_PAGESIZE;
2750 2787
2788 cctx_current = cctx_new_empty ();
2789
2751 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 2790 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
2752 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 2791 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
2753 2792
2754 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; 2793 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; 2794 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
2881_exit (int code) 2920_exit (int code)
2882 PROTOTYPE: $ 2921 PROTOTYPE: $
2883 CODE: 2922 CODE:
2884 _exit (code); 2923 _exit (code);
2885 2924
2886#if CORO_CLONE
2887
2888SV * 2925SV *
2889clone (Coro::State coro) 2926clone (Coro::State coro)
2890 CODE: 2927 CODE:
2891{ 2928{
2929#if CORO_CLONE
2892 struct coro *ncoro = coro_clone (coro); 2930 struct coro *ncoro = coro_clone (coro);
2893 MAGIC *mg; 2931 MAGIC *mg;
2894 /* TODO: too much duplication */ 2932 /* TODO: too much duplication */
2895 ncoro->hv = newHV (); 2933 ncoro->hv = newHV ();
2896 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); 2934 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
2897 mg->mg_flags |= MGf_DUP; 2935 mg->mg_flags |= MGf_DUP;
2898 RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv)); 2936 RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv));
2937#else
2938 croak ("Coro::State->clone has not been configured into this installation of Coro, realised");
2939#endif
2899} 2940}
2900 OUTPUT: 2941 OUTPUT:
2901 RETVAL 2942 RETVAL
2902
2903#endif
2904 2943
2905int 2944int
2906cctx_stacksize (int new_stacksize = 0) 2945cctx_stacksize (int new_stacksize = 0)
2907 PROTOTYPE: ;$ 2946 PROTOTYPE: ;$
2908 CODE: 2947 CODE:
3029 3068
3030SV * 3069SV *
3031has_cctx (Coro::State coro) 3070has_cctx (Coro::State coro)
3032 PROTOTYPE: $ 3071 PROTOTYPE: $
3033 CODE: 3072 CODE:
3034 RETVAL = boolSV (!!coro->cctx); 3073 /* maybe manage the running flag differently */
3074 RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING));
3035 OUTPUT: 3075 OUTPUT:
3036 RETVAL 3076 RETVAL
3037 3077
3038int 3078int
3039is_traced (Coro::State coro) 3079is_traced (Coro::State coro)
3059 3099
3060void 3100void
3061force_cctx () 3101force_cctx ()
3062 PROTOTYPE: 3102 PROTOTYPE:
3063 CODE: 3103 CODE:
3064 SvSTATE_current->cctx->idle_sp = 0; 3104 cctx_current->idle_sp = 0;
3065 3105
3066void 3106void
3067swap_defsv (Coro::State self) 3107swap_defsv (Coro::State self)
3068 PROTOTYPE: $ 3108 PROTOTYPE: $
3069 ALIAS: 3109 ALIAS:
3092 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3132 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3093 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3133 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); 3134 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3095 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3135 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3096 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3136 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3137 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3097 3138
3098 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3139 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); 3140 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); 3141 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); 3142 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