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.312 by root, Thu Nov 20 03:10:30 2008 UTC vs.
Revision 1.339 by root, Mon Dec 15 00:28:30 2008 UTC

98# define newSV(l) NEWSV(0,l) 98# define newSV(l) NEWSV(0,l)
99#endif 99#endif
100#ifndef CvISXSUB_on 100#ifndef CvISXSUB_on
101# define CvISXSUB_on(cv) (void)cv 101# define CvISXSUB_on(cv) (void)cv
102#endif 102#endif
103#ifndef CvISXSUB
104# define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE)
105#endif
106#ifndef Newx
107# define Newx(ptr,nitems,type) New (0,ptr,nitems,type)
108#endif
103 109
104/* 5.8.7 */ 110/* 5.8.7 */
105#ifndef SvRV_set 111#ifndef SvRV_set
106# define SvRV_set(s,v) SvRV(s) = (v) 112# define SvRV_set(s,v) SvRV(s) = (v)
107#endif 113#endif
127#else 133#else
128# define dSTACKLEVEL volatile void *stacklevel 134# define dSTACKLEVEL volatile void *stacklevel
129# define STACKLEVEL ((void *)&stacklevel) 135# define STACKLEVEL ((void *)&stacklevel)
130#endif 136#endif
131 137
132#define IN_DESTRUCT (PL_main_cv == Nullcv) 138#define IN_DESTRUCT PL_dirty
133 139
134#if __GNUC__ >= 3 140#if __GNUC__ >= 3
135# define attribute(x) __attribute__(x) 141# define attribute(x) __attribute__(x)
136# define expect(expr,value) __builtin_expect ((expr),(value)) 142# define expect(expr,value) __builtin_expect ((expr),(value))
137# define INLINE static inline 143# define INLINE static inline
167static AV *main_mainstack; /* used to differentiate between $main and others */ 173static AV *main_mainstack; /* used to differentiate between $main and others */
168static JMPENV *main_top_env; 174static JMPENV *main_top_env;
169static HV *coro_state_stash, *coro_stash; 175static HV *coro_state_stash, *coro_stash;
170static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ 176static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
171 177
178static AV *av_destroy; /* destruction queue */
179static SV *sv_manager; /* the manager coro */
180static SV *sv_idle; /* $Coro::idle */
181
172static GV *irsgv; /* $/ */ 182static GV *irsgv; /* $/ */
173static GV *stdoutgv; /* *STDOUT */ 183static GV *stdoutgv; /* *STDOUT */
174static SV *rv_diehook; 184static SV *rv_diehook;
175static SV *rv_warnhook; 185static SV *rv_warnhook;
176static HV *hv_sig; /* %SIG */ 186static HV *hv_sig; /* %SIG */
177 187
178/* async_pool helper stuff */ 188/* async_pool helper stuff */
179static SV *sv_pool_rss; 189static SV *sv_pool_rss;
180static SV *sv_pool_size; 190static SV *sv_pool_size;
181static SV *sv_async_pool_idle; 191static SV *sv_async_pool_idle; /* description string */
182static AV *av_async_pool; 192static AV *av_async_pool; /* idle pool */
183static SV *sv_Coro; 193static SV *sv_Coro; /* class string */
184static CV *cv_pool_handler; 194static CV *cv_pool_handler;
185static CV *cv_coro_new; 195static CV *cv_coro_state_new;
186 196
187/* Coro::AnyEvent */ 197/* Coro::AnyEvent */
188static SV *sv_activity; 198static SV *sv_activity;
189 199
190static struct coro_cctx *cctx_first; 200static struct coro_cctx *cctx_first;
219 int valgrind_id; 229 int valgrind_id;
220#endif 230#endif
221 unsigned char flags; 231 unsigned char flags;
222} coro_cctx; 232} coro_cctx;
223 233
234coro_cctx *cctx_current; /* the currently running cctx */
235
236/*****************************************************************************/
237
224enum { 238enum {
225 CF_RUNNING = 0x0001, /* coroutine is running */ 239 CF_RUNNING = 0x0001, /* coroutine is running */
226 CF_READY = 0x0002, /* coroutine is ready */ 240 CF_READY = 0x0002, /* coroutine is ready */
227 CF_NEW = 0x0004, /* has never been switched to */ 241 CF_NEW = 0x0004, /* has never been switched to */
228 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 242 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
233{ 247{
234 SV *defsv; 248 SV *defsv;
235 AV *defav; 249 AV *defav;
236 SV *errsv; 250 SV *errsv;
237 SV *irsgv; 251 SV *irsgv;
252 HV *hinthv;
238#define VAR(name,type) type name; 253#define VAR(name,type) type name;
239# include "state.h" 254# include "state.h"
240#undef VAR 255#undef VAR
241} perl_slots; 256} perl_slots;
242 257
269 284
270 /* async_pool */ 285 /* async_pool */
271 SV *saved_deffh; 286 SV *saved_deffh;
272 SV *invoke_cb; 287 SV *invoke_cb;
273 AV *invoke_av; 288 AV *invoke_av;
289
290 /* on_enter/on_leave */
291 AV *on_enter;
292 AV *on_leave;
274 293
275 /* linked list */ 294 /* linked list */
276 struct coro *next, *prev; 295 struct coro *next, *prev;
277}; 296};
278 297
337INLINE CV * 356INLINE CV *
338coro_sv_2cv (pTHX_ SV *sv) 357coro_sv_2cv (pTHX_ SV *sv)
339{ 358{
340 HV *st; 359 HV *st;
341 GV *gvp; 360 GV *gvp;
342 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;
343} 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 */
344 420
345static AV * 421static AV *
346coro_clone_padlist (pTHX_ CV *cv) 422coro_derive_padlist (pTHX_ CV *cv)
347{ 423{
348 AV *padlist = CvPADLIST (cv); 424 AV *padlist = CvPADLIST (cv);
349 AV *newpadlist, *newpad; 425 AV *newpadlist, *newpad;
350 426
351 newpadlist = newAV (); 427 newpadlist = newAV ();
356 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 432 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
357#endif 433#endif
358 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 434 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
359 --AvFILLp (padlist); 435 --AvFILLp (padlist);
360 436
361 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); 437 av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0]));
362 av_store (newpadlist, 1, (SV *)newpad); 438 av_store (newpadlist, 1, (SV *)newpad);
363 439
364 return newpadlist; 440 return newpadlist;
365} 441}
366 442
367static void 443static void
368free_padlist (pTHX_ AV *padlist) 444free_padlist (pTHX_ AV *padlist)
369{ 445{
370 /* may be during global destruction */ 446 /* may be during global destruction */
371 if (SvREFCNT (padlist)) 447 if (!IN_DESTRUCT)
372 { 448 {
373 I32 i = AvFILLp (padlist); 449 I32 i = AvFILLp (padlist);
374 while (i >= 0) 450
451 while (i > 0) /* special-case index 0 */
375 { 452 {
376 SV **svp = av_fetch (padlist, i--, FALSE); 453 /* we try to be extra-careful here */
377 if (svp) 454 AV *av = (AV *)AvARRAY (padlist)[i--];
378 { 455 I32 j = AvFILLp (av);
379 SV *sv; 456
380 while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) 457 while (j >= 0)
458 SvREFCNT_dec (AvARRAY (av)[j--]);
459
460 AvFILLp (av) = -1;
381 SvREFCNT_dec (sv); 461 SvREFCNT_dec (av);
382
383 SvREFCNT_dec (*svp);
384 }
385 } 462 }
386 463
464 SvREFCNT_dec (AvARRAY (padlist)[0]);
465
466 AvFILLp (padlist) = -1;
387 SvREFCNT_dec ((SV*)padlist); 467 SvREFCNT_dec ((SV*)padlist);
388 } 468 }
389} 469}
390 470
391static int 471static int
400 480
401 SvREFCNT_dec (av); /* sv_magicext increased the refcount */ 481 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
402 482
403 return 0; 483 return 0;
404} 484}
405
406#define CORO_MAGIC_type_cv 26
407#define CORO_MAGIC_type_state PERL_MAGIC_ext
408 485
409static MGVTBL coro_cv_vtbl = { 486static MGVTBL coro_cv_vtbl = {
410 0, 0, 0, 0, 487 0, 0, 0, 0,
411 coro_cv_free 488 coro_cv_free
412}; 489};
413
414#define CORO_MAGIC_NN(sv, type) \
415 (expect_true (SvMAGIC (sv)->mg_type == type) \
416 ? SvMAGIC (sv) \
417 : mg_find (sv, type))
418
419#define CORO_MAGIC(sv, type) \
420 (expect_true (SvMAGIC (sv)) \
421 ? CORO_MAGIC_NN (sv, type) \
422 : 0)
423
424#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
425#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
426
427INLINE struct coro *
428SvSTATE_ (pTHX_ SV *coro)
429{
430 HV *stash;
431 MAGIC *mg;
432
433 if (SvROK (coro))
434 coro = SvRV (coro);
435
436 if (expect_false (SvTYPE (coro) != SVt_PVHV))
437 croak ("Coro::State object required");
438
439 stash = SvSTASH (coro);
440 if (expect_false (stash != coro_stash && stash != coro_state_stash))
441 {
442 /* very slow, but rare, check */
443 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
444 croak ("Coro::State object required");
445 }
446
447 mg = CORO_MAGIC_state (coro);
448 return (struct coro *)mg->mg_ptr;
449}
450
451#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
452
453/* faster than SvSTATE, but expects a coroutine hv */
454#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr)
455#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
456 490
457/* the next two functions merely cache the padlists */ 491/* the next two functions merely cache the padlists */
458static void 492static void
459get_padlist (pTHX_ CV *cv) 493get_padlist (pTHX_ CV *cv)
460{ 494{
471 CV *cp = Perl_cv_clone (aTHX_ cv); 505 CV *cp = Perl_cv_clone (aTHX_ cv);
472 CvPADLIST (cv) = CvPADLIST (cp); 506 CvPADLIST (cv) = CvPADLIST (cp);
473 CvPADLIST (cp) = 0; 507 CvPADLIST (cp) = 0;
474 SvREFCNT_dec (cp); 508 SvREFCNT_dec (cp);
475#else 509#else
476 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 510 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv);
477#endif 511#endif
478 } 512 }
479} 513}
480 514
481static void 515static void
488 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);
489 523
490 av = (AV *)mg->mg_obj; 524 av = (AV *)mg->mg_obj;
491 525
492 if (expect_false (AvFILLp (av) >= AvMAX (av))) 526 if (expect_false (AvFILLp (av) >= AvMAX (av)))
493 av_extend (av, AvMAX (av) + 1); 527 av_extend (av, AvFILLp (av) + 1);
494 528
495 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 529 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
496} 530}
497 531
498/** load & save, init *******************************************************/ 532/** load & save, init *******************************************************/
533
534static void
535on_enterleave_call (pTHX_ SV *cb);
499 536
500static void 537static void
501load_perl (pTHX_ Coro__State c) 538load_perl (pTHX_ Coro__State c)
502{ 539{
503 perl_slots *slot = c->slot; 540 perl_slots *slot = c->slot;
504 c->slot = 0; 541 c->slot = 0;
505 542
506 PL_mainstack = c->mainstack; 543 PL_mainstack = c->mainstack;
507 544
508 GvSV (PL_defgv) = slot->defsv; 545 GvSV (PL_defgv) = slot->defsv;
509 GvAV (PL_defgv) = slot->defav; 546 GvAV (PL_defgv) = slot->defav;
510 GvSV (PL_errgv) = slot->errsv; 547 GvSV (PL_errgv) = slot->errsv;
511 GvSV (irsgv) = slot->irsgv; 548 GvSV (irsgv) = slot->irsgv;
549 GvHV (PL_hintgv) = slot->hinthv;
512 550
513 #define VAR(name,type) PL_ ## name = slot->name; 551 #define VAR(name,type) PL_ ## name = slot->name;
514 # include "state.h" 552 # include "state.h"
515 #undef VAR 553 #undef VAR
516 554
530 PUTBACK; 568 PUTBACK;
531 } 569 }
532 570
533 slf_frame = c->slf_frame; 571 slf_frame = c->slf_frame;
534 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 }
535} 581}
536 582
537static void 583static void
538save_perl (pTHX_ Coro__State c) 584save_perl (pTHX_ Coro__State c)
539{ 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
540 c->except = CORO_THROW; 594 c->except = CORO_THROW;
541 c->slf_frame = slf_frame; 595 c->slf_frame = slf_frame;
542 596
543 { 597 {
544 dSP; 598 dSP;
602 c->mainstack = PL_mainstack; 656 c->mainstack = PL_mainstack;
603 657
604 { 658 {
605 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 659 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
606 660
607 slot->defav = GvAV (PL_defgv); 661 slot->defav = GvAV (PL_defgv);
608 slot->defsv = DEFSV; 662 slot->defsv = DEFSV;
609 slot->errsv = ERRSV; 663 slot->errsv = ERRSV;
610 slot->irsgv = GvSV (irsgv); 664 slot->irsgv = GvSV (irsgv);
665 slot->hinthv = GvHV (PL_hintgv);
611 666
612 #define VAR(name,type) slot->name = PL_ ## name; 667 #define VAR(name,type) slot->name = PL_ ## name;
613 # include "state.h" 668 # include "state.h"
614 #undef VAR 669 #undef VAR
615 } 670 }
742#endif 797#endif
743 798
744/* 799/*
745 * This overrides the default magic get method of %SIG elements. 800 * This overrides the default magic get method of %SIG elements.
746 * 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
747 * 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
748 * readback here. 803 * readback here.
749 * We only do this when the hook is != 0, as they are often set to 0 temporarily,
750 * not expecting this to actually change the hook. This is a potential problem
751 * when a schedule happens then, but we ignore this.
752 */ 804 */
753static int 805static int
754coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) 806coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
755{ 807{
756 const char *s = MgPV_nolen_const (mg); 808 const char *s = MgPV_nolen_const (mg);
809 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 861 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
810 862
811 if (svp) 863 if (svp)
812 { 864 {
813 SV *old = *svp; 865 SV *old = *svp;
814 *svp = newSVsv (sv); 866 *svp = SvOK (sv) ? newSVsv (sv) : 0;
815 SvREFCNT_dec (old); 867 SvREFCNT_dec (old);
816 return 0; 868 return 0;
817 } 869 }
818 } 870 }
819 871
829 881
830static int 882static int
831slf_check_nop (pTHX_ struct CoroSLF *frame) 883slf_check_nop (pTHX_ struct CoroSLF *frame)
832{ 884{
833 return 0; 885 return 0;
886}
887
888static int
889slf_check_repeat (pTHX_ struct CoroSLF *frame)
890{
891 return 1;
834} 892}
835 893
836static UNOP coro_setup_op; 894static UNOP coro_setup_op;
837 895
838static void NOINLINE /* noinline to keep it out of the transfer fast path */ 896static void NOINLINE /* noinline to keep it out of the transfer fast path */
845 903
846 PL_runops = RUNOPS_DEFAULT; 904 PL_runops = RUNOPS_DEFAULT;
847 PL_curcop = &PL_compiling; 905 PL_curcop = &PL_compiling;
848 PL_in_eval = EVAL_NULL; 906 PL_in_eval = EVAL_NULL;
849 PL_comppad = 0; 907 PL_comppad = 0;
908 PL_comppad_name = 0;
909 PL_comppad_name_fill = 0;
910 PL_comppad_name_floor = 0;
850 PL_curpm = 0; 911 PL_curpm = 0;
851 PL_curpad = 0; 912 PL_curpad = 0;
852 PL_localizing = 0; 913 PL_localizing = 0;
853 PL_dirty = 0; 914 PL_dirty = 0;
854 PL_restartop = 0; 915 PL_restartop = 0;
855#if PERL_VERSION_ATLEAST (5,10,0) 916#if PERL_VERSION_ATLEAST (5,10,0)
856 PL_parser = 0; 917 PL_parser = 0;
857#endif 918#endif
919 PL_hints = 0;
858 920
859 /* recreate the die/warn hooks */ 921 /* recreate the die/warn hooks */
860 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 );
861 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);
862 924
863 GvSV (PL_defgv) = newSV (0); 925 GvSV (PL_defgv) = newSV (0);
864 GvAV (PL_defgv) = coro->args; coro->args = 0; 926 GvAV (PL_defgv) = coro->args; coro->args = 0;
865 GvSV (PL_errgv) = newSV (0); 927 GvSV (PL_errgv) = newSV (0);
866 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;
867 PL_rs = newSVsv (GvSV (irsgv)); 930 PL_rs = newSVsv (GvSV (irsgv));
868 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 931 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
869 932
870 { 933 {
871 dSP; 934 dSP;
889 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ 952 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */
890 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ 953 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */
891 954
892 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ 955 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */
893 coro_setup_op.op_next = PL_op; 956 coro_setup_op.op_next = PL_op;
894 coro_setup_op.op_type = OP_CUSTOM; 957 coro_setup_op.op_type = OP_ENTERSUB;
895 coro_setup_op.op_ppaddr = pp_slf; 958 coro_setup_op.op_ppaddr = pp_slf;
896 /* no flags etc. required, as an init function won't be called */ 959 /* no flags etc. required, as an init function won't be called */
897 960
898 PL_op = (OP *)&coro_setup_op; 961 PL_op = (OP *)&coro_setup_op;
899 962
900 /* copy throw, in case it was set before coro_setup */ 963 /* copy throw, in case it was set before coro_setup */
901 CORO_THROW = coro->except; 964 CORO_THROW = coro->except;
902} 965}
903 966
904static void 967static void
905coro_destruct (pTHX_ struct coro *coro) 968coro_unwind_stacks (pTHX)
906{ 969{
907 if (!IN_DESTRUCT) 970 if (!IN_DESTRUCT)
908 { 971 {
909 /* restore all saved variables and stuff */ 972 /* restore all saved variables and stuff */
910 LEAVE_SCOPE (0); 973 LEAVE_SCOPE (0);
918 POPSTACK_TO (PL_mainstack); 981 POPSTACK_TO (PL_mainstack);
919 982
920 /* unwind main stack */ 983 /* unwind main stack */
921 dounwind (-1); 984 dounwind (-1);
922 } 985 }
986}
987
988static void
989coro_destruct_perl (pTHX_ struct coro *coro)
990{
991 coro_unwind_stacks (aTHX);
923 992
924 SvREFCNT_dec (GvSV (PL_defgv)); 993 SvREFCNT_dec (GvSV (PL_defgv));
925 SvREFCNT_dec (GvAV (PL_defgv)); 994 SvREFCNT_dec (GvAV (PL_defgv));
926 SvREFCNT_dec (GvSV (PL_errgv)); 995 SvREFCNT_dec (GvSV (PL_errgv));
927 SvREFCNT_dec (PL_defoutgv); 996 SvREFCNT_dec (PL_defoutgv);
928 SvREFCNT_dec (PL_rs); 997 SvREFCNT_dec (PL_rs);
929 SvREFCNT_dec (GvSV (irsgv)); 998 SvREFCNT_dec (GvSV (irsgv));
999 SvREFCNT_dec (GvHV (PL_hintgv));
930 1000
931 SvREFCNT_dec (PL_diehook); 1001 SvREFCNT_dec (PL_diehook);
932 SvREFCNT_dec (PL_warnhook); 1002 SvREFCNT_dec (PL_warnhook);
933 1003
934 SvREFCNT_dec (coro->saved_deffh); 1004 SvREFCNT_dec (coro->saved_deffh);
952static int 1022static int
953runops_trace (pTHX) 1023runops_trace (pTHX)
954{ 1024{
955 COP *oldcop = 0; 1025 COP *oldcop = 0;
956 int oldcxix = -2; 1026 int oldcxix = -2;
957 struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */
958 coro_cctx *cctx = coro->cctx;
959 1027
960 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 1028 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
961 { 1029 {
962 PERL_ASYNC_CHECK (); 1030 PERL_ASYNC_CHECK ();
963 1031
964 if (cctx->flags & CC_TRACE_ALL) 1032 if (cctx_current->flags & CC_TRACE_ALL)
965 { 1033 {
966 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)
967 { 1035 {
968 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1036 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
969 SV **bot, **top; 1037 SV **bot, **top;
970 AV *av = newAV (); /* return values */ 1038 AV *av = newAV (); /* return values */
971 SV **cb; 1039 SV **cb;
1008 1076
1009 if (PL_curcop != &PL_compiling) 1077 if (PL_curcop != &PL_compiling)
1010 { 1078 {
1011 SV **cb; 1079 SV **cb;
1012 1080
1013 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) 1081 if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB)
1014 { 1082 {
1015 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1083 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1016 1084
1017 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 1085 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
1018 { 1086 {
1019 runops_proc_t old_runops = PL_runops;
1020 dSP; 1087 dSP;
1021 GV *gv = CvGV (cx->blk_sub.cv); 1088 GV *gv = CvGV (cx->blk_sub.cv);
1022 SV *fullname = sv_2mortal (newSV (0)); 1089 SV *fullname = sv_2mortal (newSV (0));
1023 1090
1024 if (isGV (gv)) 1091 if (isGV (gv))
1042 } 1109 }
1043 1110
1044 oldcxix = cxstack_ix; 1111 oldcxix = cxstack_ix;
1045 } 1112 }
1046 1113
1047 if (cctx->flags & CC_TRACE_LINE) 1114 if (cctx_current->flags & CC_TRACE_LINE)
1048 { 1115 {
1049 dSP; 1116 dSP;
1050 1117
1051 PL_runops = RUNOPS_DEFAULT; 1118 PL_runops = RUNOPS_DEFAULT;
1052 ENTER; 1119 ENTER;
1071 1138
1072 TAINT_NOT; 1139 TAINT_NOT;
1073 return 0; 1140 return 0;
1074} 1141}
1075 1142
1076static struct coro_cctx *cctx_ssl_cctx;
1077static struct CoroSLF cctx_ssl_frame; 1143static struct CoroSLF cctx_ssl_frame;
1078 1144
1079static void 1145static void
1080slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) 1146slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1081{ 1147{
1082 ta->prev = (struct coro *)cctx_ssl_cctx;
1083 ta->next = 0; 1148 ta->prev = 0;
1084} 1149}
1085 1150
1086static int 1151static int
1087slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) 1152slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1088{ 1153{
1091 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 */
1092} 1157}
1093 1158
1094/* 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 */
1095static void NOINLINE 1160static void NOINLINE
1096cctx_prepare (pTHX_ coro_cctx *cctx) 1161cctx_prepare (pTHX)
1097{ 1162{
1098 PL_top_env = &PL_start_env; 1163 PL_top_env = &PL_start_env;
1099 1164
1100 if (cctx->flags & CC_TRACE) 1165 if (cctx_current->flags & CC_TRACE)
1101 PL_runops = runops_trace; 1166 PL_runops = runops_trace;
1102 1167
1103 /* 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
1104 * that can lead to creation of a new cctx */ 1169 * that can lead to creation of a new cctx */
1105 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)",
1106 slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); 1171 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1107 1172
1108 /* 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 */
1109 cctx_ssl_cctx = cctx;
1110 cctx_ssl_frame = slf_frame; 1174 cctx_ssl_frame = slf_frame;
1111 1175
1112 slf_frame.prepare = slf_prepare_set_stacklevel; 1176 slf_frame.prepare = slf_prepare_set_stacklevel;
1113 slf_frame.check = slf_check_set_stacklevel; 1177 slf_frame.check = slf_check_set_stacklevel;
1114} 1178}
1137 /* normally we would need to skip the entersub here */ 1201 /* normally we would need to skip the entersub here */
1138 /* 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 */
1139 /* PL_nop = PL_nop->op_next */ 1203 /* PL_nop = PL_nop->op_next */
1140 1204
1141 /* inject a fake subroutine call to cctx_init */ 1205 /* inject a fake subroutine call to cctx_init */
1142 cctx_prepare (aTHX_ (coro_cctx *)arg); 1206 cctx_prepare (aTHX);
1143 1207
1144 /* cctx_run is the alternative tail of transfer() */ 1208 /* cctx_run is the alternative tail of transfer() */
1145 transfer_tail (aTHX); 1209 transfer_tail (aTHX);
1146 1210
1147 /* 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 */
1155 /* 1219 /*
1156 * 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
1157 * 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)
1158 * 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
1159 * bootstrap-time "top" top_env, as we cannot restore the "main" 1223 * bootstrap-time "top" top_env, as we cannot restore the "main"
1160 * 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.
1161 */ 1227 */
1162 PL_top_env = main_top_env; 1228 PL_top_env = main_top_env;
1163 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 */
1164 } 1230 }
1165} 1231}
1242cctx_destroy (coro_cctx *cctx) 1308cctx_destroy (coro_cctx *cctx)
1243{ 1309{
1244 if (!cctx) 1310 if (!cctx)
1245 return; 1311 return;
1246 1312
1313 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary?
1314
1247 --cctx_count; 1315 --cctx_count;
1248 coro_destroy (&cctx->cctx); 1316 coro_destroy (&cctx->cctx);
1249 1317
1250 /* coro_transfer creates new, empty cctx's */ 1318 /* coro_transfer creates new, empty cctx's */
1251 if (cctx->sptr) 1319 if (cctx->sptr)
1314 /* TODO: throwing up here is considered harmful */ 1382 /* TODO: throwing up here is considered harmful */
1315 1383
1316 if (expect_true (prev != next)) 1384 if (expect_true (prev != next))
1317 { 1385 {
1318 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1386 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1319 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,");
1320 1388
1321 if (expect_false (next->flags & CF_RUNNING)) 1389 if (expect_false (next->flags & CF_RUNNING))
1322 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,");
1323 1391
1324 if (expect_false (next->flags & CF_DESTROYED)) 1392 if (expect_false (next->flags & CF_DESTROYED))
1336transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1404transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1337{ 1405{
1338 dSTACKLEVEL; 1406 dSTACKLEVEL;
1339 1407
1340 /* sometimes transfer is only called to set idle_sp */ 1408 /* sometimes transfer is only called to set idle_sp */
1341 if (expect_false (!next)) 1409 if (expect_false (!prev))
1342 { 1410 {
1343 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1411 cctx_current->idle_sp = STACKLEVEL;
1344 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 */
1345 } 1413 }
1346 else if (expect_true (prev != next)) 1414 else if (expect_true (prev != next))
1347 { 1415 {
1348 coro_cctx *prev__cctx; 1416 coro_cctx *cctx_prev;
1349 1417
1350 if (expect_false (prev->flags & CF_NEW)) 1418 if (expect_false (prev->flags & CF_NEW))
1351 { 1419 {
1352 /* create a new empty/source context */ 1420 /* create a new empty/source context */
1353 prev->cctx = cctx_new_empty ();
1354 prev->flags &= ~CF_NEW; 1421 prev->flags &= ~CF_NEW;
1355 prev->flags |= CF_RUNNING; 1422 prev->flags |= CF_RUNNING;
1356 } 1423 }
1357 1424
1358 prev->flags &= ~CF_RUNNING; 1425 prev->flags &= ~CF_RUNNING;
1369 coro_setup (aTHX_ next); 1436 coro_setup (aTHX_ next);
1370 } 1437 }
1371 else 1438 else
1372 load_perl (aTHX_ next); 1439 load_perl (aTHX_ next);
1373 1440
1374 prev__cctx = prev->cctx;
1375
1376 /* possibly untie and reuse the cctx */ 1441 /* possibly untie and reuse the cctx */
1377 if (expect_true ( 1442 if (expect_true (
1378 prev__cctx->idle_sp == STACKLEVEL 1443 cctx_current->idle_sp == STACKLEVEL
1379 && !(prev__cctx->flags & CC_TRACE) 1444 && !(cctx_current->flags & CC_TRACE)
1380 && !force_cctx 1445 && !force_cctx
1381 )) 1446 ))
1382 { 1447 {
1383 /* 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 */
1384 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));
1385 1450
1386 prev->cctx = 0;
1387
1388 /* 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. */
1389 /* 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 */
1390 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1453 if (expect_false (CCTX_EXPIRED (cctx_current)))
1391 if (!next->cctx) 1454 if (expect_true (!next->cctx))
1392 next->cctx = cctx_get (aTHX); 1455 next->cctx = cctx_get (aTHX);
1393 1456
1394 cctx_put (prev__cctx); 1457 cctx_put (cctx_current);
1395 } 1458 }
1459 else
1460 prev->cctx = cctx_current;
1396 1461
1397 ++next->usecount; 1462 ++next->usecount;
1398 1463
1399 if (expect_true (!next->cctx)) 1464 cctx_prev = cctx_current;
1400 next->cctx = cctx_get (aTHX); 1465 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1401 1466
1402 if (expect_false (prev__cctx != next->cctx)) 1467 next->cctx = 0;
1468
1469 if (expect_false (cctx_prev != cctx_current))
1403 { 1470 {
1404 prev__cctx->top_env = PL_top_env; 1471 cctx_prev->top_env = PL_top_env;
1405 PL_top_env = next->cctx->top_env; 1472 PL_top_env = cctx_current->top_env;
1406 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1473 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1407 } 1474 }
1408 1475
1409 transfer_tail (aTHX); 1476 transfer_tail (aTHX);
1410 } 1477 }
1411} 1478}
1433 --coro_nready; 1500 --coro_nready;
1434 } 1501 }
1435 else 1502 else
1436 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 */
1437 1504
1438 if (coro->mainstack && coro->mainstack != main_mainstack) 1505 if (coro->mainstack
1506 && coro->mainstack != main_mainstack
1507 && coro->slot
1508 && !PL_dirty)
1439 { 1509 {
1440 struct coro temp; 1510 struct coro *current = SvSTATE_current;
1441 1511
1442 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));
1443 1513
1444 save_perl (aTHX_ &temp); 1514 save_perl (aTHX_ current);
1445 load_perl (aTHX_ coro); 1515 load_perl (aTHX_ coro);
1446 1516
1447 coro_destruct (aTHX_ coro); 1517 coro_destruct_perl (aTHX_ coro);
1448 1518
1449 load_perl (aTHX_ &temp); 1519 load_perl (aTHX_ current);
1450 1520
1451 coro->slot = 0; 1521 coro->slot = 0;
1452 } 1522 }
1453 1523
1454 cctx_destroy (coro->cctx); 1524 cctx_destroy (coro->cctx);
1566{ 1636{
1567 struct coro *coro; 1637 struct coro *coro;
1568 SV *sv_hook; 1638 SV *sv_hook;
1569 void (*xs_hook)(void); 1639 void (*xs_hook)(void);
1570 1640
1571 if (SvROK (coro_sv))
1572 coro_sv = SvRV (coro_sv);
1573
1574 coro = SvSTATE (coro_sv); 1641 coro = SvSTATE (coro_sv);
1575 1642
1576 if (coro->flags & CF_READY) 1643 if (coro->flags & CF_READY)
1577 return 0; 1644 return 0;
1578 1645
1609api_is_ready (pTHX_ SV *coro_sv) 1676api_is_ready (pTHX_ SV *coro_sv)
1610{ 1677{
1611 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1678 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1612} 1679}
1613 1680
1681/* expects to own a reference to next->hv */
1614INLINE void 1682INLINE void
1615prepare_schedule (pTHX_ struct coro_transfer_args *ta) 1683prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next)
1616{ 1684{
1617 SV *prev_sv, *next_sv;
1618
1619 for (;;)
1620 {
1621 next_sv = coro_deq (aTHX);
1622
1623 /* nothing to schedule: call the idle handler */
1624 if (expect_false (!next_sv))
1625 {
1626 dSP;
1627
1628 ENTER;
1629 SAVETMPS;
1630
1631 PUSHMARK (SP);
1632 PUTBACK;
1633 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD);
1634
1635 FREETMPS;
1636 LEAVE;
1637 continue;
1638 }
1639
1640 ta->next = SvSTATE_hv (next_sv);
1641
1642 /* cannot transfer to destroyed coros, skip and look for next */
1643 if (expect_false (ta->next->flags & CF_DESTROYED))
1644 {
1645 SvREFCNT_dec (next_sv);
1646 /* coro_nready has already been taken care of by destroy */
1647 continue;
1648 }
1649
1650 --coro_nready;
1651 break;
1652 }
1653
1654 /* free this only after the transfer */
1655 prev_sv = SvRV (coro_current); 1685 SV *prev_sv = SvRV (coro_current);
1686
1656 ta->prev = SvSTATE_hv (prev_sv); 1687 ta->prev = SvSTATE_hv (prev_sv);
1688 ta->next = next;
1689
1657 TRANSFER_CHECK (*ta); 1690 TRANSFER_CHECK (*ta);
1658 assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); 1691
1659 ta->next->flags &= ~CF_READY;
1660 SvRV_set (coro_current, next_sv); 1692 SvRV_set (coro_current, (SV *)next->hv);
1661 1693
1662 free_coro_mortal (aTHX); 1694 free_coro_mortal (aTHX);
1663 coro_mortal = prev_sv; 1695 coro_mortal = prev_sv;
1664} 1696}
1665 1697
1698static void
1699prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1700{
1701 for (;;)
1702 {
1703 SV *next_sv = coro_deq (aTHX);
1704
1705 if (expect_true (next_sv))
1706 {
1707 struct coro *next = SvSTATE_hv (next_sv);
1708
1709 /* cannot transfer to destroyed coros, skip and look for next */
1710 if (expect_false (next->flags & CF_DESTROYED))
1711 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */
1712 else
1713 {
1714 next->flags &= ~CF_READY;
1715 --coro_nready;
1716
1717 prepare_schedule_to (aTHX_ ta, next);
1718 break;
1719 }
1720 }
1721 else
1722 {
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 {
1733 dSP;
1734
1735 ENTER;
1736 SAVETMPS;
1737
1738 PUSHMARK (SP);
1739 PUTBACK;
1740 call_sv (sv_idle, G_VOID | G_DISCARD);
1741
1742 FREETMPS;
1743 LEAVE;
1744 }
1745 }
1746 }
1747}
1748
1666INLINE void 1749INLINE void
1667prepare_cede (pTHX_ struct coro_transfer_args *ta) 1750prepare_cede (pTHX_ struct coro_transfer_args *ta)
1668{ 1751{
1669 api_ready (aTHX_ coro_current); 1752 api_ready (aTHX_ coro_current);
1670 prepare_schedule (aTHX_ ta); 1753 prepare_schedule (aTHX_ ta);
1689{ 1772{
1690 struct coro_transfer_args ta; 1773 struct coro_transfer_args ta;
1691 1774
1692 prepare_schedule (aTHX_ &ta); 1775 prepare_schedule (aTHX_ &ta);
1693 TRANSFER (ta, 1); 1776 TRANSFER (ta, 1);
1777}
1778
1779static void
1780api_schedule_to (pTHX_ SV *coro_sv)
1781{
1782 struct coro_transfer_args ta;
1783 struct coro *next = SvSTATE (coro_sv);
1784
1785 SvREFCNT_inc_NN (coro_sv);
1786 prepare_schedule_to (aTHX_ &ta, next);
1694} 1787}
1695 1788
1696static int 1789static int
1697api_cede (pTHX) 1790api_cede (pTHX)
1698{ 1791{
1727static void 1820static void
1728api_trace (pTHX_ SV *coro_sv, int flags) 1821api_trace (pTHX_ SV *coro_sv, int flags)
1729{ 1822{
1730 struct coro *coro = SvSTATE (coro_sv); 1823 struct coro *coro = SvSTATE (coro_sv);
1731 1824
1825 if (coro->flags & CF_RUNNING)
1826 croak ("cannot enable tracing on a running coroutine, caught");
1827
1732 if (flags & CC_TRACE) 1828 if (flags & CC_TRACE)
1733 { 1829 {
1734 if (!coro->cctx) 1830 if (!coro->cctx)
1735 coro->cctx = cctx_new_run (); 1831 coro->cctx = cctx_new_run ();
1736 else if (!(coro->cctx->flags & CC_TRACE)) 1832 else if (!(coro->cctx->flags & CC_TRACE))
1737 croak ("cannot enable tracing on coroutine with custom stack,"); 1833 croak ("cannot enable tracing on coroutine with custom stack, caught");
1738 1834
1739 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1835 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1740 } 1836 }
1741 else if (coro->cctx && coro->cctx->flags & CC_TRACE) 1837 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1742 { 1838 {
1747 else 1843 else
1748 coro->slot->runops = RUNOPS_DEFAULT; 1844 coro->slot->runops = RUNOPS_DEFAULT;
1749 } 1845 }
1750} 1846}
1751 1847
1848static void
1849coro_call_on_destroy (pTHX_ struct coro *coro)
1850{
1851 SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0);
1852 SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0);
1853
1854 if (on_destroyp)
1855 {
1856 AV *on_destroy = (AV *)SvRV (*on_destroyp);
1857
1858 while (AvFILLp (on_destroy) >= 0)
1859 {
1860 dSP; /* don't disturb outer sp */
1861 SV *cb = av_pop (on_destroy);
1862
1863 PUSHMARK (SP);
1864
1865 if (statusp)
1866 {
1867 int i;
1868 AV *status = (AV *)SvRV (*statusp);
1869 EXTEND (SP, AvFILLp (status) + 1);
1870
1871 for (i = 0; i <= AvFILLp (status); ++i)
1872 PUSHs (AvARRAY (status)[i]);
1873 }
1874
1875 PUTBACK;
1876 call_sv (sv_2mortal (cb), G_VOID | G_DISCARD);
1877 }
1878 }
1879}
1880
1881static void
1882slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1883{
1884 int i;
1885 HV *hv = (HV *)SvRV (coro_current);
1886 AV *av = newAV ();
1887
1888 av_extend (av, items - 1);
1889 for (i = 0; i < items; ++i)
1890 av_push (av, SvREFCNT_inc_NN (arg [i]));
1891
1892 hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0);
1893
1894 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1895 api_ready (aTHX_ sv_manager);
1896
1897 frame->prepare = prepare_schedule;
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);*/
1903}
1904
1752/*****************************************************************************/ 1905/*****************************************************************************/
1753/* async pool handler */ 1906/* async pool handler */
1754 1907
1755static int 1908static int
1756slf_check_pool_handler (pTHX_ struct CoroSLF *frame) 1909slf_check_pool_handler (pTHX_ struct CoroSLF *frame)
1760 1913
1761 if (!coro->invoke_cb) 1914 if (!coro->invoke_cb)
1762 return 1; /* loop till we have invoke */ 1915 return 1; /* loop till we have invoke */
1763 else 1916 else
1764 { 1917 {
1765 int i, len;
1766
1767 hv_store (hv, "desc", sizeof ("desc") - 1, 1918 hv_store (hv, "desc", sizeof ("desc") - 1,
1768 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 1919 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1769 1920
1770 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv); 1921 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
1771
1772 len = av_len (coro->invoke_av);
1773 1922
1774 { 1923 {
1775 dSP; 1924 dSP;
1776 XPUSHs (sv_2mortal (coro->invoke_cb)); coro->invoke_cb = 0; 1925 XPUSHs (sv_2mortal (coro->invoke_cb)); coro->invoke_cb = 0;
1777 PUTBACK; 1926 PUTBACK;
1841 SV *data = (SV *)GENSUB_ARG; 1990 SV *data = (SV *)GENSUB_ARG;
1842 1991
1843 if (SvTYPE (SvRV (data)) != SVt_PVAV) 1992 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1844 { 1993 {
1845 /* first call, set args */ 1994 /* first call, set args */
1846 int i;
1847 AV *av = newAV (); 1995 AV *av = newAV ();
1848 SV *coro = SvRV (data); 1996 SV *coro = SvRV (data);
1849 1997
1850 SvRV_set (data, (SV *)av); 1998 SvRV_set (data, (SV *)av);
1851 api_ready (aTHX_ coro); 1999 api_ready (aTHX_ coro);
1992static void 2140static void
1993slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2141slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1994{ 2142{
1995 frame->prepare = prepare_schedule; 2143 frame->prepare = prepare_schedule;
1996 frame->check = slf_check_nop; 2144 frame->check = slf_check_nop;
2145}
2146
2147static void
2148slf_prepare_schedule_to (pTHX_ struct coro_transfer_args *ta)
2149{
2150 struct coro *next = (struct coro *)slf_frame.data;
2151
2152 SvREFCNT_inc_NN (next->hv);
2153 prepare_schedule_to (aTHX_ ta, next);
2154}
2155
2156static void
2157slf_init_schedule_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2158{
2159 if (!items)
2160 croak ("Coro::schedule_to expects a coroutine argument, caught");
2161
2162 frame->data = (void *)SvSTATE (arg [0]);
2163 frame->prepare = slf_prepare_schedule_to;
2164 frame->check = slf_check_nop;
2165}
2166
2167static void
2168slf_init_cede_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2169{
2170 api_ready (aTHX_ SvRV (coro_current));
2171
2172 slf_init_schedule_to (aTHX_ frame, cv, arg, items);
1997} 2173}
1998 2174
1999static void 2175static void
2000slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2176slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2001{ 2177{
2147 } 2323 }
2148 else 2324 else
2149 slf_argc = 0; 2325 slf_argc = 0;
2150 2326
2151 PL_op->op_ppaddr = pp_slf; 2327 PL_op->op_ppaddr = pp_slf;
2152 PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ 2328 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2153 2329
2154 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));
2155} 2378}
2156 2379
2157/*****************************************************************************/ 2380/*****************************************************************************/
2158/* PerlIO::cede */ 2381/* PerlIO::cede */
2159 2382
2240 SV **ary; 2463 SV **ary;
2241 2464
2242 /* unfortunately, building manually saves memory */ 2465 /* unfortunately, building manually saves memory */
2243 Newx (ary, 2, SV *); 2466 Newx (ary, 2, SV *);
2244 AvALLOC (av) = ary; 2467 AvALLOC (av) = ary;
2468#if PERL_VERSION_ATLEAST (5,10,0)
2245 /*AvARRAY (av) = ary;*/ 2469 AvARRAY (av) = ary;
2470#else
2246 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
2247 AvMAX (av) = 1; 2475 AvMAX (av) = 1;
2248 AvFILLp (av) = 0; 2476 AvFILLp (av) = 0;
2249 ary [0] = newSViv (count); 2477 ary [0] = newSViv (count);
2250 2478
2251 return newRV_noinc ((SV *)av); 2479 return newRV_noinc ((SV *)av);
2621 XSRETURN_EMPTY; 2849 XSRETURN_EMPTY;
2622} 2850}
2623 2851
2624/*****************************************************************************/ 2852/*****************************************************************************/
2625 2853
2854#if CORO_CLONE
2855# include "clone.c"
2856#endif
2857
2626MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 2858MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2627 2859
2628PROTOTYPES: DISABLE 2860PROTOTYPES: DISABLE
2629 2861
2630BOOT: 2862BOOT:
2634 coro_thx = PERL_GET_CONTEXT; 2866 coro_thx = PERL_GET_CONTEXT;
2635# endif 2867# endif
2636#endif 2868#endif
2637 BOOT_PAGESIZE; 2869 BOOT_PAGESIZE;
2638 2870
2871 cctx_current = cctx_new_empty ();
2872
2639 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 2873 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
2640 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 2874 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
2641 2875
2642 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;
2643 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;
2662 2896
2663 { 2897 {
2664 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); 2898 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf)));
2665 2899
2666 if (!PL_custom_op_names) PL_custom_op_names = newHV (); 2900 if (!PL_custom_op_names) PL_custom_op_names = newHV ();
2667 hv_store_ent (PL_custom_op_names, slf, 2901 hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0);
2668 newSVpv ("coro_slf", 0), 0);
2669 2902
2670 if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); 2903 if (!PL_custom_op_descs) PL_custom_op_descs = newHV ();
2671 hv_store_ent (PL_custom_op_descs, slf, 2904 hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0);
2672 newSVpv ("coro schedule like function", 0), 0);
2673 } 2905 }
2674 2906
2675 coroapi.ver = CORO_API_VERSION; 2907 coroapi.ver = CORO_API_VERSION;
2676 coroapi.rev = CORO_API_REVISION; 2908 coroapi.rev = CORO_API_REVISION;
2677 2909
2770void 3002void
2771_exit (int code) 3003_exit (int code)
2772 PROTOTYPE: $ 3004 PROTOTYPE: $
2773 CODE: 3005 CODE:
2774 _exit (code); 3006 _exit (code);
3007
3008SV *
3009clone (Coro::State coro)
3010 CODE:
3011{
3012#if CORO_CLONE
3013 struct coro *ncoro = coro_clone (aTHX_ coro);
3014 MAGIC *mg;
3015 /* TODO: too much duplication */
3016 ncoro->hv = newHV ();
3017 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
3018 mg->mg_flags |= MGf_DUP;
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
3023}
3024 OUTPUT:
3025 RETVAL
2775 3026
2776int 3027int
2777cctx_stacksize (int new_stacksize = 0) 3028cctx_stacksize (int new_stacksize = 0)
2778 PROTOTYPE: ;$ 3029 PROTOTYPE: ;$
2779 CODE: 3030 CODE:
2829 eval = 1 3080 eval = 1
2830 CODE: 3081 CODE:
2831{ 3082{
2832 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3083 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
2833 { 3084 {
2834 struct coro temp; 3085 struct coro *current = SvSTATE_current;
2835 3086
2836 if (!(coro->flags & CF_RUNNING)) 3087 if (current != coro)
2837 { 3088 {
2838 PUTBACK; 3089 PUTBACK;
2839 save_perl (aTHX_ &temp); 3090 save_perl (aTHX_ current);
2840 load_perl (aTHX_ coro); 3091 load_perl (aTHX_ coro);
3092 SPAGAIN;
2841 } 3093 }
2842 3094
2843 {
2844 dSP;
2845 ENTER;
2846 SAVETMPS;
2847 PUTBACK;
2848 PUSHSTACK; 3095 PUSHSTACK;
3096
2849 PUSHMARK (SP); 3097 PUSHMARK (SP);
3098 PUTBACK;
2850 3099
2851 if (ix) 3100 if (ix)
2852 eval_sv (coderef, 0); 3101 eval_sv (coderef, 0);
2853 else 3102 else
2854 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3103 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
2855 3104
3105 SPAGAIN;
2856 POPSTACK; 3106 POPSTACK;
2857 SPAGAIN;
2858 FREETMPS;
2859 LEAVE;
2860 PUTBACK;
2861 }
2862 3107
2863 if (!(coro->flags & CF_RUNNING)) 3108 if (current != coro)
2864 { 3109 {
3110 PUTBACK;
2865 save_perl (aTHX_ coro); 3111 save_perl (aTHX_ coro);
2866 load_perl (aTHX_ &temp); 3112 load_perl (aTHX_ current);
2867 SPAGAIN; 3113 SPAGAIN;
2868 } 3114 }
2869 } 3115 }
2870} 3116}
2871 3117
2900 3146
2901SV * 3147SV *
2902has_cctx (Coro::State coro) 3148has_cctx (Coro::State coro)
2903 PROTOTYPE: $ 3149 PROTOTYPE: $
2904 CODE: 3150 CODE:
2905 RETVAL = boolSV (!!coro->cctx); 3151 /* maybe manage the running flag differently */
3152 RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING));
2906 OUTPUT: 3153 OUTPUT:
2907 RETVAL 3154 RETVAL
2908 3155
2909int 3156int
2910is_traced (Coro::State coro) 3157is_traced (Coro::State coro)
2930 3177
2931void 3178void
2932force_cctx () 3179force_cctx ()
2933 PROTOTYPE: 3180 PROTOTYPE:
2934 CODE: 3181 CODE:
2935 SvSTATE_current->cctx->idle_sp = 0; 3182 cctx_current->idle_sp = 0;
2936 3183
2937void 3184void
2938swap_defsv (Coro::State self) 3185swap_defsv (Coro::State self)
2939 PROTOTYPE: $ 3186 PROTOTYPE: $
2940 ALIAS: 3187 ALIAS:
2955 3202
2956BOOT: 3203BOOT:
2957{ 3204{
2958 int i; 3205 int i;
2959 3206
2960 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2961 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3207 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2962 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3208 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2963 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3209 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
2964 cv_coro_terminate = get_cv ( "Coro::terminate", GV_ADD); 3210 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
2965 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); 3211 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
2966 SvREADONLY_on (coro_current); 3212 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3213 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3214 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3215 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
2967 3216
2968 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);
2969 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3218 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
2970 cv_pool_handler = get_cv ("Coro::_pool_handler", 0); SvREADONLY_on (cv_pool_handler); 3219 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
2971 cv_coro_new = get_cv ("Coro::new", 0); SvREADONLY_on (cv_coro_new); 3220 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);
2972 3221
2973 coro_stash = gv_stashpv ("Coro", TRUE); 3222 coro_stash = gv_stashpv ("Coro", TRUE);
2974 3223
2975 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 3224 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
2976 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 3225 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
2984 3233
2985 { 3234 {
2986 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3235 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
2987 3236
2988 coroapi.schedule = api_schedule; 3237 coroapi.schedule = api_schedule;
3238 coroapi.schedule_to = api_schedule_to;
2989 coroapi.cede = api_cede; 3239 coroapi.cede = api_cede;
2990 coroapi.cede_notself = api_cede_notself; 3240 coroapi.cede_notself = api_cede_notself;
2991 coroapi.ready = api_ready; 3241 coroapi.ready = api_ready;
2992 coroapi.is_ready = api_is_ready; 3242 coroapi.is_ready = api_is_ready;
2993 coroapi.nready = coro_nready; 3243 coroapi.nready = coro_nready;
2998 SvREADONLY_on (sv); 3248 SvREADONLY_on (sv);
2999 } 3249 }
3000} 3250}
3001 3251
3002void 3252void
3253terminate (...)
3254 CODE:
3255 CORO_EXECUTE_SLF_XS (slf_init_terminate);
3256
3257void
3003schedule (...) 3258schedule (...)
3004 CODE: 3259 CODE:
3005 CORO_EXECUTE_SLF_XS (slf_init_schedule); 3260 CORO_EXECUTE_SLF_XS (slf_init_schedule);
3261
3262void
3263schedule_to (...)
3264 CODE:
3265 CORO_EXECUTE_SLF_XS (slf_init_schedule_to);
3266
3267void
3268cede_to (...)
3269 CODE:
3270 CORO_EXECUTE_SLF_XS (slf_init_cede_to);
3006 3271
3007void 3272void
3008cede (...) 3273cede (...)
3009 CODE: 3274 CODE:
3010 CORO_EXECUTE_SLF_XS (slf_init_cede); 3275 CORO_EXECUTE_SLF_XS (slf_init_cede);
3011 3276
3012void 3277void
3013cede_notself (...) 3278cede_notself (...)
3014 CODE: 3279 CODE:
3015 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3280 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3281
3282void
3283_cancel (Coro::State self)
3284 CODE:
3285 coro_state_destroy (aTHX_ self);
3286 coro_call_on_destroy (aTHX_ self);
3016 3287
3017void 3288void
3018_set_current (SV *current) 3289_set_current (SV *current)
3019 PROTOTYPE: $ 3290 PROTOTYPE: $
3020 CODE: 3291 CODE:
3091 PUSHMARK (SP); 3362 PUSHMARK (SP);
3092 EXTEND (SP, 2); 3363 EXTEND (SP, 2);
3093 PUSHs (sv_Coro); 3364 PUSHs (sv_Coro);
3094 PUSHs ((SV *)cv_pool_handler); 3365 PUSHs ((SV *)cv_pool_handler);
3095 PUTBACK; 3366 PUTBACK;
3096 call_sv (cv_coro_new, G_SCALAR); 3367 call_sv ((SV *)cv_coro_state_new, G_SCALAR);
3097 SPAGAIN; 3368 SPAGAIN;
3098 3369
3099 hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); 3370 hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs));
3100 } 3371 }
3101 3372
3106 assert (!coro->invoke_av); 3377 assert (!coro->invoke_av);
3107 coro->invoke_cb = SvREFCNT_inc (cb); 3378 coro->invoke_cb = SvREFCNT_inc (cb);
3108 coro->invoke_av = av; 3379 coro->invoke_av = av;
3109 } 3380 }
3110 3381
3111 api_ready ((SV *)hv); 3382 api_ready (aTHX_ (SV *)hv);
3112 3383
3113 if (GIMME_V != G_VOID) 3384 if (GIMME_V != G_VOID)
3114 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); 3385 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
3115 else 3386 else
3116 SvREFCNT_dec (hv); 3387 SvREFCNT_dec (hv);
3127void 3398void
3128rouse_wait (...) 3399rouse_wait (...)
3129 PROTOTYPE: ;$ 3400 PROTOTYPE: ;$
3130 PPCODE: 3401 PPCODE:
3131 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3402 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
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}
3132 3428
3133 3429
3134MODULE = Coro::State PACKAGE = PerlIO::cede 3430MODULE = Coro::State PACKAGE = PerlIO::cede
3135 3431
3136BOOT: 3432BOOT:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines