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.160 by root, Sat Sep 22 22:39:15 2007 UTC vs.
Revision 1.178 by root, Wed Oct 3 01:48:06 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines