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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines