… | |
… | |
299 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
299 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
300 | ? SvMAGIC (cv) \ |
300 | ? SvMAGIC (cv) \ |
301 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
301 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
302 | : 0 |
302 | : 0 |
303 | |
303 | |
|
|
304 | static struct coro * |
|
|
305 | SvSTATE_ (pTHX_ SV *coro) |
|
|
306 | { |
|
|
307 | HV *stash; |
|
|
308 | MAGIC *mg; |
|
|
309 | |
|
|
310 | if (SvROK (coro)) |
|
|
311 | coro = SvRV (coro); |
|
|
312 | |
|
|
313 | stash = SvSTASH (coro); |
|
|
314 | if (stash != coro_stash && stash != coro_state_stash) |
|
|
315 | { |
|
|
316 | /* very slow, but rare, check */ |
|
|
317 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
318 | croak ("Coro::State object required"); |
|
|
319 | } |
|
|
320 | |
|
|
321 | mg = CORO_MAGIC (coro); |
|
|
322 | return (struct coro *)mg->mg_ptr; |
|
|
323 | } |
|
|
324 | |
|
|
325 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
326 | |
304 | /* the next two functions merely cache the padlists */ |
327 | /* the next two functions merely cache the padlists */ |
305 | static void |
328 | static void |
306 | get_padlist (pTHX_ CV *cv) |
329 | get_padlist (pTHX_ CV *cv) |
307 | { |
330 | { |
308 | MAGIC *mg = CORO_MAGIC (cv); |
331 | MAGIC *mg = CORO_MAGIC (cv); |
… | |
… | |
593 | /* |
616 | /* |
594 | * emulate part of the perl startup here. |
617 | * emulate part of the perl startup here. |
595 | */ |
618 | */ |
596 | coro_init_stacks (aTHX); |
619 | coro_init_stacks (aTHX); |
597 | |
620 | |
|
|
621 | PL_runops = RUNOPS_DEFAULT; |
598 | PL_curcop = &PL_compiling; |
622 | PL_curcop = &PL_compiling; |
599 | PL_in_eval = EVAL_NULL; |
623 | PL_in_eval = EVAL_NULL; |
600 | PL_comppad = 0; |
624 | PL_comppad = 0; |
601 | PL_curpm = 0; |
625 | PL_curpm = 0; |
602 | PL_localizing = 0; |
626 | PL_localizing = 0; |
… | |
… | |
634 | coro_mortal = 0; |
658 | coro_mortal = 0; |
635 | } |
659 | } |
636 | } |
660 | } |
637 | |
661 | |
638 | static int |
662 | static int |
639 | runops_coro (pTHX) |
663 | runops_trace (pTHX) |
640 | { |
664 | { |
641 | COP *oldcop = 0; |
665 | COP *oldcop = 0; |
642 | int oldcxix = -2; |
666 | int oldcxix = -2; |
|
|
667 | struct coro *coro = SvSTATE (coro_current); /* trace cctx is tied to specific coro */ |
|
|
668 | coro_cctx *cctx = coro->cctx; |
643 | |
669 | |
644 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
670 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
645 | { |
671 | { |
646 | PERL_ASYNC_CHECK (); |
672 | PERL_ASYNC_CHECK (); |
647 | |
673 | |
648 | if (PL_op->op_type == OP_LEAVESUB) |
674 | if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) |
649 | { |
675 | { |
650 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
676 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
651 | SV **bot, **top; |
677 | SV **bot, **top; |
652 | AV *av = newAV (); /* return values */ |
678 | AV *av = newAV (); /* return values */ |
|
|
679 | SV **cb; |
653 | runops_proc_t old_runops = PL_runops; |
680 | runops_proc_t old_runops = PL_runops; |
654 | dSP; |
681 | dSP; |
655 | ENTER; |
682 | ENTER; |
656 | SAVETMPS; |
683 | SAVETMPS; |
657 | EXTEND (SP, 3); |
684 | EXTEND (SP, 3); |
… | |
… | |
673 | PUSHMARK (SP); |
700 | PUSHMARK (SP); |
674 | PUSHs (&PL_sv_no); |
701 | PUSHs (&PL_sv_no); |
675 | PUSHs (fullname); |
702 | PUSHs (fullname); |
676 | PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
703 | PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
677 | PUTBACK; |
704 | PUTBACK; |
|
|
705 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
678 | call_pv ("Coro::_do_trace_sub", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
706 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
679 | SPAGAIN; |
707 | SPAGAIN; |
680 | |
708 | |
681 | FREETMPS; |
709 | FREETMPS; |
682 | LEAVE; |
710 | LEAVE; |
683 | PL_runops = old_runops; |
711 | PL_runops = old_runops; |
… | |
… | |
687 | { |
715 | { |
688 | oldcop = PL_curcop; |
716 | oldcop = PL_curcop; |
689 | |
717 | |
690 | if (PL_curcop != &PL_compiling) |
718 | if (PL_curcop != &PL_compiling) |
691 | { |
719 | { |
|
|
720 | SV **cb; |
692 | runops_proc_t old_runops = PL_runops; |
721 | runops_proc_t old_runops = PL_runops; |
693 | dSP; |
722 | dSP; |
694 | ENTER; |
723 | ENTER; |
695 | SAVETMPS; |
724 | SAVETMPS; |
696 | EXTEND (SP, 3); |
725 | EXTEND (SP, 3); |
697 | PL_runops = RUNOPS_DEFAULT; |
726 | PL_runops = RUNOPS_DEFAULT; |
698 | |
727 | |
699 | if (oldcxix != cxstack_ix) |
728 | if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) |
700 | { |
729 | { |
701 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
730 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
702 | |
731 | |
703 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
732 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
704 | { |
733 | { |
… | |
… | |
710 | PUSHMARK (SP); |
739 | PUSHMARK (SP); |
711 | PUSHs (&PL_sv_yes); |
740 | PUSHs (&PL_sv_yes); |
712 | PUSHs (fullname); |
741 | PUSHs (fullname); |
713 | PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
742 | PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
714 | PUTBACK; |
743 | PUTBACK; |
|
|
744 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
715 | call_pv ("Coro::_do_trace_sub", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
745 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
716 | SPAGAIN; |
746 | SPAGAIN; |
717 | } |
747 | } |
718 | |
748 | |
719 | oldcxix = cxstack_ix; |
749 | oldcxix = cxstack_ix; |
720 | } |
750 | } |
721 | |
751 | |
|
|
752 | if (cctx->flags & CC_TRACE_LINE) |
722 | if (0) { |
753 | { |
723 | PUSHMARK (SP); |
754 | PUSHMARK (SP); |
724 | PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); |
755 | PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); |
725 | PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); |
756 | PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); |
726 | PUTBACK; |
757 | PUTBACK; |
|
|
758 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0); |
727 | call_pv ("Coro::_do_trace_line", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
759 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
728 | SPAGAIN; |
760 | SPAGAIN; |
729 | } |
761 | } |
730 | |
762 | |
731 | FREETMPS; |
763 | FREETMPS; |
732 | LEAVE; |
764 | LEAVE; |
733 | PL_runops = old_runops; |
765 | PL_runops = old_runops; |
734 | } |
766 | } |
… | |
… | |
749 | LOGOP myop; |
781 | LOGOP myop; |
750 | |
782 | |
751 | PL_top_env = &PL_start_env; |
783 | PL_top_env = &PL_start_env; |
752 | |
784 | |
753 | if (cctx->flags & CC_TRACE) |
785 | if (cctx->flags & CC_TRACE) |
754 | PL_runops = runops_coro; |
786 | PL_runops = runops_trace; |
755 | |
787 | |
756 | Zero (&myop, 1, LOGOP); |
788 | Zero (&myop, 1, LOGOP); |
757 | myop.op_next = PL_op; |
789 | myop.op_next = PL_op; |
758 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
790 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
759 | |
791 | |
… | |
… | |
905 | cctx_first = cctx; |
937 | cctx_first = cctx; |
906 | } |
938 | } |
907 | |
939 | |
908 | /** coroutine switching *****************************************************/ |
940 | /** coroutine switching *****************************************************/ |
909 | |
941 | |
910 | /* never call directly, always through the coro_state_transfer global variable */ |
942 | static void NOINLINE |
|
|
943 | transfer_check (pTHX_ struct coro *prev, struct coro *next) |
|
|
944 | { |
|
|
945 | if (prev != next) |
|
|
946 | { |
|
|
947 | if (!(prev->flags & (CF_RUNNING | CF_NEW))) |
|
|
948 | croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states"); |
|
|
949 | |
|
|
950 | if (next->flags & CF_RUNNING) |
|
|
951 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
|
|
952 | |
|
|
953 | if (next->flags & CF_DESTROYED) |
|
|
954 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
|
|
955 | |
|
|
956 | if (PL_lex_state != LEX_NOTPARSING) |
|
|
957 | croak ("Coro::State::transfer called while parsing, but this is not supported"); |
|
|
958 | } |
|
|
959 | } |
|
|
960 | |
|
|
961 | /* always use the TRANSFER macro */ |
911 | static void NOINLINE |
962 | static void NOINLINE |
912 | transfer (pTHX_ struct coro *prev, struct coro *next) |
963 | transfer (pTHX_ struct coro *prev, struct coro *next) |
913 | { |
964 | { |
914 | dSTACKLEVEL; |
965 | dSTACKLEVEL; |
915 | |
966 | |
… | |
… | |
928 | /* create a new empty context */ |
979 | /* create a new empty context */ |
929 | Newz (0, prev->cctx, 1, coro_cctx); |
980 | Newz (0, prev->cctx, 1, coro_cctx); |
930 | prev->flags &= ~CF_NEW; |
981 | prev->flags &= ~CF_NEW; |
931 | prev->flags |= CF_RUNNING; |
982 | prev->flags |= CF_RUNNING; |
932 | } |
983 | } |
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 | |
984 | |
944 | prev->flags &= ~CF_RUNNING; |
985 | prev->flags &= ~CF_RUNNING; |
945 | next->flags |= CF_RUNNING; |
986 | next->flags |= CF_RUNNING; |
946 | |
987 | |
947 | LOCK; |
988 | LOCK; |
… | |
… | |
994 | { |
1035 | { |
995 | struct coro *prev, *next; |
1036 | struct coro *prev, *next; |
996 | }; |
1037 | }; |
997 | |
1038 | |
998 | #define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) |
1039 | #define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) |
|
|
1040 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
999 | |
1041 | |
1000 | /** high level stuff ********************************************************/ |
1042 | /** high level stuff ********************************************************/ |
1001 | |
1043 | |
1002 | static int |
1044 | static int |
1003 | coro_state_destroy (pTHX_ struct coro *coro) |
1045 | coro_state_destroy (pTHX_ struct coro *coro) |
… | |
… | |
1086 | #else |
1128 | #else |
1087 | # define MGf_DUP 0 |
1129 | # define MGf_DUP 0 |
1088 | #endif |
1130 | #endif |
1089 | }; |
1131 | }; |
1090 | |
1132 | |
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 |
1133 | static void |
1115 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
1134 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
1116 | { |
1135 | { |
1117 | ta->prev = SvSTATE (prev_sv); |
1136 | ta->prev = SvSTATE (prev_sv); |
1118 | ta->next = SvSTATE (next_sv); |
1137 | ta->next = SvSTATE (next_sv); |
|
|
1138 | TRANSFER_CHECK (*ta); |
1119 | } |
1139 | } |
1120 | |
1140 | |
1121 | static void |
1141 | static void |
1122 | api_transfer (SV *prev_sv, SV *next_sv) |
1142 | api_transfer (SV *prev_sv, SV *next_sv) |
1123 | { |
1143 | { |
… | |
… | |
1240 | break; |
1260 | break; |
1241 | } |
1261 | } |
1242 | |
1262 | |
1243 | /* free this only after the transfer */ |
1263 | /* free this only after the transfer */ |
1244 | prev_sv = SvRV (coro_current); |
1264 | prev_sv = SvRV (coro_current); |
1245 | SvRV_set (coro_current, next_sv); |
|
|
1246 | ta->prev = SvSTATE (prev_sv); |
1265 | ta->prev = SvSTATE (prev_sv); |
1247 | |
1266 | TRANSFER_CHECK (*ta); |
1248 | assert (ta->next->flags & CF_READY); |
1267 | assert (ta->next->flags & CF_READY); |
1249 | ta->next->flags &= ~CF_READY; |
1268 | ta->next->flags &= ~CF_READY; |
|
|
1269 | SvRV_set (coro_current, next_sv); |
1250 | |
1270 | |
1251 | LOCK; |
1271 | LOCK; |
1252 | free_coro_mortal (aTHX); |
1272 | free_coro_mortal (aTHX); |
1253 | coro_mortal = prev_sv; |
1273 | coro_mortal = prev_sv; |
1254 | UNLOCK; |
1274 | UNLOCK; |