… | |
… | |
141 | |
141 | |
142 | static struct coro_cctx *cctx_first; |
142 | static struct coro_cctx *cctx_first; |
143 | static int cctx_count, cctx_idle; |
143 | static int cctx_count, cctx_idle; |
144 | |
144 | |
145 | enum { |
145 | enum { |
146 | CC_MAPPED = 0x01, |
146 | CC_MAPPED = 0x01, |
147 | CC_TRACE = 0x02, |
|
|
148 | CC_NOREUSE = 0x04, /* throw this away after tracing */ |
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, |
149 | }; |
152 | }; |
150 | |
153 | |
151 | /* this is a structure representing a c-level coroutine */ |
154 | /* this is a structure representing a c-level coroutine */ |
152 | typedef struct coro_cctx { |
155 | typedef struct coro_cctx { |
153 | struct coro_cctx *next; |
156 | struct coro_cctx *next; |
… | |
… | |
196 | |
199 | |
197 | #define VAR(name,type) type name; |
200 | #define VAR(name,type) type name; |
198 | # include "state.h" |
201 | # include "state.h" |
199 | #undef VAR |
202 | #undef VAR |
200 | |
203 | |
|
|
204 | /* statistics */ |
|
|
205 | int usecount; /* number of switches to this coro */ |
|
|
206 | |
201 | /* coro process data */ |
207 | /* coro process data */ |
202 | int prio; |
208 | int prio; |
203 | |
209 | |
204 | /* linked list */ |
210 | /* linked list */ |
205 | struct coro *next, *prev; |
211 | struct coro *next, *prev; |
… | |
… | |
295 | SvMAGIC (cv) \ |
301 | SvMAGIC (cv) \ |
296 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
302 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
297 | ? SvMAGIC (cv) \ |
303 | ? SvMAGIC (cv) \ |
298 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
304 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
299 | : 0 |
305 | : 0 |
|
|
306 | |
|
|
307 | static struct coro * |
|
|
308 | SvSTATE_ (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)) |
300 | |
332 | |
301 | /* the next two functions merely cache the padlists */ |
333 | /* the next two functions merely cache the padlists */ |
302 | static void |
334 | static void |
303 | get_padlist (pTHX_ CV *cv) |
335 | get_padlist (pTHX_ CV *cv) |
304 | { |
336 | { |
… | |
… | |
550 | Safefree (PL_retstack); |
582 | Safefree (PL_retstack); |
551 | #endif |
583 | #endif |
552 | } |
584 | } |
553 | |
585 | |
554 | static size_t |
586 | static size_t |
555 | coro_rss (struct coro *coro) |
587 | coro_rss (pTHX_ struct coro *coro) |
556 | { |
588 | { |
557 | size_t rss = sizeof (coro); |
589 | size_t rss = sizeof (coro); |
558 | |
590 | |
559 | if (coro->mainstack) |
591 | if (coro->mainstack) |
560 | { |
592 | { |
… | |
… | |
590 | /* |
622 | /* |
591 | * emulate part of the perl startup here. |
623 | * emulate part of the perl startup here. |
592 | */ |
624 | */ |
593 | coro_init_stacks (aTHX); |
625 | coro_init_stacks (aTHX); |
594 | |
626 | |
|
|
627 | PL_runops = RUNOPS_DEFAULT; |
595 | PL_curcop = &PL_compiling; |
628 | PL_curcop = &PL_compiling; |
596 | PL_in_eval = EVAL_NULL; |
629 | PL_in_eval = EVAL_NULL; |
597 | PL_comppad = 0; |
630 | PL_comppad = 0; |
598 | PL_curpm = 0; |
631 | PL_curpm = 0; |
599 | PL_localizing = 0; |
632 | PL_localizing = 0; |
… | |
… | |
630 | SvREFCNT_dec (coro_mortal); |
663 | SvREFCNT_dec (coro_mortal); |
631 | coro_mortal = 0; |
664 | coro_mortal = 0; |
632 | } |
665 | } |
633 | } |
666 | } |
634 | |
667 | |
635 | static void |
|
|
636 | do_trace (pTHX) |
|
|
637 | { |
|
|
638 | if (PL_curcop != &PL_compiling) |
|
|
639 | { |
|
|
640 | runops_proc_t old_runops = PL_runops; |
|
|
641 | dSP; |
|
|
642 | ENTER; |
|
|
643 | SAVETMPS; |
|
|
644 | PUSHMARK (SP); |
|
|
645 | PUTBACK; |
|
|
646 | PL_runops = RUNOPS_DEFAULT; |
|
|
647 | call_pv ("Coro::_do_trace", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD | G_NOARGS); |
|
|
648 | PL_runops = old_runops; |
|
|
649 | SPAGAIN; |
|
|
650 | FREETMPS; |
|
|
651 | LEAVE; |
|
|
652 | PUTBACK; |
|
|
653 | } |
|
|
654 | } |
|
|
655 | |
|
|
656 | static int |
668 | static int |
657 | runops_coro (pTHX) |
669 | runops_trace (pTHX) |
658 | { |
670 | { |
659 | COP *oldcop = 0; |
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; |
660 | |
675 | |
661 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
676 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
662 | { |
677 | { |
663 | PERL_ASYNC_CHECK (); |
678 | PERL_ASYNC_CHECK (); |
664 | |
679 | |
665 | if (oldcop != PL_curcop) |
680 | if (cctx->flags & CC_TRACE_ALL) |
666 | { |
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 | { |
667 | oldcop = PL_curcop; |
722 | oldcop = PL_curcop; |
668 | do_trace (aTHX); |
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 | } |
669 | } |
784 | } |
670 | } |
785 | } |
671 | |
786 | |
672 | TAINT_NOT; |
787 | TAINT_NOT; |
673 | return 0; |
788 | return 0; |
… | |
… | |
683 | LOGOP myop; |
798 | LOGOP myop; |
684 | |
799 | |
685 | PL_top_env = &PL_start_env; |
800 | PL_top_env = &PL_start_env; |
686 | |
801 | |
687 | if (cctx->flags & CC_TRACE) |
802 | if (cctx->flags & CC_TRACE) |
688 | PL_runops = runops_coro; |
803 | PL_runops = runops_trace; |
689 | |
804 | |
690 | Zero (&myop, 1, LOGOP); |
805 | Zero (&myop, 1, LOGOP); |
691 | myop.op_next = PL_op; |
806 | myop.op_next = PL_op; |
692 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
807 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
693 | |
808 | |
… | |
… | |
839 | cctx_first = cctx; |
954 | cctx_first = cctx; |
840 | } |
955 | } |
841 | |
956 | |
842 | /** coroutine switching *****************************************************/ |
957 | /** coroutine switching *****************************************************/ |
843 | |
958 | |
844 | /* never call directly, always through the coro_state_transfer global variable */ |
959 | static void NOINLINE |
|
|
960 | transfer_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 */ |
845 | static void NOINLINE |
979 | static void NOINLINE |
846 | transfer (pTHX_ struct coro *prev, struct coro *next) |
980 | transfer (pTHX_ struct coro *prev, struct coro *next) |
847 | { |
981 | { |
848 | dSTACKLEVEL; |
982 | dSTACKLEVEL; |
849 | |
983 | |
… | |
… | |
862 | /* create a new empty context */ |
996 | /* create a new empty context */ |
863 | Newz (0, prev->cctx, 1, coro_cctx); |
997 | Newz (0, prev->cctx, 1, coro_cctx); |
864 | prev->flags &= ~CF_NEW; |
998 | prev->flags &= ~CF_NEW; |
865 | prev->flags |= CF_RUNNING; |
999 | prev->flags |= CF_RUNNING; |
866 | } |
1000 | } |
867 | |
|
|
868 | /*TODO: must not croak here */ |
|
|
869 | if (!prev->flags & CF_RUNNING) |
|
|
870 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
|
|
871 | |
|
|
872 | if (next->flags & CF_RUNNING) |
|
|
873 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
|
|
874 | |
|
|
875 | if (next->flags & CF_DESTROYED) |
|
|
876 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
|
|
877 | |
1001 | |
878 | prev->flags &= ~CF_RUNNING; |
1002 | prev->flags &= ~CF_RUNNING; |
879 | next->flags |= CF_RUNNING; |
1003 | next->flags |= CF_RUNNING; |
880 | |
1004 | |
881 | LOCK; |
1005 | LOCK; |
… | |
… | |
897 | } |
1021 | } |
898 | |
1022 | |
899 | prev__cctx = prev->cctx; |
1023 | prev__cctx = prev->cctx; |
900 | |
1024 | |
901 | /* possibly "free" the cctx */ |
1025 | /* possibly "free" the cctx */ |
902 | if (prev__cctx->idle_sp == STACKLEVEL) |
1026 | if (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE)) |
903 | { |
1027 | { |
904 | /* 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 */ |
905 | 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)); |
906 | |
1030 | |
907 | prev->cctx = 0; |
1031 | prev->cctx = 0; |
908 | |
1032 | |
909 | cctx_put (prev__cctx); |
1033 | cctx_put (prev__cctx); |
910 | } |
1034 | } |
|
|
1035 | |
|
|
1036 | ++next->usecount; |
911 | |
1037 | |
912 | if (!next->cctx) |
1038 | if (!next->cctx) |
913 | next->cctx = cctx_get (aTHX); |
1039 | next->cctx = cctx_get (aTHX); |
914 | |
1040 | |
915 | if (prev__cctx != next->cctx) |
1041 | if (prev__cctx != next->cctx) |
… | |
… | |
928 | { |
1054 | { |
929 | struct coro *prev, *next; |
1055 | struct coro *prev, *next; |
930 | }; |
1056 | }; |
931 | |
1057 | |
932 | #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) |
933 | |
1060 | |
934 | /** high level stuff ********************************************************/ |
1061 | /** high level stuff ********************************************************/ |
935 | |
1062 | |
936 | static int |
1063 | static int |
937 | coro_state_destroy (pTHX_ struct coro *coro) |
1064 | coro_state_destroy (pTHX_ struct coro *coro) |
… | |
… | |
1020 | #else |
1147 | #else |
1021 | # define MGf_DUP 0 |
1148 | # define MGf_DUP 0 |
1022 | #endif |
1149 | #endif |
1023 | }; |
1150 | }; |
1024 | |
1151 | |
1025 | static struct coro * |
|
|
1026 | SvSTATE_ (pTHX_ SV *coro) |
|
|
1027 | { |
|
|
1028 | HV *stash; |
|
|
1029 | MAGIC *mg; |
|
|
1030 | |
|
|
1031 | if (SvROK (coro)) |
|
|
1032 | coro = SvRV (coro); |
|
|
1033 | |
|
|
1034 | stash = SvSTASH (coro); |
|
|
1035 | if (stash != coro_stash && stash != coro_state_stash) |
|
|
1036 | { |
|
|
1037 | /* very slow, but rare, check */ |
|
|
1038 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
1039 | croak ("Coro::State object required"); |
|
|
1040 | } |
|
|
1041 | |
|
|
1042 | mg = CORO_MAGIC (coro); |
|
|
1043 | return (struct coro *)mg->mg_ptr; |
|
|
1044 | } |
|
|
1045 | |
|
|
1046 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
1047 | |
|
|
1048 | static void |
1152 | static void |
1049 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
1153 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
1050 | { |
1154 | { |
1051 | ta->prev = SvSTATE (prev_sv); |
1155 | ta->prev = SvSTATE (prev_sv); |
1052 | ta->next = SvSTATE (next_sv); |
1156 | ta->next = SvSTATE (next_sv); |
|
|
1157 | TRANSFER_CHECK (*ta); |
1053 | } |
1158 | } |
1054 | |
1159 | |
1055 | static void |
1160 | static void |
1056 | api_transfer (SV *prev_sv, SV *next_sv) |
1161 | api_transfer (SV *prev_sv, SV *next_sv) |
1057 | { |
1162 | { |
… | |
… | |
1174 | break; |
1279 | break; |
1175 | } |
1280 | } |
1176 | |
1281 | |
1177 | /* free this only after the transfer */ |
1282 | /* free this only after the transfer */ |
1178 | prev_sv = SvRV (coro_current); |
1283 | prev_sv = SvRV (coro_current); |
1179 | SvRV_set (coro_current, next_sv); |
|
|
1180 | ta->prev = SvSTATE (prev_sv); |
1284 | ta->prev = SvSTATE (prev_sv); |
1181 | |
1285 | TRANSFER_CHECK (*ta); |
1182 | assert (ta->next->flags & CF_READY); |
1286 | assert (ta->next->flags & CF_READY); |
1183 | ta->next->flags &= ~CF_READY; |
1287 | ta->next->flags &= ~CF_READY; |
|
|
1288 | SvRV_set (coro_current, next_sv); |
1184 | |
1289 | |
1185 | LOCK; |
1290 | LOCK; |
1186 | free_coro_mortal (aTHX); |
1291 | free_coro_mortal (aTHX); |
1187 | coro_mortal = prev_sv; |
1292 | coro_mortal = prev_sv; |
1188 | UNLOCK; |
1293 | UNLOCK; |
… | |
… | |
1249 | } |
1354 | } |
1250 | else |
1355 | else |
1251 | return 0; |
1356 | return 0; |
1252 | } |
1357 | } |
1253 | |
1358 | |
|
|
1359 | static void |
|
|
1360 | api_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 | |
1254 | MODULE = Coro::State PACKAGE = Coro::State |
1385 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
1255 | |
1386 | |
1256 | PROTOTYPES: DISABLE |
1387 | PROTOTYPES: DISABLE |
1257 | |
1388 | |
1258 | BOOT: |
1389 | BOOT: |
1259 | { |
1390 | { |
… | |
… | |
1261 | MUTEX_INIT (&coro_mutex); |
1392 | MUTEX_INIT (&coro_mutex); |
1262 | #endif |
1393 | #endif |
1263 | BOOT_PAGESIZE; |
1394 | BOOT_PAGESIZE; |
1264 | |
1395 | |
1265 | 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)); |
1266 | |
1402 | |
1267 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1403 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1268 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1404 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1269 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
1405 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
1270 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
1406 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
… | |
… | |
1462 | save_perl (aTHX_ coro); |
1598 | save_perl (aTHX_ coro); |
1463 | load_perl (aTHX_ &temp); |
1599 | load_perl (aTHX_ &temp); |
1464 | } |
1600 | } |
1465 | } |
1601 | } |
1466 | } |
1602 | } |
1467 | |
1603 | |
1468 | SV * |
1604 | SV * |
1469 | is_ready (Coro::State coro) |
1605 | is_ready (Coro::State coro) |
1470 | PROTOTYPE: $ |
1606 | PROTOTYPE: $ |
1471 | ALIAS: |
1607 | ALIAS: |
1472 | is_ready = CF_READY |
1608 | is_ready = CF_READY |
… | |
… | |
1477 | RETVAL = boolSV (coro->flags & ix); |
1613 | RETVAL = boolSV (coro->flags & ix); |
1478 | OUTPUT: |
1614 | OUTPUT: |
1479 | RETVAL |
1615 | RETVAL |
1480 | |
1616 | |
1481 | void |
1617 | void |
1482 | trace (Coro::State coro, int enable = 0) |
1618 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
1483 | CODE: |
|
|
1484 | if (enable) |
|
|
1485 | { |
|
|
1486 | if (coro->flags & CF_RUNNING) |
|
|
1487 | croak ("cannot enable tracing on running coroutine"); |
|
|
1488 | |
|
|
1489 | if (coro->cctx) |
|
|
1490 | croak ("cannot enable tracing on coroutine with custom stack"); |
|
|
1491 | |
|
|
1492 | coro->cctx = cctx_new (); |
|
|
1493 | coro->cctx->flags |= CC_TRACE; |
|
|
1494 | } |
|
|
1495 | else |
|
|
1496 | if (coro->cctx && coro->cctx->flags & CC_TRACE) |
|
|
1497 | { |
|
|
1498 | coro->cctx->flags &= ~CC_TRACE; |
|
|
1499 | coro->cctx->flags |= CC_NOREUSE; |
|
|
1500 | } |
|
|
1501 | |
1619 | |
1502 | SV * |
1620 | SV * |
1503 | has_stack (Coro::State coro) |
1621 | has_stack (Coro::State coro) |
1504 | PROTOTYPE: $ |
1622 | PROTOTYPE: $ |
1505 | CODE: |
1623 | CODE: |
1506 | RETVAL = boolSV (!!coro->cctx); |
1624 | RETVAL = boolSV (!!coro->cctx); |
1507 | OUTPUT: |
1625 | OUTPUT: |
1508 | RETVAL |
1626 | RETVAL |
1509 | |
1627 | |
|
|
1628 | int |
|
|
1629 | is_traced (Coro::State coro) |
|
|
1630 | PROTOTYPE: $ |
|
|
1631 | CODE: |
|
|
1632 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
|
|
1633 | OUTPUT: |
|
|
1634 | RETVAL |
|
|
1635 | |
1510 | IV |
1636 | IV |
1511 | rss (Coro::State coro) |
1637 | rss (Coro::State coro) |
1512 | PROTOTYPE: $ |
1638 | PROTOTYPE: $ |
|
|
1639 | ALIAS: |
|
|
1640 | usecount = 1 |
1513 | CODE: |
1641 | CODE: |
1514 | 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 | } |
1515 | OUTPUT: |
1647 | OUTPUT: |
1516 | RETVAL |
1648 | RETVAL |
1517 | |
1649 | |
1518 | |
1650 | |
1519 | MODULE = Coro::State PACKAGE = Coro |
1651 | MODULE = Coro::State PACKAGE = Coro |
… | |
… | |
1642 | { |
1774 | { |
1643 | struct coro *coro = SvSTATE (coro_current); |
1775 | struct coro *coro = SvSTATE (coro_current); |
1644 | |
1776 | |
1645 | sv_setsv (cb, &PL_sv_undef); |
1777 | sv_setsv (cb, &PL_sv_undef); |
1646 | |
1778 | |
1647 | if (coro_rss (coro) > SvIV (sv_pool_rss) |
1779 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
1648 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1780 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1649 | croak ("\3terminate\2\n"); |
1781 | croak ("\3terminate\2\n"); |
1650 | |
1782 | |
1651 | av_clear (GvAV (PL_defgv)); |
1783 | av_clear (GvAV (PL_defgv)); |
1652 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
1784 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
1653 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
1785 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
|
|
1786 | |
1654 | coro->save = CORO_SAVE_DEF; |
1787 | coro->save = CORO_SAVE_DEF; |
1655 | coro->prio = 0; |
1788 | coro->prio = 0; |
|
|
1789 | |
|
|
1790 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
|
|
1791 | api_trace (coro_current, 0); |
|
|
1792 | |
1656 | av_push (av_async_pool, newSVsv (coro_current)); |
1793 | av_push (av_async_pool, newSVsv (coro_current)); |
1657 | } |
1794 | } |
1658 | |
1795 | |
1659 | |
1796 | |
1660 | MODULE = Coro::State PACKAGE = Coro::AIO |
1797 | MODULE = Coro::State PACKAGE = Coro::AIO |