… | |
… | |
21 | |
21 | |
22 | #include <stddef.h> |
22 | #include <stddef.h> |
23 | #include <stdio.h> |
23 | #include <stdio.h> |
24 | #include <errno.h> |
24 | #include <errno.h> |
25 | #include <assert.h> |
25 | #include <assert.h> |
|
|
26 | |
|
|
27 | #ifndef SvREFCNT_dec_NN |
|
|
28 | #define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv) |
|
|
29 | #endif |
|
|
30 | |
|
|
31 | #ifndef SvREFCNT_inc_NN |
|
|
32 | #define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv) |
|
|
33 | #endif |
26 | |
34 | |
27 | #ifndef SVs_PADSTALE |
35 | #ifndef SVs_PADSTALE |
28 | # define SVs_PADSTALE 0 |
36 | # define SVs_PADSTALE 0 |
29 | #endif |
37 | #endif |
30 | |
38 | |
… | |
… | |
108 | # define CORO_CLOCK_MONOTONIC 1 |
116 | # define CORO_CLOCK_MONOTONIC 1 |
109 | # define CORO_CLOCK_THREAD_CPUTIME_ID 3 |
117 | # define CORO_CLOCK_THREAD_CPUTIME_ID 3 |
110 | # endif |
118 | # endif |
111 | #endif |
119 | #endif |
112 | |
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 | |
113 | static double (*nvtime)(); /* so why doesn't it take void? */ |
127 | static double (*nvtime)(); /* so why doesn't it take void? */ |
114 | static void (*u2time)(pTHX_ UV ret[2]); |
128 | static void (*u2time)(pTHX_ UV ret[2]); |
115 | |
129 | |
116 | /* we hijack an hopefully unused CV flag for our purposes */ |
130 | /* we hijack an hopefully unused CV flag for our purposes */ |
117 | #define CVf_SLF 0x4000 |
131 | #define CVf_SLF 0x4000 |
… | |
… | |
132 | |
146 | |
133 | static GV *irsgv; /* $/ */ |
147 | static GV *irsgv; /* $/ */ |
134 | static GV *stdoutgv; /* *STDOUT */ |
148 | static GV *stdoutgv; /* *STDOUT */ |
135 | static SV *rv_diehook; |
149 | static SV *rv_diehook; |
136 | static SV *rv_warnhook; |
150 | static SV *rv_warnhook; |
137 | static HV *hv_sig; /* %SIG */ |
|
|
138 | |
151 | |
139 | /* async_pool helper stuff */ |
152 | /* async_pool helper stuff */ |
140 | static SV *sv_pool_rss; |
153 | static SV *sv_pool_rss; |
141 | static SV *sv_pool_size; |
154 | static SV *sv_pool_size; |
142 | static SV *sv_async_pool_idle; /* description string */ |
155 | static SV *sv_async_pool_idle; /* description string */ |
… | |
… | |
701 | tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u; |
714 | tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u; |
702 | |
715 | |
703 | /* if SvANY points to the head, we need to adjust the pointers, |
716 | /* if SvANY points to the head, we need to adjust the pointers, |
704 | * as the pointer for a still points to b, and maybe vice versa. |
717 | * as the pointer for a still points to b, and maybe vice versa. |
705 | */ |
718 | */ |
706 | #define svany_in_head(type) \ |
719 | U32 svany_in_head_set = (1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV); |
707 | (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type))) |
720 | #if NVSIZE <= IVSIZE && PERL_VERSION_ATLEAST(5,22,0) |
|
|
721 | svany_in_head_set |= 1 << SVt_NV; |
|
|
722 | #endif |
|
|
723 | |
|
|
724 | #define svany_in_head(type) (svany_in_head_set & (1 << (type))) |
708 | |
725 | |
709 | if (svany_in_head (SvTYPE (a))) |
726 | if (svany_in_head (SvTYPE (a))) |
710 | SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a); |
727 | SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a); |
711 | |
728 | |
712 | if (svany_in_head (SvTYPE (b))) |
729 | if (svany_in_head (SvTYPE (b))) |
… | |
… | |
715 | #endif |
732 | #endif |
716 | } |
733 | } |
717 | |
734 | |
718 | /* swap sv heads, at least logically */ |
735 | /* swap sv heads, at least logically */ |
719 | static void |
736 | static void |
720 | swap_svs (pTHX_ Coro__State c) |
737 | swap_svs_enter (pTHX_ Coro__State c) |
721 | { |
738 | { |
722 | int i; |
739 | int i; |
723 | |
740 | |
724 | for (i = 0; i <= AvFILLp (c->swap_sv); i += 2) |
741 | for (i = 0; i <= AvFILLp (c->swap_sv); i += 2) |
725 | 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]); |
726 | } |
743 | } |
727 | |
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 | |
728 | #define SWAP_SVS(coro) \ |
754 | #define SWAP_SVS_ENTER(coro) \ |
729 | if (ecb_expect_false ((coro)->swap_sv)) \ |
755 | if (ecb_expect_false ((coro)->swap_sv)) \ |
730 | 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)) |
731 | |
761 | |
732 | static void |
762 | static void |
733 | on_enterleave_call (pTHX_ SV *cb); |
763 | on_enterleave_call (pTHX_ SV *cb); |
734 | |
764 | |
735 | static void |
765 | static void |
… | |
… | |
788 | |
818 | |
789 | for (i = 0; i <= AvFILLp (c->on_enter_xs); i += 2) |
819 | for (i = 0; i <= AvFILLp (c->on_enter_xs); i += 2) |
790 | ((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]); |
791 | } |
821 | } |
792 | |
822 | |
793 | SWAP_SVS (c); |
823 | SWAP_SVS_ENTER (c); |
794 | } |
824 | } |
795 | |
825 | |
796 | static void |
826 | static void |
797 | save_perl (pTHX_ Coro__State c) |
827 | save_perl (pTHX_ Coro__State c) |
798 | { |
828 | { |
799 | SWAP_SVS (c); |
829 | SWAP_SVS_LEAVE (c); |
800 | |
830 | |
801 | if (ecb_expect_false (c->on_leave_xs)) |
831 | if (ecb_expect_false (c->on_leave_xs)) |
802 | { |
832 | { |
803 | int i; |
833 | int i; |
804 | |
834 | |
… | |
… | |
939 | #endif |
969 | #endif |
940 | |
970 | |
941 | New(54,PL_savestack,24,ANY); |
971 | New(54,PL_savestack,24,ANY); |
942 | PL_savestack_ix = 0; |
972 | PL_savestack_ix = 0; |
943 | 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 |
944 | |
979 | |
945 | #if !PERL_VERSION_ATLEAST (5,10,0) |
980 | #if !PERL_VERSION_ATLEAST (5,10,0) |
946 | New(54,PL_retstack,4,OP*); |
981 | New(54,PL_retstack,4,OP*); |
947 | PL_retstack_ix = 0; |
982 | PL_retstack_ix = 0; |
948 | PL_retstack_max = 4; |
983 | PL_retstack_max = 4; |
… | |
… | |
1014 | } |
1049 | } |
1015 | |
1050 | |
1016 | return rss; |
1051 | return rss; |
1017 | } |
1052 | } |
1018 | |
1053 | |
1019 | /** coroutine stack handling ************************************************/ |
1054 | /** provide custom get/set/clear methods for %SIG elements ******************/ |
1020 | |
|
|
1021 | static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); |
|
|
1022 | static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); |
|
|
1023 | static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg); |
|
|
1024 | |
1055 | |
1025 | /* apparently < 5.8.8 */ |
1056 | /* apparently < 5.8.8 */ |
1026 | #ifndef MgPV_nolen_const |
1057 | #ifndef MgPV_nolen_const |
1027 | #define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ |
1058 | #define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ |
1028 | SvPV_nolen((SV*)((mg)->mg_ptr)) : \ |
1059 | SvPV_nolen((SV*)((mg)->mg_ptr)) : \ |
1029 | (const char*)(mg)->mg_ptr) |
1060 | (const char*)(mg)->mg_ptr) |
1030 | #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 | sv_magic (nsv, mg->mg_obj, PERL_MAGIC_sigelem, name, namlen); |
|
|
1070 | assert (mg_find (nsv, PERL_MAGIC_sigelem)->mg_virtual == &PL_vtbl_sigelem); |
|
|
1071 | mg_find (nsv, PERL_MAGIC_sigelem)->mg_virtual = &coro_sigelem_vtbl; |
|
|
1072 | return 1; |
|
|
1073 | } |
|
|
1074 | |
|
|
1075 | /* perl does not have a %SIG vtbl, we provide one so we can override */ |
|
|
1076 | /* the cwvtblagic for %SIG members */ |
|
|
1077 | static const MGVTBL coro_sig_vtbl = { |
|
|
1078 | 0, 0, 0, 0, 0, |
|
|
1079 | coro_sig_copy |
|
|
1080 | }; |
1031 | |
1081 | |
1032 | /* |
1082 | /* |
1033 | * This overrides the default magic get method of %SIG elements. |
1083 | * This overrides the default magic get method of %SIG elements. |
1034 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
1084 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
1035 | * and instead of trying to save and restore the hash elements (extremely slow), |
1085 | * and instead of trying to save and restore the hash elements (extremely slow), |
… | |
… | |
1061 | sv_setsv (sv, ssv); |
1111 | sv_setsv (sv, ssv); |
1062 | return 0; |
1112 | return 0; |
1063 | } |
1113 | } |
1064 | } |
1114 | } |
1065 | |
1115 | |
1066 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
1116 | return PL_vtbl_sigelem.svt_get ? PL_vtbl_sigelem.svt_get (aTHX_ sv, mg) : 0; |
1067 | } |
1117 | } |
1068 | |
1118 | |
1069 | static int ecb_cold |
1119 | static int ecb_cold |
1070 | coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) |
1120 | coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) |
1071 | { |
1121 | { |
… | |
… | |
1085 | SvREFCNT_dec (old); |
1135 | SvREFCNT_dec (old); |
1086 | return 0; |
1136 | return 0; |
1087 | } |
1137 | } |
1088 | } |
1138 | } |
1089 | |
1139 | |
1090 | return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0; |
1140 | return PL_vtbl_sigelem.svt_clear ? PL_vtbl_sigelem.svt_clear (aTHX_ sv, mg) : 0; |
1091 | } |
1141 | } |
1092 | |
1142 | |
1093 | static int ecb_cold |
1143 | static int ecb_cold |
1094 | coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) |
1144 | coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) |
1095 | { |
1145 | { |
… | |
… | |
1109 | SvREFCNT_dec (old); |
1159 | SvREFCNT_dec (old); |
1110 | return 0; |
1160 | return 0; |
1111 | } |
1161 | } |
1112 | } |
1162 | } |
1113 | |
1163 | |
1114 | return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; |
1164 | return PL_vtbl_sigelem.svt_set ? PL_vtbl_sigelem.svt_set (aTHX_ sv, mg) : 0; |
1115 | } |
1165 | } |
1116 | |
1166 | |
1117 | static void |
1167 | static void |
1118 | prepare_nop (pTHX_ struct coro_transfer_args *ta) |
1168 | prepare_nop (pTHX_ struct coro_transfer_args *ta) |
1119 | { |
1169 | { |
… | |
… | |
1130 | static int |
1180 | static int |
1131 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
1181 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
1132 | { |
1182 | { |
1133 | return 1; |
1183 | return 1; |
1134 | } |
1184 | } |
|
|
1185 | |
|
|
1186 | /** coroutine stack handling ************************************************/ |
1135 | |
1187 | |
1136 | static UNOP init_perl_op; |
1188 | static UNOP init_perl_op; |
1137 | |
1189 | |
1138 | ecb_noinline static void /* noinline to keep it out of the transfer fast path */ |
1190 | ecb_noinline static void /* noinline to keep it out of the transfer fast path */ |
1139 | init_perl (pTHX_ struct coro *coro) |
1191 | init_perl (pTHX_ struct coro *coro) |
… | |
… | |
1206 | PL_op = (OP *)&init_perl_op; |
1258 | PL_op = (OP *)&init_perl_op; |
1207 | |
1259 | |
1208 | /* copy throw, in case it was set before init_perl */ |
1260 | /* copy throw, in case it was set before init_perl */ |
1209 | CORO_THROW = coro->except; |
1261 | CORO_THROW = coro->except; |
1210 | |
1262 | |
1211 | SWAP_SVS (coro); |
1263 | SWAP_SVS_ENTER (coro); |
1212 | |
1264 | |
1213 | if (ecb_expect_false (enable_times)) |
1265 | if (ecb_expect_false (enable_times)) |
1214 | { |
1266 | { |
1215 | coro_times_update (); |
1267 | coro_times_update (); |
1216 | coro_times_sub (coro); |
1268 | coro_times_sub (coro); |
… | |
… | |
1254 | /* this will cause transfer_check to croak on block*/ |
1306 | /* this will cause transfer_check to croak on block*/ |
1255 | SvRV_set (coro_current, (SV *)coro->hv); |
1307 | SvRV_set (coro_current, (SV *)coro->hv); |
1256 | |
1308 | |
1257 | load_perl (aTHX_ coro); |
1309 | load_perl (aTHX_ coro); |
1258 | |
1310 | |
|
|
1311 | /* restore swapped sv's */ |
|
|
1312 | SWAP_SVS_LEAVE (coro); |
|
|
1313 | |
1259 | coro_unwind_stacks (aTHX); |
1314 | coro_unwind_stacks (aTHX); |
1260 | |
|
|
1261 | /* restore swapped sv's */ |
|
|
1262 | SWAP_SVS (coro); |
|
|
1263 | |
1315 | |
1264 | coro_destruct_stacks (aTHX); |
1316 | coro_destruct_stacks (aTHX); |
1265 | |
1317 | |
1266 | /* now save some sv's to be free'd later */ |
1318 | /* now save some sv's to be free'd later */ |
1267 | svf [0] = GvSV (PL_defgv); |
1319 | svf [0] = GvSV (PL_defgv); |
… | |
… | |
1514 | * coro body here, as perl_run destroys these. Likewise, we cannot catch |
1566 | * coro body here, as perl_run destroys these. Likewise, we cannot catch |
1515 | * runtime errors here, as this is just a random interpreter, not a thread. |
1567 | * runtime errors here, as this is just a random interpreter, not a thread. |
1516 | */ |
1568 | */ |
1517 | |
1569 | |
1518 | /* |
1570 | /* |
|
|
1571 | * pp_entersub in 5.24 no longer ENTERs, but perl_destruct |
|
|
1572 | * requires PL_scopestack_ix, so do it here if required. |
|
|
1573 | */ |
|
|
1574 | if (!PL_scopestack_ix) |
|
|
1575 | ENTER; |
|
|
1576 | |
|
|
1577 | /* |
1519 | * If perl-run returns we assume exit() was being called or the coro |
1578 | * If perl-run returns we assume exit() was being called or the coro |
1520 | * fell off the end, which seems to be the only valid (non-bug) |
1579 | * fell off the end, which seems to be the only valid (non-bug) |
1521 | * reason for perl_run to return. We try to mimic whatever perl is normally |
1580 | * reason for perl_run to return. We try to mimic whatever perl is normally |
1522 | * doing in that case. YMMV. |
1581 | * doing in that case. YMMV. |
1523 | */ |
1582 | */ |
… | |
… | |
2402 | else |
2461 | else |
2403 | { |
2462 | { |
2404 | av_clear (GvAV (PL_defgv)); |
2463 | av_clear (GvAV (PL_defgv)); |
2405 | hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0); |
2464 | hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0); |
2406 | |
2465 | |
|
|
2466 | if (ecb_expect_false (coro->swap_sv)) |
|
|
2467 | { |
|
|
2468 | swap_svs_leave (coro); |
|
|
2469 | SvREFCNT_dec_NN (coro->swap_sv); |
|
|
2470 | coro->swap_sv = 0; |
|
|
2471 | } |
|
|
2472 | |
2407 | coro->prio = 0; |
2473 | coro->prio = 0; |
2408 | |
2474 | |
2409 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
2475 | if (ecb_expect_false (coro->cctx) && ecb_expect_false (coro->cctx->flags & CC_TRACE)) |
2410 | api_trace (aTHX_ coro_current, 0); |
2476 | api_trace (aTHX_ coro_current, 0); |
2411 | |
2477 | |
2412 | frame->prepare = prepare_schedule; |
2478 | frame->prepare = prepare_schedule; |
2413 | av_push (av_async_pool, SvREFCNT_inc (hv)); |
2479 | av_push (av_async_pool, SvREFCNT_inc (hv)); |
2414 | } |
2480 | } |
… | |
… | |
2870 | |
2936 | |
2871 | for (i = AvFILLp (av) - 1; i >= 0; i -= 2) |
2937 | for (i = AvFILLp (av) - 1; i >= 0; i -= 2) |
2872 | if (AvARRAY (av)[i] == (SV *)hook) |
2938 | if (AvARRAY (av)[i] == (SV *)hook) |
2873 | { |
2939 | { |
2874 | if (execute) |
2940 | if (execute) |
2875 | hook ((void *)AvARRAY (av)[i + 1]); |
2941 | hook (aTHX_ (void *)AvARRAY (av)[i + 1]); |
2876 | |
2942 | |
2877 | memmove (AvARRAY (av) + i, AvARRAY (av) + i + 2, AvFILLp (av) - i - 1); |
2943 | memmove (AvARRAY (av) + i, AvARRAY (av) + i + 2, AvFILLp (av) - i - 1); |
2878 | av_pop (av); |
2944 | av_pop (av); |
2879 | av_pop (av); |
2945 | av_pop (av); |
2880 | break; |
2946 | break; |
… | |
… | |
2892 | { |
2958 | { |
2893 | struct coro *coro = SvSTATE (coro_sv); |
2959 | struct coro *coro = SvSTATE (coro_sv); |
2894 | |
2960 | |
2895 | if (SvSTATE_current == coro) |
2961 | if (SvSTATE_current == coro) |
2896 | if (enter) |
2962 | if (enter) |
2897 | enter (aTHX enter_arg); |
2963 | enter (aTHX_ enter_arg); |
2898 | |
2964 | |
2899 | enterleave_hook_xs (aTHX_ coro, &coro->on_enter_xs, enter, enter_arg); |
2965 | enterleave_hook_xs (aTHX_ coro, &coro->on_enter_xs, enter, enter_arg); |
2900 | enterleave_hook_xs (aTHX_ coro, &coro->on_leave_xs, leave, leave_arg); |
2966 | enterleave_hook_xs (aTHX_ coro, &coro->on_leave_xs, leave, leave_arg); |
2901 | } |
2967 | } |
2902 | |
2968 | |
… | |
… | |
3588 | cctx_current = cctx_new_empty (); |
3654 | cctx_current = cctx_new_empty (); |
3589 | |
3655 | |
3590 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
3656 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
3591 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
3657 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
3592 | |
3658 | |
3593 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
3659 | { |
3594 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
3660 | /* |
3595 | orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr; |
3661 | * we provide a vtbvl for %SIG magic that replaces PL_vtbl_sig |
|
|
3662 | * by coro_sig_vtbl in hash values. |
|
|
3663 | */ |
|
|
3664 | MAGIC *mg = mg_find ((SV *)GvHV (gv_fetchpv ("SIG", GV_ADD | GV_NOTQUAL, SVt_PVHV)), PERL_MAGIC_sig); |
|
|
3665 | |
|
|
3666 | /* this only works if perl doesn't have a vtbl for %SIG */ |
|
|
3667 | assert (!mg->mg_virtual); |
|
|
3668 | |
|
|
3669 | /* |
|
|
3670 | * The irony is that the perl API itself asserts that mg_virtual |
|
|
3671 | * must be non-const, yet perl5porters insisted on marking their |
|
|
3672 | * vtbls as read-only, just to thwart perl modules from patching |
|
|
3673 | * them. |
|
|
3674 | */ |
|
|
3675 | mg->mg_virtual = (MGVTBL *)&coro_sig_vtbl; |
|
|
3676 | mg->mg_flags |= MGf_COPY; |
|
|
3677 | |
|
|
3678 | coro_sigelem_vtbl = PL_vtbl_sigelem; |
|
|
3679 | coro_sigelem_vtbl.svt_get = coro_sigelem_get; |
|
|
3680 | coro_sigelem_vtbl.svt_set = coro_sigelem_set; |
|
|
3681 | coro_sigelem_vtbl.svt_clear = coro_sigelem_clr; |
|
|
3682 | } |
3596 | |
3683 | |
3597 | hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); |
|
|
3598 | rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); |
3684 | rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); |
3599 | rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); |
3685 | rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); |
3600 | |
3686 | |
3601 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
3687 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
3602 | |
3688 | |
… | |
… | |
3908 | if (ecb_expect_false (current == self)) |
3994 | if (ecb_expect_false (current == self)) |
3909 | coro_times_sub (SvSTATE (coro_current)); |
3995 | coro_times_sub (SvSTATE (coro_current)); |
3910 | } |
3996 | } |
3911 | |
3997 | |
3912 | void |
3998 | void |
3913 | swap_sv (Coro::State coro, SV *sv, SV *swapsv) |
3999 | swap_sv (Coro::State coro, SV *sva, SV *svb) |
3914 | CODE: |
4000 | CODE: |
3915 | { |
4001 | { |
3916 | struct coro *current = SvSTATE_current; |
4002 | struct coro *current = SvSTATE_current; |
|
|
4003 | AV *swap_sv; |
|
|
4004 | int i; |
|
|
4005 | |
|
|
4006 | sva = SvRV (sva); |
|
|
4007 | svb = SvRV (svb); |
3917 | |
4008 | |
3918 | if (current == coro) |
4009 | if (current == coro) |
3919 | SWAP_SVS (current); |
4010 | SWAP_SVS_LEAVE (current); |
3920 | |
4011 | |
3921 | if (!coro->swap_sv) |
4012 | if (!coro->swap_sv) |
3922 | coro->swap_sv = newAV (); |
4013 | coro->swap_sv = newAV (); |
3923 | |
4014 | |
|
|
4015 | swap_sv = coro->swap_sv; |
|
|
4016 | |
|
|
4017 | for (i = AvFILLp (swap_sv) - 1; i >= 0; i -= 2) |
|
|
4018 | { |
|
|
4019 | SV *a = AvARRAY (swap_sv)[i ]; |
|
|
4020 | SV *b = AvARRAY (swap_sv)[i + 1]; |
|
|
4021 | |
|
|
4022 | if (a == sva && b == svb) |
|
|
4023 | { |
|
|
4024 | SvREFCNT_dec_NN (a); |
|
|
4025 | SvREFCNT_dec_NN (b); |
|
|
4026 | |
|
|
4027 | for (; i <= AvFILLp (swap_sv) - 2; i++) |
|
|
4028 | AvARRAY (swap_sv)[i] = AvARRAY (swap_sv)[i + 2]; |
|
|
4029 | |
|
|
4030 | AvFILLp (swap_sv) -= 2; |
|
|
4031 | |
|
|
4032 | goto removed; |
|
|
4033 | } |
|
|
4034 | } |
|
|
4035 | |
3924 | av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (sv ))); |
4036 | av_push (swap_sv, SvREFCNT_inc_NN (sva)); |
3925 | av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (swapsv))); |
4037 | av_push (swap_sv, SvREFCNT_inc_NN (svb)); |
|
|
4038 | |
|
|
4039 | removed: |
3926 | |
4040 | |
3927 | if (current == coro) |
4041 | if (current == coro) |
3928 | SWAP_SVS (current); |
4042 | SWAP_SVS_ENTER (current); |
3929 | } |
4043 | } |
3930 | |
4044 | |
3931 | |
4045 | |
3932 | MODULE = Coro::State PACKAGE = Coro |
4046 | MODULE = Coro::State PACKAGE = Coro |
3933 | |
4047 | |
3934 | BOOT: |
4048 | BOOT: |
3935 | { |
4049 | { |
|
|
4050 | if (SVt_LAST > 32) |
|
|
4051 | croak ("Coro internal error: SVt_LAST > 32, swap_sv might need adjustment"); |
|
|
4052 | |
3936 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
4053 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3937 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
4054 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3938 | cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); |
4055 | cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); |
3939 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
4056 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
3940 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
4057 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
… | |
… | |
4244 | RETVAL = newSVsv (AvARRAY ((AV *)SvRV (self))[0]); |
4361 | RETVAL = newSVsv (AvARRAY ((AV *)SvRV (self))[0]); |
4245 | OUTPUT: |
4362 | OUTPUT: |
4246 | RETVAL |
4363 | RETVAL |
4247 | |
4364 | |
4248 | void |
4365 | void |
4249 | up (SV *self, int adjust = 1) |
4366 | up (SV *self) |
4250 | ALIAS: |
|
|
4251 | adjust = 1 |
|
|
4252 | CODE: |
4367 | CODE: |
|
|
4368 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), 1); |
|
|
4369 | |
|
|
4370 | void |
|
|
4371 | adjust (SV *self, int adjust) |
|
|
4372 | CODE: |
4253 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
4373 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), adjust); |
4254 | |
4374 | |
4255 | void |
4375 | void |
4256 | down (...) |
4376 | down (...) |
4257 | CODE: |
4377 | CODE: |
4258 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
4378 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |