ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.xs
(Generate patch)

Comparing Coro/Coro/State.xs (file contents):
Revision 1.162 by root, Sun Sep 23 21:49:58 2007 UTC vs.
Revision 1.172 by root, Sun Sep 30 13:15:01 2007 UTC

140static AV *av_async_pool; 140static AV *av_async_pool;
141 141
142static struct coro_cctx *cctx_first; 142static struct coro_cctx *cctx_first;
143static int cctx_count, cctx_idle; 143static int cctx_count, cctx_idle;
144 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};
153
145/* this is a structure representing a c-level coroutine */ 154/* this is a structure representing a c-level coroutine */
146typedef struct coro_cctx { 155typedef struct coro_cctx {
147 struct coro_cctx *next; 156 struct coro_cctx *next;
148 157
149 /* the stack */ 158 /* the stack */
157 coro_context cctx; 166 coro_context cctx;
158 167
159#if CORO_USE_VALGRIND 168#if CORO_USE_VALGRIND
160 int valgrind_id; 169 int valgrind_id;
161#endif 170#endif
162 char inuse, mapped; 171 unsigned char flags;
163} coro_cctx; 172} coro_cctx;
164 173
165enum { 174enum {
166 CF_RUNNING = 0x0001, /* coroutine is running */ 175 CF_RUNNING = 0x0001, /* coroutine is running */
167 CF_READY = 0x0002, /* coroutine is ready */ 176 CF_READY = 0x0002, /* coroutine is ready */
190 199
191#define VAR(name,type) type name; 200#define VAR(name,type) type name;
192# include "state.h" 201# include "state.h"
193#undef VAR 202#undef VAR
194 203
204 /* statistics */
205 int usecount; /* number of switches to this coro */
206
195 /* coro process data */ 207 /* coro process data */
196 int prio; 208 int prio;
197 209
198 /* linked list */ 210 /* linked list */
199 struct coro *next, *prev; 211 struct coro *next, *prev;
289 SvMAGIC (cv) \ 301 SvMAGIC (cv) \
290 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ 302 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \
291 ? SvMAGIC (cv) \ 303 ? SvMAGIC (cv) \
292 : mg_find ((SV *)cv, PERL_MAGIC_coro) \ 304 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
293 : 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))
294 332
295/* the next two functions merely cache the padlists */ 333/* the next two functions merely cache the padlists */
296static void 334static void
297get_padlist (pTHX_ CV *cv) 335get_padlist (pTHX_ CV *cv)
298{ 336{
544 Safefree (PL_retstack); 582 Safefree (PL_retstack);
545#endif 583#endif
546} 584}
547 585
548static size_t 586static size_t
549coro_rss (struct coro *coro) 587coro_rss (pTHX_ struct coro *coro)
550{ 588{
551 size_t rss = sizeof (coro); 589 size_t rss = sizeof (coro);
552 590
553 if (coro->mainstack) 591 if (coro->mainstack)
554 { 592 {
584 /* 622 /*
585 * emulate part of the perl startup here. 623 * emulate part of the perl startup here.
586 */ 624 */
587 coro_init_stacks (aTHX); 625 coro_init_stacks (aTHX);
588 626
627 PL_runops = RUNOPS_DEFAULT;
589 PL_curcop = &PL_compiling; 628 PL_curcop = &PL_compiling;
590 PL_in_eval = EVAL_NULL; 629 PL_in_eval = EVAL_NULL;
591 PL_comppad = 0; 630 PL_comppad = 0;
592 PL_curpm = 0; 631 PL_curpm = 0;
593 PL_localizing = 0; 632 PL_localizing = 0;
624 SvREFCNT_dec (coro_mortal); 663 SvREFCNT_dec (coro_mortal);
625 coro_mortal = 0; 664 coro_mortal = 0;
626 } 665 }
627} 666}
628 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 (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB)
681 {
682 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
683 SV **bot, **top;
684 AV *av = newAV (); /* return values */
685 SV **cb;
686 runops_proc_t old_runops = PL_runops;
687 dSP;
688 ENTER;
689 SAVETMPS;
690 EXTEND (SP, 3);
691 PL_runops = RUNOPS_DEFAULT;
692
693 GV *gv = CvGV (cx->blk_sub.cv);
694 SV *fullname = sv_2mortal (newSV (0));
695 if (isGV (gv))
696 gv_efullname3 (fullname, gv, 0);
697
698 bot = PL_stack_base + cx->blk_oldsp + 1;
699 top = cx->blk_gimme == G_ARRAY ? SP + 1
700 : cx->blk_gimme == G_SCALAR ? bot + 1
701 : bot;
702
703 while (bot < top)
704 av_push (av, SvREFCNT_inc (*bot++));
705
706 PUSHMARK (SP);
707 PUSHs (&PL_sv_no);
708 PUSHs (fullname);
709 PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
710 PUTBACK;
711 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
712 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
713 SPAGAIN;
714
715 FREETMPS;
716 LEAVE;
717 PL_runops = old_runops;
718 }
719
720 if (oldcop != PL_curcop)
721 {
722 oldcop = PL_curcop;
723
724 if (PL_curcop != &PL_compiling)
725 {
726 SV **cb;
727 runops_proc_t old_runops = PL_runops;
728 dSP;
729 ENTER;
730 SAVETMPS;
731 EXTEND (SP, 3);
732 PL_runops = RUNOPS_DEFAULT;
733
734 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB)
735 {
736 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
737
738 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
739 {
740 GV *gv = CvGV (cx->blk_sub.cv);
741 SV *fullname = sv_2mortal (newSV (0));
742 if (isGV (gv))
743 gv_efullname3 (fullname, gv, 0);
744
745 PUSHMARK (SP);
746 PUSHs (&PL_sv_yes);
747 PUSHs (fullname);
748 PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
749 PUTBACK;
750 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
751 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
752 SPAGAIN;
753 }
754
755 oldcxix = cxstack_ix;
756 }
757
758 if (cctx->flags & CC_TRACE_LINE)
759 {
760 PUSHMARK (SP);
761 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0)));
762 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop))));
763 PUTBACK;
764 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0);
765 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
766 SPAGAIN;
767 }
768
769 FREETMPS;
770 LEAVE;
771 PL_runops = old_runops;
772 }
773 }
774 }
775
776 TAINT_NOT;
777 return 0;
778}
779
629/* inject a fake call to Coro::State::_cctx_init into the execution */ 780/* inject a fake call to Coro::State::_cctx_init into the execution */
630/* _cctx_init should be careful, as it could be called at almost any time */ 781/* _cctx_init should be careful, as it could be called at almost any time */
631/* during execution of a perl program */ 782/* during execution of a perl program */
632static void NOINLINE 783static void NOINLINE
633prepare_cctx (pTHX_ coro_cctx *cctx) 784prepare_cctx (pTHX_ coro_cctx *cctx)
634{ 785{
635 dSP; 786 dSP;
636 LOGOP myop; 787 LOGOP myop;
788
789 PL_top_env = &PL_start_env;
790
791 if (cctx->flags & CC_TRACE)
792 PL_runops = runops_trace;
637 793
638 Zero (&myop, 1, LOGOP); 794 Zero (&myop, 1, LOGOP);
639 myop.op_next = PL_op; 795 myop.op_next = PL_op;
640 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 796 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
641 797
658 dTHX; 814 dTHX;
659 815
660 /* coro_run is the alternative tail of transfer(), so unlock here. */ 816 /* coro_run is the alternative tail of transfer(), so unlock here. */
661 UNLOCK; 817 UNLOCK;
662 818
663 PL_top_env = &PL_start_env; 819 /* we now skip the entersub that lead to transfer() */
820 PL_op = PL_op->op_next;
664 821
665 /* inject a fake subroutine call to cctx_init */ 822 /* inject a fake subroutine call to cctx_init */
666 prepare_cctx (aTHX_ (coro_cctx *)arg); 823 prepare_cctx (aTHX_ (coro_cctx *)arg);
667 824
668 /* somebody or something will hit me for both perl_run and PL_restartop */ 825 /* somebody or something will hit me for both perl_run and PL_restartop */
702# if CORO_STACKGUARD 859# if CORO_STACKGUARD
703 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 860 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
704# endif 861# endif
705 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 862 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr;
706 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 863 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
707 cctx->mapped = 1; 864 cctx->flags |= CC_MAPPED;
708 } 865 }
709 else 866 else
710#endif 867#endif
711 { 868 {
712 cctx->ssize = coro_stacksize * (long)sizeof (long); 869 cctx->ssize = coro_stacksize * (long)sizeof (long);
739#if CORO_USE_VALGRIND 896#if CORO_USE_VALGRIND
740 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 897 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
741#endif 898#endif
742 899
743#if HAVE_MMAP 900#if HAVE_MMAP
744 if (cctx->mapped) 901 if (cctx->flags & CC_MAPPED)
745 munmap (cctx->sptr, cctx->ssize); 902 munmap (cctx->sptr, cctx->ssize);
746 else 903 else
747#endif 904#endif
748 Safefree (cctx->sptr); 905 Safefree (cctx->sptr);
749 906
757 { 914 {
758 coro_cctx *cctx = cctx_first; 915 coro_cctx *cctx = cctx_first;
759 cctx_first = cctx->next; 916 cctx_first = cctx->next;
760 --cctx_idle; 917 --cctx_idle;
761 918
762 if (cctx->ssize >= coro_stacksize) 919 if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE))
763 return cctx; 920 return cctx;
764 921
765 cctx_destroy (cctx); 922 cctx_destroy (cctx);
766 } 923 }
767 924
768 PL_op = PL_op->op_next;
769 return cctx_new (); 925 return cctx_new ();
770} 926}
771 927
772static void 928static void
773cctx_put (coro_cctx *cctx) 929cctx_put (coro_cctx *cctx)
777 { 933 {
778 coro_cctx *first = cctx_first; 934 coro_cctx *first = cctx_first;
779 cctx_first = first->next; 935 cctx_first = first->next;
780 --cctx_idle; 936 --cctx_idle;
781 937
782 assert (!first->inuse);
783 cctx_destroy (first); 938 cctx_destroy (first);
784 } 939 }
785 940
786 ++cctx_idle; 941 ++cctx_idle;
787 cctx->next = cctx_first; 942 cctx->next = cctx_first;
788 cctx_first = cctx; 943 cctx_first = cctx;
789} 944}
790 945
791/** coroutine switching *****************************************************/ 946/** coroutine switching *****************************************************/
792 947
793/* never call directly, always through the coro_state_transfer global variable */ 948static void NOINLINE
949transfer_check (pTHX_ struct coro *prev, struct coro *next)
950{
951 if (prev != next)
952 {
953 if (!(prev->flags & (CF_RUNNING | CF_NEW)))
954 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states");
955
956 if (next->flags & CF_RUNNING)
957 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
958
959 if (next->flags & CF_DESTROYED)
960 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
961
962 if (PL_lex_state != LEX_NOTPARSING)
963 croak ("Coro::State::transfer called while parsing, but this is not supported");
964 }
965}
966
967/* always use the TRANSFER macro */
794static void NOINLINE 968static void NOINLINE
795transfer (pTHX_ struct coro *prev, struct coro *next) 969transfer (pTHX_ struct coro *prev, struct coro *next)
796{ 970{
797 dSTACKLEVEL; 971 dSTACKLEVEL;
798 972
808 982
809 if (prev->flags & CF_NEW) 983 if (prev->flags & CF_NEW)
810 { 984 {
811 /* create a new empty context */ 985 /* create a new empty context */
812 Newz (0, prev->cctx, 1, coro_cctx); 986 Newz (0, prev->cctx, 1, coro_cctx);
813 prev->cctx->inuse = 1;
814 prev->flags &= ~CF_NEW; 987 prev->flags &= ~CF_NEW;
815 prev->flags |= CF_RUNNING; 988 prev->flags |= CF_RUNNING;
816 } 989 }
817
818 /*TODO: must not croak here */
819 if (!prev->flags & CF_RUNNING)
820 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
821
822 if (next->flags & CF_RUNNING)
823 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
824
825 if (next->flags & CF_DESTROYED)
826 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
827 990
828 prev->flags &= ~CF_RUNNING; 991 prev->flags &= ~CF_RUNNING;
829 next->flags |= CF_RUNNING; 992 next->flags |= CF_RUNNING;
830 993
831 LOCK; 994 LOCK;
836 next->flags &= ~CF_NEW; 999 next->flags &= ~CF_NEW;
837 /* first get rid of the old state */ 1000 /* first get rid of the old state */
838 save_perl (aTHX_ prev); 1001 save_perl (aTHX_ prev);
839 /* setup coroutine call */ 1002 /* setup coroutine call */
840 setup_coro (aTHX_ next); 1003 setup_coro (aTHX_ next);
841 /* need a new stack */
842 assert (!next->cctx);
843 } 1004 }
844 else 1005 else
845 { 1006 {
846 /* coroutine already started */ 1007 /* coroutine already started */
847 save_perl (aTHX_ prev); 1008 save_perl (aTHX_ prev);
849 } 1010 }
850 1011
851 prev__cctx = prev->cctx; 1012 prev__cctx = prev->cctx;
852 1013
853 /* possibly "free" the cctx */ 1014 /* possibly "free" the cctx */
854 if (prev__cctx->idle_sp == STACKLEVEL) 1015 if (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))
855 { 1016 {
856 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1017 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
857 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); 1018 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
858 1019
859 prev->cctx = 0; 1020 prev->cctx = 0;
860 1021
861 cctx_put (prev__cctx); 1022 cctx_put (prev__cctx);
862 prev__cctx->inuse = 0;
863 } 1023 }
864 1024
1025 ++next->usecount;
1026
865 if (!next->cctx) 1027 if (!next->cctx)
866 {
867 next->cctx = cctx_get (aTHX); 1028 next->cctx = cctx_get (aTHX);
868 assert (!next->cctx->inuse);
869 next->cctx->inuse = 1;
870 }
871 1029
872 if (prev__cctx != next->cctx) 1030 if (prev__cctx != next->cctx)
873 { 1031 {
874 prev__cctx->top_env = PL_top_env; 1032 prev__cctx->top_env = PL_top_env;
875 PL_top_env = next->cctx->top_env; 1033 PL_top_env = next->cctx->top_env;
885{ 1043{
886 struct coro *prev, *next; 1044 struct coro *prev, *next;
887}; 1045};
888 1046
889#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) 1047#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next)
1048#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
890 1049
891/** high level stuff ********************************************************/ 1050/** high level stuff ********************************************************/
892 1051
893static int 1052static int
894coro_state_destroy (pTHX_ struct coro *coro) 1053coro_state_destroy (pTHX_ struct coro *coro)
977#else 1136#else
978# define MGf_DUP 0 1137# define MGf_DUP 0
979#endif 1138#endif
980}; 1139};
981 1140
982static struct coro *
983SvSTATE_ (pTHX_ SV *coro)
984{
985 HV *stash;
986 MAGIC *mg;
987
988 if (SvROK (coro))
989 coro = SvRV (coro);
990
991 stash = SvSTASH (coro);
992 if (stash != coro_stash && stash != coro_state_stash)
993 {
994 /* very slow, but rare, check */
995 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
996 croak ("Coro::State object required");
997 }
998
999 mg = CORO_MAGIC (coro);
1000 return (struct coro *)mg->mg_ptr;
1001}
1002
1003#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
1004
1005static void 1141static void
1006prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) 1142prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv)
1007{ 1143{
1008 ta->prev = SvSTATE (prev_sv); 1144 ta->prev = SvSTATE (prev_sv);
1009 ta->next = SvSTATE (next_sv); 1145 ta->next = SvSTATE (next_sv);
1146 TRANSFER_CHECK (*ta);
1010} 1147}
1011 1148
1012static void 1149static void
1013api_transfer (SV *prev_sv, SV *next_sv) 1150api_transfer (SV *prev_sv, SV *next_sv)
1014{ 1151{
1131 break; 1268 break;
1132 } 1269 }
1133 1270
1134 /* free this only after the transfer */ 1271 /* free this only after the transfer */
1135 prev_sv = SvRV (coro_current); 1272 prev_sv = SvRV (coro_current);
1136 SvRV_set (coro_current, next_sv);
1137 ta->prev = SvSTATE (prev_sv); 1273 ta->prev = SvSTATE (prev_sv);
1138 1274 TRANSFER_CHECK (*ta);
1139 assert (ta->next->flags & CF_READY); 1275 assert (ta->next->flags & CF_READY);
1140 ta->next->flags &= ~CF_READY; 1276 ta->next->flags &= ~CF_READY;
1277 SvRV_set (coro_current, next_sv);
1141 1278
1142 LOCK; 1279 LOCK;
1143 free_coro_mortal (aTHX); 1280 free_coro_mortal (aTHX);
1144 coro_mortal = prev_sv; 1281 coro_mortal = prev_sv;
1145 UNLOCK; 1282 UNLOCK;
1218 MUTEX_INIT (&coro_mutex); 1355 MUTEX_INIT (&coro_mutex);
1219#endif 1356#endif
1220 BOOT_PAGESIZE; 1357 BOOT_PAGESIZE;
1221 1358
1222 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1359 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1360
1361 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1362 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1363 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1364 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1223 1365
1224 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); 1366 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1225 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); 1367 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1226 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); 1368 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1227 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); 1369 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1379 if (coro->hv) 1521 if (coro->hv)
1380 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); 1522 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
1381} 1523}
1382 1524
1383void 1525void
1384_eval (Coro::State coro, SV *coderef) 1526call (Coro::State coro, SV *coderef)
1527 ALIAS:
1528 eval = 1
1385 CODE: 1529 CODE:
1386{ 1530{
1387 if (coro->mainstack) 1531 if (coro->mainstack)
1388 { 1532 {
1389 struct coro temp; 1533 struct coro temp;
1400 dSP; 1544 dSP;
1401 ENTER; 1545 ENTER;
1402 SAVETMPS; 1546 SAVETMPS;
1403 PUSHMARK (SP); 1547 PUSHMARK (SP);
1404 PUTBACK; 1548 PUTBACK;
1549 if (ix)
1550 eval_sv (coderef, 0);
1551 else
1405 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1552 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1406 SPAGAIN; 1553 SPAGAIN;
1407 FREETMPS; 1554 FREETMPS;
1408 LEAVE; 1555 LEAVE;
1409 PUTBACK; 1556 PUTBACK;
1410 } 1557 }
1414 save_perl (aTHX_ coro); 1561 save_perl (aTHX_ coro);
1415 load_perl (aTHX_ &temp); 1562 load_perl (aTHX_ &temp);
1416 } 1563 }
1417 } 1564 }
1418} 1565}
1419 1566
1420SV * 1567SV *
1421is_ready (Coro::State coro) 1568is_ready (Coro::State coro)
1422 PROTOTYPE: $ 1569 PROTOTYPE: $
1423 ALIAS: 1570 ALIAS:
1424 is_ready = CF_READY 1571 is_ready = CF_READY
1428 CODE: 1575 CODE:
1429 RETVAL = boolSV (coro->flags & ix); 1576 RETVAL = boolSV (coro->flags & ix);
1430 OUTPUT: 1577 OUTPUT:
1431 RETVAL 1578 RETVAL
1432 1579
1580void
1581trace (Coro::State coro, int flags = CC_TRACE | CC_TRACE_SUB)
1582 CODE:
1583 if (flags & CC_TRACE)
1584 {
1585 if (!coro->cctx)
1586 coro->cctx = cctx_new ();
1587 else if (!(coro->cctx->flags & CC_TRACE))
1588 croak ("cannot enable tracing on coroutine with custom stack");
1589
1590 coro->cctx->flags |= flags & (CC_TRACE | CC_TRACE_ALL);
1591 }
1592 else
1593 if (coro->cctx && coro->cctx->flags & CC_TRACE)
1594 {
1595 coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL);
1596 coro->cctx->flags |= CC_NOREUSE;
1597
1598 if (coro->flags & CF_RUNNING)
1599 PL_runops = RUNOPS_DEFAULT;
1600 else
1601 coro->runops = RUNOPS_DEFAULT;
1602 }
1603
1433SV * 1604SV *
1434has_stack (Coro::State coro) 1605has_stack (Coro::State coro)
1435 PROTOTYPE: $ 1606 PROTOTYPE: $
1436 CODE: 1607 CODE:
1437 RETVAL = boolSV (!!coro->cctx); 1608 RETVAL = boolSV (!!coro->cctx);
1438 OUTPUT: 1609 OUTPUT:
1439 RETVAL 1610 RETVAL
1440 1611
1612int
1613is_traced (Coro::State coro)
1614 PROTOTYPE: $
1615 CODE:
1616 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1617 OUTPUT:
1618 RETVAL
1619
1441IV 1620IV
1442rss (Coro::State coro) 1621rss (Coro::State coro)
1443 PROTOTYPE: $ 1622 PROTOTYPE: $
1623 ALIAS:
1624 usecount = 1
1444 CODE: 1625 CODE:
1445 RETVAL = coro_rss (coro); 1626 switch (ix)
1627 {
1628 case 0: RETVAL = coro_rss (aTHX_ coro); break;
1629 case 1: RETVAL = coro->usecount; break;
1630 }
1446 OUTPUT: 1631 OUTPUT:
1447 RETVAL 1632 RETVAL
1448 1633
1449 1634
1450MODULE = Coro::State PACKAGE = Coro 1635MODULE = Coro::State PACKAGE = Coro
1573{ 1758{
1574 struct coro *coro = SvSTATE (coro_current); 1759 struct coro *coro = SvSTATE (coro_current);
1575 1760
1576 sv_setsv (cb, &PL_sv_undef); 1761 sv_setsv (cb, &PL_sv_undef);
1577 1762
1578 if (coro_rss (coro) > SvIV (sv_pool_rss) 1763 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1579 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 1764 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1580 croak ("\3terminate\2\n"); 1765 croak ("\3terminate\2\n");
1581 1766
1582 av_clear (GvAV (PL_defgv)); 1767 av_clear (GvAV (PL_defgv));
1583 hv_store (SvRV (coro_current), "desc", sizeof ("desc") - 1, 1768 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1584 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); 1769 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1585 coro->save = CORO_SAVE_DEF; 1770 coro->save = CORO_SAVE_DEF;
1586 coro->prio = 0; 1771 coro->prio = 0;
1587 av_push (av_async_pool, newSVsv (coro_current)); 1772 av_push (av_async_pool, newSVsv (coro_current));
1588} 1773}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines