… | |
… | |
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) \ |
… | |
… | |
142 | #define NOINLINE attribute ((noinline)) |
142 | #define NOINLINE attribute ((noinline)) |
143 | |
143 | |
144 | #include "CoroAPI.h" |
144 | #include "CoroAPI.h" |
145 | |
145 | |
146 | #ifdef USE_ITHREADS |
146 | #ifdef USE_ITHREADS |
|
|
147 | |
147 | static perl_mutex coro_lock; |
148 | static perl_mutex coro_lock; |
148 | # define LOCK do { MUTEX_LOCK (&coro_lock); } while (0) |
149 | # define LOCK do { MUTEX_LOCK (&coro_lock); } while (0) |
149 | # define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0) |
150 | # define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0) |
|
|
151 | # if CORO_PTHREAD |
|
|
152 | static void *coro_thx; |
|
|
153 | # endif |
|
|
154 | |
150 | #else |
155 | #else |
|
|
156 | |
151 | # define LOCK (void)0 |
157 | # define LOCK (void)0 |
152 | # define UNLOCK (void)0 |
158 | # define UNLOCK (void)0 |
|
|
159 | |
153 | #endif |
160 | #endif |
|
|
161 | |
|
|
162 | # undef LOCK |
|
|
163 | # define LOCK (void)0 |
|
|
164 | # undef UNLOCK |
|
|
165 | # define UNLOCK (void)0 |
154 | |
166 | |
155 | /* helper storage struct for Coro::AIO */ |
167 | /* helper storage struct for Coro::AIO */ |
156 | struct io_state |
168 | struct io_state |
157 | { |
169 | { |
158 | AV *res; |
170 | AV *res; |
159 | int errorno; |
171 | int errorno; |
160 | I32 laststype; |
172 | I32 laststype; /* U16 in 5.10.0 */ |
161 | int laststatval; |
173 | int laststatval; |
162 | Stat_t statcache; |
174 | Stat_t statcache; |
163 | }; |
175 | }; |
164 | |
176 | |
165 | static double (*nvtime)(); /* so why doesn't it take void? */ |
177 | static double (*nvtime)(); /* so why doesn't it take void? */ |
166 | |
178 | |
|
|
179 | static U32 cctx_gen; |
167 | static size_t coro_stacksize = CORO_STACKSIZE; |
180 | static size_t cctx_stacksize = CORO_STACKSIZE; |
168 | static struct CoroAPI coroapi; |
181 | static struct CoroAPI coroapi; |
169 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
182 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
170 | static JMPENV *main_top_env; |
183 | static JMPENV *main_top_env; |
171 | static HV *coro_state_stash, *coro_stash; |
184 | static HV *coro_state_stash, *coro_stash; |
172 | static volatile SV *coro_mortal; /* will be freed/thrown 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 | }; |
173 | |
192 | |
174 | static GV *irsgv; /* $/ */ |
193 | static GV *irsgv; /* $/ */ |
175 | static GV *stdoutgv; /* *STDOUT */ |
194 | static GV *stdoutgv; /* *STDOUT */ |
176 | static SV *rv_diehook; |
195 | static SV *rv_diehook; |
177 | static SV *rv_warnhook; |
196 | static SV *rv_warnhook; |
… | |
… | |
209 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
228 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
210 | 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 */ |
211 | JMPENV *top_env; |
230 | JMPENV *top_env; |
212 | coro_context cctx; |
231 | coro_context cctx; |
213 | |
232 | |
|
|
233 | U32 gen; |
214 | #if CORO_USE_VALGRIND |
234 | #if CORO_USE_VALGRIND |
215 | int valgrind_id; |
235 | int valgrind_id; |
216 | #endif |
236 | #endif |
217 | unsigned char flags; |
237 | unsigned char flags; |
218 | } coro_cctx; |
238 | } coro_cctx; |
… | |
… | |
278 | #define PRIO_MIN -4 |
298 | #define PRIO_MIN -4 |
279 | |
299 | |
280 | /* for Coro.pm */ |
300 | /* for Coro.pm */ |
281 | static SV *coro_current; |
301 | static SV *coro_current; |
282 | static SV *coro_readyhook; |
302 | static SV *coro_readyhook; |
283 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
303 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
284 | static int coro_nready; |
304 | static int coro_nready; |
285 | static struct coro *coro_first; |
305 | static struct coro *coro_first; |
286 | |
306 | |
287 | /** lowlevel stuff **********************************************************/ |
307 | /** lowlevel stuff **********************************************************/ |
288 | |
308 | |
… | |
… | |
820 | PL_rs = newSVsv (GvSV (irsgv)); |
840 | PL_rs = newSVsv (GvSV (irsgv)); |
821 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
841 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
822 | |
842 | |
823 | { |
843 | { |
824 | dSP; |
844 | dSP; |
825 | LOGOP myop; |
845 | UNOP myop; |
826 | |
846 | |
827 | Zero (&myop, 1, LOGOP); |
847 | Zero (&myop, 1, UNOP); |
828 | myop.op_next = Nullop; |
848 | myop.op_next = Nullop; |
829 | myop.op_flags = OPf_WANT_VOID; |
849 | myop.op_flags = OPf_WANT_VOID; |
830 | |
850 | |
831 | PUSHMARK (SP); |
851 | PUSHMARK (SP); |
832 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
852 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
… | |
… | |
836 | SPAGAIN; |
856 | SPAGAIN; |
837 | } |
857 | } |
838 | |
858 | |
839 | /* 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 |
840 | * likely was suspended in set_stacklevel, called from entersub. |
860 | * likely was suspended in set_stacklevel, called from entersub. |
841 | * 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, |
842 | * so we ENTER here for symmetry |
862 | * so we ENTER here for symmetry. |
843 | */ |
863 | */ |
844 | ENTER; |
864 | ENTER; |
845 | } |
865 | } |
846 | |
866 | |
847 | static void |
867 | static void |
… | |
… | |
1012 | |
1032 | |
1013 | TAINT_NOT; |
1033 | TAINT_NOT; |
1014 | return 0; |
1034 | return 0; |
1015 | } |
1035 | } |
1016 | |
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 | |
1017 | /* 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 */ |
1018 | /* _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 */ |
1019 | /* during execution of a perl program */ |
1046 | /* during execution of a perl program */ |
|
|
1047 | /* also initialises PL_top_env */ |
1020 | static void NOINLINE |
1048 | static void NOINLINE |
1021 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1049 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1022 | { |
1050 | { |
1023 | dSP; |
1051 | dSP; |
1024 | LOGOP myop; |
1052 | UNOP myop; |
1025 | |
1053 | |
1026 | PL_top_env = &PL_start_env; |
1054 | PL_top_env = &PL_start_env; |
1027 | |
1055 | |
1028 | if (cctx->flags & CC_TRACE) |
1056 | if (cctx->flags & CC_TRACE) |
1029 | PL_runops = runops_trace; |
1057 | PL_runops = runops_trace; |
1030 | |
1058 | |
1031 | Zero (&myop, 1, LOGOP); |
1059 | Zero (&myop, 1, UNOP); |
1032 | myop.op_next = PL_op; |
1060 | myop.op_next = PL_op; |
1033 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
1061 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
1034 | |
1062 | |
1035 | PUSHMARK (SP); |
1063 | PUSHMARK (SP); |
1036 | EXTEND (SP, 2); |
1064 | EXTEND (SP, 2); |
1037 | PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); |
1065 | PUSHs (sv_2mortal (newSViv ((IV)cctx))); |
1038 | PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
1066 | PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
1039 | PUTBACK; |
1067 | PUTBACK; |
1040 | PL_op = (OP *)&myop; |
1068 | PL_op = (OP *)&myop; |
1041 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
1069 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
1042 | SPAGAIN; |
1070 | SPAGAIN; |
1043 | } |
1071 | } |
1044 | |
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 | |
1045 | /* |
1094 | /* |
1046 | * this is a _very_ stripped down perl interpreter ;) |
1095 | * this is a _very_ stripped down perl interpreter ;) |
1047 | */ |
1096 | */ |
1048 | static void |
1097 | static void |
1049 | cctx_run (void *arg) |
1098 | cctx_run (void *arg) |
1050 | { |
1099 | { |
|
|
1100 | #ifdef USE_ITHREADS |
|
|
1101 | # if CORO_PTHREAD |
|
|
1102 | PERL_SET_CONTEXT (coro_thx); |
|
|
1103 | # endif |
|
|
1104 | #endif |
|
|
1105 | { |
1051 | dTHX; |
1106 | dTHX; |
1052 | |
1107 | |
1053 | /* cctx_run is the alternative tail of transfer(), so unlock here. */ |
1108 | /* entersub called ENTER, but we never 'returned', undo that here */ |
1054 | UNLOCK; |
1109 | LEAVE; |
1055 | |
1110 | |
1056 | /* we now skip the entersub that lead to transfer() */ |
1111 | /* we now skip the entersub that did lead to transfer() */ |
1057 | PL_op = PL_op->op_next; |
1112 | PL_op = PL_op->op_next; |
1058 | |
1113 | |
1059 | /* inject a fake subroutine call to cctx_init */ |
1114 | /* inject a fake subroutine call to cctx_init */ |
1060 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1115 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1061 | |
1116 | |
|
|
1117 | /* cctx_run is the alternative tail of transfer() */ |
|
|
1118 | transfer_tail (aTHX); |
|
|
1119 | |
1062 | /* 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 */ |
1063 | PL_restartop = PL_op; |
1121 | PL_restartop = PL_op; |
1064 | perl_run (PL_curinterp); |
1122 | perl_run (PL_curinterp); |
1065 | |
1123 | |
1066 | /* |
1124 | /* |
1067 | * 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 |
1068 | * 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) |
1069 | * 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 |
1070 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1128 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1071 | * coroutine as Coro has no such concept |
1129 | * coroutine as Coro has no such concept |
1072 | */ |
1130 | */ |
1073 | PL_top_env = main_top_env; |
1131 | PL_top_env = main_top_env; |
1074 | 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 | } |
1075 | } |
1134 | } |
1076 | |
1135 | |
1077 | static coro_cctx * |
1136 | static coro_cctx * |
1078 | cctx_new () |
1137 | cctx_new () |
1079 | { |
1138 | { |
1080 | 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 (); |
1081 | void *stack_start; |
1168 | void *stack_start; |
1082 | size_t stack_size; |
1169 | size_t stack_size; |
1083 | |
1170 | |
1084 | ++cctx_count; |
|
|
1085 | Newz (0, cctx, 1, coro_cctx); |
|
|
1086 | |
|
|
1087 | #if HAVE_MMAP |
1171 | #if HAVE_MMAP |
1088 | 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; |
1089 | /* mmap supposedly does allocate-on-write for us */ |
1173 | /* mmap supposedly does allocate-on-write for us */ |
1090 | 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); |
1091 | |
1175 | |
1092 | if (cctx->sptr != (void *)-1) |
1176 | if (cctx->sptr != (void *)-1) |
1093 | { |
1177 | { |
… | |
… | |
1099 | cctx->flags |= CC_MAPPED; |
1183 | cctx->flags |= CC_MAPPED; |
1100 | } |
1184 | } |
1101 | else |
1185 | else |
1102 | #endif |
1186 | #endif |
1103 | { |
1187 | { |
1104 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
1188 | cctx->ssize = cctx_stacksize * (long)sizeof (long); |
1105 | New (0, cctx->sptr, coro_stacksize, long); |
1189 | New (0, cctx->sptr, cctx_stacksize, long); |
1106 | |
1190 | |
1107 | if (!cctx->sptr) |
1191 | if (!cctx->sptr) |
1108 | { |
1192 | { |
1109 | perror ("FATAL: unable to allocate stack for coroutine"); |
1193 | perror ("FATAL: unable to allocate stack for coroutine"); |
1110 | _exit (EXIT_FAILURE); |
1194 | _exit (EXIT_FAILURE); |
… | |
… | |
1146 | |
1230 | |
1147 | Safefree (cctx); |
1231 | Safefree (cctx); |
1148 | } |
1232 | } |
1149 | |
1233 | |
1150 | /* wether this cctx should be destructed */ |
1234 | /* wether this cctx should be destructed */ |
1151 | #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)) |
1152 | |
1236 | |
1153 | static coro_cctx * |
1237 | static coro_cctx * |
1154 | cctx_get (pTHX) |
1238 | cctx_get (pTHX) |
1155 | { |
1239 | { |
1156 | while (expect_true (cctx_first)) |
1240 | while (expect_true (cctx_first)) |
… | |
… | |
1163 | return cctx; |
1247 | return cctx; |
1164 | |
1248 | |
1165 | cctx_destroy (cctx); |
1249 | cctx_destroy (cctx); |
1166 | } |
1250 | } |
1167 | |
1251 | |
1168 | return cctx_new (); |
1252 | return cctx_new_run (); |
1169 | } |
1253 | } |
1170 | |
1254 | |
1171 | static void |
1255 | static void |
1172 | cctx_put (coro_cctx *cctx) |
1256 | cctx_put (coro_cctx *cctx) |
1173 | { |
1257 | { |
1174 | assert (("cctx_put called on non-initialised cctx", cctx->sptr)); |
1258 | assert (("cctx_put called on non-initialised cctx", cctx->sptr)); |
1175 | |
1259 | |
1176 | /* free another cctx if overlimit */ |
1260 | /* free another cctx if overlimit */ |
1177 | if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) |
1261 | if (expect_false (cctx_idle >= cctx_max_idle)) |
1178 | { |
1262 | { |
1179 | coro_cctx *first = cctx_first; |
1263 | coro_cctx *first = cctx_first; |
1180 | cctx_first = first->next; |
1264 | cctx_first = first->next; |
1181 | --cctx_idle; |
1265 | --cctx_idle; |
1182 | |
1266 | |
… | |
… | |
1223 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1307 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1224 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1308 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1225 | } |
1309 | } |
1226 | else if (expect_true (prev != next)) |
1310 | else if (expect_true (prev != next)) |
1227 | { |
1311 | { |
1228 | static volatile int has_throw; |
|
|
1229 | coro_cctx *prev__cctx; |
1312 | coro_cctx *prev__cctx; |
1230 | |
1313 | |
1231 | if (expect_false (prev->flags & CF_NEW)) |
1314 | if (expect_false (prev->flags & CF_NEW)) |
1232 | { |
1315 | { |
1233 | /* create a new empty/source context */ |
1316 | /* create a new empty/source context */ |
1234 | ++cctx_count; |
1317 | prev->cctx = cctx_new_empty (); |
1235 | New (0, prev->cctx, 1, coro_cctx); |
|
|
1236 | prev->cctx->sptr = 0; |
|
|
1237 | coro_create (&prev->cctx->cctx, 0, 0, 0, 0); |
|
|
1238 | |
|
|
1239 | prev->flags &= ~CF_NEW; |
1318 | prev->flags &= ~CF_NEW; |
1240 | prev->flags |= CF_RUNNING; |
1319 | prev->flags |= CF_RUNNING; |
1241 | } |
1320 | } |
1242 | |
1321 | |
1243 | prev->flags &= ~CF_RUNNING; |
1322 | prev->flags &= ~CF_RUNNING; |
… | |
… | |
1258 | else |
1337 | else |
1259 | load_perl (aTHX_ next); |
1338 | load_perl (aTHX_ next); |
1260 | |
1339 | |
1261 | prev__cctx = prev->cctx; |
1340 | prev__cctx = prev->cctx; |
1262 | |
1341 | |
1263 | /* possibly "free" the cctx */ |
1342 | /* possibly untie and reuse the cctx */ |
1264 | if (expect_true ( |
1343 | if (expect_true ( |
1265 | prev__cctx->idle_sp == STACKLEVEL |
1344 | prev__cctx->idle_sp == STACKLEVEL |
1266 | && !(prev__cctx->flags & CC_TRACE) |
1345 | && !(prev__cctx->flags & CC_TRACE) |
1267 | && !force_cctx |
1346 | && !force_cctx |
1268 | )) |
1347 | )) |
… | |
… | |
1284 | ++next->usecount; |
1363 | ++next->usecount; |
1285 | |
1364 | |
1286 | if (expect_true (!next->cctx)) |
1365 | if (expect_true (!next->cctx)) |
1287 | next->cctx = cctx_get (aTHX); |
1366 | next->cctx = cctx_get (aTHX); |
1288 | |
1367 | |
1289 | has_throw = !!next->throw; |
1368 | assert (("FATAL ERROR: internal error 1352 in Coro, please report", !transfer_next));//D |
|
|
1369 | transfer_next = next; |
1290 | |
1370 | |
1291 | if (expect_false (prev__cctx != next->cctx)) |
1371 | if (expect_false (prev__cctx != next->cctx)) |
1292 | { |
1372 | { |
1293 | prev__cctx->top_env = PL_top_env; |
1373 | prev__cctx->top_env = PL_top_env; |
1294 | PL_top_env = next->cctx->top_env; |
1374 | PL_top_env = next->cctx->top_env; |
1295 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1375 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1296 | } |
1376 | } |
1297 | |
1377 | |
1298 | free_coro_mortal (aTHX); |
1378 | transfer_tail (aTHX); |
1299 | UNLOCK; |
|
|
1300 | |
|
|
1301 | if (expect_false (has_throw)) |
|
|
1302 | { |
|
|
1303 | struct coro *coro = SvSTATE (coro_current); |
|
|
1304 | |
|
|
1305 | if (coro->throw) |
|
|
1306 | { |
|
|
1307 | SV *exception = coro->throw; |
|
|
1308 | coro->throw = 0; |
|
|
1309 | sv_setsv (ERRSV, exception); |
|
|
1310 | croak (0); |
|
|
1311 | } |
|
|
1312 | } |
|
|
1313 | } |
1379 | } |
1314 | } |
1380 | } |
1315 | |
|
|
1316 | struct transfer_args |
|
|
1317 | { |
|
|
1318 | struct coro *prev, *next; |
|
|
1319 | }; |
|
|
1320 | |
1381 | |
1321 | #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)) |
1322 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
1383 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
1323 | |
1384 | |
1324 | /** high level stuff ********************************************************/ |
1385 | /** high level stuff ********************************************************/ |
… | |
… | |
1536 | /* cannot transfer to destroyed coros, skip and look for next */ |
1597 | /* cannot transfer to destroyed coros, skip and look for next */ |
1537 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
1598 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
1538 | { |
1599 | { |
1539 | UNLOCK; |
1600 | UNLOCK; |
1540 | SvREFCNT_dec (next_sv); |
1601 | SvREFCNT_dec (next_sv); |
1541 | /* coro_nready is already taken care of by destroy */ |
1602 | /* coro_nready has already been taken care of by destroy */ |
1542 | continue; |
1603 | continue; |
1543 | } |
1604 | } |
1544 | |
1605 | |
1545 | --coro_nready; |
1606 | --coro_nready; |
1546 | UNLOCK; |
1607 | UNLOCK; |
… | |
… | |
1631 | struct coro *coro = SvSTATE (coro_sv); |
1692 | struct coro *coro = SvSTATE (coro_sv); |
1632 | |
1693 | |
1633 | if (flags & CC_TRACE) |
1694 | if (flags & CC_TRACE) |
1634 | { |
1695 | { |
1635 | if (!coro->cctx) |
1696 | if (!coro->cctx) |
1636 | coro->cctx = cctx_new (); |
1697 | coro->cctx = cctx_new_run (); |
1637 | else if (!(coro->cctx->flags & CC_TRACE)) |
1698 | else if (!(coro->cctx->flags & CC_TRACE)) |
1638 | croak ("cannot enable tracing on coroutine with custom stack"); |
1699 | croak ("cannot enable tracing on coroutine with custom stack"); |
1639 | |
1700 | |
1640 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1701 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1641 | } |
1702 | } |
… | |
… | |
1648 | else |
1709 | else |
1649 | coro->slot->runops = RUNOPS_DEFAULT; |
1710 | coro->slot->runops = RUNOPS_DEFAULT; |
1650 | } |
1711 | } |
1651 | } |
1712 | } |
1652 | |
1713 | |
|
|
1714 | #if 0 |
1653 | static int |
1715 | static int |
1654 | coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) |
1716 | coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) |
1655 | { |
1717 | { |
1656 | AV *padlist; |
1718 | AV *padlist; |
1657 | AV *av = (AV *)mg->mg_obj; |
1719 | AV *av = (AV *)mg->mg_obj; |
… | |
… | |
1663 | |
1725 | |
1664 | static MGVTBL coro_gensub_vtbl = { |
1726 | static MGVTBL coro_gensub_vtbl = { |
1665 | 0, 0, 0, 0, |
1727 | 0, 0, 0, 0, |
1666 | coro_gensub_free |
1728 | coro_gensub_free |
1667 | }; |
1729 | }; |
|
|
1730 | #endif |
1668 | |
1731 | |
1669 | /*****************************************************************************/ |
1732 | /*****************************************************************************/ |
1670 | /* PerlIO::cede */ |
1733 | /* PerlIO::cede */ |
1671 | |
1734 | |
1672 | typedef struct |
1735 | typedef struct |
… | |
… | |
1748 | |
1811 | |
1749 | BOOT: |
1812 | BOOT: |
1750 | { |
1813 | { |
1751 | #ifdef USE_ITHREADS |
1814 | #ifdef USE_ITHREADS |
1752 | MUTEX_INIT (&coro_lock); |
1815 | MUTEX_INIT (&coro_lock); |
|
|
1816 | # if CORO_PTHREAD |
|
|
1817 | coro_thx = PERL_GET_CONTEXT; |
|
|
1818 | # endif |
1753 | #endif |
1819 | #endif |
1754 | BOOT_PAGESIZE; |
1820 | BOOT_PAGESIZE; |
1755 | |
1821 | |
1756 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1822 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1757 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1823 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
… | |
… | |
1838 | |
1904 | |
1839 | PUTBACK; |
1905 | PUTBACK; |
1840 | switch (ix) |
1906 | switch (ix) |
1841 | { |
1907 | { |
1842 | case 0: |
1908 | case 0: |
1843 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1909 | prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (ST (0))); |
1844 | ta.next = 0; |
|
|
1845 | break; |
1910 | break; |
1846 | |
1911 | |
1847 | case 1: |
1912 | case 1: |
1848 | if (items != 2) |
1913 | if (items != 2) |
1849 | 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); |
… | |
… | |
1888 | _exit (code); |
1953 | _exit (code); |
1889 | |
1954 | |
1890 | int |
1955 | int |
1891 | cctx_stacksize (int new_stacksize = 0) |
1956 | cctx_stacksize (int new_stacksize = 0) |
1892 | CODE: |
1957 | CODE: |
1893 | RETVAL = coro_stacksize; |
1958 | RETVAL = cctx_stacksize; |
1894 | if (new_stacksize) |
1959 | if (new_stacksize) |
|
|
1960 | { |
1895 | 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; |
1896 | OUTPUT: |
1973 | OUTPUT: |
1897 | RETVAL |
1974 | RETVAL |
1898 | |
1975 | |
1899 | int |
1976 | int |
1900 | cctx_count () |
1977 | cctx_count () |
… | |
… | |
1978 | RETVAL = boolSV (coro->flags & ix); |
2055 | RETVAL = boolSV (coro->flags & ix); |
1979 | OUTPUT: |
2056 | OUTPUT: |
1980 | RETVAL |
2057 | RETVAL |
1981 | |
2058 | |
1982 | 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 |
1983 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
2067 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
1984 | |
2068 | |
1985 | SV * |
2069 | SV * |
1986 | has_cctx (Coro::State coro) |
2070 | has_cctx (Coro::State coro) |
1987 | PROTOTYPE: $ |
2071 | PROTOTYPE: $ |
… | |
… | |
1996 | CODE: |
2080 | CODE: |
1997 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
2081 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
1998 | OUTPUT: |
2082 | OUTPUT: |
1999 | RETVAL |
2083 | RETVAL |
2000 | |
2084 | |
2001 | IV |
2085 | UV |
2002 | rss (Coro::State coro) |
2086 | rss (Coro::State coro) |
2003 | PROTOTYPE: $ |
2087 | PROTOTYPE: $ |
2004 | ALIAS: |
2088 | ALIAS: |
2005 | usecount = 1 |
2089 | usecount = 1 |
2006 | CODE: |
2090 | CODE: |
… | |
… | |
2129 | CODE: |
2213 | CODE: |
2130 | RETVAL = coro_nready; |
2214 | RETVAL = coro_nready; |
2131 | OUTPUT: |
2215 | OUTPUT: |
2132 | RETVAL |
2216 | RETVAL |
2133 | |
2217 | |
2134 | void |
|
|
2135 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
|
|
2136 | PROTOTYPE: $;$ |
|
|
2137 | CODE: |
|
|
2138 | SvREFCNT_dec (self->throw); |
|
|
2139 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
2140 | |
|
|
2141 | # for async_pool speedup |
2218 | # for async_pool speedup |
2142 | void |
2219 | void |
2143 | _pool_1 (SV *cb) |
2220 | _pool_1 (SV *cb) |
2144 | CODE: |
2221 | CODE: |
2145 | { |
2222 | { |
… | |
… | |
2188 | sv_setsv (cb, &PL_sv_undef); |
2265 | sv_setsv (cb, &PL_sv_undef); |
2189 | |
2266 | |
2190 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2267 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2191 | coro->saved_deffh = 0; |
2268 | coro->saved_deffh = 0; |
2192 | |
2269 | |
2193 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
2270 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
2194 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
2271 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
2195 | { |
2272 | { |
2196 | SV *old = PL_diehook; |
2273 | SV *old = PL_diehook; |
2197 | PL_diehook = 0; |
2274 | PL_diehook = 0; |
2198 | SvREFCNT_dec (old); |
2275 | SvREFCNT_dec (old); |