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.320 by root, Fri Nov 21 07:29:04 2008 UTC vs.
Revision 1.346 by root, Tue Jun 9 18:56:45 2009 UTC

101# define CvISXSUB_on(cv) (void)cv 101# define CvISXSUB_on(cv) (void)cv
102#endif 102#endif
103#ifndef CvISXSUB 103#ifndef CvISXSUB
104# define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE) 104# define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE)
105#endif 105#endif
106#ifndef Newx
107# define Newx(ptr,nitems,type) New (0,ptr,nitems,type)
108#endif
106 109
107/* 5.8.7 */ 110/* 5.8.7 */
108#ifndef SvRV_set 111#ifndef SvRV_set
109# define SvRV_set(s,v) SvRV(s) = (v) 112# define SvRV_set(s,v) SvRV(s) = (v)
110#endif 113#endif
130#else 133#else
131# define dSTACKLEVEL volatile void *stacklevel 134# define dSTACKLEVEL volatile void *stacklevel
132# define STACKLEVEL ((void *)&stacklevel) 135# define STACKLEVEL ((void *)&stacklevel)
133#endif 136#endif
134 137
135#define IN_DESTRUCT (PL_main_cv == Nullcv) 138#define IN_DESTRUCT PL_dirty
136 139
137#if __GNUC__ >= 3 140#if __GNUC__ >= 3
138# define attribute(x) __attribute__(x) 141# define attribute(x) __attribute__(x)
139# define expect(expr,value) __builtin_expect ((expr),(value)) 142# define expect(expr,value) __builtin_expect ((expr),(value))
140# define INLINE static inline 143# define INLINE static inline
172static HV *coro_state_stash, *coro_stash; 175static HV *coro_state_stash, *coro_stash;
173static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ 176static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
174 177
175static AV *av_destroy; /* destruction queue */ 178static AV *av_destroy; /* destruction queue */
176static SV *sv_manager; /* the manager coro */ 179static SV *sv_manager; /* the manager coro */
180static SV *sv_idle; /* $Coro::idle */
177 181
178static GV *irsgv; /* $/ */ 182static GV *irsgv; /* $/ */
179static GV *stdoutgv; /* *STDOUT */ 183static GV *stdoutgv; /* *STDOUT */
180static SV *rv_diehook; 184static SV *rv_diehook;
181static SV *rv_warnhook; 185static SV *rv_warnhook;
225 int valgrind_id; 229 int valgrind_id;
226#endif 230#endif
227 unsigned char flags; 231 unsigned char flags;
228} coro_cctx; 232} coro_cctx;
229 233
234coro_cctx *cctx_current; /* the currently running cctx */
235
236/*****************************************************************************/
237
230enum { 238enum {
231 CF_RUNNING = 0x0001, /* coroutine is running */ 239 CF_RUNNING = 0x0001, /* coroutine is running */
232 CF_READY = 0x0002, /* coroutine is ready */ 240 CF_READY = 0x0002, /* coroutine is ready */
233 CF_NEW = 0x0004, /* has never been switched to */ 241 CF_NEW = 0x0004, /* has never been switched to */
234 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 242 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
243 CF_SUSPENDED = 0x0010, /* coroutine can't be scheduled */
235}; 244};
236 245
237/* the structure where most of the perl state is stored, overlaid on the cxstack */ 246/* the structure where most of the perl state is stored, overlaid on the cxstack */
238typedef struct 247typedef struct
239{ 248{
240 SV *defsv; 249 SV *defsv;
241 AV *defav; 250 AV *defav;
242 SV *errsv; 251 SV *errsv;
243 SV *irsgv; 252 SV *irsgv;
253 HV *hinthv;
244#define VAR(name,type) type name; 254#define VAR(name,type) type name;
245# include "state.h" 255# include "state.h"
246#undef VAR 256#undef VAR
247} perl_slots; 257} perl_slots;
248 258
275 285
276 /* async_pool */ 286 /* async_pool */
277 SV *saved_deffh; 287 SV *saved_deffh;
278 SV *invoke_cb; 288 SV *invoke_cb;
279 AV *invoke_av; 289 AV *invoke_av;
290
291 /* on_enter/on_leave */
292 AV *on_enter;
293 AV *on_leave;
280 294
281 /* linked list */ 295 /* linked list */
282 struct coro *next, *prev; 296 struct coro *next, *prev;
283}; 297};
284 298
343INLINE CV * 357INLINE CV *
344coro_sv_2cv (pTHX_ SV *sv) 358coro_sv_2cv (pTHX_ SV *sv)
345{ 359{
346 HV *st; 360 HV *st;
347 GV *gvp; 361 GV *gvp;
348 return sv_2cv (sv, &st, &gvp, 0); 362 CV *cv = sv_2cv (sv, &st, &gvp, 0);
363
364 if (!cv)
365 croak ("code reference expected");
366
367 return cv;
349} 368}
369
370/*****************************************************************************/
371/* magic glue */
372
373#define CORO_MAGIC_type_cv 26
374#define CORO_MAGIC_type_state PERL_MAGIC_ext
375
376#define CORO_MAGIC_NN(sv, type) \
377 (expect_true (SvMAGIC (sv)->mg_type == type) \
378 ? SvMAGIC (sv) \
379 : mg_find (sv, type))
380
381#define CORO_MAGIC(sv, type) \
382 (expect_true (SvMAGIC (sv)) \
383 ? CORO_MAGIC_NN (sv, type) \
384 : 0)
385
386#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
387#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
388
389INLINE struct coro *
390SvSTATE_ (pTHX_ SV *coro)
391{
392 HV *stash;
393 MAGIC *mg;
394
395 if (SvROK (coro))
396 coro = SvRV (coro);
397
398 if (expect_false (SvTYPE (coro) != SVt_PVHV))
399 croak ("Coro::State object required");
400
401 stash = SvSTASH (coro);
402 if (expect_false (stash != coro_stash && stash != coro_state_stash))
403 {
404 /* very slow, but rare, check */
405 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
406 croak ("Coro::State object required");
407 }
408
409 mg = CORO_MAGIC_state (coro);
410 return (struct coro *)mg->mg_ptr;
411}
412
413#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
414
415/* faster than SvSTATE, but expects a coroutine hv */
416#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr)
417#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
418
419/*****************************************************************************/
420/* padlist management and caching */
350 421
351static AV * 422static AV *
352coro_clone_padlist (pTHX_ CV *cv) 423coro_derive_padlist (pTHX_ CV *cv)
353{ 424{
354 AV *padlist = CvPADLIST (cv); 425 AV *padlist = CvPADLIST (cv);
355 AV *newpadlist, *newpad; 426 AV *newpadlist, *newpad;
356 427
357 newpadlist = newAV (); 428 newpadlist = newAV ();
362 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 433 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
363#endif 434#endif
364 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 435 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
365 --AvFILLp (padlist); 436 --AvFILLp (padlist);
366 437
367 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); 438 av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0]));
368 av_store (newpadlist, 1, (SV *)newpad); 439 av_store (newpadlist, 1, (SV *)newpad);
369 440
370 return newpadlist; 441 return newpadlist;
371} 442}
372 443
373static void 444static void
374free_padlist (pTHX_ AV *padlist) 445free_padlist (pTHX_ AV *padlist)
375{ 446{
376 /* may be during global destruction */ 447 /* may be during global destruction */
377 if (SvREFCNT (padlist)) 448 if (!IN_DESTRUCT)
378 { 449 {
379 I32 i = AvFILLp (padlist); 450 I32 i = AvFILLp (padlist);
380 while (i >= 0) 451
452 while (i > 0) /* special-case index 0 */
381 { 453 {
382 SV **svp = av_fetch (padlist, i--, FALSE); 454 /* we try to be extra-careful here */
383 if (svp) 455 AV *av = (AV *)AvARRAY (padlist)[i--];
384 { 456 I32 j = AvFILLp (av);
385 SV *sv; 457
386 while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) 458 while (j >= 0)
459 SvREFCNT_dec (AvARRAY (av)[j--]);
460
461 AvFILLp (av) = -1;
387 SvREFCNT_dec (sv); 462 SvREFCNT_dec (av);
388
389 SvREFCNT_dec (*svp);
390 }
391 } 463 }
392 464
465 SvREFCNT_dec (AvARRAY (padlist)[0]);
466
467 AvFILLp (padlist) = -1;
393 SvREFCNT_dec ((SV*)padlist); 468 SvREFCNT_dec ((SV*)padlist);
394 } 469 }
395} 470}
396 471
397static int 472static int
406 481
407 SvREFCNT_dec (av); /* sv_magicext increased the refcount */ 482 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
408 483
409 return 0; 484 return 0;
410} 485}
411
412#define CORO_MAGIC_type_cv 26
413#define CORO_MAGIC_type_state PERL_MAGIC_ext
414 486
415static MGVTBL coro_cv_vtbl = { 487static MGVTBL coro_cv_vtbl = {
416 0, 0, 0, 0, 488 0, 0, 0, 0,
417 coro_cv_free 489 coro_cv_free
418}; 490};
419
420#define CORO_MAGIC_NN(sv, type) \
421 (expect_true (SvMAGIC (sv)->mg_type == type) \
422 ? SvMAGIC (sv) \
423 : mg_find (sv, type))
424
425#define CORO_MAGIC(sv, type) \
426 (expect_true (SvMAGIC (sv)) \
427 ? CORO_MAGIC_NN (sv, type) \
428 : 0)
429
430#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
431#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
432
433INLINE struct coro *
434SvSTATE_ (pTHX_ SV *coro)
435{
436 HV *stash;
437 MAGIC *mg;
438
439 if (SvROK (coro))
440 coro = SvRV (coro);
441
442 if (expect_false (SvTYPE (coro) != SVt_PVHV))
443 croak ("Coro::State object required");
444
445 stash = SvSTASH (coro);
446 if (expect_false (stash != coro_stash && stash != coro_state_stash))
447 {
448 /* very slow, but rare, check */
449 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
450 croak ("Coro::State object required");
451 }
452
453 mg = CORO_MAGIC_state (coro);
454 return (struct coro *)mg->mg_ptr;
455}
456
457#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
458
459/* faster than SvSTATE, but expects a coroutine hv */
460#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr)
461#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
462 491
463/* the next two functions merely cache the padlists */ 492/* the next two functions merely cache the padlists */
464static void 493static void
465get_padlist (pTHX_ CV *cv) 494get_padlist (pTHX_ CV *cv)
466{ 495{
477 CV *cp = Perl_cv_clone (aTHX_ cv); 506 CV *cp = Perl_cv_clone (aTHX_ cv);
478 CvPADLIST (cv) = CvPADLIST (cp); 507 CvPADLIST (cv) = CvPADLIST (cp);
479 CvPADLIST (cp) = 0; 508 CvPADLIST (cp) = 0;
480 SvREFCNT_dec (cp); 509 SvREFCNT_dec (cp);
481#else 510#else
482 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 511 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv);
483#endif 512#endif
484 } 513 }
485} 514}
486 515
487static void 516static void
494 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); 523 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
495 524
496 av = (AV *)mg->mg_obj; 525 av = (AV *)mg->mg_obj;
497 526
498 if (expect_false (AvFILLp (av) >= AvMAX (av))) 527 if (expect_false (AvFILLp (av) >= AvMAX (av)))
499 av_extend (av, AvMAX (av) + 1); 528 av_extend (av, AvFILLp (av) + 1);
500 529
501 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 530 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
502} 531}
503 532
504/** load & save, init *******************************************************/ 533/** load & save, init *******************************************************/
534
535static void
536on_enterleave_call (pTHX_ SV *cb);
505 537
506static void 538static void
507load_perl (pTHX_ Coro__State c) 539load_perl (pTHX_ Coro__State c)
508{ 540{
509 perl_slots *slot = c->slot; 541 perl_slots *slot = c->slot;
510 c->slot = 0; 542 c->slot = 0;
511 543
512 PL_mainstack = c->mainstack; 544 PL_mainstack = c->mainstack;
513 545
514 GvSV (PL_defgv) = slot->defsv; 546 GvSV (PL_defgv) = slot->defsv;
515 GvAV (PL_defgv) = slot->defav; 547 GvAV (PL_defgv) = slot->defav;
516 GvSV (PL_errgv) = slot->errsv; 548 GvSV (PL_errgv) = slot->errsv;
517 GvSV (irsgv) = slot->irsgv; 549 GvSV (irsgv) = slot->irsgv;
550 GvHV (PL_hintgv) = slot->hinthv;
518 551
519 #define VAR(name,type) PL_ ## name = slot->name; 552 #define VAR(name,type) PL_ ## name = slot->name;
520 # include "state.h" 553 # include "state.h"
521 #undef VAR 554 #undef VAR
522 555
536 PUTBACK; 569 PUTBACK;
537 } 570 }
538 571
539 slf_frame = c->slf_frame; 572 slf_frame = c->slf_frame;
540 CORO_THROW = c->except; 573 CORO_THROW = c->except;
574
575 if (expect_false (c->on_enter))
576 {
577 int i;
578
579 for (i = 0; i <= AvFILLp (c->on_enter); ++i)
580 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]);
581 }
541} 582}
542 583
543static void 584static void
544save_perl (pTHX_ Coro__State c) 585save_perl (pTHX_ Coro__State c)
545{ 586{
587 if (expect_false (c->on_leave))
588 {
589 int i;
590
591 for (i = AvFILLp (c->on_leave); i >= 0; --i)
592 on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]);
593 }
594
546 c->except = CORO_THROW; 595 c->except = CORO_THROW;
547 c->slf_frame = slf_frame; 596 c->slf_frame = slf_frame;
548 597
549 { 598 {
550 dSP; 599 dSP;
608 c->mainstack = PL_mainstack; 657 c->mainstack = PL_mainstack;
609 658
610 { 659 {
611 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 660 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
612 661
613 slot->defav = GvAV (PL_defgv); 662 slot->defav = GvAV (PL_defgv);
614 slot->defsv = DEFSV; 663 slot->defsv = DEFSV;
615 slot->errsv = ERRSV; 664 slot->errsv = ERRSV;
616 slot->irsgv = GvSV (irsgv); 665 slot->irsgv = GvSV (irsgv);
666 slot->hinthv = GvHV (PL_hintgv);
617 667
618 #define VAR(name,type) slot->name = PL_ ## name; 668 #define VAR(name,type) slot->name = PL_ ## name;
619 # include "state.h" 669 # include "state.h"
620 #undef VAR 670 #undef VAR
621 } 671 }
748#endif 798#endif
749 799
750/* 800/*
751 * This overrides the default magic get method of %SIG elements. 801 * This overrides the default magic get method of %SIG elements.
752 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook 802 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
753 * and instead of tryign to save and restore the hash elements, we just provide 803 * and instead of trying to save and restore the hash elements, we just provide
754 * readback here. 804 * readback here.
755 * We only do this when the hook is != 0, as they are often set to 0 temporarily,
756 * not expecting this to actually change the hook. This is a potential problem
757 * when a schedule happens then, but we ignore this.
758 */ 805 */
759static int 806static int
760coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) 807coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
761{ 808{
762 const char *s = MgPV_nolen_const (mg); 809 const char *s = MgPV_nolen_const (mg);
815 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 862 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
816 863
817 if (svp) 864 if (svp)
818 { 865 {
819 SV *old = *svp; 866 SV *old = *svp;
820 *svp = newSVsv (sv); 867 *svp = SvOK (sv) ? newSVsv (sv) : 0;
821 SvREFCNT_dec (old); 868 SvREFCNT_dec (old);
822 return 0; 869 return 0;
823 } 870 }
824 } 871 }
825 872
857 904
858 PL_runops = RUNOPS_DEFAULT; 905 PL_runops = RUNOPS_DEFAULT;
859 PL_curcop = &PL_compiling; 906 PL_curcop = &PL_compiling;
860 PL_in_eval = EVAL_NULL; 907 PL_in_eval = EVAL_NULL;
861 PL_comppad = 0; 908 PL_comppad = 0;
909 PL_comppad_name = 0;
910 PL_comppad_name_fill = 0;
911 PL_comppad_name_floor = 0;
862 PL_curpm = 0; 912 PL_curpm = 0;
863 PL_curpad = 0; 913 PL_curpad = 0;
864 PL_localizing = 0; 914 PL_localizing = 0;
865 PL_dirty = 0; 915 PL_dirty = 0;
866 PL_restartop = 0; 916 PL_restartop = 0;
867#if PERL_VERSION_ATLEAST (5,10,0) 917#if PERL_VERSION_ATLEAST (5,10,0)
868 PL_parser = 0; 918 PL_parser = 0;
869#endif 919#endif
920 PL_hints = 0;
870 921
871 /* recreate the die/warn hooks */ 922 /* recreate the die/warn hooks */
872 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); 923 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook );
873 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); 924 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook);
874 925
875 GvSV (PL_defgv) = newSV (0); 926 GvSV (PL_defgv) = newSV (0);
876 GvAV (PL_defgv) = coro->args; coro->args = 0; 927 GvAV (PL_defgv) = coro->args; coro->args = 0;
877 GvSV (PL_errgv) = newSV (0); 928 GvSV (PL_errgv) = newSV (0);
878 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 929 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
930 GvHV (PL_hintgv) = 0;
879 PL_rs = newSVsv (GvSV (irsgv)); 931 PL_rs = newSVsv (GvSV (irsgv));
880 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 932 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
881 933
882 { 934 {
883 dSP; 935 dSP;
912 /* copy throw, in case it was set before coro_setup */ 964 /* copy throw, in case it was set before coro_setup */
913 CORO_THROW = coro->except; 965 CORO_THROW = coro->except;
914} 966}
915 967
916static void 968static void
917coro_destruct (pTHX_ struct coro *coro) 969coro_unwind_stacks (pTHX)
918{ 970{
919 if (!IN_DESTRUCT) 971 if (!IN_DESTRUCT)
920 { 972 {
921 /* restore all saved variables and stuff */ 973 /* restore all saved variables and stuff */
922 LEAVE_SCOPE (0); 974 LEAVE_SCOPE (0);
930 POPSTACK_TO (PL_mainstack); 982 POPSTACK_TO (PL_mainstack);
931 983
932 /* unwind main stack */ 984 /* unwind main stack */
933 dounwind (-1); 985 dounwind (-1);
934 } 986 }
987}
988
989static void
990coro_destruct_perl (pTHX_ struct coro *coro)
991{
992 coro_unwind_stacks (aTHX);
935 993
936 SvREFCNT_dec (GvSV (PL_defgv)); 994 SvREFCNT_dec (GvSV (PL_defgv));
937 SvREFCNT_dec (GvAV (PL_defgv)); 995 SvREFCNT_dec (GvAV (PL_defgv));
938 SvREFCNT_dec (GvSV (PL_errgv)); 996 SvREFCNT_dec (GvSV (PL_errgv));
939 SvREFCNT_dec (PL_defoutgv); 997 SvREFCNT_dec (PL_defoutgv);
940 SvREFCNT_dec (PL_rs); 998 SvREFCNT_dec (PL_rs);
941 SvREFCNT_dec (GvSV (irsgv)); 999 SvREFCNT_dec (GvSV (irsgv));
1000 SvREFCNT_dec (GvHV (PL_hintgv));
942 1001
943 SvREFCNT_dec (PL_diehook); 1002 SvREFCNT_dec (PL_diehook);
944 SvREFCNT_dec (PL_warnhook); 1003 SvREFCNT_dec (PL_warnhook);
945 1004
946 SvREFCNT_dec (coro->saved_deffh); 1005 SvREFCNT_dec (coro->saved_deffh);
964static int 1023static int
965runops_trace (pTHX) 1024runops_trace (pTHX)
966{ 1025{
967 COP *oldcop = 0; 1026 COP *oldcop = 0;
968 int oldcxix = -2; 1027 int oldcxix = -2;
969 struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */
970 coro_cctx *cctx = coro->cctx;
971 1028
972 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 1029 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
973 { 1030 {
974 PERL_ASYNC_CHECK (); 1031 PERL_ASYNC_CHECK ();
975 1032
976 if (cctx->flags & CC_TRACE_ALL) 1033 if (cctx_current->flags & CC_TRACE_ALL)
977 { 1034 {
978 if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) 1035 if (PL_op->op_type == OP_LEAVESUB && cctx_current->flags & CC_TRACE_SUB)
979 { 1036 {
980 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1037 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
981 SV **bot, **top; 1038 SV **bot, **top;
982 AV *av = newAV (); /* return values */ 1039 AV *av = newAV (); /* return values */
983 SV **cb; 1040 SV **cb;
1020 1077
1021 if (PL_curcop != &PL_compiling) 1078 if (PL_curcop != &PL_compiling)
1022 { 1079 {
1023 SV **cb; 1080 SV **cb;
1024 1081
1025 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) 1082 if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB)
1026 { 1083 {
1027 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1084 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1028 1085
1029 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 1086 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
1030 { 1087 {
1053 } 1110 }
1054 1111
1055 oldcxix = cxstack_ix; 1112 oldcxix = cxstack_ix;
1056 } 1113 }
1057 1114
1058 if (cctx->flags & CC_TRACE_LINE) 1115 if (cctx_current->flags & CC_TRACE_LINE)
1059 { 1116 {
1060 dSP; 1117 dSP;
1061 1118
1062 PL_runops = RUNOPS_DEFAULT; 1119 PL_runops = RUNOPS_DEFAULT;
1063 ENTER; 1120 ENTER;
1082 1139
1083 TAINT_NOT; 1140 TAINT_NOT;
1084 return 0; 1141 return 0;
1085} 1142}
1086 1143
1087static struct coro_cctx *cctx_ssl_cctx;
1088static struct CoroSLF cctx_ssl_frame; 1144static struct CoroSLF cctx_ssl_frame;
1089 1145
1090static void 1146static void
1091slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) 1147slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1092{ 1148{
1093 ta->prev = (struct coro *)cctx_ssl_cctx;
1094 ta->next = 0; 1149 ta->prev = 0;
1095} 1150}
1096 1151
1097static int 1152static int
1098slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) 1153slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1099{ 1154{
1102 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ 1157 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1103} 1158}
1104 1159
1105/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ 1160/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1106static void NOINLINE 1161static void NOINLINE
1107cctx_prepare (pTHX_ coro_cctx *cctx) 1162cctx_prepare (pTHX)
1108{ 1163{
1109 PL_top_env = &PL_start_env; 1164 PL_top_env = &PL_start_env;
1110 1165
1111 if (cctx->flags & CC_TRACE) 1166 if (cctx_current->flags & CC_TRACE)
1112 PL_runops = runops_trace; 1167 PL_runops = runops_trace;
1113 1168
1114 /* we already must be executing an SLF op, there is no other valid way 1169 /* we already must be executing an SLF op, there is no other valid way
1115 * that can lead to creation of a new cctx */ 1170 * that can lead to creation of a new cctx */
1116 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", 1171 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)",
1117 slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); 1172 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1118 1173
1119 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ 1174 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */
1120 cctx_ssl_cctx = cctx;
1121 cctx_ssl_frame = slf_frame; 1175 cctx_ssl_frame = slf_frame;
1122 1176
1123 slf_frame.prepare = slf_prepare_set_stacklevel; 1177 slf_frame.prepare = slf_prepare_set_stacklevel;
1124 slf_frame.check = slf_check_set_stacklevel; 1178 slf_frame.check = slf_check_set_stacklevel;
1125} 1179}
1148 /* normally we would need to skip the entersub here */ 1202 /* normally we would need to skip the entersub here */
1149 /* not doing so will re-execute it, which is exactly what we want */ 1203 /* not doing so will re-execute it, which is exactly what we want */
1150 /* PL_nop = PL_nop->op_next */ 1204 /* PL_nop = PL_nop->op_next */
1151 1205
1152 /* inject a fake subroutine call to cctx_init */ 1206 /* inject a fake subroutine call to cctx_init */
1153 cctx_prepare (aTHX_ (coro_cctx *)arg); 1207 cctx_prepare (aTHX);
1154 1208
1155 /* cctx_run is the alternative tail of transfer() */ 1209 /* cctx_run is the alternative tail of transfer() */
1156 transfer_tail (aTHX); 1210 transfer_tail (aTHX);
1157 1211
1158 /* somebody or something will hit me for both perl_run and PL_restartop */ 1212 /* somebody or something will hit me for both perl_run and PL_restartop */
1166 /* 1220 /*
1167 * If perl-run returns we assume exit() was being called or the coro 1221 * If perl-run returns we assume exit() was being called or the coro
1168 * fell off the end, which seems to be the only valid (non-bug) 1222 * fell off the end, which seems to be the only valid (non-bug)
1169 * reason for perl_run to return. We try to exit by jumping to the 1223 * reason for perl_run to return. We try to exit by jumping to the
1170 * bootstrap-time "top" top_env, as we cannot restore the "main" 1224 * bootstrap-time "top" top_env, as we cannot restore the "main"
1171 * coroutine as Coro has no such concept 1225 * coroutine as Coro has no such concept.
1226 * This actually isn't valid with the pthread backend, but OSes requiring
1227 * that backend are too broken to do it in a standards-compliant way.
1172 */ 1228 */
1173 PL_top_env = main_top_env; 1229 PL_top_env = main_top_env;
1174 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1230 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1175 } 1231 }
1176} 1232}
1253cctx_destroy (coro_cctx *cctx) 1309cctx_destroy (coro_cctx *cctx)
1254{ 1310{
1255 if (!cctx) 1311 if (!cctx)
1256 return; 1312 return;
1257 1313
1314 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary?
1315
1258 --cctx_count; 1316 --cctx_count;
1259 coro_destroy (&cctx->cctx); 1317 coro_destroy (&cctx->cctx);
1260 1318
1261 /* coro_transfer creates new, empty cctx's */ 1319 /* coro_transfer creates new, empty cctx's */
1262 if (cctx->sptr) 1320 if (cctx->sptr)
1325 /* TODO: throwing up here is considered harmful */ 1383 /* TODO: throwing up here is considered harmful */
1326 1384
1327 if (expect_true (prev != next)) 1385 if (expect_true (prev != next))
1328 { 1386 {
1329 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1387 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1330 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,"); 1388 croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,");
1331 1389
1332 if (expect_false (next->flags & CF_RUNNING))
1333 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1334
1335 if (expect_false (next->flags & CF_DESTROYED)) 1390 if (expect_false (next->flags & (CF_RUNNING | CF_DESTROYED | CF_SUSPENDED)))
1336 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states,"); 1391 croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,");
1337 1392
1338#if !PERL_VERSION_ATLEAST (5,10,0) 1393#if !PERL_VERSION_ATLEAST (5,10,0)
1339 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1394 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1340 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); 1395 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,");
1341#endif 1396#endif
1347transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1402transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1348{ 1403{
1349 dSTACKLEVEL; 1404 dSTACKLEVEL;
1350 1405
1351 /* sometimes transfer is only called to set idle_sp */ 1406 /* sometimes transfer is only called to set idle_sp */
1352 if (expect_false (!next)) 1407 if (expect_false (!prev))
1353 { 1408 {
1354 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1409 cctx_current->idle_sp = STACKLEVEL;
1355 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1410 assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1356 } 1411 }
1357 else if (expect_true (prev != next)) 1412 else if (expect_true (prev != next))
1358 { 1413 {
1359 coro_cctx *prev__cctx; 1414 coro_cctx *cctx_prev;
1360 1415
1361 if (expect_false (prev->flags & CF_NEW)) 1416 if (expect_false (prev->flags & CF_NEW))
1362 { 1417 {
1363 /* create a new empty/source context */ 1418 /* create a new empty/source context */
1364 prev->cctx = cctx_new_empty ();
1365 prev->flags &= ~CF_NEW; 1419 prev->flags &= ~CF_NEW;
1366 prev->flags |= CF_RUNNING; 1420 prev->flags |= CF_RUNNING;
1367 } 1421 }
1368 1422
1369 prev->flags &= ~CF_RUNNING; 1423 prev->flags &= ~CF_RUNNING;
1380 coro_setup (aTHX_ next); 1434 coro_setup (aTHX_ next);
1381 } 1435 }
1382 else 1436 else
1383 load_perl (aTHX_ next); 1437 load_perl (aTHX_ next);
1384 1438
1385 prev__cctx = prev->cctx;
1386
1387 /* possibly untie and reuse the cctx */ 1439 /* possibly untie and reuse the cctx */
1388 if (expect_true ( 1440 if (expect_true (
1389 prev__cctx->idle_sp == STACKLEVEL 1441 cctx_current->idle_sp == STACKLEVEL
1390 && !(prev__cctx->flags & CC_TRACE) 1442 && !(cctx_current->flags & CC_TRACE)
1391 && !force_cctx 1443 && !force_cctx
1392 )) 1444 ))
1393 { 1445 {
1394 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1446 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1395 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); 1447 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te));
1396 1448
1397 prev->cctx = 0;
1398
1399 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1449 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */
1400 /* without this the next cctx_get might destroy the prev__cctx while still in use */ 1450 /* without this the next cctx_get might destroy the running cctx while still in use */
1401 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1451 if (expect_false (CCTX_EXPIRED (cctx_current)))
1402 if (!next->cctx) 1452 if (expect_true (!next->cctx))
1403 next->cctx = cctx_get (aTHX); 1453 next->cctx = cctx_get (aTHX);
1404 1454
1405 cctx_put (prev__cctx); 1455 cctx_put (cctx_current);
1406 } 1456 }
1457 else
1458 prev->cctx = cctx_current;
1407 1459
1408 ++next->usecount; 1460 ++next->usecount;
1409 1461
1410 if (expect_true (!next->cctx)) 1462 cctx_prev = cctx_current;
1411 next->cctx = cctx_get (aTHX); 1463 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1412 1464
1413 if (expect_false (prev__cctx != next->cctx)) 1465 next->cctx = 0;
1466
1467 if (expect_false (cctx_prev != cctx_current))
1414 { 1468 {
1415 prev__cctx->top_env = PL_top_env; 1469 cctx_prev->top_env = PL_top_env;
1416 PL_top_env = next->cctx->top_env; 1470 PL_top_env = cctx_current->top_env;
1417 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1471 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1418 } 1472 }
1419 1473
1420 transfer_tail (aTHX); 1474 transfer_tail (aTHX);
1421 } 1475 }
1422} 1476}
1430coro_state_destroy (pTHX_ struct coro *coro) 1484coro_state_destroy (pTHX_ struct coro *coro)
1431{ 1485{
1432 if (coro->flags & CF_DESTROYED) 1486 if (coro->flags & CF_DESTROYED)
1433 return 0; 1487 return 0;
1434 1488
1435 if (coro->on_destroy) 1489 if (coro->on_destroy && !PL_dirty)
1436 coro->on_destroy (aTHX_ coro); 1490 coro->on_destroy (aTHX_ coro);
1437 1491
1438 coro->flags |= CF_DESTROYED; 1492 coro->flags |= CF_DESTROYED;
1439 1493
1440 if (coro->flags & CF_READY) 1494 if (coro->flags & CF_READY)
1444 --coro_nready; 1498 --coro_nready;
1445 } 1499 }
1446 else 1500 else
1447 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ 1501 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
1448 1502
1449 if (coro->mainstack && coro->mainstack != main_mainstack) 1503 if (coro->mainstack
1504 && coro->mainstack != main_mainstack
1505 && coro->slot
1506 && !PL_dirty)
1450 { 1507 {
1451 struct coro temp; 1508 struct coro *current = SvSTATE_current;
1452 1509
1453 assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING))); 1510 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack));
1454 1511
1455 save_perl (aTHX_ &temp); 1512 save_perl (aTHX_ current);
1456 load_perl (aTHX_ coro); 1513 load_perl (aTHX_ coro);
1457 1514
1458 coro_destruct (aTHX_ coro); 1515 coro_destruct_perl (aTHX_ coro);
1459 1516
1460 load_perl (aTHX_ &temp); 1517 load_perl (aTHX_ current);
1461 1518
1462 coro->slot = 0; 1519 coro->slot = 0;
1463 } 1520 }
1464 1521
1465 cctx_destroy (coro->cctx); 1522 cctx_destroy (coro->cctx);
1577{ 1634{
1578 struct coro *coro; 1635 struct coro *coro;
1579 SV *sv_hook; 1636 SV *sv_hook;
1580 void (*xs_hook)(void); 1637 void (*xs_hook)(void);
1581 1638
1582 if (SvROK (coro_sv))
1583 coro_sv = SvRV (coro_sv);
1584
1585 coro = SvSTATE (coro_sv); 1639 coro = SvSTATE (coro_sv);
1586 1640
1587 if (coro->flags & CF_READY) 1641 if (coro->flags & CF_READY)
1588 return 0; 1642 return 0;
1589 1643
1649 if (expect_true (next_sv)) 1703 if (expect_true (next_sv))
1650 { 1704 {
1651 struct coro *next = SvSTATE_hv (next_sv); 1705 struct coro *next = SvSTATE_hv (next_sv);
1652 1706
1653 /* cannot transfer to destroyed coros, skip and look for next */ 1707 /* cannot transfer to destroyed coros, skip and look for next */
1654 if (expect_false (next->flags & CF_DESTROYED)) 1708 if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED)))
1655 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ 1709 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */
1656 else 1710 else
1657 { 1711 {
1658 next->flags &= ~CF_READY; 1712 next->flags &= ~CF_READY;
1659 --coro_nready; 1713 --coro_nready;
1663 } 1717 }
1664 } 1718 }
1665 else 1719 else
1666 { 1720 {
1667 /* nothing to schedule: call the idle handler */ 1721 /* nothing to schedule: call the idle handler */
1722 if (SvROK (sv_idle)
1723 && SvOBJECT (SvRV (sv_idle)))
1724 {
1725 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1726 api_ready (aTHX_ SvRV (sv_idle));
1727 --coro_nready;
1728 }
1729 else
1730 {
1668 dSP; 1731 dSP;
1669 1732
1670 ENTER; 1733 ENTER;
1671 SAVETMPS; 1734 SAVETMPS;
1672 1735
1673 PUSHMARK (SP); 1736 PUSHMARK (SP);
1674 PUTBACK; 1737 PUTBACK;
1675 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); 1738 call_sv (sv_idle, G_VOID | G_DISCARD);
1676 1739
1677 FREETMPS; 1740 FREETMPS;
1678 LEAVE; 1741 LEAVE;
1742 }
1679 } 1743 }
1680 } 1744 }
1681} 1745}
1682 1746
1683INLINE void 1747INLINE void
1754static void 1818static void
1755api_trace (pTHX_ SV *coro_sv, int flags) 1819api_trace (pTHX_ SV *coro_sv, int flags)
1756{ 1820{
1757 struct coro *coro = SvSTATE (coro_sv); 1821 struct coro *coro = SvSTATE (coro_sv);
1758 1822
1823 if (coro->flags & CF_RUNNING)
1824 croak ("cannot enable tracing on a running coroutine, caught");
1825
1759 if (flags & CC_TRACE) 1826 if (flags & CC_TRACE)
1760 { 1827 {
1761 if (!coro->cctx) 1828 if (!coro->cctx)
1762 coro->cctx = cctx_new_run (); 1829 coro->cctx = cctx_new_run ();
1763 else if (!(coro->cctx->flags & CC_TRACE)) 1830 else if (!(coro->cctx->flags & CC_TRACE))
1764 croak ("cannot enable tracing on coroutine with custom stack,"); 1831 croak ("cannot enable tracing on coroutine with custom stack, caught");
1765 1832
1766 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1833 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1767 } 1834 }
1768 else if (coro->cctx && coro->cctx->flags & CC_TRACE) 1835 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1769 { 1836 {
1825 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ 1892 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1826 api_ready (aTHX_ sv_manager); 1893 api_ready (aTHX_ sv_manager);
1827 1894
1828 frame->prepare = prepare_schedule; 1895 frame->prepare = prepare_schedule;
1829 frame->check = slf_check_repeat; 1896 frame->check = slf_check_repeat;
1897
1898 /* as a minor optimisation, we could unwind all stacks here */
1899 /* but that puts extra pressure on pp_slf, and is not worth much */
1900 /*coro_unwind_stacks (aTHX);*/
1830} 1901}
1831 1902
1832/*****************************************************************************/ 1903/*****************************************************************************/
1833/* async pool handler */ 1904/* async pool handler */
1834 1905
1917 SV *data = (SV *)GENSUB_ARG; 1988 SV *data = (SV *)GENSUB_ARG;
1918 1989
1919 if (SvTYPE (SvRV (data)) != SVt_PVAV) 1990 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1920 { 1991 {
1921 /* first call, set args */ 1992 /* first call, set args */
1993 SV *coro = SvRV (data);
1922 AV *av = newAV (); 1994 AV *av = newAV ();
1923 SV *coro = SvRV (data);
1924 1995
1925 SvRV_set (data, (SV *)av); 1996 SvRV_set (data, (SV *)av);
1926 api_ready (aTHX_ coro);
1927 SvREFCNT_dec (coro);
1928 1997
1929 /* better take a full copy of the arguments */ 1998 /* better take a full copy of the arguments */
1930 while (items--) 1999 while (items--)
1931 av_store (av, items, newSVsv (ST (items))); 2000 av_store (av, items, newSVsv (ST (items)));
2001
2002 api_ready (aTHX_ coro);
2003 SvREFCNT_dec (coro);
1932 } 2004 }
1933 2005
1934 XSRETURN_EMPTY; 2006 XSRETURN_EMPTY;
1935} 2007}
1936 2008
1953 2025
1954 EXTEND (SP, AvFILLp (av) + 1); 2026 EXTEND (SP, AvFILLp (av) + 1);
1955 for (i = 0; i <= AvFILLp (av); ++i) 2027 for (i = 0; i <= AvFILLp (av); ++i)
1956 PUSHs (sv_2mortal (AvARRAY (av)[i])); 2028 PUSHs (sv_2mortal (AvARRAY (av)[i]));
1957 2029
1958 /* we have stolen the elements, so ste length to zero and free */ 2030 /* we have stolen the elements, so set length to zero and free */
1959 AvFILLp (av) = -1; 2031 AvFILLp (av) = -1;
1960 av_undef (av); 2032 av_undef (av);
1961 2033
1962 PUTBACK; 2034 PUTBACK;
1963 } 2035 }
2253 2325
2254 PL_op->op_ppaddr = pp_slf; 2326 PL_op->op_ppaddr = pp_slf;
2255 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ 2327 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2256 2328
2257 PL_op = (OP *)&slf_restore; 2329 PL_op = (OP *)&slf_restore;
2330}
2331
2332/*****************************************************************************/
2333/* dynamic wind */
2334
2335static void
2336on_enterleave_call (pTHX_ SV *cb)
2337{
2338 dSP;
2339
2340 PUSHSTACK;
2341
2342 PUSHMARK (SP);
2343 PUTBACK;
2344 call_sv (cb, G_VOID | G_DISCARD);
2345 SPAGAIN;
2346
2347 POPSTACK;
2348}
2349
2350static SV *
2351coro_avp_pop_and_free (pTHX_ AV **avp)
2352{
2353 AV *av = *avp;
2354 SV *res = av_pop (av);
2355
2356 if (AvFILLp (av) < 0)
2357 {
2358 *avp = 0;
2359 SvREFCNT_dec (av);
2360 }
2361
2362 return res;
2363}
2364
2365static void
2366coro_pop_on_enter (pTHX_ void *coro)
2367{
2368 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_enter);
2369 SvREFCNT_dec (cb);
2370}
2371
2372static void
2373coro_pop_on_leave (pTHX_ void *coro)
2374{
2375 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave);
2376 on_enterleave_call (aTHX_ sv_2mortal (cb));
2258} 2377}
2259 2378
2260/*****************************************************************************/ 2379/*****************************************************************************/
2261/* PerlIO::cede */ 2380/* PerlIO::cede */
2262 2381
2343 SV **ary; 2462 SV **ary;
2344 2463
2345 /* unfortunately, building manually saves memory */ 2464 /* unfortunately, building manually saves memory */
2346 Newx (ary, 2, SV *); 2465 Newx (ary, 2, SV *);
2347 AvALLOC (av) = ary; 2466 AvALLOC (av) = ary;
2467#if PERL_VERSION_ATLEAST (5,10,0)
2348 /*AvARRAY (av) = ary;*/ 2468 AvARRAY (av) = ary;
2469#else
2349 SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ 2470 /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */
2471 /* -DDEBUGGING flags this as a bug, despite it perfectly working */
2472 SvPVX ((SV *)av) = (char *)ary;
2473#endif
2350 AvMAX (av) = 1; 2474 AvMAX (av) = 1;
2351 AvFILLp (av) = 0; 2475 AvFILLp (av) = 0;
2352 ary [0] = newSViv (count); 2476 ary [0] = newSViv (count);
2353 2477
2354 return newRV_noinc ((SV *)av); 2478 return newRV_noinc ((SV *)av);
2724 XSRETURN_EMPTY; 2848 XSRETURN_EMPTY;
2725} 2849}
2726 2850
2727/*****************************************************************************/ 2851/*****************************************************************************/
2728 2852
2853#if CORO_CLONE
2854# include "clone.c"
2855#endif
2856
2729MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 2857MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2730 2858
2731PROTOTYPES: DISABLE 2859PROTOTYPES: DISABLE
2732 2860
2733BOOT: 2861BOOT:
2736# if CORO_PTHREAD 2864# if CORO_PTHREAD
2737 coro_thx = PERL_GET_CONTEXT; 2865 coro_thx = PERL_GET_CONTEXT;
2738# endif 2866# endif
2739#endif 2867#endif
2740 BOOT_PAGESIZE; 2868 BOOT_PAGESIZE;
2869
2870 cctx_current = cctx_new_empty ();
2741 2871
2742 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 2872 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
2743 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 2873 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
2744 2874
2745 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; 2875 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
2871void 3001void
2872_exit (int code) 3002_exit (int code)
2873 PROTOTYPE: $ 3003 PROTOTYPE: $
2874 CODE: 3004 CODE:
2875 _exit (code); 3005 _exit (code);
3006
3007SV *
3008clone (Coro::State coro)
3009 CODE:
3010{
3011#if CORO_CLONE
3012 struct coro *ncoro = coro_clone (aTHX_ coro);
3013 MAGIC *mg;
3014 /* TODO: too much duplication */
3015 ncoro->hv = newHV ();
3016 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
3017 mg->mg_flags |= MGf_DUP;
3018 RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv));
3019#else
3020 croak ("Coro::State->clone has not been configured into this installation of Coro, realised");
3021#endif
3022}
3023 OUTPUT:
3024 RETVAL
2876 3025
2877int 3026int
2878cctx_stacksize (int new_stacksize = 0) 3027cctx_stacksize (int new_stacksize = 0)
2879 PROTOTYPE: ;$ 3028 PROTOTYPE: ;$
2880 CODE: 3029 CODE:
2930 eval = 1 3079 eval = 1
2931 CODE: 3080 CODE:
2932{ 3081{
2933 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3082 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
2934 { 3083 {
2935 struct coro temp; 3084 struct coro *current = SvSTATE_current;
2936 3085
2937 if (!(coro->flags & CF_RUNNING)) 3086 if (current != coro)
2938 { 3087 {
2939 PUTBACK; 3088 PUTBACK;
2940 save_perl (aTHX_ &temp); 3089 save_perl (aTHX_ current);
2941 load_perl (aTHX_ coro); 3090 load_perl (aTHX_ coro);
3091 SPAGAIN;
2942 } 3092 }
2943 3093
2944 {
2945 dSP;
2946 ENTER;
2947 SAVETMPS;
2948 PUTBACK;
2949 PUSHSTACK; 3094 PUSHSTACK;
3095
2950 PUSHMARK (SP); 3096 PUSHMARK (SP);
3097 PUTBACK;
2951 3098
2952 if (ix) 3099 if (ix)
2953 eval_sv (coderef, 0); 3100 eval_sv (coderef, 0);
2954 else 3101 else
2955 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3102 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
2956 3103
2957 POPSTACK; 3104 POPSTACK;
2958 SPAGAIN; 3105 SPAGAIN;
2959 FREETMPS;
2960 LEAVE;
2961 PUTBACK;
2962 }
2963 3106
2964 if (!(coro->flags & CF_RUNNING)) 3107 if (current != coro)
2965 { 3108 {
3109 PUTBACK;
2966 save_perl (aTHX_ coro); 3110 save_perl (aTHX_ coro);
2967 load_perl (aTHX_ &temp); 3111 load_perl (aTHX_ current);
2968 SPAGAIN; 3112 SPAGAIN;
2969 } 3113 }
2970 } 3114 }
2971} 3115}
2972 3116
2976 ALIAS: 3120 ALIAS:
2977 is_ready = CF_READY 3121 is_ready = CF_READY
2978 is_running = CF_RUNNING 3122 is_running = CF_RUNNING
2979 is_new = CF_NEW 3123 is_new = CF_NEW
2980 is_destroyed = CF_DESTROYED 3124 is_destroyed = CF_DESTROYED
3125 is_suspended = CF_SUSPENDED
2981 CODE: 3126 CODE:
2982 RETVAL = boolSV (coro->flags & ix); 3127 RETVAL = boolSV (coro->flags & ix);
2983 OUTPUT: 3128 OUTPUT:
2984 RETVAL 3129 RETVAL
2985 3130
3001 3146
3002SV * 3147SV *
3003has_cctx (Coro::State coro) 3148has_cctx (Coro::State coro)
3004 PROTOTYPE: $ 3149 PROTOTYPE: $
3005 CODE: 3150 CODE:
3006 RETVAL = boolSV (!!coro->cctx); 3151 /* maybe manage the running flag differently */
3152 RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING));
3007 OUTPUT: 3153 OUTPUT:
3008 RETVAL 3154 RETVAL
3009 3155
3010int 3156int
3011is_traced (Coro::State coro) 3157is_traced (Coro::State coro)
3031 3177
3032void 3178void
3033force_cctx () 3179force_cctx ()
3034 PROTOTYPE: 3180 PROTOTYPE:
3035 CODE: 3181 CODE:
3036 SvSTATE_current->cctx->idle_sp = 0; 3182 cctx_current->idle_sp = 0;
3037 3183
3038void 3184void
3039swap_defsv (Coro::State self) 3185swap_defsv (Coro::State self)
3040 PROTOTYPE: $ 3186 PROTOTYPE: $
3041 ALIAS: 3187 ALIAS:
3049 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 3195 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
3050 3196
3051 SV *tmp = *src; *src = *dst; *dst = tmp; 3197 SV *tmp = *src; *src = *dst; *dst = tmp;
3052 } 3198 }
3053 3199
3200void
3201cancel (Coro::State self)
3202 CODE:
3203 coro_state_destroy (aTHX_ self);
3204 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3205
3054 3206
3055MODULE = Coro::State PACKAGE = Coro 3207MODULE = Coro::State PACKAGE = Coro
3056 3208
3057BOOT: 3209BOOT:
3058{ 3210{
3059 int i; 3211 int i;
3060 3212
3061 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3213 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3062 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3214 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3063 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3215 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3064 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3216 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3065 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3217 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
3066 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 3218 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3067 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3219 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3068 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3220 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3221 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3069 3222
3070 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3223 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
3071 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3224 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3072 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); 3225 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3073 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); 3226 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);
3131cede_notself (...) 3284cede_notself (...)
3132 CODE: 3285 CODE:
3133 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3286 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3134 3287
3135void 3288void
3136_cancel (Coro::State self)
3137 CODE:
3138 coro_state_destroy (aTHX_ self);
3139 coro_call_on_destroy (aTHX_ self);
3140
3141void
3142_set_current (SV *current) 3289_set_current (SV *current)
3143 PROTOTYPE: $ 3290 PROTOTYPE: $
3144 CODE: 3291 CODE:
3145 SvREFCNT_dec (SvRV (coro_current)); 3292 SvREFCNT_dec (SvRV (coro_current));
3146 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); 3293 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
3188 PROTOTYPE: 3335 PROTOTYPE:
3189 CODE: 3336 CODE:
3190 RETVAL = coro_nready; 3337 RETVAL = coro_nready;
3191 OUTPUT: 3338 OUTPUT:
3192 RETVAL 3339 RETVAL
3340
3341void
3342suspend (Coro::State self)
3343 PROTOTYPE: $
3344 CODE:
3345 self->flags |= CF_SUSPENDED;
3346
3347void
3348resume (Coro::State self)
3349 PROTOTYPE: $
3350 CODE:
3351 self->flags &= ~CF_SUSPENDED;
3193 3352
3194void 3353void
3195_pool_handler (...) 3354_pool_handler (...)
3196 CODE: 3355 CODE:
3197 CORO_EXECUTE_SLF_XS (slf_init_pool_handler); 3356 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
3252rouse_wait (...) 3411rouse_wait (...)
3253 PROTOTYPE: ;$ 3412 PROTOTYPE: ;$
3254 PPCODE: 3413 PPCODE:
3255 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3414 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3256 3415
3416void
3417on_enter (SV *block)
3418 ALIAS:
3419 on_leave = 1
3420 PROTOTYPE: &
3421 CODE:
3422{
3423 struct coro *coro = SvSTATE_current;
3424 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
3425
3426 block = (SV *)coro_sv_2cv (aTHX_ block);
3427
3428 if (!*avp)
3429 *avp = newAV ();
3430
3431 av_push (*avp, SvREFCNT_inc (block));
3432
3433 if (!ix)
3434 on_enterleave_call (aTHX_ block);
3435
3436 LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3437 SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro);
3438 ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3439}
3440
3257 3441
3258MODULE = Coro::State PACKAGE = PerlIO::cede 3442MODULE = Coro::State PACKAGE = PerlIO::cede
3259 3443
3260BOOT: 3444BOOT:
3261 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3445 PerlIO_define_layer (aTHX_ &PerlIO_cede);
3271 GvSTASH (CvGV (cv)) 3455 GvSTASH (CvGV (cv))
3272 ); 3456 );
3273 OUTPUT: 3457 OUTPUT:
3274 RETVAL 3458 RETVAL
3275 3459
3276# helper for Coro::Channel 3460# helper for Coro::Channel and others
3277SV * 3461SV *
3278_alloc (int count) 3462_alloc (int count)
3279 CODE: 3463 CODE:
3280 RETVAL = coro_waitarray_new (aTHX_ count); 3464 RETVAL = coro_waitarray_new (aTHX_ count);
3281 OUTPUT: 3465 OUTPUT:
3339 for (i = 1; i <= wcount; ++i) 3523 for (i = 1; i <= wcount; ++i)
3340 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); 3524 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
3341 } 3525 }
3342} 3526}
3343 3527
3528MODULE = Coro::State PACKAGE = Coro::SemaphoreSet
3529
3530void
3531_may_delete (SV *sem, int count, int extra_refs)
3532 PPCODE:
3533{
3534 AV *av = (AV *)SvRV (sem);
3535
3536 if (SvREFCNT ((SV *)av) == 1 + extra_refs
3537 && AvFILLp (av) == 0 /* no waiters, just count */
3538 && SvIV (AvARRAY (av)[0]) == count)
3539 XSRETURN_YES;
3540
3541 XSRETURN_NO;
3542}
3543
3344MODULE = Coro::State PACKAGE = Coro::Signal 3544MODULE = Coro::State PACKAGE = Coro::Signal
3345 3545
3346SV * 3546SV *
3347new (SV *klass) 3547new (SV *klass)
3348 CODE: 3548 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines