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.156 by root, Fri Sep 21 01:10:55 2007 UTC vs.
Revision 1.177 by root, Tue Oct 2 10:24:44 2007 UTC

88/* prefer perl internal functions over our own? */ 88/* prefer perl internal functions over our own? */
89#ifndef CORO_PREFER_PERL_FUNCTIONS 89#ifndef CORO_PREFER_PERL_FUNCTIONS
90# define CORO_PREFER_PERL_FUNCTIONS 0 90# define CORO_PREFER_PERL_FUNCTIONS 0
91#endif 91#endif
92 92
93/* The next macro should declare a variable stacklevel that contains and approximation 93/* The next macros try to return the current stack pointer, in an as
94 * to the current C stack pointer. Its property is that it changes with each call 94 * portable way as possible. */
95 * and should be unique. */
96#define dSTACKLEVEL int stacklevel 95#define dSTACKLEVEL volatile char stacklevel
97#define STACKLEVEL ((void *)&stacklevel) 96#define STACKLEVEL ((void *)&stacklevel)
98 97
99#define IN_DESTRUCT (PL_main_cv == Nullcv) 98#define IN_DESTRUCT (PL_main_cv == Nullcv)
100 99
101#if __GNUC__ >= 3 100#if __GNUC__ >= 3
133static AV *main_mainstack; /* used to differentiate between $main and others */ 132static AV *main_mainstack; /* used to differentiate between $main and others */
134static JMPENV *main_top_env; 133static JMPENV *main_top_env;
135static HV *coro_state_stash, *coro_stash; 134static HV *coro_state_stash, *coro_stash;
136static SV *coro_mortal; /* will be freed after next transfer */ 135static SV *coro_mortal; /* will be freed after next transfer */
137 136
137/* async_pool helper stuff */
138static SV *sv_pool_rss;
139static SV *sv_pool_size;
140static AV *av_async_pool;
141
138static struct coro_cctx *cctx_first; 142static struct coro_cctx *cctx_first;
139static int cctx_count, cctx_idle; 143static int cctx_count, cctx_idle;
144
145enum {
146 CC_MAPPED = 0x01,
147 CC_NOREUSE = 0x02, /* throw this away after tracing */
148 CC_TRACE = 0x04,
149 CC_TRACE_SUB = 0x08, /* trace sub calls */
150 CC_TRACE_LINE = 0x10, /* trace each statement */
151 CC_TRACE_ALL = CC_TRACE_SUB | CC_TRACE_LINE,
152};
140 153
141/* this is a structure representing a c-level coroutine */ 154/* this is a structure representing a c-level coroutine */
142typedef struct coro_cctx { 155typedef struct coro_cctx {
143 struct coro_cctx *next; 156 struct coro_cctx *next;
144 157
153 coro_context cctx; 166 coro_context cctx;
154 167
155#if CORO_USE_VALGRIND 168#if CORO_USE_VALGRIND
156 int valgrind_id; 169 int valgrind_id;
157#endif 170#endif
158 char inuse, mapped; 171 unsigned char flags;
159} coro_cctx; 172} coro_cctx;
160 173
161enum { 174enum {
162 CF_RUNNING = 0x0001, /* coroutine is running */ 175 CF_RUNNING = 0x0001, /* coroutine is running */
163 CF_READY = 0x0002, /* coroutine is ready */ 176 CF_READY = 0x0002, /* coroutine is ready */
186 199
187#define VAR(name,type) type name; 200#define VAR(name,type) type name;
188# include "state.h" 201# include "state.h"
189#undef VAR 202#undef VAR
190 203
204 /* statistics */
205 int usecount; /* number of switches to this coro */
206
191 /* coro process data */ 207 /* coro process data */
192 int prio; 208 int prio;
193 209
194 /* linked list */ 210 /* linked list */
195 struct coro *next, *prev; 211 struct coro *next, *prev;
285 SvMAGIC (cv) \ 301 SvMAGIC (cv) \
286 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ 302 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \
287 ? SvMAGIC (cv) \ 303 ? SvMAGIC (cv) \
288 : mg_find ((SV *)cv, PERL_MAGIC_coro) \ 304 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
289 : 0 305 : 0
306
307static struct coro *
308SvSTATE_ (pTHX_ SV *coro)
309{
310 HV *stash;
311 MAGIC *mg;
312
313 if (SvROK (coro))
314 coro = SvRV (coro);
315
316 if (SvTYPE (coro) != SVt_PVHV)
317 croak ("Coro::State object required");
318
319 stash = SvSTASH (coro);
320 if (stash != coro_stash && stash != coro_state_stash)
321 {
322 /* very slow, but rare, check */
323 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
324 croak ("Coro::State object required");
325 }
326
327 mg = CORO_MAGIC (coro);
328 return (struct coro *)mg->mg_ptr;
329}
330
331#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
290 332
291/* the next two functions merely cache the padlists */ 333/* the next two functions merely cache the padlists */
292static void 334static void
293get_padlist (pTHX_ CV *cv) 335get_padlist (pTHX_ CV *cv)
294{ 336{
395 /* 437 /*
396 * the worst thing you can imagine happens first - we have to save 438 * the worst thing you can imagine happens first - we have to save
397 * (and reinitialize) all cv's in the whole callchain :( 439 * (and reinitialize) all cv's in the whole callchain :(
398 */ 440 */
399 441
400 EXTEND (SP, 3 + 1);
401 PUSHs (Nullsv); 442 XPUSHs (Nullsv);
402 /* this loop was inspired by pp_caller */ 443 /* this loop was inspired by pp_caller */
403 for (;;) 444 for (;;)
404 { 445 {
405 while (cxix >= 0) 446 while (cxix >= 0)
406 { 447 {
425 466
426 if (top_si->si_type == PERLSI_MAIN) 467 if (top_si->si_type == PERLSI_MAIN)
427 break; 468 break;
428 469
429 top_si = top_si->si_prev; 470 top_si = top_si->si_prev;
430 ccstk = top_si->si_cxstack; 471 ccstk = top_si->si_cxstack;
431 cxix = top_si->si_cxix; 472 cxix = top_si->si_cxix;
432 } 473 }
433 474
434 PUTBACK; 475 PUTBACK;
435 } 476 }
436 477
541 Safefree (PL_retstack); 582 Safefree (PL_retstack);
542#endif 583#endif
543} 584}
544 585
545static size_t 586static size_t
546coro_rss (struct coro *coro) 587coro_rss (pTHX_ struct coro *coro)
547{ 588{
548 size_t rss = sizeof (coro); 589 size_t rss = sizeof (coro);
549 590
550 if (coro->mainstack) 591 if (coro->mainstack)
551 { 592 {
581 /* 622 /*
582 * emulate part of the perl startup here. 623 * emulate part of the perl startup here.
583 */ 624 */
584 coro_init_stacks (aTHX); 625 coro_init_stacks (aTHX);
585 626
627 PL_runops = RUNOPS_DEFAULT;
586 PL_curcop = &PL_compiling; 628 PL_curcop = &PL_compiling;
587 PL_in_eval = EVAL_NULL; 629 PL_in_eval = EVAL_NULL;
588 PL_comppad = 0; 630 PL_comppad = 0;
589 PL_curpm = 0; 631 PL_curpm = 0;
590 PL_localizing = 0; 632 PL_localizing = 0;
621 SvREFCNT_dec (coro_mortal); 663 SvREFCNT_dec (coro_mortal);
622 coro_mortal = 0; 664 coro_mortal = 0;
623 } 665 }
624} 666}
625 667
668static int
669runops_trace (pTHX)
670{
671 COP *oldcop = 0;
672 int oldcxix = -2;
673 struct coro *coro = SvSTATE (coro_current); /* trace cctx is tied to specific coro */
674 coro_cctx *cctx = coro->cctx;
675
676 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
677 {
678 PERL_ASYNC_CHECK ();
679
680 if (cctx->flags & CC_TRACE_ALL)
681 {
682 if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB)
683 {
684 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
685 SV **bot, **top;
686 AV *av = newAV (); /* return values */
687 SV **cb;
688 dSP;
689
690 GV *gv = CvGV (cx->blk_sub.cv);
691 SV *fullname = sv_2mortal (newSV (0));
692 if (isGV (gv))
693 gv_efullname3 (fullname, gv, 0);
694
695 bot = PL_stack_base + cx->blk_oldsp + 1;
696 top = cx->blk_gimme == G_ARRAY ? SP + 1
697 : cx->blk_gimme == G_SCALAR ? bot + 1
698 : bot;
699
700 while (bot < top)
701 av_push (av, SvREFCNT_inc (*bot++));
702
703 PL_runops = RUNOPS_DEFAULT;
704 ENTER;
705 SAVETMPS;
706 EXTEND (SP, 3);
707 PUSHMARK (SP);
708 PUSHs (&PL_sv_no);
709 PUSHs (fullname);
710 PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
711 PUTBACK;
712 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
713 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
714 SPAGAIN;
715 FREETMPS;
716 LEAVE;
717 PL_runops = runops_trace;
718 }
719
720 if (oldcop != PL_curcop)
721 {
722 oldcop = PL_curcop;
723
724 if (PL_curcop != &PL_compiling)
725 {
726 SV **cb;
727
728 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB)
729 {
730 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
731
732 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
733 {
734 runops_proc_t old_runops = PL_runops;
735 dSP;
736 GV *gv = CvGV (cx->blk_sub.cv);
737 SV *fullname = sv_2mortal (newSV (0));
738
739 if (isGV (gv))
740 gv_efullname3 (fullname, gv, 0);
741
742 PL_runops = RUNOPS_DEFAULT;
743 ENTER;
744 SAVETMPS;
745 EXTEND (SP, 3);
746 PUSHMARK (SP);
747 PUSHs (&PL_sv_yes);
748 PUSHs (fullname);
749 PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
750 PUTBACK;
751 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
752 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
753 SPAGAIN;
754 FREETMPS;
755 LEAVE;
756 PL_runops = runops_trace;
757 }
758
759 oldcxix = cxstack_ix;
760 }
761
762 if (cctx->flags & CC_TRACE_LINE)
763 {
764 dSP;
765
766 PL_runops = RUNOPS_DEFAULT;
767 ENTER;
768 SAVETMPS;
769 EXTEND (SP, 3);
770 PL_runops = RUNOPS_DEFAULT;
771 PUSHMARK (SP);
772 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0)));
773 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop))));
774 PUTBACK;
775 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0);
776 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
777 SPAGAIN;
778 FREETMPS;
779 LEAVE;
780 PL_runops = runops_trace;
781 }
782 }
783 }
784 }
785 }
786
787 TAINT_NOT;
788 return 0;
789}
790
626/* inject a fake call to Coro::State::_cctx_init into the execution */ 791/* inject a fake call to Coro::State::_cctx_init into the execution */
627/* _cctx_init should be careful, as it could be called at almost any time */ 792/* _cctx_init should be careful, as it could be called at almost any time */
628/* during execution of a perl program */ 793/* during execution of a perl program */
629static void NOINLINE 794static void NOINLINE
630prepare_cctx (pTHX_ coro_cctx *cctx) 795prepare_cctx (pTHX_ coro_cctx *cctx)
631{ 796{
632 dSP; 797 dSP;
633 LOGOP myop; 798 LOGOP myop;
799
800 PL_top_env = &PL_start_env;
801
802 if (cctx->flags & CC_TRACE)
803 PL_runops = runops_trace;
634 804
635 Zero (&myop, 1, LOGOP); 805 Zero (&myop, 1, LOGOP);
636 myop.op_next = PL_op; 806 myop.op_next = PL_op;
637 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 807 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
638 808
655 dTHX; 825 dTHX;
656 826
657 /* coro_run is the alternative tail of transfer(), so unlock here. */ 827 /* coro_run is the alternative tail of transfer(), so unlock here. */
658 UNLOCK; 828 UNLOCK;
659 829
660 PL_top_env = &PL_start_env; 830 /* we now skip the entersub that lead to transfer() */
831 PL_op = PL_op->op_next;
661 832
662 /* inject a fake subroutine call to cctx_init */ 833 /* inject a fake subroutine call to cctx_init */
663 prepare_cctx (aTHX_ (coro_cctx *)arg); 834 prepare_cctx (aTHX_ (coro_cctx *)arg);
664 835
665 /* somebody or something will hit me for both perl_run and PL_restartop */ 836 /* somebody or something will hit me for both perl_run and PL_restartop */
699# if CORO_STACKGUARD 870# if CORO_STACKGUARD
700 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 871 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
701# endif 872# endif
702 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 873 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr;
703 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 874 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
704 cctx->mapped = 1; 875 cctx->flags |= CC_MAPPED;
705 } 876 }
706 else 877 else
707#endif 878#endif
708 { 879 {
709 cctx->ssize = coro_stacksize * (long)sizeof (long); 880 cctx->ssize = coro_stacksize * (long)sizeof (long);
736#if CORO_USE_VALGRIND 907#if CORO_USE_VALGRIND
737 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 908 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
738#endif 909#endif
739 910
740#if HAVE_MMAP 911#if HAVE_MMAP
741 if (cctx->mapped) 912 if (cctx->flags & CC_MAPPED)
742 munmap (cctx->sptr, cctx->ssize); 913 munmap (cctx->sptr, cctx->ssize);
743 else 914 else
744#endif 915#endif
745 Safefree (cctx->sptr); 916 Safefree (cctx->sptr);
746 917
754 { 925 {
755 coro_cctx *cctx = cctx_first; 926 coro_cctx *cctx = cctx_first;
756 cctx_first = cctx->next; 927 cctx_first = cctx->next;
757 --cctx_idle; 928 --cctx_idle;
758 929
759 if (cctx->ssize >= coro_stacksize) 930 if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE))
760 return cctx; 931 return cctx;
761 932
762 cctx_destroy (cctx); 933 cctx_destroy (cctx);
763 } 934 }
764 935
765 PL_op = PL_op->op_next;
766 return cctx_new (); 936 return cctx_new ();
767} 937}
768 938
769static void 939static void
770cctx_put (coro_cctx *cctx) 940cctx_put (coro_cctx *cctx)
774 { 944 {
775 coro_cctx *first = cctx_first; 945 coro_cctx *first = cctx_first;
776 cctx_first = first->next; 946 cctx_first = first->next;
777 --cctx_idle; 947 --cctx_idle;
778 948
779 assert (!first->inuse);
780 cctx_destroy (first); 949 cctx_destroy (first);
781 } 950 }
782 951
783 ++cctx_idle; 952 ++cctx_idle;
784 cctx->next = cctx_first; 953 cctx->next = cctx_first;
785 cctx_first = cctx; 954 cctx_first = cctx;
786} 955}
787 956
788/** coroutine switching *****************************************************/ 957/** coroutine switching *****************************************************/
789 958
790/* never call directly, always through the coro_state_transfer global variable */ 959static void NOINLINE
960transfer_check (pTHX_ struct coro *prev, struct coro *next)
961{
962 if (prev != next)
963 {
964 if (!(prev->flags & (CF_RUNNING | CF_NEW)))
965 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states");
966
967 if (next->flags & CF_RUNNING)
968 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
969
970 if (next->flags & CF_DESTROYED)
971 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
972
973 if (PL_lex_state != LEX_NOTPARSING)
974 croak ("Coro::State::transfer called while parsing, but this is not supported");
975 }
976}
977
978/* always use the TRANSFER macro */
791static void NOINLINE 979static void NOINLINE
792transfer (pTHX_ struct coro *prev, struct coro *next) 980transfer (pTHX_ struct coro *prev, struct coro *next)
793{ 981{
794 dSTACKLEVEL; 982 dSTACKLEVEL;
795 983
805 993
806 if (prev->flags & CF_NEW) 994 if (prev->flags & CF_NEW)
807 { 995 {
808 /* create a new empty context */ 996 /* create a new empty context */
809 Newz (0, prev->cctx, 1, coro_cctx); 997 Newz (0, prev->cctx, 1, coro_cctx);
810 prev->cctx->inuse = 1;
811 prev->flags &= ~CF_NEW; 998 prev->flags &= ~CF_NEW;
812 prev->flags |= CF_RUNNING; 999 prev->flags |= CF_RUNNING;
813 } 1000 }
814
815 /*TODO: must not croak here */
816 if (!prev->flags & CF_RUNNING)
817 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
818
819 if (next->flags & CF_RUNNING)
820 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
821
822 if (next->flags & CF_DESTROYED)
823 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
824 1001
825 prev->flags &= ~CF_RUNNING; 1002 prev->flags &= ~CF_RUNNING;
826 next->flags |= CF_RUNNING; 1003 next->flags |= CF_RUNNING;
827 1004
828 LOCK; 1005 LOCK;
833 next->flags &= ~CF_NEW; 1010 next->flags &= ~CF_NEW;
834 /* first get rid of the old state */ 1011 /* first get rid of the old state */
835 save_perl (aTHX_ prev); 1012 save_perl (aTHX_ prev);
836 /* setup coroutine call */ 1013 /* setup coroutine call */
837 setup_coro (aTHX_ next); 1014 setup_coro (aTHX_ next);
838 /* need a new stack */
839 assert (!next->cctx);
840 } 1015 }
841 else 1016 else
842 { 1017 {
843 /* coroutine already started */ 1018 /* coroutine already started */
844 save_perl (aTHX_ prev); 1019 save_perl (aTHX_ prev);
846 } 1021 }
847 1022
848 prev__cctx = prev->cctx; 1023 prev__cctx = prev->cctx;
849 1024
850 /* possibly "free" the cctx */ 1025 /* possibly "free" the cctx */
851 if (prev__cctx->idle_sp == STACKLEVEL) 1026 if (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))
852 { 1027 {
853 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1028 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
854 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); 1029 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
855 1030
856 prev->cctx = 0; 1031 prev->cctx = 0;
857 1032
858 cctx_put (prev__cctx); 1033 cctx_put (prev__cctx);
859 prev__cctx->inuse = 0;
860 } 1034 }
861 1035
1036 ++next->usecount;
1037
862 if (!next->cctx) 1038 if (!next->cctx)
863 {
864 next->cctx = cctx_get (aTHX); 1039 next->cctx = cctx_get (aTHX);
865 assert (!next->cctx->inuse);
866 next->cctx->inuse = 1;
867 }
868 1040
869 if (prev__cctx != next->cctx) 1041 if (prev__cctx != next->cctx)
870 { 1042 {
871 prev__cctx->top_env = PL_top_env; 1043 prev__cctx->top_env = PL_top_env;
872 PL_top_env = next->cctx->top_env; 1044 PL_top_env = next->cctx->top_env;
882{ 1054{
883 struct coro *prev, *next; 1055 struct coro *prev, *next;
884}; 1056};
885 1057
886#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) 1058#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next)
1059#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
887 1060
888/** high level stuff ********************************************************/ 1061/** high level stuff ********************************************************/
889 1062
890static int 1063static int
891coro_state_destroy (pTHX_ struct coro *coro) 1064coro_state_destroy (pTHX_ struct coro *coro)
908 1081
909 if (coro->mainstack && coro->mainstack != main_mainstack) 1082 if (coro->mainstack && coro->mainstack != main_mainstack)
910 { 1083 {
911 struct coro temp; 1084 struct coro temp;
912 1085
913 assert (!(coro->flags & CF_RUNNING)); 1086 if (coro->flags & CF_RUNNING)
1087 croak ("FATAL: tried to destroy currently running coroutine");
914 1088
915 Zero (&temp, 1, struct coro); 1089 Zero (&temp, 1, struct coro);
916 temp.save = CORO_SAVE_ALL; 1090 temp.save = CORO_SAVE_ALL;
917
918 if (coro->flags & CF_RUNNING)
919 croak ("FATAL: tried to destroy currently running coroutine");
920 1091
921 save_perl (aTHX_ &temp); 1092 save_perl (aTHX_ &temp);
922 load_perl (aTHX_ coro); 1093 load_perl (aTHX_ coro);
923 1094
924 coro_destroy_stacks (aTHX); 1095 coro_destroy_stacks (aTHX);
974#else 1145#else
975# define MGf_DUP 0 1146# define MGf_DUP 0
976#endif 1147#endif
977}; 1148};
978 1149
979static struct coro *
980SvSTATE_ (pTHX_ SV *coro)
981{
982 HV *stash;
983 MAGIC *mg;
984
985 if (SvROK (coro))
986 coro = SvRV (coro);
987
988 stash = SvSTASH (coro);
989 if (stash != coro_stash && stash != coro_state_stash)
990 {
991 /* very slow, but rare, check */
992 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
993 croak ("Coro::State object required");
994 }
995
996 mg = CORO_MAGIC (coro);
997 return (struct coro *)mg->mg_ptr;
998}
999
1000#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
1001
1002static void 1150static void
1003prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) 1151prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv)
1004{ 1152{
1005 ta->prev = SvSTATE (prev_sv); 1153 ta->prev = SvSTATE (prev_sv);
1006 ta->next = SvSTATE (next_sv); 1154 ta->next = SvSTATE (next_sv);
1155 TRANSFER_CHECK (*ta);
1007} 1156}
1008 1157
1009static void 1158static void
1010api_transfer (SV *prev_sv, SV *next_sv) 1159api_transfer (SV *prev_sv, SV *next_sv)
1011{ 1160{
1128 break; 1277 break;
1129 } 1278 }
1130 1279
1131 /* free this only after the transfer */ 1280 /* free this only after the transfer */
1132 prev_sv = SvRV (coro_current); 1281 prev_sv = SvRV (coro_current);
1133 SvRV_set (coro_current, next_sv);
1134 ta->prev = SvSTATE (prev_sv); 1282 ta->prev = SvSTATE (prev_sv);
1135 1283 TRANSFER_CHECK (*ta);
1136 assert (ta->next->flags & CF_READY); 1284 assert (ta->next->flags & CF_READY);
1137 ta->next->flags &= ~CF_READY; 1285 ta->next->flags &= ~CF_READY;
1286 SvRV_set (coro_current, next_sv);
1138 1287
1139 LOCK; 1288 LOCK;
1140 free_coro_mortal (aTHX); 1289 free_coro_mortal (aTHX);
1141 coro_mortal = prev_sv; 1290 coro_mortal = prev_sv;
1142 UNLOCK; 1291 UNLOCK;
1203 } 1352 }
1204 else 1353 else
1205 return 0; 1354 return 0;
1206} 1355}
1207 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
1208MODULE = Coro::State PACKAGE = Coro::State 1383MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1209 1384
1210PROTOTYPES: DISABLE 1385PROTOTYPES: DISABLE
1211 1386
1212BOOT: 1387BOOT:
1213{ 1388{
1215 MUTEX_INIT (&coro_mutex); 1390 MUTEX_INIT (&coro_mutex);
1216#endif 1391#endif
1217 BOOT_PAGESIZE; 1392 BOOT_PAGESIZE;
1218 1393
1219 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1394 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1395
1396 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1397 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1398 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1399 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1220 1400
1221 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); 1401 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1222 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); 1402 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1223 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); 1403 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1224 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); 1404 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1281 coro->save |= save_also; 1461 coro->save |= save_also;
1282} 1462}
1283 OUTPUT: 1463 OUTPUT:
1284 RETVAL 1464 RETVAL
1285 1465
1466# these not obviously related functions are all rolled into the same xs
1467# function to increase chances that they all will call transfer with the same
1468# stack offset
1286void 1469void
1287_set_stacklevel (...) 1470_set_stacklevel (...)
1288 ALIAS: 1471 ALIAS:
1289 Coro::State::transfer = 1 1472 Coro::State::transfer = 1
1290 Coro::schedule = 2 1473 Coro::schedule = 2
1376 if (coro->hv) 1559 if (coro->hv)
1377 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); 1560 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
1378} 1561}
1379 1562
1380void 1563void
1381_eval (Coro::State coro, SV *coderef) 1564call (Coro::State coro, SV *coderef)
1565 ALIAS:
1566 eval = 1
1382 CODE: 1567 CODE:
1383{ 1568{
1384 if (coro->mainstack) 1569 if (coro->mainstack)
1385 { 1570 {
1386 struct coro temp; 1571 struct coro temp;
1397 dSP; 1582 dSP;
1398 ENTER; 1583 ENTER;
1399 SAVETMPS; 1584 SAVETMPS;
1400 PUSHMARK (SP); 1585 PUSHMARK (SP);
1401 PUTBACK; 1586 PUTBACK;
1587 if (ix)
1588 eval_sv (coderef, 0);
1589 else
1402 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1590 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1403 SPAGAIN; 1591 SPAGAIN;
1404 FREETMPS; 1592 FREETMPS;
1405 LEAVE; 1593 LEAVE;
1406 PUTBACK; 1594 PUTBACK;
1407 } 1595 }
1411 save_perl (aTHX_ coro); 1599 save_perl (aTHX_ coro);
1412 load_perl (aTHX_ &temp); 1600 load_perl (aTHX_ &temp);
1413 } 1601 }
1414 } 1602 }
1415} 1603}
1416 1604
1417SV * 1605SV *
1418is_ready (Coro::State coro) 1606is_ready (Coro::State coro)
1419 PROTOTYPE: $ 1607 PROTOTYPE: $
1420 ALIAS: 1608 ALIAS:
1421 is_ready = CF_READY 1609 is_ready = CF_READY
1425 CODE: 1613 CODE:
1426 RETVAL = boolSV (coro->flags & ix); 1614 RETVAL = boolSV (coro->flags & ix);
1427 OUTPUT: 1615 OUTPUT:
1428 RETVAL 1616 RETVAL
1429 1617
1618void
1619api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
1620
1621SV *
1622has_stack (Coro::State coro)
1623 PROTOTYPE: $
1624 CODE:
1625 RETVAL = boolSV (!!coro->cctx);
1626 OUTPUT:
1627 RETVAL
1628
1629int
1630is_traced (Coro::State coro)
1631 PROTOTYPE: $
1632 CODE:
1633 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1634 OUTPUT:
1635 RETVAL
1636
1430IV 1637IV
1431rss (Coro::State coro) 1638rss (Coro::State coro)
1432 PROTOTYPE: $ 1639 PROTOTYPE: $
1640 ALIAS:
1641 usecount = 1
1433 CODE: 1642 CODE:
1434 RETVAL = coro_rss (coro); 1643 switch (ix)
1644 {
1645 case 0: RETVAL = coro_rss (aTHX_ coro); break;
1646 case 1: RETVAL = coro->usecount; break;
1647 }
1435 OUTPUT: 1648 OUTPUT:
1436 RETVAL 1649 RETVAL
1437 1650
1438 1651
1439MODULE = Coro::State PACKAGE = Coro 1652MODULE = Coro::State PACKAGE = Coro
1440 1653
1441BOOT: 1654BOOT:
1442{ 1655{
1443 int i; 1656 int i;
1657
1658 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE);
1659 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE);
1660 av_async_pool = get_av ("Coro::async_pool", TRUE);
1661
1662 coro_current = get_sv ("Coro::current", FALSE);
1663 SvREADONLY_on (coro_current);
1444 1664
1445 coro_stash = gv_stashpv ("Coro", TRUE); 1665 coro_stash = gv_stashpv ("Coro", TRUE);
1446 1666
1447 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1667 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1448 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 1668 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1449 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1669 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1450 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1670 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1451 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1671 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1452 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1672 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1453
1454 coro_current = get_sv ("Coro::current", FALSE);
1455 SvREADONLY_on (coro_current);
1456 1673
1457 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1674 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1458 coro_ready[i] = newAV (); 1675 coro_ready[i] = newAV ();
1459 1676
1460 { 1677 {
1518 CODE: 1735 CODE:
1519 RETVAL = coro_nready; 1736 RETVAL = coro_nready;
1520 OUTPUT: 1737 OUTPUT:
1521 RETVAL 1738 RETVAL
1522 1739
1740# for async_pool speedup
1741void
1742_pool_1 (SV *cb)
1743 CODE:
1744{
1745 int i, len;
1746 HV *hv = (HV *)SvRV (coro_current);
1747 AV *defav = GvAV (PL_defgv);
1748 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1749 AV *invoke_av;
1750
1751 if (!invoke)
1752 croak ("\3terminate\2\n");
1753
1754 hv_store (hv, "desc", sizeof ("desc") - 1,
1755 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1756
1757 invoke_av = (AV *)SvRV (invoke);
1758 len = av_len (invoke_av);
1759
1760 sv_setsv (cb, AvARRAY (invoke_av)[0]);
1761
1762 if (len > 0)
1763 {
1764 av_fill (defav, len - 1);
1765 for (i = 0; i < len; ++i)
1766 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1]));
1767 }
1768
1769 SvREFCNT_dec (invoke);
1770}
1771
1772void
1773_pool_2 (SV *cb)
1774 CODE:
1775{
1776 struct coro *coro = SvSTATE (coro_current);
1777
1778 sv_setsv (cb, &PL_sv_undef);
1779
1780 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1781 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1782 croak ("\3terminate\2\n");
1783
1784 av_clear (GvAV (PL_defgv));
1785 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1786 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1787
1788 coro->save = CORO_SAVE_DEF;
1789 coro->prio = 0;
1790
1791 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
1792 api_trace (coro_current, 0);
1793
1794 av_push (av_async_pool, newSVsv (coro_current));
1795}
1796
1797
1523MODULE = Coro::State PACKAGE = Coro::AIO 1798MODULE = Coro::State PACKAGE = Coro::AIO
1524 1799
1525SV * 1800SV *
1526_get_state () 1801_get_state ()
1527 CODE: 1802 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines