… | |
… | |
116 | # define CORO_CLOCK_MONOTONIC 1 |
116 | # define CORO_CLOCK_MONOTONIC 1 |
117 | # define CORO_CLOCK_THREAD_CPUTIME_ID 3 |
117 | # define CORO_CLOCK_THREAD_CPUTIME_ID 3 |
118 | # endif |
118 | # endif |
119 | #endif |
119 | #endif |
120 | |
120 | |
|
|
121 | /* perl usually suppressed asserts. for debugging, we sometimes force it to be on */ |
|
|
122 | #if 0 |
|
|
123 | # undef NDEBUG |
|
|
124 | # include <assert.h> |
|
|
125 | #endif |
|
|
126 | |
121 | static double (*nvtime)(); /* so why doesn't it take void? */ |
127 | static double (*nvtime)(); /* so why doesn't it take void? */ |
122 | static void (*u2time)(pTHX_ UV ret[2]); |
128 | static void (*u2time)(pTHX_ UV ret[2]); |
123 | |
129 | |
124 | /* we hijack an hopefully unused CV flag for our purposes */ |
130 | /* we hijack an hopefully unused CV flag for our purposes */ |
125 | #define CVf_SLF 0x4000 |
131 | #define CVf_SLF 0x4000 |
… | |
… | |
963 | #endif |
969 | #endif |
964 | |
970 | |
965 | New(54,PL_savestack,24,ANY); |
971 | New(54,PL_savestack,24,ANY); |
966 | PL_savestack_ix = 0; |
972 | PL_savestack_ix = 0; |
967 | PL_savestack_max = 24; |
973 | PL_savestack_max = 24; |
968 | #if !PERL_VERSION_ATLEAST (5,24,0) |
974 | #if PERL_VERSION_ATLEAST (5,24,0) |
969 | /* perl 5.24 moves SS_MAXPUSH optimisation from */ |
975 | /* perl 5.24 moves SS_MAXPUSH optimisation from */ |
970 | /* the header macros to PL_savestack_max */ |
976 | /* the header macros to PL_savestack_max */ |
971 | PL_savestack_max -= SS_MAXPUSH; |
977 | PL_savestack_max -= SS_MAXPUSH; |
972 | #endif |
978 | #endif |
973 | |
979 | |
… | |
… | |
1043 | } |
1049 | } |
1044 | |
1050 | |
1045 | return rss; |
1051 | return rss; |
1046 | } |
1052 | } |
1047 | |
1053 | |
1048 | /** coroutine stack handling ************************************************/ |
1054 | /** provide custom get/set/clear methods for %SIG elements ******************/ |
1049 | |
|
|
1050 | static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); |
|
|
1051 | static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); |
|
|
1052 | static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg); |
|
|
1053 | |
1055 | |
1054 | /* apparently < 5.8.8 */ |
1056 | /* apparently < 5.8.8 */ |
1055 | #ifndef MgPV_nolen_const |
1057 | #ifndef MgPV_nolen_const |
1056 | #define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ |
1058 | #define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ |
1057 | SvPV_nolen((SV*)((mg)->mg_ptr)) : \ |
1059 | SvPV_nolen((SV*)((mg)->mg_ptr)) : \ |
1058 | (const char*)(mg)->mg_ptr) |
1060 | (const char*)(mg)->mg_ptr) |
1059 | #endif |
1061 | #endif |
|
|
1062 | |
|
|
1063 | /* this will be a patched copy of PL_vtbl_sigelem */ |
|
|
1064 | static MGVTBL coro_sigelem_vtbl; |
|
|
1065 | |
|
|
1066 | static int ecb_cold |
|
|
1067 | coro_sig_copy (pTHX_ SV *sv, MAGIC *mg, SV *nsv, const char *name, I32 namlen) |
|
|
1068 | { |
|
|
1069 | char *key = SvPV_nolen ((SV *)name); |
|
|
1070 | |
|
|
1071 | /* do what mg_copy normally does */ |
|
|
1072 | sv_magic (nsv, mg->mg_obj, PERL_MAGIC_sigelem, name, namlen); |
|
|
1073 | assert (mg_find (nsv, PERL_MAGIC_sigelem)->mg_virtual == &PL_vtbl_sigelem); |
|
|
1074 | |
|
|
1075 | /* patch sigelem vtbl, but only for __WARN__ and __DIE__ */ |
|
|
1076 | if (*key == '_' |
|
|
1077 | && (strEQ (key, "__DIE__") |
|
|
1078 | || strEQ (key, "__WARN__"))) |
|
|
1079 | mg_find (nsv, PERL_MAGIC_sigelem)->mg_virtual = &coro_sigelem_vtbl; |
|
|
1080 | |
|
|
1081 | return 1; |
|
|
1082 | } |
|
|
1083 | |
|
|
1084 | /* perl does not have a %SIG vtbl, we provide one so we can override */ |
|
|
1085 | /* the magic vtbl for the __DIE__ and __WARN__ members */ |
|
|
1086 | static const MGVTBL coro_sig_vtbl = { |
|
|
1087 | 0, 0, 0, 0, 0, |
|
|
1088 | coro_sig_copy |
|
|
1089 | }; |
1060 | |
1090 | |
1061 | /* |
1091 | /* |
1062 | * This overrides the default magic get method of %SIG elements. |
1092 | * This overrides the default magic get method of %SIG elements. |
1063 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
1093 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
1064 | * and instead of trying to save and restore the hash elements (extremely slow), |
1094 | * and instead of trying to save and restore the hash elements (extremely slow), |
… | |
… | |
1066 | */ |
1096 | */ |
1067 | static int ecb_cold |
1097 | static int ecb_cold |
1068 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
1098 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
1069 | { |
1099 | { |
1070 | const char *s = MgPV_nolen_const (mg); |
1100 | const char *s = MgPV_nolen_const (mg); |
|
|
1101 | /* the key must be either __DIE__ or __WARN__ here */ |
|
|
1102 | SV **svp = s[2] == 'D' ? &PL_diehook : &PL_warnhook; |
1071 | |
1103 | |
1072 | if (*s == '_') |
1104 | SV *ssv; |
1073 | { |
|
|
1074 | SV **svp = 0; |
|
|
1075 | |
1105 | |
1076 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
|
|
1077 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
|
|
1078 | |
|
|
1079 | if (svp) |
1106 | if (!*svp) |
1080 | { |
|
|
1081 | SV *ssv; |
|
|
1082 | |
|
|
1083 | if (!*svp) |
|
|
1084 | ssv = &PL_sv_undef; |
1107 | ssv = &PL_sv_undef; |
1085 | else if (SvTYPE (*svp) == SVt_PVCV) /* perlio directly stores a CV in warnhook. ugh. */ |
1108 | else if (SvTYPE (*svp) == SVt_PVCV) /* perlio directly stores a CV in warnhook. ugh. */ |
1086 | ssv = sv_2mortal (newRV_inc (*svp)); |
1109 | ssv = sv_2mortal (newRV_inc (*svp)); |
1087 | else |
1110 | else |
1088 | ssv = *svp; |
1111 | ssv = *svp; |
1089 | |
1112 | |
1090 | sv_setsv (sv, ssv); |
1113 | sv_setsv (sv, ssv); |
1091 | return 0; |
1114 | return 0; |
1092 | } |
|
|
1093 | } |
|
|
1094 | |
|
|
1095 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
|
|
1096 | } |
1115 | } |
1097 | |
1116 | |
1098 | static int ecb_cold |
1117 | static int ecb_cold |
1099 | coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) |
1118 | coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) |
1100 | { |
1119 | { |
1101 | const char *s = MgPV_nolen_const (mg); |
1120 | const char *s = MgPV_nolen_const (mg); |
|
|
1121 | /* the key must be either __DIE__ or __WARN__ here */ |
|
|
1122 | SV **svp = s[2] == 'D' ? &PL_diehook : &PL_warnhook; |
1102 | |
1123 | |
1103 | if (*s == '_') |
|
|
1104 | { |
|
|
1105 | SV **svp = 0; |
|
|
1106 | |
|
|
1107 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
|
|
1108 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
|
|
1109 | |
|
|
1110 | if (svp) |
|
|
1111 | { |
|
|
1112 | SV *old = *svp; |
1124 | SV *old = *svp; |
1113 | *svp = 0; |
1125 | *svp = 0; |
1114 | SvREFCNT_dec (old); |
1126 | SvREFCNT_dec (old); |
1115 | return 0; |
1127 | return 0; |
1116 | } |
|
|
1117 | } |
|
|
1118 | |
|
|
1119 | return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0; |
|
|
1120 | } |
1128 | } |
1121 | |
1129 | |
1122 | static int ecb_cold |
1130 | static int ecb_cold |
1123 | coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) |
1131 | coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) |
1124 | { |
1132 | { |
1125 | const char *s = MgPV_nolen_const (mg); |
1133 | const char *s = MgPV_nolen_const (mg); |
|
|
1134 | /* the key must be either __DIE__ or __WARN__ here */ |
|
|
1135 | SV **svp = s[2] == 'D' ? &PL_diehook : &PL_warnhook; |
1126 | |
1136 | |
1127 | if (*s == '_') |
|
|
1128 | { |
|
|
1129 | SV **svp = 0; |
|
|
1130 | |
|
|
1131 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
|
|
1132 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
|
|
1133 | |
|
|
1134 | if (svp) |
|
|
1135 | { |
|
|
1136 | SV *old = *svp; |
1137 | SV *old = *svp; |
1137 | *svp = SvOK (sv) ? newSVsv (sv) : 0; |
1138 | *svp = SvOK (sv) ? newSVsv (sv) : 0; |
1138 | SvREFCNT_dec (old); |
1139 | SvREFCNT_dec (old); |
1139 | return 0; |
1140 | return 0; |
1140 | } |
|
|
1141 | } |
|
|
1142 | |
|
|
1143 | return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; |
|
|
1144 | } |
1141 | } |
1145 | |
1142 | |
1146 | static void |
1143 | static void |
1147 | prepare_nop (pTHX_ struct coro_transfer_args *ta) |
1144 | prepare_nop (pTHX_ struct coro_transfer_args *ta) |
1148 | { |
1145 | { |
… | |
… | |
1159 | static int |
1156 | static int |
1160 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
1157 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
1161 | { |
1158 | { |
1162 | return 1; |
1159 | return 1; |
1163 | } |
1160 | } |
|
|
1161 | |
|
|
1162 | /** coroutine stack handling ************************************************/ |
1164 | |
1163 | |
1165 | static UNOP init_perl_op; |
1164 | static UNOP init_perl_op; |
1166 | |
1165 | |
1167 | ecb_noinline static void /* noinline to keep it out of the transfer fast path */ |
1166 | ecb_noinline static void /* noinline to keep it out of the transfer fast path */ |
1168 | init_perl (pTHX_ struct coro *coro) |
1167 | init_perl (pTHX_ struct coro *coro) |
… | |
… | |
2440 | av_clear (GvAV (PL_defgv)); |
2439 | av_clear (GvAV (PL_defgv)); |
2441 | hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0); |
2440 | hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0); |
2442 | |
2441 | |
2443 | if (ecb_expect_false (coro->swap_sv)) |
2442 | if (ecb_expect_false (coro->swap_sv)) |
2444 | { |
2443 | { |
2445 | swap_svs_leave (coro); |
2444 | SWAP_SVS_LEAVE (coro); |
2446 | SvREFCNT_dec_NN (coro->swap_sv); |
2445 | SvREFCNT_dec_NN (coro->swap_sv); |
2447 | coro->swap_sv = 0; |
2446 | coro->swap_sv = 0; |
2448 | } |
2447 | } |
2449 | |
2448 | |
2450 | coro->prio = 0; |
2449 | coro->prio = 0; |
… | |
… | |
3631 | cctx_current = cctx_new_empty (); |
3630 | cctx_current = cctx_new_empty (); |
3632 | |
3631 | |
3633 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
3632 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
3634 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
3633 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
3635 | |
3634 | |
3636 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
3635 | { |
3637 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
3636 | /* |
3638 | orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr; |
3637 | * we provide a vtbvl for %SIG magic that replaces PL_vtbl_sig |
|
|
3638 | * by coro_sig_vtbl in hash values. |
|
|
3639 | */ |
|
|
3640 | MAGIC *mg = mg_find ((SV *)GvHV (gv_fetchpv ("SIG", GV_ADD | GV_NOTQUAL, SVt_PVHV)), PERL_MAGIC_sig); |
|
|
3641 | |
|
|
3642 | /* this only works if perl doesn't have a vtbl for %SIG */ |
|
|
3643 | assert (!mg->mg_virtual); |
|
|
3644 | |
|
|
3645 | /* |
|
|
3646 | * The irony is that the perl API itself asserts that mg_virtual |
|
|
3647 | * must be non-const, yet perl5porters insisted on marking their |
|
|
3648 | * vtbls as read-only, just to thwart perl modules from patching |
|
|
3649 | * them. |
|
|
3650 | */ |
|
|
3651 | mg->mg_virtual = (MGVTBL *)&coro_sig_vtbl; |
|
|
3652 | mg->mg_flags |= MGf_COPY; |
|
|
3653 | |
|
|
3654 | coro_sigelem_vtbl = PL_vtbl_sigelem; |
|
|
3655 | coro_sigelem_vtbl.svt_get = coro_sigelem_get; |
|
|
3656 | coro_sigelem_vtbl.svt_set = coro_sigelem_set; |
|
|
3657 | coro_sigelem_vtbl.svt_clear = coro_sigelem_clr; |
|
|
3658 | } |
3639 | |
3659 | |
3640 | rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); |
3660 | rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); |
3641 | rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); |
3661 | rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); |
3642 | |
3662 | |
3643 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
3663 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |