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.162 by root, Sun Sep 23 21:49:58 2007 UTC vs.
Revision 1.190 by root, Fri Oct 5 20:39:39 2007 UTC

75/* 5.8.7 */ 75/* 5.8.7 */
76#ifndef SvRV_set 76#ifndef SvRV_set
77# define SvRV_set(s,v) SvRV(s) = (v) 77# define SvRV_set(s,v) SvRV(s) = (v)
78#endif 78#endif
79 79
80/* 5.8.8 */
81#ifndef GV_NOTQUAL
82# define GV_NOTQUAL 0
83#endif
84
80#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 85#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
81# undef CORO_STACKGUARD 86# undef CORO_STACKGUARD
82#endif 87#endif
83 88
84#ifndef CORO_STACKGUARD 89#ifndef CORO_STACKGUARD
132static AV *main_mainstack; /* used to differentiate between $main and others */ 137static AV *main_mainstack; /* used to differentiate between $main and others */
133static JMPENV *main_top_env; 138static JMPENV *main_top_env;
134static HV *coro_state_stash, *coro_stash; 139static HV *coro_state_stash, *coro_stash;
135static SV *coro_mortal; /* will be freed after next transfer */ 140static SV *coro_mortal; /* will be freed after next transfer */
136 141
142static GV *irsgv; /* $/ */
143static GV *stdoutgv; /* *STDOUT */
144
137/* async_pool helper stuff */ 145/* async_pool helper stuff */
138static SV *sv_pool_rss; 146static SV *sv_pool_rss;
139static SV *sv_pool_size; 147static SV *sv_pool_size;
140static AV *av_async_pool; 148static AV *av_async_pool;
141 149
142static struct coro_cctx *cctx_first; 150static struct coro_cctx *cctx_first;
143static int cctx_count, cctx_idle; 151static int cctx_count, cctx_idle;
152
153enum {
154 CC_MAPPED = 0x01,
155 CC_NOREUSE = 0x02, /* throw this away after tracing */
156 CC_TRACE = 0x04,
157 CC_TRACE_SUB = 0x08, /* trace sub calls */
158 CC_TRACE_LINE = 0x10, /* trace each statement */
159 CC_TRACE_ALL = CC_TRACE_SUB | CC_TRACE_LINE,
160};
144 161
145/* this is a structure representing a c-level coroutine */ 162/* this is a structure representing a c-level coroutine */
146typedef struct coro_cctx { 163typedef struct coro_cctx {
147 struct coro_cctx *next; 164 struct coro_cctx *next;
148 165
157 coro_context cctx; 174 coro_context cctx;
158 175
159#if CORO_USE_VALGRIND 176#if CORO_USE_VALGRIND
160 int valgrind_id; 177 int valgrind_id;
161#endif 178#endif
162 char inuse, mapped; 179 unsigned char flags;
163} coro_cctx; 180} coro_cctx;
164 181
165enum { 182enum {
166 CF_RUNNING = 0x0001, /* coroutine is running */ 183 CF_RUNNING = 0x0001, /* coroutine is running */
167 CF_READY = 0x0002, /* coroutine is ready */ 184 CF_READY = 0x0002, /* coroutine is ready */
175 coro_cctx *cctx; 192 coro_cctx *cctx;
176 193
177 /* data associated with this coroutine (initial args) */ 194 /* data associated with this coroutine (initial args) */
178 AV *args; 195 AV *args;
179 int refcnt; 196 int refcnt;
180 int save; /* CORO_SAVE flags */
181 int flags; /* CF_ flags */ 197 int flags; /* CF_ flags */
182 198
183 /* optionally saved, might be zero */ 199 /* optionally saved, might be zero */
184 AV *defav; /* @_ */ 200 AV *defav; /* @_ */
185 SV *defsv; /* $_ */ 201 SV *defsv; /* $_ */
186 SV *errsv; /* $@ */ 202 SV *errsv; /* $@ */
187 GV *deffh; /* default filehandle */ 203 SV *deffh; /* default filehandle */
188 SV *irssv; /* $/ */ 204 SV *irssv; /* $/ */
189 SV *irssv_sv; /* real $/ cache */ 205 SV *irssv_sv; /* real $/ cache */
190 206
191#define VAR(name,type) type name; 207#define VAR(name,type) type name;
192# include "state.h" 208# include "state.h"
193#undef VAR 209#undef VAR
194 210
211 /* statistics */
212 int usecount; /* number of transfers to this coro */
213
195 /* coro process data */ 214 /* coro process data */
196 int prio; 215 int prio;
216 //SV *throw;
217
218 /* async_pool */
219 SV *saved_deffh;
197 220
198 /* linked list */ 221 /* linked list */
199 struct coro *next, *prev; 222 struct coro *next, *prev;
200 HV *hv; /* the perl hash associated with this coro, if any */ 223 HV *hv; /* the perl hash associated with this coro, if any */
201}; 224};
289 SvMAGIC (cv) \ 312 SvMAGIC (cv) \
290 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ 313 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \
291 ? SvMAGIC (cv) \ 314 ? SvMAGIC (cv) \
292 : mg_find ((SV *)cv, PERL_MAGIC_coro) \ 315 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
293 : 0 316 : 0
317
318static struct coro *
319SvSTATE_ (pTHX_ SV *coro)
320{
321 HV *stash;
322 MAGIC *mg;
323
324 if (SvROK (coro))
325 coro = SvRV (coro);
326
327 if (SvTYPE (coro) != SVt_PVHV)
328 croak ("Coro::State object required");
329
330 stash = SvSTASH (coro);
331 if (stash != coro_stash && stash != coro_state_stash)
332 {
333 /* very slow, but rare, check */
334 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
335 croak ("Coro::State object required");
336 }
337
338 mg = CORO_MAGIC (coro);
339 return (struct coro *)mg->mg_ptr;
340}
341
342#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
294 343
295/* the next two functions merely cache the padlists */ 344/* the next two functions merely cache the padlists */
296static void 345static void
297get_padlist (pTHX_ CV *cv) 346get_padlist (pTHX_ CV *cv)
298{ 347{
337 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 386 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
338} 387}
339 388
340/** load & save, init *******************************************************/ 389/** load & save, init *******************************************************/
341 390
342#define SB do {
343#define SE } while (0)
344
345#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
346
347static void 391static void
348load_perl (pTHX_ Coro__State c) 392load_perl (pTHX_ Coro__State c)
349{ 393{
350#define VAR(name,type) PL_ ## name = c->name; 394#define VAR(name,type) PL_ ## name = c->name;
351# include "state.h" 395# include "state.h"
352#undef VAR 396#undef VAR
353 397
354 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 398 GvSV (PL_defgv) = c->defsv;
355 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 399 GvAV (PL_defgv) = c->defav;
356 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 400 GvSV (PL_errgv) = c->errsv;
357 if (c->deffh) REPLACE_SV (PL_defoutgv , c->deffh); 401 GvSV (irsgv) = c->irssv_sv;
358
359 if (c->irssv)
360 {
361 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv))
362 {
363 SvREFCNT_dec (c->irssv);
364 c->irssv = 0;
365 }
366 else
367 {
368 REPLACE_SV (PL_rs, c->irssv);
369 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0);
370 sv_setsv (c->irssv_sv, PL_rs);
371 }
372 }
373 402
374 { 403 {
375 dSP; 404 dSP;
376 CV *cv; 405 CV *cv;
377 406
435 } 464 }
436 465
437 PUTBACK; 466 PUTBACK;
438 } 467 }
439 468
440 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 469 c->defav = GvAV (PL_defgv);
441 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 470 c->defsv = DEFSV;
442 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 471 c->errsv = ERRSV;
443 c->deffh = c->save & CORO_SAVE_DEFFH ? (GV *)SvREFCNT_inc (PL_defoutgv) : 0; 472 c->irssv_sv = GvSV (irsgv);
444 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0;
445 473
446#define VAR(name,type)c->name = PL_ ## name; 474#define VAR(name,type)c->name = PL_ ## name;
447# include "state.h" 475# include "state.h"
448#undef VAR 476#undef VAR
449} 477}
502 * destroy the stacks, the callchain etc... 530 * destroy the stacks, the callchain etc...
503 */ 531 */
504static void 532static void
505coro_destroy_stacks (pTHX) 533coro_destroy_stacks (pTHX)
506{ 534{
507 if (!IN_DESTRUCT)
508 {
509 /* restore all saved variables and stuff */
510 LEAVE_SCOPE (0);
511 assert (PL_tmps_floor == -1);
512
513 /* free all temporaries */
514 FREETMPS;
515 assert (PL_tmps_ix == -1);
516
517 /* unwind all extra stacks */
518 POPSTACK_TO (PL_mainstack);
519
520 /* unwind main stack */
521 dounwind (-1);
522 }
523
524 while (PL_curstackinfo->si_next) 535 while (PL_curstackinfo->si_next)
525 PL_curstackinfo = PL_curstackinfo->si_next; 536 PL_curstackinfo = PL_curstackinfo->si_next;
526 537
527 while (PL_curstackinfo) 538 while (PL_curstackinfo)
528 { 539 {
544 Safefree (PL_retstack); 555 Safefree (PL_retstack);
545#endif 556#endif
546} 557}
547 558
548static size_t 559static size_t
549coro_rss (struct coro *coro) 560coro_rss (pTHX_ struct coro *coro)
550{ 561{
551 size_t rss = sizeof (coro); 562 size_t rss = sizeof (*coro);
552 563
553 if (coro->mainstack) 564 if (coro->mainstack)
554 { 565 {
555 if (coro->flags & CF_RUNNING) 566 if (coro->flags & CF_RUNNING)
556 { 567 {
558 # include "state.h" 569 # include "state.h"
559 #undef VAR 570 #undef VAR
560 } 571 }
561 572
562 rss += sizeof (coro->curstackinfo); 573 rss += sizeof (coro->curstackinfo);
563 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *); 574 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *);
564 rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 575 rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
565 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); 576 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *);
566 rss += coro->tmps_max * sizeof (SV *); 577 rss += coro->tmps_max * sizeof (SV *);
567 rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); 578 rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32);
568 rss += coro->scopestack_max * sizeof (I32); 579 rss += coro->scopestack_max * sizeof (I32);
569 rss += coro->savestack_max * sizeof (ANY); 580 rss += coro->savestack_max * sizeof (ANY);
570 581
577} 588}
578 589
579/** coroutine stack handling ************************************************/ 590/** coroutine stack handling ************************************************/
580 591
581static void 592static void
582setup_coro (pTHX_ struct coro *coro) 593coro_setup (pTHX_ struct coro *coro)
583{ 594{
584 /* 595 /*
585 * emulate part of the perl startup here. 596 * emulate part of the perl startup here.
586 */ 597 */
587 coro_init_stacks (aTHX); 598 coro_init_stacks (aTHX);
588 599
600 PL_runops = RUNOPS_DEFAULT;
589 PL_curcop = &PL_compiling; 601 PL_curcop = &PL_compiling;
590 PL_in_eval = EVAL_NULL; 602 PL_in_eval = EVAL_NULL;
591 PL_comppad = 0; 603 PL_comppad = 0;
592 PL_curpm = 0; 604 PL_curpm = 0;
593 PL_localizing = 0; 605 PL_localizing = 0;
594 PL_dirty = 0; 606 PL_dirty = 0;
595 PL_restartop = 0; 607 PL_restartop = 0;
608
609 GvSV (PL_defgv) = NEWSV (0, 0);
610 GvAV (PL_defgv) = coro->args; coro->args = 0;
611 GvSV (PL_errgv) = NEWSV (0, 0);
612 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
613 PL_rs = newSVsv (GvSV (irsgv));
614 PL_defoutgv = SvREFCNT_inc (stdoutgv);
596 615
597 { 616 {
598 dSP; 617 dSP;
599 LOGOP myop; 618 LOGOP myop;
600
601 SvREFCNT_dec (GvAV (PL_defgv));
602 GvAV (PL_defgv) = coro->args; coro->args = 0;
603 619
604 Zero (&myop, 1, LOGOP); 620 Zero (&myop, 1, LOGOP);
605 myop.op_next = Nullop; 621 myop.op_next = Nullop;
606 myop.op_flags = OPf_WANT_VOID; 622 myop.op_flags = OPf_WANT_VOID;
607 623
615 631
616 ENTER; /* necessary e.g. for dounwind */ 632 ENTER; /* necessary e.g. for dounwind */
617} 633}
618 634
619static void 635static void
636coro_destroy (pTHX_ struct coro *coro)
637{
638 if (!IN_DESTRUCT)
639 {
640 /* restore all saved variables and stuff */
641 LEAVE_SCOPE (0);
642 assert (PL_tmps_floor == -1);
643
644 /* free all temporaries */
645 FREETMPS;
646 assert (PL_tmps_ix == -1);
647
648 /* unwind all extra stacks */
649 POPSTACK_TO (PL_mainstack);
650
651 /* unwind main stack */
652 dounwind (-1);
653 }
654
655 SvREFCNT_dec (GvSV (PL_defgv));
656 SvREFCNT_dec (GvAV (PL_defgv));
657 SvREFCNT_dec (GvSV (PL_errgv));
658 SvREFCNT_dec (PL_defoutgv);
659 SvREFCNT_dec (PL_rs);
660 SvREFCNT_dec (GvSV (irsgv));
661
662 SvREFCNT_dec (coro->saved_deffh);
663 //SvREFCNT_dec (coro->throw);
664
665 coro_destroy_stacks (aTHX);
666}
667
668static void
620free_coro_mortal (pTHX) 669free_coro_mortal (pTHX)
621{ 670{
622 if (coro_mortal) 671 if (coro_mortal)
623 { 672 {
624 SvREFCNT_dec (coro_mortal); 673 SvREFCNT_dec (coro_mortal);
625 coro_mortal = 0; 674 coro_mortal = 0;
626 } 675 }
676}
677
678static int
679runops_trace (pTHX)
680{
681 COP *oldcop = 0;
682 int oldcxix = -2;
683 struct coro *coro = SvSTATE (coro_current); /* trace cctx is tied to specific coro */
684 coro_cctx *cctx = coro->cctx;
685
686 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
687 {
688 PERL_ASYNC_CHECK ();
689
690 if (cctx->flags & CC_TRACE_ALL)
691 {
692 if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB)
693 {
694 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
695 SV **bot, **top;
696 AV *av = newAV (); /* return values */
697 SV **cb;
698 dSP;
699
700 GV *gv = CvGV (cx->blk_sub.cv);
701 SV *fullname = sv_2mortal (newSV (0));
702 if (isGV (gv))
703 gv_efullname3 (fullname, gv, 0);
704
705 bot = PL_stack_base + cx->blk_oldsp + 1;
706 top = cx->blk_gimme == G_ARRAY ? SP + 1
707 : cx->blk_gimme == G_SCALAR ? bot + 1
708 : bot;
709
710 while (bot < top)
711 av_push (av, SvREFCNT_inc (*bot++));
712
713 PL_runops = RUNOPS_DEFAULT;
714 ENTER;
715 SAVETMPS;
716 EXTEND (SP, 3);
717 PUSHMARK (SP);
718 PUSHs (&PL_sv_no);
719 PUSHs (fullname);
720 PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
721 PUTBACK;
722 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
723 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
724 SPAGAIN;
725 FREETMPS;
726 LEAVE;
727 PL_runops = runops_trace;
728 }
729
730 if (oldcop != PL_curcop)
731 {
732 oldcop = PL_curcop;
733
734 if (PL_curcop != &PL_compiling)
735 {
736 SV **cb;
737
738 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB)
739 {
740 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
741
742 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
743 {
744 runops_proc_t old_runops = PL_runops;
745 dSP;
746 GV *gv = CvGV (cx->blk_sub.cv);
747 SV *fullname = sv_2mortal (newSV (0));
748
749 if (isGV (gv))
750 gv_efullname3 (fullname, gv, 0);
751
752 PL_runops = RUNOPS_DEFAULT;
753 ENTER;
754 SAVETMPS;
755 EXTEND (SP, 3);
756 PUSHMARK (SP);
757 PUSHs (&PL_sv_yes);
758 PUSHs (fullname);
759 PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
760 PUTBACK;
761 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
762 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
763 SPAGAIN;
764 FREETMPS;
765 LEAVE;
766 PL_runops = runops_trace;
767 }
768
769 oldcxix = cxstack_ix;
770 }
771
772 if (cctx->flags & CC_TRACE_LINE)
773 {
774 dSP;
775
776 PL_runops = RUNOPS_DEFAULT;
777 ENTER;
778 SAVETMPS;
779 EXTEND (SP, 3);
780 PL_runops = RUNOPS_DEFAULT;
781 PUSHMARK (SP);
782 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0)));
783 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop))));
784 PUTBACK;
785 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0);
786 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
787 SPAGAIN;
788 FREETMPS;
789 LEAVE;
790 PL_runops = runops_trace;
791 }
792 }
793 }
794 }
795 }
796
797 TAINT_NOT;
798 return 0;
627} 799}
628 800
629/* inject a fake call to Coro::State::_cctx_init into the execution */ 801/* inject a fake call to Coro::State::_cctx_init into the execution */
630/* _cctx_init should be careful, as it could be called at almost any time */ 802/* _cctx_init should be careful, as it could be called at almost any time */
631/* during execution of a perl program */ 803/* during execution of a perl program */
632static void NOINLINE 804static void NOINLINE
633prepare_cctx (pTHX_ coro_cctx *cctx) 805prepare_cctx (pTHX_ coro_cctx *cctx)
634{ 806{
635 dSP; 807 dSP;
636 LOGOP myop; 808 LOGOP myop;
809
810 PL_top_env = &PL_start_env;
811
812 if (cctx->flags & CC_TRACE)
813 PL_runops = runops_trace;
637 814
638 Zero (&myop, 1, LOGOP); 815 Zero (&myop, 1, LOGOP);
639 myop.op_next = PL_op; 816 myop.op_next = PL_op;
640 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 817 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
641 818
658 dTHX; 835 dTHX;
659 836
660 /* coro_run is the alternative tail of transfer(), so unlock here. */ 837 /* coro_run is the alternative tail of transfer(), so unlock here. */
661 UNLOCK; 838 UNLOCK;
662 839
663 PL_top_env = &PL_start_env; 840 /* we now skip the entersub that lead to transfer() */
841 PL_op = PL_op->op_next;
664 842
665 /* inject a fake subroutine call to cctx_init */ 843 /* inject a fake subroutine call to cctx_init */
666 prepare_cctx (aTHX_ (coro_cctx *)arg); 844 prepare_cctx (aTHX_ (coro_cctx *)arg);
667 845
668 /* somebody or something will hit me for both perl_run and PL_restartop */ 846 /* somebody or something will hit me for both perl_run and PL_restartop */
702# if CORO_STACKGUARD 880# if CORO_STACKGUARD
703 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 881 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
704# endif 882# endif
705 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 883 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr;
706 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 884 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
707 cctx->mapped = 1; 885 cctx->flags |= CC_MAPPED;
708 } 886 }
709 else 887 else
710#endif 888#endif
711 { 889 {
712 cctx->ssize = coro_stacksize * (long)sizeof (long); 890 cctx->ssize = coro_stacksize * (long)sizeof (long);
739#if CORO_USE_VALGRIND 917#if CORO_USE_VALGRIND
740 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 918 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
741#endif 919#endif
742 920
743#if HAVE_MMAP 921#if HAVE_MMAP
744 if (cctx->mapped) 922 if (cctx->flags & CC_MAPPED)
745 munmap (cctx->sptr, cctx->ssize); 923 munmap (cctx->sptr, cctx->ssize);
746 else 924 else
747#endif 925#endif
748 Safefree (cctx->sptr); 926 Safefree (cctx->sptr);
749 927
757 { 935 {
758 coro_cctx *cctx = cctx_first; 936 coro_cctx *cctx = cctx_first;
759 cctx_first = cctx->next; 937 cctx_first = cctx->next;
760 --cctx_idle; 938 --cctx_idle;
761 939
762 if (cctx->ssize >= coro_stacksize) 940 if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE))
763 return cctx; 941 return cctx;
764 942
765 cctx_destroy (cctx); 943 cctx_destroy (cctx);
766 } 944 }
767 945
768 PL_op = PL_op->op_next;
769 return cctx_new (); 946 return cctx_new ();
770} 947}
771 948
772static void 949static void
773cctx_put (coro_cctx *cctx) 950cctx_put (coro_cctx *cctx)
777 { 954 {
778 coro_cctx *first = cctx_first; 955 coro_cctx *first = cctx_first;
779 cctx_first = first->next; 956 cctx_first = first->next;
780 --cctx_idle; 957 --cctx_idle;
781 958
782 assert (!first->inuse);
783 cctx_destroy (first); 959 cctx_destroy (first);
784 } 960 }
785 961
786 ++cctx_idle; 962 ++cctx_idle;
787 cctx->next = cctx_first; 963 cctx->next = cctx_first;
788 cctx_first = cctx; 964 cctx_first = cctx;
789} 965}
790 966
791/** coroutine switching *****************************************************/ 967/** coroutine switching *****************************************************/
792 968
793/* never call directly, always through the coro_state_transfer global variable */ 969static void NOINLINE
970transfer_check (pTHX_ struct coro *prev, struct coro *next)
971{
972 if (prev != next)
973 {
974 if (!(prev->flags & (CF_RUNNING | CF_NEW)))
975 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states");
976
977 if (next->flags & CF_RUNNING)
978 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
979
980 if (next->flags & CF_DESTROYED)
981 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
982
983 if (PL_lex_state != LEX_NOTPARSING)
984 croak ("Coro::State::transfer called while parsing, but this is not supported");
985 }
986}
987
988/* always use the TRANSFER macro */
794static void NOINLINE 989static void NOINLINE
795transfer (pTHX_ struct coro *prev, struct coro *next) 990transfer (pTHX_ struct coro *prev, struct coro *next)
796{ 991{
797 dSTACKLEVEL; 992 dSTACKLEVEL;
798 993
808 1003
809 if (prev->flags & CF_NEW) 1004 if (prev->flags & CF_NEW)
810 { 1005 {
811 /* create a new empty context */ 1006 /* create a new empty context */
812 Newz (0, prev->cctx, 1, coro_cctx); 1007 Newz (0, prev->cctx, 1, coro_cctx);
813 prev->cctx->inuse = 1;
814 prev->flags &= ~CF_NEW; 1008 prev->flags &= ~CF_NEW;
815 prev->flags |= CF_RUNNING; 1009 prev->flags |= CF_RUNNING;
816 } 1010 }
817
818 /*TODO: must not croak here */
819 if (!prev->flags & CF_RUNNING)
820 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
821
822 if (next->flags & CF_RUNNING)
823 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
824
825 if (next->flags & CF_DESTROYED)
826 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
827 1011
828 prev->flags &= ~CF_RUNNING; 1012 prev->flags &= ~CF_RUNNING;
829 next->flags |= CF_RUNNING; 1013 next->flags |= CF_RUNNING;
830 1014
831 LOCK; 1015 LOCK;
835 /* need to start coroutine */ 1019 /* need to start coroutine */
836 next->flags &= ~CF_NEW; 1020 next->flags &= ~CF_NEW;
837 /* first get rid of the old state */ 1021 /* first get rid of the old state */
838 save_perl (aTHX_ prev); 1022 save_perl (aTHX_ prev);
839 /* setup coroutine call */ 1023 /* setup coroutine call */
840 setup_coro (aTHX_ next); 1024 coro_setup (aTHX_ next);
841 /* need a new stack */
842 assert (!next->cctx);
843 } 1025 }
844 else 1026 else
845 { 1027 {
846 /* coroutine already started */ 1028 /* coroutine already started */
847 save_perl (aTHX_ prev); 1029 save_perl (aTHX_ prev);
849 } 1031 }
850 1032
851 prev__cctx = prev->cctx; 1033 prev__cctx = prev->cctx;
852 1034
853 /* possibly "free" the cctx */ 1035 /* possibly "free" the cctx */
854 if (prev__cctx->idle_sp == STACKLEVEL) 1036 if (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))
855 { 1037 {
856 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1038 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
857 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); 1039 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
858 1040
859 prev->cctx = 0; 1041 prev->cctx = 0;
860 1042
861 cctx_put (prev__cctx); 1043 cctx_put (prev__cctx);
862 prev__cctx->inuse = 0;
863 } 1044 }
864 1045
1046 ++next->usecount;
1047
865 if (!next->cctx) 1048 if (!next->cctx)
866 {
867 next->cctx = cctx_get (aTHX); 1049 next->cctx = cctx_get (aTHX);
868 assert (!next->cctx->inuse);
869 next->cctx->inuse = 1;
870 }
871 1050
872 if (prev__cctx != next->cctx) 1051 if (prev__cctx != next->cctx)
873 { 1052 {
874 prev__cctx->top_env = PL_top_env; 1053 prev__cctx->top_env = PL_top_env;
875 PL_top_env = next->cctx->top_env; 1054 PL_top_env = next->cctx->top_env;
885{ 1064{
886 struct coro *prev, *next; 1065 struct coro *prev, *next;
887}; 1066};
888 1067
889#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) 1068#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next)
1069#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
890 1070
891/** high level stuff ********************************************************/ 1071/** high level stuff ********************************************************/
892 1072
893static int 1073static int
894coro_state_destroy (pTHX_ struct coro *coro) 1074coro_state_destroy (pTHX_ struct coro *coro)
911 1091
912 if (coro->mainstack && coro->mainstack != main_mainstack) 1092 if (coro->mainstack && coro->mainstack != main_mainstack)
913 { 1093 {
914 struct coro temp; 1094 struct coro temp;
915 1095
916 assert (!(coro->flags & CF_RUNNING));
917
918 Zero (&temp, 1, struct coro);
919 temp.save = CORO_SAVE_ALL;
920
921 if (coro->flags & CF_RUNNING) 1096 if (coro->flags & CF_RUNNING)
922 croak ("FATAL: tried to destroy currently running coroutine"); 1097 croak ("FATAL: tried to destroy currently running coroutine");
923 1098
924 save_perl (aTHX_ &temp); 1099 save_perl (aTHX_ &temp);
925 load_perl (aTHX_ coro); 1100 load_perl (aTHX_ coro);
926 1101
927 coro_destroy_stacks (aTHX); 1102 coro_destroy (aTHX_ coro);
928 1103
929 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ 1104 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */
930 1105
931 coro->mainstack = 0; 1106 coro->mainstack = 0;
932 } 1107 }
977#else 1152#else
978# define MGf_DUP 0 1153# define MGf_DUP 0
979#endif 1154#endif
980}; 1155};
981 1156
982static struct coro *
983SvSTATE_ (pTHX_ SV *coro)
984{
985 HV *stash;
986 MAGIC *mg;
987
988 if (SvROK (coro))
989 coro = SvRV (coro);
990
991 stash = SvSTASH (coro);
992 if (stash != coro_stash && stash != coro_state_stash)
993 {
994 /* very slow, but rare, check */
995 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
996 croak ("Coro::State object required");
997 }
998
999 mg = CORO_MAGIC (coro);
1000 return (struct coro *)mg->mg_ptr;
1001}
1002
1003#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
1004
1005static void 1157static void
1006prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) 1158prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv)
1007{ 1159{
1008 ta->prev = SvSTATE (prev_sv); 1160 ta->prev = SvSTATE (prev_sv);
1009 ta->next = SvSTATE (next_sv); 1161 ta->next = SvSTATE (next_sv);
1162 TRANSFER_CHECK (*ta);
1010} 1163}
1011 1164
1012static void 1165static void
1013api_transfer (SV *prev_sv, SV *next_sv) 1166api_transfer (SV *prev_sv, SV *next_sv)
1014{ 1167{
1015 dTHX; 1168 dTHX;
1016 struct transfer_args ta; 1169 struct transfer_args ta;
1017 1170
1018 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1171 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1019 TRANSFER (ta); 1172 TRANSFER (ta);
1020}
1021
1022static int
1023api_save (SV *coro_sv, int new_save)
1024{
1025 dTHX;
1026 struct coro *coro = SvSTATE (coro_sv);
1027 int old_save = coro->save;
1028
1029 if (new_save >= 0)
1030 coro->save = new_save;
1031
1032 return old_save;
1033} 1173}
1034 1174
1035/** Coro ********************************************************************/ 1175/** Coro ********************************************************************/
1036 1176
1037static void 1177static void
1131 break; 1271 break;
1132 } 1272 }
1133 1273
1134 /* free this only after the transfer */ 1274 /* free this only after the transfer */
1135 prev_sv = SvRV (coro_current); 1275 prev_sv = SvRV (coro_current);
1136 SvRV_set (coro_current, next_sv);
1137 ta->prev = SvSTATE (prev_sv); 1276 ta->prev = SvSTATE (prev_sv);
1138 1277 TRANSFER_CHECK (*ta);
1139 assert (ta->next->flags & CF_READY); 1278 assert (ta->next->flags & CF_READY);
1140 ta->next->flags &= ~CF_READY; 1279 ta->next->flags &= ~CF_READY;
1280 SvRV_set (coro_current, next_sv);
1141 1281
1142 LOCK; 1282 LOCK;
1143 free_coro_mortal (aTHX); 1283 free_coro_mortal (aTHX);
1144 coro_mortal = prev_sv; 1284 coro_mortal = prev_sv;
1145 UNLOCK; 1285 UNLOCK;
1206 } 1346 }
1207 else 1347 else
1208 return 0; 1348 return 0;
1209} 1349}
1210 1350
1351static void
1352api_trace (SV *coro_sv, int flags)
1353{
1354 dTHX;
1355 struct coro *coro = SvSTATE (coro_sv);
1356
1357 if (flags & CC_TRACE)
1358 {
1359 if (!coro->cctx)
1360 coro->cctx = cctx_new ();
1361 else if (!(coro->cctx->flags & CC_TRACE))
1362 croak ("cannot enable tracing on coroutine with custom stack");
1363
1364 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1365 }
1366 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1367 {
1368 coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL);
1369
1370 if (coro->flags & CF_RUNNING)
1371 PL_runops = RUNOPS_DEFAULT;
1372 else
1373 coro->runops = RUNOPS_DEFAULT;
1374 }
1375}
1376
1211MODULE = Coro::State PACKAGE = Coro::State 1377MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1212 1378
1213PROTOTYPES: DISABLE 1379PROTOTYPES: DISABLE
1214 1380
1215BOOT: 1381BOOT:
1216{ 1382{
1217#ifdef USE_ITHREADS 1383#ifdef USE_ITHREADS
1218 MUTEX_INIT (&coro_mutex); 1384 MUTEX_INIT (&coro_mutex);
1219#endif 1385#endif
1220 BOOT_PAGESIZE; 1386 BOOT_PAGESIZE;
1221 1387
1388 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1389 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1390
1222 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1391 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1223 1392
1224 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); 1393 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1225 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); 1394 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1226 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1227 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1228 newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH));
1229 newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF)); 1395 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1230 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); 1396 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1231 1397
1232 main_mainstack = PL_mainstack; 1398 main_mainstack = PL_mainstack;
1233 main_top_env = PL_top_env; 1399 main_top_env = PL_top_env;
1234 1400
1235 while (main_top_env->je_prev) 1401 while (main_top_env->je_prev)
1248 struct coro *coro; 1414 struct coro *coro;
1249 HV *hv; 1415 HV *hv;
1250 int i; 1416 int i;
1251 1417
1252 Newz (0, coro, 1, struct coro); 1418 Newz (0, coro, 1, struct coro);
1253 coro->args = newAV (); 1419 coro->args = newAV ();
1254 coro->save = CORO_SAVE_DEF;
1255 coro->flags = CF_NEW; 1420 coro->flags = CF_NEW;
1256 1421
1257 if (coro_first) coro_first->prev = coro; 1422 if (coro_first) coro_first->prev = coro;
1258 coro->next = coro_first; 1423 coro->next = coro_first;
1259 coro_first = coro; 1424 coro_first = coro;
1266 av_push (coro->args, newSVsv (ST (i))); 1431 av_push (coro->args, newSVsv (ST (i)));
1267} 1432}
1268 OUTPUT: 1433 OUTPUT:
1269 RETVAL 1434 RETVAL
1270 1435
1271int 1436# these not obviously related functions are all rolled into the same xs
1272save (SV *coro, int new_save = -1) 1437# function to increase chances that they all will call transfer with the same
1273 CODE: 1438# stack offset
1274 RETVAL = api_save (coro, new_save);
1275 OUTPUT:
1276 RETVAL
1277
1278int
1279save_also (SV *coro_sv, int save_also)
1280 CODE:
1281{
1282 struct coro *coro = SvSTATE (coro_sv);
1283 RETVAL = coro->save;
1284 coro->save |= save_also;
1285}
1286 OUTPUT:
1287 RETVAL
1288
1289void 1439void
1290_set_stacklevel (...) 1440_set_stacklevel (...)
1291 ALIAS: 1441 ALIAS:
1292 Coro::State::transfer = 1 1442 Coro::State::transfer = 1
1293 Coro::schedule = 2 1443 Coro::schedule = 2
1379 if (coro->hv) 1529 if (coro->hv)
1380 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); 1530 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
1381} 1531}
1382 1532
1383void 1533void
1384_eval (Coro::State coro, SV *coderef) 1534call (Coro::State coro, SV *coderef)
1535 ALIAS:
1536 eval = 1
1385 CODE: 1537 CODE:
1386{ 1538{
1387 if (coro->mainstack) 1539 if (coro->mainstack)
1388 { 1540 {
1389 struct coro temp; 1541 struct coro temp;
1390 Zero (&temp, 1, struct coro); 1542 Zero (&temp, 1, struct coro);
1391 temp.save = CORO_SAVE_ALL;
1392 1543
1393 if (!(coro->flags & CF_RUNNING)) 1544 if (!(coro->flags & CF_RUNNING))
1394 { 1545 {
1395 save_perl (aTHX_ &temp); 1546 save_perl (aTHX_ &temp);
1396 load_perl (aTHX_ coro); 1547 load_perl (aTHX_ coro);
1400 dSP; 1551 dSP;
1401 ENTER; 1552 ENTER;
1402 SAVETMPS; 1553 SAVETMPS;
1403 PUSHMARK (SP); 1554 PUSHMARK (SP);
1404 PUTBACK; 1555 PUTBACK;
1556 if (ix)
1557 eval_sv (coderef, 0);
1558 else
1405 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1559 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1406 SPAGAIN; 1560 SPAGAIN;
1407 FREETMPS; 1561 FREETMPS;
1408 LEAVE; 1562 LEAVE;
1409 PUTBACK; 1563 PUTBACK;
1410 } 1564 }
1414 save_perl (aTHX_ coro); 1568 save_perl (aTHX_ coro);
1415 load_perl (aTHX_ &temp); 1569 load_perl (aTHX_ &temp);
1416 } 1570 }
1417 } 1571 }
1418} 1572}
1419 1573
1420SV * 1574SV *
1421is_ready (Coro::State coro) 1575is_ready (Coro::State coro)
1422 PROTOTYPE: $ 1576 PROTOTYPE: $
1423 ALIAS: 1577 ALIAS:
1424 is_ready = CF_READY 1578 is_ready = CF_READY
1428 CODE: 1582 CODE:
1429 RETVAL = boolSV (coro->flags & ix); 1583 RETVAL = boolSV (coro->flags & ix);
1430 OUTPUT: 1584 OUTPUT:
1431 RETVAL 1585 RETVAL
1432 1586
1587void
1588api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
1589
1433SV * 1590SV *
1434has_stack (Coro::State coro) 1591has_stack (Coro::State coro)
1435 PROTOTYPE: $ 1592 PROTOTYPE: $
1436 CODE: 1593 CODE:
1437 RETVAL = boolSV (!!coro->cctx); 1594 RETVAL = boolSV (!!coro->cctx);
1438 OUTPUT: 1595 OUTPUT:
1439 RETVAL 1596 RETVAL
1440 1597
1598int
1599is_traced (Coro::State coro)
1600 PROTOTYPE: $
1601 CODE:
1602 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1603 OUTPUT:
1604 RETVAL
1605
1441IV 1606IV
1442rss (Coro::State coro) 1607rss (Coro::State coro)
1443 PROTOTYPE: $ 1608 PROTOTYPE: $
1609 ALIAS:
1610 usecount = 1
1444 CODE: 1611 CODE:
1445 RETVAL = coro_rss (coro); 1612 switch (ix)
1613 {
1614 case 0: RETVAL = coro_rss (aTHX_ coro); break;
1615 case 1: RETVAL = coro->usecount; break;
1616 }
1446 OUTPUT: 1617 OUTPUT:
1447 RETVAL 1618 RETVAL
1448 1619
1449 1620
1450MODULE = Coro::State PACKAGE = Coro 1621MODULE = Coro::State PACKAGE = Coro
1455 1626
1456 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); 1627 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE);
1457 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); 1628 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE);
1458 av_async_pool = get_av ("Coro::async_pool", TRUE); 1629 av_async_pool = get_av ("Coro::async_pool", TRUE);
1459 1630
1460 coro_current = get_sv ("Coro::current", FALSE); 1631 coro_current = get_sv ("Coro::current", FALSE);
1461 SvREADONLY_on (coro_current); 1632 SvREADONLY_on (coro_current);
1462 1633
1463 coro_stash = gv_stashpv ("Coro", TRUE); 1634 coro_stash = gv_stashpv ("Coro", TRUE);
1464 1635
1465 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1636 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1474 1645
1475 { 1646 {
1476 SV *sv = perl_get_sv("Coro::API", 1); 1647 SV *sv = perl_get_sv("Coro::API", 1);
1477 1648
1478 coroapi.schedule = api_schedule; 1649 coroapi.schedule = api_schedule;
1479 coroapi.save = api_save;
1480 coroapi.cede = api_cede; 1650 coroapi.cede = api_cede;
1481 coroapi.cede_notself = api_cede_notself; 1651 coroapi.cede_notself = api_cede_notself;
1482 coroapi.ready = api_ready; 1652 coroapi.ready = api_ready;
1483 coroapi.is_ready = api_is_ready; 1653 coroapi.is_ready = api_is_ready;
1484 coroapi.nready = &coro_nready; 1654 coroapi.nready = &coro_nready;
1538# for async_pool speedup 1708# for async_pool speedup
1539void 1709void
1540_pool_1 (SV *cb) 1710_pool_1 (SV *cb)
1541 CODE: 1711 CODE:
1542{ 1712{
1543 int i, len; 1713 struct coro *coro = SvSTATE (coro_current);
1544 HV *hv = (HV *)SvRV (coro_current); 1714 HV *hv = (HV *)SvRV (coro_current);
1545 AV *defav = GvAV (PL_defgv); 1715 AV *defav = GvAV (PL_defgv);
1546 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); 1716 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1547 AV *invoke_av; 1717 AV *invoke_av;
1718 int i, len;
1548 1719
1549 if (!invoke) 1720 if (!invoke)
1550 croak ("\3terminate\2\n"); 1721 croak ("\3terminate\2\n");
1722
1723 SvREFCNT_dec (coro->saved_deffh);
1724 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv);
1551 1725
1552 hv_store (hv, "desc", sizeof ("desc") - 1, 1726 hv_store (hv, "desc", sizeof ("desc") - 1,
1553 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 1727 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1554 1728
1555 invoke_av = (AV *)SvRV (invoke); 1729 invoke_av = (AV *)SvRV (invoke);
1573{ 1747{
1574 struct coro *coro = SvSTATE (coro_current); 1748 struct coro *coro = SvSTATE (coro_current);
1575 1749
1576 sv_setsv (cb, &PL_sv_undef); 1750 sv_setsv (cb, &PL_sv_undef);
1577 1751
1752 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
1753 coro->saved_deffh = 0;
1754
1578 if (coro_rss (coro) > SvIV (sv_pool_rss) 1755 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1579 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 1756 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1580 croak ("\3terminate\2\n"); 1757 croak ("\3terminate\2\n");
1581 1758
1582 av_clear (GvAV (PL_defgv)); 1759 av_clear (GvAV (PL_defgv));
1583 hv_store (SvRV (coro_current), "desc", sizeof ("desc") - 1, 1760 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1584 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); 1761 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1585 coro->save = CORO_SAVE_DEF; 1762
1586 coro->prio = 0; 1763 coro->prio = 0;
1764
1765 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
1766 api_trace (coro_current, 0);
1767
1587 av_push (av_async_pool, newSVsv (coro_current)); 1768 av_push (av_async_pool, newSVsv (coro_current));
1588} 1769}
1589 1770
1590 1771
1591MODULE = Coro::State PACKAGE = Coro::AIO 1772MODULE = Coro::State PACKAGE = Coro::AIO

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines