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.339 by root, Mon Dec 15 00:28:30 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
280 284
281 /* async_pool */ 285 /* async_pool */
282 SV *saved_deffh; 286 SV *saved_deffh;
283 SV *invoke_cb; 287 SV *invoke_cb;
284 AV *invoke_av; 288 AV *invoke_av;
289
290 /* on_enter/on_leave */
291 AV *on_enter;
292 AV *on_leave;
285 293
286 /* linked list */ 294 /* linked list */
287 struct coro *next, *prev; 295 struct coro *next, *prev;
288}; 296};
289 297
348INLINE CV * 356INLINE CV *
349coro_sv_2cv (pTHX_ SV *sv) 357coro_sv_2cv (pTHX_ SV *sv)
350{ 358{
351 HV *st; 359 HV *st;
352 GV *gvp; 360 GV *gvp;
353 return sv_2cv (sv, &st, &gvp, 0); 361 CV *cv = sv_2cv (sv, &st, &gvp, 0);
362
363 if (!cv)
364 croak ("code reference expected");
365
366 return cv;
354} 367}
368
369/*****************************************************************************/
370/* magic glue */
371
372#define CORO_MAGIC_type_cv 26
373#define CORO_MAGIC_type_state PERL_MAGIC_ext
374
375#define CORO_MAGIC_NN(sv, type) \
376 (expect_true (SvMAGIC (sv)->mg_type == type) \
377 ? SvMAGIC (sv) \
378 : mg_find (sv, type))
379
380#define CORO_MAGIC(sv, type) \
381 (expect_true (SvMAGIC (sv)) \
382 ? CORO_MAGIC_NN (sv, type) \
383 : 0)
384
385#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
386#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
387
388INLINE struct coro *
389SvSTATE_ (pTHX_ SV *coro)
390{
391 HV *stash;
392 MAGIC *mg;
393
394 if (SvROK (coro))
395 coro = SvRV (coro);
396
397 if (expect_false (SvTYPE (coro) != SVt_PVHV))
398 croak ("Coro::State object required");
399
400 stash = SvSTASH (coro);
401 if (expect_false (stash != coro_stash && stash != coro_state_stash))
402 {
403 /* very slow, but rare, check */
404 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
405 croak ("Coro::State object required");
406 }
407
408 mg = CORO_MAGIC_state (coro);
409 return (struct coro *)mg->mg_ptr;
410}
411
412#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
413
414/* faster than SvSTATE, but expects a coroutine hv */
415#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr)
416#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
417
418/*****************************************************************************/
419/* padlist management and caching */
355 420
356static AV * 421static AV *
357coro_derive_padlist (pTHX_ CV *cv) 422coro_derive_padlist (pTHX_ CV *cv)
358{ 423{
359 AV *padlist = CvPADLIST (cv); 424 AV *padlist = CvPADLIST (cv);
367 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 432 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
368#endif 433#endif
369 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 434 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
370 --AvFILLp (padlist); 435 --AvFILLp (padlist);
371 436
372 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); 437 av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0]));
373 av_store (newpadlist, 1, (SV *)newpad); 438 av_store (newpadlist, 1, (SV *)newpad);
374 439
375 return newpadlist; 440 return newpadlist;
376} 441}
377 442
378static void 443static void
379free_padlist (pTHX_ AV *padlist) 444free_padlist (pTHX_ AV *padlist)
380{ 445{
381 /* may be during global destruction */ 446 /* may be during global destruction */
382 if (SvREFCNT (padlist)) 447 if (!IN_DESTRUCT)
383 { 448 {
384 I32 i = AvFILLp (padlist); 449 I32 i = AvFILLp (padlist);
385 while (i >= 0) 450
451 while (i > 0) /* special-case index 0 */
386 { 452 {
387 SV **svp = av_fetch (padlist, i--, FALSE); 453 /* we try to be extra-careful here */
388 if (svp) 454 AV *av = (AV *)AvARRAY (padlist)[i--];
389 { 455 I32 j = AvFILLp (av);
390 SV *sv; 456
391 while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) 457 while (j >= 0)
458 SvREFCNT_dec (AvARRAY (av)[j--]);
459
460 AvFILLp (av) = -1;
392 SvREFCNT_dec (sv); 461 SvREFCNT_dec (av);
393
394 SvREFCNT_dec (*svp);
395 }
396 } 462 }
397 463
464 SvREFCNT_dec (AvARRAY (padlist)[0]);
465
466 AvFILLp (padlist) = -1;
398 SvREFCNT_dec ((SV*)padlist); 467 SvREFCNT_dec ((SV*)padlist);
399 } 468 }
400} 469}
401 470
402static int 471static int
411 480
412 SvREFCNT_dec (av); /* sv_magicext increased the refcount */ 481 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
413 482
414 return 0; 483 return 0;
415} 484}
416
417#define CORO_MAGIC_type_cv 26
418#define CORO_MAGIC_type_state PERL_MAGIC_ext
419 485
420static MGVTBL coro_cv_vtbl = { 486static MGVTBL coro_cv_vtbl = {
421 0, 0, 0, 0, 487 0, 0, 0, 0,
422 coro_cv_free 488 coro_cv_free
423}; 489};
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 490
468/* the next two functions merely cache the padlists */ 491/* the next two functions merely cache the padlists */
469static void 492static void
470get_padlist (pTHX_ CV *cv) 493get_padlist (pTHX_ CV *cv)
471{ 494{
499 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); 522 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
500 523
501 av = (AV *)mg->mg_obj; 524 av = (AV *)mg->mg_obj;
502 525
503 if (expect_false (AvFILLp (av) >= AvMAX (av))) 526 if (expect_false (AvFILLp (av) >= AvMAX (av)))
504 av_extend (av, AvMAX (av) + 1); 527 av_extend (av, AvFILLp (av) + 1);
505 528
506 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 529 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
507} 530}
508 531
509/** load & save, init *******************************************************/ 532/** load & save, init *******************************************************/
533
534static void
535on_enterleave_call (pTHX_ SV *cb);
510 536
511static void 537static void
512load_perl (pTHX_ Coro__State c) 538load_perl (pTHX_ Coro__State c)
513{ 539{
514 perl_slots *slot = c->slot; 540 perl_slots *slot = c->slot;
515 c->slot = 0; 541 c->slot = 0;
516 542
517 PL_mainstack = c->mainstack; 543 PL_mainstack = c->mainstack;
518 544
519 GvSV (PL_defgv) = slot->defsv; 545 GvSV (PL_defgv) = slot->defsv;
520 GvAV (PL_defgv) = slot->defav; 546 GvAV (PL_defgv) = slot->defav;
521 GvSV (PL_errgv) = slot->errsv; 547 GvSV (PL_errgv) = slot->errsv;
522 GvSV (irsgv) = slot->irsgv; 548 GvSV (irsgv) = slot->irsgv;
549 GvHV (PL_hintgv) = slot->hinthv;
523 550
524 #define VAR(name,type) PL_ ## name = slot->name; 551 #define VAR(name,type) PL_ ## name = slot->name;
525 # include "state.h" 552 # include "state.h"
526 #undef VAR 553 #undef VAR
527 554
541 PUTBACK; 568 PUTBACK;
542 } 569 }
543 570
544 slf_frame = c->slf_frame; 571 slf_frame = c->slf_frame;
545 CORO_THROW = c->except; 572 CORO_THROW = c->except;
573
574 if (expect_false (c->on_enter))
575 {
576 int i;
577
578 for (i = 0; i <= AvFILLp (c->on_enter); ++i)
579 on_enterleave_call (AvARRAY (c->on_enter)[i]);
580 }
546} 581}
547 582
548static void 583static void
549save_perl (pTHX_ Coro__State c) 584save_perl (pTHX_ Coro__State c)
550{ 585{
586 if (expect_false (c->on_leave))
587 {
588 int i;
589
590 for (i = AvFILLp (c->on_leave); i >= 0; --i)
591 on_enterleave_call (AvARRAY (c->on_leave)[i]);
592 }
593
551 c->except = CORO_THROW; 594 c->except = CORO_THROW;
552 c->slf_frame = slf_frame; 595 c->slf_frame = slf_frame;
553 596
554 { 597 {
555 dSP; 598 dSP;
613 c->mainstack = PL_mainstack; 656 c->mainstack = PL_mainstack;
614 657
615 { 658 {
616 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 659 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
617 660
618 slot->defav = GvAV (PL_defgv); 661 slot->defav = GvAV (PL_defgv);
619 slot->defsv = DEFSV; 662 slot->defsv = DEFSV;
620 slot->errsv = ERRSV; 663 slot->errsv = ERRSV;
621 slot->irsgv = GvSV (irsgv); 664 slot->irsgv = GvSV (irsgv);
665 slot->hinthv = GvHV (PL_hintgv);
622 666
623 #define VAR(name,type) slot->name = PL_ ## name; 667 #define VAR(name,type) slot->name = PL_ ## name;
624 # include "state.h" 668 # include "state.h"
625 #undef VAR 669 #undef VAR
626 } 670 }
753#endif 797#endif
754 798
755/* 799/*
756 * This overrides the default magic get method of %SIG elements. 800 * This overrides the default magic get method of %SIG elements.
757 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook 801 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
758 * and instead of tryign to save and restore the hash elements, we just provide 802 * and instead of trying to save and restore the hash elements, we just provide
759 * readback here. 803 * readback here.
760 * We only do this when the hook is != 0, as they are often set to 0 temporarily,
761 * not expecting this to actually change the hook. This is a potential problem
762 * when a schedule happens then, but we ignore this.
763 */ 804 */
764static int 805static int
765coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) 806coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
766{ 807{
767 const char *s = MgPV_nolen_const (mg); 808 const char *s = MgPV_nolen_const (mg);
820 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 861 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
821 862
822 if (svp) 863 if (svp)
823 { 864 {
824 SV *old = *svp; 865 SV *old = *svp;
825 *svp = newSVsv (sv); 866 *svp = SvOK (sv) ? newSVsv (sv) : 0;
826 SvREFCNT_dec (old); 867 SvREFCNT_dec (old);
827 return 0; 868 return 0;
828 } 869 }
829 } 870 }
830 871
862 903
863 PL_runops = RUNOPS_DEFAULT; 904 PL_runops = RUNOPS_DEFAULT;
864 PL_curcop = &PL_compiling; 905 PL_curcop = &PL_compiling;
865 PL_in_eval = EVAL_NULL; 906 PL_in_eval = EVAL_NULL;
866 PL_comppad = 0; 907 PL_comppad = 0;
908 PL_comppad_name = 0;
909 PL_comppad_name_fill = 0;
910 PL_comppad_name_floor = 0;
867 PL_curpm = 0; 911 PL_curpm = 0;
868 PL_curpad = 0; 912 PL_curpad = 0;
869 PL_localizing = 0; 913 PL_localizing = 0;
870 PL_dirty = 0; 914 PL_dirty = 0;
871 PL_restartop = 0; 915 PL_restartop = 0;
872#if PERL_VERSION_ATLEAST (5,10,0) 916#if PERL_VERSION_ATLEAST (5,10,0)
873 PL_parser = 0; 917 PL_parser = 0;
874#endif 918#endif
919 PL_hints = 0;
875 920
876 /* recreate the die/warn hooks */ 921 /* recreate the die/warn hooks */
877 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); 922 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); 923 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook);
879 924
880 GvSV (PL_defgv) = newSV (0); 925 GvSV (PL_defgv) = newSV (0);
881 GvAV (PL_defgv) = coro->args; coro->args = 0; 926 GvAV (PL_defgv) = coro->args; coro->args = 0;
882 GvSV (PL_errgv) = newSV (0); 927 GvSV (PL_errgv) = newSV (0);
883 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 928 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
929 GvHV (PL_hintgv) = 0;
884 PL_rs = newSVsv (GvSV (irsgv)); 930 PL_rs = newSVsv (GvSV (irsgv));
885 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 931 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
886 932
887 { 933 {
888 dSP; 934 dSP;
917 /* copy throw, in case it was set before coro_setup */ 963 /* copy throw, in case it was set before coro_setup */
918 CORO_THROW = coro->except; 964 CORO_THROW = coro->except;
919} 965}
920 966
921static void 967static void
922coro_destruct (pTHX_ struct coro *coro) 968coro_unwind_stacks (pTHX)
923{ 969{
924 if (!IN_DESTRUCT) 970 if (!IN_DESTRUCT)
925 { 971 {
926 /* restore all saved variables and stuff */ 972 /* restore all saved variables and stuff */
927 LEAVE_SCOPE (0); 973 LEAVE_SCOPE (0);
935 POPSTACK_TO (PL_mainstack); 981 POPSTACK_TO (PL_mainstack);
936 982
937 /* unwind main stack */ 983 /* unwind main stack */
938 dounwind (-1); 984 dounwind (-1);
939 } 985 }
986}
987
988static void
989coro_destruct_perl (pTHX_ struct coro *coro)
990{
991 coro_unwind_stacks (aTHX);
940 992
941 SvREFCNT_dec (GvSV (PL_defgv)); 993 SvREFCNT_dec (GvSV (PL_defgv));
942 SvREFCNT_dec (GvAV (PL_defgv)); 994 SvREFCNT_dec (GvAV (PL_defgv));
943 SvREFCNT_dec (GvSV (PL_errgv)); 995 SvREFCNT_dec (GvSV (PL_errgv));
944 SvREFCNT_dec (PL_defoutgv); 996 SvREFCNT_dec (PL_defoutgv);
945 SvREFCNT_dec (PL_rs); 997 SvREFCNT_dec (PL_rs);
946 SvREFCNT_dec (GvSV (irsgv)); 998 SvREFCNT_dec (GvSV (irsgv));
999 SvREFCNT_dec (GvHV (PL_hintgv));
947 1000
948 SvREFCNT_dec (PL_diehook); 1001 SvREFCNT_dec (PL_diehook);
949 SvREFCNT_dec (PL_warnhook); 1002 SvREFCNT_dec (PL_warnhook);
950 1003
951 SvREFCNT_dec (coro->saved_deffh); 1004 SvREFCNT_dec (coro->saved_deffh);
969static int 1022static int
970runops_trace (pTHX) 1023runops_trace (pTHX)
971{ 1024{
972 COP *oldcop = 0; 1025 COP *oldcop = 0;
973 int oldcxix = -2; 1026 int oldcxix = -2;
974 struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */
975 coro_cctx *cctx = coro->cctx;
976 1027
977 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 1028 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
978 { 1029 {
979 PERL_ASYNC_CHECK (); 1030 PERL_ASYNC_CHECK ();
980 1031
981 if (cctx->flags & CC_TRACE_ALL) 1032 if (cctx_current->flags & CC_TRACE_ALL)
982 { 1033 {
983 if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) 1034 if (PL_op->op_type == OP_LEAVESUB && cctx_current->flags & CC_TRACE_SUB)
984 { 1035 {
985 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1036 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
986 SV **bot, **top; 1037 SV **bot, **top;
987 AV *av = newAV (); /* return values */ 1038 AV *av = newAV (); /* return values */
988 SV **cb; 1039 SV **cb;
1025 1076
1026 if (PL_curcop != &PL_compiling) 1077 if (PL_curcop != &PL_compiling)
1027 { 1078 {
1028 SV **cb; 1079 SV **cb;
1029 1080
1030 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) 1081 if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB)
1031 { 1082 {
1032 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1083 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1033 1084
1034 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 1085 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
1035 { 1086 {
1058 } 1109 }
1059 1110
1060 oldcxix = cxstack_ix; 1111 oldcxix = cxstack_ix;
1061 } 1112 }
1062 1113
1063 if (cctx->flags & CC_TRACE_LINE) 1114 if (cctx_current->flags & CC_TRACE_LINE)
1064 { 1115 {
1065 dSP; 1116 dSP;
1066 1117
1067 PL_runops = RUNOPS_DEFAULT; 1118 PL_runops = RUNOPS_DEFAULT;
1068 ENTER; 1119 ENTER;
1087 1138
1088 TAINT_NOT; 1139 TAINT_NOT;
1089 return 0; 1140 return 0;
1090} 1141}
1091 1142
1092static struct coro_cctx *cctx_ssl_cctx;
1093static struct CoroSLF cctx_ssl_frame; 1143static struct CoroSLF cctx_ssl_frame;
1094 1144
1095static void 1145static void
1096slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) 1146slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1097{ 1147{
1098 ta->prev = (struct coro *)cctx_ssl_cctx;
1099 ta->next = 0; 1148 ta->prev = 0;
1100} 1149}
1101 1150
1102static int 1151static int
1103slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) 1152slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1104{ 1153{
1107 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ 1156 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1108} 1157}
1109 1158
1110/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ 1159/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1111static void NOINLINE 1160static void NOINLINE
1112cctx_prepare (pTHX_ coro_cctx *cctx) 1161cctx_prepare (pTHX)
1113{ 1162{
1114 PL_top_env = &PL_start_env; 1163 PL_top_env = &PL_start_env;
1115 1164
1116 if (cctx->flags & CC_TRACE) 1165 if (cctx_current->flags & CC_TRACE)
1117 PL_runops = runops_trace; 1166 PL_runops = runops_trace;
1118 1167
1119 /* we already must be executing an SLF op, there is no other valid way 1168 /* we already must be executing an SLF op, there is no other valid way
1120 * that can lead to creation of a new cctx */ 1169 * that can lead to creation of a new cctx */
1121 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", 1170 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)",
1122 slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); 1171 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1123 1172
1124 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ 1173 /* 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; 1174 cctx_ssl_frame = slf_frame;
1127 1175
1128 slf_frame.prepare = slf_prepare_set_stacklevel; 1176 slf_frame.prepare = slf_prepare_set_stacklevel;
1129 slf_frame.check = slf_check_set_stacklevel; 1177 slf_frame.check = slf_check_set_stacklevel;
1130} 1178}
1153 /* normally we would need to skip the entersub here */ 1201 /* normally we would need to skip the entersub here */
1154 /* not doing so will re-execute it, which is exactly what we want */ 1202 /* not doing so will re-execute it, which is exactly what we want */
1155 /* PL_nop = PL_nop->op_next */ 1203 /* PL_nop = PL_nop->op_next */
1156 1204
1157 /* inject a fake subroutine call to cctx_init */ 1205 /* inject a fake subroutine call to cctx_init */
1158 cctx_prepare (aTHX_ (coro_cctx *)arg); 1206 cctx_prepare (aTHX);
1159 1207
1160 /* cctx_run is the alternative tail of transfer() */ 1208 /* cctx_run is the alternative tail of transfer() */
1161 transfer_tail (aTHX); 1209 transfer_tail (aTHX);
1162 1210
1163 /* somebody or something will hit me for both perl_run and PL_restartop */ 1211 /* somebody or something will hit me for both perl_run and PL_restartop */
1171 /* 1219 /*
1172 * If perl-run returns we assume exit() was being called or the coro 1220 * 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) 1221 * 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 1222 * 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" 1223 * bootstrap-time "top" top_env, as we cannot restore the "main"
1176 * coroutine as Coro has no such concept 1224 * coroutine as Coro has no such concept.
1225 * This actually isn't valid with the pthread backend, but OSes requiring
1226 * that backend are too broken to do it in a standards-compliant way.
1177 */ 1227 */
1178 PL_top_env = main_top_env; 1228 PL_top_env = main_top_env;
1179 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1229 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1180 } 1230 }
1181} 1231}
1258cctx_destroy (coro_cctx *cctx) 1308cctx_destroy (coro_cctx *cctx)
1259{ 1309{
1260 if (!cctx) 1310 if (!cctx)
1261 return; 1311 return;
1262 1312
1313 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary?
1314
1263 --cctx_count; 1315 --cctx_count;
1264 coro_destroy (&cctx->cctx); 1316 coro_destroy (&cctx->cctx);
1265 1317
1266 /* coro_transfer creates new, empty cctx's */ 1318 /* coro_transfer creates new, empty cctx's */
1267 if (cctx->sptr) 1319 if (cctx->sptr)
1330 /* TODO: throwing up here is considered harmful */ 1382 /* TODO: throwing up here is considered harmful */
1331 1383
1332 if (expect_true (prev != next)) 1384 if (expect_true (prev != next))
1333 { 1385 {
1334 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1386 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,"); 1387 croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,");
1336 1388
1337 if (expect_false (next->flags & CF_RUNNING)) 1389 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,"); 1390 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1339 1391
1340 if (expect_false (next->flags & CF_DESTROYED)) 1392 if (expect_false (next->flags & CF_DESTROYED))
1352transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1404transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1353{ 1405{
1354 dSTACKLEVEL; 1406 dSTACKLEVEL;
1355 1407
1356 /* sometimes transfer is only called to set idle_sp */ 1408 /* sometimes transfer is only called to set idle_sp */
1357 if (expect_false (!next)) 1409 if (expect_false (!prev))
1358 { 1410 {
1359 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1411 cctx_current->idle_sp = STACKLEVEL;
1360 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1412 assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1361 } 1413 }
1362 else if (expect_true (prev != next)) 1414 else if (expect_true (prev != next))
1363 { 1415 {
1364 coro_cctx *prev__cctx; 1416 coro_cctx *cctx_prev;
1365 1417
1366 if (expect_false (prev->flags & CF_NEW)) 1418 if (expect_false (prev->flags & CF_NEW))
1367 { 1419 {
1368 /* create a new empty/source context */ 1420 /* create a new empty/source context */
1369 prev->cctx = cctx_new_empty ();
1370 prev->flags &= ~CF_NEW; 1421 prev->flags &= ~CF_NEW;
1371 prev->flags |= CF_RUNNING; 1422 prev->flags |= CF_RUNNING;
1372 } 1423 }
1373 1424
1374 prev->flags &= ~CF_RUNNING; 1425 prev->flags &= ~CF_RUNNING;
1385 coro_setup (aTHX_ next); 1436 coro_setup (aTHX_ next);
1386 } 1437 }
1387 else 1438 else
1388 load_perl (aTHX_ next); 1439 load_perl (aTHX_ next);
1389 1440
1390 prev__cctx = prev->cctx;
1391
1392 /* possibly untie and reuse the cctx */ 1441 /* possibly untie and reuse the cctx */
1393 if (expect_true ( 1442 if (expect_true (
1394 prev__cctx->idle_sp == STACKLEVEL 1443 cctx_current->idle_sp == STACKLEVEL
1395 && !(prev__cctx->flags & CC_TRACE) 1444 && !(cctx_current->flags & CC_TRACE)
1396 && !force_cctx 1445 && !force_cctx
1397 )) 1446 ))
1398 { 1447 {
1399 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1448 /* 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)); 1449 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te));
1401 1450
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 */ 1451 /* 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 */ 1452 /* without this the next cctx_get might destroy the running cctx while still in use */
1406 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1453 if (expect_false (CCTX_EXPIRED (cctx_current)))
1407 if (!next->cctx) 1454 if (expect_true (!next->cctx))
1408 next->cctx = cctx_get (aTHX); 1455 next->cctx = cctx_get (aTHX);
1409 1456
1410 cctx_put (prev__cctx); 1457 cctx_put (cctx_current);
1411 } 1458 }
1459 else
1460 prev->cctx = cctx_current;
1412 1461
1413 ++next->usecount; 1462 ++next->usecount;
1414 1463
1415 if (expect_true (!next->cctx)) 1464 cctx_prev = cctx_current;
1416 next->cctx = cctx_get (aTHX); 1465 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1417 1466
1418 if (expect_false (prev__cctx != next->cctx)) 1467 next->cctx = 0;
1468
1469 if (expect_false (cctx_prev != cctx_current))
1419 { 1470 {
1420 prev__cctx->top_env = PL_top_env; 1471 cctx_prev->top_env = PL_top_env;
1421 PL_top_env = next->cctx->top_env; 1472 PL_top_env = cctx_current->top_env;
1422 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1473 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1423 } 1474 }
1424 1475
1425 transfer_tail (aTHX); 1476 transfer_tail (aTHX);
1426 } 1477 }
1427} 1478}
1449 --coro_nready; 1500 --coro_nready;
1450 } 1501 }
1451 else 1502 else
1452 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ 1503 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
1453 1504
1454 if (coro->mainstack && coro->mainstack != main_mainstack) 1505 if (coro->mainstack
1506 && coro->mainstack != main_mainstack
1507 && coro->slot
1508 && !PL_dirty)
1455 { 1509 {
1456 struct coro temp; 1510 struct coro *current = SvSTATE_current;
1457 1511
1458 assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING))); 1512 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack));
1459 1513
1460 save_perl (aTHX_ &temp); 1514 save_perl (aTHX_ current);
1461 load_perl (aTHX_ coro); 1515 load_perl (aTHX_ coro);
1462 1516
1463 coro_destruct (aTHX_ coro); 1517 coro_destruct_perl (aTHX_ coro);
1464 1518
1465 load_perl (aTHX_ &temp); 1519 load_perl (aTHX_ current);
1466 1520
1467 coro->slot = 0; 1521 coro->slot = 0;
1468 } 1522 }
1469 1523
1470 cctx_destroy (coro->cctx); 1524 cctx_destroy (coro->cctx);
1582{ 1636{
1583 struct coro *coro; 1637 struct coro *coro;
1584 SV *sv_hook; 1638 SV *sv_hook;
1585 void (*xs_hook)(void); 1639 void (*xs_hook)(void);
1586 1640
1587 if (SvROK (coro_sv))
1588 coro_sv = SvRV (coro_sv);
1589
1590 coro = SvSTATE (coro_sv); 1641 coro = SvSTATE (coro_sv);
1591 1642
1592 if (coro->flags & CF_READY) 1643 if (coro->flags & CF_READY)
1593 return 0; 1644 return 0;
1594 1645
1668 } 1719 }
1669 } 1720 }
1670 else 1721 else
1671 { 1722 {
1672 /* nothing to schedule: call the idle handler */ 1723 /* nothing to schedule: call the idle handler */
1724 if (SvROK (sv_idle)
1725 && SvOBJECT (SvRV (sv_idle)))
1726 {
1727 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1728 api_ready (aTHX_ SvRV (sv_idle));
1729 --coro_nready;
1730 }
1731 else
1732 {
1673 dSP; 1733 dSP;
1674 1734
1675 ENTER; 1735 ENTER;
1676 SAVETMPS; 1736 SAVETMPS;
1677 1737
1678 PUSHMARK (SP); 1738 PUSHMARK (SP);
1679 PUTBACK; 1739 PUTBACK;
1680 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); 1740 call_sv (sv_idle, G_VOID | G_DISCARD);
1681 1741
1682 FREETMPS; 1742 FREETMPS;
1683 LEAVE; 1743 LEAVE;
1744 }
1684 } 1745 }
1685 } 1746 }
1686} 1747}
1687 1748
1688INLINE void 1749INLINE void
1759static void 1820static void
1760api_trace (pTHX_ SV *coro_sv, int flags) 1821api_trace (pTHX_ SV *coro_sv, int flags)
1761{ 1822{
1762 struct coro *coro = SvSTATE (coro_sv); 1823 struct coro *coro = SvSTATE (coro_sv);
1763 1824
1825 if (coro->flags & CF_RUNNING)
1826 croak ("cannot enable tracing on a running coroutine, caught");
1827
1764 if (flags & CC_TRACE) 1828 if (flags & CC_TRACE)
1765 { 1829 {
1766 if (!coro->cctx) 1830 if (!coro->cctx)
1767 coro->cctx = cctx_new_run (); 1831 coro->cctx = cctx_new_run ();
1768 else if (!(coro->cctx->flags & CC_TRACE)) 1832 else if (!(coro->cctx->flags & CC_TRACE))
1769 croak ("cannot enable tracing on coroutine with custom stack,"); 1833 croak ("cannot enable tracing on coroutine with custom stack, caught");
1770 1834
1771 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1835 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1772 } 1836 }
1773 else if (coro->cctx && coro->cctx->flags & CC_TRACE) 1837 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1774 { 1838 {
1830 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ 1894 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1831 api_ready (aTHX_ sv_manager); 1895 api_ready (aTHX_ sv_manager);
1832 1896
1833 frame->prepare = prepare_schedule; 1897 frame->prepare = prepare_schedule;
1834 frame->check = slf_check_repeat; 1898 frame->check = slf_check_repeat;
1899
1900 /* as a minor optimisation, we could unwind all stacks here */
1901 /* but that puts extra pressure on pp_slf, and is not worth much */
1902 /*coro_unwind_stacks (aTHX);*/
1835} 1903}
1836 1904
1837/*****************************************************************************/ 1905/*****************************************************************************/
1838/* async pool handler */ 1906/* async pool handler */
1839 1907
2258 2326
2259 PL_op->op_ppaddr = pp_slf; 2327 PL_op->op_ppaddr = pp_slf;
2260 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ 2328 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2261 2329
2262 PL_op = (OP *)&slf_restore; 2330 PL_op = (OP *)&slf_restore;
2331}
2332
2333/*****************************************************************************/
2334/* dynamic wind */
2335
2336static void
2337on_enterleave_call (pTHX_ SV *cb)
2338{
2339 dSP;
2340
2341 PUSHSTACK;
2342
2343 PUSHMARK (SP);
2344 PUTBACK;
2345 call_sv (cb, G_VOID | G_DISCARD);
2346 SPAGAIN;
2347
2348 POPSTACK;
2349}
2350
2351static SV *
2352coro_avp_pop_and_free (AV **avp)
2353{
2354 AV *av = *avp;
2355 SV *res = av_pop (av);
2356
2357 if (AvFILLp (av) < 0)
2358 {
2359 *avp = 0;
2360 SvREFCNT_dec (av);
2361 }
2362
2363 return res;
2364}
2365
2366static void
2367coro_pop_on_enter (pTHX_ void *coro)
2368{
2369 SV *cb = coro_avp_pop_and_free (&((struct coro *)coro)->on_enter);
2370 SvREFCNT_dec (cb);
2371}
2372
2373static void
2374coro_pop_on_leave (pTHX_ void *coro)
2375{
2376 SV *cb = coro_avp_pop_and_free (&((struct coro *)coro)->on_leave);
2377 on_enterleave_call (sv_2mortal (cb));
2263} 2378}
2264 2379
2265/*****************************************************************************/ 2380/*****************************************************************************/
2266/* PerlIO::cede */ 2381/* PerlIO::cede */
2267 2382
2348 SV **ary; 2463 SV **ary;
2349 2464
2350 /* unfortunately, building manually saves memory */ 2465 /* unfortunately, building manually saves memory */
2351 Newx (ary, 2, SV *); 2466 Newx (ary, 2, SV *);
2352 AvALLOC (av) = ary; 2467 AvALLOC (av) = ary;
2468#if PERL_VERSION_ATLEAST (5,10,0)
2353 /*AvARRAY (av) = ary;*/ 2469 AvARRAY (av) = ary;
2470#else
2354 SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ 2471 /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */
2472 /* -DDEBUGGING flags this as a bug, despite it perfectly working */
2473 SvPVX ((SV *)av) = (char *)ary;
2474#endif
2355 AvMAX (av) = 1; 2475 AvMAX (av) = 1;
2356 AvFILLp (av) = 0; 2476 AvFILLp (av) = 0;
2357 ary [0] = newSViv (count); 2477 ary [0] = newSViv (count);
2358 2478
2359 return newRV_noinc ((SV *)av); 2479 return newRV_noinc ((SV *)av);
2746 coro_thx = PERL_GET_CONTEXT; 2866 coro_thx = PERL_GET_CONTEXT;
2747# endif 2867# endif
2748#endif 2868#endif
2749 BOOT_PAGESIZE; 2869 BOOT_PAGESIZE;
2750 2870
2871 cctx_current = cctx_new_empty ();
2872
2751 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 2873 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
2752 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 2874 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
2753 2875
2754 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; 2876 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; 2877 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
2881_exit (int code) 3003_exit (int code)
2882 PROTOTYPE: $ 3004 PROTOTYPE: $
2883 CODE: 3005 CODE:
2884 _exit (code); 3006 _exit (code);
2885 3007
2886#if CORO_CLONE
2887
2888SV * 3008SV *
2889clone (Coro::State coro) 3009clone (Coro::State coro)
2890 CODE: 3010 CODE:
2891{ 3011{
3012#if CORO_CLONE
2892 struct coro *ncoro = coro_clone (coro); 3013 struct coro *ncoro = coro_clone (aTHX_ coro);
2893 MAGIC *mg; 3014 MAGIC *mg;
2894 /* TODO: too much duplication */ 3015 /* TODO: too much duplication */
2895 ncoro->hv = newHV (); 3016 ncoro->hv = newHV ();
2896 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); 3017 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
2897 mg->mg_flags |= MGf_DUP; 3018 mg->mg_flags |= MGf_DUP;
2898 RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv)); 3019 RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv));
3020#else
3021 croak ("Coro::State->clone has not been configured into this installation of Coro, realised");
3022#endif
2899} 3023}
2900 OUTPUT: 3024 OUTPUT:
2901 RETVAL 3025 RETVAL
2902
2903#endif
2904 3026
2905int 3027int
2906cctx_stacksize (int new_stacksize = 0) 3028cctx_stacksize (int new_stacksize = 0)
2907 PROTOTYPE: ;$ 3029 PROTOTYPE: ;$
2908 CODE: 3030 CODE:
2958 eval = 1 3080 eval = 1
2959 CODE: 3081 CODE:
2960{ 3082{
2961 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3083 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
2962 { 3084 {
2963 struct coro temp; 3085 struct coro *current = SvSTATE_current;
2964 3086
2965 if (!(coro->flags & CF_RUNNING)) 3087 if (current != coro)
2966 { 3088 {
2967 PUTBACK; 3089 PUTBACK;
2968 save_perl (aTHX_ &temp); 3090 save_perl (aTHX_ current);
2969 load_perl (aTHX_ coro); 3091 load_perl (aTHX_ coro);
3092 SPAGAIN;
2970 } 3093 }
2971 3094
2972 {
2973 dSP;
2974 ENTER;
2975 SAVETMPS;
2976 PUTBACK;
2977 PUSHSTACK; 3095 PUSHSTACK;
3096
2978 PUSHMARK (SP); 3097 PUSHMARK (SP);
3098 PUTBACK;
2979 3099
2980 if (ix) 3100 if (ix)
2981 eval_sv (coderef, 0); 3101 eval_sv (coderef, 0);
2982 else 3102 else
2983 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3103 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
2984 3104
3105 SPAGAIN;
2985 POPSTACK; 3106 POPSTACK;
2986 SPAGAIN;
2987 FREETMPS;
2988 LEAVE;
2989 PUTBACK;
2990 }
2991 3107
2992 if (!(coro->flags & CF_RUNNING)) 3108 if (current != coro)
2993 { 3109 {
3110 PUTBACK;
2994 save_perl (aTHX_ coro); 3111 save_perl (aTHX_ coro);
2995 load_perl (aTHX_ &temp); 3112 load_perl (aTHX_ current);
2996 SPAGAIN; 3113 SPAGAIN;
2997 } 3114 }
2998 } 3115 }
2999} 3116}
3000 3117
3029 3146
3030SV * 3147SV *
3031has_cctx (Coro::State coro) 3148has_cctx (Coro::State coro)
3032 PROTOTYPE: $ 3149 PROTOTYPE: $
3033 CODE: 3150 CODE:
3034 RETVAL = boolSV (!!coro->cctx); 3151 /* maybe manage the running flag differently */
3152 RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING));
3035 OUTPUT: 3153 OUTPUT:
3036 RETVAL 3154 RETVAL
3037 3155
3038int 3156int
3039is_traced (Coro::State coro) 3157is_traced (Coro::State coro)
3059 3177
3060void 3178void
3061force_cctx () 3179force_cctx ()
3062 PROTOTYPE: 3180 PROTOTYPE:
3063 CODE: 3181 CODE:
3064 SvSTATE_current->cctx->idle_sp = 0; 3182 cctx_current->idle_sp = 0;
3065 3183
3066void 3184void
3067swap_defsv (Coro::State self) 3185swap_defsv (Coro::State self)
3068 PROTOTYPE: $ 3186 PROTOTYPE: $
3069 ALIAS: 3187 ALIAS:
3086{ 3204{
3087 int i; 3205 int i;
3088 3206
3089 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3207 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3090 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3208 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3091 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3209 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3092 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3210 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3093 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3211 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); 3212 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3095 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3213 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3096 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3214 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3215 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3097 3216
3098 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3217 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); 3218 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); 3219 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); 3220 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);
3280rouse_wait (...) 3399rouse_wait (...)
3281 PROTOTYPE: ;$ 3400 PROTOTYPE: ;$
3282 PPCODE: 3401 PPCODE:
3283 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3402 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3284 3403
3404void
3405on_enter (SV *block)
3406 ALIAS:
3407 on_leave = 1
3408 PROTOTYPE: &
3409 CODE:
3410{
3411 struct coro *coro = SvSTATE_current;
3412 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
3413
3414 block = (SV *)coro_sv_2cv (block);
3415
3416 if (!*avp)
3417 *avp = newAV ();
3418
3419 av_push (*avp, SvREFCNT_inc (block));
3420
3421 if (!ix)
3422 on_enterleave_call (aTHX_ block);
3423
3424 LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around xs calls */
3425 SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro);
3426 ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around xs calls */
3427}
3428
3285 3429
3286MODULE = Coro::State PACKAGE = PerlIO::cede 3430MODULE = Coro::State PACKAGE = PerlIO::cede
3287 3431
3288BOOT: 3432BOOT:
3289 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3433 PerlIO_define_layer (aTHX_ &PerlIO_cede);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines