… | |
… | |
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 | #if PERL_VERSION_ATLEAST(5,24,0) |
|
|
122 | # define SUB_ARGARRAY PL_curpad[0] |
|
|
123 | #else |
|
|
124 | # define SUB_ARGARRAY (SV *)cx->blk_sub.argarray |
|
|
125 | #endif |
|
|
126 | |
|
|
127 | /* perl usually suppressed asserts. for debugging, we sometimes force it to be on */ |
|
|
128 | #if 0 |
|
|
129 | # undef NDEBUG |
|
|
130 | # include <assert.h> |
|
|
131 | #endif |
|
|
132 | |
121 | static double (*nvtime)(); /* so why doesn't it take void? */ |
133 | static double (*nvtime)(); /* so why doesn't it take void? */ |
122 | static void (*u2time)(pTHX_ UV ret[2]); |
134 | static void (*u2time)(pTHX_ UV ret[2]); |
123 | |
135 | |
124 | /* we hijack an hopefully unused CV flag for our purposes */ |
136 | /* we hijack an hopefully unused CV flag for our purposes */ |
125 | #define CVf_SLF 0x4000 |
137 | #define CVf_SLF 0x4000 |
… | |
… | |
963 | #endif |
975 | #endif |
964 | |
976 | |
965 | New(54,PL_savestack,24,ANY); |
977 | New(54,PL_savestack,24,ANY); |
966 | PL_savestack_ix = 0; |
978 | PL_savestack_ix = 0; |
967 | PL_savestack_max = 24; |
979 | PL_savestack_max = 24; |
968 | #if !PERL_VERSION_ATLEAST (5,24,0) |
980 | #if PERL_VERSION_ATLEAST (5,24,0) |
969 | /* perl 5.24 moves SS_MAXPUSH optimisation from */ |
981 | /* perl 5.24 moves SS_MAXPUSH optimisation from */ |
970 | /* the header macros to PL_savestack_max */ |
982 | /* the header macros to PL_savestack_max */ |
971 | PL_savestack_max -= SS_MAXPUSH; |
983 | PL_savestack_max -= SS_MAXPUSH; |
972 | #endif |
984 | #endif |
973 | |
985 | |
… | |
… | |
1043 | } |
1055 | } |
1044 | |
1056 | |
1045 | return rss; |
1057 | return rss; |
1046 | } |
1058 | } |
1047 | |
1059 | |
1048 | /** coroutine stack handling ************************************************/ |
1060 | /** 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 | |
1061 | |
1054 | /* apparently < 5.8.8 */ |
1062 | /* apparently < 5.8.8 */ |
1055 | #ifndef MgPV_nolen_const |
1063 | #ifndef MgPV_nolen_const |
1056 | #define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ |
1064 | #define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ |
1057 | SvPV_nolen((SV*)((mg)->mg_ptr)) : \ |
1065 | SvPV_nolen((SV*)((mg)->mg_ptr)) : \ |
1058 | (const char*)(mg)->mg_ptr) |
1066 | (const char*)(mg)->mg_ptr) |
1059 | #endif |
1067 | #endif |
|
|
1068 | |
|
|
1069 | /* this will be a patched copy of PL_vtbl_sigelem */ |
|
|
1070 | static MGVTBL coro_sigelem_vtbl; |
|
|
1071 | |
|
|
1072 | static int ecb_cold |
|
|
1073 | coro_sig_copy (pTHX_ SV *sv, MAGIC *mg, SV *nsv, const char *name, I32 namlen) |
|
|
1074 | { |
|
|
1075 | char *key = SvPV_nolen ((SV *)name); |
|
|
1076 | |
|
|
1077 | /* do what mg_copy normally does */ |
|
|
1078 | sv_magic (nsv, mg->mg_obj, PERL_MAGIC_sigelem, name, namlen); |
|
|
1079 | assert (mg_find (nsv, PERL_MAGIC_sigelem)->mg_virtual == &PL_vtbl_sigelem); |
|
|
1080 | |
|
|
1081 | /* patch sigelem vtbl, but only for __WARN__ and __DIE__ */ |
|
|
1082 | if (*key == '_' |
|
|
1083 | && (strEQ (key, "__DIE__") |
|
|
1084 | || strEQ (key, "__WARN__"))) |
|
|
1085 | mg_find (nsv, PERL_MAGIC_sigelem)->mg_virtual = &coro_sigelem_vtbl; |
|
|
1086 | |
|
|
1087 | return 1; |
|
|
1088 | } |
|
|
1089 | |
|
|
1090 | /* perl does not have a %SIG vtbl, we provide one so we can override */ |
|
|
1091 | /* the magic vtbl for the __DIE__ and __WARN__ members */ |
|
|
1092 | static const MGVTBL coro_sig_vtbl = { |
|
|
1093 | 0, 0, 0, 0, 0, |
|
|
1094 | coro_sig_copy |
|
|
1095 | }; |
1060 | |
1096 | |
1061 | /* |
1097 | /* |
1062 | * This overrides the default magic get method of %SIG elements. |
1098 | * 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 |
1099 | * 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), |
1100 | * and instead of trying to save and restore the hash elements (extremely slow), |
… | |
… | |
1066 | */ |
1102 | */ |
1067 | static int ecb_cold |
1103 | static int ecb_cold |
1068 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
1104 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
1069 | { |
1105 | { |
1070 | const char *s = MgPV_nolen_const (mg); |
1106 | const char *s = MgPV_nolen_const (mg); |
|
|
1107 | /* the key must be either __DIE__ or __WARN__ here */ |
|
|
1108 | SV **svp = s[2] == 'D' ? &PL_diehook : &PL_warnhook; |
1071 | |
1109 | |
1072 | if (*s == '_') |
1110 | SV *ssv; |
1073 | { |
|
|
1074 | SV **svp = 0; |
|
|
1075 | |
1111 | |
1076 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
|
|
1077 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
|
|
1078 | |
|
|
1079 | if (svp) |
1112 | if (!*svp) |
1080 | { |
|
|
1081 | SV *ssv; |
|
|
1082 | |
|
|
1083 | if (!*svp) |
|
|
1084 | ssv = &PL_sv_undef; |
1113 | ssv = &PL_sv_undef; |
1085 | else if (SvTYPE (*svp) == SVt_PVCV) /* perlio directly stores a CV in warnhook. ugh. */ |
1114 | else if (SvTYPE (*svp) == SVt_PVCV) /* perlio directly stores a CV in warnhook. ugh. */ |
1086 | ssv = sv_2mortal (newRV_inc (*svp)); |
1115 | ssv = sv_2mortal (newRV_inc (*svp)); |
1087 | else |
1116 | else |
1088 | ssv = *svp; |
1117 | ssv = *svp; |
1089 | |
1118 | |
1090 | sv_setsv (sv, ssv); |
1119 | sv_setsv (sv, ssv); |
1091 | return 0; |
1120 | return 0; |
1092 | } |
|
|
1093 | } |
|
|
1094 | |
|
|
1095 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
|
|
1096 | } |
1121 | } |
1097 | |
1122 | |
1098 | static int ecb_cold |
1123 | static int ecb_cold |
1099 | coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) |
1124 | coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) |
1100 | { |
1125 | { |
1101 | const char *s = MgPV_nolen_const (mg); |
1126 | const char *s = MgPV_nolen_const (mg); |
|
|
1127 | /* the key must be either __DIE__ or __WARN__ here */ |
|
|
1128 | SV **svp = s[2] == 'D' ? &PL_diehook : &PL_warnhook; |
1102 | |
1129 | |
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; |
1130 | SV *old = *svp; |
1113 | *svp = 0; |
1131 | *svp = 0; |
1114 | SvREFCNT_dec (old); |
1132 | SvREFCNT_dec (old); |
1115 | return 0; |
1133 | return 0; |
1116 | } |
|
|
1117 | } |
|
|
1118 | |
|
|
1119 | return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0; |
|
|
1120 | } |
1134 | } |
1121 | |
1135 | |
1122 | static int ecb_cold |
1136 | static int ecb_cold |
1123 | coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) |
1137 | coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) |
1124 | { |
1138 | { |
1125 | const char *s = MgPV_nolen_const (mg); |
1139 | const char *s = MgPV_nolen_const (mg); |
|
|
1140 | /* the key must be either __DIE__ or __WARN__ here */ |
|
|
1141 | SV **svp = s[2] == 'D' ? &PL_diehook : &PL_warnhook; |
1126 | |
1142 | |
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; |
1143 | SV *old = *svp; |
1137 | *svp = SvOK (sv) ? newSVsv (sv) : 0; |
1144 | *svp = SvOK (sv) ? newSVsv (sv) : 0; |
1138 | SvREFCNT_dec (old); |
1145 | SvREFCNT_dec (old); |
1139 | return 0; |
1146 | return 0; |
1140 | } |
|
|
1141 | } |
|
|
1142 | |
|
|
1143 | return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; |
|
|
1144 | } |
1147 | } |
1145 | |
1148 | |
1146 | static void |
1149 | static void |
1147 | prepare_nop (pTHX_ struct coro_transfer_args *ta) |
1150 | prepare_nop (pTHX_ struct coro_transfer_args *ta) |
1148 | { |
1151 | { |
… | |
… | |
1159 | static int |
1162 | static int |
1160 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
1163 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
1161 | { |
1164 | { |
1162 | return 1; |
1165 | return 1; |
1163 | } |
1166 | } |
|
|
1167 | |
|
|
1168 | /** coroutine stack handling ************************************************/ |
1164 | |
1169 | |
1165 | static UNOP init_perl_op; |
1170 | static UNOP init_perl_op; |
1166 | |
1171 | |
1167 | ecb_noinline static void /* noinline to keep it out of the transfer fast path */ |
1172 | ecb_noinline static void /* noinline to keep it out of the transfer fast path */ |
1168 | init_perl (pTHX_ struct coro *coro) |
1173 | init_perl (pTHX_ struct coro *coro) |
… | |
… | |
1411 | SAVETMPS; |
1416 | SAVETMPS; |
1412 | EXTEND (SP, 3); |
1417 | EXTEND (SP, 3); |
1413 | PUSHMARK (SP); |
1418 | PUSHMARK (SP); |
1414 | PUSHs (&PL_sv_yes); |
1419 | PUSHs (&PL_sv_yes); |
1415 | PUSHs (fullname); |
1420 | PUSHs (fullname); |
1416 | PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
1421 | PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc (SUB_ARGARRAY)) : &PL_sv_undef); |
1417 | PUTBACK; |
1422 | PUTBACK; |
1418 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
1423 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
1419 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1424 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1420 | SPAGAIN; |
1425 | SPAGAIN; |
1421 | FREETMPS; |
1426 | FREETMPS; |
… | |
… | |
1543 | * coro body here, as perl_run destroys these. Likewise, we cannot catch |
1548 | * coro body here, as perl_run destroys these. Likewise, we cannot catch |
1544 | * runtime errors here, as this is just a random interpreter, not a thread. |
1549 | * runtime errors here, as this is just a random interpreter, not a thread. |
1545 | */ |
1550 | */ |
1546 | |
1551 | |
1547 | /* |
1552 | /* |
|
|
1553 | * pp_entersub in 5.24 no longer ENTERs, but perl_destruct |
|
|
1554 | * requires PL_scopestack_ix, so do it here if required. |
|
|
1555 | */ |
|
|
1556 | if (!PL_scopestack_ix) |
|
|
1557 | ENTER; |
|
|
1558 | |
|
|
1559 | /* |
1548 | * If perl-run returns we assume exit() was being called or the coro |
1560 | * If perl-run returns we assume exit() was being called or the coro |
1549 | * fell off the end, which seems to be the only valid (non-bug) |
1561 | * fell off the end, which seems to be the only valid (non-bug) |
1550 | * reason for perl_run to return. We try to mimic whatever perl is normally |
1562 | * reason for perl_run to return. We try to mimic whatever perl is normally |
1551 | * doing in that case. YMMV. |
1563 | * doing in that case. YMMV. |
1552 | */ |
1564 | */ |
… | |
… | |
2433 | av_clear (GvAV (PL_defgv)); |
2445 | av_clear (GvAV (PL_defgv)); |
2434 | hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0); |
2446 | hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0); |
2435 | |
2447 | |
2436 | if (ecb_expect_false (coro->swap_sv)) |
2448 | if (ecb_expect_false (coro->swap_sv)) |
2437 | { |
2449 | { |
2438 | swap_svs_leave (coro); |
2450 | SWAP_SVS_LEAVE (coro); |
2439 | SvREFCNT_dec_NN (coro->swap_sv); |
2451 | SvREFCNT_dec_NN (coro->swap_sv); |
2440 | coro->swap_sv = 0; |
2452 | coro->swap_sv = 0; |
2441 | } |
2453 | } |
2442 | |
2454 | |
2443 | coro->prio = 0; |
2455 | coro->prio = 0; |
… | |
… | |
3579 | |
3591 | |
3580 | map_len = load_len + save_len + 16; |
3592 | map_len = load_len + save_len + 16; |
3581 | |
3593 | |
3582 | map_base = mmap (0, map_len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
3594 | map_base = mmap (0, map_len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
3583 | |
3595 | |
|
|
3596 | if (map_base == (char *)MAP_FAILED) |
|
|
3597 | map_base = mmap (0, map_len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
|
|
3598 | |
3584 | assert (("Coro: unable to mmap jit code page, cannot continue.", map_base != (char *)MAP_FAILED)); |
3599 | assert (("Coro: unable to mmap jit code page, cannot continue.", map_base != (char *)MAP_FAILED)); |
3585 | |
3600 | |
3586 | load_perl_slots = (load_save_perl_slots_type)map_base; |
3601 | load_perl_slots = (load_save_perl_slots_type)map_base; |
3587 | memcpy (map_base, load_ptr, load_len); |
3602 | memcpy (map_base, load_ptr, load_len); |
3588 | |
3603 | |
… | |
… | |
3624 | cctx_current = cctx_new_empty (); |
3639 | cctx_current = cctx_new_empty (); |
3625 | |
3640 | |
3626 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
3641 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
3627 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
3642 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
3628 | |
3643 | |
3629 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
3644 | { |
3630 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
3645 | /* |
3631 | orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr; |
3646 | * we provide a vtbvl for %SIG magic that replaces PL_vtbl_sig |
|
|
3647 | * by coro_sig_vtbl in hash values. |
|
|
3648 | */ |
|
|
3649 | MAGIC *mg = mg_find ((SV *)GvHV (gv_fetchpv ("SIG", GV_ADD | GV_NOTQUAL, SVt_PVHV)), PERL_MAGIC_sig); |
|
|
3650 | |
|
|
3651 | /* this only works if perl doesn't have a vtbl for %SIG */ |
|
|
3652 | assert (!mg->mg_virtual); |
|
|
3653 | |
|
|
3654 | /* |
|
|
3655 | * The irony is that the perl API itself asserts that mg_virtual |
|
|
3656 | * must be non-const, yet perl5porters insisted on marking their |
|
|
3657 | * vtbls as read-only, just to thwart perl modules from patching |
|
|
3658 | * them. |
|
|
3659 | */ |
|
|
3660 | mg->mg_virtual = (MGVTBL *)&coro_sig_vtbl; |
|
|
3661 | mg->mg_flags |= MGf_COPY; |
|
|
3662 | |
|
|
3663 | coro_sigelem_vtbl = PL_vtbl_sigelem; |
|
|
3664 | coro_sigelem_vtbl.svt_get = coro_sigelem_get; |
|
|
3665 | coro_sigelem_vtbl.svt_set = coro_sigelem_set; |
|
|
3666 | coro_sigelem_vtbl.svt_clear = coro_sigelem_clr; |
|
|
3667 | } |
3632 | |
3668 | |
3633 | rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); |
3669 | rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); |
3634 | rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); |
3670 | rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); |
3635 | |
3671 | |
3636 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
3672 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |