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.322 by root, Sat Nov 22 05:13:34 2008 UTC vs.
Revision 1.355 by root, Wed Jun 24 02:52:38 2009 UTC

133#else 133#else
134# define dSTACKLEVEL volatile void *stacklevel 134# define dSTACKLEVEL volatile void *stacklevel
135# define STACKLEVEL ((void *)&stacklevel) 135# define STACKLEVEL ((void *)&stacklevel)
136#endif 136#endif
137 137
138#define IN_DESTRUCT (PL_main_cv == Nullcv) 138#define IN_DESTRUCT PL_dirty
139 139
140#if __GNUC__ >= 3 140#if __GNUC__ >= 3
141# define attribute(x) __attribute__(x) 141# define attribute(x) __attribute__(x)
142# define expect(expr,value) __builtin_expect ((expr),(value)) 142# define expect(expr,value) __builtin_expect ((expr), (value))
143# define INLINE static inline 143# define INLINE static inline
144#else 144#else
145# define attribute(x) 145# define attribute(x)
146# define expect(expr,value) (expr) 146# define expect(expr,value) (expr)
147# define INLINE static 147# define INLINE static
175static HV *coro_state_stash, *coro_stash; 175static HV *coro_state_stash, *coro_stash;
176static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ 176static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
177 177
178static AV *av_destroy; /* destruction queue */ 178static AV *av_destroy; /* destruction queue */
179static SV *sv_manager; /* the manager coro */ 179static SV *sv_manager; /* the manager coro */
180static SV *sv_idle; /* $Coro::idle */
180 181
181static GV *irsgv; /* $/ */ 182static GV *irsgv; /* $/ */
182static GV *stdoutgv; /* *STDOUT */ 183static GV *stdoutgv; /* *STDOUT */
183static SV *rv_diehook; 184static SV *rv_diehook;
184static SV *rv_warnhook; 185static SV *rv_warnhook;
237enum { 238enum {
238 CF_RUNNING = 0x0001, /* coroutine is running */ 239 CF_RUNNING = 0x0001, /* coroutine is running */
239 CF_READY = 0x0002, /* coroutine is ready */ 240 CF_READY = 0x0002, /* coroutine is ready */
240 CF_NEW = 0x0004, /* has never been switched to */ 241 CF_NEW = 0x0004, /* has never been switched to */
241 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 */
242}; 244};
243 245
244/* 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 */
245typedef struct 247typedef struct
246{ 248{
247 SV *defsv; 249 SV *defsv;
248 AV *defav; 250 AV *defav;
249 SV *errsv; 251 SV *errsv;
250 SV *irsgv; 252 SV *irsgv;
253 HV *hinthv;
251#define VAR(name,type) type name; 254#define VAR(name,type) type name;
252# include "state.h" 255# include "state.h"
253#undef VAR 256#undef VAR
254} perl_slots; 257} perl_slots;
255 258
257 260
258/* this is a structure representing a perl-level coroutine */ 261/* this is a structure representing a perl-level coroutine */
259struct coro { 262struct coro {
260 /* the C coroutine allocated to this perl coroutine, if any */ 263 /* the C coroutine allocated to this perl coroutine, if any */
261 coro_cctx *cctx; 264 coro_cctx *cctx;
265
266 /* ready queue */
267 struct coro *next_ready;
262 268
263 /* state data */ 269 /* state data */
264 struct CoroSLF slf_frame; /* saved slf frame */ 270 struct CoroSLF slf_frame; /* saved slf frame */
265 AV *mainstack; 271 AV *mainstack;
266 perl_slots *slot; /* basically the saved sp */ 272 perl_slots *slot; /* basically the saved sp */
283 /* async_pool */ 289 /* async_pool */
284 SV *saved_deffh; 290 SV *saved_deffh;
285 SV *invoke_cb; 291 SV *invoke_cb;
286 AV *invoke_av; 292 AV *invoke_av;
287 293
294 /* on_enter/on_leave */
295 AV *on_enter;
296 AV *on_leave;
297
288 /* linked list */ 298 /* linked list */
289 struct coro *next, *prev; 299 struct coro *next, *prev;
290}; 300};
291 301
292typedef struct coro *Coro__State; 302typedef struct coro *Coro__State;
307#define PRIO_MIN -4 317#define PRIO_MIN -4
308 318
309/* for Coro.pm */ 319/* for Coro.pm */
310static SV *coro_current; 320static SV *coro_current;
311static SV *coro_readyhook; 321static SV *coro_readyhook;
312static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; 322static struct coro *coro_ready [PRIO_MAX - PRIO_MIN + 1][2]; /* head|tail */
313static CV *cv_coro_run, *cv_coro_terminate; 323static CV *cv_coro_run, *cv_coro_terminate;
314static struct coro *coro_first; 324static struct coro *coro_first;
315#define coro_nready coroapi.nready 325#define coro_nready coroapi.nready
316 326
317/** lowlevel stuff **********************************************************/ 327/** lowlevel stuff **********************************************************/
350INLINE CV * 360INLINE CV *
351coro_sv_2cv (pTHX_ SV *sv) 361coro_sv_2cv (pTHX_ SV *sv)
352{ 362{
353 HV *st; 363 HV *st;
354 GV *gvp; 364 GV *gvp;
355 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;
356} 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 */
357 424
358static AV * 425static AV *
359coro_derive_padlist (pTHX_ CV *cv) 426coro_derive_padlist (pTHX_ CV *cv)
360{ 427{
361 AV *padlist = CvPADLIST (cv); 428 AV *padlist = CvPADLIST (cv);
369 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 436 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
370#endif 437#endif
371 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 438 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
372 --AvFILLp (padlist); 439 --AvFILLp (padlist);
373 440
374 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); 441 av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0]));
375 av_store (newpadlist, 1, (SV *)newpad); 442 av_store (newpadlist, 1, (SV *)newpad);
376 443
377 return newpadlist; 444 return newpadlist;
378} 445}
379 446
380static void 447static void
381free_padlist (pTHX_ AV *padlist) 448free_padlist (pTHX_ AV *padlist)
382{ 449{
383 /* may be during global destruction */ 450 /* may be during global destruction */
384 if (SvREFCNT (padlist)) 451 if (!IN_DESTRUCT)
385 { 452 {
386 I32 i = AvFILLp (padlist); 453 I32 i = AvFILLp (padlist);
387 while (i >= 0) 454
455 while (i > 0) /* special-case index 0 */
388 { 456 {
389 SV **svp = av_fetch (padlist, i--, FALSE); 457 /* we try to be extra-careful here */
390 if (svp) 458 AV *av = (AV *)AvARRAY (padlist)[i--];
391 { 459 I32 j = AvFILLp (av);
392 SV *sv; 460
393 while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) 461 while (j >= 0)
462 SvREFCNT_dec (AvARRAY (av)[j--]);
463
464 AvFILLp (av) = -1;
394 SvREFCNT_dec (sv); 465 SvREFCNT_dec (av);
395
396 SvREFCNT_dec (*svp);
397 }
398 } 466 }
399 467
468 SvREFCNT_dec (AvARRAY (padlist)[0]);
469
470 AvFILLp (padlist) = -1;
400 SvREFCNT_dec ((SV*)padlist); 471 SvREFCNT_dec ((SV*)padlist);
401 } 472 }
402} 473}
403 474
404static int 475static int
413 484
414 SvREFCNT_dec (av); /* sv_magicext increased the refcount */ 485 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
415 486
416 return 0; 487 return 0;
417} 488}
418
419#define CORO_MAGIC_type_cv 26
420#define CORO_MAGIC_type_state PERL_MAGIC_ext
421 489
422static MGVTBL coro_cv_vtbl = { 490static MGVTBL coro_cv_vtbl = {
423 0, 0, 0, 0, 491 0, 0, 0, 0,
424 coro_cv_free 492 coro_cv_free
425}; 493};
426
427#define CORO_MAGIC_NN(sv, type) \
428 (expect_true (SvMAGIC (sv)->mg_type == type) \
429 ? SvMAGIC (sv) \
430 : mg_find (sv, type))
431
432#define CORO_MAGIC(sv, type) \
433 (expect_true (SvMAGIC (sv)) \
434 ? CORO_MAGIC_NN (sv, type) \
435 : 0)
436
437#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
438#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
439
440INLINE struct coro *
441SvSTATE_ (pTHX_ SV *coro)
442{
443 HV *stash;
444 MAGIC *mg;
445
446 if (SvROK (coro))
447 coro = SvRV (coro);
448
449 if (expect_false (SvTYPE (coro) != SVt_PVHV))
450 croak ("Coro::State object required");
451
452 stash = SvSTASH (coro);
453 if (expect_false (stash != coro_stash && stash != coro_state_stash))
454 {
455 /* very slow, but rare, check */
456 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
457 croak ("Coro::State object required");
458 }
459
460 mg = CORO_MAGIC_state (coro);
461 return (struct coro *)mg->mg_ptr;
462}
463
464#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
465
466/* faster than SvSTATE, but expects a coroutine hv */
467#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr)
468#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
469 494
470/* the next two functions merely cache the padlists */ 495/* the next two functions merely cache the padlists */
471static void 496static void
472get_padlist (pTHX_ CV *cv) 497get_padlist (pTHX_ CV *cv)
473{ 498{
501 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);
502 527
503 av = (AV *)mg->mg_obj; 528 av = (AV *)mg->mg_obj;
504 529
505 if (expect_false (AvFILLp (av) >= AvMAX (av))) 530 if (expect_false (AvFILLp (av) >= AvMAX (av)))
506 av_extend (av, AvMAX (av) + 1); 531 av_extend (av, AvFILLp (av) + 1);
507 532
508 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 533 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
509} 534}
510 535
511/** load & save, init *******************************************************/ 536/** load & save, init *******************************************************/
537
538static void
539on_enterleave_call (pTHX_ SV *cb);
512 540
513static void 541static void
514load_perl (pTHX_ Coro__State c) 542load_perl (pTHX_ Coro__State c)
515{ 543{
516 perl_slots *slot = c->slot; 544 perl_slots *slot = c->slot;
517 c->slot = 0; 545 c->slot = 0;
518 546
519 PL_mainstack = c->mainstack; 547 PL_mainstack = c->mainstack;
520 548
521 GvSV (PL_defgv) = slot->defsv; 549 GvSV (PL_defgv) = slot->defsv;
522 GvAV (PL_defgv) = slot->defav; 550 GvAV (PL_defgv) = slot->defav;
523 GvSV (PL_errgv) = slot->errsv; 551 GvSV (PL_errgv) = slot->errsv;
524 GvSV (irsgv) = slot->irsgv; 552 GvSV (irsgv) = slot->irsgv;
553 GvHV (PL_hintgv) = slot->hinthv;
525 554
526 #define VAR(name,type) PL_ ## name = slot->name; 555 #define VAR(name,type) PL_ ## name = slot->name;
527 # include "state.h" 556 # include "state.h"
528 #undef VAR 557 #undef VAR
529 558
543 PUTBACK; 572 PUTBACK;
544 } 573 }
545 574
546 slf_frame = c->slf_frame; 575 slf_frame = c->slf_frame;
547 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 }
548} 585}
549 586
550static void 587static void
551save_perl (pTHX_ Coro__State c) 588save_perl (pTHX_ Coro__State c)
552{ 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
553 c->except = CORO_THROW; 598 c->except = CORO_THROW;
554 c->slf_frame = slf_frame; 599 c->slf_frame = slf_frame;
555 600
556 { 601 {
557 dSP; 602 dSP;
570 { 615 {
571 while (expect_true (cxix >= 0)) 616 while (expect_true (cxix >= 0))
572 { 617 {
573 PERL_CONTEXT *cx = &ccstk[cxix--]; 618 PERL_CONTEXT *cx = &ccstk[cxix--];
574 619
575 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))
576 { 621 {
577 CV *cv = cx->blk_sub.cv; 622 CV *cv = cx->blk_sub.cv;
578 623
579 if (expect_true (CvDEPTH (cv))) 624 if (expect_true (CvDEPTH (cv)))
580 { 625 {
615 c->mainstack = PL_mainstack; 660 c->mainstack = PL_mainstack;
616 661
617 { 662 {
618 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 663 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
619 664
620 slot->defav = GvAV (PL_defgv); 665 slot->defav = GvAV (PL_defgv);
621 slot->defsv = DEFSV; 666 slot->defsv = DEFSV;
622 slot->errsv = ERRSV; 667 slot->errsv = ERRSV;
623 slot->irsgv = GvSV (irsgv); 668 slot->irsgv = GvSV (irsgv);
669 slot->hinthv = GvHV (PL_hintgv);
624 670
625 #define VAR(name,type) slot->name = PL_ ## name; 671 #define VAR(name,type) slot->name = PL_ ## name;
626 # include "state.h" 672 # include "state.h"
627 #undef VAR 673 #undef VAR
628 } 674 }
755#endif 801#endif
756 802
757/* 803/*
758 * This overrides the default magic get method of %SIG elements. 804 * This overrides the default magic get method of %SIG elements.
759 * 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
760 * 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
761 * readback here. 807 * readback here.
762 * We only do this when the hook is != 0, as they are often set to 0 temporarily,
763 * not expecting this to actually change the hook. This is a potential problem
764 * when a schedule happens then, but we ignore this.
765 */ 808 */
766static int 809static int
767coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) 810coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
768{ 811{
769 const char *s = MgPV_nolen_const (mg); 812 const char *s = MgPV_nolen_const (mg);
822 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 865 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
823 866
824 if (svp) 867 if (svp)
825 { 868 {
826 SV *old = *svp; 869 SV *old = *svp;
827 *svp = newSVsv (sv); 870 *svp = SvOK (sv) ? newSVsv (sv) : 0;
828 SvREFCNT_dec (old); 871 SvREFCNT_dec (old);
829 return 0; 872 return 0;
830 } 873 }
831 } 874 }
832 875
864 907
865 PL_runops = RUNOPS_DEFAULT; 908 PL_runops = RUNOPS_DEFAULT;
866 PL_curcop = &PL_compiling; 909 PL_curcop = &PL_compiling;
867 PL_in_eval = EVAL_NULL; 910 PL_in_eval = EVAL_NULL;
868 PL_comppad = 0; 911 PL_comppad = 0;
912 PL_comppad_name = 0;
913 PL_comppad_name_fill = 0;
914 PL_comppad_name_floor = 0;
869 PL_curpm = 0; 915 PL_curpm = 0;
870 PL_curpad = 0; 916 PL_curpad = 0;
871 PL_localizing = 0; 917 PL_localizing = 0;
872 PL_dirty = 0; 918 PL_dirty = 0;
873 PL_restartop = 0; 919 PL_restartop = 0;
874#if PERL_VERSION_ATLEAST (5,10,0) 920#if PERL_VERSION_ATLEAST (5,10,0)
875 PL_parser = 0; 921 PL_parser = 0;
876#endif 922#endif
923 PL_hints = 0;
877 924
878 /* recreate the die/warn hooks */ 925 /* recreate the die/warn hooks */
879 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 );
880 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);
881 928
882 GvSV (PL_defgv) = newSV (0); 929 GvSV (PL_defgv) = newSV (0);
883 GvAV (PL_defgv) = coro->args; coro->args = 0; 930 GvAV (PL_defgv) = coro->args; coro->args = 0;
884 GvSV (PL_errgv) = newSV (0); 931 GvSV (PL_errgv) = newSV (0);
885 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;
886 PL_rs = newSVsv (GvSV (irsgv)); 934 PL_rs = newSVsv (GvSV (irsgv));
887 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 935 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
888 936
889 { 937 {
890 dSP; 938 dSP;
919 /* copy throw, in case it was set before coro_setup */ 967 /* copy throw, in case it was set before coro_setup */
920 CORO_THROW = coro->except; 968 CORO_THROW = coro->except;
921} 969}
922 970
923static void 971static void
924coro_destruct (pTHX_ struct coro *coro) 972coro_unwind_stacks (pTHX)
925{ 973{
926 if (!IN_DESTRUCT) 974 if (!IN_DESTRUCT)
927 { 975 {
928 /* restore all saved variables and stuff */ 976 /* restore all saved variables and stuff */
929 LEAVE_SCOPE (0); 977 LEAVE_SCOPE (0);
937 POPSTACK_TO (PL_mainstack); 985 POPSTACK_TO (PL_mainstack);
938 986
939 /* unwind main stack */ 987 /* unwind main stack */
940 dounwind (-1); 988 dounwind (-1);
941 } 989 }
990}
942 991
943 SvREFCNT_dec (GvSV (PL_defgv)); 992static void
944 SvREFCNT_dec (GvAV (PL_defgv)); 993coro_destruct_perl (pTHX_ struct coro *coro)
945 SvREFCNT_dec (GvSV (PL_errgv)); 994{
946 SvREFCNT_dec (PL_defoutgv); 995 SV *svf [9];
947 SvREFCNT_dec (PL_rs);
948 SvREFCNT_dec (GvSV (irsgv));
949 996
950 SvREFCNT_dec (PL_diehook);
951 SvREFCNT_dec (PL_warnhook);
952 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
953 SvREFCNT_dec (coro->saved_deffh); 1029 SvREFCNT_dec (coro->saved_deffh);
954 SvREFCNT_dec (coro->rouse_cb); 1030 SvREFCNT_dec (coro->rouse_cb);
955 SvREFCNT_dec (coro->invoke_cb); 1031 SvREFCNT_dec (coro->invoke_cb);
956 SvREFCNT_dec (coro->invoke_av); 1032 SvREFCNT_dec (coro->invoke_av);
957 1033 }
958 coro_destruct_stacks (aTHX);
959} 1034}
960 1035
961INLINE void 1036INLINE void
962free_coro_mortal (pTHX) 1037free_coro_mortal (pTHX)
963{ 1038{
971static int 1046static int
972runops_trace (pTHX) 1047runops_trace (pTHX)
973{ 1048{
974 COP *oldcop = 0; 1049 COP *oldcop = 0;
975 int oldcxix = -2; 1050 int oldcxix = -2;
976 struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */
977 coro_cctx *cctx = coro->cctx;
978 1051
979 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 1052 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
980 { 1053 {
981 PERL_ASYNC_CHECK (); 1054 PERL_ASYNC_CHECK ();
982 1055
983 if (cctx->flags & CC_TRACE_ALL) 1056 if (cctx_current->flags & CC_TRACE_ALL)
984 { 1057 {
985 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)
986 { 1059 {
987 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1060 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
988 SV **bot, **top; 1061 SV **bot, **top;
989 AV *av = newAV (); /* return values */ 1062 AV *av = newAV (); /* return values */
990 SV **cb; 1063 SV **cb;
1027 1100
1028 if (PL_curcop != &PL_compiling) 1101 if (PL_curcop != &PL_compiling)
1029 { 1102 {
1030 SV **cb; 1103 SV **cb;
1031 1104
1032 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) 1105 if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB)
1033 { 1106 {
1034 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1107 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1035 1108
1036 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 1109 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
1037 { 1110 {
1060 } 1133 }
1061 1134
1062 oldcxix = cxstack_ix; 1135 oldcxix = cxstack_ix;
1063 } 1136 }
1064 1137
1065 if (cctx->flags & CC_TRACE_LINE) 1138 if (cctx_current->flags & CC_TRACE_LINE)
1066 { 1139 {
1067 dSP; 1140 dSP;
1068 1141
1069 PL_runops = RUNOPS_DEFAULT; 1142 PL_runops = RUNOPS_DEFAULT;
1070 ENTER; 1143 ENTER;
1170 /* 1243 /*
1171 * 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
1172 * 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)
1173 * 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
1174 * bootstrap-time "top" top_env, as we cannot restore the "main" 1247 * bootstrap-time "top" top_env, as we cannot restore the "main"
1175 * 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.
1176 */ 1251 */
1177 PL_top_env = main_top_env; 1252 PL_top_env = main_top_env;
1178 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 */
1179 } 1254 }
1180} 1255}
1257cctx_destroy (coro_cctx *cctx) 1332cctx_destroy (coro_cctx *cctx)
1258{ 1333{
1259 if (!cctx) 1334 if (!cctx)
1260 return; 1335 return;
1261 1336
1262 assert (cctx != cctx_current);//D temporary 1337 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary?
1263 1338
1264 --cctx_count; 1339 --cctx_count;
1265 coro_destroy (&cctx->cctx); 1340 coro_destroy (&cctx->cctx);
1266 1341
1267 /* coro_transfer creates new, empty cctx's */ 1342 /* coro_transfer creates new, empty cctx's */
1331 /* TODO: throwing up here is considered harmful */ 1406 /* TODO: throwing up here is considered harmful */
1332 1407
1333 if (expect_true (prev != next)) 1408 if (expect_true (prev != next))
1334 { 1409 {
1335 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1410 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1336 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,");
1337 1412
1338 if (expect_false (next->flags & CF_RUNNING))
1339 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1340
1341 if (expect_false (next->flags & CF_DESTROYED)) 1413 if (expect_false (next->flags & (CF_RUNNING | CF_DESTROYED | CF_SUSPENDED)))
1342 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,");
1343 1415
1344#if !PERL_VERSION_ATLEAST (5,10,0) 1416#if !PERL_VERSION_ATLEAST (5,10,0)
1345 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1417 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1346 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,");
1347#endif 1419#endif
1395 )) 1467 ))
1396 { 1468 {
1397 /* 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 */
1398 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te)); 1470 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te));
1399 1471
1400 /* 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. */
1401 /* 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 */
1402 if (expect_false (CCTX_EXPIRED (cctx_current))) 1474 if (expect_false (CCTX_EXPIRED (cctx_current)))
1403 if (!next->cctx) 1475 if (expect_true (!next->cctx))
1404 next->cctx = cctx_get (aTHX); 1476 next->cctx = cctx_get (aTHX);
1405 1477
1406 cctx_put (cctx_current); 1478 cctx_put (cctx_current);
1407 assert (!prev->cctx);//D temporary
1408 } 1479 }
1409 else 1480 else
1410 prev->cctx = cctx_current; 1481 prev->cctx = cctx_current;
1411 1482
1412 ++next->usecount; 1483 ++next->usecount;
1436coro_state_destroy (pTHX_ struct coro *coro) 1507coro_state_destroy (pTHX_ struct coro *coro)
1437{ 1508{
1438 if (coro->flags & CF_DESTROYED) 1509 if (coro->flags & CF_DESTROYED)
1439 return 0; 1510 return 0;
1440 1511
1441 if (coro->on_destroy) 1512 if (coro->on_destroy && !PL_dirty)
1442 coro->on_destroy (aTHX_ coro); 1513 coro->on_destroy (aTHX_ coro);
1443 1514
1444 coro->flags |= CF_DESTROYED; 1515 coro->flags |= CF_DESTROYED;
1445 1516
1446 if (coro->flags & CF_READY) 1517 if (coro->flags & CF_READY)
1450 --coro_nready; 1521 --coro_nready;
1451 } 1522 }
1452 else 1523 else
1453 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 */
1454 1525
1455 if (coro->mainstack && coro->mainstack != main_mainstack) 1526 if (coro->mainstack
1456 { 1527 && coro->mainstack != main_mainstack
1457 struct coro temp; 1528 && coro->slot
1458 1529 && !PL_dirty)
1459 assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING)));
1460
1461 save_perl (aTHX_ &temp);
1462 load_perl (aTHX_ coro);
1463
1464 coro_destruct (aTHX_ coro); 1530 coro_destruct_perl (aTHX_ coro);
1465
1466 load_perl (aTHX_ &temp);
1467
1468 coro->slot = 0;
1469 }
1470 1531
1471 cctx_destroy (coro->cctx); 1532 cctx_destroy (coro->cctx);
1472 SvREFCNT_dec (coro->startcv); 1533 SvREFCNT_dec (coro->startcv);
1473 SvREFCNT_dec (coro->args); 1534 SvREFCNT_dec (coro->args);
1474 SvREFCNT_dec (CORO_THROW); 1535 SvREFCNT_dec (CORO_THROW);
1561/** Coro ********************************************************************/ 1622/** Coro ********************************************************************/
1562 1623
1563INLINE void 1624INLINE void
1564coro_enq (pTHX_ struct coro *coro) 1625coro_enq (pTHX_ struct coro *coro)
1565{ 1626{
1566 av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv)); 1627 struct coro **ready = coro_ready [coro->prio - PRIO_MIN];
1567}
1568 1628
1569INLINE 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 *
1570coro_deq (pTHX) 1637coro_deq (pTHX)
1571{ 1638{
1572 int prio; 1639 int prio;
1573 1640
1574 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) 1641 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; )
1575 if (AvFILLp (coro_ready [prio]) >= 0) 1642 {
1576 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 }
1577 1652
1578 return 0; 1653 return 0;
1579} 1654}
1580 1655
1581static int 1656static int
1583{ 1658{
1584 struct coro *coro; 1659 struct coro *coro;
1585 SV *sv_hook; 1660 SV *sv_hook;
1586 void (*xs_hook)(void); 1661 void (*xs_hook)(void);
1587 1662
1588 if (SvROK (coro_sv))
1589 coro_sv = SvRV (coro_sv);
1590
1591 coro = SvSTATE (coro_sv); 1663 coro = SvSTATE (coro_sv);
1592 1664
1593 if (coro->flags & CF_READY) 1665 if (coro->flags & CF_READY)
1594 return 0; 1666 return 0;
1595 1667
1648static void 1720static void
1649prepare_schedule (pTHX_ struct coro_transfer_args *ta) 1721prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1650{ 1722{
1651 for (;;) 1723 for (;;)
1652 { 1724 {
1653 SV *next_sv = coro_deq (aTHX); 1725 struct coro *next = coro_deq (aTHX);
1654 1726
1655 if (expect_true (next_sv)) 1727 if (expect_true (next))
1656 { 1728 {
1657 struct coro *next = SvSTATE_hv (next_sv);
1658
1659 /* cannot transfer to destroyed coros, skip and look for next */ 1729 /* cannot transfer to destroyed coros, skip and look for next */
1660 if (expect_false (next->flags & CF_DESTROYED)) 1730 if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED)))
1661 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 */
1662 else 1732 else
1663 { 1733 {
1664 next->flags &= ~CF_READY; 1734 next->flags &= ~CF_READY;
1665 --coro_nready; 1735 --coro_nready;
1666 1736
1669 } 1739 }
1670 } 1740 }
1671 else 1741 else
1672 { 1742 {
1673 /* 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 {
1674 dSP; 1753 dSP;
1675 1754
1676 ENTER; 1755 ENTER;
1677 SAVETMPS; 1756 SAVETMPS;
1678 1757
1679 PUSHMARK (SP); 1758 PUSHMARK (SP);
1680 PUTBACK; 1759 PUTBACK;
1681 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); 1760 call_sv (sv_idle, G_VOID | G_DISCARD);
1682 1761
1683 FREETMPS; 1762 FREETMPS;
1684 LEAVE; 1763 LEAVE;
1764 }
1685 } 1765 }
1686 } 1766 }
1687} 1767}
1688 1768
1689INLINE void 1769INLINE void
1760static void 1840static void
1761api_trace (pTHX_ SV *coro_sv, int flags) 1841api_trace (pTHX_ SV *coro_sv, int flags)
1762{ 1842{
1763 struct coro *coro = SvSTATE (coro_sv); 1843 struct coro *coro = SvSTATE (coro_sv);
1764 1844
1845 if (coro->flags & CF_RUNNING)
1846 croak ("cannot enable tracing on a running coroutine, caught");
1847
1765 if (flags & CC_TRACE) 1848 if (flags & CC_TRACE)
1766 { 1849 {
1767 if (!coro->cctx) 1850 if (!coro->cctx)
1768 coro->cctx = cctx_new_run (); 1851 coro->cctx = cctx_new_run ();
1769 else if (!(coro->cctx->flags & CC_TRACE)) 1852 else if (!(coro->cctx->flags & CC_TRACE))
1770 croak ("cannot enable tracing on coroutine with custom stack,"); 1853 croak ("cannot enable tracing on coroutine with custom stack, caught");
1771 1854
1772 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1855 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1773 } 1856 }
1774 else if (coro->cctx && coro->cctx->flags & CC_TRACE) 1857 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1775 { 1858 {
1831 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 */
1832 api_ready (aTHX_ sv_manager); 1915 api_ready (aTHX_ sv_manager);
1833 1916
1834 frame->prepare = prepare_schedule; 1917 frame->prepare = prepare_schedule;
1835 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);*/
1836} 1923}
1837 1924
1838/*****************************************************************************/ 1925/*****************************************************************************/
1839/* async pool handler */ 1926/* async pool handler */
1840 1927
1923 SV *data = (SV *)GENSUB_ARG; 2010 SV *data = (SV *)GENSUB_ARG;
1924 2011
1925 if (SvTYPE (SvRV (data)) != SVt_PVAV) 2012 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1926 { 2013 {
1927 /* first call, set args */ 2014 /* first call, set args */
2015 SV *coro = SvRV (data);
1928 AV *av = newAV (); 2016 AV *av = newAV ();
1929 SV *coro = SvRV (data);
1930 2017
1931 SvRV_set (data, (SV *)av); 2018 SvRV_set (data, (SV *)av);
1932 api_ready (aTHX_ coro);
1933 SvREFCNT_dec (coro);
1934 2019
1935 /* better take a full copy of the arguments */ 2020 /* better take a full copy of the arguments */
1936 while (items--) 2021 while (items--)
1937 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);
1938 } 2026 }
1939 2027
1940 XSRETURN_EMPTY; 2028 XSRETURN_EMPTY;
1941} 2029}
1942 2030
1959 2047
1960 EXTEND (SP, AvFILLp (av) + 1); 2048 EXTEND (SP, AvFILLp (av) + 1);
1961 for (i = 0; i <= AvFILLp (av); ++i) 2049 for (i = 0; i <= AvFILLp (av); ++i)
1962 PUSHs (sv_2mortal (AvARRAY (av)[i])); 2050 PUSHs (sv_2mortal (AvARRAY (av)[i]));
1963 2051
1964 /* 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 */
1965 AvFILLp (av) = -1; 2053 AvFILLp (av) = -1;
1966 av_undef (av); 2054 av_undef (av);
1967 2055
1968 PUTBACK; 2056 PUTBACK;
1969 } 2057 }
2259 2347
2260 PL_op->op_ppaddr = pp_slf; 2348 PL_op->op_ppaddr = pp_slf;
2261 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ 2349 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2262 2350
2263 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));
2264} 2399}
2265 2400
2266/*****************************************************************************/ 2401/*****************************************************************************/
2267/* PerlIO::cede */ 2402/* PerlIO::cede */
2268 2403
2342/* Coro::Semaphore & Coro::Signal */ 2477/* Coro::Semaphore & Coro::Signal */
2343 2478
2344static SV * 2479static SV *
2345coro_waitarray_new (pTHX_ int count) 2480coro_waitarray_new (pTHX_ int count)
2346{ 2481{
2347 /* 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 */
2348 AV *av = newAV (); 2483 AV *av = newAV ();
2349 SV **ary; 2484 SV **ary;
2350 2485
2351 /* unfortunately, building manually saves memory */ 2486 /* unfortunately, building manually saves memory */
2352 Newx (ary, 2, SV *); 2487 Newx (ary, 2, SV *);
2353 AvALLOC (av) = ary; 2488 AvALLOC (av) = ary;
2489#if PERL_VERSION_ATLEAST (5,10,0)
2354 /*AvARRAY (av) = ary;*/ 2490 AvARRAY (av) = ary;
2491#else
2355 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
2356 AvMAX (av) = 1; 2496 AvMAX (av) = 1;
2357 AvFILLp (av) = 0; 2497 AvFILLp (av) = 0;
2358 ary [0] = newSViv (count); 2498 ary [0] = newSViv (count);
2359 2499
2360 return newRV_noinc ((SV *)av); 2500 return newRV_noinc ((SV *)av);
2491 { 2631 {
2492 /* callback form */ 2632 /* callback form */
2493 AV *av = (AV *)SvRV (arg [0]); 2633 AV *av = (AV *)SvRV (arg [0]);
2494 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); 2634 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]);
2495 2635
2496 av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); 2636 av_push (av, SvREFCNT_inc_NN (cb_cv));
2497 2637
2498 if (SvIVX (AvARRAY (av)[0]) > 0) 2638 if (SvIVX (AvARRAY (av)[0]) > 0)
2499 coro_semaphore_adjust (aTHX_ av, 0); 2639 coro_semaphore_adjust (aTHX_ av, 0);
2500 2640
2501 frame->prepare = prepare_nop; 2641 frame->prepare = prepare_nop;
2525 AvARRAY (av)[0] = AvARRAY (av)[1]; 2665 AvARRAY (av)[0] = AvARRAY (av)[1];
2526 AvARRAY (av)[1] = cb; 2666 AvARRAY (av)[1] = cb;
2527 2667
2528 cb = av_shift (av); 2668 cb = av_shift (av);
2529 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 {
2530 api_ready (aTHX_ cb); 2680 api_ready (aTHX_ cb);
2531 sv_setiv (cb, 0); /* signal waiter */ 2681 sv_setiv (cb, 0); /* signal waiter */
2682 }
2683
2532 SvREFCNT_dec (cb); 2684 SvREFCNT_dec (cb);
2533 2685
2534 --count; 2686 --count;
2535 } 2687 }
2536} 2688}
2545static void 2697static void
2546slf_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)
2547{ 2699{
2548 AV *av = (AV *)SvRV (arg [0]); 2700 AV *av = (AV *)SvRV (arg [0]);
2549 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
2550 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]))
2551 { 2714 {
2552 SvIVX (AvARRAY (av)[0]) = 0; 2715 SvIVX (AvARRAY (av)[0]) = 0;
2553 frame->prepare = prepare_nop; 2716 frame->prepare = prepare_nop;
2554 frame->check = slf_check_nop; 2717 frame->check = slf_check_nop;
2555 } 2718 }
2556 else 2719 else
2557 { 2720 {
2558 SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */ 2721 SV *waiter = newSVsv (coro_current); /* owned by signal av */
2559 2722
2560 av_push (av, waiter); 2723 av_push (av, waiter);
2561 2724
2562 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 */
2563 frame->prepare = prepare_schedule; 2726 frame->prepare = prepare_schedule;
2889SV * 3052SV *
2890clone (Coro::State coro) 3053clone (Coro::State coro)
2891 CODE: 3054 CODE:
2892{ 3055{
2893#if CORO_CLONE 3056#if CORO_CLONE
2894 struct coro *ncoro = coro_clone (coro); 3057 struct coro *ncoro = coro_clone (aTHX_ coro);
2895 MAGIC *mg; 3058 MAGIC *mg;
2896 /* TODO: too much duplication */ 3059 /* TODO: too much duplication */
2897 ncoro->hv = newHV (); 3060 ncoro->hv = newHV ();
2898 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); 3061 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
2899 mg->mg_flags |= MGf_DUP; 3062 mg->mg_flags |= MGf_DUP;
2961 eval = 1 3124 eval = 1
2962 CODE: 3125 CODE:
2963{ 3126{
2964 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3127 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
2965 { 3128 {
2966 struct coro temp; 3129 struct coro *current = SvSTATE_current;
2967 3130
2968 if (!(coro->flags & CF_RUNNING)) 3131 if (current != coro)
2969 { 3132 {
2970 PUTBACK; 3133 PUTBACK;
2971 save_perl (aTHX_ &temp); 3134 save_perl (aTHX_ current);
2972 load_perl (aTHX_ coro); 3135 load_perl (aTHX_ coro);
3136 SPAGAIN;
2973 } 3137 }
2974 3138
2975 {
2976 dSP;
2977 ENTER;
2978 SAVETMPS;
2979 PUTBACK;
2980 PUSHSTACK; 3139 PUSHSTACK;
3140
2981 PUSHMARK (SP); 3141 PUSHMARK (SP);
3142 PUTBACK;
2982 3143
2983 if (ix) 3144 if (ix)
2984 eval_sv (coderef, 0); 3145 eval_sv (coderef, 0);
2985 else 3146 else
2986 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3147 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
2987 3148
2988 POPSTACK; 3149 POPSTACK;
2989 SPAGAIN; 3150 SPAGAIN;
2990 FREETMPS;
2991 LEAVE;
2992 PUTBACK;
2993 }
2994 3151
2995 if (!(coro->flags & CF_RUNNING)) 3152 if (current != coro)
2996 { 3153 {
3154 PUTBACK;
2997 save_perl (aTHX_ coro); 3155 save_perl (aTHX_ coro);
2998 load_perl (aTHX_ &temp); 3156 load_perl (aTHX_ current);
2999 SPAGAIN; 3157 SPAGAIN;
3000 } 3158 }
3001 } 3159 }
3002} 3160}
3003 3161
3007 ALIAS: 3165 ALIAS:
3008 is_ready = CF_READY 3166 is_ready = CF_READY
3009 is_running = CF_RUNNING 3167 is_running = CF_RUNNING
3010 is_new = CF_NEW 3168 is_new = CF_NEW
3011 is_destroyed = CF_DESTROYED 3169 is_destroyed = CF_DESTROYED
3170 is_suspended = CF_SUSPENDED
3012 CODE: 3171 CODE:
3013 RETVAL = boolSV (coro->flags & ix); 3172 RETVAL = boolSV (coro->flags & ix);
3014 OUTPUT: 3173 OUTPUT:
3015 RETVAL 3174 RETVAL
3016 3175
3032 3191
3033SV * 3192SV *
3034has_cctx (Coro::State coro) 3193has_cctx (Coro::State coro)
3035 PROTOTYPE: $ 3194 PROTOTYPE: $
3036 CODE: 3195 CODE:
3037 RETVAL = boolSV (!!coro->cctx); 3196 /* maybe manage the running flag differently */
3197 RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING));
3038 OUTPUT: 3198 OUTPUT:
3039 RETVAL 3199 RETVAL
3040 3200
3041int 3201int
3042is_traced (Coro::State coro) 3202is_traced (Coro::State coro)
3062 3222
3063void 3223void
3064force_cctx () 3224force_cctx ()
3065 PROTOTYPE: 3225 PROTOTYPE:
3066 CODE: 3226 CODE:
3067 SvSTATE_current->cctx->idle_sp = 0; 3227 cctx_current->idle_sp = 0;
3068 3228
3069void 3229void
3070swap_defsv (Coro::State self) 3230swap_defsv (Coro::State self)
3071 PROTOTYPE: $ 3231 PROTOTYPE: $
3072 ALIAS: 3232 ALIAS:
3080 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 3240 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
3081 3241
3082 SV *tmp = *src; *src = *dst; *dst = tmp; 3242 SV *tmp = *src; *src = *dst; *dst = tmp;
3083 } 3243 }
3084 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
3085 3251
3086MODULE = Coro::State PACKAGE = Coro 3252MODULE = Coro::State PACKAGE = Coro
3087 3253
3088BOOT: 3254BOOT:
3089{ 3255{
3090 int i;
3091
3092 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3256 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3093 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3257 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3094 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3258 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3095 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3259 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3096 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);
3097 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 3261 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3098 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3262 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3099 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);
3100 3265
3101 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);
3102 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3267 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3103 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);
3104 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);
3109 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 3274 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
3110 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 3275 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
3111 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 3276 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
3112 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 3277 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
3113 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 3278 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
3114
3115 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
3116 coro_ready[i] = newAV ();
3117 3279
3118 { 3280 {
3119 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3281 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
3120 3282
3121 coroapi.schedule = api_schedule; 3283 coroapi.schedule = api_schedule;
3162cede_notself (...) 3324cede_notself (...)
3163 CODE: 3325 CODE:
3164 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3326 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3165 3327
3166void 3328void
3167_cancel (Coro::State self)
3168 CODE:
3169 coro_state_destroy (aTHX_ self);
3170 coro_call_on_destroy (aTHX_ self);
3171
3172void
3173_set_current (SV *current) 3329_set_current (SV *current)
3174 PROTOTYPE: $ 3330 PROTOTYPE: $
3175 CODE: 3331 CODE:
3176 SvREFCNT_dec (SvRV (coro_current)); 3332 SvREFCNT_dec (SvRV (coro_current));
3177 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); 3333 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
3219 PROTOTYPE: 3375 PROTOTYPE:
3220 CODE: 3376 CODE:
3221 RETVAL = coro_nready; 3377 RETVAL = coro_nready;
3222 OUTPUT: 3378 OUTPUT:
3223 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;
3224 3392
3225void 3393void
3226_pool_handler (...) 3394_pool_handler (...)
3227 CODE: 3395 CODE:
3228 CORO_EXECUTE_SLF_XS (slf_init_pool_handler); 3396 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
3283rouse_wait (...) 3451rouse_wait (...)
3284 PROTOTYPE: ;$ 3452 PROTOTYPE: ;$
3285 PPCODE: 3453 PPCODE:
3286 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3454 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3287 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
3288 3481
3289MODULE = Coro::State PACKAGE = PerlIO::cede 3482MODULE = Coro::State PACKAGE = PerlIO::cede
3290 3483
3291BOOT: 3484BOOT:
3292 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3485 PerlIO_define_layer (aTHX_ &PerlIO_cede);
3302 GvSTASH (CvGV (cv)) 3495 GvSTASH (CvGV (cv))
3303 ); 3496 );
3304 OUTPUT: 3497 OUTPUT:
3305 RETVAL 3498 RETVAL
3306 3499
3307# helper for Coro::Channel 3500# helper for Coro::Channel and others
3308SV * 3501SV *
3309_alloc (int count) 3502_alloc (int count)
3310 CODE: 3503 CODE:
3311 RETVAL = coro_waitarray_new (aTHX_ count); 3504 RETVAL = coro_waitarray_new (aTHX_ count);
3312 OUTPUT: 3505 OUTPUT:
3370 for (i = 1; i <= wcount; ++i) 3563 for (i = 1; i <= wcount; ++i)
3371 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); 3564 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
3372 } 3565 }
3373} 3566}
3374 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
3375MODULE = Coro::State PACKAGE = Coro::Signal 3584MODULE = Coro::State PACKAGE = Coro::Signal
3376 3585
3377SV * 3586SV *
3378new (SV *klass) 3587new (SV *klass)
3379 CODE: 3588 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines