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.326 by root, Mon Nov 24 04:56:38 2008 UTC vs.
Revision 1.357 by root, Sat Jun 27 14:09:28 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
238enum { 238enum {
239 CF_RUNNING = 0x0001, /* coroutine is running */ 239 CF_RUNNING = 0x0001, /* coroutine is running */
240 CF_READY = 0x0002, /* coroutine is ready */ 240 CF_READY = 0x0002, /* coroutine is ready */
241 CF_NEW = 0x0004, /* has never been switched to */ 241 CF_NEW = 0x0004, /* has never been switched to */
242 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 */
243}; 244};
244 245
245/* 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 */
246typedef struct 247typedef struct
247{ 248{
248 SV *defsv; 249 SV *defsv;
249 AV *defav; 250 AV *defav;
250 SV *errsv; 251 SV *errsv;
251 SV *irsgv; 252 SV *irsgv;
253 HV *hinthv;
252#define VAR(name,type) type name; 254#define VAR(name,type) type name;
253# include "state.h" 255# include "state.h"
254#undef VAR 256#undef VAR
255} perl_slots; 257} perl_slots;
256 258
258 260
259/* this is a structure representing a perl-level coroutine */ 261/* this is a structure representing a perl-level coroutine */
260struct coro { 262struct coro {
261 /* the C coroutine allocated to this perl coroutine, if any */ 263 /* the C coroutine allocated to this perl coroutine, if any */
262 coro_cctx *cctx; 264 coro_cctx *cctx;
265
266 /* ready queue */
267 struct coro *next_ready;
263 268
264 /* state data */ 269 /* state data */
265 struct CoroSLF slf_frame; /* saved slf frame */ 270 struct CoroSLF slf_frame; /* saved slf frame */
266 AV *mainstack; 271 AV *mainstack;
267 perl_slots *slot; /* basically the saved sp */ 272 perl_slots *slot; /* basically the saved sp */
284 /* async_pool */ 289 /* async_pool */
285 SV *saved_deffh; 290 SV *saved_deffh;
286 SV *invoke_cb; 291 SV *invoke_cb;
287 AV *invoke_av; 292 AV *invoke_av;
288 293
294 /* on_enter/on_leave */
295 AV *on_enter;
296 AV *on_leave;
297
289 /* linked list */ 298 /* linked list */
290 struct coro *next, *prev; 299 struct coro *next, *prev;
291}; 300};
292 301
293typedef struct coro *Coro__State; 302typedef struct coro *Coro__State;
308#define PRIO_MIN -4 317#define PRIO_MIN -4
309 318
310/* for Coro.pm */ 319/* for Coro.pm */
311static SV *coro_current; 320static SV *coro_current;
312static SV *coro_readyhook; 321static SV *coro_readyhook;
313static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; 322static struct coro *coro_ready [PRIO_MAX - PRIO_MIN + 1][2]; /* head|tail */
314static CV *cv_coro_run, *cv_coro_terminate; 323static CV *cv_coro_run, *cv_coro_terminate;
315static struct coro *coro_first; 324static struct coro *coro_first;
316#define coro_nready coroapi.nready 325#define coro_nready coroapi.nready
317 326
318/** lowlevel stuff **********************************************************/ 327/** lowlevel stuff **********************************************************/
351INLINE CV * 360INLINE CV *
352coro_sv_2cv (pTHX_ SV *sv) 361coro_sv_2cv (pTHX_ SV *sv)
353{ 362{
354 HV *st; 363 HV *st;
355 GV *gvp; 364 GV *gvp;
356 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;
357} 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 */
358 424
359static AV * 425static AV *
360coro_derive_padlist (pTHX_ CV *cv) 426coro_derive_padlist (pTHX_ CV *cv)
361{ 427{
362 AV *padlist = CvPADLIST (cv); 428 AV *padlist = CvPADLIST (cv);
370 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 436 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
371#endif 437#endif
372 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 438 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
373 --AvFILLp (padlist); 439 --AvFILLp (padlist);
374 440
375 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); 441 av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0]));
376 av_store (newpadlist, 1, (SV *)newpad); 442 av_store (newpadlist, 1, (SV *)newpad);
377 443
378 return newpadlist; 444 return newpadlist;
379} 445}
380 446
381static void 447static void
382free_padlist (pTHX_ AV *padlist) 448free_padlist (pTHX_ AV *padlist)
383{ 449{
384 /* may be during global destruction */ 450 /* may be during global destruction */
385 if (SvREFCNT (padlist)) 451 if (!IN_DESTRUCT)
386 { 452 {
387 I32 i = AvFILLp (padlist); 453 I32 i = AvFILLp (padlist);
388 while (i >= 0) 454
455 while (i > 0) /* special-case index 0 */
389 { 456 {
390 SV **svp = av_fetch (padlist, i--, FALSE); 457 /* we try to be extra-careful here */
391 if (svp) 458 AV *av = (AV *)AvARRAY (padlist)[i--];
392 { 459 I32 j = AvFILLp (av);
393 SV *sv; 460
394 while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) 461 while (j >= 0)
462 SvREFCNT_dec (AvARRAY (av)[j--]);
463
464 AvFILLp (av) = -1;
395 SvREFCNT_dec (sv); 465 SvREFCNT_dec (av);
396
397 SvREFCNT_dec (*svp);
398 }
399 } 466 }
400 467
468 SvREFCNT_dec (AvARRAY (padlist)[0]);
469
470 AvFILLp (padlist) = -1;
401 SvREFCNT_dec ((SV*)padlist); 471 SvREFCNT_dec ((SV*)padlist);
402 } 472 }
403} 473}
404 474
405static int 475static int
414 484
415 SvREFCNT_dec (av); /* sv_magicext increased the refcount */ 485 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
416 486
417 return 0; 487 return 0;
418} 488}
419
420#define CORO_MAGIC_type_cv 26
421#define CORO_MAGIC_type_state PERL_MAGIC_ext
422 489
423static MGVTBL coro_cv_vtbl = { 490static MGVTBL coro_cv_vtbl = {
424 0, 0, 0, 0, 491 0, 0, 0, 0,
425 coro_cv_free 492 coro_cv_free
426}; 493};
427
428#define CORO_MAGIC_NN(sv, type) \
429 (expect_true (SvMAGIC (sv)->mg_type == type) \
430 ? SvMAGIC (sv) \
431 : mg_find (sv, type))
432
433#define CORO_MAGIC(sv, type) \
434 (expect_true (SvMAGIC (sv)) \
435 ? CORO_MAGIC_NN (sv, type) \
436 : 0)
437
438#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
439#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
440
441INLINE struct coro *
442SvSTATE_ (pTHX_ SV *coro)
443{
444 HV *stash;
445 MAGIC *mg;
446
447 if (SvROK (coro))
448 coro = SvRV (coro);
449
450 if (expect_false (SvTYPE (coro) != SVt_PVHV))
451 croak ("Coro::State object required");
452
453 stash = SvSTASH (coro);
454 if (expect_false (stash != coro_stash && stash != coro_state_stash))
455 {
456 /* very slow, but rare, check */
457 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
458 croak ("Coro::State object required");
459 }
460
461 mg = CORO_MAGIC_state (coro);
462 return (struct coro *)mg->mg_ptr;
463}
464
465#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
466
467/* faster than SvSTATE, but expects a coroutine hv */
468#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr)
469#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
470 494
471/* the next two functions merely cache the padlists */ 495/* the next two functions merely cache the padlists */
472static void 496static void
473get_padlist (pTHX_ CV *cv) 497get_padlist (pTHX_ CV *cv)
474{ 498{
502 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);
503 527
504 av = (AV *)mg->mg_obj; 528 av = (AV *)mg->mg_obj;
505 529
506 if (expect_false (AvFILLp (av) >= AvMAX (av))) 530 if (expect_false (AvFILLp (av) >= AvMAX (av)))
507 av_extend (av, AvMAX (av) + 1); 531 av_extend (av, AvFILLp (av) + 1);
508 532
509 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 533 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
510} 534}
511 535
512/** load & save, init *******************************************************/ 536/** load & save, init *******************************************************/
537
538static void
539on_enterleave_call (pTHX_ SV *cb);
513 540
514static void 541static void
515load_perl (pTHX_ Coro__State c) 542load_perl (pTHX_ Coro__State c)
516{ 543{
517 perl_slots *slot = c->slot; 544 perl_slots *slot = c->slot;
518 c->slot = 0; 545 c->slot = 0;
519 546
520 PL_mainstack = c->mainstack; 547 PL_mainstack = c->mainstack;
521 548
522 GvSV (PL_defgv) = slot->defsv; 549 GvSV (PL_defgv) = slot->defsv;
523 GvAV (PL_defgv) = slot->defav; 550 GvAV (PL_defgv) = slot->defav;
524 GvSV (PL_errgv) = slot->errsv; 551 GvSV (PL_errgv) = slot->errsv;
525 GvSV (irsgv) = slot->irsgv; 552 GvSV (irsgv) = slot->irsgv;
553 GvHV (PL_hintgv) = slot->hinthv;
526 554
527 #define VAR(name,type) PL_ ## name = slot->name; 555 #define VAR(name,type) PL_ ## name = slot->name;
528 # include "state.h" 556 # include "state.h"
529 #undef VAR 557 #undef VAR
530 558
544 PUTBACK; 572 PUTBACK;
545 } 573 }
546 574
547 slf_frame = c->slf_frame; 575 slf_frame = c->slf_frame;
548 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 }
549} 585}
550 586
551static void 587static void
552save_perl (pTHX_ Coro__State c) 588save_perl (pTHX_ Coro__State c)
553{ 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
554 c->except = CORO_THROW; 598 c->except = CORO_THROW;
555 c->slf_frame = slf_frame; 599 c->slf_frame = slf_frame;
556 600
557 { 601 {
558 dSP; 602 dSP;
571 { 615 {
572 while (expect_true (cxix >= 0)) 616 while (expect_true (cxix >= 0))
573 { 617 {
574 PERL_CONTEXT *cx = &ccstk[cxix--]; 618 PERL_CONTEXT *cx = &ccstk[cxix--];
575 619
576 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))
577 { 621 {
578 CV *cv = cx->blk_sub.cv; 622 CV *cv = cx->blk_sub.cv;
579 623
580 if (expect_true (CvDEPTH (cv))) 624 if (expect_true (CvDEPTH (cv)))
581 { 625 {
616 c->mainstack = PL_mainstack; 660 c->mainstack = PL_mainstack;
617 661
618 { 662 {
619 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 663 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
620 664
621 slot->defav = GvAV (PL_defgv); 665 slot->defav = GvAV (PL_defgv);
622 slot->defsv = DEFSV; 666 slot->defsv = DEFSV;
623 slot->errsv = ERRSV; 667 slot->errsv = ERRSV;
624 slot->irsgv = GvSV (irsgv); 668 slot->irsgv = GvSV (irsgv);
669 slot->hinthv = GvHV (PL_hintgv);
625 670
626 #define VAR(name,type) slot->name = PL_ ## name; 671 #define VAR(name,type) slot->name = PL_ ## name;
627 # include "state.h" 672 # include "state.h"
628 #undef VAR 673 #undef VAR
629 } 674 }
756#endif 801#endif
757 802
758/* 803/*
759 * This overrides the default magic get method of %SIG elements. 804 * This overrides the default magic get method of %SIG elements.
760 * 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
761 * 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
762 * readback here. 807 * readback here.
763 * We only do this when the hook is != 0, as they are often set to 0 temporarily,
764 * not expecting this to actually change the hook. This is a potential problem
765 * when a schedule happens then, but we ignore this.
766 */ 808 */
767static int 809static int
768coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) 810coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
769{ 811{
770 const char *s = MgPV_nolen_const (mg); 812 const char *s = MgPV_nolen_const (mg);
823 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 865 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
824 866
825 if (svp) 867 if (svp)
826 { 868 {
827 SV *old = *svp; 869 SV *old = *svp;
828 *svp = newSVsv (sv); 870 *svp = SvOK (sv) ? newSVsv (sv) : 0;
829 SvREFCNT_dec (old); 871 SvREFCNT_dec (old);
830 return 0; 872 return 0;
831 } 873 }
832 } 874 }
833 875
865 907
866 PL_runops = RUNOPS_DEFAULT; 908 PL_runops = RUNOPS_DEFAULT;
867 PL_curcop = &PL_compiling; 909 PL_curcop = &PL_compiling;
868 PL_in_eval = EVAL_NULL; 910 PL_in_eval = EVAL_NULL;
869 PL_comppad = 0; 911 PL_comppad = 0;
912 PL_comppad_name = 0;
913 PL_comppad_name_fill = 0;
914 PL_comppad_name_floor = 0;
870 PL_curpm = 0; 915 PL_curpm = 0;
871 PL_curpad = 0; 916 PL_curpad = 0;
872 PL_localizing = 0; 917 PL_localizing = 0;
873 PL_dirty = 0; 918 PL_dirty = 0;
874 PL_restartop = 0; 919 PL_restartop = 0;
875#if PERL_VERSION_ATLEAST (5,10,0) 920#if PERL_VERSION_ATLEAST (5,10,0)
876 PL_parser = 0; 921 PL_parser = 0;
877#endif 922#endif
923 PL_hints = 0;
878 924
879 /* recreate the die/warn hooks */ 925 /* recreate the die/warn hooks */
880 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 );
881 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);
882 928
883 GvSV (PL_defgv) = newSV (0); 929 GvSV (PL_defgv) = newSV (0);
884 GvAV (PL_defgv) = coro->args; coro->args = 0; 930 GvAV (PL_defgv) = coro->args; coro->args = 0;
885 GvSV (PL_errgv) = newSV (0); 931 GvSV (PL_errgv) = newSV (0);
886 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;
887 PL_rs = newSVsv (GvSV (irsgv)); 934 PL_rs = newSVsv (GvSV (irsgv));
888 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 935 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
889 936
890 { 937 {
891 dSP; 938 dSP;
920 /* copy throw, in case it was set before coro_setup */ 967 /* copy throw, in case it was set before coro_setup */
921 CORO_THROW = coro->except; 968 CORO_THROW = coro->except;
922} 969}
923 970
924static void 971static void
925coro_destruct (pTHX_ struct coro *coro) 972coro_unwind_stacks (pTHX)
926{ 973{
927 if (!IN_DESTRUCT) 974 if (!IN_DESTRUCT)
928 { 975 {
929 /* restore all saved variables and stuff */ 976 /* restore all saved variables and stuff */
930 LEAVE_SCOPE (0); 977 LEAVE_SCOPE (0);
938 POPSTACK_TO (PL_mainstack); 985 POPSTACK_TO (PL_mainstack);
939 986
940 /* unwind main stack */ 987 /* unwind main stack */
941 dounwind (-1); 988 dounwind (-1);
942 } 989 }
990}
943 991
944 SvREFCNT_dec (GvSV (PL_defgv)); 992static void
945 SvREFCNT_dec (GvAV (PL_defgv)); 993coro_destruct_perl (pTHX_ struct coro *coro)
946 SvREFCNT_dec (GvSV (PL_errgv)); 994{
947 SvREFCNT_dec (PL_defoutgv); 995 SV *svf [9];
948 SvREFCNT_dec (PL_rs);
949 SvREFCNT_dec (GvSV (irsgv));
950 996
951 SvREFCNT_dec (PL_diehook);
952 SvREFCNT_dec (PL_warnhook);
953 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
954 SvREFCNT_dec (coro->saved_deffh); 1029 SvREFCNT_dec (coro->saved_deffh);
955 SvREFCNT_dec (coro->rouse_cb); 1030 SvREFCNT_dec (coro->rouse_cb);
956 SvREFCNT_dec (coro->invoke_cb); 1031 SvREFCNT_dec (coro->invoke_cb);
957 SvREFCNT_dec (coro->invoke_av); 1032 SvREFCNT_dec (coro->invoke_av);
958 1033 }
959 coro_destruct_stacks (aTHX);
960} 1034}
961 1035
962INLINE void 1036INLINE void
963free_coro_mortal (pTHX) 1037free_coro_mortal (pTHX)
964{ 1038{
1169 /* 1243 /*
1170 * 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
1171 * 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)
1172 * 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
1173 * bootstrap-time "top" top_env, as we cannot restore the "main" 1247 * bootstrap-time "top" top_env, as we cannot restore the "main"
1174 * 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.
1175 */ 1251 */
1176 PL_top_env = main_top_env; 1252 PL_top_env = main_top_env;
1177 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 */
1178 } 1254 }
1179} 1255}
1256cctx_destroy (coro_cctx *cctx) 1332cctx_destroy (coro_cctx *cctx)
1257{ 1333{
1258 if (!cctx) 1334 if (!cctx)
1259 return; 1335 return;
1260 1336
1261 assert (cctx != cctx_current);//D temporary 1337 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary?
1262 1338
1263 --cctx_count; 1339 --cctx_count;
1264 coro_destroy (&cctx->cctx); 1340 coro_destroy (&cctx->cctx);
1265 1341
1266 /* coro_transfer creates new, empty cctx's */ 1342 /* coro_transfer creates new, empty cctx's */
1330 /* TODO: throwing up here is considered harmful */ 1406 /* TODO: throwing up here is considered harmful */
1331 1407
1332 if (expect_true (prev != next)) 1408 if (expect_true (prev != next))
1333 { 1409 {
1334 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1410 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1335 croak ("Coro::State::transfer called with a suspended 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,");
1336 1412
1337 if (expect_false (next->flags & CF_RUNNING))
1338 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1339
1340 if (expect_false (next->flags & CF_DESTROYED)) 1413 if (expect_false (next->flags & (CF_RUNNING | CF_DESTROYED | CF_SUSPENDED)))
1341 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,");
1342 1415
1343#if !PERL_VERSION_ATLEAST (5,10,0) 1416#if !PERL_VERSION_ATLEAST (5,10,0)
1344 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1417 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1345 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,");
1346#endif 1419#endif
1384 coro_setup (aTHX_ next); 1457 coro_setup (aTHX_ next);
1385 } 1458 }
1386 else 1459 else
1387 load_perl (aTHX_ next); 1460 load_perl (aTHX_ next);
1388 1461
1389 assert (!prev->cctx);//D temporary
1390
1391 /* possibly untie and reuse the cctx */ 1462 /* possibly untie and reuse the cctx */
1392 if (expect_true ( 1463 if (expect_true (
1393 cctx_current->idle_sp == STACKLEVEL 1464 cctx_current->idle_sp == STACKLEVEL
1394 && !(cctx_current->flags & CC_TRACE) 1465 && !(cctx_current->flags & CC_TRACE)
1395 && !force_cctx 1466 && !force_cctx
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
1645static void 1720static void
1646prepare_schedule (pTHX_ struct coro_transfer_args *ta) 1721prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1647{ 1722{
1648 for (;;) 1723 for (;;)
1649 { 1724 {
1650 SV *next_sv = coro_deq (aTHX); 1725 struct coro *next = coro_deq (aTHX);
1651 1726
1652 if (expect_true (next_sv)) 1727 if (expect_true (next))
1653 { 1728 {
1654 struct coro *next = SvSTATE_hv (next_sv);
1655
1656 /* cannot transfer to destroyed coros, skip and look for next */ 1729 /* cannot transfer to destroyed coros, skip and look for next */
1657 if (expect_false (next->flags & CF_DESTROYED)) 1730 if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED)))
1658 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 */
1659 else 1732 else
1660 { 1733 {
1661 next->flags &= ~CF_READY; 1734 next->flags &= ~CF_READY;
1662 --coro_nready; 1735 --coro_nready;
1663 1736
1670 /* nothing to schedule: call the idle handler */ 1743 /* nothing to schedule: call the idle handler */
1671 if (SvROK (sv_idle) 1744 if (SvROK (sv_idle)
1672 && SvOBJECT (SvRV (sv_idle))) 1745 && SvOBJECT (SvRV (sv_idle)))
1673 { 1746 {
1674 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ 1747 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1675 api_ready (SvRV (sv_idle)); 1748 api_ready (aTHX_ SvRV (sv_idle));
1676 --coro_nready; 1749 --coro_nready;
1677 } 1750 }
1678 else 1751 else
1679 { 1752 {
1680 dSP; 1753 dSP;
1841 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 */
1842 api_ready (aTHX_ sv_manager); 1915 api_ready (aTHX_ sv_manager);
1843 1916
1844 frame->prepare = prepare_schedule; 1917 frame->prepare = prepare_schedule;
1845 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);*/
1846} 1923}
1847 1924
1848/*****************************************************************************/ 1925/*****************************************************************************/
1849/* async pool handler */ 1926/* async pool handler */
1850 1927
1933 SV *data = (SV *)GENSUB_ARG; 2010 SV *data = (SV *)GENSUB_ARG;
1934 2011
1935 if (SvTYPE (SvRV (data)) != SVt_PVAV) 2012 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1936 { 2013 {
1937 /* first call, set args */ 2014 /* first call, set args */
2015 SV *coro = SvRV (data);
1938 AV *av = newAV (); 2016 AV *av = newAV ();
1939 SV *coro = SvRV (data);
1940 2017
1941 SvRV_set (data, (SV *)av); 2018 SvRV_set (data, (SV *)av);
1942 api_ready (aTHX_ coro);
1943 SvREFCNT_dec (coro);
1944 2019
1945 /* better take a full copy of the arguments */ 2020 /* better take a full copy of the arguments */
1946 while (items--) 2021 while (items--)
1947 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);
1948 } 2026 }
1949 2027
1950 XSRETURN_EMPTY; 2028 XSRETURN_EMPTY;
1951} 2029}
1952 2030
1969 2047
1970 EXTEND (SP, AvFILLp (av) + 1); 2048 EXTEND (SP, AvFILLp (av) + 1);
1971 for (i = 0; i <= AvFILLp (av); ++i) 2049 for (i = 0; i <= AvFILLp (av); ++i)
1972 PUSHs (sv_2mortal (AvARRAY (av)[i])); 2050 PUSHs (sv_2mortal (AvARRAY (av)[i]));
1973 2051
1974 /* 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 */
1975 AvFILLp (av) = -1; 2053 AvFILLp (av) = -1;
1976 av_undef (av); 2054 av_undef (av);
1977 2055
1978 PUTBACK; 2056 PUTBACK;
1979 } 2057 }
2269 2347
2270 PL_op->op_ppaddr = pp_slf; 2348 PL_op->op_ppaddr = pp_slf;
2271 /*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 */
2272 2350
2273 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));
2274} 2399}
2275 2400
2276/*****************************************************************************/ 2401/*****************************************************************************/
2277/* PerlIO::cede */ 2402/* PerlIO::cede */
2278 2403
2352/* Coro::Semaphore & Coro::Signal */ 2477/* Coro::Semaphore & Coro::Signal */
2353 2478
2354static SV * 2479static SV *
2355coro_waitarray_new (pTHX_ int count) 2480coro_waitarray_new (pTHX_ int count)
2356{ 2481{
2357 /* 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 */
2358 AV *av = newAV (); 2483 AV *av = newAV ();
2359 SV **ary; 2484 SV **ary;
2360 2485
2361 /* unfortunately, building manually saves memory */ 2486 /* unfortunately, building manually saves memory */
2362 Newx (ary, 2, SV *); 2487 Newx (ary, 2, SV *);
2363 AvALLOC (av) = ary; 2488 AvALLOC (av) = ary;
2489#if PERL_VERSION_ATLEAST (5,10,0)
2364 /*AvARRAY (av) = ary;*/ 2490 AvARRAY (av) = ary;
2491#else
2365 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
2366 AvMAX (av) = 1; 2496 AvMAX (av) = 1;
2367 AvFILLp (av) = 0; 2497 AvFILLp (av) = 0;
2368 ary [0] = newSViv (count); 2498 ary [0] = newSViv (count);
2369 2499
2370 return newRV_noinc ((SV *)av); 2500 return newRV_noinc ((SV *)av);
2501 { 2631 {
2502 /* callback form */ 2632 /* callback form */
2503 AV *av = (AV *)SvRV (arg [0]); 2633 AV *av = (AV *)SvRV (arg [0]);
2504 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); 2634 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]);
2505 2635
2506 av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); 2636 av_push (av, SvREFCNT_inc_NN (cb_cv));
2507 2637
2508 if (SvIVX (AvARRAY (av)[0]) > 0) 2638 if (SvIVX (AvARRAY (av)[0]) > 0)
2509 coro_semaphore_adjust (aTHX_ av, 0); 2639 coro_semaphore_adjust (aTHX_ av, 0);
2510 2640
2511 frame->prepare = prepare_nop; 2641 frame->prepare = prepare_nop;
2535 AvARRAY (av)[0] = AvARRAY (av)[1]; 2665 AvARRAY (av)[0] = AvARRAY (av)[1];
2536 AvARRAY (av)[1] = cb; 2666 AvARRAY (av)[1] = cb;
2537 2667
2538 cb = av_shift (av); 2668 cb = av_shift (av);
2539 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 {
2540 api_ready (aTHX_ cb); 2680 api_ready (aTHX_ cb);
2541 sv_setiv (cb, 0); /* signal waiter */ 2681 sv_setiv (cb, 0); /* signal waiter */
2682 }
2683
2542 SvREFCNT_dec (cb); 2684 SvREFCNT_dec (cb);
2543 2685
2544 --count; 2686 --count;
2545 } 2687 }
2546} 2688}
2555static void 2697static void
2556slf_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)
2557{ 2699{
2558 AV *av = (AV *)SvRV (arg [0]); 2700 AV *av = (AV *)SvRV (arg [0]);
2559 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
2560 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]))
2561 { 2714 {
2562 SvIVX (AvARRAY (av)[0]) = 0; 2715 SvIVX (AvARRAY (av)[0]) = 0;
2563 frame->prepare = prepare_nop; 2716 frame->prepare = prepare_nop;
2564 frame->check = slf_check_nop; 2717 frame->check = slf_check_nop;
2565 } 2718 }
2566 else 2719 else
2567 { 2720 {
2568 SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */ 2721 SV *waiter = newSVsv (coro_current); /* owned by signal av */
2569 2722
2570 av_push (av, waiter); 2723 av_push (av, waiter);
2571 2724
2572 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 */
2573 frame->prepare = prepare_schedule; 2726 frame->prepare = prepare_schedule;
2899SV * 3052SV *
2900clone (Coro::State coro) 3053clone (Coro::State coro)
2901 CODE: 3054 CODE:
2902{ 3055{
2903#if CORO_CLONE 3056#if CORO_CLONE
2904 struct coro *ncoro = coro_clone (coro); 3057 struct coro *ncoro = coro_clone (aTHX_ coro);
2905 MAGIC *mg; 3058 MAGIC *mg;
2906 /* TODO: too much duplication */ 3059 /* TODO: too much duplication */
2907 ncoro->hv = newHV (); 3060 ncoro->hv = newHV ();
2908 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);
2909 mg->mg_flags |= MGf_DUP; 3062 mg->mg_flags |= MGf_DUP;
2971 eval = 1 3124 eval = 1
2972 CODE: 3125 CODE:
2973{ 3126{
2974 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3127 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
2975 { 3128 {
2976 struct coro temp; 3129 struct coro *current = SvSTATE_current;
2977 3130
2978 if (!(coro->flags & CF_RUNNING)) 3131 if (current != coro)
2979 { 3132 {
2980 PUTBACK; 3133 PUTBACK;
2981 save_perl (aTHX_ &temp); 3134 save_perl (aTHX_ current);
2982 load_perl (aTHX_ coro); 3135 load_perl (aTHX_ coro);
3136 SPAGAIN;
2983 } 3137 }
2984 3138
2985 {
2986 dSP;
2987 ENTER;
2988 SAVETMPS;
2989 PUTBACK;
2990 PUSHSTACK; 3139 PUSHSTACK;
3140
2991 PUSHMARK (SP); 3141 PUSHMARK (SP);
3142 PUTBACK;
2992 3143
2993 if (ix) 3144 if (ix)
2994 eval_sv (coderef, 0); 3145 eval_sv (coderef, 0);
2995 else 3146 else
2996 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3147 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
2997 3148
2998 POPSTACK; 3149 POPSTACK;
2999 SPAGAIN; 3150 SPAGAIN;
3000 FREETMPS;
3001 LEAVE;
3002 PUTBACK;
3003 }
3004 3151
3005 if (!(coro->flags & CF_RUNNING)) 3152 if (current != coro)
3006 { 3153 {
3154 PUTBACK;
3007 save_perl (aTHX_ coro); 3155 save_perl (aTHX_ coro);
3008 load_perl (aTHX_ &temp); 3156 load_perl (aTHX_ current);
3009 SPAGAIN; 3157 SPAGAIN;
3010 } 3158 }
3011 } 3159 }
3012} 3160}
3013 3161
3017 ALIAS: 3165 ALIAS:
3018 is_ready = CF_READY 3166 is_ready = CF_READY
3019 is_running = CF_RUNNING 3167 is_running = CF_RUNNING
3020 is_new = CF_NEW 3168 is_new = CF_NEW
3021 is_destroyed = CF_DESTROYED 3169 is_destroyed = CF_DESTROYED
3170 is_suspended = CF_SUSPENDED
3022 CODE: 3171 CODE:
3023 RETVAL = boolSV (coro->flags & ix); 3172 RETVAL = boolSV (coro->flags & ix);
3024 OUTPUT: 3173 OUTPUT:
3025 RETVAL 3174 RETVAL
3026 3175
3030 CODE: 3179 CODE:
3031{ 3180{
3032 struct coro *current = SvSTATE_current; 3181 struct coro *current = SvSTATE_current;
3033 SV **throwp = self == current ? &CORO_THROW : &self->except; 3182 SV **throwp = self == current ? &CORO_THROW : &self->except;
3034 SvREFCNT_dec (*throwp); 3183 SvREFCNT_dec (*throwp);
3184 SvGETMAGIC (throw);
3035 *throwp = SvOK (throw) ? newSVsv (throw) : 0; 3185 *throwp = SvOK (throw) ? newSVsv (throw) : 0;
3036} 3186}
3037 3187
3038void 3188void
3039api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 3189api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
3091 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 3241 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
3092 3242
3093 SV *tmp = *src; *src = *dst; *dst = tmp; 3243 SV *tmp = *src; *src = *dst; *dst = tmp;
3094 } 3244 }
3095 3245
3246void
3247cancel (Coro::State self)
3248 CODE:
3249 coro_state_destroy (aTHX_ self);
3250 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3251
3096 3252
3097MODULE = Coro::State PACKAGE = Coro 3253MODULE = Coro::State PACKAGE = Coro
3098 3254
3099BOOT: 3255BOOT:
3100{ 3256{
3101 int i;
3102
3103 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3257 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3104 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3258 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3105 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3259 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3106 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3260 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3107 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3261 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
3108 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 3262 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3109 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3263 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3110 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3264 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3121 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 3275 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
3122 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 3276 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
3123 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 3277 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
3124 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 3278 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
3125 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 3279 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
3126
3127 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
3128 coro_ready[i] = newAV ();
3129 3280
3130 { 3281 {
3131 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3282 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
3132 3283
3133 coroapi.schedule = api_schedule; 3284 coroapi.schedule = api_schedule;
3174cede_notself (...) 3325cede_notself (...)
3175 CODE: 3326 CODE:
3176 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3327 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3177 3328
3178void 3329void
3179_cancel (Coro::State self)
3180 CODE:
3181 coro_state_destroy (aTHX_ self);
3182 coro_call_on_destroy (aTHX_ self);
3183
3184void
3185_set_current (SV *current) 3330_set_current (SV *current)
3186 PROTOTYPE: $ 3331 PROTOTYPE: $
3187 CODE: 3332 CODE:
3188 SvREFCNT_dec (SvRV (coro_current)); 3333 SvREFCNT_dec (SvRV (coro_current));
3189 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); 3334 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
3191void 3336void
3192_set_readyhook (SV *hook) 3337_set_readyhook (SV *hook)
3193 PROTOTYPE: $ 3338 PROTOTYPE: $
3194 CODE: 3339 CODE:
3195 SvREFCNT_dec (coro_readyhook); 3340 SvREFCNT_dec (coro_readyhook);
3341 SvGETMAGIC (hook);
3196 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; 3342 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0;
3197 3343
3198int 3344int
3199prio (Coro::State coro, int newprio = 0) 3345prio (Coro::State coro, int newprio = 0)
3200 PROTOTYPE: $;$ 3346 PROTOTYPE: $;$
3231 PROTOTYPE: 3377 PROTOTYPE:
3232 CODE: 3378 CODE:
3233 RETVAL = coro_nready; 3379 RETVAL = coro_nready;
3234 OUTPUT: 3380 OUTPUT:
3235 RETVAL 3381 RETVAL
3382
3383void
3384suspend (Coro::State self)
3385 PROTOTYPE: $
3386 CODE:
3387 self->flags |= CF_SUSPENDED;
3388
3389void
3390resume (Coro::State self)
3391 PROTOTYPE: $
3392 CODE:
3393 self->flags &= ~CF_SUSPENDED;
3236 3394
3237void 3395void
3238_pool_handler (...) 3396_pool_handler (...)
3239 CODE: 3397 CODE:
3240 CORO_EXECUTE_SLF_XS (slf_init_pool_handler); 3398 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
3295rouse_wait (...) 3453rouse_wait (...)
3296 PROTOTYPE: ;$ 3454 PROTOTYPE: ;$
3297 PPCODE: 3455 PPCODE:
3298 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3456 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3299 3457
3458void
3459on_enter (SV *block)
3460 ALIAS:
3461 on_leave = 1
3462 PROTOTYPE: &
3463 CODE:
3464{
3465 struct coro *coro = SvSTATE_current;
3466 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
3467
3468 block = (SV *)coro_sv_2cv (aTHX_ block);
3469
3470 if (!*avp)
3471 *avp = newAV ();
3472
3473 av_push (*avp, SvREFCNT_inc (block));
3474
3475 if (!ix)
3476 on_enterleave_call (aTHX_ block);
3477
3478 LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3479 SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro);
3480 ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3481}
3482
3300 3483
3301MODULE = Coro::State PACKAGE = PerlIO::cede 3484MODULE = Coro::State PACKAGE = PerlIO::cede
3302 3485
3303BOOT: 3486BOOT:
3304 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3487 PerlIO_define_layer (aTHX_ &PerlIO_cede);
3307MODULE = Coro::State PACKAGE = Coro::Semaphore 3490MODULE = Coro::State PACKAGE = Coro::Semaphore
3308 3491
3309SV * 3492SV *
3310new (SV *klass, SV *count = 0) 3493new (SV *klass, SV *count = 0)
3311 CODE: 3494 CODE:
3495{
3496 int semcnt = 1;
3497
3498 if (count)
3499 {
3500 SvGETMAGIC (count);
3501
3502 if (SvOK (count))
3503 semcnt = SvIV (count);
3504 }
3505
3312 RETVAL = sv_bless ( 3506 RETVAL = sv_bless (
3313 coro_waitarray_new (aTHX_ count && SvOK (count) ? SvIV (count) : 1), 3507 coro_waitarray_new (aTHX_ semcnt),
3314 GvSTASH (CvGV (cv)) 3508 GvSTASH (CvGV (cv))
3315 ); 3509 );
3510}
3316 OUTPUT: 3511 OUTPUT:
3317 RETVAL 3512 RETVAL
3318 3513
3319# helper for Coro::Channel 3514# helper for Coro::Channel and others
3320SV * 3515SV *
3321_alloc (int count) 3516_alloc (int count)
3322 CODE: 3517 CODE:
3323 RETVAL = coro_waitarray_new (aTHX_ count); 3518 RETVAL = coro_waitarray_new (aTHX_ count);
3324 OUTPUT: 3519 OUTPUT:
3382 for (i = 1; i <= wcount; ++i) 3577 for (i = 1; i <= wcount; ++i)
3383 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); 3578 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
3384 } 3579 }
3385} 3580}
3386 3581
3582MODULE = Coro::State PACKAGE = Coro::SemaphoreSet
3583
3584void
3585_may_delete (SV *sem, int count, int extra_refs)
3586 PPCODE:
3587{
3588 AV *av = (AV *)SvRV (sem);
3589
3590 if (SvREFCNT ((SV *)av) == 1 + extra_refs
3591 && AvFILLp (av) == 0 /* no waiters, just count */
3592 && SvIV (AvARRAY (av)[0]) == count)
3593 XSRETURN_YES;
3594
3595 XSRETURN_NO;
3596}
3597
3387MODULE = Coro::State PACKAGE = Coro::Signal 3598MODULE = Coro::State PACKAGE = Coro::Signal
3388 3599
3389SV * 3600SV *
3390new (SV *klass) 3601new (SV *klass)
3391 CODE: 3602 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines