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.165 by root, Wed Sep 26 19:26:48 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;
144 146
145enum { 147enum {
146 CC_MAPPED = 0x01, 148 CC_MAPPED = 0x01,
147 CC_TRACE = 0x02,
148 CC_NOREUSE = 0x04, /* throw this away after tracing */ 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,
149}; 154};
150 155
151/* this is a structure representing a c-level coroutine */ 156/* this is a structure representing a c-level coroutine */
152typedef struct coro_cctx { 157typedef struct coro_cctx {
153 struct coro_cctx *next; 158 struct coro_cctx *next;
181 coro_cctx *cctx; 186 coro_cctx *cctx;
182 187
183 /* data associated with this coroutine (initial args) */ 188 /* data associated with this coroutine (initial args) */
184 AV *args; 189 AV *args;
185 int refcnt; 190 int refcnt;
186 int save; /* CORO_SAVE flags */
187 int flags; /* CF_ flags */ 191 int flags; /* CF_ flags */
188 192
189 /* optionally saved, might be zero */ 193 /* optionally saved, might be zero */
190 AV *defav; /* @_ */ 194 AV *defav; /* @_ */
191 SV *defsv; /* $_ */ 195 SV *defsv; /* $_ */
192 SV *errsv; /* $@ */ 196 SV *errsv; /* $@ */
193 GV *deffh; /* default filehandle */ 197 IO *deffh; /* default filehandle */
194 SV *irssv; /* $/ */ 198 SV *irssv; /* $/ */
195 SV *irssv_sv; /* real $/ cache */ 199 SV *irssv_sv; /* real $/ cache */
196 200
197#define VAR(name,type) type name; 201#define VAR(name,type) type name;
198# include "state.h" 202# include "state.h"
199#undef VAR 203#undef VAR
204
205 /* statistics */
206 int usecount; /* number of switches to this coro */
200 207
201 /* coro process data */ 208 /* coro process data */
202 int prio; 209 int prio;
203 210
204 /* linked list */ 211 /* linked list */
295 SvMAGIC (cv) \ 302 SvMAGIC (cv) \
296 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ 303 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \
297 ? SvMAGIC (cv) \ 304 ? SvMAGIC (cv) \
298 : mg_find ((SV *)cv, PERL_MAGIC_coro) \ 305 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
299 : 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))
300 333
301/* the next two functions merely cache the padlists */ 334/* the next two functions merely cache the padlists */
302static void 335static void
303get_padlist (pTHX_ CV *cv) 336get_padlist (pTHX_ CV *cv)
304{ 337{
343 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 376 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
344} 377}
345 378
346/** load & save, init *******************************************************/ 379/** load & save, init *******************************************************/
347 380
348#define SB do {
349#define SE } while (0)
350
351#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
352
353static void 381static void
354load_perl (pTHX_ Coro__State c) 382load_perl (pTHX_ Coro__State c)
355{ 383{
356#define VAR(name,type) PL_ ## name = c->name; 384#define VAR(name,type) PL_ ## name = c->name;
357# include "state.h" 385# include "state.h"
358#undef VAR 386#undef VAR
359 387
360 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 388 GvSV (PL_defgv) = c->defsv;
361 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 389 GvAV (PL_defgv) = c->defav;
362 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 390 GvSV (PL_errgv) = c->errsv;
363 if (c->deffh) REPLACE_SV (PL_defoutgv , c->deffh); 391 GvIOp(PL_defoutgv) = c->deffh;
364 392 PL_rs = c->irssv;
365 if (c->irssv) 393 GvSV (irsgv) = c->irssv_sv;
366 {
367 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv))
368 {
369 SvREFCNT_dec (c->irssv);
370 c->irssv = 0;
371 }
372 else
373 {
374 REPLACE_SV (PL_rs, c->irssv);
375 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0);
376 sv_setsv (c->irssv_sv, PL_rs);
377 }
378 }
379 394
380 { 395 {
381 dSP; 396 dSP;
382 CV *cv; 397 CV *cv;
383 398
441 } 456 }
442 457
443 PUTBACK; 458 PUTBACK;
444 } 459 }
445 460
446 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 461 c->defav = GvAV (PL_defgv);
447 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 462 c->defsv = DEFSV;
448 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 463 c->errsv = ERRSV;
449 c->deffh = c->save & CORO_SAVE_DEFFH ? (GV *)SvREFCNT_inc (PL_defoutgv) : 0; 464 c->deffh = GvIOp (PL_defoutgv);
450 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; 465 c->irssv = PL_rs;
466 c->irssv_sv = GvSV (irsgv);
451 467
452#define VAR(name,type)c->name = PL_ ## name; 468#define VAR(name,type)c->name = PL_ ## name;
453# include "state.h" 469# include "state.h"
454#undef VAR 470#undef VAR
455} 471}
508 * destroy the stacks, the callchain etc... 524 * destroy the stacks, the callchain etc...
509 */ 525 */
510static void 526static void
511coro_destroy_stacks (pTHX) 527coro_destroy_stacks (pTHX)
512{ 528{
513 if (!IN_DESTRUCT)
514 {
515 /* restore all saved variables and stuff */
516 LEAVE_SCOPE (0);
517 assert (PL_tmps_floor == -1);
518
519 /* free all temporaries */
520 FREETMPS;
521 assert (PL_tmps_ix == -1);
522
523 /* unwind all extra stacks */
524 POPSTACK_TO (PL_mainstack);
525
526 /* unwind main stack */
527 dounwind (-1);
528 }
529
530 while (PL_curstackinfo->si_next) 529 while (PL_curstackinfo->si_next)
531 PL_curstackinfo = PL_curstackinfo->si_next; 530 PL_curstackinfo = PL_curstackinfo->si_next;
532 531
533 while (PL_curstackinfo) 532 while (PL_curstackinfo)
534 { 533 {
550 Safefree (PL_retstack); 549 Safefree (PL_retstack);
551#endif 550#endif
552} 551}
553 552
554static size_t 553static size_t
555coro_rss (struct coro *coro) 554coro_rss (pTHX_ struct coro *coro)
556{ 555{
557 size_t rss = sizeof (coro); 556 size_t rss = sizeof (coro);
558 557
559 if (coro->mainstack) 558 if (coro->mainstack)
560 { 559 {
590 /* 589 /*
591 * emulate part of the perl startup here. 590 * emulate part of the perl startup here.
592 */ 591 */
593 coro_init_stacks (aTHX); 592 coro_init_stacks (aTHX);
594 593
594 PL_runops = RUNOPS_DEFAULT;
595 PL_curcop = &PL_compiling; 595 PL_curcop = &PL_compiling;
596 PL_in_eval = EVAL_NULL; 596 PL_in_eval = EVAL_NULL;
597 PL_comppad = 0; 597 PL_comppad = 0;
598 PL_curpm = 0; 598 PL_curpm = 0;
599 PL_localizing = 0; 599 PL_localizing = 0;
600 PL_dirty = 0; 600 PL_dirty = 0;
601 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 }
602 616
603 { 617 {
604 dSP; 618 dSP;
605 LOGOP myop; 619 LOGOP myop;
606
607 SvREFCNT_dec (GvAV (PL_defgv));
608 GvAV (PL_defgv) = coro->args; coro->args = 0;
609 620
610 Zero (&myop, 1, LOGOP); 621 Zero (&myop, 1, LOGOP);
611 myop.op_next = Nullop; 622 myop.op_next = Nullop;
612 myop.op_flags = OPf_WANT_VOID; 623 myop.op_flags = OPf_WANT_VOID;
613 624
621 632
622 ENTER; /* necessary e.g. for dounwind */ 633 ENTER; /* necessary e.g. for dounwind */
623} 634}
624 635
625static 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
626free_coro_mortal (pTHX) 667free_coro_mortal (pTHX)
627{ 668{
628 if (coro_mortal) 669 if (coro_mortal)
629 { 670 {
630 SvREFCNT_dec (coro_mortal); 671 SvREFCNT_dec (coro_mortal);
631 coro_mortal = 0; 672 coro_mortal = 0;
632 } 673 }
633} 674}
634 675
635static void
636do_trace (pTHX)
637{
638 if (PL_curcop != &PL_compiling)
639 {
640 runops_proc_t old_runops = PL_runops;
641 dSP;
642 ENTER;
643 SAVETMPS;
644 PUSHMARK (SP);
645 PUTBACK;
646 PL_runops = RUNOPS_DEFAULT;
647 call_pv ("Coro::_do_trace", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD | G_NOARGS);
648 PL_runops = old_runops;
649 SPAGAIN;
650 FREETMPS;
651 LEAVE;
652 PUTBACK;
653 }
654}
655
656static int 676static int
657runops_coro (pTHX) 677runops_trace (pTHX)
658{ 678{
659 COP *oldcop = 0; 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;
660 683
661 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 684 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
662 { 685 {
663 PERL_ASYNC_CHECK (); 686 PERL_ASYNC_CHECK ();
664 687
665 if (oldcop != PL_curcop) 688 if (cctx->flags & CC_TRACE_ALL)
666 { 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 {
667 oldcop = PL_curcop; 730 oldcop = PL_curcop;
668 do_trace (aTHX); 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 }
669 } 792 }
670 } 793 }
671 794
672 TAINT_NOT; 795 TAINT_NOT;
673 return 0; 796 return 0;
683 LOGOP myop; 806 LOGOP myop;
684 807
685 PL_top_env = &PL_start_env; 808 PL_top_env = &PL_start_env;
686 809
687 if (cctx->flags & CC_TRACE) 810 if (cctx->flags & CC_TRACE)
688 PL_runops = runops_coro; 811 PL_runops = runops_trace;
689 812
690 Zero (&myop, 1, LOGOP); 813 Zero (&myop, 1, LOGOP);
691 myop.op_next = PL_op; 814 myop.op_next = PL_op;
692 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 815 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
693 816
839 cctx_first = cctx; 962 cctx_first = cctx;
840} 963}
841 964
842/** coroutine switching *****************************************************/ 965/** coroutine switching *****************************************************/
843 966
844/* 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 */
845static void NOINLINE 987static void NOINLINE
846transfer (pTHX_ struct coro *prev, struct coro *next) 988transfer (pTHX_ struct coro *prev, struct coro *next)
847{ 989{
848 dSTACKLEVEL; 990 dSTACKLEVEL;
849 991
862 /* create a new empty context */ 1004 /* create a new empty context */
863 Newz (0, prev->cctx, 1, coro_cctx); 1005 Newz (0, prev->cctx, 1, coro_cctx);
864 prev->flags &= ~CF_NEW; 1006 prev->flags &= ~CF_NEW;
865 prev->flags |= CF_RUNNING; 1007 prev->flags |= CF_RUNNING;
866 } 1008 }
867
868 /*TODO: must not croak here */
869 if (!prev->flags & CF_RUNNING)
870 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
871
872 if (next->flags & CF_RUNNING)
873 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
874
875 if (next->flags & CF_DESTROYED)
876 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
877 1009
878 prev->flags &= ~CF_RUNNING; 1010 prev->flags &= ~CF_RUNNING;
879 next->flags |= CF_RUNNING; 1011 next->flags |= CF_RUNNING;
880 1012
881 LOCK; 1013 LOCK;
897 } 1029 }
898 1030
899 prev__cctx = prev->cctx; 1031 prev__cctx = prev->cctx;
900 1032
901 /* possibly "free" the cctx */ 1033 /* possibly "free" the cctx */
902 if (prev__cctx->idle_sp == STACKLEVEL) 1034 if (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))
903 { 1035 {
904 /* 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 */
905 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));
906 1038
907 prev->cctx = 0; 1039 prev->cctx = 0;
908 1040
909 cctx_put (prev__cctx); 1041 cctx_put (prev__cctx);
910 } 1042 }
1043
1044 ++next->usecount;
911 1045
912 if (!next->cctx) 1046 if (!next->cctx)
913 next->cctx = cctx_get (aTHX); 1047 next->cctx = cctx_get (aTHX);
914 1048
915 if (prev__cctx != next->cctx) 1049 if (prev__cctx != next->cctx)
928{ 1062{
929 struct coro *prev, *next; 1063 struct coro *prev, *next;
930}; 1064};
931 1065
932#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)
933 1068
934/** high level stuff ********************************************************/ 1069/** high level stuff ********************************************************/
935 1070
936static int 1071static int
937coro_state_destroy (pTHX_ struct coro *coro) 1072coro_state_destroy (pTHX_ struct coro *coro)
954 1089
955 if (coro->mainstack && coro->mainstack != main_mainstack) 1090 if (coro->mainstack && coro->mainstack != main_mainstack)
956 { 1091 {
957 struct coro temp; 1092 struct coro temp;
958 1093
959 assert (!(coro->flags & CF_RUNNING));
960
961 Zero (&temp, 1, struct coro);
962 temp.save = CORO_SAVE_ALL;
963
964 if (coro->flags & CF_RUNNING) 1094 if (coro->flags & CF_RUNNING)
965 croak ("FATAL: tried to destroy currently running coroutine"); 1095 croak ("FATAL: tried to destroy currently running coroutine");
966 1096
967 save_perl (aTHX_ &temp); 1097 save_perl (aTHX_ &temp);
968 load_perl (aTHX_ coro); 1098 load_perl (aTHX_ coro);
969 1099
970 coro_destroy_stacks (aTHX); 1100 destroy_coro (aTHX_ coro);
971 1101
972 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ 1102 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */
973 1103
974 coro->mainstack = 0; 1104 coro->mainstack = 0;
975 } 1105 }
1020#else 1150#else
1021# define MGf_DUP 0 1151# define MGf_DUP 0
1022#endif 1152#endif
1023}; 1153};
1024 1154
1025static struct coro *
1026SvSTATE_ (pTHX_ SV *coro)
1027{
1028 HV *stash;
1029 MAGIC *mg;
1030
1031 if (SvROK (coro))
1032 coro = SvRV (coro);
1033
1034 stash = SvSTASH (coro);
1035 if (stash != coro_stash && stash != coro_state_stash)
1036 {
1037 /* very slow, but rare, check */
1038 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
1039 croak ("Coro::State object required");
1040 }
1041
1042 mg = CORO_MAGIC (coro);
1043 return (struct coro *)mg->mg_ptr;
1044}
1045
1046#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
1047
1048static void 1155static void
1049prepare_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)
1050{ 1157{
1051 ta->prev = SvSTATE (prev_sv); 1158 ta->prev = SvSTATE (prev_sv);
1052 ta->next = SvSTATE (next_sv); 1159 ta->next = SvSTATE (next_sv);
1160 TRANSFER_CHECK (*ta);
1053} 1161}
1054 1162
1055static void 1163static void
1056api_transfer (SV *prev_sv, SV *next_sv) 1164api_transfer (SV *prev_sv, SV *next_sv)
1057{ 1165{
1058 dTHX; 1166 dTHX;
1059 struct transfer_args ta; 1167 struct transfer_args ta;
1060 1168
1061 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1169 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1062 TRANSFER (ta); 1170 TRANSFER (ta);
1063}
1064
1065static int
1066api_save (SV *coro_sv, int new_save)
1067{
1068 dTHX;
1069 struct coro *coro = SvSTATE (coro_sv);
1070 int old_save = coro->save;
1071
1072 if (new_save >= 0)
1073 coro->save = new_save;
1074
1075 return old_save;
1076} 1171}
1077 1172
1078/** Coro ********************************************************************/ 1173/** Coro ********************************************************************/
1079 1174
1080static void 1175static void
1174 break; 1269 break;
1175 } 1270 }
1176 1271
1177 /* free this only after the transfer */ 1272 /* free this only after the transfer */
1178 prev_sv = SvRV (coro_current); 1273 prev_sv = SvRV (coro_current);
1179 SvRV_set (coro_current, next_sv);
1180 ta->prev = SvSTATE (prev_sv); 1274 ta->prev = SvSTATE (prev_sv);
1181 1275 TRANSFER_CHECK (*ta);
1182 assert (ta->next->flags & CF_READY); 1276 assert (ta->next->flags & CF_READY);
1183 ta->next->flags &= ~CF_READY; 1277 ta->next->flags &= ~CF_READY;
1278 SvRV_set (coro_current, next_sv);
1184 1279
1185 LOCK; 1280 LOCK;
1186 free_coro_mortal (aTHX); 1281 free_coro_mortal (aTHX);
1187 coro_mortal = prev_sv; 1282 coro_mortal = prev_sv;
1188 UNLOCK; 1283 UNLOCK;
1249 } 1344 }
1250 else 1345 else
1251 return 0; 1346 return 0;
1252} 1347}
1253 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
1254MODULE = Coro::State PACKAGE = Coro::State 1375MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1255 1376
1256PROTOTYPES: DISABLE 1377PROTOTYPES: DISABLE
1257 1378
1258BOOT: 1379BOOT:
1259{ 1380{
1260#ifdef USE_ITHREADS 1381#ifdef USE_ITHREADS
1261 MUTEX_INIT (&coro_mutex); 1382 MUTEX_INIT (&coro_mutex);
1262#endif 1383#endif
1263 BOOT_PAGESIZE; 1384 BOOT_PAGESIZE;
1264 1385
1386 irsgv = gv_fetchpv ("/", 1, SVt_PV);
1387
1265 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1388 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1266 1389
1267 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); 1390 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1268 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); 1391 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1269 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1270 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1271 newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH));
1272 newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF)); 1392 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1273 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); 1393 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1274 1394
1275 main_mainstack = PL_mainstack; 1395 main_mainstack = PL_mainstack;
1276 main_top_env = PL_top_env; 1396 main_top_env = PL_top_env;
1277 1397
1278 while (main_top_env->je_prev) 1398 while (main_top_env->je_prev)
1291 struct coro *coro; 1411 struct coro *coro;
1292 HV *hv; 1412 HV *hv;
1293 int i; 1413 int i;
1294 1414
1295 Newz (0, coro, 1, struct coro); 1415 Newz (0, coro, 1, struct coro);
1296 coro->args = newAV (); 1416 coro->args = newAV ();
1297 coro->save = CORO_SAVE_DEF;
1298 coro->flags = CF_NEW; 1417 coro->flags = CF_NEW;
1299 1418
1300 if (coro_first) coro_first->prev = coro; 1419 if (coro_first) coro_first->prev = coro;
1301 coro->next = coro_first; 1420 coro->next = coro_first;
1302 coro_first = coro; 1421 coro_first = coro;
1309 av_push (coro->args, newSVsv (ST (i))); 1428 av_push (coro->args, newSVsv (ST (i)));
1310} 1429}
1311 OUTPUT: 1430 OUTPUT:
1312 RETVAL 1431 RETVAL
1313 1432
1314int 1433# these not obviously related functions are all rolled into the same xs
1315save (SV *coro, int new_save = -1) 1434# function to increase chances that they all will call transfer with the same
1316 CODE: 1435# stack offset
1317 RETVAL = api_save (coro, new_save);
1318 OUTPUT:
1319 RETVAL
1320
1321int
1322save_also (SV *coro_sv, int save_also)
1323 CODE:
1324{
1325 struct coro *coro = SvSTATE (coro_sv);
1326 RETVAL = coro->save;
1327 coro->save |= save_also;
1328}
1329 OUTPUT:
1330 RETVAL
1331
1332void 1436void
1333_set_stacklevel (...) 1437_set_stacklevel (...)
1334 ALIAS: 1438 ALIAS:
1335 Coro::State::transfer = 1 1439 Coro::State::transfer = 1
1336 Coro::schedule = 2 1440 Coro::schedule = 2
1431{ 1535{
1432 if (coro->mainstack) 1536 if (coro->mainstack)
1433 { 1537 {
1434 struct coro temp; 1538 struct coro temp;
1435 Zero (&temp, 1, struct coro); 1539 Zero (&temp, 1, struct coro);
1436 temp.save = CORO_SAVE_ALL;
1437 1540
1438 if (!(coro->flags & CF_RUNNING)) 1541 if (!(coro->flags & CF_RUNNING))
1439 { 1542 {
1440 save_perl (aTHX_ &temp); 1543 save_perl (aTHX_ &temp);
1441 load_perl (aTHX_ coro); 1544 load_perl (aTHX_ coro);
1462 save_perl (aTHX_ coro); 1565 save_perl (aTHX_ coro);
1463 load_perl (aTHX_ &temp); 1566 load_perl (aTHX_ &temp);
1464 } 1567 }
1465 } 1568 }
1466} 1569}
1467 1570
1468SV * 1571SV *
1469is_ready (Coro::State coro) 1572is_ready (Coro::State coro)
1470 PROTOTYPE: $ 1573 PROTOTYPE: $
1471 ALIAS: 1574 ALIAS:
1472 is_ready = CF_READY 1575 is_ready = CF_READY
1477 RETVAL = boolSV (coro->flags & ix); 1580 RETVAL = boolSV (coro->flags & ix);
1478 OUTPUT: 1581 OUTPUT:
1479 RETVAL 1582 RETVAL
1480 1583
1481void 1584void
1482trace (Coro::State coro, int enable = 0) 1585api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
1483 CODE:
1484 if (enable)
1485 {
1486 if (coro->flags & CF_RUNNING)
1487 croak ("cannot enable tracing on running coroutine");
1488
1489 if (coro->cctx)
1490 croak ("cannot enable tracing on coroutine with custom stack");
1491
1492 coro->cctx = cctx_new ();
1493 coro->cctx->flags |= CC_TRACE;
1494 }
1495 else
1496 if (coro->cctx && coro->cctx->flags & CC_TRACE)
1497 {
1498 coro->cctx->flags &= ~CC_TRACE;
1499 coro->cctx->flags |= CC_NOREUSE;
1500 }
1501 1586
1502SV * 1587SV *
1503has_stack (Coro::State coro) 1588has_stack (Coro::State coro)
1504 PROTOTYPE: $ 1589 PROTOTYPE: $
1505 CODE: 1590 CODE:
1506 RETVAL = boolSV (!!coro->cctx); 1591 RETVAL = boolSV (!!coro->cctx);
1507 OUTPUT: 1592 OUTPUT:
1508 RETVAL 1593 RETVAL
1509 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
1510IV 1603IV
1511rss (Coro::State coro) 1604rss (Coro::State coro)
1512 PROTOTYPE: $ 1605 PROTOTYPE: $
1606 ALIAS:
1607 usecount = 1
1513 CODE: 1608 CODE:
1514 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 }
1515 OUTPUT: 1614 OUTPUT:
1516 RETVAL 1615 RETVAL
1517 1616
1518 1617
1519MODULE = Coro::State PACKAGE = Coro 1618MODULE = Coro::State PACKAGE = Coro
1543 1642
1544 { 1643 {
1545 SV *sv = perl_get_sv("Coro::API", 1); 1644 SV *sv = perl_get_sv("Coro::API", 1);
1546 1645
1547 coroapi.schedule = api_schedule; 1646 coroapi.schedule = api_schedule;
1548 coroapi.save = api_save;
1549 coroapi.cede = api_cede; 1647 coroapi.cede = api_cede;
1550 coroapi.cede_notself = api_cede_notself; 1648 coroapi.cede_notself = api_cede_notself;
1551 coroapi.ready = api_ready; 1649 coroapi.ready = api_ready;
1552 coroapi.is_ready = api_is_ready; 1650 coroapi.is_ready = api_is_ready;
1553 coroapi.nready = &coro_nready; 1651 coroapi.nready = &coro_nready;
1642{ 1740{
1643 struct coro *coro = SvSTATE (coro_current); 1741 struct coro *coro = SvSTATE (coro_current);
1644 1742
1645 sv_setsv (cb, &PL_sv_undef); 1743 sv_setsv (cb, &PL_sv_undef);
1646 1744
1647 if (coro_rss (coro) > SvIV (sv_pool_rss) 1745 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1648 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 1746 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1649 croak ("\3terminate\2\n"); 1747 croak ("\3terminate\2\n");
1650 1748
1651 av_clear (GvAV (PL_defgv)); 1749 av_clear (GvAV (PL_defgv));
1652 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, 1750 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1653 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); 1751 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1654 coro->save = CORO_SAVE_DEF; 1752
1655 coro->prio = 0; 1753 coro->prio = 0;
1754
1755 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
1756 api_trace (coro_current, 0);
1757
1656 av_push (av_async_pool, newSVsv (coro_current)); 1758 av_push (av_async_pool, newSVsv (coro_current));
1657} 1759}
1658 1760
1659 1761
1660MODULE = Coro::State PACKAGE = Coro::AIO 1762MODULE = Coro::State PACKAGE = Coro::AIO

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines