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.318 by root, Thu Nov 20 07:02:43 2008 UTC vs.
Revision 1.348 by root, Wed Jun 10 00:20:26 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
250 260
251/* this is a structure representing a perl-level coroutine */ 261/* this is a structure representing a perl-level coroutine */
252struct coro { 262struct coro {
253 /* the C coroutine allocated to this perl coroutine, if any */ 263 /* the C coroutine allocated to this perl coroutine, if any */
254 coro_cctx *cctx; 264 coro_cctx *cctx;
265
266 /* ready queue */
267 struct coro *next_ready;
255 268
256 /* state data */ 269 /* state data */
257 struct CoroSLF slf_frame; /* saved slf frame */ 270 struct CoroSLF slf_frame; /* saved slf frame */
258 AV *mainstack; 271 AV *mainstack;
259 perl_slots *slot; /* basically the saved sp */ 272 perl_slots *slot; /* basically the saved sp */
276 /* async_pool */ 289 /* async_pool */
277 SV *saved_deffh; 290 SV *saved_deffh;
278 SV *invoke_cb; 291 SV *invoke_cb;
279 AV *invoke_av; 292 AV *invoke_av;
280 293
294 /* on_enter/on_leave */
295 AV *on_enter;
296 AV *on_leave;
297
281 /* linked list */ 298 /* linked list */
282 struct coro *next, *prev; 299 struct coro *next, *prev;
283}; 300};
284 301
285typedef struct coro *Coro__State; 302typedef struct coro *Coro__State;
300#define PRIO_MIN -4 317#define PRIO_MIN -4
301 318
302/* for Coro.pm */ 319/* for Coro.pm */
303static SV *coro_current; 320static SV *coro_current;
304static SV *coro_readyhook; 321static SV *coro_readyhook;
305static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; 322static struct coro *coro_ready [PRIO_MAX - PRIO_MIN + 1][2]; /* head|tail */
306static CV *cv_coro_run, *cv_coro_terminate; 323static CV *cv_coro_run, *cv_coro_terminate;
307static struct coro *coro_first; 324static struct coro *coro_first;
308#define coro_nready coroapi.nready 325#define coro_nready coroapi.nready
309 326
310/** lowlevel stuff **********************************************************/ 327/** lowlevel stuff **********************************************************/
343INLINE CV * 360INLINE CV *
344coro_sv_2cv (pTHX_ SV *sv) 361coro_sv_2cv (pTHX_ SV *sv)
345{ 362{
346 HV *st; 363 HV *st;
347 GV *gvp; 364 GV *gvp;
348 return sv_2cv (sv, &st, &gvp, 0); 365 CV *cv = sv_2cv (sv, &st, &gvp, 0);
366
367 if (!cv)
368 croak ("code reference expected");
369
370 return cv;
349} 371}
372
373/*****************************************************************************/
374/* magic glue */
375
376#define CORO_MAGIC_type_cv 26
377#define CORO_MAGIC_type_state PERL_MAGIC_ext
378
379#define CORO_MAGIC_NN(sv, type) \
380 (expect_true (SvMAGIC (sv)->mg_type == type) \
381 ? SvMAGIC (sv) \
382 : mg_find (sv, type))
383
384#define CORO_MAGIC(sv, type) \
385 (expect_true (SvMAGIC (sv)) \
386 ? CORO_MAGIC_NN (sv, type) \
387 : 0)
388
389#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
390#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
391
392INLINE struct coro *
393SvSTATE_ (pTHX_ SV *coro)
394{
395 HV *stash;
396 MAGIC *mg;
397
398 if (SvROK (coro))
399 coro = SvRV (coro);
400
401 if (expect_false (SvTYPE (coro) != SVt_PVHV))
402 croak ("Coro::State object required");
403
404 stash = SvSTASH (coro);
405 if (expect_false (stash != coro_stash && stash != coro_state_stash))
406 {
407 /* very slow, but rare, check */
408 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
409 croak ("Coro::State object required");
410 }
411
412 mg = CORO_MAGIC_state (coro);
413 return (struct coro *)mg->mg_ptr;
414}
415
416#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
417
418/* faster than SvSTATE, but expects a coroutine hv */
419#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr)
420#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
421
422/*****************************************************************************/
423/* padlist management and caching */
350 424
351static AV * 425static AV *
352coro_clone_padlist (pTHX_ CV *cv) 426coro_derive_padlist (pTHX_ CV *cv)
353{ 427{
354 AV *padlist = CvPADLIST (cv); 428 AV *padlist = CvPADLIST (cv);
355 AV *newpadlist, *newpad; 429 AV *newpadlist, *newpad;
356 430
357 newpadlist = newAV (); 431 newpadlist = newAV ();
362 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 436 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
363#endif 437#endif
364 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 438 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
365 --AvFILLp (padlist); 439 --AvFILLp (padlist);
366 440
367 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); 441 av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0]));
368 av_store (newpadlist, 1, (SV *)newpad); 442 av_store (newpadlist, 1, (SV *)newpad);
369 443
370 return newpadlist; 444 return newpadlist;
371} 445}
372 446
373static void 447static void
374free_padlist (pTHX_ AV *padlist) 448free_padlist (pTHX_ AV *padlist)
375{ 449{
376 /* may be during global destruction */ 450 /* may be during global destruction */
377 if (SvREFCNT (padlist)) 451 if (!IN_DESTRUCT)
378 { 452 {
379 I32 i = AvFILLp (padlist); 453 I32 i = AvFILLp (padlist);
380 while (i >= 0) 454
455 while (i > 0) /* special-case index 0 */
381 { 456 {
382 SV **svp = av_fetch (padlist, i--, FALSE); 457 /* we try to be extra-careful here */
383 if (svp) 458 AV *av = (AV *)AvARRAY (padlist)[i--];
384 { 459 I32 j = AvFILLp (av);
385 SV *sv; 460
386 while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) 461 while (j >= 0)
462 SvREFCNT_dec (AvARRAY (av)[j--]);
463
464 AvFILLp (av) = -1;
387 SvREFCNT_dec (sv); 465 SvREFCNT_dec (av);
388
389 SvREFCNT_dec (*svp);
390 }
391 } 466 }
392 467
468 SvREFCNT_dec (AvARRAY (padlist)[0]);
469
470 AvFILLp (padlist) = -1;
393 SvREFCNT_dec ((SV*)padlist); 471 SvREFCNT_dec ((SV*)padlist);
394 } 472 }
395} 473}
396 474
397static int 475static int
406 484
407 SvREFCNT_dec (av); /* sv_magicext increased the refcount */ 485 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
408 486
409 return 0; 487 return 0;
410} 488}
411
412#define CORO_MAGIC_type_cv 26
413#define CORO_MAGIC_type_state PERL_MAGIC_ext
414 489
415static MGVTBL coro_cv_vtbl = { 490static MGVTBL coro_cv_vtbl = {
416 0, 0, 0, 0, 491 0, 0, 0, 0,
417 coro_cv_free 492 coro_cv_free
418}; 493};
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 494
463/* the next two functions merely cache the padlists */ 495/* the next two functions merely cache the padlists */
464static void 496static void
465get_padlist (pTHX_ CV *cv) 497get_padlist (pTHX_ CV *cv)
466{ 498{
477 CV *cp = Perl_cv_clone (aTHX_ cv); 509 CV *cp = Perl_cv_clone (aTHX_ cv);
478 CvPADLIST (cv) = CvPADLIST (cp); 510 CvPADLIST (cv) = CvPADLIST (cp);
479 CvPADLIST (cp) = 0; 511 CvPADLIST (cp) = 0;
480 SvREFCNT_dec (cp); 512 SvREFCNT_dec (cp);
481#else 513#else
482 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 514 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv);
483#endif 515#endif
484 } 516 }
485} 517}
486 518
487static void 519static void
494 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); 526 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
495 527
496 av = (AV *)mg->mg_obj; 528 av = (AV *)mg->mg_obj;
497 529
498 if (expect_false (AvFILLp (av) >= AvMAX (av))) 530 if (expect_false (AvFILLp (av) >= AvMAX (av)))
499 av_extend (av, AvMAX (av) + 1); 531 av_extend (av, AvFILLp (av) + 1);
500 532
501 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 533 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
502} 534}
503 535
504/** load & save, init *******************************************************/ 536/** load & save, init *******************************************************/
537
538static void
539on_enterleave_call (pTHX_ SV *cb);
505 540
506static void 541static void
507load_perl (pTHX_ Coro__State c) 542load_perl (pTHX_ Coro__State c)
508{ 543{
509 perl_slots *slot = c->slot; 544 perl_slots *slot = c->slot;
510 c->slot = 0; 545 c->slot = 0;
511 546
512 PL_mainstack = c->mainstack; 547 PL_mainstack = c->mainstack;
513 548
514 GvSV (PL_defgv) = slot->defsv; 549 GvSV (PL_defgv) = slot->defsv;
515 GvAV (PL_defgv) = slot->defav; 550 GvAV (PL_defgv) = slot->defav;
516 GvSV (PL_errgv) = slot->errsv; 551 GvSV (PL_errgv) = slot->errsv;
517 GvSV (irsgv) = slot->irsgv; 552 GvSV (irsgv) = slot->irsgv;
553 GvHV (PL_hintgv) = slot->hinthv;
518 554
519 #define VAR(name,type) PL_ ## name = slot->name; 555 #define VAR(name,type) PL_ ## name = slot->name;
520 # include "state.h" 556 # include "state.h"
521 #undef VAR 557 #undef VAR
522 558
536 PUTBACK; 572 PUTBACK;
537 } 573 }
538 574
539 slf_frame = c->slf_frame; 575 slf_frame = c->slf_frame;
540 CORO_THROW = c->except; 576 CORO_THROW = c->except;
577
578 if (expect_false (c->on_enter))
579 {
580 int i;
581
582 for (i = 0; i <= AvFILLp (c->on_enter); ++i)
583 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]);
584 }
541} 585}
542 586
543static void 587static void
544save_perl (pTHX_ Coro__State c) 588save_perl (pTHX_ Coro__State c)
545{ 589{
590 if (expect_false (c->on_leave))
591 {
592 int i;
593
594 for (i = AvFILLp (c->on_leave); i >= 0; --i)
595 on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]);
596 }
597
546 c->except = CORO_THROW; 598 c->except = CORO_THROW;
547 c->slf_frame = slf_frame; 599 c->slf_frame = slf_frame;
548 600
549 { 601 {
550 dSP; 602 dSP;
608 c->mainstack = PL_mainstack; 660 c->mainstack = PL_mainstack;
609 661
610 { 662 {
611 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 663 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
612 664
613 slot->defav = GvAV (PL_defgv); 665 slot->defav = GvAV (PL_defgv);
614 slot->defsv = DEFSV; 666 slot->defsv = DEFSV;
615 slot->errsv = ERRSV; 667 slot->errsv = ERRSV;
616 slot->irsgv = GvSV (irsgv); 668 slot->irsgv = GvSV (irsgv);
669 slot->hinthv = GvHV (PL_hintgv);
617 670
618 #define VAR(name,type) slot->name = PL_ ## name; 671 #define VAR(name,type) slot->name = PL_ ## name;
619 # include "state.h" 672 # include "state.h"
620 #undef VAR 673 #undef VAR
621 } 674 }
748#endif 801#endif
749 802
750/* 803/*
751 * This overrides the default magic get method of %SIG elements. 804 * 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 805 * 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 806 * and instead of trying to save and restore the hash elements, we just provide
754 * readback here. 807 * 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 */ 808 */
759static int 809static int
760coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) 810coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
761{ 811{
762 const char *s = MgPV_nolen_const (mg); 812 const char *s = MgPV_nolen_const (mg);
815 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 865 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
816 866
817 if (svp) 867 if (svp)
818 { 868 {
819 SV *old = *svp; 869 SV *old = *svp;
820 *svp = newSVsv (sv); 870 *svp = SvOK (sv) ? newSVsv (sv) : 0;
821 SvREFCNT_dec (old); 871 SvREFCNT_dec (old);
822 return 0; 872 return 0;
823 } 873 }
824 } 874 }
825 875
857 907
858 PL_runops = RUNOPS_DEFAULT; 908 PL_runops = RUNOPS_DEFAULT;
859 PL_curcop = &PL_compiling; 909 PL_curcop = &PL_compiling;
860 PL_in_eval = EVAL_NULL; 910 PL_in_eval = EVAL_NULL;
861 PL_comppad = 0; 911 PL_comppad = 0;
912 PL_comppad_name = 0;
913 PL_comppad_name_fill = 0;
914 PL_comppad_name_floor = 0;
862 PL_curpm = 0; 915 PL_curpm = 0;
863 PL_curpad = 0; 916 PL_curpad = 0;
864 PL_localizing = 0; 917 PL_localizing = 0;
865 PL_dirty = 0; 918 PL_dirty = 0;
866 PL_restartop = 0; 919 PL_restartop = 0;
867#if PERL_VERSION_ATLEAST (5,10,0) 920#if PERL_VERSION_ATLEAST (5,10,0)
868 PL_parser = 0; 921 PL_parser = 0;
869#endif 922#endif
923 PL_hints = 0;
870 924
871 /* recreate the die/warn hooks */ 925 /* recreate the die/warn hooks */
872 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); 926 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); 927 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook);
874 928
875 GvSV (PL_defgv) = newSV (0); 929 GvSV (PL_defgv) = newSV (0);
876 GvAV (PL_defgv) = coro->args; coro->args = 0; 930 GvAV (PL_defgv) = coro->args; coro->args = 0;
877 GvSV (PL_errgv) = newSV (0); 931 GvSV (PL_errgv) = newSV (0);
878 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 932 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
933 GvHV (PL_hintgv) = 0;
879 PL_rs = newSVsv (GvSV (irsgv)); 934 PL_rs = newSVsv (GvSV (irsgv));
880 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 935 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
881 936
882 { 937 {
883 dSP; 938 dSP;
901 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ 956 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */
902 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ 957 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */
903 958
904 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ 959 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */
905 coro_setup_op.op_next = PL_op; 960 coro_setup_op.op_next = PL_op;
906 coro_setup_op.op_type = OP_CUSTOM; 961 coro_setup_op.op_type = OP_ENTERSUB;
907 coro_setup_op.op_ppaddr = pp_slf; 962 coro_setup_op.op_ppaddr = pp_slf;
908 /* no flags etc. required, as an init function won't be called */ 963 /* no flags etc. required, as an init function won't be called */
909 964
910 PL_op = (OP *)&coro_setup_op; 965 PL_op = (OP *)&coro_setup_op;
911 966
912 /* copy throw, in case it was set before coro_setup */ 967 /* copy throw, in case it was set before coro_setup */
913 CORO_THROW = coro->except; 968 CORO_THROW = coro->except;
914} 969}
915 970
916static void 971static void
917coro_destruct (pTHX_ struct coro *coro) 972coro_unwind_stacks (pTHX)
918{ 973{
919 if (!IN_DESTRUCT) 974 if (!IN_DESTRUCT)
920 { 975 {
921 /* restore all saved variables and stuff */ 976 /* restore all saved variables and stuff */
922 LEAVE_SCOPE (0); 977 LEAVE_SCOPE (0);
930 POPSTACK_TO (PL_mainstack); 985 POPSTACK_TO (PL_mainstack);
931 986
932 /* unwind main stack */ 987 /* unwind main stack */
933 dounwind (-1); 988 dounwind (-1);
934 } 989 }
990}
991
992static void
993coro_destruct_perl (pTHX_ struct coro *coro)
994{
995 coro_unwind_stacks (aTHX);
935 996
936 SvREFCNT_dec (GvSV (PL_defgv)); 997 SvREFCNT_dec (GvSV (PL_defgv));
937 SvREFCNT_dec (GvAV (PL_defgv)); 998 SvREFCNT_dec (GvAV (PL_defgv));
938 SvREFCNT_dec (GvSV (PL_errgv)); 999 SvREFCNT_dec (GvSV (PL_errgv));
939 SvREFCNT_dec (PL_defoutgv); 1000 SvREFCNT_dec (PL_defoutgv);
940 SvREFCNT_dec (PL_rs); 1001 SvREFCNT_dec (PL_rs);
941 SvREFCNT_dec (GvSV (irsgv)); 1002 SvREFCNT_dec (GvSV (irsgv));
1003 SvREFCNT_dec (GvHV (PL_hintgv));
942 1004
943 SvREFCNT_dec (PL_diehook); 1005 SvREFCNT_dec (PL_diehook);
944 SvREFCNT_dec (PL_warnhook); 1006 SvREFCNT_dec (PL_warnhook);
945 1007
946 SvREFCNT_dec (coro->saved_deffh); 1008 SvREFCNT_dec (coro->saved_deffh);
964static int 1026static int
965runops_trace (pTHX) 1027runops_trace (pTHX)
966{ 1028{
967 COP *oldcop = 0; 1029 COP *oldcop = 0;
968 int oldcxix = -2; 1030 int oldcxix = -2;
969 struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */
970 coro_cctx *cctx = coro->cctx;
971 1031
972 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 1032 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
973 { 1033 {
974 PERL_ASYNC_CHECK (); 1034 PERL_ASYNC_CHECK ();
975 1035
976 if (cctx->flags & CC_TRACE_ALL) 1036 if (cctx_current->flags & CC_TRACE_ALL)
977 { 1037 {
978 if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) 1038 if (PL_op->op_type == OP_LEAVESUB && cctx_current->flags & CC_TRACE_SUB)
979 { 1039 {
980 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1040 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
981 SV **bot, **top; 1041 SV **bot, **top;
982 AV *av = newAV (); /* return values */ 1042 AV *av = newAV (); /* return values */
983 SV **cb; 1043 SV **cb;
1020 1080
1021 if (PL_curcop != &PL_compiling) 1081 if (PL_curcop != &PL_compiling)
1022 { 1082 {
1023 SV **cb; 1083 SV **cb;
1024 1084
1025 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) 1085 if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB)
1026 { 1086 {
1027 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1087 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1028 1088
1029 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 1089 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
1030 { 1090 {
1031 runops_proc_t old_runops = PL_runops;
1032 dSP; 1091 dSP;
1033 GV *gv = CvGV (cx->blk_sub.cv); 1092 GV *gv = CvGV (cx->blk_sub.cv);
1034 SV *fullname = sv_2mortal (newSV (0)); 1093 SV *fullname = sv_2mortal (newSV (0));
1035 1094
1036 if (isGV (gv)) 1095 if (isGV (gv))
1054 } 1113 }
1055 1114
1056 oldcxix = cxstack_ix; 1115 oldcxix = cxstack_ix;
1057 } 1116 }
1058 1117
1059 if (cctx->flags & CC_TRACE_LINE) 1118 if (cctx_current->flags & CC_TRACE_LINE)
1060 { 1119 {
1061 dSP; 1120 dSP;
1062 1121
1063 PL_runops = RUNOPS_DEFAULT; 1122 PL_runops = RUNOPS_DEFAULT;
1064 ENTER; 1123 ENTER;
1083 1142
1084 TAINT_NOT; 1143 TAINT_NOT;
1085 return 0; 1144 return 0;
1086} 1145}
1087 1146
1088static struct coro_cctx *cctx_ssl_cctx;
1089static struct CoroSLF cctx_ssl_frame; 1147static struct CoroSLF cctx_ssl_frame;
1090 1148
1091static void 1149static void
1092slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) 1150slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1093{ 1151{
1094 ta->prev = (struct coro *)cctx_ssl_cctx;
1095 ta->next = 0; 1152 ta->prev = 0;
1096} 1153}
1097 1154
1098static int 1155static int
1099slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) 1156slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1100{ 1157{
1103 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ 1160 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1104} 1161}
1105 1162
1106/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ 1163/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1107static void NOINLINE 1164static void NOINLINE
1108cctx_prepare (pTHX_ coro_cctx *cctx) 1165cctx_prepare (pTHX)
1109{ 1166{
1110 PL_top_env = &PL_start_env; 1167 PL_top_env = &PL_start_env;
1111 1168
1112 if (cctx->flags & CC_TRACE) 1169 if (cctx_current->flags & CC_TRACE)
1113 PL_runops = runops_trace; 1170 PL_runops = runops_trace;
1114 1171
1115 /* we already must be executing an SLF op, there is no other valid way 1172 /* we already must be executing an SLF op, there is no other valid way
1116 * that can lead to creation of a new cctx */ 1173 * that can lead to creation of a new cctx */
1117 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", 1174 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)",
1118 slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); 1175 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1119 1176
1120 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ 1177 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */
1121 cctx_ssl_cctx = cctx;
1122 cctx_ssl_frame = slf_frame; 1178 cctx_ssl_frame = slf_frame;
1123 1179
1124 slf_frame.prepare = slf_prepare_set_stacklevel; 1180 slf_frame.prepare = slf_prepare_set_stacklevel;
1125 slf_frame.check = slf_check_set_stacklevel; 1181 slf_frame.check = slf_check_set_stacklevel;
1126} 1182}
1149 /* normally we would need to skip the entersub here */ 1205 /* normally we would need to skip the entersub here */
1150 /* not doing so will re-execute it, which is exactly what we want */ 1206 /* not doing so will re-execute it, which is exactly what we want */
1151 /* PL_nop = PL_nop->op_next */ 1207 /* PL_nop = PL_nop->op_next */
1152 1208
1153 /* inject a fake subroutine call to cctx_init */ 1209 /* inject a fake subroutine call to cctx_init */
1154 cctx_prepare (aTHX_ (coro_cctx *)arg); 1210 cctx_prepare (aTHX);
1155 1211
1156 /* cctx_run is the alternative tail of transfer() */ 1212 /* cctx_run is the alternative tail of transfer() */
1157 transfer_tail (aTHX); 1213 transfer_tail (aTHX);
1158 1214
1159 /* somebody or something will hit me for both perl_run and PL_restartop */ 1215 /* somebody or something will hit me for both perl_run and PL_restartop */
1167 /* 1223 /*
1168 * If perl-run returns we assume exit() was being called or the coro 1224 * If perl-run returns we assume exit() was being called or the coro
1169 * fell off the end, which seems to be the only valid (non-bug) 1225 * fell off the end, which seems to be the only valid (non-bug)
1170 * reason for perl_run to return. We try to exit by jumping to the 1226 * reason for perl_run to return. We try to exit by jumping to the
1171 * bootstrap-time "top" top_env, as we cannot restore the "main" 1227 * bootstrap-time "top" top_env, as we cannot restore the "main"
1172 * coroutine as Coro has no such concept 1228 * coroutine as Coro has no such concept.
1229 * This actually isn't valid with the pthread backend, but OSes requiring
1230 * that backend are too broken to do it in a standards-compliant way.
1173 */ 1231 */
1174 PL_top_env = main_top_env; 1232 PL_top_env = main_top_env;
1175 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1233 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1176 } 1234 }
1177} 1235}
1254cctx_destroy (coro_cctx *cctx) 1312cctx_destroy (coro_cctx *cctx)
1255{ 1313{
1256 if (!cctx) 1314 if (!cctx)
1257 return; 1315 return;
1258 1316
1317 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary?
1318
1259 --cctx_count; 1319 --cctx_count;
1260 coro_destroy (&cctx->cctx); 1320 coro_destroy (&cctx->cctx);
1261 1321
1262 /* coro_transfer creates new, empty cctx's */ 1322 /* coro_transfer creates new, empty cctx's */
1263 if (cctx->sptr) 1323 if (cctx->sptr)
1326 /* TODO: throwing up here is considered harmful */ 1386 /* TODO: throwing up here is considered harmful */
1327 1387
1328 if (expect_true (prev != next)) 1388 if (expect_true (prev != next))
1329 { 1389 {
1330 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1390 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1331 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,"); 1391 croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,");
1332 1392
1333 if (expect_false (next->flags & CF_RUNNING))
1334 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1335
1336 if (expect_false (next->flags & CF_DESTROYED)) 1393 if (expect_false (next->flags & (CF_RUNNING | CF_DESTROYED | CF_SUSPENDED)))
1337 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states,"); 1394 croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,");
1338 1395
1339#if !PERL_VERSION_ATLEAST (5,10,0) 1396#if !PERL_VERSION_ATLEAST (5,10,0)
1340 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1397 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1341 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); 1398 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,");
1342#endif 1399#endif
1348transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1405transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1349{ 1406{
1350 dSTACKLEVEL; 1407 dSTACKLEVEL;
1351 1408
1352 /* sometimes transfer is only called to set idle_sp */ 1409 /* sometimes transfer is only called to set idle_sp */
1353 if (expect_false (!next)) 1410 if (expect_false (!prev))
1354 { 1411 {
1355 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1412 cctx_current->idle_sp = STACKLEVEL;
1356 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1413 assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1357 } 1414 }
1358 else if (expect_true (prev != next)) 1415 else if (expect_true (prev != next))
1359 { 1416 {
1360 coro_cctx *prev__cctx; 1417 coro_cctx *cctx_prev;
1361 1418
1362 if (expect_false (prev->flags & CF_NEW)) 1419 if (expect_false (prev->flags & CF_NEW))
1363 { 1420 {
1364 /* create a new empty/source context */ 1421 /* create a new empty/source context */
1365 prev->cctx = cctx_new_empty ();
1366 prev->flags &= ~CF_NEW; 1422 prev->flags &= ~CF_NEW;
1367 prev->flags |= CF_RUNNING; 1423 prev->flags |= CF_RUNNING;
1368 } 1424 }
1369 1425
1370 prev->flags &= ~CF_RUNNING; 1426 prev->flags &= ~CF_RUNNING;
1381 coro_setup (aTHX_ next); 1437 coro_setup (aTHX_ next);
1382 } 1438 }
1383 else 1439 else
1384 load_perl (aTHX_ next); 1440 load_perl (aTHX_ next);
1385 1441
1386 prev__cctx = prev->cctx;
1387
1388 /* possibly untie and reuse the cctx */ 1442 /* possibly untie and reuse the cctx */
1389 if (expect_true ( 1443 if (expect_true (
1390 prev__cctx->idle_sp == STACKLEVEL 1444 cctx_current->idle_sp == STACKLEVEL
1391 && !(prev__cctx->flags & CC_TRACE) 1445 && !(cctx_current->flags & CC_TRACE)
1392 && !force_cctx 1446 && !force_cctx
1393 )) 1447 ))
1394 { 1448 {
1395 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1449 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1396 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); 1450 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te));
1397 1451
1398 prev->cctx = 0;
1399
1400 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1452 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */
1401 /* without this the next cctx_get might destroy the prev__cctx while still in use */ 1453 /* without this the next cctx_get might destroy the running cctx while still in use */
1402 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1454 if (expect_false (CCTX_EXPIRED (cctx_current)))
1403 if (!next->cctx) 1455 if (expect_true (!next->cctx))
1404 next->cctx = cctx_get (aTHX); 1456 next->cctx = cctx_get (aTHX);
1405 1457
1406 cctx_put (prev__cctx); 1458 cctx_put (cctx_current);
1407 } 1459 }
1460 else
1461 prev->cctx = cctx_current;
1408 1462
1409 ++next->usecount; 1463 ++next->usecount;
1410 1464
1411 if (expect_true (!next->cctx)) 1465 cctx_prev = cctx_current;
1412 next->cctx = cctx_get (aTHX); 1466 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1413 1467
1414 if (expect_false (prev__cctx != next->cctx)) 1468 next->cctx = 0;
1469
1470 if (expect_false (cctx_prev != cctx_current))
1415 { 1471 {
1416 prev__cctx->top_env = PL_top_env; 1472 cctx_prev->top_env = PL_top_env;
1417 PL_top_env = next->cctx->top_env; 1473 PL_top_env = cctx_current->top_env;
1418 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1474 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1419 } 1475 }
1420 1476
1421 transfer_tail (aTHX); 1477 transfer_tail (aTHX);
1422 } 1478 }
1423} 1479}
1431coro_state_destroy (pTHX_ struct coro *coro) 1487coro_state_destroy (pTHX_ struct coro *coro)
1432{ 1488{
1433 if (coro->flags & CF_DESTROYED) 1489 if (coro->flags & CF_DESTROYED)
1434 return 0; 1490 return 0;
1435 1491
1436 if (coro->on_destroy) 1492 if (coro->on_destroy && !PL_dirty)
1437 coro->on_destroy (aTHX_ coro); 1493 coro->on_destroy (aTHX_ coro);
1438 1494
1439 coro->flags |= CF_DESTROYED; 1495 coro->flags |= CF_DESTROYED;
1440 1496
1441 if (coro->flags & CF_READY) 1497 if (coro->flags & CF_READY)
1445 --coro_nready; 1501 --coro_nready;
1446 } 1502 }
1447 else 1503 else
1448 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ 1504 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
1449 1505
1450 if (coro->mainstack && coro->mainstack != main_mainstack) 1506 if (coro->mainstack
1507 && coro->mainstack != main_mainstack
1508 && coro->slot
1509 && !PL_dirty)
1451 { 1510 {
1452 struct coro temp; 1511 struct coro *current = SvSTATE_current;
1453 1512
1454 assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING))); 1513 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack));
1455 1514
1456 save_perl (aTHX_ &temp); 1515 save_perl (aTHX_ current);
1457 load_perl (aTHX_ coro); 1516 load_perl (aTHX_ coro);
1458 1517
1459 coro_destruct (aTHX_ coro); 1518 coro_destruct_perl (aTHX_ coro);
1460 1519
1461 load_perl (aTHX_ &temp); 1520 load_perl (aTHX_ current);
1462 1521
1463 coro->slot = 0; 1522 coro->slot = 0;
1464 } 1523 }
1465 1524
1466 cctx_destroy (coro->cctx); 1525 cctx_destroy (coro->cctx);
1556/** Coro ********************************************************************/ 1615/** Coro ********************************************************************/
1557 1616
1558INLINE void 1617INLINE void
1559coro_enq (pTHX_ struct coro *coro) 1618coro_enq (pTHX_ struct coro *coro)
1560{ 1619{
1561 av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv)); 1620 struct coro **ready = coro_ready [coro->prio - PRIO_MIN];
1562}
1563 1621
1564INLINE SV * 1622 SvREFCNT_inc_NN (coro->hv);
1623
1624 coro->next_ready = 0;
1625 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro;
1626 ready [1] = coro;
1627}
1628
1629INLINE struct coro *
1565coro_deq (pTHX) 1630coro_deq (pTHX)
1566{ 1631{
1567 int prio; 1632 int prio;
1568 1633
1569 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) 1634 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; )
1570 if (AvFILLp (coro_ready [prio]) >= 0) 1635 {
1571 return av_shift (coro_ready [prio]); 1636 struct coro **ready = coro_ready [prio];
1637
1638 if (ready [0])
1639 {
1640 struct coro *coro = ready [0];
1641 ready [0] = coro->next_ready;
1642 return coro;
1643 }
1644 }
1572 1645
1573 return 0; 1646 return 0;
1574} 1647}
1575 1648
1576static int 1649static int
1578{ 1651{
1579 struct coro *coro; 1652 struct coro *coro;
1580 SV *sv_hook; 1653 SV *sv_hook;
1581 void (*xs_hook)(void); 1654 void (*xs_hook)(void);
1582 1655
1583 if (SvROK (coro_sv))
1584 coro_sv = SvRV (coro_sv);
1585
1586 coro = SvSTATE (coro_sv); 1656 coro = SvSTATE (coro_sv);
1587 1657
1588 if (coro->flags & CF_READY) 1658 if (coro->flags & CF_READY)
1589 return 0; 1659 return 0;
1590 1660
1643static void 1713static void
1644prepare_schedule (pTHX_ struct coro_transfer_args *ta) 1714prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1645{ 1715{
1646 for (;;) 1716 for (;;)
1647 { 1717 {
1648 SV *next_sv = coro_deq (aTHX); 1718 struct coro *next = coro_deq (aTHX);
1649 1719
1650 if (expect_true (next_sv)) 1720 if (expect_true (next))
1651 { 1721 {
1652 struct coro *next = SvSTATE_hv (next_sv);
1653
1654 /* cannot transfer to destroyed coros, skip and look for next */ 1722 /* cannot transfer to destroyed coros, skip and look for next */
1655 if (expect_false (next->flags & CF_DESTROYED)) 1723 if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED)))
1656 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ 1724 SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */
1657 else 1725 else
1658 { 1726 {
1659 next->flags &= ~CF_READY; 1727 next->flags &= ~CF_READY;
1660 --coro_nready; 1728 --coro_nready;
1661 1729
1662 return prepare_schedule_to (aTHX_ ta, next); 1730 prepare_schedule_to (aTHX_ ta, next);
1731 break;
1663 } 1732 }
1664 } 1733 }
1665 else 1734 else
1666 { 1735 {
1667 /* nothing to schedule: call the idle handler */ 1736 /* nothing to schedule: call the idle handler */
1737 if (SvROK (sv_idle)
1738 && SvOBJECT (SvRV (sv_idle)))
1739 {
1740 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1741 api_ready (aTHX_ SvRV (sv_idle));
1742 --coro_nready;
1743 }
1744 else
1745 {
1668 dSP; 1746 dSP;
1669 1747
1670 ENTER; 1748 ENTER;
1671 SAVETMPS; 1749 SAVETMPS;
1672 1750
1673 PUSHMARK (SP); 1751 PUSHMARK (SP);
1674 PUTBACK; 1752 PUTBACK;
1675 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); 1753 call_sv (sv_idle, G_VOID | G_DISCARD);
1676 1754
1677 FREETMPS; 1755 FREETMPS;
1678 LEAVE; 1756 LEAVE;
1757 }
1679 } 1758 }
1680 } 1759 }
1681} 1760}
1682 1761
1683INLINE void 1762INLINE void
1754static void 1833static void
1755api_trace (pTHX_ SV *coro_sv, int flags) 1834api_trace (pTHX_ SV *coro_sv, int flags)
1756{ 1835{
1757 struct coro *coro = SvSTATE (coro_sv); 1836 struct coro *coro = SvSTATE (coro_sv);
1758 1837
1838 if (coro->flags & CF_RUNNING)
1839 croak ("cannot enable tracing on a running coroutine, caught");
1840
1759 if (flags & CC_TRACE) 1841 if (flags & CC_TRACE)
1760 { 1842 {
1761 if (!coro->cctx) 1843 if (!coro->cctx)
1762 coro->cctx = cctx_new_run (); 1844 coro->cctx = cctx_new_run ();
1763 else if (!(coro->cctx->flags & CC_TRACE)) 1845 else if (!(coro->cctx->flags & CC_TRACE))
1764 croak ("cannot enable tracing on coroutine with custom stack,"); 1846 croak ("cannot enable tracing on coroutine with custom stack, caught");
1765 1847
1766 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1848 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1767 } 1849 }
1768 else if (coro->cctx && coro->cctx->flags & CC_TRACE) 1850 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1769 { 1851 {
1825 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ 1907 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1826 api_ready (aTHX_ sv_manager); 1908 api_ready (aTHX_ sv_manager);
1827 1909
1828 frame->prepare = prepare_schedule; 1910 frame->prepare = prepare_schedule;
1829 frame->check = slf_check_repeat; 1911 frame->check = slf_check_repeat;
1912
1913 /* as a minor optimisation, we could unwind all stacks here */
1914 /* but that puts extra pressure on pp_slf, and is not worth much */
1915 /*coro_unwind_stacks (aTHX);*/
1830} 1916}
1831 1917
1832/*****************************************************************************/ 1918/*****************************************************************************/
1833/* async pool handler */ 1919/* async pool handler */
1834 1920
1917 SV *data = (SV *)GENSUB_ARG; 2003 SV *data = (SV *)GENSUB_ARG;
1918 2004
1919 if (SvTYPE (SvRV (data)) != SVt_PVAV) 2005 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1920 { 2006 {
1921 /* first call, set args */ 2007 /* first call, set args */
2008 SV *coro = SvRV (data);
1922 AV *av = newAV (); 2009 AV *av = newAV ();
1923 SV *coro = SvRV (data);
1924 2010
1925 SvRV_set (data, (SV *)av); 2011 SvRV_set (data, (SV *)av);
1926 api_ready (aTHX_ coro);
1927 SvREFCNT_dec (coro);
1928 2012
1929 /* better take a full copy of the arguments */ 2013 /* better take a full copy of the arguments */
1930 while (items--) 2014 while (items--)
1931 av_store (av, items, newSVsv (ST (items))); 2015 av_store (av, items, newSVsv (ST (items)));
2016
2017 api_ready (aTHX_ coro);
2018 SvREFCNT_dec (coro);
1932 } 2019 }
1933 2020
1934 XSRETURN_EMPTY; 2021 XSRETURN_EMPTY;
1935} 2022}
1936 2023
1953 2040
1954 EXTEND (SP, AvFILLp (av) + 1); 2041 EXTEND (SP, AvFILLp (av) + 1);
1955 for (i = 0; i <= AvFILLp (av); ++i) 2042 for (i = 0; i <= AvFILLp (av); ++i)
1956 PUSHs (sv_2mortal (AvARRAY (av)[i])); 2043 PUSHs (sv_2mortal (AvARRAY (av)[i]));
1957 2044
1958 /* we have stolen the elements, so ste length to zero and free */ 2045 /* we have stolen the elements, so set length to zero and free */
1959 AvFILLp (av) = -1; 2046 AvFILLp (av) = -1;
1960 av_undef (av); 2047 av_undef (av);
1961 2048
1962 PUTBACK; 2049 PUTBACK;
1963 } 2050 }
2250 } 2337 }
2251 else 2338 else
2252 slf_argc = 0; 2339 slf_argc = 0;
2253 2340
2254 PL_op->op_ppaddr = pp_slf; 2341 PL_op->op_ppaddr = pp_slf;
2255 PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ 2342 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2256 2343
2257 PL_op = (OP *)&slf_restore; 2344 PL_op = (OP *)&slf_restore;
2345}
2346
2347/*****************************************************************************/
2348/* dynamic wind */
2349
2350static void
2351on_enterleave_call (pTHX_ SV *cb)
2352{
2353 dSP;
2354
2355 PUSHSTACK;
2356
2357 PUSHMARK (SP);
2358 PUTBACK;
2359 call_sv (cb, G_VOID | G_DISCARD);
2360 SPAGAIN;
2361
2362 POPSTACK;
2363}
2364
2365static SV *
2366coro_avp_pop_and_free (pTHX_ AV **avp)
2367{
2368 AV *av = *avp;
2369 SV *res = av_pop (av);
2370
2371 if (AvFILLp (av) < 0)
2372 {
2373 *avp = 0;
2374 SvREFCNT_dec (av);
2375 }
2376
2377 return res;
2378}
2379
2380static void
2381coro_pop_on_enter (pTHX_ void *coro)
2382{
2383 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_enter);
2384 SvREFCNT_dec (cb);
2385}
2386
2387static void
2388coro_pop_on_leave (pTHX_ void *coro)
2389{
2390 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave);
2391 on_enterleave_call (aTHX_ sv_2mortal (cb));
2258} 2392}
2259 2393
2260/*****************************************************************************/ 2394/*****************************************************************************/
2261/* PerlIO::cede */ 2395/* PerlIO::cede */
2262 2396
2343 SV **ary; 2477 SV **ary;
2344 2478
2345 /* unfortunately, building manually saves memory */ 2479 /* unfortunately, building manually saves memory */
2346 Newx (ary, 2, SV *); 2480 Newx (ary, 2, SV *);
2347 AvALLOC (av) = ary; 2481 AvALLOC (av) = ary;
2482#if PERL_VERSION_ATLEAST (5,10,0)
2348 /*AvARRAY (av) = ary;*/ 2483 AvARRAY (av) = ary;
2484#else
2349 SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ 2485 /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */
2486 /* -DDEBUGGING flags this as a bug, despite it perfectly working */
2487 SvPVX ((SV *)av) = (char *)ary;
2488#endif
2350 AvMAX (av) = 1; 2489 AvMAX (av) = 1;
2351 AvFILLp (av) = 0; 2490 AvFILLp (av) = 0;
2352 ary [0] = newSViv (count); 2491 ary [0] = newSViv (count);
2353 2492
2354 return newRV_noinc ((SV *)av); 2493 return newRV_noinc ((SV *)av);
2724 XSRETURN_EMPTY; 2863 XSRETURN_EMPTY;
2725} 2864}
2726 2865
2727/*****************************************************************************/ 2866/*****************************************************************************/
2728 2867
2868#if CORO_CLONE
2869# include "clone.c"
2870#endif
2871
2729MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 2872MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2730 2873
2731PROTOTYPES: DISABLE 2874PROTOTYPES: DISABLE
2732 2875
2733BOOT: 2876BOOT:
2737 coro_thx = PERL_GET_CONTEXT; 2880 coro_thx = PERL_GET_CONTEXT;
2738# endif 2881# endif
2739#endif 2882#endif
2740 BOOT_PAGESIZE; 2883 BOOT_PAGESIZE;
2741 2884
2885 cctx_current = cctx_new_empty ();
2886
2742 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 2887 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
2743 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 2888 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
2744 2889
2745 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; 2890 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
2746 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; 2891 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
2765 2910
2766 { 2911 {
2767 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); 2912 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf)));
2768 2913
2769 if (!PL_custom_op_names) PL_custom_op_names = newHV (); 2914 if (!PL_custom_op_names) PL_custom_op_names = newHV ();
2770 hv_store_ent (PL_custom_op_names, slf, 2915 hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0);
2771 newSVpv ("coro_slf", 0), 0);
2772 2916
2773 if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); 2917 if (!PL_custom_op_descs) PL_custom_op_descs = newHV ();
2774 hv_store_ent (PL_custom_op_descs, slf, 2918 hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0);
2775 newSVpv ("coro schedule like function", 0), 0);
2776 } 2919 }
2777 2920
2778 coroapi.ver = CORO_API_VERSION; 2921 coroapi.ver = CORO_API_VERSION;
2779 coroapi.rev = CORO_API_REVISION; 2922 coroapi.rev = CORO_API_REVISION;
2780 2923
2873void 3016void
2874_exit (int code) 3017_exit (int code)
2875 PROTOTYPE: $ 3018 PROTOTYPE: $
2876 CODE: 3019 CODE:
2877 _exit (code); 3020 _exit (code);
3021
3022SV *
3023clone (Coro::State coro)
3024 CODE:
3025{
3026#if CORO_CLONE
3027 struct coro *ncoro = coro_clone (aTHX_ coro);
3028 MAGIC *mg;
3029 /* TODO: too much duplication */
3030 ncoro->hv = newHV ();
3031 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
3032 mg->mg_flags |= MGf_DUP;
3033 RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv));
3034#else
3035 croak ("Coro::State->clone has not been configured into this installation of Coro, realised");
3036#endif
3037}
3038 OUTPUT:
3039 RETVAL
2878 3040
2879int 3041int
2880cctx_stacksize (int new_stacksize = 0) 3042cctx_stacksize (int new_stacksize = 0)
2881 PROTOTYPE: ;$ 3043 PROTOTYPE: ;$
2882 CODE: 3044 CODE:
2932 eval = 1 3094 eval = 1
2933 CODE: 3095 CODE:
2934{ 3096{
2935 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3097 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
2936 { 3098 {
2937 struct coro temp; 3099 struct coro *current = SvSTATE_current;
2938 3100
2939 if (!(coro->flags & CF_RUNNING)) 3101 if (current != coro)
2940 { 3102 {
2941 PUTBACK; 3103 PUTBACK;
2942 save_perl (aTHX_ &temp); 3104 save_perl (aTHX_ current);
2943 load_perl (aTHX_ coro); 3105 load_perl (aTHX_ coro);
3106 SPAGAIN;
2944 } 3107 }
2945 3108
2946 {
2947 dSP;
2948 ENTER;
2949 SAVETMPS;
2950 PUTBACK;
2951 PUSHSTACK; 3109 PUSHSTACK;
3110
2952 PUSHMARK (SP); 3111 PUSHMARK (SP);
3112 PUTBACK;
2953 3113
2954 if (ix) 3114 if (ix)
2955 eval_sv (coderef, 0); 3115 eval_sv (coderef, 0);
2956 else 3116 else
2957 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3117 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
2958 3118
2959 POPSTACK; 3119 POPSTACK;
2960 SPAGAIN; 3120 SPAGAIN;
2961 FREETMPS;
2962 LEAVE;
2963 PUTBACK;
2964 }
2965 3121
2966 if (!(coro->flags & CF_RUNNING)) 3122 if (current != coro)
2967 { 3123 {
3124 PUTBACK;
2968 save_perl (aTHX_ coro); 3125 save_perl (aTHX_ coro);
2969 load_perl (aTHX_ &temp); 3126 load_perl (aTHX_ current);
2970 SPAGAIN; 3127 SPAGAIN;
2971 } 3128 }
2972 } 3129 }
2973} 3130}
2974 3131
2978 ALIAS: 3135 ALIAS:
2979 is_ready = CF_READY 3136 is_ready = CF_READY
2980 is_running = CF_RUNNING 3137 is_running = CF_RUNNING
2981 is_new = CF_NEW 3138 is_new = CF_NEW
2982 is_destroyed = CF_DESTROYED 3139 is_destroyed = CF_DESTROYED
3140 is_suspended = CF_SUSPENDED
2983 CODE: 3141 CODE:
2984 RETVAL = boolSV (coro->flags & ix); 3142 RETVAL = boolSV (coro->flags & ix);
2985 OUTPUT: 3143 OUTPUT:
2986 RETVAL 3144 RETVAL
2987 3145
3003 3161
3004SV * 3162SV *
3005has_cctx (Coro::State coro) 3163has_cctx (Coro::State coro)
3006 PROTOTYPE: $ 3164 PROTOTYPE: $
3007 CODE: 3165 CODE:
3008 RETVAL = boolSV (!!coro->cctx); 3166 /* maybe manage the running flag differently */
3167 RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING));
3009 OUTPUT: 3168 OUTPUT:
3010 RETVAL 3169 RETVAL
3011 3170
3012int 3171int
3013is_traced (Coro::State coro) 3172is_traced (Coro::State coro)
3033 3192
3034void 3193void
3035force_cctx () 3194force_cctx ()
3036 PROTOTYPE: 3195 PROTOTYPE:
3037 CODE: 3196 CODE:
3038 SvSTATE_current->cctx->idle_sp = 0; 3197 cctx_current->idle_sp = 0;
3039 3198
3040void 3199void
3041swap_defsv (Coro::State self) 3200swap_defsv (Coro::State self)
3042 PROTOTYPE: $ 3201 PROTOTYPE: $
3043 ALIAS: 3202 ALIAS:
3051 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 3210 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
3052 3211
3053 SV *tmp = *src; *src = *dst; *dst = tmp; 3212 SV *tmp = *src; *src = *dst; *dst = tmp;
3054 } 3213 }
3055 3214
3215void
3216cancel (Coro::State self)
3217 CODE:
3218 coro_state_destroy (aTHX_ self);
3219 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3220
3056 3221
3057MODULE = Coro::State PACKAGE = Coro 3222MODULE = Coro::State PACKAGE = Coro
3058 3223
3059BOOT: 3224BOOT:
3060{ 3225{
3061 int i; 3226 int i;
3062 3227
3063 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3228 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3064 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3229 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3065 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3230 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3066 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3231 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3067 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3232 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
3068 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 3233 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3069 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3234 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3070 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3235 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3236 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3071 3237
3072 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3238 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
3073 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3239 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3074 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); 3240 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3075 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); 3241 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);
3080 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 3246 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
3081 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 3247 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
3082 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 3248 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
3083 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 3249 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
3084 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 3250 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
3085
3086 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
3087 coro_ready[i] = newAV ();
3088 3251
3089 { 3252 {
3090 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3253 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
3091 3254
3092 coroapi.schedule = api_schedule; 3255 coroapi.schedule = api_schedule;
3133cede_notself (...) 3296cede_notself (...)
3134 CODE: 3297 CODE:
3135 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3298 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3136 3299
3137void 3300void
3138_cancel (Coro::State self)
3139 CODE:
3140 coro_state_destroy (aTHX_ self);
3141 coro_call_on_destroy (aTHX_ self);
3142
3143void
3144_set_current (SV *current) 3301_set_current (SV *current)
3145 PROTOTYPE: $ 3302 PROTOTYPE: $
3146 CODE: 3303 CODE:
3147 SvREFCNT_dec (SvRV (coro_current)); 3304 SvREFCNT_dec (SvRV (coro_current));
3148 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); 3305 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
3190 PROTOTYPE: 3347 PROTOTYPE:
3191 CODE: 3348 CODE:
3192 RETVAL = coro_nready; 3349 RETVAL = coro_nready;
3193 OUTPUT: 3350 OUTPUT:
3194 RETVAL 3351 RETVAL
3352
3353void
3354suspend (Coro::State self)
3355 PROTOTYPE: $
3356 CODE:
3357 self->flags |= CF_SUSPENDED;
3358
3359void
3360resume (Coro::State self)
3361 PROTOTYPE: $
3362 CODE:
3363 self->flags &= ~CF_SUSPENDED;
3195 3364
3196void 3365void
3197_pool_handler (...) 3366_pool_handler (...)
3198 CODE: 3367 CODE:
3199 CORO_EXECUTE_SLF_XS (slf_init_pool_handler); 3368 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
3254rouse_wait (...) 3423rouse_wait (...)
3255 PROTOTYPE: ;$ 3424 PROTOTYPE: ;$
3256 PPCODE: 3425 PPCODE:
3257 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3426 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3258 3427
3428void
3429on_enter (SV *block)
3430 ALIAS:
3431 on_leave = 1
3432 PROTOTYPE: &
3433 CODE:
3434{
3435 struct coro *coro = SvSTATE_current;
3436 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
3437
3438 block = (SV *)coro_sv_2cv (aTHX_ block);
3439
3440 if (!*avp)
3441 *avp = newAV ();
3442
3443 av_push (*avp, SvREFCNT_inc (block));
3444
3445 if (!ix)
3446 on_enterleave_call (aTHX_ block);
3447
3448 LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3449 SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro);
3450 ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3451}
3452
3259 3453
3260MODULE = Coro::State PACKAGE = PerlIO::cede 3454MODULE = Coro::State PACKAGE = PerlIO::cede
3261 3455
3262BOOT: 3456BOOT:
3263 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3457 PerlIO_define_layer (aTHX_ &PerlIO_cede);
3273 GvSTASH (CvGV (cv)) 3467 GvSTASH (CvGV (cv))
3274 ); 3468 );
3275 OUTPUT: 3469 OUTPUT:
3276 RETVAL 3470 RETVAL
3277 3471
3278# helper for Coro::Channel 3472# helper for Coro::Channel and others
3279SV * 3473SV *
3280_alloc (int count) 3474_alloc (int count)
3281 CODE: 3475 CODE:
3282 RETVAL = coro_waitarray_new (aTHX_ count); 3476 RETVAL = coro_waitarray_new (aTHX_ count);
3283 OUTPUT: 3477 OUTPUT:
3341 for (i = 1; i <= wcount; ++i) 3535 for (i = 1; i <= wcount; ++i)
3342 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); 3536 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
3343 } 3537 }
3344} 3538}
3345 3539
3540MODULE = Coro::State PACKAGE = Coro::SemaphoreSet
3541
3542void
3543_may_delete (SV *sem, int count, int extra_refs)
3544 PPCODE:
3545{
3546 AV *av = (AV *)SvRV (sem);
3547
3548 if (SvREFCNT ((SV *)av) == 1 + extra_refs
3549 && AvFILLp (av) == 0 /* no waiters, just count */
3550 && SvIV (AvARRAY (av)[0]) == count)
3551 XSRETURN_YES;
3552
3553 XSRETURN_NO;
3554}
3555
3346MODULE = Coro::State PACKAGE = Coro::Signal 3556MODULE = Coro::State PACKAGE = Coro::Signal
3347 3557
3348SV * 3558SV *
3349new (SV *klass) 3559new (SV *klass)
3350 CODE: 3560 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines