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.319 by root, Thu Nov 20 07:25:01 2008 UTC vs.
Revision 1.355 by root, Wed Jun 24 02:52:38 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
141#else 144#else
142# define attribute(x) 145# define attribute(x)
143# define expect(expr,value) (expr) 146# define expect(expr,value) (expr)
144# define INLINE static 147# define INLINE static
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;
563 { 615 {
564 while (expect_true (cxix >= 0)) 616 while (expect_true (cxix >= 0))
565 { 617 {
566 PERL_CONTEXT *cx = &ccstk[cxix--]; 618 PERL_CONTEXT *cx = &ccstk[cxix--];
567 619
568 if (expect_true (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT)) 620 if (expect_true (CxTYPE (cx) == CXt_SUB) || expect_false (CxTYPE (cx) == CXt_FORMAT))
569 { 621 {
570 CV *cv = cx->blk_sub.cv; 622 CV *cv = cx->blk_sub.cv;
571 623
572 if (expect_true (CvDEPTH (cv))) 624 if (expect_true (CvDEPTH (cv)))
573 { 625 {
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}
935 991
936 SvREFCNT_dec (GvSV (PL_defgv)); 992static void
937 SvREFCNT_dec (GvAV (PL_defgv)); 993coro_destruct_perl (pTHX_ struct coro *coro)
938 SvREFCNT_dec (GvSV (PL_errgv)); 994{
939 SvREFCNT_dec (PL_defoutgv); 995 SV *svf [9];
940 SvREFCNT_dec (PL_rs);
941 SvREFCNT_dec (GvSV (irsgv));
942 996
943 SvREFCNT_dec (PL_diehook);
944 SvREFCNT_dec (PL_warnhook);
945 997 {
998 struct coro *current = SvSTATE_current;
999
1000 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack));
1001
1002 save_perl (aTHX_ current);
1003 load_perl (aTHX_ coro);
1004
1005 coro_unwind_stacks (aTHX);
1006 coro_destruct_stacks (aTHX);
1007
1008 // now save some sv's to be free'd later
1009 svf [0] = GvSV (PL_defgv);
1010 svf [1] = (SV *)GvAV (PL_defgv);
1011 svf [2] = GvSV (PL_errgv);
1012 svf [3] = (SV *)PL_defoutgv;
1013 svf [4] = PL_rs;
1014 svf [5] = GvSV (irsgv);
1015 svf [6] = (SV *)GvHV (PL_hintgv);
1016 svf [7] = PL_diehook;
1017 svf [8] = PL_warnhook;
1018 assert (9 == sizeof (svf) / sizeof (*svf));
1019
1020 load_perl (aTHX_ current);
1021 }
1022
1023 {
1024 int i;
1025
1026 for (i = 0; i < sizeof (svf) / sizeof (*svf); ++i)
1027 SvREFCNT_dec (svf [i]);
1028
946 SvREFCNT_dec (coro->saved_deffh); 1029 SvREFCNT_dec (coro->saved_deffh);
947 SvREFCNT_dec (coro->rouse_cb); 1030 SvREFCNT_dec (coro->rouse_cb);
948 SvREFCNT_dec (coro->invoke_cb); 1031 SvREFCNT_dec (coro->invoke_cb);
949 SvREFCNT_dec (coro->invoke_av); 1032 SvREFCNT_dec (coro->invoke_av);
950 1033 }
951 coro_destruct_stacks (aTHX);
952} 1034}
953 1035
954INLINE void 1036INLINE void
955free_coro_mortal (pTHX) 1037free_coro_mortal (pTHX)
956{ 1038{
964static int 1046static int
965runops_trace (pTHX) 1047runops_trace (pTHX)
966{ 1048{
967 COP *oldcop = 0; 1049 COP *oldcop = 0;
968 int oldcxix = -2; 1050 int oldcxix = -2;
969 struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */
970 coro_cctx *cctx = coro->cctx;
971 1051
972 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 1052 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
973 { 1053 {
974 PERL_ASYNC_CHECK (); 1054 PERL_ASYNC_CHECK ();
975 1055
976 if (cctx->flags & CC_TRACE_ALL) 1056 if (cctx_current->flags & CC_TRACE_ALL)
977 { 1057 {
978 if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) 1058 if (PL_op->op_type == OP_LEAVESUB && cctx_current->flags & CC_TRACE_SUB)
979 { 1059 {
980 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1060 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
981 SV **bot, **top; 1061 SV **bot, **top;
982 AV *av = newAV (); /* return values */ 1062 AV *av = newAV (); /* return values */
983 SV **cb; 1063 SV **cb;
1020 1100
1021 if (PL_curcop != &PL_compiling) 1101 if (PL_curcop != &PL_compiling)
1022 { 1102 {
1023 SV **cb; 1103 SV **cb;
1024 1104
1025 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) 1105 if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB)
1026 { 1106 {
1027 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1107 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1028 1108
1029 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 1109 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
1030 { 1110 {
1053 } 1133 }
1054 1134
1055 oldcxix = cxstack_ix; 1135 oldcxix = cxstack_ix;
1056 } 1136 }
1057 1137
1058 if (cctx->flags & CC_TRACE_LINE) 1138 if (cctx_current->flags & CC_TRACE_LINE)
1059 { 1139 {
1060 dSP; 1140 dSP;
1061 1141
1062 PL_runops = RUNOPS_DEFAULT; 1142 PL_runops = RUNOPS_DEFAULT;
1063 ENTER; 1143 ENTER;
1082 1162
1083 TAINT_NOT; 1163 TAINT_NOT;
1084 return 0; 1164 return 0;
1085} 1165}
1086 1166
1087static struct coro_cctx *cctx_ssl_cctx;
1088static struct CoroSLF cctx_ssl_frame; 1167static struct CoroSLF cctx_ssl_frame;
1089 1168
1090static void 1169static void
1091slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) 1170slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1092{ 1171{
1093 ta->prev = (struct coro *)cctx_ssl_cctx;
1094 ta->next = 0; 1172 ta->prev = 0;
1095} 1173}
1096 1174
1097static int 1175static int
1098slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) 1176slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1099{ 1177{
1102 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ 1180 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1103} 1181}
1104 1182
1105/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ 1183/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1106static void NOINLINE 1184static void NOINLINE
1107cctx_prepare (pTHX_ coro_cctx *cctx) 1185cctx_prepare (pTHX)
1108{ 1186{
1109 PL_top_env = &PL_start_env; 1187 PL_top_env = &PL_start_env;
1110 1188
1111 if (cctx->flags & CC_TRACE) 1189 if (cctx_current->flags & CC_TRACE)
1112 PL_runops = runops_trace; 1190 PL_runops = runops_trace;
1113 1191
1114 /* we already must be executing an SLF op, there is no other valid way 1192 /* we already must be executing an SLF op, there is no other valid way
1115 * that can lead to creation of a new cctx */ 1193 * that can lead to creation of a new cctx */
1116 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", 1194 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)",
1117 slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); 1195 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1118 1196
1119 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ 1197 /* 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; 1198 cctx_ssl_frame = slf_frame;
1122 1199
1123 slf_frame.prepare = slf_prepare_set_stacklevel; 1200 slf_frame.prepare = slf_prepare_set_stacklevel;
1124 slf_frame.check = slf_check_set_stacklevel; 1201 slf_frame.check = slf_check_set_stacklevel;
1125} 1202}
1148 /* normally we would need to skip the entersub here */ 1225 /* normally we would need to skip the entersub here */
1149 /* not doing so will re-execute it, which is exactly what we want */ 1226 /* not doing so will re-execute it, which is exactly what we want */
1150 /* PL_nop = PL_nop->op_next */ 1227 /* PL_nop = PL_nop->op_next */
1151 1228
1152 /* inject a fake subroutine call to cctx_init */ 1229 /* inject a fake subroutine call to cctx_init */
1153 cctx_prepare (aTHX_ (coro_cctx *)arg); 1230 cctx_prepare (aTHX);
1154 1231
1155 /* cctx_run is the alternative tail of transfer() */ 1232 /* cctx_run is the alternative tail of transfer() */
1156 transfer_tail (aTHX); 1233 transfer_tail (aTHX);
1157 1234
1158 /* somebody or something will hit me for both perl_run and PL_restartop */ 1235 /* somebody or something will hit me for both perl_run and PL_restartop */
1166 /* 1243 /*
1167 * If perl-run returns we assume exit() was being called or the coro 1244 * 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) 1245 * 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 1246 * 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" 1247 * bootstrap-time "top" top_env, as we cannot restore the "main"
1171 * coroutine as Coro has no such concept 1248 * coroutine as Coro has no such concept.
1249 * This actually isn't valid with the pthread backend, but OSes requiring
1250 * that backend are too broken to do it in a standards-compliant way.
1172 */ 1251 */
1173 PL_top_env = main_top_env; 1252 PL_top_env = main_top_env;
1174 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1253 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1175 } 1254 }
1176} 1255}
1253cctx_destroy (coro_cctx *cctx) 1332cctx_destroy (coro_cctx *cctx)
1254{ 1333{
1255 if (!cctx) 1334 if (!cctx)
1256 return; 1335 return;
1257 1336
1337 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary?
1338
1258 --cctx_count; 1339 --cctx_count;
1259 coro_destroy (&cctx->cctx); 1340 coro_destroy (&cctx->cctx);
1260 1341
1261 /* coro_transfer creates new, empty cctx's */ 1342 /* coro_transfer creates new, empty cctx's */
1262 if (cctx->sptr) 1343 if (cctx->sptr)
1325 /* TODO: throwing up here is considered harmful */ 1406 /* TODO: throwing up here is considered harmful */
1326 1407
1327 if (expect_true (prev != next)) 1408 if (expect_true (prev != next))
1328 { 1409 {
1329 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1410 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,"); 1411 croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,");
1331 1412
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)) 1413 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,"); 1414 croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,");
1337 1415
1338#if !PERL_VERSION_ATLEAST (5,10,0) 1416#if !PERL_VERSION_ATLEAST (5,10,0)
1339 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1417 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,"); 1418 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,");
1341#endif 1419#endif
1347transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1425transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1348{ 1426{
1349 dSTACKLEVEL; 1427 dSTACKLEVEL;
1350 1428
1351 /* sometimes transfer is only called to set idle_sp */ 1429 /* sometimes transfer is only called to set idle_sp */
1352 if (expect_false (!next)) 1430 if (expect_false (!prev))
1353 { 1431 {
1354 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1432 cctx_current->idle_sp = STACKLEVEL;
1355 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1433 assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1356 } 1434 }
1357 else if (expect_true (prev != next)) 1435 else if (expect_true (prev != next))
1358 { 1436 {
1359 coro_cctx *prev__cctx; 1437 coro_cctx *cctx_prev;
1360 1438
1361 if (expect_false (prev->flags & CF_NEW)) 1439 if (expect_false (prev->flags & CF_NEW))
1362 { 1440 {
1363 /* create a new empty/source context */ 1441 /* create a new empty/source context */
1364 prev->cctx = cctx_new_empty ();
1365 prev->flags &= ~CF_NEW; 1442 prev->flags &= ~CF_NEW;
1366 prev->flags |= CF_RUNNING; 1443 prev->flags |= CF_RUNNING;
1367 } 1444 }
1368 1445
1369 prev->flags &= ~CF_RUNNING; 1446 prev->flags &= ~CF_RUNNING;
1380 coro_setup (aTHX_ next); 1457 coro_setup (aTHX_ next);
1381 } 1458 }
1382 else 1459 else
1383 load_perl (aTHX_ next); 1460 load_perl (aTHX_ next);
1384 1461
1385 prev__cctx = prev->cctx;
1386
1387 /* possibly untie and reuse the cctx */ 1462 /* possibly untie and reuse the cctx */
1388 if (expect_true ( 1463 if (expect_true (
1389 prev__cctx->idle_sp == STACKLEVEL 1464 cctx_current->idle_sp == STACKLEVEL
1390 && !(prev__cctx->flags & CC_TRACE) 1465 && !(cctx_current->flags & CC_TRACE)
1391 && !force_cctx 1466 && !force_cctx
1392 )) 1467 ))
1393 { 1468 {
1394 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1469 /* 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)); 1470 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te));
1396 1471
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 */ 1472 /* 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 */ 1473 /* without this the next cctx_get might destroy the running cctx while still in use */
1401 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1474 if (expect_false (CCTX_EXPIRED (cctx_current)))
1402 if (!next->cctx) 1475 if (expect_true (!next->cctx))
1403 next->cctx = cctx_get (aTHX); 1476 next->cctx = cctx_get (aTHX);
1404 1477
1405 cctx_put (prev__cctx); 1478 cctx_put (cctx_current);
1406 } 1479 }
1480 else
1481 prev->cctx = cctx_current;
1407 1482
1408 ++next->usecount; 1483 ++next->usecount;
1409 1484
1410 if (expect_true (!next->cctx)) 1485 cctx_prev = cctx_current;
1411 next->cctx = cctx_get (aTHX); 1486 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1412 1487
1413 if (expect_false (prev__cctx != next->cctx)) 1488 next->cctx = 0;
1489
1490 if (expect_false (cctx_prev != cctx_current))
1414 { 1491 {
1415 prev__cctx->top_env = PL_top_env; 1492 cctx_prev->top_env = PL_top_env;
1416 PL_top_env = next->cctx->top_env; 1493 PL_top_env = cctx_current->top_env;
1417 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1494 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1418 } 1495 }
1419 1496
1420 transfer_tail (aTHX); 1497 transfer_tail (aTHX);
1421 } 1498 }
1422} 1499}
1430coro_state_destroy (pTHX_ struct coro *coro) 1507coro_state_destroy (pTHX_ struct coro *coro)
1431{ 1508{
1432 if (coro->flags & CF_DESTROYED) 1509 if (coro->flags & CF_DESTROYED)
1433 return 0; 1510 return 0;
1434 1511
1435 if (coro->on_destroy) 1512 if (coro->on_destroy && !PL_dirty)
1436 coro->on_destroy (aTHX_ coro); 1513 coro->on_destroy (aTHX_ coro);
1437 1514
1438 coro->flags |= CF_DESTROYED; 1515 coro->flags |= CF_DESTROYED;
1439 1516
1440 if (coro->flags & CF_READY) 1517 if (coro->flags & CF_READY)
1444 --coro_nready; 1521 --coro_nready;
1445 } 1522 }
1446 else 1523 else
1447 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ 1524 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
1448 1525
1449 if (coro->mainstack && coro->mainstack != main_mainstack) 1526 if (coro->mainstack
1450 { 1527 && coro->mainstack != main_mainstack
1451 struct coro temp; 1528 && coro->slot
1452 1529 && !PL_dirty)
1453 assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING)));
1454
1455 save_perl (aTHX_ &temp);
1456 load_perl (aTHX_ coro);
1457
1458 coro_destruct (aTHX_ coro); 1530 coro_destruct_perl (aTHX_ coro);
1459
1460 load_perl (aTHX_ &temp);
1461
1462 coro->slot = 0;
1463 }
1464 1531
1465 cctx_destroy (coro->cctx); 1532 cctx_destroy (coro->cctx);
1466 SvREFCNT_dec (coro->startcv); 1533 SvREFCNT_dec (coro->startcv);
1467 SvREFCNT_dec (coro->args); 1534 SvREFCNT_dec (coro->args);
1468 SvREFCNT_dec (CORO_THROW); 1535 SvREFCNT_dec (CORO_THROW);
1555/** Coro ********************************************************************/ 1622/** Coro ********************************************************************/
1556 1623
1557INLINE void 1624INLINE void
1558coro_enq (pTHX_ struct coro *coro) 1625coro_enq (pTHX_ struct coro *coro)
1559{ 1626{
1560 av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv)); 1627 struct coro **ready = coro_ready [coro->prio - PRIO_MIN];
1561}
1562 1628
1563INLINE SV * 1629 SvREFCNT_inc_NN (coro->hv);
1630
1631 coro->next_ready = 0;
1632 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro;
1633 ready [1] = coro;
1634}
1635
1636INLINE struct coro *
1564coro_deq (pTHX) 1637coro_deq (pTHX)
1565{ 1638{
1566 int prio; 1639 int prio;
1567 1640
1568 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) 1641 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; )
1569 if (AvFILLp (coro_ready [prio]) >= 0) 1642 {
1570 return av_shift (coro_ready [prio]); 1643 struct coro **ready = coro_ready [prio];
1644
1645 if (ready [0])
1646 {
1647 struct coro *coro = ready [0];
1648 ready [0] = coro->next_ready;
1649 return coro;
1650 }
1651 }
1571 1652
1572 return 0; 1653 return 0;
1573} 1654}
1574 1655
1575static int 1656static int
1577{ 1658{
1578 struct coro *coro; 1659 struct coro *coro;
1579 SV *sv_hook; 1660 SV *sv_hook;
1580 void (*xs_hook)(void); 1661 void (*xs_hook)(void);
1581 1662
1582 if (SvROK (coro_sv))
1583 coro_sv = SvRV (coro_sv);
1584
1585 coro = SvSTATE (coro_sv); 1663 coro = SvSTATE (coro_sv);
1586 1664
1587 if (coro->flags & CF_READY) 1665 if (coro->flags & CF_READY)
1588 return 0; 1666 return 0;
1589 1667
1642static void 1720static void
1643prepare_schedule (pTHX_ struct coro_transfer_args *ta) 1721prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1644{ 1722{
1645 for (;;) 1723 for (;;)
1646 { 1724 {
1647 SV *next_sv = coro_deq (aTHX); 1725 struct coro *next = coro_deq (aTHX);
1648 1726
1649 if (expect_true (next_sv)) 1727 if (expect_true (next))
1650 { 1728 {
1651 struct coro *next = SvSTATE_hv (next_sv);
1652
1653 /* cannot transfer to destroyed coros, skip and look for next */ 1729 /* cannot transfer to destroyed coros, skip and look for next */
1654 if (expect_false (next->flags & CF_DESTROYED)) 1730 if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED)))
1655 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ 1731 SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */
1656 else 1732 else
1657 { 1733 {
1658 next->flags &= ~CF_READY; 1734 next->flags &= ~CF_READY;
1659 --coro_nready; 1735 --coro_nready;
1660 1736
1663 } 1739 }
1664 } 1740 }
1665 else 1741 else
1666 { 1742 {
1667 /* nothing to schedule: call the idle handler */ 1743 /* nothing to schedule: call the idle handler */
1744 if (SvROK (sv_idle)
1745 && SvOBJECT (SvRV (sv_idle)))
1746 {
1747 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1748 api_ready (aTHX_ SvRV (sv_idle));
1749 --coro_nready;
1750 }
1751 else
1752 {
1668 dSP; 1753 dSP;
1669 1754
1670 ENTER; 1755 ENTER;
1671 SAVETMPS; 1756 SAVETMPS;
1672 1757
1673 PUSHMARK (SP); 1758 PUSHMARK (SP);
1674 PUTBACK; 1759 PUTBACK;
1675 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); 1760 call_sv (sv_idle, G_VOID | G_DISCARD);
1676 1761
1677 FREETMPS; 1762 FREETMPS;
1678 LEAVE; 1763 LEAVE;
1764 }
1679 } 1765 }
1680 } 1766 }
1681} 1767}
1682 1768
1683INLINE void 1769INLINE void
1754static void 1840static void
1755api_trace (pTHX_ SV *coro_sv, int flags) 1841api_trace (pTHX_ SV *coro_sv, int flags)
1756{ 1842{
1757 struct coro *coro = SvSTATE (coro_sv); 1843 struct coro *coro = SvSTATE (coro_sv);
1758 1844
1845 if (coro->flags & CF_RUNNING)
1846 croak ("cannot enable tracing on a running coroutine, caught");
1847
1759 if (flags & CC_TRACE) 1848 if (flags & CC_TRACE)
1760 { 1849 {
1761 if (!coro->cctx) 1850 if (!coro->cctx)
1762 coro->cctx = cctx_new_run (); 1851 coro->cctx = cctx_new_run ();
1763 else if (!(coro->cctx->flags & CC_TRACE)) 1852 else if (!(coro->cctx->flags & CC_TRACE))
1764 croak ("cannot enable tracing on coroutine with custom stack,"); 1853 croak ("cannot enable tracing on coroutine with custom stack, caught");
1765 1854
1766 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1855 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1767 } 1856 }
1768 else if (coro->cctx && coro->cctx->flags & CC_TRACE) 1857 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1769 { 1858 {
1825 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ 1914 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1826 api_ready (aTHX_ sv_manager); 1915 api_ready (aTHX_ sv_manager);
1827 1916
1828 frame->prepare = prepare_schedule; 1917 frame->prepare = prepare_schedule;
1829 frame->check = slf_check_repeat; 1918 frame->check = slf_check_repeat;
1919
1920 /* as a minor optimisation, we could unwind all stacks here */
1921 /* but that puts extra pressure on pp_slf, and is not worth much */
1922 /*coro_unwind_stacks (aTHX);*/
1830} 1923}
1831 1924
1832/*****************************************************************************/ 1925/*****************************************************************************/
1833/* async pool handler */ 1926/* async pool handler */
1834 1927
1917 SV *data = (SV *)GENSUB_ARG; 2010 SV *data = (SV *)GENSUB_ARG;
1918 2011
1919 if (SvTYPE (SvRV (data)) != SVt_PVAV) 2012 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1920 { 2013 {
1921 /* first call, set args */ 2014 /* first call, set args */
2015 SV *coro = SvRV (data);
1922 AV *av = newAV (); 2016 AV *av = newAV ();
1923 SV *coro = SvRV (data);
1924 2017
1925 SvRV_set (data, (SV *)av); 2018 SvRV_set (data, (SV *)av);
1926 api_ready (aTHX_ coro);
1927 SvREFCNT_dec (coro);
1928 2019
1929 /* better take a full copy of the arguments */ 2020 /* better take a full copy of the arguments */
1930 while (items--) 2021 while (items--)
1931 av_store (av, items, newSVsv (ST (items))); 2022 av_store (av, items, newSVsv (ST (items)));
2023
2024 api_ready (aTHX_ coro);
2025 SvREFCNT_dec (coro);
1932 } 2026 }
1933 2027
1934 XSRETURN_EMPTY; 2028 XSRETURN_EMPTY;
1935} 2029}
1936 2030
1953 2047
1954 EXTEND (SP, AvFILLp (av) + 1); 2048 EXTEND (SP, AvFILLp (av) + 1);
1955 for (i = 0; i <= AvFILLp (av); ++i) 2049 for (i = 0; i <= AvFILLp (av); ++i)
1956 PUSHs (sv_2mortal (AvARRAY (av)[i])); 2050 PUSHs (sv_2mortal (AvARRAY (av)[i]));
1957 2051
1958 /* we have stolen the elements, so ste length to zero and free */ 2052 /* we have stolen the elements, so set length to zero and free */
1959 AvFILLp (av) = -1; 2053 AvFILLp (av) = -1;
1960 av_undef (av); 2054 av_undef (av);
1961 2055
1962 PUTBACK; 2056 PUTBACK;
1963 } 2057 }
2250 } 2344 }
2251 else 2345 else
2252 slf_argc = 0; 2346 slf_argc = 0;
2253 2347
2254 PL_op->op_ppaddr = pp_slf; 2348 PL_op->op_ppaddr = pp_slf;
2255 PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ 2349 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2256 2350
2257 PL_op = (OP *)&slf_restore; 2351 PL_op = (OP *)&slf_restore;
2352}
2353
2354/*****************************************************************************/
2355/* dynamic wind */
2356
2357static void
2358on_enterleave_call (pTHX_ SV *cb)
2359{
2360 dSP;
2361
2362 PUSHSTACK;
2363
2364 PUSHMARK (SP);
2365 PUTBACK;
2366 call_sv (cb, G_VOID | G_DISCARD);
2367 SPAGAIN;
2368
2369 POPSTACK;
2370}
2371
2372static SV *
2373coro_avp_pop_and_free (pTHX_ AV **avp)
2374{
2375 AV *av = *avp;
2376 SV *res = av_pop (av);
2377
2378 if (AvFILLp (av) < 0)
2379 {
2380 *avp = 0;
2381 SvREFCNT_dec (av);
2382 }
2383
2384 return res;
2385}
2386
2387static void
2388coro_pop_on_enter (pTHX_ void *coro)
2389{
2390 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_enter);
2391 SvREFCNT_dec (cb);
2392}
2393
2394static void
2395coro_pop_on_leave (pTHX_ void *coro)
2396{
2397 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave);
2398 on_enterleave_call (aTHX_ sv_2mortal (cb));
2258} 2399}
2259 2400
2260/*****************************************************************************/ 2401/*****************************************************************************/
2261/* PerlIO::cede */ 2402/* PerlIO::cede */
2262 2403
2336/* Coro::Semaphore & Coro::Signal */ 2477/* Coro::Semaphore & Coro::Signal */
2337 2478
2338static SV * 2479static SV *
2339coro_waitarray_new (pTHX_ int count) 2480coro_waitarray_new (pTHX_ int count)
2340{ 2481{
2341 /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ 2482 /* a waitarray=semaphore contains a counter IV in $sem->[0] and any waiters after that */
2342 AV *av = newAV (); 2483 AV *av = newAV ();
2343 SV **ary; 2484 SV **ary;
2344 2485
2345 /* unfortunately, building manually saves memory */ 2486 /* unfortunately, building manually saves memory */
2346 Newx (ary, 2, SV *); 2487 Newx (ary, 2, SV *);
2347 AvALLOC (av) = ary; 2488 AvALLOC (av) = ary;
2489#if PERL_VERSION_ATLEAST (5,10,0)
2348 /*AvARRAY (av) = ary;*/ 2490 AvARRAY (av) = ary;
2491#else
2349 SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ 2492 /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */
2493 /* -DDEBUGGING flags this as a bug, despite it perfectly working */
2494 SvPVX ((SV *)av) = (char *)ary;
2495#endif
2350 AvMAX (av) = 1; 2496 AvMAX (av) = 1;
2351 AvFILLp (av) = 0; 2497 AvFILLp (av) = 0;
2352 ary [0] = newSViv (count); 2498 ary [0] = newSViv (count);
2353 2499
2354 return newRV_noinc ((SV *)av); 2500 return newRV_noinc ((SV *)av);
2485 { 2631 {
2486 /* callback form */ 2632 /* callback form */
2487 AV *av = (AV *)SvRV (arg [0]); 2633 AV *av = (AV *)SvRV (arg [0]);
2488 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); 2634 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]);
2489 2635
2490 av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); 2636 av_push (av, SvREFCNT_inc_NN (cb_cv));
2491 2637
2492 if (SvIVX (AvARRAY (av)[0]) > 0) 2638 if (SvIVX (AvARRAY (av)[0]) > 0)
2493 coro_semaphore_adjust (aTHX_ av, 0); 2639 coro_semaphore_adjust (aTHX_ av, 0);
2494 2640
2495 frame->prepare = prepare_nop; 2641 frame->prepare = prepare_nop;
2519 AvARRAY (av)[0] = AvARRAY (av)[1]; 2665 AvARRAY (av)[0] = AvARRAY (av)[1];
2520 AvARRAY (av)[1] = cb; 2666 AvARRAY (av)[1] = cb;
2521 2667
2522 cb = av_shift (av); 2668 cb = av_shift (av);
2523 2669
2670 if (SvTYPE (cb) == SVt_PVCV)
2671 {
2672 dSP;
2673 PUSHMARK (SP);
2674 XPUSHs (sv_2mortal (newRV_inc ((SV *)av)));
2675 PUTBACK;
2676 call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR);
2677 }
2678 else
2679 {
2524 api_ready (aTHX_ cb); 2680 api_ready (aTHX_ cb);
2525 sv_setiv (cb, 0); /* signal waiter */ 2681 sv_setiv (cb, 0); /* signal waiter */
2682 }
2683
2526 SvREFCNT_dec (cb); 2684 SvREFCNT_dec (cb);
2527 2685
2528 --count; 2686 --count;
2529 } 2687 }
2530} 2688}
2539static void 2697static void
2540slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2698slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2541{ 2699{
2542 AV *av = (AV *)SvRV (arg [0]); 2700 AV *av = (AV *)SvRV (arg [0]);
2543 2701
2702 if (items >= 2)
2703 {
2704 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]);
2705 av_push (av, SvREFCNT_inc_NN (cb_cv));
2706
2544 if (SvIVX (AvARRAY (av)[0])) 2707 if (SvIVX (AvARRAY (av)[0]))
2708 coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */
2709
2710 frame->prepare = prepare_nop;
2711 frame->check = slf_check_nop;
2712 }
2713 else if (SvIVX (AvARRAY (av)[0]))
2545 { 2714 {
2546 SvIVX (AvARRAY (av)[0]) = 0; 2715 SvIVX (AvARRAY (av)[0]) = 0;
2547 frame->prepare = prepare_nop; 2716 frame->prepare = prepare_nop;
2548 frame->check = slf_check_nop; 2717 frame->check = slf_check_nop;
2549 } 2718 }
2550 else 2719 else
2551 { 2720 {
2552 SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */ 2721 SV *waiter = newSVsv (coro_current); /* owned by signal av */
2553 2722
2554 av_push (av, waiter); 2723 av_push (av, waiter);
2555 2724
2556 frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */ 2725 frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */
2557 frame->prepare = prepare_schedule; 2726 frame->prepare = prepare_schedule;
2724 XSRETURN_EMPTY; 2893 XSRETURN_EMPTY;
2725} 2894}
2726 2895
2727/*****************************************************************************/ 2896/*****************************************************************************/
2728 2897
2898#if CORO_CLONE
2899# include "clone.c"
2900#endif
2901
2729MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 2902MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2730 2903
2731PROTOTYPES: DISABLE 2904PROTOTYPES: DISABLE
2732 2905
2733BOOT: 2906BOOT:
2736# if CORO_PTHREAD 2909# if CORO_PTHREAD
2737 coro_thx = PERL_GET_CONTEXT; 2910 coro_thx = PERL_GET_CONTEXT;
2738# endif 2911# endif
2739#endif 2912#endif
2740 BOOT_PAGESIZE; 2913 BOOT_PAGESIZE;
2914
2915 cctx_current = cctx_new_empty ();
2741 2916
2742 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 2917 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
2743 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 2918 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
2744 2919
2745 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; 2920 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
2871void 3046void
2872_exit (int code) 3047_exit (int code)
2873 PROTOTYPE: $ 3048 PROTOTYPE: $
2874 CODE: 3049 CODE:
2875 _exit (code); 3050 _exit (code);
3051
3052SV *
3053clone (Coro::State coro)
3054 CODE:
3055{
3056#if CORO_CLONE
3057 struct coro *ncoro = coro_clone (aTHX_ coro);
3058 MAGIC *mg;
3059 /* TODO: too much duplication */
3060 ncoro->hv = newHV ();
3061 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
3062 mg->mg_flags |= MGf_DUP;
3063 RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv));
3064#else
3065 croak ("Coro::State->clone has not been configured into this installation of Coro, realised");
3066#endif
3067}
3068 OUTPUT:
3069 RETVAL
2876 3070
2877int 3071int
2878cctx_stacksize (int new_stacksize = 0) 3072cctx_stacksize (int new_stacksize = 0)
2879 PROTOTYPE: ;$ 3073 PROTOTYPE: ;$
2880 CODE: 3074 CODE:
2930 eval = 1 3124 eval = 1
2931 CODE: 3125 CODE:
2932{ 3126{
2933 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3127 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
2934 { 3128 {
2935 struct coro temp; 3129 struct coro *current = SvSTATE_current;
2936 3130
2937 if (!(coro->flags & CF_RUNNING)) 3131 if (current != coro)
2938 { 3132 {
2939 PUTBACK; 3133 PUTBACK;
2940 save_perl (aTHX_ &temp); 3134 save_perl (aTHX_ current);
2941 load_perl (aTHX_ coro); 3135 load_perl (aTHX_ coro);
3136 SPAGAIN;
2942 } 3137 }
2943 3138
2944 {
2945 dSP;
2946 ENTER;
2947 SAVETMPS;
2948 PUTBACK;
2949 PUSHSTACK; 3139 PUSHSTACK;
3140
2950 PUSHMARK (SP); 3141 PUSHMARK (SP);
3142 PUTBACK;
2951 3143
2952 if (ix) 3144 if (ix)
2953 eval_sv (coderef, 0); 3145 eval_sv (coderef, 0);
2954 else 3146 else
2955 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3147 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
2956 3148
2957 POPSTACK; 3149 POPSTACK;
2958 SPAGAIN; 3150 SPAGAIN;
2959 FREETMPS;
2960 LEAVE;
2961 PUTBACK;
2962 }
2963 3151
2964 if (!(coro->flags & CF_RUNNING)) 3152 if (current != coro)
2965 { 3153 {
3154 PUTBACK;
2966 save_perl (aTHX_ coro); 3155 save_perl (aTHX_ coro);
2967 load_perl (aTHX_ &temp); 3156 load_perl (aTHX_ current);
2968 SPAGAIN; 3157 SPAGAIN;
2969 } 3158 }
2970 } 3159 }
2971} 3160}
2972 3161
2976 ALIAS: 3165 ALIAS:
2977 is_ready = CF_READY 3166 is_ready = CF_READY
2978 is_running = CF_RUNNING 3167 is_running = CF_RUNNING
2979 is_new = CF_NEW 3168 is_new = CF_NEW
2980 is_destroyed = CF_DESTROYED 3169 is_destroyed = CF_DESTROYED
3170 is_suspended = CF_SUSPENDED
2981 CODE: 3171 CODE:
2982 RETVAL = boolSV (coro->flags & ix); 3172 RETVAL = boolSV (coro->flags & ix);
2983 OUTPUT: 3173 OUTPUT:
2984 RETVAL 3174 RETVAL
2985 3175
3001 3191
3002SV * 3192SV *
3003has_cctx (Coro::State coro) 3193has_cctx (Coro::State coro)
3004 PROTOTYPE: $ 3194 PROTOTYPE: $
3005 CODE: 3195 CODE:
3006 RETVAL = boolSV (!!coro->cctx); 3196 /* maybe manage the running flag differently */
3197 RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING));
3007 OUTPUT: 3198 OUTPUT:
3008 RETVAL 3199 RETVAL
3009 3200
3010int 3201int
3011is_traced (Coro::State coro) 3202is_traced (Coro::State coro)
3031 3222
3032void 3223void
3033force_cctx () 3224force_cctx ()
3034 PROTOTYPE: 3225 PROTOTYPE:
3035 CODE: 3226 CODE:
3036 SvSTATE_current->cctx->idle_sp = 0; 3227 cctx_current->idle_sp = 0;
3037 3228
3038void 3229void
3039swap_defsv (Coro::State self) 3230swap_defsv (Coro::State self)
3040 PROTOTYPE: $ 3231 PROTOTYPE: $
3041 ALIAS: 3232 ALIAS:
3049 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 3240 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
3050 3241
3051 SV *tmp = *src; *src = *dst; *dst = tmp; 3242 SV *tmp = *src; *src = *dst; *dst = tmp;
3052 } 3243 }
3053 3244
3245void
3246cancel (Coro::State self)
3247 CODE:
3248 coro_state_destroy (aTHX_ self);
3249 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3250
3054 3251
3055MODULE = Coro::State PACKAGE = Coro 3252MODULE = Coro::State PACKAGE = Coro
3056 3253
3057BOOT: 3254BOOT:
3058{ 3255{
3059 int i;
3060
3061 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3256 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3062 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3257 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3063 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3258 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3064 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3259 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3065 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3260 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); 3261 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3067 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3262 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3068 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3263 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3264 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3069 3265
3070 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3266 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); 3267 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); 3268 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); 3269 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);
3078 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 3274 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
3079 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 3275 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
3080 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 3276 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
3081 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 3277 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
3082 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 3278 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
3083
3084 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
3085 coro_ready[i] = newAV ();
3086 3279
3087 { 3280 {
3088 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3281 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
3089 3282
3090 coroapi.schedule = api_schedule; 3283 coroapi.schedule = api_schedule;
3131cede_notself (...) 3324cede_notself (...)
3132 CODE: 3325 CODE:
3133 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3326 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3134 3327
3135void 3328void
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) 3329_set_current (SV *current)
3143 PROTOTYPE: $ 3330 PROTOTYPE: $
3144 CODE: 3331 CODE:
3145 SvREFCNT_dec (SvRV (coro_current)); 3332 SvREFCNT_dec (SvRV (coro_current));
3146 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); 3333 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
3188 PROTOTYPE: 3375 PROTOTYPE:
3189 CODE: 3376 CODE:
3190 RETVAL = coro_nready; 3377 RETVAL = coro_nready;
3191 OUTPUT: 3378 OUTPUT:
3192 RETVAL 3379 RETVAL
3380
3381void
3382suspend (Coro::State self)
3383 PROTOTYPE: $
3384 CODE:
3385 self->flags |= CF_SUSPENDED;
3386
3387void
3388resume (Coro::State self)
3389 PROTOTYPE: $
3390 CODE:
3391 self->flags &= ~CF_SUSPENDED;
3193 3392
3194void 3393void
3195_pool_handler (...) 3394_pool_handler (...)
3196 CODE: 3395 CODE:
3197 CORO_EXECUTE_SLF_XS (slf_init_pool_handler); 3396 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
3252rouse_wait (...) 3451rouse_wait (...)
3253 PROTOTYPE: ;$ 3452 PROTOTYPE: ;$
3254 PPCODE: 3453 PPCODE:
3255 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3454 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3256 3455
3456void
3457on_enter (SV *block)
3458 ALIAS:
3459 on_leave = 1
3460 PROTOTYPE: &
3461 CODE:
3462{
3463 struct coro *coro = SvSTATE_current;
3464 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
3465
3466 block = (SV *)coro_sv_2cv (aTHX_ block);
3467
3468 if (!*avp)
3469 *avp = newAV ();
3470
3471 av_push (*avp, SvREFCNT_inc (block));
3472
3473 if (!ix)
3474 on_enterleave_call (aTHX_ block);
3475
3476 LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3477 SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro);
3478 ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3479}
3480
3257 3481
3258MODULE = Coro::State PACKAGE = PerlIO::cede 3482MODULE = Coro::State PACKAGE = PerlIO::cede
3259 3483
3260BOOT: 3484BOOT:
3261 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3485 PerlIO_define_layer (aTHX_ &PerlIO_cede);
3271 GvSTASH (CvGV (cv)) 3495 GvSTASH (CvGV (cv))
3272 ); 3496 );
3273 OUTPUT: 3497 OUTPUT:
3274 RETVAL 3498 RETVAL
3275 3499
3276# helper for Coro::Channel 3500# helper for Coro::Channel and others
3277SV * 3501SV *
3278_alloc (int count) 3502_alloc (int count)
3279 CODE: 3503 CODE:
3280 RETVAL = coro_waitarray_new (aTHX_ count); 3504 RETVAL = coro_waitarray_new (aTHX_ count);
3281 OUTPUT: 3505 OUTPUT:
3339 for (i = 1; i <= wcount; ++i) 3563 for (i = 1; i <= wcount; ++i)
3340 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); 3564 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
3341 } 3565 }
3342} 3566}
3343 3567
3568MODULE = Coro::State PACKAGE = Coro::SemaphoreSet
3569
3570void
3571_may_delete (SV *sem, int count, int extra_refs)
3572 PPCODE:
3573{
3574 AV *av = (AV *)SvRV (sem);
3575
3576 if (SvREFCNT ((SV *)av) == 1 + extra_refs
3577 && AvFILLp (av) == 0 /* no waiters, just count */
3578 && SvIV (AvARRAY (av)[0]) == count)
3579 XSRETURN_YES;
3580
3581 XSRETURN_NO;
3582}
3583
3344MODULE = Coro::State PACKAGE = Coro::Signal 3584MODULE = Coro::State PACKAGE = Coro::Signal
3345 3585
3346SV * 3586SV *
3347new (SV *klass) 3587new (SV *klass)
3348 CODE: 3588 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines