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.182 by root, Wed Oct 3 18:37:48 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; /* $_ */
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
194 204
205 /* statistics */
206 int usecount; /* number of transfers to this coro */
207
195 /* coro process data */ 208 /* coro process data */
196 int prio; 209 int prio;
210 //SV *throw;
211
212 /* async_pool */
213 GV *asp_deffh;
197 214
198 /* linked list */ 215 /* linked list */
199 struct coro *next, *prev; 216 struct coro *next, *prev;
200 HV *hv; /* the perl hash associated with this coro, if any */ 217 HV *hv; /* the perl hash associated with this coro, if any */
201}; 218};
289 SvMAGIC (cv) \ 306 SvMAGIC (cv) \
290 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ 307 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \
291 ? SvMAGIC (cv) \ 308 ? SvMAGIC (cv) \
292 : mg_find ((SV *)cv, PERL_MAGIC_coro) \ 309 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
293 : 0 310 : 0
311
312static struct coro *
313SvSTATE_ (pTHX_ SV *coro)
314{
315 HV *stash;
316 MAGIC *mg;
317
318 if (SvROK (coro))
319 coro = SvRV (coro);
320
321 if (SvTYPE (coro) != SVt_PVHV)
322 croak ("Coro::State object required");
323
324 stash = SvSTASH (coro);
325 if (stash != coro_stash && stash != coro_state_stash)
326 {
327 /* very slow, but rare, check */
328 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
329 croak ("Coro::State object required");
330 }
331
332 mg = CORO_MAGIC (coro);
333 return (struct coro *)mg->mg_ptr;
334}
335
336#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
294 337
295/* the next two functions merely cache the padlists */ 338/* the next two functions merely cache the padlists */
296static void 339static void
297get_padlist (pTHX_ CV *cv) 340get_padlist (pTHX_ CV *cv)
298{ 341{
337 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 380 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
338} 381}
339 382
340/** load & save, init *******************************************************/ 383/** load & save, init *******************************************************/
341 384
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 385static void
348load_perl (pTHX_ Coro__State c) 386load_perl (pTHX_ Coro__State c)
349{ 387{
350#define VAR(name,type) PL_ ## name = c->name; 388#define VAR(name,type) PL_ ## name = c->name;
351# include "state.h" 389# include "state.h"
352#undef VAR 390#undef VAR
353 391
354 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 392 GvSV (PL_defgv) = c->defsv;
355 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 393 GvAV (PL_defgv) = c->defav;
356 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 394 GvSV (PL_errgv) = c->errsv;
357 if (c->deffh) REPLACE_SV (PL_defoutgv , c->deffh); 395 PL_defoutgv = c->deffh;
358 396 PL_rs = c->irssv;
359 if (c->irssv) 397 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 398
374 { 399 {
375 dSP; 400 dSP;
376 CV *cv; 401 CV *cv;
377 402
435 } 460 }
436 461
437 PUTBACK; 462 PUTBACK;
438 } 463 }
439 464
440 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 465 c->defav = GvAV (PL_defgv);
441 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 466 c->defsv = DEFSV;
442 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 467 c->errsv = ERRSV;
443 c->deffh = c->save & CORO_SAVE_DEFFH ? (GV *)SvREFCNT_inc (PL_defoutgv) : 0; 468 c->deffh = PL_defoutgv;
444 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; 469 c->irssv = PL_rs;
470 c->irssv_sv = GvSV (irsgv);
445 471
446#define VAR(name,type)c->name = PL_ ## name; 472#define VAR(name,type)c->name = PL_ ## name;
447# include "state.h" 473# include "state.h"
448#undef VAR 474#undef VAR
449} 475}
502 * destroy the stacks, the callchain etc... 528 * destroy the stacks, the callchain etc...
503 */ 529 */
504static void 530static void
505coro_destroy_stacks (pTHX) 531coro_destroy_stacks (pTHX)
506{ 532{
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) 533 while (PL_curstackinfo->si_next)
525 PL_curstackinfo = PL_curstackinfo->si_next; 534 PL_curstackinfo = PL_curstackinfo->si_next;
526 535
527 while (PL_curstackinfo) 536 while (PL_curstackinfo)
528 { 537 {
544 Safefree (PL_retstack); 553 Safefree (PL_retstack);
545#endif 554#endif
546} 555}
547 556
548static size_t 557static size_t
549coro_rss (struct coro *coro) 558coro_rss (pTHX_ struct coro *coro)
550{ 559{
551 size_t rss = sizeof (coro); 560 size_t rss = sizeof (coro);
552 561
553 if (coro->mainstack) 562 if (coro->mainstack)
554 { 563 {
577} 586}
578 587
579/** coroutine stack handling ************************************************/ 588/** coroutine stack handling ************************************************/
580 589
581static void 590static void
582setup_coro (pTHX_ struct coro *coro) 591coro_setup (pTHX_ struct coro *coro)
583{ 592{
584 /* 593 /*
585 * emulate part of the perl startup here. 594 * emulate part of the perl startup here.
586 */ 595 */
587 coro_init_stacks (aTHX); 596 coro_init_stacks (aTHX);
588 597
598 PL_runops = RUNOPS_DEFAULT;
589 PL_curcop = &PL_compiling; 599 PL_curcop = &PL_compiling;
590 PL_in_eval = EVAL_NULL; 600 PL_in_eval = EVAL_NULL;
591 PL_comppad = 0; 601 PL_comppad = 0;
592 PL_curpm = 0; 602 PL_curpm = 0;
593 PL_localizing = 0; 603 PL_localizing = 0;
594 PL_dirty = 0; 604 PL_dirty = 0;
595 PL_restartop = 0; 605 PL_restartop = 0;
606
607 GvSV (PL_defgv) = NEWSV (0, 0);
608 GvAV (PL_defgv) = coro->args; coro->args = 0;
609 GvSV (PL_errgv) = NEWSV (0, 0);
610 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
611 PL_rs = newSVsv (GvSV (irsgv));
612
613 {
614 IO *io = newIO ();
615 PL_defoutgv = newGVgen ("Coro");
616 GvIOp(PL_defoutgv) = io;
617 IoTYPE (io) = IoTYPE_WRONLY;
618 IoOFP (io) = IoIFP (io) = PerlIO_stdout ();
619 IoFLAGS (io) |= IOf_FLUSH;
620 }
596 621
597 { 622 {
598 dSP; 623 dSP;
599 LOGOP myop; 624 LOGOP myop;
600
601 SvREFCNT_dec (GvAV (PL_defgv));
602 GvAV (PL_defgv) = coro->args; coro->args = 0;
603 625
604 Zero (&myop, 1, LOGOP); 626 Zero (&myop, 1, LOGOP);
605 myop.op_next = Nullop; 627 myop.op_next = Nullop;
606 myop.op_flags = OPf_WANT_VOID; 628 myop.op_flags = OPf_WANT_VOID;
607 629
615 637
616 ENTER; /* necessary e.g. for dounwind */ 638 ENTER; /* necessary e.g. for dounwind */
617} 639}
618 640
619static void 641static void
642coro_destroy (pTHX_ struct coro *coro)
643{
644 if (!IN_DESTRUCT)
645 {
646 /* restore all saved variables and stuff */
647 LEAVE_SCOPE (0);
648 assert (PL_tmps_floor == -1);
649
650 /* free all temporaries */
651 FREETMPS;
652 assert (PL_tmps_ix == -1);
653
654 /* unwind all extra stacks */
655 POPSTACK_TO (PL_mainstack);
656
657 /* unwind main stack */
658 dounwind (-1);
659 }
660
661 SvREFCNT_dec (GvSV (PL_defgv));
662 SvREFCNT_dec (GvAV (PL_defgv));
663 SvREFCNT_dec (GvSV (PL_errgv));
664 SvREFCNT_dec (PL_defoutgv);
665 SvREFCNT_dec (PL_rs);
666 SvREFCNT_dec (GvSV (irsgv));
667
668 SvREFCNT_dec (coro->asp_deffh);
669 //SvREFCNT_dec (coro->throw);
670
671 coro_destroy_stacks (aTHX);
672}
673
674static void
620free_coro_mortal (pTHX) 675free_coro_mortal (pTHX)
621{ 676{
622 if (coro_mortal) 677 if (coro_mortal)
623 { 678 {
624 SvREFCNT_dec (coro_mortal); 679 SvREFCNT_dec (coro_mortal);
625 coro_mortal = 0; 680 coro_mortal = 0;
626 } 681 }
682}
683
684static int
685runops_trace (pTHX)
686{
687 COP *oldcop = 0;
688 int oldcxix = -2;
689 struct coro *coro = SvSTATE (coro_current); /* trace cctx is tied to specific coro */
690 coro_cctx *cctx = coro->cctx;
691
692 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
693 {
694 PERL_ASYNC_CHECK ();
695
696 if (cctx->flags & CC_TRACE_ALL)
697 {
698 if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB)
699 {
700 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
701 SV **bot, **top;
702 AV *av = newAV (); /* return values */
703 SV **cb;
704 dSP;
705
706 GV *gv = CvGV (cx->blk_sub.cv);
707 SV *fullname = sv_2mortal (newSV (0));
708 if (isGV (gv))
709 gv_efullname3 (fullname, gv, 0);
710
711 bot = PL_stack_base + cx->blk_oldsp + 1;
712 top = cx->blk_gimme == G_ARRAY ? SP + 1
713 : cx->blk_gimme == G_SCALAR ? bot + 1
714 : bot;
715
716 while (bot < top)
717 av_push (av, SvREFCNT_inc (*bot++));
718
719 PL_runops = RUNOPS_DEFAULT;
720 ENTER;
721 SAVETMPS;
722 EXTEND (SP, 3);
723 PUSHMARK (SP);
724 PUSHs (&PL_sv_no);
725 PUSHs (fullname);
726 PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
727 PUTBACK;
728 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
729 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
730 SPAGAIN;
731 FREETMPS;
732 LEAVE;
733 PL_runops = runops_trace;
734 }
735
736 if (oldcop != PL_curcop)
737 {
738 oldcop = PL_curcop;
739
740 if (PL_curcop != &PL_compiling)
741 {
742 SV **cb;
743
744 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB)
745 {
746 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
747
748 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
749 {
750 runops_proc_t old_runops = PL_runops;
751 dSP;
752 GV *gv = CvGV (cx->blk_sub.cv);
753 SV *fullname = sv_2mortal (newSV (0));
754
755 if (isGV (gv))
756 gv_efullname3 (fullname, gv, 0);
757
758 PL_runops = RUNOPS_DEFAULT;
759 ENTER;
760 SAVETMPS;
761 EXTEND (SP, 3);
762 PUSHMARK (SP);
763 PUSHs (&PL_sv_yes);
764 PUSHs (fullname);
765 PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
766 PUTBACK;
767 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
768 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
769 SPAGAIN;
770 FREETMPS;
771 LEAVE;
772 PL_runops = runops_trace;
773 }
774
775 oldcxix = cxstack_ix;
776 }
777
778 if (cctx->flags & CC_TRACE_LINE)
779 {
780 dSP;
781
782 PL_runops = RUNOPS_DEFAULT;
783 ENTER;
784 SAVETMPS;
785 EXTEND (SP, 3);
786 PL_runops = RUNOPS_DEFAULT;
787 PUSHMARK (SP);
788 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0)));
789 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop))));
790 PUTBACK;
791 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0);
792 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
793 SPAGAIN;
794 FREETMPS;
795 LEAVE;
796 PL_runops = runops_trace;
797 }
798 }
799 }
800 }
801 }
802
803 TAINT_NOT;
804 return 0;
627} 805}
628 806
629/* inject a fake call to Coro::State::_cctx_init into the execution */ 807/* 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 */ 808/* _cctx_init should be careful, as it could be called at almost any time */
631/* during execution of a perl program */ 809/* during execution of a perl program */
632static void NOINLINE 810static void NOINLINE
633prepare_cctx (pTHX_ coro_cctx *cctx) 811prepare_cctx (pTHX_ coro_cctx *cctx)
634{ 812{
635 dSP; 813 dSP;
636 LOGOP myop; 814 LOGOP myop;
815
816 PL_top_env = &PL_start_env;
817
818 if (cctx->flags & CC_TRACE)
819 PL_runops = runops_trace;
637 820
638 Zero (&myop, 1, LOGOP); 821 Zero (&myop, 1, LOGOP);
639 myop.op_next = PL_op; 822 myop.op_next = PL_op;
640 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 823 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
641 824
658 dTHX; 841 dTHX;
659 842
660 /* coro_run is the alternative tail of transfer(), so unlock here. */ 843 /* coro_run is the alternative tail of transfer(), so unlock here. */
661 UNLOCK; 844 UNLOCK;
662 845
663 PL_top_env = &PL_start_env; 846 /* we now skip the entersub that lead to transfer() */
847 PL_op = PL_op->op_next;
664 848
665 /* inject a fake subroutine call to cctx_init */ 849 /* inject a fake subroutine call to cctx_init */
666 prepare_cctx (aTHX_ (coro_cctx *)arg); 850 prepare_cctx (aTHX_ (coro_cctx *)arg);
667 851
668 /* somebody or something will hit me for both perl_run and PL_restartop */ 852 /* somebody or something will hit me for both perl_run and PL_restartop */
702# if CORO_STACKGUARD 886# if CORO_STACKGUARD
703 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 887 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
704# endif 888# endif
705 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 889 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr;
706 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 890 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
707 cctx->mapped = 1; 891 cctx->flags |= CC_MAPPED;
708 } 892 }
709 else 893 else
710#endif 894#endif
711 { 895 {
712 cctx->ssize = coro_stacksize * (long)sizeof (long); 896 cctx->ssize = coro_stacksize * (long)sizeof (long);
739#if CORO_USE_VALGRIND 923#if CORO_USE_VALGRIND
740 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 924 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
741#endif 925#endif
742 926
743#if HAVE_MMAP 927#if HAVE_MMAP
744 if (cctx->mapped) 928 if (cctx->flags & CC_MAPPED)
745 munmap (cctx->sptr, cctx->ssize); 929 munmap (cctx->sptr, cctx->ssize);
746 else 930 else
747#endif 931#endif
748 Safefree (cctx->sptr); 932 Safefree (cctx->sptr);
749 933
757 { 941 {
758 coro_cctx *cctx = cctx_first; 942 coro_cctx *cctx = cctx_first;
759 cctx_first = cctx->next; 943 cctx_first = cctx->next;
760 --cctx_idle; 944 --cctx_idle;
761 945
762 if (cctx->ssize >= coro_stacksize) 946 if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE))
763 return cctx; 947 return cctx;
764 948
765 cctx_destroy (cctx); 949 cctx_destroy (cctx);
766 } 950 }
767 951
768 PL_op = PL_op->op_next;
769 return cctx_new (); 952 return cctx_new ();
770} 953}
771 954
772static void 955static void
773cctx_put (coro_cctx *cctx) 956cctx_put (coro_cctx *cctx)
777 { 960 {
778 coro_cctx *first = cctx_first; 961 coro_cctx *first = cctx_first;
779 cctx_first = first->next; 962 cctx_first = first->next;
780 --cctx_idle; 963 --cctx_idle;
781 964
782 assert (!first->inuse);
783 cctx_destroy (first); 965 cctx_destroy (first);
784 } 966 }
785 967
786 ++cctx_idle; 968 ++cctx_idle;
787 cctx->next = cctx_first; 969 cctx->next = cctx_first;
788 cctx_first = cctx; 970 cctx_first = cctx;
789} 971}
790 972
791/** coroutine switching *****************************************************/ 973/** coroutine switching *****************************************************/
792 974
793/* never call directly, always through the coro_state_transfer global variable */ 975static void NOINLINE
976transfer_check (pTHX_ struct coro *prev, struct coro *next)
977{
978 if (prev != next)
979 {
980 if (!(prev->flags & (CF_RUNNING | CF_NEW)))
981 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states");
982
983 if (next->flags & CF_RUNNING)
984 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
985
986 if (next->flags & CF_DESTROYED)
987 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
988
989 if (PL_lex_state != LEX_NOTPARSING)
990 croak ("Coro::State::transfer called while parsing, but this is not supported");
991 }
992}
993
994/* always use the TRANSFER macro */
794static void NOINLINE 995static void NOINLINE
795transfer (pTHX_ struct coro *prev, struct coro *next) 996transfer (pTHX_ struct coro *prev, struct coro *next)
796{ 997{
797 dSTACKLEVEL; 998 dSTACKLEVEL;
798 999
808 1009
809 if (prev->flags & CF_NEW) 1010 if (prev->flags & CF_NEW)
810 { 1011 {
811 /* create a new empty context */ 1012 /* create a new empty context */
812 Newz (0, prev->cctx, 1, coro_cctx); 1013 Newz (0, prev->cctx, 1, coro_cctx);
813 prev->cctx->inuse = 1;
814 prev->flags &= ~CF_NEW; 1014 prev->flags &= ~CF_NEW;
815 prev->flags |= CF_RUNNING; 1015 prev->flags |= CF_RUNNING;
816 } 1016 }
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 1017
828 prev->flags &= ~CF_RUNNING; 1018 prev->flags &= ~CF_RUNNING;
829 next->flags |= CF_RUNNING; 1019 next->flags |= CF_RUNNING;
830 1020
831 LOCK; 1021 LOCK;
835 /* need to start coroutine */ 1025 /* need to start coroutine */
836 next->flags &= ~CF_NEW; 1026 next->flags &= ~CF_NEW;
837 /* first get rid of the old state */ 1027 /* first get rid of the old state */
838 save_perl (aTHX_ prev); 1028 save_perl (aTHX_ prev);
839 /* setup coroutine call */ 1029 /* setup coroutine call */
840 setup_coro (aTHX_ next); 1030 coro_setup (aTHX_ next);
841 /* need a new stack */
842 assert (!next->cctx);
843 } 1031 }
844 else 1032 else
845 { 1033 {
846 /* coroutine already started */ 1034 /* coroutine already started */
847 save_perl (aTHX_ prev); 1035 save_perl (aTHX_ prev);
849 } 1037 }
850 1038
851 prev__cctx = prev->cctx; 1039 prev__cctx = prev->cctx;
852 1040
853 /* possibly "free" the cctx */ 1041 /* possibly "free" the cctx */
854 if (prev__cctx->idle_sp == STACKLEVEL) 1042 if (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))
855 { 1043 {
856 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1044 /* 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)); 1045 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
858 1046
859 prev->cctx = 0; 1047 prev->cctx = 0;
860 1048
861 cctx_put (prev__cctx); 1049 cctx_put (prev__cctx);
862 prev__cctx->inuse = 0;
863 } 1050 }
864 1051
1052 ++next->usecount;
1053
865 if (!next->cctx) 1054 if (!next->cctx)
866 {
867 next->cctx = cctx_get (aTHX); 1055 next->cctx = cctx_get (aTHX);
868 assert (!next->cctx->inuse);
869 next->cctx->inuse = 1;
870 }
871 1056
872 if (prev__cctx != next->cctx) 1057 if (prev__cctx != next->cctx)
873 { 1058 {
874 prev__cctx->top_env = PL_top_env; 1059 prev__cctx->top_env = PL_top_env;
875 PL_top_env = next->cctx->top_env; 1060 PL_top_env = next->cctx->top_env;
885{ 1070{
886 struct coro *prev, *next; 1071 struct coro *prev, *next;
887}; 1072};
888 1073
889#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) 1074#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next)
1075#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
890 1076
891/** high level stuff ********************************************************/ 1077/** high level stuff ********************************************************/
892 1078
893static int 1079static int
894coro_state_destroy (pTHX_ struct coro *coro) 1080coro_state_destroy (pTHX_ struct coro *coro)
911 1097
912 if (coro->mainstack && coro->mainstack != main_mainstack) 1098 if (coro->mainstack && coro->mainstack != main_mainstack)
913 { 1099 {
914 struct coro temp; 1100 struct coro temp;
915 1101
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) 1102 if (coro->flags & CF_RUNNING)
922 croak ("FATAL: tried to destroy currently running coroutine"); 1103 croak ("FATAL: tried to destroy currently running coroutine");
923 1104
924 save_perl (aTHX_ &temp); 1105 save_perl (aTHX_ &temp);
925 load_perl (aTHX_ coro); 1106 load_perl (aTHX_ coro);
926 1107
927 coro_destroy_stacks (aTHX); 1108 coro_destroy (aTHX_ coro);
928 1109
929 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ 1110 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */
930 1111
931 coro->mainstack = 0; 1112 coro->mainstack = 0;
932 } 1113 }
977#else 1158#else
978# define MGf_DUP 0 1159# define MGf_DUP 0
979#endif 1160#endif
980}; 1161};
981 1162
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 1163static void
1006prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) 1164prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv)
1007{ 1165{
1008 ta->prev = SvSTATE (prev_sv); 1166 ta->prev = SvSTATE (prev_sv);
1009 ta->next = SvSTATE (next_sv); 1167 ta->next = SvSTATE (next_sv);
1168 TRANSFER_CHECK (*ta);
1010} 1169}
1011 1170
1012static void 1171static void
1013api_transfer (SV *prev_sv, SV *next_sv) 1172api_transfer (SV *prev_sv, SV *next_sv)
1014{ 1173{
1015 dTHX; 1174 dTHX;
1016 struct transfer_args ta; 1175 struct transfer_args ta;
1017 1176
1018 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1177 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1019 TRANSFER (ta); 1178 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} 1179}
1034 1180
1035/** Coro ********************************************************************/ 1181/** Coro ********************************************************************/
1036 1182
1037static void 1183static void
1131 break; 1277 break;
1132 } 1278 }
1133 1279
1134 /* free this only after the transfer */ 1280 /* free this only after the transfer */
1135 prev_sv = SvRV (coro_current); 1281 prev_sv = SvRV (coro_current);
1136 SvRV_set (coro_current, next_sv);
1137 ta->prev = SvSTATE (prev_sv); 1282 ta->prev = SvSTATE (prev_sv);
1138 1283 TRANSFER_CHECK (*ta);
1139 assert (ta->next->flags & CF_READY); 1284 assert (ta->next->flags & CF_READY);
1140 ta->next->flags &= ~CF_READY; 1285 ta->next->flags &= ~CF_READY;
1286 SvRV_set (coro_current, next_sv);
1141 1287
1142 LOCK; 1288 LOCK;
1143 free_coro_mortal (aTHX); 1289 free_coro_mortal (aTHX);
1144 coro_mortal = prev_sv; 1290 coro_mortal = prev_sv;
1145 UNLOCK; 1291 UNLOCK;
1206 } 1352 }
1207 else 1353 else
1208 return 0; 1354 return 0;
1209} 1355}
1210 1356
1357static void
1358api_trace (SV *coro_sv, int flags)
1359{
1360 dTHX;
1361 struct coro *coro = SvSTATE (coro_sv);
1362
1363 if (flags & CC_TRACE)
1364 {
1365 if (!coro->cctx)
1366 coro->cctx = cctx_new ();
1367 else if (!(coro->cctx->flags & CC_TRACE))
1368 croak ("cannot enable tracing on coroutine with custom stack");
1369
1370 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1371 }
1372 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1373 {
1374 coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL);
1375
1376 if (coro->flags & CF_RUNNING)
1377 PL_runops = RUNOPS_DEFAULT;
1378 else
1379 coro->runops = RUNOPS_DEFAULT;
1380 }
1381}
1382
1211MODULE = Coro::State PACKAGE = Coro::State 1383MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1212 1384
1213PROTOTYPES: DISABLE 1385PROTOTYPES: DISABLE
1214 1386
1215BOOT: 1387BOOT:
1216{ 1388{
1217#ifdef USE_ITHREADS 1389#ifdef USE_ITHREADS
1218 MUTEX_INIT (&coro_mutex); 1390 MUTEX_INIT (&coro_mutex);
1219#endif 1391#endif
1220 BOOT_PAGESIZE; 1392 BOOT_PAGESIZE;
1221 1393
1394 irsgv = gv_fetchpv ("/", 1, SVt_PV);
1395
1222 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1396 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1223 1397
1224 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); 1398 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1225 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); 1399 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)); 1400 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1230 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); 1401 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1231 1402
1232 main_mainstack = PL_mainstack; 1403 main_mainstack = PL_mainstack;
1233 main_top_env = PL_top_env; 1404 main_top_env = PL_top_env;
1234 1405
1235 while (main_top_env->je_prev) 1406 while (main_top_env->je_prev)
1248 struct coro *coro; 1419 struct coro *coro;
1249 HV *hv; 1420 HV *hv;
1250 int i; 1421 int i;
1251 1422
1252 Newz (0, coro, 1, struct coro); 1423 Newz (0, coro, 1, struct coro);
1253 coro->args = newAV (); 1424 coro->args = newAV ();
1254 coro->save = CORO_SAVE_DEF;
1255 coro->flags = CF_NEW; 1425 coro->flags = CF_NEW;
1256 1426
1257 if (coro_first) coro_first->prev = coro; 1427 if (coro_first) coro_first->prev = coro;
1258 coro->next = coro_first; 1428 coro->next = coro_first;
1259 coro_first = coro; 1429 coro_first = coro;
1266 av_push (coro->args, newSVsv (ST (i))); 1436 av_push (coro->args, newSVsv (ST (i)));
1267} 1437}
1268 OUTPUT: 1438 OUTPUT:
1269 RETVAL 1439 RETVAL
1270 1440
1271int 1441# these not obviously related functions are all rolled into the same xs
1272save (SV *coro, int new_save = -1) 1442# function to increase chances that they all will call transfer with the same
1273 CODE: 1443# 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 1444void
1290_set_stacklevel (...) 1445_set_stacklevel (...)
1291 ALIAS: 1446 ALIAS:
1292 Coro::State::transfer = 1 1447 Coro::State::transfer = 1
1293 Coro::schedule = 2 1448 Coro::schedule = 2
1379 if (coro->hv) 1534 if (coro->hv)
1380 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); 1535 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
1381} 1536}
1382 1537
1383void 1538void
1384_eval (Coro::State coro, SV *coderef) 1539call (Coro::State coro, SV *coderef)
1540 ALIAS:
1541 eval = 1
1385 CODE: 1542 CODE:
1386{ 1543{
1387 if (coro->mainstack) 1544 if (coro->mainstack)
1388 { 1545 {
1389 struct coro temp; 1546 struct coro temp;
1390 Zero (&temp, 1, struct coro); 1547 Zero (&temp, 1, struct coro);
1391 temp.save = CORO_SAVE_ALL;
1392 1548
1393 if (!(coro->flags & CF_RUNNING)) 1549 if (!(coro->flags & CF_RUNNING))
1394 { 1550 {
1395 save_perl (aTHX_ &temp); 1551 save_perl (aTHX_ &temp);
1396 load_perl (aTHX_ coro); 1552 load_perl (aTHX_ coro);
1400 dSP; 1556 dSP;
1401 ENTER; 1557 ENTER;
1402 SAVETMPS; 1558 SAVETMPS;
1403 PUSHMARK (SP); 1559 PUSHMARK (SP);
1404 PUTBACK; 1560 PUTBACK;
1561 if (ix)
1562 eval_sv (coderef, 0);
1563 else
1405 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1564 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1406 SPAGAIN; 1565 SPAGAIN;
1407 FREETMPS; 1566 FREETMPS;
1408 LEAVE; 1567 LEAVE;
1409 PUTBACK; 1568 PUTBACK;
1410 } 1569 }
1414 save_perl (aTHX_ coro); 1573 save_perl (aTHX_ coro);
1415 load_perl (aTHX_ &temp); 1574 load_perl (aTHX_ &temp);
1416 } 1575 }
1417 } 1576 }
1418} 1577}
1419 1578
1420SV * 1579SV *
1421is_ready (Coro::State coro) 1580is_ready (Coro::State coro)
1422 PROTOTYPE: $ 1581 PROTOTYPE: $
1423 ALIAS: 1582 ALIAS:
1424 is_ready = CF_READY 1583 is_ready = CF_READY
1428 CODE: 1587 CODE:
1429 RETVAL = boolSV (coro->flags & ix); 1588 RETVAL = boolSV (coro->flags & ix);
1430 OUTPUT: 1589 OUTPUT:
1431 RETVAL 1590 RETVAL
1432 1591
1592void
1593api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
1594
1433SV * 1595SV *
1434has_stack (Coro::State coro) 1596has_stack (Coro::State coro)
1435 PROTOTYPE: $ 1597 PROTOTYPE: $
1436 CODE: 1598 CODE:
1437 RETVAL = boolSV (!!coro->cctx); 1599 RETVAL = boolSV (!!coro->cctx);
1438 OUTPUT: 1600 OUTPUT:
1439 RETVAL 1601 RETVAL
1440 1602
1603int
1604is_traced (Coro::State coro)
1605 PROTOTYPE: $
1606 CODE:
1607 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1608 OUTPUT:
1609 RETVAL
1610
1441IV 1611IV
1442rss (Coro::State coro) 1612rss (Coro::State coro)
1443 PROTOTYPE: $ 1613 PROTOTYPE: $
1614 ALIAS:
1615 usecount = 1
1444 CODE: 1616 CODE:
1445 RETVAL = coro_rss (coro); 1617 switch (ix)
1618 {
1619 case 0: RETVAL = coro_rss (aTHX_ coro); break;
1620 case 1: RETVAL = coro->usecount; break;
1621 }
1446 OUTPUT: 1622 OUTPUT:
1447 RETVAL 1623 RETVAL
1448 1624
1449 1625
1450MODULE = Coro::State PACKAGE = Coro 1626MODULE = Coro::State PACKAGE = Coro
1455 1631
1456 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); 1632 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE);
1457 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); 1633 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE);
1458 av_async_pool = get_av ("Coro::async_pool", TRUE); 1634 av_async_pool = get_av ("Coro::async_pool", TRUE);
1459 1635
1460 coro_current = get_sv ("Coro::current", FALSE); 1636 coro_current = get_sv ("Coro::current", FALSE);
1461 SvREADONLY_on (coro_current); 1637 SvREADONLY_on (coro_current);
1462 1638
1463 coro_stash = gv_stashpv ("Coro", TRUE); 1639 coro_stash = gv_stashpv ("Coro", TRUE);
1464 1640
1465 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1641 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1474 1650
1475 { 1651 {
1476 SV *sv = perl_get_sv("Coro::API", 1); 1652 SV *sv = perl_get_sv("Coro::API", 1);
1477 1653
1478 coroapi.schedule = api_schedule; 1654 coroapi.schedule = api_schedule;
1479 coroapi.save = api_save;
1480 coroapi.cede = api_cede; 1655 coroapi.cede = api_cede;
1481 coroapi.cede_notself = api_cede_notself; 1656 coroapi.cede_notself = api_cede_notself;
1482 coroapi.ready = api_ready; 1657 coroapi.ready = api_ready;
1483 coroapi.is_ready = api_is_ready; 1658 coroapi.is_ready = api_is_ready;
1484 coroapi.nready = &coro_nready; 1659 coroapi.nready = &coro_nready;
1538# for async_pool speedup 1713# for async_pool speedup
1539void 1714void
1540_pool_1 (SV *cb) 1715_pool_1 (SV *cb)
1541 CODE: 1716 CODE:
1542{ 1717{
1543 int i, len; 1718 struct coro *coro = SvSTATE (coro_current);
1544 HV *hv = (HV *)SvRV (coro_current); 1719 HV *hv = (HV *)SvRV (coro_current);
1545 AV *defav = GvAV (PL_defgv); 1720 AV *defav = GvAV (PL_defgv);
1546 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); 1721 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1547 AV *invoke_av; 1722 AV *invoke_av;
1723 int i, len;
1548 1724
1549 if (!invoke) 1725 if (!invoke)
1550 croak ("\3terminate\2\n"); 1726 croak ("\3terminate\2\n");
1727
1728 SvREFCNT_dec (coro->asp_deffh);
1729 coro->asp_deffh = SvREFCNT_inc (PL_defoutgv);
1551 1730
1552 hv_store (hv, "desc", sizeof ("desc") - 1, 1731 hv_store (hv, "desc", sizeof ("desc") - 1,
1553 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 1732 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1554 1733
1555 invoke_av = (AV *)SvRV (invoke); 1734 invoke_av = (AV *)SvRV (invoke);
1573{ 1752{
1574 struct coro *coro = SvSTATE (coro_current); 1753 struct coro *coro = SvSTATE (coro_current);
1575 1754
1576 sv_setsv (cb, &PL_sv_undef); 1755 sv_setsv (cb, &PL_sv_undef);
1577 1756
1757 SvREFCNT_dec (PL_defoutgv); PL_defoutgv = coro->asp_deffh;
1758 coro->asp_deffh = 0;
1759
1578 if (coro_rss (coro) > SvIV (sv_pool_rss) 1760 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1579 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 1761 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1580 croak ("\3terminate\2\n"); 1762 croak ("\3terminate\2\n");
1581 1763
1582 av_clear (GvAV (PL_defgv)); 1764 av_clear (GvAV (PL_defgv));
1583 hv_store (SvRV (coro_current), "desc", sizeof ("desc") - 1, 1765 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1584 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); 1766 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1585 coro->save = CORO_SAVE_DEF; 1767
1586 coro->prio = 0; 1768 coro->prio = 0;
1769
1770 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
1771 api_trace (coro_current, 0);
1772
1587 av_push (av_async_pool, newSVsv (coro_current)); 1773 av_push (av_async_pool, newSVsv (coro_current));
1588} 1774}
1589 1775
1590 1776
1591MODULE = Coro::State PACKAGE = Coro::AIO 1777MODULE = Coro::State PACKAGE = Coro::AIO

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines