… | |
… | |
52 | #else |
52 | #else |
53 | # define REGISTER_STACK(cctx,start,end) |
53 | # define REGISTER_STACK(cctx,start,end) |
54 | #endif |
54 | #endif |
55 | |
55 | |
56 | /* the maximum number of idle cctx that will be pooled */ |
56 | /* the maximum number of idle cctx that will be pooled */ |
57 | #define MAX_IDLE_CCTX 8 |
57 | static int cctx_max_idle = 4; |
58 | |
58 | |
59 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
59 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
60 | (PERL_REVISION > (a) \ |
60 | (PERL_REVISION > (a) \ |
61 | || (PERL_REVISION == (a) \ |
61 | || (PERL_REVISION == (a) \ |
62 | && (PERL_VERSION > (b) \ |
62 | && (PERL_VERSION > (b) \ |
… | |
… | |
81 | # ifndef IS_PADCONST |
81 | # ifndef IS_PADCONST |
82 | # define IS_PADCONST(v) 0 |
82 | # define IS_PADCONST(v) 0 |
83 | # endif |
83 | # endif |
84 | #endif |
84 | #endif |
85 | |
85 | |
|
|
86 | /* 5.11 */ |
|
|
87 | #ifndef CxHASARGS |
|
|
88 | # define CxHASARGS(cx) (cx)->blk_sub.hasargs |
|
|
89 | #endif |
|
|
90 | |
|
|
91 | /* 5.10.0 */ |
|
|
92 | #ifndef SvREFCNT_inc_NN |
|
|
93 | # define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv) |
|
|
94 | #endif |
|
|
95 | |
86 | /* 5.8.8 */ |
96 | /* 5.8.8 */ |
87 | #ifndef GV_NOTQUAL |
97 | #ifndef GV_NOTQUAL |
88 | # define GV_NOTQUAL 0 |
98 | # define GV_NOTQUAL 0 |
89 | #endif |
99 | #endif |
90 | #ifndef newSV |
100 | #ifndef newSV |
91 | # define newSV(l) NEWSV(0,l) |
101 | # define newSV(l) NEWSV(0,l) |
92 | #endif |
|
|
93 | #ifndef SvREFCNT_inc_NN |
|
|
94 | # define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv) |
|
|
95 | #endif |
|
|
96 | |
|
|
97 | /* 5.11 */ |
|
|
98 | #ifndef CxHASARGS |
|
|
99 | # define CxHASARGS(cx) (cx)->blk_sub.hasargs |
|
|
100 | #endif |
102 | #endif |
101 | |
103 | |
102 | /* 5.8.7 */ |
104 | /* 5.8.7 */ |
103 | #ifndef SvRV_set |
105 | #ifndef SvRV_set |
104 | # define SvRV_set(s,v) SvRV(s) = (v) |
106 | # define SvRV_set(s,v) SvRV(s) = (v) |
… | |
… | |
140 | #define NOINLINE attribute ((noinline)) |
142 | #define NOINLINE attribute ((noinline)) |
141 | |
143 | |
142 | #include "CoroAPI.h" |
144 | #include "CoroAPI.h" |
143 | |
145 | |
144 | #ifdef USE_ITHREADS |
146 | #ifdef USE_ITHREADS |
|
|
147 | |
145 | static perl_mutex coro_mutex; |
148 | static perl_mutex coro_lock; |
146 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
149 | # define LOCK do { MUTEX_LOCK (&coro_lock); } while (0) |
147 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
150 | # define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0) |
|
|
151 | # if CORO_PTHREAD |
|
|
152 | static void *coro_thx; |
|
|
153 | # endif |
|
|
154 | |
148 | #else |
155 | #else |
|
|
156 | |
149 | # define LOCK (void)0 |
157 | # define LOCK (void)0 |
150 | # define UNLOCK (void)0 |
158 | # define UNLOCK (void)0 |
|
|
159 | |
151 | #endif |
160 | #endif |
|
|
161 | |
|
|
162 | # undef LOCK |
|
|
163 | # define LOCK (void)0 |
|
|
164 | # undef UNLOCK |
|
|
165 | # define UNLOCK (void)0 |
152 | |
166 | |
153 | /* helper storage struct for Coro::AIO */ |
167 | /* helper storage struct for Coro::AIO */ |
154 | struct io_state |
168 | struct io_state |
155 | { |
169 | { |
156 | AV *res; |
170 | AV *res; |
157 | int errorno; |
171 | int errorno; |
158 | I32 laststype; |
172 | I32 laststype; /* U16 in 5.10.0 */ |
159 | int laststatval; |
173 | int laststatval; |
160 | Stat_t statcache; |
174 | Stat_t statcache; |
161 | }; |
175 | }; |
162 | |
176 | |
163 | static double (*nvtime)(); /* so why doesn't it take void? */ |
177 | static double (*nvtime)(); /* so why doesn't it take void? */ |
164 | |
178 | |
|
|
179 | static U32 cctx_gen; |
165 | static size_t coro_stacksize = CORO_STACKSIZE; |
180 | static size_t cctx_stacksize = CORO_STACKSIZE; |
166 | static struct CoroAPI coroapi; |
181 | static struct CoroAPI coroapi; |
167 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
182 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
168 | static JMPENV *main_top_env; |
183 | static JMPENV *main_top_env; |
169 | static HV *coro_state_stash, *coro_stash; |
184 | static HV *coro_state_stash, *coro_stash; |
170 | static volatile SV *coro_mortal; /* will be freed after next transfer */ |
185 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
|
|
186 | static volatile struct coro *transfer_next; |
|
|
187 | |
|
|
188 | struct transfer_args |
|
|
189 | { |
|
|
190 | struct coro *prev, *next; |
|
|
191 | }; |
171 | |
192 | |
172 | static GV *irsgv; /* $/ */ |
193 | static GV *irsgv; /* $/ */ |
173 | static GV *stdoutgv; /* *STDOUT */ |
194 | static GV *stdoutgv; /* *STDOUT */ |
174 | static SV *rv_diehook; |
195 | static SV *rv_diehook; |
175 | static SV *rv_warnhook; |
196 | static SV *rv_warnhook; |
… | |
… | |
207 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
228 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
208 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
229 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
209 | JMPENV *top_env; |
230 | JMPENV *top_env; |
210 | coro_context cctx; |
231 | coro_context cctx; |
211 | |
232 | |
|
|
233 | U32 gen; |
212 | #if CORO_USE_VALGRIND |
234 | #if CORO_USE_VALGRIND |
213 | int valgrind_id; |
235 | int valgrind_id; |
214 | #endif |
236 | #endif |
215 | unsigned char flags; |
237 | unsigned char flags; |
216 | } coro_cctx; |
238 | } coro_cctx; |
… | |
… | |
276 | #define PRIO_MIN -4 |
298 | #define PRIO_MIN -4 |
277 | |
299 | |
278 | /* for Coro.pm */ |
300 | /* for Coro.pm */ |
279 | static SV *coro_current; |
301 | static SV *coro_current; |
280 | static SV *coro_readyhook; |
302 | static SV *coro_readyhook; |
281 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
303 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
282 | static int coro_nready; |
304 | static int coro_nready; |
283 | static struct coro *coro_first; |
305 | static struct coro *coro_first; |
284 | |
306 | |
285 | /** lowlevel stuff **********************************************************/ |
307 | /** lowlevel stuff **********************************************************/ |
286 | |
308 | |
… | |
… | |
381 | static MGVTBL coro_cv_vtbl = { |
403 | static MGVTBL coro_cv_vtbl = { |
382 | 0, 0, 0, 0, |
404 | 0, 0, 0, 0, |
383 | coro_cv_free |
405 | coro_cv_free |
384 | }; |
406 | }; |
385 | |
407 | |
386 | #define CORO_MAGIC(sv,type) \ |
408 | #define CORO_MAGIC(sv, type) \ |
387 | SvMAGIC (sv) \ |
409 | SvMAGIC (sv) \ |
388 | ? SvMAGIC (sv)->mg_type == type \ |
410 | ? SvMAGIC (sv)->mg_type == type \ |
389 | ? SvMAGIC (sv) \ |
411 | ? SvMAGIC (sv) \ |
390 | : mg_find (sv, type) \ |
412 | : mg_find (sv, type) \ |
391 | : 0 |
413 | : 0 |
392 | |
414 | |
393 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
415 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
394 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
416 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
395 | |
417 | |
396 | static struct coro * |
418 | static struct coro * |
… | |
… | |
626 | |
648 | |
627 | /* |
649 | /* |
628 | * destroy the stacks, the callchain etc... |
650 | * destroy the stacks, the callchain etc... |
629 | */ |
651 | */ |
630 | static void |
652 | static void |
631 | coro_destroy_stacks (pTHX) |
653 | coro_destruct_stacks (pTHX) |
632 | { |
654 | { |
633 | while (PL_curstackinfo->si_next) |
655 | while (PL_curstackinfo->si_next) |
634 | PL_curstackinfo = PL_curstackinfo->si_next; |
656 | PL_curstackinfo = PL_curstackinfo->si_next; |
635 | |
657 | |
636 | while (PL_curstackinfo) |
658 | while (PL_curstackinfo) |
… | |
… | |
818 | PL_rs = newSVsv (GvSV (irsgv)); |
840 | PL_rs = newSVsv (GvSV (irsgv)); |
819 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
841 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
820 | |
842 | |
821 | { |
843 | { |
822 | dSP; |
844 | dSP; |
823 | LOGOP myop; |
845 | UNOP myop; |
824 | |
846 | |
825 | Zero (&myop, 1, LOGOP); |
847 | Zero (&myop, 1, UNOP); |
826 | myop.op_next = Nullop; |
848 | myop.op_next = Nullop; |
827 | myop.op_flags = OPf_WANT_VOID; |
849 | myop.op_flags = OPf_WANT_VOID; |
828 | |
850 | |
829 | PUSHMARK (SP); |
851 | PUSHMARK (SP); |
830 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
852 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
… | |
… | |
834 | SPAGAIN; |
856 | SPAGAIN; |
835 | } |
857 | } |
836 | |
858 | |
837 | /* this newly created coroutine might be run on an existing cctx which most |
859 | /* this newly created coroutine might be run on an existing cctx which most |
838 | * likely was suspended in set_stacklevel, called from entersub. |
860 | * likely was suspended in set_stacklevel, called from entersub. |
839 | * set_stacklevl doesn't do anything on return, but entersub does LEAVE, |
861 | * set_stacklevel doesn't do anything on return, but entersub does LEAVE, |
840 | * so we ENTER here for symmetry |
862 | * so we ENTER here for symmetry. |
841 | */ |
863 | */ |
842 | ENTER; |
864 | ENTER; |
843 | } |
865 | } |
844 | |
866 | |
845 | static void |
867 | static void |
846 | coro_destroy (pTHX_ struct coro *coro) |
868 | coro_destruct (pTHX_ struct coro *coro) |
847 | { |
869 | { |
848 | if (!IN_DESTRUCT) |
870 | if (!IN_DESTRUCT) |
849 | { |
871 | { |
850 | /* restore all saved variables and stuff */ |
872 | /* restore all saved variables and stuff */ |
851 | LEAVE_SCOPE (0); |
873 | LEAVE_SCOPE (0); |
… | |
… | |
873 | SvREFCNT_dec (PL_warnhook); |
895 | SvREFCNT_dec (PL_warnhook); |
874 | |
896 | |
875 | SvREFCNT_dec (coro->saved_deffh); |
897 | SvREFCNT_dec (coro->saved_deffh); |
876 | SvREFCNT_dec (coro->throw); |
898 | SvREFCNT_dec (coro->throw); |
877 | |
899 | |
878 | coro_destroy_stacks (aTHX); |
900 | coro_destruct_stacks (aTHX); |
879 | } |
901 | } |
880 | |
902 | |
881 | static void |
903 | static void |
882 | free_coro_mortal (pTHX) |
904 | free_coro_mortal (pTHX) |
883 | { |
905 | { |
… | |
… | |
1010 | |
1032 | |
1011 | TAINT_NOT; |
1033 | TAINT_NOT; |
1012 | return 0; |
1034 | return 0; |
1013 | } |
1035 | } |
1014 | |
1036 | |
|
|
1037 | static void |
|
|
1038 | prepare_set_stacklevel (struct transfer_args *ta, struct coro_cctx *cctx) |
|
|
1039 | { |
|
|
1040 | ta->prev = (struct coro *)cctx; |
|
|
1041 | ta->next = 0; |
|
|
1042 | } |
|
|
1043 | |
1015 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
1044 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
1016 | /* _cctx_init should be careful, as it could be called at almost any time */ |
1045 | /* _cctx_init should be careful, as it could be called at almost any time */ |
1017 | /* during execution of a perl program */ |
1046 | /* during execution of a perl program */ |
|
|
1047 | /* also initialises PL_top_env */ |
1018 | static void NOINLINE |
1048 | static void NOINLINE |
1019 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1049 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1020 | { |
1050 | { |
1021 | dSP; |
1051 | dSP; |
1022 | LOGOP myop; |
1052 | UNOP myop; |
1023 | |
1053 | |
1024 | PL_top_env = &PL_start_env; |
1054 | PL_top_env = &PL_start_env; |
1025 | |
1055 | |
1026 | if (cctx->flags & CC_TRACE) |
1056 | if (cctx->flags & CC_TRACE) |
1027 | PL_runops = runops_trace; |
1057 | PL_runops = runops_trace; |
1028 | |
1058 | |
1029 | Zero (&myop, 1, LOGOP); |
1059 | Zero (&myop, 1, UNOP); |
1030 | myop.op_next = PL_op; |
1060 | myop.op_next = PL_op; |
1031 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
1061 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
1032 | |
1062 | |
1033 | PUSHMARK (SP); |
1063 | PUSHMARK (SP); |
1034 | EXTEND (SP, 2); |
1064 | EXTEND (SP, 2); |
1035 | PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); |
1065 | PUSHs (sv_2mortal (newSViv ((IV)cctx))); |
1036 | PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
1066 | PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
1037 | PUTBACK; |
1067 | PUTBACK; |
1038 | PL_op = (OP *)&myop; |
1068 | PL_op = (OP *)&myop; |
1039 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
1069 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
1040 | SPAGAIN; |
1070 | SPAGAIN; |
1041 | } |
1071 | } |
1042 | |
1072 | |
|
|
1073 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
|
|
1074 | static void |
|
|
1075 | transfer_tail (pTHX) |
|
|
1076 | { |
|
|
1077 | struct coro *next = (struct coro *)transfer_next; |
|
|
1078 | transfer_next = 0; //D for temporary assertion in transfer |
|
|
1079 | assert (("FATAL ERROR: internal error 1067 in Coro module, please report", next));//D |
|
|
1080 | |
|
|
1081 | free_coro_mortal (aTHX); |
|
|
1082 | UNLOCK; |
|
|
1083 | |
|
|
1084 | if (expect_false (next->throw)) |
|
|
1085 | { |
|
|
1086 | SV *exception = sv_2mortal (next->throw); |
|
|
1087 | |
|
|
1088 | next->throw = 0; |
|
|
1089 | sv_setsv (ERRSV, exception); |
|
|
1090 | croak (0); |
|
|
1091 | } |
|
|
1092 | } |
|
|
1093 | |
1043 | /* |
1094 | /* |
1044 | * this is a _very_ stripped down perl interpreter ;) |
1095 | * this is a _very_ stripped down perl interpreter ;) |
1045 | */ |
1096 | */ |
1046 | static void |
1097 | static void |
1047 | cctx_run (void *arg) |
1098 | cctx_run (void *arg) |
1048 | { |
1099 | { |
|
|
1100 | #ifdef USE_ITHREADS |
|
|
1101 | # if CORO_PTHREAD |
|
|
1102 | PERL_SET_CONTEXT (coro_thx); |
|
|
1103 | # endif |
|
|
1104 | #endif |
|
|
1105 | { |
1049 | dTHX; |
1106 | dTHX; |
1050 | |
1107 | |
1051 | /* cctx_run is the alternative tail of transfer(), so unlock here. */ |
1108 | /* entersub called ENTER, but we never 'returned', undo that here */ |
1052 | UNLOCK; |
1109 | LEAVE; |
1053 | |
1110 | |
1054 | /* we now skip the entersub that lead to transfer() */ |
1111 | /* we now skip the entersub that did lead to transfer() */ |
1055 | PL_op = PL_op->op_next; |
1112 | PL_op = PL_op->op_next; |
1056 | |
1113 | |
1057 | /* inject a fake subroutine call to cctx_init */ |
1114 | /* inject a fake subroutine call to cctx_init */ |
1058 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1115 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1059 | |
1116 | |
|
|
1117 | /* cctx_run is the alternative tail of transfer() */ |
|
|
1118 | transfer_tail (aTHX); |
|
|
1119 | |
1060 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1120 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1061 | PL_restartop = PL_op; |
1121 | PL_restartop = PL_op; |
1062 | perl_run (PL_curinterp); |
1122 | perl_run (PL_curinterp); |
1063 | |
1123 | |
1064 | /* |
1124 | /* |
1065 | * If perl-run returns we assume exit() was being called or the coro |
1125 | * If perl-run returns we assume exit() was being called or the coro |
1066 | * fell off the end, which seems to be the only valid (non-bug) |
1126 | * fell off the end, which seems to be the only valid (non-bug) |
1067 | * reason for perl_run to return. We try to exit by jumping to the |
1127 | * reason for perl_run to return. We try to exit by jumping to the |
1068 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1128 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1069 | * coroutine as Coro has no such concept |
1129 | * coroutine as Coro has no such concept |
1070 | */ |
1130 | */ |
1071 | PL_top_env = main_top_env; |
1131 | PL_top_env = main_top_env; |
1072 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1132 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
|
|
1133 | } |
1073 | } |
1134 | } |
1074 | |
1135 | |
1075 | static coro_cctx * |
1136 | static coro_cctx * |
1076 | cctx_new () |
1137 | cctx_new () |
1077 | { |
1138 | { |
1078 | coro_cctx *cctx; |
1139 | coro_cctx *cctx; |
|
|
1140 | |
|
|
1141 | ++cctx_count; |
|
|
1142 | New (0, cctx, 1, coro_cctx); |
|
|
1143 | |
|
|
1144 | cctx->gen = cctx_gen; |
|
|
1145 | cctx->flags = 0; |
|
|
1146 | cctx->idle_sp = 0; /* can be accessed by transfer between cctx_run and set_stacklevel, on throw */ |
|
|
1147 | |
|
|
1148 | return cctx; |
|
|
1149 | } |
|
|
1150 | |
|
|
1151 | /* create a new cctx only suitable as source */ |
|
|
1152 | static coro_cctx * |
|
|
1153 | cctx_new_empty () |
|
|
1154 | { |
|
|
1155 | coro_cctx *cctx = cctx_new (); |
|
|
1156 | |
|
|
1157 | cctx->sptr = 0; |
|
|
1158 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
|
|
1159 | |
|
|
1160 | return cctx; |
|
|
1161 | } |
|
|
1162 | |
|
|
1163 | /* create a new cctx suitable as destination/running a perl interpreter */ |
|
|
1164 | static coro_cctx * |
|
|
1165 | cctx_new_run () |
|
|
1166 | { |
|
|
1167 | coro_cctx *cctx = cctx_new (); |
1079 | void *stack_start; |
1168 | void *stack_start; |
1080 | size_t stack_size; |
1169 | size_t stack_size; |
1081 | |
1170 | |
1082 | ++cctx_count; |
|
|
1083 | |
|
|
1084 | Newz (0, cctx, 1, coro_cctx); |
|
|
1085 | |
|
|
1086 | #if HAVE_MMAP |
1171 | #if HAVE_MMAP |
1087 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1172 | cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1088 | /* mmap supposedly does allocate-on-write for us */ |
1173 | /* mmap supposedly does allocate-on-write for us */ |
1089 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
1174 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
1090 | |
1175 | |
1091 | if (cctx->sptr != (void *)-1) |
1176 | if (cctx->sptr != (void *)-1) |
1092 | { |
1177 | { |
… | |
… | |
1098 | cctx->flags |= CC_MAPPED; |
1183 | cctx->flags |= CC_MAPPED; |
1099 | } |
1184 | } |
1100 | else |
1185 | else |
1101 | #endif |
1186 | #endif |
1102 | { |
1187 | { |
1103 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
1188 | cctx->ssize = cctx_stacksize * (long)sizeof (long); |
1104 | New (0, cctx->sptr, coro_stacksize, long); |
1189 | New (0, cctx->sptr, cctx_stacksize, long); |
1105 | |
1190 | |
1106 | if (!cctx->sptr) |
1191 | if (!cctx->sptr) |
1107 | { |
1192 | { |
1108 | perror ("FATAL: unable to allocate stack for coroutine"); |
1193 | perror ("FATAL: unable to allocate stack for coroutine"); |
1109 | _exit (EXIT_FAILURE); |
1194 | _exit (EXIT_FAILURE); |
… | |
… | |
1124 | { |
1209 | { |
1125 | if (!cctx) |
1210 | if (!cctx) |
1126 | return; |
1211 | return; |
1127 | |
1212 | |
1128 | --cctx_count; |
1213 | --cctx_count; |
|
|
1214 | coro_destroy (&cctx->cctx); |
1129 | |
1215 | |
|
|
1216 | /* coro_transfer creates new, empty cctx's */ |
|
|
1217 | if (cctx->sptr) |
|
|
1218 | { |
1130 | #if CORO_USE_VALGRIND |
1219 | #if CORO_USE_VALGRIND |
1131 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1220 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1132 | #endif |
1221 | #endif |
1133 | |
1222 | |
1134 | #if HAVE_MMAP |
1223 | #if HAVE_MMAP |
1135 | if (cctx->flags & CC_MAPPED) |
1224 | if (cctx->flags & CC_MAPPED) |
1136 | munmap (cctx->sptr, cctx->ssize); |
1225 | munmap (cctx->sptr, cctx->ssize); |
1137 | else |
1226 | else |
1138 | #endif |
1227 | #endif |
1139 | Safefree (cctx->sptr); |
1228 | Safefree (cctx->sptr); |
|
|
1229 | } |
1140 | |
1230 | |
1141 | Safefree (cctx); |
1231 | Safefree (cctx); |
1142 | } |
1232 | } |
1143 | |
1233 | |
1144 | /* wether this cctx should be destructed */ |
1234 | /* wether this cctx should be destructed */ |
1145 | #define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) |
1235 | #define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE)) |
1146 | |
1236 | |
1147 | static coro_cctx * |
1237 | static coro_cctx * |
1148 | cctx_get (pTHX) |
1238 | cctx_get (pTHX) |
1149 | { |
1239 | { |
1150 | while (expect_true (cctx_first)) |
1240 | while (expect_true (cctx_first)) |
… | |
… | |
1157 | return cctx; |
1247 | return cctx; |
1158 | |
1248 | |
1159 | cctx_destroy (cctx); |
1249 | cctx_destroy (cctx); |
1160 | } |
1250 | } |
1161 | |
1251 | |
1162 | return cctx_new (); |
1252 | return cctx_new_run (); |
1163 | } |
1253 | } |
1164 | |
1254 | |
1165 | static void |
1255 | static void |
1166 | cctx_put (coro_cctx *cctx) |
1256 | cctx_put (coro_cctx *cctx) |
1167 | { |
1257 | { |
|
|
1258 | assert (("cctx_put called on non-initialised cctx", cctx->sptr)); |
|
|
1259 | |
1168 | /* free another cctx if overlimit */ |
1260 | /* free another cctx if overlimit */ |
1169 | if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) |
1261 | if (expect_false (cctx_idle >= cctx_max_idle)) |
1170 | { |
1262 | { |
1171 | coro_cctx *first = cctx_first; |
1263 | coro_cctx *first = cctx_first; |
1172 | cctx_first = first->next; |
1264 | cctx_first = first->next; |
1173 | --cctx_idle; |
1265 | --cctx_idle; |
1174 | |
1266 | |
… | |
… | |
1206 | /* always use the TRANSFER macro */ |
1298 | /* always use the TRANSFER macro */ |
1207 | static void NOINLINE |
1299 | static void NOINLINE |
1208 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1300 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1209 | { |
1301 | { |
1210 | dSTACKLEVEL; |
1302 | dSTACKLEVEL; |
1211 | static volatile int has_throw; |
|
|
1212 | |
1303 | |
1213 | /* sometimes transfer is only called to set idle_sp */ |
1304 | /* sometimes transfer is only called to set idle_sp */ |
1214 | if (expect_false (!next)) |
1305 | if (expect_false (!next)) |
1215 | { |
1306 | { |
1216 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1307 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
… | |
… | |
1220 | { |
1311 | { |
1221 | coro_cctx *prev__cctx; |
1312 | coro_cctx *prev__cctx; |
1222 | |
1313 | |
1223 | if (expect_false (prev->flags & CF_NEW)) |
1314 | if (expect_false (prev->flags & CF_NEW)) |
1224 | { |
1315 | { |
1225 | /* create a new empty context */ |
1316 | /* create a new empty/source context */ |
1226 | Newz (0, prev->cctx, 1, coro_cctx); |
1317 | prev->cctx = cctx_new_empty (); |
1227 | prev->flags &= ~CF_NEW; |
1318 | prev->flags &= ~CF_NEW; |
1228 | prev->flags |= CF_RUNNING; |
1319 | prev->flags |= CF_RUNNING; |
1229 | } |
1320 | } |
1230 | |
1321 | |
1231 | prev->flags &= ~CF_RUNNING; |
1322 | prev->flags &= ~CF_RUNNING; |
… | |
… | |
1246 | else |
1337 | else |
1247 | load_perl (aTHX_ next); |
1338 | load_perl (aTHX_ next); |
1248 | |
1339 | |
1249 | prev__cctx = prev->cctx; |
1340 | prev__cctx = prev->cctx; |
1250 | |
1341 | |
1251 | /* possibly "free" the cctx */ |
1342 | /* possibly untie and reuse the cctx */ |
1252 | if (expect_true ( |
1343 | if (expect_true ( |
1253 | prev__cctx->idle_sp == STACKLEVEL |
1344 | prev__cctx->idle_sp == STACKLEVEL |
1254 | && !(prev__cctx->flags & CC_TRACE) |
1345 | && !(prev__cctx->flags & CC_TRACE) |
1255 | && !force_cctx |
1346 | && !force_cctx |
1256 | )) |
1347 | )) |
… | |
… | |
1272 | ++next->usecount; |
1363 | ++next->usecount; |
1273 | |
1364 | |
1274 | if (expect_true (!next->cctx)) |
1365 | if (expect_true (!next->cctx)) |
1275 | next->cctx = cctx_get (aTHX); |
1366 | next->cctx = cctx_get (aTHX); |
1276 | |
1367 | |
1277 | has_throw = !!next->throw; |
1368 | assert (("FATAL ERROR: internal error 1352 in Coro, please report", !transfer_next));//D |
|
|
1369 | transfer_next = next; |
1278 | |
1370 | |
1279 | if (expect_false (prev__cctx != next->cctx)) |
1371 | if (expect_false (prev__cctx != next->cctx)) |
1280 | { |
1372 | { |
1281 | prev__cctx->top_env = PL_top_env; |
1373 | prev__cctx->top_env = PL_top_env; |
1282 | PL_top_env = next->cctx->top_env; |
1374 | PL_top_env = next->cctx->top_env; |
1283 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1375 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1284 | } |
1376 | } |
1285 | |
1377 | |
1286 | free_coro_mortal (aTHX); |
1378 | transfer_tail (aTHX); |
1287 | UNLOCK; |
|
|
1288 | |
|
|
1289 | if (expect_false (has_throw)) |
|
|
1290 | { |
|
|
1291 | struct coro *coro = SvSTATE (coro_current); |
|
|
1292 | |
|
|
1293 | if (coro->throw) |
|
|
1294 | { |
|
|
1295 | SV *exception = coro->throw; |
|
|
1296 | coro->throw = 0; |
|
|
1297 | sv_setsv (ERRSV, exception); |
|
|
1298 | croak (0); |
|
|
1299 | } |
|
|
1300 | } |
|
|
1301 | } |
1379 | } |
1302 | } |
1380 | } |
1303 | |
|
|
1304 | struct transfer_args |
|
|
1305 | { |
|
|
1306 | struct coro *prev, *next; |
|
|
1307 | }; |
|
|
1308 | |
1381 | |
1309 | #define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx)) |
1382 | #define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx)) |
1310 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
1383 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
1311 | |
1384 | |
1312 | /** high level stuff ********************************************************/ |
1385 | /** high level stuff ********************************************************/ |
… | |
… | |
1338 | croak ("FATAL: tried to destroy currently running coroutine"); |
1411 | croak ("FATAL: tried to destroy currently running coroutine"); |
1339 | |
1412 | |
1340 | save_perl (aTHX_ &temp); |
1413 | save_perl (aTHX_ &temp); |
1341 | load_perl (aTHX_ coro); |
1414 | load_perl (aTHX_ coro); |
1342 | |
1415 | |
1343 | coro_destroy (aTHX_ coro); |
1416 | coro_destruct (aTHX_ coro); |
1344 | |
1417 | |
1345 | load_perl (aTHX_ &temp); |
1418 | load_perl (aTHX_ &temp); |
1346 | |
1419 | |
1347 | coro->slot = 0; |
1420 | coro->slot = 0; |
1348 | } |
1421 | } |
… | |
… | |
1524 | /* cannot transfer to destroyed coros, skip and look for next */ |
1597 | /* cannot transfer to destroyed coros, skip and look for next */ |
1525 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
1598 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
1526 | { |
1599 | { |
1527 | UNLOCK; |
1600 | UNLOCK; |
1528 | SvREFCNT_dec (next_sv); |
1601 | SvREFCNT_dec (next_sv); |
1529 | /* coro_nready is already taken care of by destroy */ |
1602 | /* coro_nready has already been taken care of by destroy */ |
1530 | continue; |
1603 | continue; |
1531 | } |
1604 | } |
1532 | |
1605 | |
1533 | --coro_nready; |
1606 | --coro_nready; |
1534 | UNLOCK; |
1607 | UNLOCK; |
… | |
… | |
1619 | struct coro *coro = SvSTATE (coro_sv); |
1692 | struct coro *coro = SvSTATE (coro_sv); |
1620 | |
1693 | |
1621 | if (flags & CC_TRACE) |
1694 | if (flags & CC_TRACE) |
1622 | { |
1695 | { |
1623 | if (!coro->cctx) |
1696 | if (!coro->cctx) |
1624 | coro->cctx = cctx_new (); |
1697 | coro->cctx = cctx_new_run (); |
1625 | else if (!(coro->cctx->flags & CC_TRACE)) |
1698 | else if (!(coro->cctx->flags & CC_TRACE)) |
1626 | croak ("cannot enable tracing on coroutine with custom stack"); |
1699 | croak ("cannot enable tracing on coroutine with custom stack"); |
1627 | |
1700 | |
1628 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1701 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1629 | } |
1702 | } |
… | |
… | |
1636 | else |
1709 | else |
1637 | coro->slot->runops = RUNOPS_DEFAULT; |
1710 | coro->slot->runops = RUNOPS_DEFAULT; |
1638 | } |
1711 | } |
1639 | } |
1712 | } |
1640 | |
1713 | |
|
|
1714 | #if 0 |
1641 | static int |
1715 | static int |
1642 | coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) |
1716 | coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) |
1643 | { |
1717 | { |
1644 | AV *padlist; |
1718 | AV *padlist; |
1645 | AV *av = (AV *)mg->mg_obj; |
1719 | AV *av = (AV *)mg->mg_obj; |
… | |
… | |
1651 | |
1725 | |
1652 | static MGVTBL coro_gensub_vtbl = { |
1726 | static MGVTBL coro_gensub_vtbl = { |
1653 | 0, 0, 0, 0, |
1727 | 0, 0, 0, 0, |
1654 | coro_gensub_free |
1728 | coro_gensub_free |
1655 | }; |
1729 | }; |
|
|
1730 | #endif |
1656 | |
1731 | |
1657 | /*****************************************************************************/ |
1732 | /*****************************************************************************/ |
1658 | /* PerlIO::cede */ |
1733 | /* PerlIO::cede */ |
1659 | |
1734 | |
1660 | typedef struct |
1735 | typedef struct |
… | |
… | |
1735 | PROTOTYPES: DISABLE |
1810 | PROTOTYPES: DISABLE |
1736 | |
1811 | |
1737 | BOOT: |
1812 | BOOT: |
1738 | { |
1813 | { |
1739 | #ifdef USE_ITHREADS |
1814 | #ifdef USE_ITHREADS |
1740 | MUTEX_INIT (&coro_mutex); |
1815 | MUTEX_INIT (&coro_lock); |
|
|
1816 | # if CORO_PTHREAD |
|
|
1817 | coro_thx = PERL_GET_CONTEXT; |
|
|
1818 | # endif |
1741 | #endif |
1819 | #endif |
1742 | BOOT_PAGESIZE; |
1820 | BOOT_PAGESIZE; |
1743 | |
1821 | |
1744 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1822 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1745 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1823 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
… | |
… | |
1826 | |
1904 | |
1827 | PUTBACK; |
1905 | PUTBACK; |
1828 | switch (ix) |
1906 | switch (ix) |
1829 | { |
1907 | { |
1830 | case 0: |
1908 | case 0: |
1831 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1909 | prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (ST (0))); |
1832 | ta.next = 0; |
|
|
1833 | break; |
1910 | break; |
1834 | |
1911 | |
1835 | case 1: |
1912 | case 1: |
1836 | if (items != 2) |
1913 | if (items != 2) |
1837 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1914 | croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items); |
1838 | |
1915 | |
1839 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1916 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1840 | break; |
1917 | break; |
1841 | |
1918 | |
1842 | case 2: |
1919 | case 2: |
… | |
… | |
1876 | _exit (code); |
1953 | _exit (code); |
1877 | |
1954 | |
1878 | int |
1955 | int |
1879 | cctx_stacksize (int new_stacksize = 0) |
1956 | cctx_stacksize (int new_stacksize = 0) |
1880 | CODE: |
1957 | CODE: |
1881 | RETVAL = coro_stacksize; |
1958 | RETVAL = cctx_stacksize; |
1882 | if (new_stacksize) |
1959 | if (new_stacksize) |
|
|
1960 | { |
1883 | coro_stacksize = new_stacksize; |
1961 | cctx_stacksize = new_stacksize; |
|
|
1962 | ++cctx_gen; |
|
|
1963 | } |
|
|
1964 | OUTPUT: |
|
|
1965 | RETVAL |
|
|
1966 | |
|
|
1967 | int |
|
|
1968 | cctx_max_idle (int max_idle = 0) |
|
|
1969 | CODE: |
|
|
1970 | RETVAL = cctx_max_idle; |
|
|
1971 | if (max_idle > 1) |
|
|
1972 | cctx_max_idle = max_idle; |
1884 | OUTPUT: |
1973 | OUTPUT: |
1885 | RETVAL |
1974 | RETVAL |
1886 | |
1975 | |
1887 | int |
1976 | int |
1888 | cctx_count () |
1977 | cctx_count () |
… | |
… | |
1966 | RETVAL = boolSV (coro->flags & ix); |
2055 | RETVAL = boolSV (coro->flags & ix); |
1967 | OUTPUT: |
2056 | OUTPUT: |
1968 | RETVAL |
2057 | RETVAL |
1969 | |
2058 | |
1970 | void |
2059 | void |
|
|
2060 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
|
|
2061 | PROTOTYPE: $;$ |
|
|
2062 | CODE: |
|
|
2063 | SvREFCNT_dec (self->throw); |
|
|
2064 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
2065 | |
|
|
2066 | void |
1971 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
2067 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
1972 | |
2068 | |
1973 | SV * |
2069 | SV * |
1974 | has_cctx (Coro::State coro) |
2070 | has_cctx (Coro::State coro) |
1975 | PROTOTYPE: $ |
2071 | PROTOTYPE: $ |
… | |
… | |
1984 | CODE: |
2080 | CODE: |
1985 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
2081 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
1986 | OUTPUT: |
2082 | OUTPUT: |
1987 | RETVAL |
2083 | RETVAL |
1988 | |
2084 | |
1989 | IV |
2085 | UV |
1990 | rss (Coro::State coro) |
2086 | rss (Coro::State coro) |
1991 | PROTOTYPE: $ |
2087 | PROTOTYPE: $ |
1992 | ALIAS: |
2088 | ALIAS: |
1993 | usecount = 1 |
2089 | usecount = 1 |
1994 | CODE: |
2090 | CODE: |
… | |
… | |
2005 | CODE: |
2101 | CODE: |
2006 | struct coro *coro = SvSTATE (coro_current); |
2102 | struct coro *coro = SvSTATE (coro_current); |
2007 | coro->cctx->idle_sp = 0; |
2103 | coro->cctx->idle_sp = 0; |
2008 | |
2104 | |
2009 | void |
2105 | void |
2010 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
|
|
2011 | PROTOTYPE: $;$ |
|
|
2012 | CODE: |
|
|
2013 | SvREFCNT_dec (self->throw); |
|
|
2014 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
2015 | |
|
|
2016 | void |
|
|
2017 | swap_defsv (Coro::State self) |
2106 | swap_defsv (Coro::State self) |
2018 | PROTOTYPE: $ |
2107 | PROTOTYPE: $ |
2019 | ALIAS: |
2108 | ALIAS: |
2020 | swap_defav = 1 |
2109 | swap_defav = 1 |
2021 | CODE: |
2110 | CODE: |
… | |
… | |
2176 | sv_setsv (cb, &PL_sv_undef); |
2265 | sv_setsv (cb, &PL_sv_undef); |
2177 | |
2266 | |
2178 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2267 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2179 | coro->saved_deffh = 0; |
2268 | coro->saved_deffh = 0; |
2180 | |
2269 | |
2181 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
2270 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
2182 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
2271 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
2183 | { |
2272 | { |
2184 | SV *old = PL_diehook; |
2273 | SV *old = PL_diehook; |
2185 | PL_diehook = 0; |
2274 | PL_diehook = 0; |
2186 | SvREFCNT_dec (old); |
2275 | SvREFCNT_dec (old); |