… | |
… | |
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 |
… | |
… | |
140 | |
146 | |
141 | static GV *irsgv; /* $/ */ |
147 | static GV *irsgv; /* $/ */ |
142 | static GV *stdoutgv; /* *STDOUT */ |
148 | static GV *stdoutgv; /* *STDOUT */ |
143 | static SV *rv_diehook; |
149 | static SV *rv_diehook; |
144 | static SV *rv_warnhook; |
150 | static SV *rv_warnhook; |
145 | static HV *hv_sig; /* %SIG */ |
|
|
146 | |
151 | |
147 | /* async_pool helper stuff */ |
152 | /* async_pool helper stuff */ |
148 | static SV *sv_pool_rss; |
153 | static SV *sv_pool_rss; |
149 | static SV *sv_pool_size; |
154 | static SV *sv_pool_size; |
150 | static SV *sv_async_pool_idle; /* description string */ |
155 | static SV *sv_async_pool_idle; /* description string */ |
… | |
… | |
727 | #endif |
732 | #endif |
728 | } |
733 | } |
729 | |
734 | |
730 | /* swap sv heads, at least logically */ |
735 | /* swap sv heads, at least logically */ |
731 | static void |
736 | static void |
732 | swap_svs (pTHX_ Coro__State c) |
737 | swap_svs_enter (pTHX_ Coro__State c) |
733 | { |
738 | { |
734 | int i; |
739 | int i; |
735 | |
740 | |
736 | for (i = 0; i <= AvFILLp (c->swap_sv); i += 2) |
741 | for (i = 0; i <= AvFILLp (c->swap_sv); i += 2) |
737 | swap_sv (AvARRAY (c->swap_sv)[i], AvARRAY (c->swap_sv)[i + 1]); |
742 | swap_sv (AvARRAY (c->swap_sv)[i], AvARRAY (c->swap_sv)[i + 1]); |
738 | } |
743 | } |
739 | |
744 | |
|
|
745 | static void |
|
|
746 | swap_svs_leave (pTHX_ Coro__State c) |
|
|
747 | { |
|
|
748 | int i; |
|
|
749 | |
|
|
750 | for (i = AvFILLp (c->swap_sv) - 1; i >= 0; i -= 2) |
|
|
751 | swap_sv (AvARRAY (c->swap_sv)[i], AvARRAY (c->swap_sv)[i + 1]); |
|
|
752 | } |
|
|
753 | |
740 | #define SWAP_SVS(coro) \ |
754 | #define SWAP_SVS_ENTER(coro) \ |
741 | if (ecb_expect_false ((coro)->swap_sv)) \ |
755 | if (ecb_expect_false ((coro)->swap_sv)) \ |
742 | swap_svs (aTHX_ (coro)) |
756 | swap_svs_enter (aTHX_ (coro)) |
|
|
757 | |
|
|
758 | #define SWAP_SVS_LEAVE(coro) \ |
|
|
759 | if (ecb_expect_false ((coro)->swap_sv)) \ |
|
|
760 | swap_svs_leave (aTHX_ (coro)) |
743 | |
761 | |
744 | static void |
762 | static void |
745 | on_enterleave_call (pTHX_ SV *cb); |
763 | on_enterleave_call (pTHX_ SV *cb); |
746 | |
764 | |
747 | static void |
765 | static void |
… | |
… | |
800 | |
818 | |
801 | for (i = 0; i <= AvFILLp (c->on_enter_xs); i += 2) |
819 | for (i = 0; i <= AvFILLp (c->on_enter_xs); i += 2) |
802 | ((coro_enterleave_hook)AvARRAY (c->on_enter_xs)[i]) (aTHX_ AvARRAY (c->on_enter_xs)[i + 1]); |
820 | ((coro_enterleave_hook)AvARRAY (c->on_enter_xs)[i]) (aTHX_ AvARRAY (c->on_enter_xs)[i + 1]); |
803 | } |
821 | } |
804 | |
822 | |
805 | SWAP_SVS (c); |
823 | SWAP_SVS_ENTER (c); |
806 | } |
824 | } |
807 | |
825 | |
808 | static void |
826 | static void |
809 | save_perl (pTHX_ Coro__State c) |
827 | save_perl (pTHX_ Coro__State c) |
810 | { |
828 | { |
811 | SWAP_SVS (c); |
829 | SWAP_SVS_LEAVE (c); |
812 | |
830 | |
813 | if (ecb_expect_false (c->on_leave_xs)) |
831 | if (ecb_expect_false (c->on_leave_xs)) |
814 | { |
832 | { |
815 | int i; |
833 | int i; |
816 | |
834 | |
… | |
… | |
951 | #endif |
969 | #endif |
952 | |
970 | |
953 | New(54,PL_savestack,24,ANY); |
971 | New(54,PL_savestack,24,ANY); |
954 | PL_savestack_ix = 0; |
972 | PL_savestack_ix = 0; |
955 | PL_savestack_max = 24; |
973 | PL_savestack_max = 24; |
|
|
974 | #if PERL_VERSION_ATLEAST (5,24,0) |
|
|
975 | /* perl 5.24 moves SS_MAXPUSH optimisation from */ |
|
|
976 | /* the header macros to PL_savestack_max */ |
|
|
977 | PL_savestack_max -= SS_MAXPUSH; |
|
|
978 | #endif |
956 | |
979 | |
957 | #if !PERL_VERSION_ATLEAST (5,10,0) |
980 | #if !PERL_VERSION_ATLEAST (5,10,0) |
958 | New(54,PL_retstack,4,OP*); |
981 | New(54,PL_retstack,4,OP*); |
959 | PL_retstack_ix = 0; |
982 | PL_retstack_ix = 0; |
960 | PL_retstack_max = 4; |
983 | PL_retstack_max = 4; |
… | |
… | |
1026 | } |
1049 | } |
1027 | |
1050 | |
1028 | return rss; |
1051 | return rss; |
1029 | } |
1052 | } |
1030 | |
1053 | |
1031 | /** coroutine stack handling ************************************************/ |
1054 | /** provide custom get/set/clear methods for %SIG elements ******************/ |
1032 | |
|
|
1033 | static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); |
|
|
1034 | static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); |
|
|
1035 | static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg); |
|
|
1036 | |
1055 | |
1037 | /* apparently < 5.8.8 */ |
1056 | /* apparently < 5.8.8 */ |
1038 | #ifndef MgPV_nolen_const |
1057 | #ifndef MgPV_nolen_const |
1039 | #define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ |
1058 | #define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ |
1040 | SvPV_nolen((SV*)((mg)->mg_ptr)) : \ |
1059 | SvPV_nolen((SV*)((mg)->mg_ptr)) : \ |
1041 | (const char*)(mg)->mg_ptr) |
1060 | (const char*)(mg)->mg_ptr) |
1042 | #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 | }; |
1043 | |
1090 | |
1044 | /* |
1091 | /* |
1045 | * This overrides the default magic get method of %SIG elements. |
1092 | * This overrides the default magic get method of %SIG elements. |
1046 | * 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 |
1047 | * 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), |
… | |
… | |
1049 | */ |
1096 | */ |
1050 | static int ecb_cold |
1097 | static int ecb_cold |
1051 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
1098 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
1052 | { |
1099 | { |
1053 | 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; |
1054 | |
1103 | |
1055 | if (*s == '_') |
1104 | SV *ssv; |
1056 | { |
|
|
1057 | SV **svp = 0; |
|
|
1058 | |
1105 | |
1059 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
|
|
1060 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
|
|
1061 | |
|
|
1062 | if (svp) |
1106 | if (!*svp) |
1063 | { |
|
|
1064 | SV *ssv; |
|
|
1065 | |
|
|
1066 | if (!*svp) |
|
|
1067 | ssv = &PL_sv_undef; |
1107 | ssv = &PL_sv_undef; |
1068 | 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. */ |
1069 | ssv = sv_2mortal (newRV_inc (*svp)); |
1109 | ssv = sv_2mortal (newRV_inc (*svp)); |
1070 | else |
1110 | else |
1071 | ssv = *svp; |
1111 | ssv = *svp; |
1072 | |
1112 | |
1073 | sv_setsv (sv, ssv); |
1113 | sv_setsv (sv, ssv); |
1074 | return 0; |
1114 | return 0; |
1075 | } |
|
|
1076 | } |
|
|
1077 | |
|
|
1078 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
|
|
1079 | } |
1115 | } |
1080 | |
1116 | |
1081 | static int ecb_cold |
1117 | static int ecb_cold |
1082 | coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) |
1118 | coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) |
1083 | { |
1119 | { |
1084 | 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; |
1085 | |
1123 | |
1086 | if (*s == '_') |
|
|
1087 | { |
|
|
1088 | SV **svp = 0; |
|
|
1089 | |
|
|
1090 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
|
|
1091 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
|
|
1092 | |
|
|
1093 | if (svp) |
|
|
1094 | { |
|
|
1095 | SV *old = *svp; |
1124 | SV *old = *svp; |
1096 | *svp = 0; |
1125 | *svp = 0; |
1097 | SvREFCNT_dec (old); |
1126 | SvREFCNT_dec (old); |
1098 | return 0; |
1127 | return 0; |
1099 | } |
|
|
1100 | } |
|
|
1101 | |
|
|
1102 | return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0; |
|
|
1103 | } |
1128 | } |
1104 | |
1129 | |
1105 | static int ecb_cold |
1130 | static int ecb_cold |
1106 | coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) |
1131 | coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) |
1107 | { |
1132 | { |
1108 | 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; |
1109 | |
1136 | |
1110 | if (*s == '_') |
|
|
1111 | { |
|
|
1112 | SV **svp = 0; |
|
|
1113 | |
|
|
1114 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
|
|
1115 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
|
|
1116 | |
|
|
1117 | if (svp) |
|
|
1118 | { |
|
|
1119 | SV *old = *svp; |
1137 | SV *old = *svp; |
1120 | *svp = SvOK (sv) ? newSVsv (sv) : 0; |
1138 | *svp = SvOK (sv) ? newSVsv (sv) : 0; |
1121 | SvREFCNT_dec (old); |
1139 | SvREFCNT_dec (old); |
1122 | return 0; |
1140 | return 0; |
1123 | } |
|
|
1124 | } |
|
|
1125 | |
|
|
1126 | return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; |
|
|
1127 | } |
1141 | } |
1128 | |
1142 | |
1129 | static void |
1143 | static void |
1130 | prepare_nop (pTHX_ struct coro_transfer_args *ta) |
1144 | prepare_nop (pTHX_ struct coro_transfer_args *ta) |
1131 | { |
1145 | { |
… | |
… | |
1142 | static int |
1156 | static int |
1143 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
1157 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
1144 | { |
1158 | { |
1145 | return 1; |
1159 | return 1; |
1146 | } |
1160 | } |
|
|
1161 | |
|
|
1162 | /** coroutine stack handling ************************************************/ |
1147 | |
1163 | |
1148 | static UNOP init_perl_op; |
1164 | static UNOP init_perl_op; |
1149 | |
1165 | |
1150 | 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 */ |
1151 | init_perl (pTHX_ struct coro *coro) |
1167 | init_perl (pTHX_ struct coro *coro) |
… | |
… | |
1218 | PL_op = (OP *)&init_perl_op; |
1234 | PL_op = (OP *)&init_perl_op; |
1219 | |
1235 | |
1220 | /* copy throw, in case it was set before init_perl */ |
1236 | /* copy throw, in case it was set before init_perl */ |
1221 | CORO_THROW = coro->except; |
1237 | CORO_THROW = coro->except; |
1222 | |
1238 | |
1223 | SWAP_SVS (coro); |
1239 | SWAP_SVS_ENTER (coro); |
1224 | |
1240 | |
1225 | if (ecb_expect_false (enable_times)) |
1241 | if (ecb_expect_false (enable_times)) |
1226 | { |
1242 | { |
1227 | coro_times_update (); |
1243 | coro_times_update (); |
1228 | coro_times_sub (coro); |
1244 | coro_times_sub (coro); |
… | |
… | |
1266 | /* this will cause transfer_check to croak on block*/ |
1282 | /* this will cause transfer_check to croak on block*/ |
1267 | SvRV_set (coro_current, (SV *)coro->hv); |
1283 | SvRV_set (coro_current, (SV *)coro->hv); |
1268 | |
1284 | |
1269 | load_perl (aTHX_ coro); |
1285 | load_perl (aTHX_ coro); |
1270 | |
1286 | |
|
|
1287 | /* restore swapped sv's */ |
|
|
1288 | SWAP_SVS_LEAVE (coro); |
|
|
1289 | |
1271 | coro_unwind_stacks (aTHX); |
1290 | coro_unwind_stacks (aTHX); |
1272 | |
|
|
1273 | /* restore swapped sv's */ |
|
|
1274 | SWAP_SVS (coro); |
|
|
1275 | |
1291 | |
1276 | coro_destruct_stacks (aTHX); |
1292 | coro_destruct_stacks (aTHX); |
1277 | |
1293 | |
1278 | /* now save some sv's to be free'd later */ |
1294 | /* now save some sv's to be free'd later */ |
1279 | svf [0] = GvSV (PL_defgv); |
1295 | svf [0] = GvSV (PL_defgv); |
… | |
… | |
1526 | * coro body here, as perl_run destroys these. Likewise, we cannot catch |
1542 | * coro body here, as perl_run destroys these. Likewise, we cannot catch |
1527 | * runtime errors here, as this is just a random interpreter, not a thread. |
1543 | * runtime errors here, as this is just a random interpreter, not a thread. |
1528 | */ |
1544 | */ |
1529 | |
1545 | |
1530 | /* |
1546 | /* |
|
|
1547 | * pp_entersub in 5.24 no longer ENTERs, but perl_destruct |
|
|
1548 | * requires PL_scopestack_ix, so do it here if required. |
|
|
1549 | */ |
|
|
1550 | if (!PL_scopestack_ix) |
|
|
1551 | ENTER; |
|
|
1552 | |
|
|
1553 | /* |
1531 | * If perl-run returns we assume exit() was being called or the coro |
1554 | * If perl-run returns we assume exit() was being called or the coro |
1532 | * fell off the end, which seems to be the only valid (non-bug) |
1555 | * fell off the end, which seems to be the only valid (non-bug) |
1533 | * reason for perl_run to return. We try to mimic whatever perl is normally |
1556 | * reason for perl_run to return. We try to mimic whatever perl is normally |
1534 | * doing in that case. YMMV. |
1557 | * doing in that case. YMMV. |
1535 | */ |
1558 | */ |
… | |
… | |
2414 | else |
2437 | else |
2415 | { |
2438 | { |
2416 | av_clear (GvAV (PL_defgv)); |
2439 | av_clear (GvAV (PL_defgv)); |
2417 | 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); |
2418 | |
2441 | |
|
|
2442 | if (ecb_expect_false (coro->swap_sv)) |
|
|
2443 | { |
|
|
2444 | SWAP_SVS_LEAVE (coro); |
|
|
2445 | SvREFCNT_dec_NN (coro->swap_sv); |
|
|
2446 | coro->swap_sv = 0; |
|
|
2447 | } |
|
|
2448 | |
2419 | coro->prio = 0; |
2449 | coro->prio = 0; |
2420 | |
2450 | |
2421 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
2451 | if (ecb_expect_false (coro->cctx) && ecb_expect_false (coro->cctx->flags & CC_TRACE)) |
2422 | api_trace (aTHX_ coro_current, 0); |
2452 | api_trace (aTHX_ coro_current, 0); |
2423 | |
2453 | |
2424 | frame->prepare = prepare_schedule; |
2454 | frame->prepare = prepare_schedule; |
2425 | av_push (av_async_pool, SvREFCNT_inc (hv)); |
2455 | av_push (av_async_pool, SvREFCNT_inc (hv)); |
2426 | } |
2456 | } |
… | |
… | |
3555 | |
3585 | |
3556 | map_len = load_len + save_len + 16; |
3586 | map_len = load_len + save_len + 16; |
3557 | |
3587 | |
3558 | map_base = mmap (0, map_len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
3588 | map_base = mmap (0, map_len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
3559 | |
3589 | |
|
|
3590 | if (map_base == (char *)MAP_FAILED) |
|
|
3591 | map_base = mmap (0, map_len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
|
|
3592 | |
3560 | assert (("Coro: unable to mmap jit code page, cannot continue.", map_base != (char *)MAP_FAILED)); |
3593 | assert (("Coro: unable to mmap jit code page, cannot continue.", map_base != (char *)MAP_FAILED)); |
3561 | |
3594 | |
3562 | load_perl_slots = (load_save_perl_slots_type)map_base; |
3595 | load_perl_slots = (load_save_perl_slots_type)map_base; |
3563 | memcpy (map_base, load_ptr, load_len); |
3596 | memcpy (map_base, load_ptr, load_len); |
3564 | |
3597 | |
… | |
… | |
3600 | cctx_current = cctx_new_empty (); |
3633 | cctx_current = cctx_new_empty (); |
3601 | |
3634 | |
3602 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
3635 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
3603 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
3636 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
3604 | |
3637 | |
3605 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
3638 | { |
3606 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
3639 | /* |
3607 | orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr; |
3640 | * we provide a vtbvl for %SIG magic that replaces PL_vtbl_sig |
|
|
3641 | * by coro_sig_vtbl in hash values. |
|
|
3642 | */ |
|
|
3643 | MAGIC *mg = mg_find ((SV *)GvHV (gv_fetchpv ("SIG", GV_ADD | GV_NOTQUAL, SVt_PVHV)), PERL_MAGIC_sig); |
|
|
3644 | |
|
|
3645 | /* this only works if perl doesn't have a vtbl for %SIG */ |
|
|
3646 | assert (!mg->mg_virtual); |
|
|
3647 | |
|
|
3648 | /* |
|
|
3649 | * The irony is that the perl API itself asserts that mg_virtual |
|
|
3650 | * must be non-const, yet perl5porters insisted on marking their |
|
|
3651 | * vtbls as read-only, just to thwart perl modules from patching |
|
|
3652 | * them. |
|
|
3653 | */ |
|
|
3654 | mg->mg_virtual = (MGVTBL *)&coro_sig_vtbl; |
|
|
3655 | mg->mg_flags |= MGf_COPY; |
|
|
3656 | |
|
|
3657 | coro_sigelem_vtbl = PL_vtbl_sigelem; |
|
|
3658 | coro_sigelem_vtbl.svt_get = coro_sigelem_get; |
|
|
3659 | coro_sigelem_vtbl.svt_set = coro_sigelem_set; |
|
|
3660 | coro_sigelem_vtbl.svt_clear = coro_sigelem_clr; |
|
|
3661 | } |
3608 | |
3662 | |
3609 | hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); |
|
|
3610 | rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); |
3663 | rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); |
3611 | rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); |
3664 | rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); |
3612 | |
3665 | |
3613 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
3666 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
3614 | |
3667 | |
… | |
… | |
3920 | if (ecb_expect_false (current == self)) |
3973 | if (ecb_expect_false (current == self)) |
3921 | coro_times_sub (SvSTATE (coro_current)); |
3974 | coro_times_sub (SvSTATE (coro_current)); |
3922 | } |
3975 | } |
3923 | |
3976 | |
3924 | void |
3977 | void |
3925 | swap_sv (Coro::State coro, SV *sv, SV *swapsv) |
3978 | swap_sv (Coro::State coro, SV *sva, SV *svb) |
3926 | CODE: |
3979 | CODE: |
3927 | { |
3980 | { |
3928 | struct coro *current = SvSTATE_current; |
3981 | struct coro *current = SvSTATE_current; |
|
|
3982 | AV *swap_sv; |
|
|
3983 | int i; |
|
|
3984 | |
|
|
3985 | sva = SvRV (sva); |
|
|
3986 | svb = SvRV (svb); |
3929 | |
3987 | |
3930 | if (current == coro) |
3988 | if (current == coro) |
3931 | SWAP_SVS (current); |
3989 | SWAP_SVS_LEAVE (current); |
3932 | |
3990 | |
3933 | if (!coro->swap_sv) |
3991 | if (!coro->swap_sv) |
3934 | coro->swap_sv = newAV (); |
3992 | coro->swap_sv = newAV (); |
3935 | |
3993 | |
|
|
3994 | swap_sv = coro->swap_sv; |
|
|
3995 | |
|
|
3996 | for (i = AvFILLp (swap_sv) - 1; i >= 0; i -= 2) |
|
|
3997 | { |
|
|
3998 | SV *a = AvARRAY (swap_sv)[i ]; |
|
|
3999 | SV *b = AvARRAY (swap_sv)[i + 1]; |
|
|
4000 | |
|
|
4001 | if (a == sva && b == svb) |
|
|
4002 | { |
|
|
4003 | SvREFCNT_dec_NN (a); |
|
|
4004 | SvREFCNT_dec_NN (b); |
|
|
4005 | |
|
|
4006 | for (; i <= AvFILLp (swap_sv) - 2; i++) |
|
|
4007 | AvARRAY (swap_sv)[i] = AvARRAY (swap_sv)[i + 2]; |
|
|
4008 | |
|
|
4009 | AvFILLp (swap_sv) -= 2; |
|
|
4010 | |
|
|
4011 | goto removed; |
|
|
4012 | } |
|
|
4013 | } |
|
|
4014 | |
3936 | av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (sv ))); |
4015 | av_push (swap_sv, SvREFCNT_inc_NN (sva)); |
3937 | av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (swapsv))); |
4016 | av_push (swap_sv, SvREFCNT_inc_NN (svb)); |
|
|
4017 | |
|
|
4018 | removed: |
3938 | |
4019 | |
3939 | if (current == coro) |
4020 | if (current == coro) |
3940 | SWAP_SVS (current); |
4021 | SWAP_SVS_ENTER (current); |
3941 | } |
4022 | } |
3942 | |
4023 | |
3943 | |
4024 | |
3944 | MODULE = Coro::State PACKAGE = Coro |
4025 | MODULE = Coro::State PACKAGE = Coro |
3945 | |
4026 | |
… | |
… | |
4259 | RETVAL = newSVsv (AvARRAY ((AV *)SvRV (self))[0]); |
4340 | RETVAL = newSVsv (AvARRAY ((AV *)SvRV (self))[0]); |
4260 | OUTPUT: |
4341 | OUTPUT: |
4261 | RETVAL |
4342 | RETVAL |
4262 | |
4343 | |
4263 | void |
4344 | void |
4264 | up (SV *self, int adjust = 1) |
4345 | up (SV *self) |
4265 | ALIAS: |
|
|
4266 | adjust = 1 |
|
|
4267 | CODE: |
4346 | CODE: |
|
|
4347 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), 1); |
|
|
4348 | |
|
|
4349 | void |
|
|
4350 | adjust (SV *self, int adjust) |
|
|
4351 | CODE: |
4268 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
4352 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), adjust); |
4269 | |
4353 | |
4270 | void |
4354 | void |
4271 | down (...) |
4355 | down (...) |
4272 | CODE: |
4356 | CODE: |
4273 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
4357 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |