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.157 by root, Fri Sep 21 02:01:13 2007 UTC vs.
Revision 1.174 by root, Sun Sep 30 13:43:18 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{
424 466
425 if (top_si->si_type == PERLSI_MAIN) 467 if (top_si->si_type == PERLSI_MAIN)
426 break; 468 break;
427 469
428 top_si = top_si->si_prev; 470 top_si = top_si->si_prev;
429 ccstk = top_si->si_cxstack; 471 ccstk = top_si->si_cxstack;
430 cxix = top_si->si_cxix; 472 cxix = top_si->si_cxix;
431 } 473 }
432 474
433 PUTBACK; 475 PUTBACK;
434 } 476 }
435 477
540 Safefree (PL_retstack); 582 Safefree (PL_retstack);
541#endif 583#endif
542} 584}
543 585
544static size_t 586static size_t
545coro_rss (struct coro *coro) 587coro_rss (pTHX_ struct coro *coro)
546{ 588{
547 size_t rss = sizeof (coro); 589 size_t rss = sizeof (coro);
548 590
549 if (coro->mainstack) 591 if (coro->mainstack)
550 { 592 {
580 /* 622 /*
581 * emulate part of the perl startup here. 623 * emulate part of the perl startup here.
582 */ 624 */
583 coro_init_stacks (aTHX); 625 coro_init_stacks (aTHX);
584 626
627 PL_runops = RUNOPS_DEFAULT;
585 PL_curcop = &PL_compiling; 628 PL_curcop = &PL_compiling;
586 PL_in_eval = EVAL_NULL; 629 PL_in_eval = EVAL_NULL;
587 PL_comppad = 0; 630 PL_comppad = 0;
588 PL_curpm = 0; 631 PL_curpm = 0;
589 PL_localizing = 0; 632 PL_localizing = 0;
620 SvREFCNT_dec (coro_mortal); 663 SvREFCNT_dec (coro_mortal);
621 coro_mortal = 0; 664 coro_mortal = 0;
622 } 665 }
623} 666}
624 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
625/* 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 */
626/* _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 */
627/* during execution of a perl program */ 793/* during execution of a perl program */
628static void NOINLINE 794static void NOINLINE
629prepare_cctx (pTHX_ coro_cctx *cctx) 795prepare_cctx (pTHX_ coro_cctx *cctx)
630{ 796{
631 dSP; 797 dSP;
632 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;
633 804
634 Zero (&myop, 1, LOGOP); 805 Zero (&myop, 1, LOGOP);
635 myop.op_next = PL_op; 806 myop.op_next = PL_op;
636 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 807 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
637 808
654 dTHX; 825 dTHX;
655 826
656 /* coro_run is the alternative tail of transfer(), so unlock here. */ 827 /* coro_run is the alternative tail of transfer(), so unlock here. */
657 UNLOCK; 828 UNLOCK;
658 829
659 PL_top_env = &PL_start_env; 830 /* we now skip the entersub that lead to transfer() */
831 PL_op = PL_op->op_next;
660 832
661 /* inject a fake subroutine call to cctx_init */ 833 /* inject a fake subroutine call to cctx_init */
662 prepare_cctx (aTHX_ (coro_cctx *)arg); 834 prepare_cctx (aTHX_ (coro_cctx *)arg);
663 835
664 /* 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 */
698# if CORO_STACKGUARD 870# if CORO_STACKGUARD
699 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 871 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
700# endif 872# endif
701 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 873 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr;
702 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 874 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
703 cctx->mapped = 1; 875 cctx->flags |= CC_MAPPED;
704 } 876 }
705 else 877 else
706#endif 878#endif
707 { 879 {
708 cctx->ssize = coro_stacksize * (long)sizeof (long); 880 cctx->ssize = coro_stacksize * (long)sizeof (long);
735#if CORO_USE_VALGRIND 907#if CORO_USE_VALGRIND
736 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 908 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
737#endif 909#endif
738 910
739#if HAVE_MMAP 911#if HAVE_MMAP
740 if (cctx->mapped) 912 if (cctx->flags & CC_MAPPED)
741 munmap (cctx->sptr, cctx->ssize); 913 munmap (cctx->sptr, cctx->ssize);
742 else 914 else
743#endif 915#endif
744 Safefree (cctx->sptr); 916 Safefree (cctx->sptr);
745 917
753 { 925 {
754 coro_cctx *cctx = cctx_first; 926 coro_cctx *cctx = cctx_first;
755 cctx_first = cctx->next; 927 cctx_first = cctx->next;
756 --cctx_idle; 928 --cctx_idle;
757 929
758 if (cctx->ssize >= coro_stacksize) 930 if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE))
759 return cctx; 931 return cctx;
760 932
761 cctx_destroy (cctx); 933 cctx_destroy (cctx);
762 } 934 }
763 935
764 PL_op = PL_op->op_next;
765 return cctx_new (); 936 return cctx_new ();
766} 937}
767 938
768static void 939static void
769cctx_put (coro_cctx *cctx) 940cctx_put (coro_cctx *cctx)
773 { 944 {
774 coro_cctx *first = cctx_first; 945 coro_cctx *first = cctx_first;
775 cctx_first = first->next; 946 cctx_first = first->next;
776 --cctx_idle; 947 --cctx_idle;
777 948
778 assert (!first->inuse);
779 cctx_destroy (first); 949 cctx_destroy (first);
780 } 950 }
781 951
782 ++cctx_idle; 952 ++cctx_idle;
783 cctx->next = cctx_first; 953 cctx->next = cctx_first;
784 cctx_first = cctx; 954 cctx_first = cctx;
785} 955}
786 956
787/** coroutine switching *****************************************************/ 957/** coroutine switching *****************************************************/
788 958
789/* 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 */
790static void NOINLINE 979static void NOINLINE
791transfer (pTHX_ struct coro *prev, struct coro *next) 980transfer (pTHX_ struct coro *prev, struct coro *next)
792{ 981{
793 dSTACKLEVEL; 982 dSTACKLEVEL;
794 983
804 993
805 if (prev->flags & CF_NEW) 994 if (prev->flags & CF_NEW)
806 { 995 {
807 /* create a new empty context */ 996 /* create a new empty context */
808 Newz (0, prev->cctx, 1, coro_cctx); 997 Newz (0, prev->cctx, 1, coro_cctx);
809 prev->cctx->inuse = 1;
810 prev->flags &= ~CF_NEW; 998 prev->flags &= ~CF_NEW;
811 prev->flags |= CF_RUNNING; 999 prev->flags |= CF_RUNNING;
812 } 1000 }
813
814 /*TODO: must not croak here */
815 if (!prev->flags & CF_RUNNING)
816 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
817
818 if (next->flags & CF_RUNNING)
819 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
820
821 if (next->flags & CF_DESTROYED)
822 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
823 1001
824 prev->flags &= ~CF_RUNNING; 1002 prev->flags &= ~CF_RUNNING;
825 next->flags |= CF_RUNNING; 1003 next->flags |= CF_RUNNING;
826 1004
827 LOCK; 1005 LOCK;
832 next->flags &= ~CF_NEW; 1010 next->flags &= ~CF_NEW;
833 /* first get rid of the old state */ 1011 /* first get rid of the old state */
834 save_perl (aTHX_ prev); 1012 save_perl (aTHX_ prev);
835 /* setup coroutine call */ 1013 /* setup coroutine call */
836 setup_coro (aTHX_ next); 1014 setup_coro (aTHX_ next);
837 /* need a new stack */
838 assert (!next->cctx);
839 } 1015 }
840 else 1016 else
841 { 1017 {
842 /* coroutine already started */ 1018 /* coroutine already started */
843 save_perl (aTHX_ prev); 1019 save_perl (aTHX_ prev);
845 } 1021 }
846 1022
847 prev__cctx = prev->cctx; 1023 prev__cctx = prev->cctx;
848 1024
849 /* possibly "free" the cctx */ 1025 /* possibly "free" the cctx */
850 if (prev__cctx->idle_sp == STACKLEVEL) 1026 if (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))
851 { 1027 {
852 /* 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 */
853 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));
854 1030
855 prev->cctx = 0; 1031 prev->cctx = 0;
856 1032
857 cctx_put (prev__cctx); 1033 cctx_put (prev__cctx);
858 prev__cctx->inuse = 0;
859 } 1034 }
860 1035
1036 ++next->usecount;
1037
861 if (!next->cctx) 1038 if (!next->cctx)
862 {
863 next->cctx = cctx_get (aTHX); 1039 next->cctx = cctx_get (aTHX);
864 assert (!next->cctx->inuse);
865 next->cctx->inuse = 1;
866 }
867 1040
868 if (prev__cctx != next->cctx) 1041 if (prev__cctx != next->cctx)
869 { 1042 {
870 prev__cctx->top_env = PL_top_env; 1043 prev__cctx->top_env = PL_top_env;
871 PL_top_env = next->cctx->top_env; 1044 PL_top_env = next->cctx->top_env;
881{ 1054{
882 struct coro *prev, *next; 1055 struct coro *prev, *next;
883}; 1056};
884 1057
885#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)
886 1060
887/** high level stuff ********************************************************/ 1061/** high level stuff ********************************************************/
888 1062
889static int 1063static int
890coro_state_destroy (pTHX_ struct coro *coro) 1064coro_state_destroy (pTHX_ struct coro *coro)
973#else 1147#else
974# define MGf_DUP 0 1148# define MGf_DUP 0
975#endif 1149#endif
976}; 1150};
977 1151
978static struct coro *
979SvSTATE_ (pTHX_ SV *coro)
980{
981 HV *stash;
982 MAGIC *mg;
983
984 if (SvROK (coro))
985 coro = SvRV (coro);
986
987 stash = SvSTASH (coro);
988 if (stash != coro_stash && stash != coro_state_stash)
989 {
990 /* very slow, but rare, check */
991 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
992 croak ("Coro::State object required");
993 }
994
995 mg = CORO_MAGIC (coro);
996 return (struct coro *)mg->mg_ptr;
997}
998
999#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
1000
1001static void 1152static void
1002prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) 1153prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv)
1003{ 1154{
1004 ta->prev = SvSTATE (prev_sv); 1155 ta->prev = SvSTATE (prev_sv);
1005 ta->next = SvSTATE (next_sv); 1156 ta->next = SvSTATE (next_sv);
1157 TRANSFER_CHECK (*ta);
1006} 1158}
1007 1159
1008static void 1160static void
1009api_transfer (SV *prev_sv, SV *next_sv) 1161api_transfer (SV *prev_sv, SV *next_sv)
1010{ 1162{
1127 break; 1279 break;
1128 } 1280 }
1129 1281
1130 /* free this only after the transfer */ 1282 /* free this only after the transfer */
1131 prev_sv = SvRV (coro_current); 1283 prev_sv = SvRV (coro_current);
1132 SvRV_set (coro_current, next_sv);
1133 ta->prev = SvSTATE (prev_sv); 1284 ta->prev = SvSTATE (prev_sv);
1134 1285 TRANSFER_CHECK (*ta);
1135 assert (ta->next->flags & CF_READY); 1286 assert (ta->next->flags & CF_READY);
1136 ta->next->flags &= ~CF_READY; 1287 ta->next->flags &= ~CF_READY;
1288 SvRV_set (coro_current, next_sv);
1137 1289
1138 LOCK; 1290 LOCK;
1139 free_coro_mortal (aTHX); 1291 free_coro_mortal (aTHX);
1140 coro_mortal = prev_sv; 1292 coro_mortal = prev_sv;
1141 UNLOCK; 1293 UNLOCK;
1202 } 1354 }
1203 else 1355 else
1204 return 0; 1356 return 0;
1205} 1357}
1206 1358
1359static void
1360api_trace (SV *coro_sv, int flags)
1361{
1362 dTHX;
1363 struct coro *coro = SvSTATE (coro_sv);
1364
1365 if (flags & CC_TRACE)
1366 {
1367 if (!coro->cctx)
1368 coro->cctx = cctx_new ();
1369 else if (!(coro->cctx->flags & CC_TRACE))
1370 croak ("cannot enable tracing on coroutine with custom stack");
1371
1372 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1373 }
1374 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1375 {
1376 coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL);
1377
1378 if (coro->flags & CF_RUNNING)
1379 PL_runops = RUNOPS_DEFAULT;
1380 else
1381 coro->runops = RUNOPS_DEFAULT;
1382 }
1383}
1384
1207MODULE = Coro::State PACKAGE = Coro::State 1385MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1208 1386
1209PROTOTYPES: DISABLE 1387PROTOTYPES: DISABLE
1210 1388
1211BOOT: 1389BOOT:
1212{ 1390{
1214 MUTEX_INIT (&coro_mutex); 1392 MUTEX_INIT (&coro_mutex);
1215#endif 1393#endif
1216 BOOT_PAGESIZE; 1394 BOOT_PAGESIZE;
1217 1395
1218 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1396 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1397
1398 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1399 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1400 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1401 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1219 1402
1220 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); 1403 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1221 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); 1404 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1222 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); 1405 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1223 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); 1406 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1375 if (coro->hv) 1558 if (coro->hv)
1376 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); 1559 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
1377} 1560}
1378 1561
1379void 1562void
1380_eval (Coro::State coro, SV *coderef) 1563call (Coro::State coro, SV *coderef)
1564 ALIAS:
1565 eval = 1
1381 CODE: 1566 CODE:
1382{ 1567{
1383 if (coro->mainstack) 1568 if (coro->mainstack)
1384 { 1569 {
1385 struct coro temp; 1570 struct coro temp;
1396 dSP; 1581 dSP;
1397 ENTER; 1582 ENTER;
1398 SAVETMPS; 1583 SAVETMPS;
1399 PUSHMARK (SP); 1584 PUSHMARK (SP);
1400 PUTBACK; 1585 PUTBACK;
1586 if (ix)
1587 eval_sv (coderef, 0);
1588 else
1401 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1589 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1402 SPAGAIN; 1590 SPAGAIN;
1403 FREETMPS; 1591 FREETMPS;
1404 LEAVE; 1592 LEAVE;
1405 PUTBACK; 1593 PUTBACK;
1406 } 1594 }
1410 save_perl (aTHX_ coro); 1598 save_perl (aTHX_ coro);
1411 load_perl (aTHX_ &temp); 1599 load_perl (aTHX_ &temp);
1412 } 1600 }
1413 } 1601 }
1414} 1602}
1415 1603
1416SV * 1604SV *
1417is_ready (Coro::State coro) 1605is_ready (Coro::State coro)
1418 PROTOTYPE: $ 1606 PROTOTYPE: $
1419 ALIAS: 1607 ALIAS:
1420 is_ready = CF_READY 1608 is_ready = CF_READY
1424 CODE: 1612 CODE:
1425 RETVAL = boolSV (coro->flags & ix); 1613 RETVAL = boolSV (coro->flags & ix);
1426 OUTPUT: 1614 OUTPUT:
1427 RETVAL 1615 RETVAL
1428 1616
1617void
1618api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
1619
1620SV *
1621has_stack (Coro::State coro)
1622 PROTOTYPE: $
1623 CODE:
1624 RETVAL = boolSV (!!coro->cctx);
1625 OUTPUT:
1626 RETVAL
1627
1628int
1629is_traced (Coro::State coro)
1630 PROTOTYPE: $
1631 CODE:
1632 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1633 OUTPUT:
1634 RETVAL
1635
1429IV 1636IV
1430rss (Coro::State coro) 1637rss (Coro::State coro)
1431 PROTOTYPE: $ 1638 PROTOTYPE: $
1639 ALIAS:
1640 usecount = 1
1432 CODE: 1641 CODE:
1433 RETVAL = coro_rss (coro); 1642 switch (ix)
1643 {
1644 case 0: RETVAL = coro_rss (aTHX_ coro); break;
1645 case 1: RETVAL = coro->usecount; break;
1646 }
1434 OUTPUT: 1647 OUTPUT:
1435 RETVAL 1648 RETVAL
1436 1649
1437 1650
1438MODULE = Coro::State PACKAGE = Coro 1651MODULE = Coro::State PACKAGE = Coro
1439 1652
1440BOOT: 1653BOOT:
1441{ 1654{
1442 int i; 1655 int i;
1656
1657 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE);
1658 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE);
1659 av_async_pool = get_av ("Coro::async_pool", TRUE);
1660
1661 coro_current = get_sv ("Coro::current", FALSE);
1662 SvREADONLY_on (coro_current);
1443 1663
1444 coro_stash = gv_stashpv ("Coro", TRUE); 1664 coro_stash = gv_stashpv ("Coro", TRUE);
1445 1665
1446 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1666 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1447 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 1667 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1448 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1668 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1449 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1669 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1450 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1670 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1451 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1671 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1452
1453 coro_current = get_sv ("Coro::current", FALSE);
1454 SvREADONLY_on (coro_current);
1455 1672
1456 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1673 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1457 coro_ready[i] = newAV (); 1674 coro_ready[i] = newAV ();
1458 1675
1459 { 1676 {
1517 CODE: 1734 CODE:
1518 RETVAL = coro_nready; 1735 RETVAL = coro_nready;
1519 OUTPUT: 1736 OUTPUT:
1520 RETVAL 1737 RETVAL
1521 1738
1739# for async_pool speedup
1740void
1741_pool_1 (SV *cb)
1742 CODE:
1743{
1744 int i, len;
1745 HV *hv = (HV *)SvRV (coro_current);
1746 AV *defav = GvAV (PL_defgv);
1747 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1748 AV *invoke_av;
1749
1750 if (!invoke)
1751 croak ("\3terminate\2\n");
1752
1753 hv_store (hv, "desc", sizeof ("desc") - 1,
1754 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1755
1756 invoke_av = (AV *)SvRV (invoke);
1757 len = av_len (invoke_av);
1758
1759 sv_setsv (cb, AvARRAY (invoke_av)[0]);
1760
1761 if (len > 0)
1762 {
1763 av_fill (defav, len - 1);
1764 for (i = 0; i < len; ++i)
1765 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1]));
1766 }
1767
1768 SvREFCNT_dec (invoke);
1769}
1770
1771void
1772_pool_2 (SV *cb)
1773 CODE:
1774{
1775 struct coro *coro = SvSTATE (coro_current);
1776
1777 sv_setsv (cb, &PL_sv_undef);
1778
1779 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1780 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1781 croak ("\3terminate\2\n");
1782
1783 av_clear (GvAV (PL_defgv));
1784 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1785 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1786 coro->save = CORO_SAVE_DEF;
1787 coro->prio = 0;
1788
1789 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
1790 api_trace (coro_current, 0);
1791
1792 av_push (av_async_pool, newSVsv (coro_current));
1793}
1794
1795
1522MODULE = Coro::State PACKAGE = Coro::AIO 1796MODULE = Coro::State PACKAGE = Coro::AIO
1523 1797
1524SV * 1798SV *
1525_get_state () 1799_get_state ()
1526 CODE: 1800 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines