… | |
… | |
199 | |
199 | |
200 | #define VAR(name,type) type name; |
200 | #define VAR(name,type) type name; |
201 | # include "state.h" |
201 | # include "state.h" |
202 | #undef VAR |
202 | #undef VAR |
203 | |
203 | |
|
|
204 | /* statistics */ |
|
|
205 | int usecount; /* number of switches to this coro */ |
|
|
206 | |
204 | /* coro process data */ |
207 | /* coro process data */ |
205 | int prio; |
208 | int prio; |
206 | |
209 | |
207 | /* linked list */ |
210 | /* linked list */ |
208 | struct coro *next, *prev; |
211 | struct coro *next, *prev; |
… | |
… | |
298 | SvMAGIC (cv) \ |
301 | SvMAGIC (cv) \ |
299 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
302 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
300 | ? SvMAGIC (cv) \ |
303 | ? SvMAGIC (cv) \ |
301 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
304 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
302 | : 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)) |
303 | |
332 | |
304 | /* the next two functions merely cache the padlists */ |
333 | /* the next two functions merely cache the padlists */ |
305 | static void |
334 | static void |
306 | get_padlist (pTHX_ CV *cv) |
335 | get_padlist (pTHX_ CV *cv) |
307 | { |
336 | { |
… | |
… | |
553 | Safefree (PL_retstack); |
582 | Safefree (PL_retstack); |
554 | #endif |
583 | #endif |
555 | } |
584 | } |
556 | |
585 | |
557 | static size_t |
586 | static size_t |
558 | coro_rss (struct coro *coro) |
587 | coro_rss (pTHX_ struct coro *coro) |
559 | { |
588 | { |
560 | size_t rss = sizeof (coro); |
589 | size_t rss = sizeof (coro); |
561 | |
590 | |
562 | if (coro->mainstack) |
591 | if (coro->mainstack) |
563 | { |
592 | { |
… | |
… | |
593 | /* |
622 | /* |
594 | * emulate part of the perl startup here. |
623 | * emulate part of the perl startup here. |
595 | */ |
624 | */ |
596 | coro_init_stacks (aTHX); |
625 | coro_init_stacks (aTHX); |
597 | |
626 | |
|
|
627 | PL_runops = RUNOPS_DEFAULT; |
598 | PL_curcop = &PL_compiling; |
628 | PL_curcop = &PL_compiling; |
599 | PL_in_eval = EVAL_NULL; |
629 | PL_in_eval = EVAL_NULL; |
600 | PL_comppad = 0; |
630 | PL_comppad = 0; |
601 | PL_curpm = 0; |
631 | PL_curpm = 0; |
602 | PL_localizing = 0; |
632 | PL_localizing = 0; |
… | |
… | |
634 | coro_mortal = 0; |
664 | coro_mortal = 0; |
635 | } |
665 | } |
636 | } |
666 | } |
637 | |
667 | |
638 | static int |
668 | static int |
639 | runops_coro (pTHX) |
669 | runops_trace (pTHX) |
640 | { |
670 | { |
641 | COP *oldcop = 0; |
671 | COP *oldcop = 0; |
642 | int oldcxix = -2; |
672 | int oldcxix = -2; |
|
|
673 | struct coro *coro = SvSTATE (coro_current); /* trace cctx is tied to specific coro */ |
|
|
674 | coro_cctx *cctx = coro->cctx; |
643 | |
675 | |
644 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
676 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
645 | { |
677 | { |
646 | PERL_ASYNC_CHECK (); |
678 | PERL_ASYNC_CHECK (); |
647 | |
679 | |
648 | if (PL_op->op_type == OP_LEAVESUB) |
680 | if (cctx->flags & CC_TRACE_ALL) |
649 | { |
681 | { |
650 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
682 | if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) |
651 | SV **bot, **top; |
|
|
652 | AV *av = newAV (); /* return values */ |
|
|
653 | runops_proc_t old_runops = PL_runops; |
|
|
654 | dSP; |
|
|
655 | ENTER; |
|
|
656 | SAVETMPS; |
|
|
657 | EXTEND (SP, 3); |
|
|
658 | PL_runops = RUNOPS_DEFAULT; |
|
|
659 | |
|
|
660 | GV *gv = CvGV (cx->blk_sub.cv); |
|
|
661 | SV *fullname = sv_2mortal (newSV (0)); |
|
|
662 | if (isGV (gv)) |
|
|
663 | gv_efullname3 (fullname, gv, 0); |
|
|
664 | |
|
|
665 | bot = PL_stack_base + cx->blk_oldsp + 1; |
|
|
666 | top = cx->blk_gimme == G_ARRAY ? SP + 1 |
|
|
667 | : cx->blk_gimme == G_SCALAR ? bot + 1 |
|
|
668 | : bot; |
|
|
669 | |
|
|
670 | while (bot < top) |
|
|
671 | av_push (av, SvREFCNT_inc (*bot++)); |
|
|
672 | |
|
|
673 | PUSHMARK (SP); |
|
|
674 | PUSHs (&PL_sv_no); |
|
|
675 | PUSHs (fullname); |
|
|
676 | PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
|
|
677 | PUTBACK; |
|
|
678 | call_pv ("Coro::_do_trace_sub", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
679 | SPAGAIN; |
|
|
680 | |
|
|
681 | FREETMPS; |
|
|
682 | LEAVE; |
|
|
683 | PL_runops = old_runops; |
|
|
684 | } |
|
|
685 | |
|
|
686 | if (oldcop != PL_curcop) |
|
|
687 | { |
|
|
688 | oldcop = PL_curcop; |
|
|
689 | |
|
|
690 | if (PL_curcop != &PL_compiling) |
|
|
691 | { |
683 | { |
692 | runops_proc_t old_runops = PL_runops; |
684 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
|
|
685 | SV **bot, **top; |
|
|
686 | AV *av = newAV (); /* return values */ |
|
|
687 | SV **cb; |
693 | dSP; |
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; |
694 | ENTER; |
704 | ENTER; |
695 | SAVETMPS; |
705 | SAVETMPS; |
696 | EXTEND (SP, 3); |
706 | EXTEND (SP, 3); |
697 | PL_runops = RUNOPS_DEFAULT; |
|
|
698 | |
|
|
699 | if (oldcxix != cxstack_ix) |
|
|
700 | { |
|
|
701 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
|
|
702 | |
|
|
703 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
|
|
704 | { |
|
|
705 | GV *gv = CvGV (cx->blk_sub.cv); |
|
|
706 | SV *fullname = sv_2mortal (newSV (0)); |
|
|
707 | if (isGV (gv)) |
|
|
708 | gv_efullname3 (fullname, gv, 0); |
|
|
709 | |
|
|
710 | PUSHMARK (SP); |
|
|
711 | PUSHs (&PL_sv_yes); |
|
|
712 | PUSHs (fullname); |
|
|
713 | PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
|
|
714 | PUTBACK; |
|
|
715 | call_pv ("Coro::_do_trace_sub", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
716 | SPAGAIN; |
|
|
717 | } |
|
|
718 | |
|
|
719 | oldcxix = cxstack_ix; |
|
|
720 | } |
|
|
721 | |
|
|
722 | if (0) { |
|
|
723 | PUSHMARK (SP); |
707 | PUSHMARK (SP); |
724 | PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); |
708 | PUSHs (&PL_sv_no); |
725 | PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); |
709 | PUSHs (fullname); |
|
|
710 | PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
726 | PUTBACK; |
711 | PUTBACK; |
|
|
712 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
727 | call_pv ("Coro::_do_trace_line", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
713 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
728 | SPAGAIN; |
714 | SPAGAIN; |
729 | } |
|
|
730 | |
|
|
731 | FREETMPS; |
715 | FREETMPS; |
732 | LEAVE; |
716 | LEAVE; |
733 | PL_runops = old_runops; |
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 | } |
734 | } |
783 | } |
735 | } |
784 | } |
736 | } |
785 | } |
737 | |
786 | |
738 | TAINT_NOT; |
787 | TAINT_NOT; |
… | |
… | |
749 | LOGOP myop; |
798 | LOGOP myop; |
750 | |
799 | |
751 | PL_top_env = &PL_start_env; |
800 | PL_top_env = &PL_start_env; |
752 | |
801 | |
753 | if (cctx->flags & CC_TRACE) |
802 | if (cctx->flags & CC_TRACE) |
754 | PL_runops = runops_coro; |
803 | PL_runops = runops_trace; |
755 | |
804 | |
756 | Zero (&myop, 1, LOGOP); |
805 | Zero (&myop, 1, LOGOP); |
757 | myop.op_next = PL_op; |
806 | myop.op_next = PL_op; |
758 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
807 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
759 | |
808 | |
… | |
… | |
905 | cctx_first = cctx; |
954 | cctx_first = cctx; |
906 | } |
955 | } |
907 | |
956 | |
908 | /** coroutine switching *****************************************************/ |
957 | /** coroutine switching *****************************************************/ |
909 | |
958 | |
910 | /* 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 */ |
911 | static void NOINLINE |
979 | static void NOINLINE |
912 | transfer (pTHX_ struct coro *prev, struct coro *next) |
980 | transfer (pTHX_ struct coro *prev, struct coro *next) |
913 | { |
981 | { |
914 | dSTACKLEVEL; |
982 | dSTACKLEVEL; |
915 | |
983 | |
… | |
… | |
928 | /* create a new empty context */ |
996 | /* create a new empty context */ |
929 | Newz (0, prev->cctx, 1, coro_cctx); |
997 | Newz (0, prev->cctx, 1, coro_cctx); |
930 | prev->flags &= ~CF_NEW; |
998 | prev->flags &= ~CF_NEW; |
931 | prev->flags |= CF_RUNNING; |
999 | prev->flags |= CF_RUNNING; |
932 | } |
1000 | } |
933 | |
|
|
934 | /*TODO: must not croak here */ |
|
|
935 | if (!prev->flags & CF_RUNNING) |
|
|
936 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
|
|
937 | |
|
|
938 | if (next->flags & CF_RUNNING) |
|
|
939 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
|
|
940 | |
|
|
941 | if (next->flags & CF_DESTROYED) |
|
|
942 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
|
|
943 | |
1001 | |
944 | prev->flags &= ~CF_RUNNING; |
1002 | prev->flags &= ~CF_RUNNING; |
945 | next->flags |= CF_RUNNING; |
1003 | next->flags |= CF_RUNNING; |
946 | |
1004 | |
947 | LOCK; |
1005 | LOCK; |
… | |
… | |
963 | } |
1021 | } |
964 | |
1022 | |
965 | prev__cctx = prev->cctx; |
1023 | prev__cctx = prev->cctx; |
966 | |
1024 | |
967 | /* possibly "free" the cctx */ |
1025 | /* possibly "free" the cctx */ |
968 | if (prev__cctx->idle_sp == STACKLEVEL) |
1026 | if (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE)) |
969 | { |
1027 | { |
970 | /* 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 */ |
971 | 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)); |
972 | |
1030 | |
973 | prev->cctx = 0; |
1031 | prev->cctx = 0; |
974 | |
1032 | |
975 | cctx_put (prev__cctx); |
1033 | cctx_put (prev__cctx); |
976 | } |
1034 | } |
|
|
1035 | |
|
|
1036 | ++next->usecount; |
977 | |
1037 | |
978 | if (!next->cctx) |
1038 | if (!next->cctx) |
979 | next->cctx = cctx_get (aTHX); |
1039 | next->cctx = cctx_get (aTHX); |
980 | |
1040 | |
981 | if (prev__cctx != next->cctx) |
1041 | if (prev__cctx != next->cctx) |
… | |
… | |
994 | { |
1054 | { |
995 | struct coro *prev, *next; |
1055 | struct coro *prev, *next; |
996 | }; |
1056 | }; |
997 | |
1057 | |
998 | #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) |
999 | |
1060 | |
1000 | /** high level stuff ********************************************************/ |
1061 | /** high level stuff ********************************************************/ |
1001 | |
1062 | |
1002 | static int |
1063 | static int |
1003 | coro_state_destroy (pTHX_ struct coro *coro) |
1064 | coro_state_destroy (pTHX_ struct coro *coro) |
… | |
… | |
1086 | #else |
1147 | #else |
1087 | # define MGf_DUP 0 |
1148 | # define MGf_DUP 0 |
1088 | #endif |
1149 | #endif |
1089 | }; |
1150 | }; |
1090 | |
1151 | |
1091 | static struct coro * |
|
|
1092 | SvSTATE_ (pTHX_ SV *coro) |
|
|
1093 | { |
|
|
1094 | HV *stash; |
|
|
1095 | MAGIC *mg; |
|
|
1096 | |
|
|
1097 | if (SvROK (coro)) |
|
|
1098 | coro = SvRV (coro); |
|
|
1099 | |
|
|
1100 | stash = SvSTASH (coro); |
|
|
1101 | if (stash != coro_stash && stash != coro_state_stash) |
|
|
1102 | { |
|
|
1103 | /* very slow, but rare, check */ |
|
|
1104 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
1105 | croak ("Coro::State object required"); |
|
|
1106 | } |
|
|
1107 | |
|
|
1108 | mg = CORO_MAGIC (coro); |
|
|
1109 | return (struct coro *)mg->mg_ptr; |
|
|
1110 | } |
|
|
1111 | |
|
|
1112 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
1113 | |
|
|
1114 | static void |
1152 | static void |
1115 | 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) |
1116 | { |
1154 | { |
1117 | ta->prev = SvSTATE (prev_sv); |
1155 | ta->prev = SvSTATE (prev_sv); |
1118 | ta->next = SvSTATE (next_sv); |
1156 | ta->next = SvSTATE (next_sv); |
|
|
1157 | TRANSFER_CHECK (*ta); |
1119 | } |
1158 | } |
1120 | |
1159 | |
1121 | static void |
1160 | static void |
1122 | api_transfer (SV *prev_sv, SV *next_sv) |
1161 | api_transfer (SV *prev_sv, SV *next_sv) |
1123 | { |
1162 | { |
… | |
… | |
1240 | break; |
1279 | break; |
1241 | } |
1280 | } |
1242 | |
1281 | |
1243 | /* free this only after the transfer */ |
1282 | /* free this only after the transfer */ |
1244 | prev_sv = SvRV (coro_current); |
1283 | prev_sv = SvRV (coro_current); |
1245 | SvRV_set (coro_current, next_sv); |
|
|
1246 | ta->prev = SvSTATE (prev_sv); |
1284 | ta->prev = SvSTATE (prev_sv); |
1247 | |
1285 | TRANSFER_CHECK (*ta); |
1248 | assert (ta->next->flags & CF_READY); |
1286 | assert (ta->next->flags & CF_READY); |
1249 | ta->next->flags &= ~CF_READY; |
1287 | ta->next->flags &= ~CF_READY; |
|
|
1288 | SvRV_set (coro_current, next_sv); |
1250 | |
1289 | |
1251 | LOCK; |
1290 | LOCK; |
1252 | free_coro_mortal (aTHX); |
1291 | free_coro_mortal (aTHX); |
1253 | coro_mortal = prev_sv; |
1292 | coro_mortal = prev_sv; |
1254 | UNLOCK; |
1293 | UNLOCK; |
… | |
… | |
1315 | } |
1354 | } |
1316 | else |
1355 | else |
1317 | return 0; |
1356 | return 0; |
1318 | } |
1357 | } |
1319 | |
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 | |
1320 | MODULE = Coro::State PACKAGE = Coro::State |
1385 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
1321 | |
1386 | |
1322 | PROTOTYPES: DISABLE |
1387 | PROTOTYPES: DISABLE |
1323 | |
1388 | |
1324 | BOOT: |
1389 | BOOT: |
1325 | { |
1390 | { |
… | |
… | |
1533 | save_perl (aTHX_ coro); |
1598 | save_perl (aTHX_ coro); |
1534 | load_perl (aTHX_ &temp); |
1599 | load_perl (aTHX_ &temp); |
1535 | } |
1600 | } |
1536 | } |
1601 | } |
1537 | } |
1602 | } |
1538 | |
1603 | |
1539 | SV * |
1604 | SV * |
1540 | is_ready (Coro::State coro) |
1605 | is_ready (Coro::State coro) |
1541 | PROTOTYPE: $ |
1606 | PROTOTYPE: $ |
1542 | ALIAS: |
1607 | ALIAS: |
1543 | is_ready = CF_READY |
1608 | is_ready = CF_READY |
… | |
… | |
1548 | RETVAL = boolSV (coro->flags & ix); |
1613 | RETVAL = boolSV (coro->flags & ix); |
1549 | OUTPUT: |
1614 | OUTPUT: |
1550 | RETVAL |
1615 | RETVAL |
1551 | |
1616 | |
1552 | void |
1617 | void |
1553 | trace (Coro::State coro, int flags = CC_TRACE | CC_TRACE_SUB) |
1618 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
1554 | CODE: |
|
|
1555 | if (flags & CC_TRACE) |
|
|
1556 | { |
|
|
1557 | if (!coro->cctx) |
|
|
1558 | coro->cctx = cctx_new (); |
|
|
1559 | else if (!(coro->cctx->flags & CC_TRACE)) |
|
|
1560 | croak ("cannot enable tracing on coroutine with custom stack"); |
|
|
1561 | |
|
|
1562 | coro->cctx->flags |= flags & (CC_TRACE | CC_TRACE_ALL); |
|
|
1563 | } |
|
|
1564 | else |
|
|
1565 | if (coro->cctx && coro->cctx->flags & CC_TRACE) |
|
|
1566 | { |
|
|
1567 | coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL); |
|
|
1568 | coro->cctx->flags |= CC_NOREUSE; |
|
|
1569 | } |
|
|
1570 | |
1619 | |
1571 | SV * |
1620 | SV * |
1572 | has_stack (Coro::State coro) |
1621 | has_stack (Coro::State coro) |
1573 | PROTOTYPE: $ |
1622 | PROTOTYPE: $ |
1574 | CODE: |
1623 | CODE: |
… | |
… | |
1585 | RETVAL |
1634 | RETVAL |
1586 | |
1635 | |
1587 | IV |
1636 | IV |
1588 | rss (Coro::State coro) |
1637 | rss (Coro::State coro) |
1589 | PROTOTYPE: $ |
1638 | PROTOTYPE: $ |
|
|
1639 | ALIAS: |
|
|
1640 | usecount = 1 |
1590 | CODE: |
1641 | CODE: |
1591 | 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 | } |
1592 | OUTPUT: |
1647 | OUTPUT: |
1593 | RETVAL |
1648 | RETVAL |
1594 | |
1649 | |
1595 | |
1650 | |
1596 | MODULE = Coro::State PACKAGE = Coro |
1651 | MODULE = Coro::State PACKAGE = Coro |
… | |
… | |
1719 | { |
1774 | { |
1720 | struct coro *coro = SvSTATE (coro_current); |
1775 | struct coro *coro = SvSTATE (coro_current); |
1721 | |
1776 | |
1722 | sv_setsv (cb, &PL_sv_undef); |
1777 | sv_setsv (cb, &PL_sv_undef); |
1723 | |
1778 | |
1724 | if (coro_rss (coro) > SvIV (sv_pool_rss) |
1779 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
1725 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1780 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1726 | croak ("\3terminate\2\n"); |
1781 | croak ("\3terminate\2\n"); |
1727 | |
1782 | |
1728 | av_clear (GvAV (PL_defgv)); |
1783 | av_clear (GvAV (PL_defgv)); |
1729 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
1784 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
1730 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
1785 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
|
|
1786 | |
1731 | coro->save = CORO_SAVE_DEF; |
1787 | coro->save = CORO_SAVE_DEF; |
1732 | coro->prio = 0; |
1788 | coro->prio = 0; |
|
|
1789 | |
|
|
1790 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
|
|
1791 | api_trace (coro_current, 0); |
|
|
1792 | |
1733 | av_push (av_async_pool, newSVsv (coro_current)); |
1793 | av_push (av_async_pool, newSVsv (coro_current)); |
1734 | } |
1794 | } |
1735 | |
1795 | |
1736 | |
1796 | |
1737 | MODULE = Coro::State PACKAGE = Coro::AIO |
1797 | MODULE = Coro::State PACKAGE = Coro::AIO |