… | |
… | |
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_mutex; |
148 | static perl_mutex coro_lock; |
148 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
149 | # define LOCK do { MUTEX_LOCK (&coro_lock); } while (0) |
149 | # 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 | |
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 after next transfer */ |
185 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
|
|
186 | static volatile char next_has_throw; /* speedup flag for next->throw check */ |
173 | |
187 | |
174 | static GV *irsgv; /* $/ */ |
188 | static GV *irsgv; /* $/ */ |
175 | static GV *stdoutgv; /* *STDOUT */ |
189 | static GV *stdoutgv; /* *STDOUT */ |
176 | static SV *rv_diehook; |
190 | static SV *rv_diehook; |
177 | static SV *rv_warnhook; |
191 | static SV *rv_warnhook; |
… | |
… | |
209 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
223 | 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 */ |
224 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
211 | JMPENV *top_env; |
225 | JMPENV *top_env; |
212 | coro_context cctx; |
226 | coro_context cctx; |
213 | |
227 | |
|
|
228 | U32 gen; |
214 | #if CORO_USE_VALGRIND |
229 | #if CORO_USE_VALGRIND |
215 | int valgrind_id; |
230 | int valgrind_id; |
216 | #endif |
231 | #endif |
217 | unsigned char flags; |
232 | unsigned char flags; |
218 | } coro_cctx; |
233 | } coro_cctx; |
… | |
… | |
278 | #define PRIO_MIN -4 |
293 | #define PRIO_MIN -4 |
279 | |
294 | |
280 | /* for Coro.pm */ |
295 | /* for Coro.pm */ |
281 | static SV *coro_current; |
296 | static SV *coro_current; |
282 | static SV *coro_readyhook; |
297 | static SV *coro_readyhook; |
283 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
298 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
284 | static int coro_nready; |
299 | static int coro_nready; |
285 | static struct coro *coro_first; |
300 | static struct coro *coro_first; |
286 | |
301 | |
287 | /** lowlevel stuff **********************************************************/ |
302 | /** lowlevel stuff **********************************************************/ |
288 | |
303 | |
… | |
… | |
383 | static MGVTBL coro_cv_vtbl = { |
398 | static MGVTBL coro_cv_vtbl = { |
384 | 0, 0, 0, 0, |
399 | 0, 0, 0, 0, |
385 | coro_cv_free |
400 | coro_cv_free |
386 | }; |
401 | }; |
387 | |
402 | |
388 | #define CORO_MAGIC(sv,type) \ |
403 | #define CORO_MAGIC(sv, type) \ |
389 | SvMAGIC (sv) \ |
404 | SvMAGIC (sv) \ |
390 | ? SvMAGIC (sv)->mg_type == type \ |
405 | ? SvMAGIC (sv)->mg_type == type \ |
391 | ? SvMAGIC (sv) \ |
406 | ? SvMAGIC (sv) \ |
392 | : mg_find (sv, type) \ |
407 | : mg_find (sv, type) \ |
393 | : 0 |
408 | : 0 |
394 | |
409 | |
395 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
410 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
396 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
411 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
397 | |
412 | |
398 | static struct coro * |
413 | static struct coro * |
… | |
… | |
628 | |
643 | |
629 | /* |
644 | /* |
630 | * destroy the stacks, the callchain etc... |
645 | * destroy the stacks, the callchain etc... |
631 | */ |
646 | */ |
632 | static void |
647 | static void |
633 | coro_destroy_stacks (pTHX) |
648 | coro_destruct_stacks (pTHX) |
634 | { |
649 | { |
635 | while (PL_curstackinfo->si_next) |
650 | while (PL_curstackinfo->si_next) |
636 | PL_curstackinfo = PL_curstackinfo->si_next; |
651 | PL_curstackinfo = PL_curstackinfo->si_next; |
637 | |
652 | |
638 | while (PL_curstackinfo) |
653 | while (PL_curstackinfo) |
… | |
… | |
843 | */ |
858 | */ |
844 | ENTER; |
859 | ENTER; |
845 | } |
860 | } |
846 | |
861 | |
847 | static void |
862 | static void |
848 | coro_destroy (pTHX_ struct coro *coro) |
863 | coro_destruct (pTHX_ struct coro *coro) |
849 | { |
864 | { |
850 | if (!IN_DESTRUCT) |
865 | if (!IN_DESTRUCT) |
851 | { |
866 | { |
852 | /* restore all saved variables and stuff */ |
867 | /* restore all saved variables and stuff */ |
853 | LEAVE_SCOPE (0); |
868 | LEAVE_SCOPE (0); |
… | |
… | |
875 | SvREFCNT_dec (PL_warnhook); |
890 | SvREFCNT_dec (PL_warnhook); |
876 | |
891 | |
877 | SvREFCNT_dec (coro->saved_deffh); |
892 | SvREFCNT_dec (coro->saved_deffh); |
878 | SvREFCNT_dec (coro->throw); |
893 | SvREFCNT_dec (coro->throw); |
879 | |
894 | |
880 | coro_destroy_stacks (aTHX); |
895 | coro_destruct_stacks (aTHX); |
881 | } |
896 | } |
882 | |
897 | |
883 | static void |
898 | static void |
884 | free_coro_mortal (pTHX) |
899 | free_coro_mortal (pTHX) |
885 | { |
900 | { |
… | |
… | |
1015 | } |
1030 | } |
1016 | |
1031 | |
1017 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
1032 | /* 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 */ |
1033 | /* _cctx_init should be careful, as it could be called at almost any time */ |
1019 | /* during execution of a perl program */ |
1034 | /* during execution of a perl program */ |
|
|
1035 | /* also initialises PL_top_env */ |
1020 | static void NOINLINE |
1036 | static void NOINLINE |
1021 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1037 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1022 | { |
1038 | { |
1023 | dSP; |
1039 | dSP; |
1024 | LOGOP myop; |
1040 | LOGOP myop; |
… | |
… | |
1040 | PL_op = (OP *)&myop; |
1056 | PL_op = (OP *)&myop; |
1041 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
1057 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
1042 | SPAGAIN; |
1058 | SPAGAIN; |
1043 | } |
1059 | } |
1044 | |
1060 | |
|
|
1061 | /* the tail of transfer: execute stuff we can onyl do afetr a transfer */ |
|
|
1062 | static void |
|
|
1063 | transfer_tail (void) |
|
|
1064 | { |
|
|
1065 | UNLOCK; |
|
|
1066 | |
|
|
1067 | if (expect_false (next_has_throw)) |
|
|
1068 | { |
|
|
1069 | struct coro *coro = SvSTATE (coro_current); |
|
|
1070 | |
|
|
1071 | if (coro->throw) |
|
|
1072 | { |
|
|
1073 | SV *exception = coro->throw; |
|
|
1074 | coro->throw = 0; |
|
|
1075 | sv_setsv (ERRSV, exception); |
|
|
1076 | croak (0); |
|
|
1077 | } |
|
|
1078 | } |
|
|
1079 | } |
|
|
1080 | |
1045 | /* |
1081 | /* |
1046 | * this is a _very_ stripped down perl interpreter ;) |
1082 | * this is a _very_ stripped down perl interpreter ;) |
1047 | */ |
1083 | */ |
1048 | static void |
1084 | static void |
1049 | cctx_run (void *arg) |
1085 | cctx_run (void *arg) |
1050 | { |
1086 | { |
|
|
1087 | #ifdef USE_ITHREADS |
|
|
1088 | # if CORO_PTHREAD |
|
|
1089 | PERL_SET_CONTEXT (coro_thx); |
|
|
1090 | # endif |
|
|
1091 | #endif |
|
|
1092 | { |
1051 | dTHX; |
1093 | dTHX; |
1052 | |
1094 | |
1053 | /* cctx_run is the alternative tail of transfer(), so unlock here. */ |
|
|
1054 | UNLOCK; |
|
|
1055 | |
|
|
1056 | /* we now skip the entersub that lead to transfer() */ |
1095 | /* we now skip the entersub that lead to transfer () */ |
1057 | PL_op = PL_op->op_next; |
1096 | PL_op = PL_op->op_next; |
1058 | |
1097 | |
1059 | /* inject a fake subroutine call to cctx_init */ |
1098 | /* inject a fake subroutine call to cctx_init */ |
1060 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1099 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1061 | |
1100 | |
|
|
1101 | /* cctx_run is the alternative tail of transfer () */ |
|
|
1102 | transfer_tail (); |
|
|
1103 | |
1062 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1104 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1063 | PL_restartop = PL_op; |
1105 | PL_restartop = PL_op; |
1064 | perl_run (PL_curinterp); |
1106 | perl_run (PL_curinterp); |
1065 | |
1107 | |
1066 | /* |
1108 | /* |
1067 | * If perl-run returns we assume exit() was being called or the coro |
1109 | * 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) |
1110 | * 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 |
1111 | * 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" |
1112 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1071 | * coroutine as Coro has no such concept |
1113 | * coroutine as Coro has no such concept |
1072 | */ |
1114 | */ |
1073 | PL_top_env = main_top_env; |
1115 | PL_top_env = main_top_env; |
1074 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1116 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
|
|
1117 | } |
1075 | } |
1118 | } |
1076 | |
1119 | |
1077 | static coro_cctx * |
1120 | static coro_cctx * |
1078 | cctx_new () |
1121 | cctx_new () |
1079 | { |
1122 | { |
1080 | coro_cctx *cctx; |
1123 | coro_cctx *cctx; |
|
|
1124 | |
|
|
1125 | ++cctx_count; |
|
|
1126 | New (0, cctx, 1, coro_cctx); |
|
|
1127 | |
|
|
1128 | cctx->gen = cctx_gen; |
|
|
1129 | cctx->flags = 0; |
|
|
1130 | cctx->idle_sp = 0; /* can be accessed by transfer between cctx_run and set_stacklevel */ |
|
|
1131 | |
|
|
1132 | return cctx; |
|
|
1133 | } |
|
|
1134 | |
|
|
1135 | /* create a new cctx only suitable as source */ |
|
|
1136 | static coro_cctx * |
|
|
1137 | cctx_new_empty () |
|
|
1138 | { |
|
|
1139 | coro_cctx *cctx = cctx_new (); |
|
|
1140 | |
|
|
1141 | cctx->sptr = 0; |
|
|
1142 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
|
|
1143 | |
|
|
1144 | return cctx; |
|
|
1145 | } |
|
|
1146 | |
|
|
1147 | /* create a new cctx suitable as destination/running a perl interpreter */ |
|
|
1148 | static coro_cctx * |
|
|
1149 | cctx_new_run () |
|
|
1150 | { |
|
|
1151 | coro_cctx *cctx = cctx_new (); |
1081 | void *stack_start; |
1152 | void *stack_start; |
1082 | size_t stack_size; |
1153 | size_t stack_size; |
1083 | |
1154 | |
1084 | ++cctx_count; |
|
|
1085 | |
|
|
1086 | Newz (0, cctx, 1, coro_cctx); |
|
|
1087 | |
|
|
1088 | #if HAVE_MMAP |
1155 | #if HAVE_MMAP |
1089 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1156 | cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1090 | /* mmap supposedly does allocate-on-write for us */ |
1157 | /* mmap supposedly does allocate-on-write for us */ |
1091 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
1158 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
1092 | |
1159 | |
1093 | if (cctx->sptr != (void *)-1) |
1160 | if (cctx->sptr != (void *)-1) |
1094 | { |
1161 | { |
… | |
… | |
1100 | cctx->flags |= CC_MAPPED; |
1167 | cctx->flags |= CC_MAPPED; |
1101 | } |
1168 | } |
1102 | else |
1169 | else |
1103 | #endif |
1170 | #endif |
1104 | { |
1171 | { |
1105 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
1172 | cctx->ssize = cctx_stacksize * (long)sizeof (long); |
1106 | New (0, cctx->sptr, coro_stacksize, long); |
1173 | New (0, cctx->sptr, cctx_stacksize, long); |
1107 | |
1174 | |
1108 | if (!cctx->sptr) |
1175 | if (!cctx->sptr) |
1109 | { |
1176 | { |
1110 | perror ("FATAL: unable to allocate stack for coroutine"); |
1177 | perror ("FATAL: unable to allocate stack for coroutine"); |
1111 | _exit (EXIT_FAILURE); |
1178 | _exit (EXIT_FAILURE); |
… | |
… | |
1126 | { |
1193 | { |
1127 | if (!cctx) |
1194 | if (!cctx) |
1128 | return; |
1195 | return; |
1129 | |
1196 | |
1130 | --cctx_count; |
1197 | --cctx_count; |
|
|
1198 | coro_destroy (&cctx->cctx); |
1131 | |
1199 | |
|
|
1200 | /* coro_transfer creates new, empty cctx's */ |
|
|
1201 | if (cctx->sptr) |
|
|
1202 | { |
1132 | #if CORO_USE_VALGRIND |
1203 | #if CORO_USE_VALGRIND |
1133 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1204 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1134 | #endif |
1205 | #endif |
1135 | |
1206 | |
1136 | #if HAVE_MMAP |
1207 | #if HAVE_MMAP |
1137 | if (cctx->flags & CC_MAPPED) |
1208 | if (cctx->flags & CC_MAPPED) |
1138 | munmap (cctx->sptr, cctx->ssize); |
1209 | munmap (cctx->sptr, cctx->ssize); |
1139 | else |
1210 | else |
1140 | #endif |
1211 | #endif |
1141 | Safefree (cctx->sptr); |
1212 | Safefree (cctx->sptr); |
|
|
1213 | } |
1142 | |
1214 | |
1143 | Safefree (cctx); |
1215 | Safefree (cctx); |
1144 | } |
1216 | } |
1145 | |
1217 | |
1146 | /* wether this cctx should be destructed */ |
1218 | /* wether this cctx should be destructed */ |
1147 | #define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) |
1219 | #define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE)) |
1148 | |
1220 | |
1149 | static coro_cctx * |
1221 | static coro_cctx * |
1150 | cctx_get (pTHX) |
1222 | cctx_get (pTHX) |
1151 | { |
1223 | { |
1152 | while (expect_true (cctx_first)) |
1224 | while (expect_true (cctx_first)) |
… | |
… | |
1159 | return cctx; |
1231 | return cctx; |
1160 | |
1232 | |
1161 | cctx_destroy (cctx); |
1233 | cctx_destroy (cctx); |
1162 | } |
1234 | } |
1163 | |
1235 | |
1164 | return cctx_new (); |
1236 | return cctx_new_run (); |
1165 | } |
1237 | } |
1166 | |
1238 | |
1167 | static void |
1239 | static void |
1168 | cctx_put (coro_cctx *cctx) |
1240 | cctx_put (coro_cctx *cctx) |
1169 | { |
1241 | { |
|
|
1242 | assert (("cctx_put called on non-initialised cctx", cctx->sptr)); |
|
|
1243 | |
1170 | /* free another cctx if overlimit */ |
1244 | /* free another cctx if overlimit */ |
1171 | if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) |
1245 | if (expect_false (cctx_idle >= cctx_max_idle)) |
1172 | { |
1246 | { |
1173 | coro_cctx *first = cctx_first; |
1247 | coro_cctx *first = cctx_first; |
1174 | cctx_first = first->next; |
1248 | cctx_first = first->next; |
1175 | --cctx_idle; |
1249 | --cctx_idle; |
1176 | |
1250 | |
… | |
… | |
1208 | /* always use the TRANSFER macro */ |
1282 | /* always use the TRANSFER macro */ |
1209 | static void NOINLINE |
1283 | static void NOINLINE |
1210 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1284 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1211 | { |
1285 | { |
1212 | dSTACKLEVEL; |
1286 | dSTACKLEVEL; |
1213 | static volatile int has_throw; |
|
|
1214 | |
1287 | |
1215 | /* sometimes transfer is only called to set idle_sp */ |
1288 | /* sometimes transfer is only called to set idle_sp */ |
1216 | if (expect_false (!next)) |
1289 | if (expect_false (!next)) |
1217 | { |
1290 | { |
1218 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1291 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
… | |
… | |
1222 | { |
1295 | { |
1223 | coro_cctx *prev__cctx; |
1296 | coro_cctx *prev__cctx; |
1224 | |
1297 | |
1225 | if (expect_false (prev->flags & CF_NEW)) |
1298 | if (expect_false (prev->flags & CF_NEW)) |
1226 | { |
1299 | { |
1227 | /* create a new empty context */ |
1300 | /* create a new empty/source context */ |
1228 | Newz (0, prev->cctx, 1, coro_cctx); |
1301 | prev->cctx = cctx_new_empty (); |
1229 | prev->flags &= ~CF_NEW; |
1302 | prev->flags &= ~CF_NEW; |
1230 | prev->flags |= CF_RUNNING; |
1303 | prev->flags |= CF_RUNNING; |
1231 | } |
1304 | } |
1232 | |
1305 | |
1233 | prev->flags &= ~CF_RUNNING; |
1306 | prev->flags &= ~CF_RUNNING; |
… | |
… | |
1248 | else |
1321 | else |
1249 | load_perl (aTHX_ next); |
1322 | load_perl (aTHX_ next); |
1250 | |
1323 | |
1251 | prev__cctx = prev->cctx; |
1324 | prev__cctx = prev->cctx; |
1252 | |
1325 | |
|
|
1326 | if (prev__cctx->idle_sp == STACKLEVEL) asm volatile("");//D |
|
|
1327 | |
1253 | /* possibly "free" the cctx */ |
1328 | /* possibly "free" the cctx */ |
1254 | if (expect_true ( |
1329 | if (expect_true ( |
1255 | prev__cctx->idle_sp == STACKLEVEL |
1330 | prev__cctx->idle_sp == STACKLEVEL |
1256 | && !(prev__cctx->flags & CC_TRACE) |
1331 | && !(prev__cctx->flags & CC_TRACE) |
1257 | && !force_cctx |
1332 | && !force_cctx |
… | |
… | |
1274 | ++next->usecount; |
1349 | ++next->usecount; |
1275 | |
1350 | |
1276 | if (expect_true (!next->cctx)) |
1351 | if (expect_true (!next->cctx)) |
1277 | next->cctx = cctx_get (aTHX); |
1352 | next->cctx = cctx_get (aTHX); |
1278 | |
1353 | |
1279 | has_throw = !!next->throw; |
1354 | next_has_throw = !!next->throw; |
1280 | |
1355 | |
1281 | if (expect_false (prev__cctx != next->cctx)) |
1356 | if (expect_false (prev__cctx != next->cctx)) |
1282 | { |
1357 | { |
1283 | prev__cctx->top_env = PL_top_env; |
1358 | prev__cctx->top_env = PL_top_env; |
1284 | PL_top_env = next->cctx->top_env; |
1359 | PL_top_env = next->cctx->top_env; |
… | |
… | |
1286 | } |
1361 | } |
1287 | |
1362 | |
1288 | free_coro_mortal (aTHX); |
1363 | free_coro_mortal (aTHX); |
1289 | UNLOCK; |
1364 | UNLOCK; |
1290 | |
1365 | |
1291 | if (expect_false (has_throw)) |
1366 | transfer_tail (); |
1292 | { |
|
|
1293 | struct coro *coro = SvSTATE (coro_current); |
|
|
1294 | |
|
|
1295 | if (coro->throw) |
|
|
1296 | { |
|
|
1297 | SV *exception = coro->throw; |
|
|
1298 | coro->throw = 0; |
|
|
1299 | sv_setsv (ERRSV, exception); |
|
|
1300 | croak (0); |
|
|
1301 | } |
|
|
1302 | } |
|
|
1303 | } |
1367 | } |
1304 | } |
1368 | } |
1305 | |
1369 | |
1306 | struct transfer_args |
1370 | struct transfer_args |
1307 | { |
1371 | { |
… | |
… | |
1340 | croak ("FATAL: tried to destroy currently running coroutine"); |
1404 | croak ("FATAL: tried to destroy currently running coroutine"); |
1341 | |
1405 | |
1342 | save_perl (aTHX_ &temp); |
1406 | save_perl (aTHX_ &temp); |
1343 | load_perl (aTHX_ coro); |
1407 | load_perl (aTHX_ coro); |
1344 | |
1408 | |
1345 | coro_destroy (aTHX_ coro); |
1409 | coro_destruct (aTHX_ coro); |
1346 | |
1410 | |
1347 | load_perl (aTHX_ &temp); |
1411 | load_perl (aTHX_ &temp); |
1348 | |
1412 | |
1349 | coro->slot = 0; |
1413 | coro->slot = 0; |
1350 | } |
1414 | } |
… | |
… | |
1621 | struct coro *coro = SvSTATE (coro_sv); |
1685 | struct coro *coro = SvSTATE (coro_sv); |
1622 | |
1686 | |
1623 | if (flags & CC_TRACE) |
1687 | if (flags & CC_TRACE) |
1624 | { |
1688 | { |
1625 | if (!coro->cctx) |
1689 | if (!coro->cctx) |
1626 | coro->cctx = cctx_new (); |
1690 | coro->cctx = cctx_new_run (); |
1627 | else if (!(coro->cctx->flags & CC_TRACE)) |
1691 | else if (!(coro->cctx->flags & CC_TRACE)) |
1628 | croak ("cannot enable tracing on coroutine with custom stack"); |
1692 | croak ("cannot enable tracing on coroutine with custom stack"); |
1629 | |
1693 | |
1630 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1694 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1631 | } |
1695 | } |
… | |
… | |
1638 | else |
1702 | else |
1639 | coro->slot->runops = RUNOPS_DEFAULT; |
1703 | coro->slot->runops = RUNOPS_DEFAULT; |
1640 | } |
1704 | } |
1641 | } |
1705 | } |
1642 | |
1706 | |
|
|
1707 | #if 0 |
1643 | static int |
1708 | static int |
1644 | coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) |
1709 | coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) |
1645 | { |
1710 | { |
1646 | AV *padlist; |
1711 | AV *padlist; |
1647 | AV *av = (AV *)mg->mg_obj; |
1712 | AV *av = (AV *)mg->mg_obj; |
… | |
… | |
1653 | |
1718 | |
1654 | static MGVTBL coro_gensub_vtbl = { |
1719 | static MGVTBL coro_gensub_vtbl = { |
1655 | 0, 0, 0, 0, |
1720 | 0, 0, 0, 0, |
1656 | coro_gensub_free |
1721 | coro_gensub_free |
1657 | }; |
1722 | }; |
|
|
1723 | #endif |
1658 | |
1724 | |
1659 | /*****************************************************************************/ |
1725 | /*****************************************************************************/ |
1660 | /* PerlIO::cede */ |
1726 | /* PerlIO::cede */ |
1661 | |
1727 | |
1662 | typedef struct |
1728 | typedef struct |
… | |
… | |
1737 | PROTOTYPES: DISABLE |
1803 | PROTOTYPES: DISABLE |
1738 | |
1804 | |
1739 | BOOT: |
1805 | BOOT: |
1740 | { |
1806 | { |
1741 | #ifdef USE_ITHREADS |
1807 | #ifdef USE_ITHREADS |
1742 | MUTEX_INIT (&coro_mutex); |
1808 | MUTEX_INIT (&coro_lock); |
|
|
1809 | # if CORO_PTHREAD |
|
|
1810 | coro_thx = PERL_GET_CONTEXT; |
|
|
1811 | # endif |
1743 | #endif |
1812 | #endif |
1744 | BOOT_PAGESIZE; |
1813 | BOOT_PAGESIZE; |
1745 | |
1814 | |
1746 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1815 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1747 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1816 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
… | |
… | |
1834 | ta.next = 0; |
1903 | ta.next = 0; |
1835 | break; |
1904 | break; |
1836 | |
1905 | |
1837 | case 1: |
1906 | case 1: |
1838 | if (items != 2) |
1907 | if (items != 2) |
1839 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1908 | croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items); |
1840 | |
1909 | |
1841 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1910 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1842 | break; |
1911 | break; |
1843 | |
1912 | |
1844 | case 2: |
1913 | case 2: |
… | |
… | |
1878 | _exit (code); |
1947 | _exit (code); |
1879 | |
1948 | |
1880 | int |
1949 | int |
1881 | cctx_stacksize (int new_stacksize = 0) |
1950 | cctx_stacksize (int new_stacksize = 0) |
1882 | CODE: |
1951 | CODE: |
1883 | RETVAL = coro_stacksize; |
1952 | RETVAL = cctx_stacksize; |
1884 | if (new_stacksize) |
1953 | if (new_stacksize) |
|
|
1954 | { |
1885 | coro_stacksize = new_stacksize; |
1955 | cctx_stacksize = new_stacksize; |
|
|
1956 | ++cctx_gen; |
|
|
1957 | } |
|
|
1958 | OUTPUT: |
|
|
1959 | RETVAL |
|
|
1960 | |
|
|
1961 | int |
|
|
1962 | cctx_max_idle (int max_idle = 0) |
|
|
1963 | CODE: |
|
|
1964 | RETVAL = cctx_max_idle; |
|
|
1965 | if (max_idle > 1) |
|
|
1966 | cctx_max_idle = max_idle; |
1886 | OUTPUT: |
1967 | OUTPUT: |
1887 | RETVAL |
1968 | RETVAL |
1888 | |
1969 | |
1889 | int |
1970 | int |
1890 | cctx_count () |
1971 | cctx_count () |
… | |
… | |
1986 | CODE: |
2067 | CODE: |
1987 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
2068 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
1988 | OUTPUT: |
2069 | OUTPUT: |
1989 | RETVAL |
2070 | RETVAL |
1990 | |
2071 | |
1991 | IV |
2072 | UV |
1992 | rss (Coro::State coro) |
2073 | rss (Coro::State coro) |
1993 | PROTOTYPE: $ |
2074 | PROTOTYPE: $ |
1994 | ALIAS: |
2075 | ALIAS: |
1995 | usecount = 1 |
2076 | usecount = 1 |
1996 | CODE: |
2077 | CODE: |
… | |
… | |
2007 | CODE: |
2088 | CODE: |
2008 | struct coro *coro = SvSTATE (coro_current); |
2089 | struct coro *coro = SvSTATE (coro_current); |
2009 | coro->cctx->idle_sp = 0; |
2090 | coro->cctx->idle_sp = 0; |
2010 | |
2091 | |
2011 | void |
2092 | void |
2012 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
|
|
2013 | PROTOTYPE: $;$ |
|
|
2014 | CODE: |
|
|
2015 | SvREFCNT_dec (self->throw); |
|
|
2016 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
2017 | |
|
|
2018 | void |
|
|
2019 | swap_defsv (Coro::State self) |
2093 | swap_defsv (Coro::State self) |
2020 | PROTOTYPE: $ |
2094 | PROTOTYPE: $ |
2021 | ALIAS: |
2095 | ALIAS: |
2022 | swap_defav = 1 |
2096 | swap_defav = 1 |
2023 | CODE: |
2097 | CODE: |
… | |
… | |
2126 | CODE: |
2200 | CODE: |
2127 | RETVAL = coro_nready; |
2201 | RETVAL = coro_nready; |
2128 | OUTPUT: |
2202 | OUTPUT: |
2129 | RETVAL |
2203 | RETVAL |
2130 | |
2204 | |
|
|
2205 | void |
|
|
2206 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
|
|
2207 | PROTOTYPE: $;$ |
|
|
2208 | CODE: |
|
|
2209 | SvREFCNT_dec (self->throw); |
|
|
2210 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
2211 | |
2131 | # for async_pool speedup |
2212 | # for async_pool speedup |
2132 | void |
2213 | void |
2133 | _pool_1 (SV *cb) |
2214 | _pool_1 (SV *cb) |
2134 | CODE: |
2215 | CODE: |
2135 | { |
2216 | { |
… | |
… | |
2178 | sv_setsv (cb, &PL_sv_undef); |
2259 | sv_setsv (cb, &PL_sv_undef); |
2179 | |
2260 | |
2180 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2261 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2181 | coro->saved_deffh = 0; |
2262 | coro->saved_deffh = 0; |
2182 | |
2263 | |
2183 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
2264 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
2184 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
2265 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
2185 | { |
2266 | { |
2186 | SV *old = PL_diehook; |
2267 | SV *old = PL_diehook; |
2187 | PL_diehook = 0; |
2268 | PL_diehook = 0; |
2188 | SvREFCNT_dec (old); |
2269 | SvREFCNT_dec (old); |