… | |
… | |
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 | |
|
|
94 | /* 5.11 */ |
|
|
95 | #ifndef CxHASARGS |
|
|
96 | # define CxHASARGS(cx) (cx)->blk_sub.hasargs |
|
|
97 | #endif |
102 | #endif |
98 | |
103 | |
99 | /* 5.8.7 */ |
104 | /* 5.8.7 */ |
100 | #ifndef SvRV_set |
105 | #ifndef SvRV_set |
101 | # define SvRV_set(s,v) SvRV(s) = (v) |
106 | # define SvRV_set(s,v) SvRV(s) = (v) |
… | |
… | |
137 | #define NOINLINE attribute ((noinline)) |
142 | #define NOINLINE attribute ((noinline)) |
138 | |
143 | |
139 | #include "CoroAPI.h" |
144 | #include "CoroAPI.h" |
140 | |
145 | |
141 | #ifdef USE_ITHREADS |
146 | #ifdef USE_ITHREADS |
|
|
147 | |
142 | static perl_mutex coro_mutex; |
148 | static perl_mutex coro_lock; |
143 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
149 | # define LOCK do { MUTEX_LOCK (&coro_lock); } while (0) |
144 | # 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 | |
145 | #else |
155 | #else |
|
|
156 | |
146 | # define LOCK (void)0 |
157 | # define LOCK (void)0 |
147 | # define UNLOCK (void)0 |
158 | # define UNLOCK (void)0 |
|
|
159 | |
148 | #endif |
160 | #endif |
|
|
161 | |
|
|
162 | # undef LOCK |
|
|
163 | # define LOCK (void)0 |
|
|
164 | # undef UNLOCK |
|
|
165 | # define UNLOCK (void)0 |
149 | |
166 | |
150 | /* helper storage struct for Coro::AIO */ |
167 | /* helper storage struct for Coro::AIO */ |
151 | struct io_state |
168 | struct io_state |
152 | { |
169 | { |
153 | AV *res; |
170 | AV *res; |
154 | int errorno; |
171 | int errorno; |
155 | I32 laststype; |
172 | I32 laststype; /* U16 in 5.10.0 */ |
156 | int laststatval; |
173 | int laststatval; |
157 | Stat_t statcache; |
174 | Stat_t statcache; |
158 | }; |
175 | }; |
159 | |
176 | |
160 | static double (*nvtime)(); /* so why doesn't it take void? */ |
177 | static double (*nvtime)(); /* so why doesn't it take void? */ |
161 | |
178 | |
|
|
179 | static U32 cctx_gen; |
162 | static size_t coro_stacksize = CORO_STACKSIZE; |
180 | static size_t cctx_stacksize = CORO_STACKSIZE; |
163 | static struct CoroAPI coroapi; |
181 | static struct CoroAPI coroapi; |
164 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
182 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
165 | static JMPENV *main_top_env; |
183 | static JMPENV *main_top_env; |
166 | static HV *coro_state_stash, *coro_stash; |
184 | static HV *coro_state_stash, *coro_stash; |
167 | static volatile SV *coro_mortal; /* will be freed after next transfer */ |
185 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
168 | |
186 | |
169 | static GV *irsgv; /* $/ */ |
187 | static GV *irsgv; /* $/ */ |
170 | static GV *stdoutgv; /* *STDOUT */ |
188 | static GV *stdoutgv; /* *STDOUT */ |
171 | static SV *rv_diehook; |
189 | static SV *rv_diehook; |
172 | static SV *rv_warnhook; |
190 | static SV *rv_warnhook; |
… | |
… | |
204 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
222 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
205 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
223 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
206 | JMPENV *top_env; |
224 | JMPENV *top_env; |
207 | coro_context cctx; |
225 | coro_context cctx; |
208 | |
226 | |
|
|
227 | U32 gen; |
209 | #if CORO_USE_VALGRIND |
228 | #if CORO_USE_VALGRIND |
210 | int valgrind_id; |
229 | int valgrind_id; |
211 | #endif |
230 | #endif |
212 | unsigned char flags; |
231 | unsigned char flags; |
213 | } coro_cctx; |
232 | } coro_cctx; |
… | |
… | |
273 | #define PRIO_MIN -4 |
292 | #define PRIO_MIN -4 |
274 | |
293 | |
275 | /* for Coro.pm */ |
294 | /* for Coro.pm */ |
276 | static SV *coro_current; |
295 | static SV *coro_current; |
277 | static SV *coro_readyhook; |
296 | static SV *coro_readyhook; |
278 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
297 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
279 | static int coro_nready; |
298 | static int coro_nready; |
280 | static struct coro *coro_first; |
299 | static struct coro *coro_first; |
281 | |
300 | |
282 | /** lowlevel stuff **********************************************************/ |
301 | /** lowlevel stuff **********************************************************/ |
283 | |
302 | |
… | |
… | |
378 | static MGVTBL coro_cv_vtbl = { |
397 | static MGVTBL coro_cv_vtbl = { |
379 | 0, 0, 0, 0, |
398 | 0, 0, 0, 0, |
380 | coro_cv_free |
399 | coro_cv_free |
381 | }; |
400 | }; |
382 | |
401 | |
383 | #define CORO_MAGIC(sv,type) \ |
402 | #define CORO_MAGIC(sv, type) \ |
384 | SvMAGIC (sv) \ |
403 | SvMAGIC (sv) \ |
385 | ? SvMAGIC (sv)->mg_type == type \ |
404 | ? SvMAGIC (sv)->mg_type == type \ |
386 | ? SvMAGIC (sv) \ |
405 | ? SvMAGIC (sv) \ |
387 | : mg_find (sv, type) \ |
406 | : mg_find (sv, type) \ |
388 | : 0 |
407 | : 0 |
389 | |
408 | |
390 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
409 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
391 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
410 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
392 | |
411 | |
393 | static struct coro * |
412 | static struct coro * |
… | |
… | |
623 | |
642 | |
624 | /* |
643 | /* |
625 | * destroy the stacks, the callchain etc... |
644 | * destroy the stacks, the callchain etc... |
626 | */ |
645 | */ |
627 | static void |
646 | static void |
628 | coro_destroy_stacks (pTHX) |
647 | coro_destruct_stacks (pTHX) |
629 | { |
648 | { |
630 | while (PL_curstackinfo->si_next) |
649 | while (PL_curstackinfo->si_next) |
631 | PL_curstackinfo = PL_curstackinfo->si_next; |
650 | PL_curstackinfo = PL_curstackinfo->si_next; |
632 | |
651 | |
633 | while (PL_curstackinfo) |
652 | while (PL_curstackinfo) |
… | |
… | |
838 | */ |
857 | */ |
839 | ENTER; |
858 | ENTER; |
840 | } |
859 | } |
841 | |
860 | |
842 | static void |
861 | static void |
843 | coro_destroy (pTHX_ struct coro *coro) |
862 | coro_destruct (pTHX_ struct coro *coro) |
844 | { |
863 | { |
845 | if (!IN_DESTRUCT) |
864 | if (!IN_DESTRUCT) |
846 | { |
865 | { |
847 | /* restore all saved variables and stuff */ |
866 | /* restore all saved variables and stuff */ |
848 | LEAVE_SCOPE (0); |
867 | LEAVE_SCOPE (0); |
… | |
… | |
870 | SvREFCNT_dec (PL_warnhook); |
889 | SvREFCNT_dec (PL_warnhook); |
871 | |
890 | |
872 | SvREFCNT_dec (coro->saved_deffh); |
891 | SvREFCNT_dec (coro->saved_deffh); |
873 | SvREFCNT_dec (coro->throw); |
892 | SvREFCNT_dec (coro->throw); |
874 | |
893 | |
875 | coro_destroy_stacks (aTHX); |
894 | coro_destruct_stacks (aTHX); |
876 | } |
895 | } |
877 | |
896 | |
878 | static void |
897 | static void |
879 | free_coro_mortal (pTHX) |
898 | free_coro_mortal (pTHX) |
880 | { |
899 | { |
… | |
… | |
1041 | * this is a _very_ stripped down perl interpreter ;) |
1060 | * this is a _very_ stripped down perl interpreter ;) |
1042 | */ |
1061 | */ |
1043 | static void |
1062 | static void |
1044 | cctx_run (void *arg) |
1063 | cctx_run (void *arg) |
1045 | { |
1064 | { |
|
|
1065 | #ifdef USE_ITHREADS |
|
|
1066 | # if CORO_PTHREAD |
|
|
1067 | PERL_SET_CONTEXT (coro_thx); |
|
|
1068 | # endif |
|
|
1069 | #endif |
|
|
1070 | { |
1046 | dTHX; |
1071 | dTHX; |
1047 | |
1072 | |
1048 | /* cctx_run is the alternative tail of transfer(), so unlock here. */ |
1073 | /* cctx_run is the alternative tail of transfer(), so unlock here. */ |
1049 | UNLOCK; |
1074 | UNLOCK; |
1050 | |
1075 | |
1051 | /* we now skip the entersub that lead to transfer() */ |
1076 | /* we now skip the entersub that lead to transfer() */ |
1052 | PL_op = PL_op->op_next; |
1077 | PL_op = PL_op->op_next; |
1053 | |
1078 | |
1054 | /* inject a fake subroutine call to cctx_init */ |
1079 | /* inject a fake subroutine call to cctx_init */ |
1055 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1080 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1056 | |
1081 | |
1057 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1082 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1058 | PL_restartop = PL_op; |
1083 | PL_restartop = PL_op; |
1059 | perl_run (PL_curinterp); |
1084 | perl_run (PL_curinterp); |
1060 | |
1085 | |
1061 | /* |
1086 | /* |
1062 | * If perl-run returns we assume exit() was being called or the coro |
1087 | * If perl-run returns we assume exit() was being called or the coro |
1063 | * fell off the end, which seems to be the only valid (non-bug) |
1088 | * fell off the end, which seems to be the only valid (non-bug) |
1064 | * reason for perl_run to return. We try to exit by jumping to the |
1089 | * reason for perl_run to return. We try to exit by jumping to the |
1065 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1090 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1066 | * coroutine as Coro has no such concept |
1091 | * coroutine as Coro has no such concept |
1067 | */ |
1092 | */ |
1068 | PL_top_env = main_top_env; |
1093 | PL_top_env = main_top_env; |
1069 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1094 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
|
|
1095 | } |
1070 | } |
1096 | } |
1071 | |
1097 | |
1072 | static coro_cctx * |
1098 | static coro_cctx * |
1073 | cctx_new () |
1099 | cctx_new () |
1074 | { |
1100 | { |
1075 | coro_cctx *cctx; |
1101 | coro_cctx *cctx; |
1076 | void *stack_start; |
1102 | void *stack_start; |
1077 | size_t stack_size; |
1103 | size_t stack_size; |
1078 | |
1104 | |
1079 | ++cctx_count; |
1105 | ++cctx_count; |
1080 | |
|
|
1081 | Newz (0, cctx, 1, coro_cctx); |
1106 | Newz (0, cctx, 1, coro_cctx); |
1082 | |
1107 | |
|
|
1108 | cctx->gen = cctx_gen; |
|
|
1109 | |
1083 | #if HAVE_MMAP |
1110 | #if HAVE_MMAP |
1084 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1111 | cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1085 | /* mmap supposedly does allocate-on-write for us */ |
1112 | /* mmap supposedly does allocate-on-write for us */ |
1086 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
1113 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
1087 | |
1114 | |
1088 | if (cctx->sptr != (void *)-1) |
1115 | if (cctx->sptr != (void *)-1) |
1089 | { |
1116 | { |
… | |
… | |
1095 | cctx->flags |= CC_MAPPED; |
1122 | cctx->flags |= CC_MAPPED; |
1096 | } |
1123 | } |
1097 | else |
1124 | else |
1098 | #endif |
1125 | #endif |
1099 | { |
1126 | { |
1100 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
1127 | cctx->ssize = cctx_stacksize * (long)sizeof (long); |
1101 | New (0, cctx->sptr, coro_stacksize, long); |
1128 | New (0, cctx->sptr, cctx_stacksize, long); |
1102 | |
1129 | |
1103 | if (!cctx->sptr) |
1130 | if (!cctx->sptr) |
1104 | { |
1131 | { |
1105 | perror ("FATAL: unable to allocate stack for coroutine"); |
1132 | perror ("FATAL: unable to allocate stack for coroutine"); |
1106 | _exit (EXIT_FAILURE); |
1133 | _exit (EXIT_FAILURE); |
… | |
… | |
1121 | { |
1148 | { |
1122 | if (!cctx) |
1149 | if (!cctx) |
1123 | return; |
1150 | return; |
1124 | |
1151 | |
1125 | --cctx_count; |
1152 | --cctx_count; |
|
|
1153 | coro_destroy (&cctx->cctx); |
1126 | |
1154 | |
|
|
1155 | /* coro_transfer creates new, empty cctx's */ |
|
|
1156 | if (cctx->sptr) |
|
|
1157 | { |
1127 | #if CORO_USE_VALGRIND |
1158 | #if CORO_USE_VALGRIND |
1128 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1159 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1129 | #endif |
1160 | #endif |
1130 | |
1161 | |
1131 | #if HAVE_MMAP |
1162 | #if HAVE_MMAP |
1132 | if (cctx->flags & CC_MAPPED) |
1163 | if (cctx->flags & CC_MAPPED) |
1133 | munmap (cctx->sptr, cctx->ssize); |
1164 | munmap (cctx->sptr, cctx->ssize); |
1134 | else |
1165 | else |
1135 | #endif |
1166 | #endif |
1136 | Safefree (cctx->sptr); |
1167 | Safefree (cctx->sptr); |
|
|
1168 | } |
1137 | |
1169 | |
1138 | Safefree (cctx); |
1170 | Safefree (cctx); |
1139 | } |
1171 | } |
1140 | |
1172 | |
1141 | /* wether this cctx should be destructed */ |
1173 | /* wether this cctx should be destructed */ |
1142 | #define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) |
1174 | #define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE)) |
1143 | |
1175 | |
1144 | static coro_cctx * |
1176 | static coro_cctx * |
1145 | cctx_get (pTHX) |
1177 | cctx_get (pTHX) |
1146 | { |
1178 | { |
1147 | while (expect_true (cctx_first)) |
1179 | while (expect_true (cctx_first)) |
… | |
… | |
1160 | } |
1192 | } |
1161 | |
1193 | |
1162 | static void |
1194 | static void |
1163 | cctx_put (coro_cctx *cctx) |
1195 | cctx_put (coro_cctx *cctx) |
1164 | { |
1196 | { |
|
|
1197 | assert (("cctx_put called on non-initialised cctx", cctx->sptr)); |
|
|
1198 | |
1165 | /* free another cctx if overlimit */ |
1199 | /* free another cctx if overlimit */ |
1166 | if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) |
1200 | if (expect_false (cctx_idle >= cctx_max_idle)) |
1167 | { |
1201 | { |
1168 | coro_cctx *first = cctx_first; |
1202 | coro_cctx *first = cctx_first; |
1169 | cctx_first = first->next; |
1203 | cctx_first = first->next; |
1170 | --cctx_idle; |
1204 | --cctx_idle; |
1171 | |
1205 | |
… | |
… | |
1203 | /* always use the TRANSFER macro */ |
1237 | /* always use the TRANSFER macro */ |
1204 | static void NOINLINE |
1238 | static void NOINLINE |
1205 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1239 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1206 | { |
1240 | { |
1207 | dSTACKLEVEL; |
1241 | dSTACKLEVEL; |
1208 | static volatile int has_throw; |
|
|
1209 | |
1242 | |
1210 | /* sometimes transfer is only called to set idle_sp */ |
1243 | /* sometimes transfer is only called to set idle_sp */ |
1211 | if (expect_false (!next)) |
1244 | if (expect_false (!next)) |
1212 | { |
1245 | { |
1213 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1246 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1214 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1247 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1215 | } |
1248 | } |
1216 | else if (expect_true (prev != next)) |
1249 | else if (expect_true (prev != next)) |
1217 | { |
1250 | { |
|
|
1251 | static volatile int has_throw; |
1218 | coro_cctx *prev__cctx; |
1252 | coro_cctx *prev__cctx; |
1219 | |
1253 | |
1220 | if (expect_false (prev->flags & CF_NEW)) |
1254 | if (expect_false (prev->flags & CF_NEW)) |
1221 | { |
1255 | { |
1222 | /* create a new empty context */ |
1256 | /* create a new empty/source context */ |
|
|
1257 | ++cctx_count; |
1223 | Newz (0, prev->cctx, 1, coro_cctx); |
1258 | New (0, prev->cctx, 1, coro_cctx); |
|
|
1259 | prev->cctx->sptr = 0; |
|
|
1260 | coro_create (&prev->cctx->cctx, 0, 0, 0, 0); |
|
|
1261 | |
1224 | prev->flags &= ~CF_NEW; |
1262 | prev->flags &= ~CF_NEW; |
1225 | prev->flags |= CF_RUNNING; |
1263 | prev->flags |= CF_RUNNING; |
1226 | } |
1264 | } |
1227 | |
1265 | |
1228 | prev->flags &= ~CF_RUNNING; |
1266 | prev->flags &= ~CF_RUNNING; |
… | |
… | |
1335 | croak ("FATAL: tried to destroy currently running coroutine"); |
1373 | croak ("FATAL: tried to destroy currently running coroutine"); |
1336 | |
1374 | |
1337 | save_perl (aTHX_ &temp); |
1375 | save_perl (aTHX_ &temp); |
1338 | load_perl (aTHX_ coro); |
1376 | load_perl (aTHX_ coro); |
1339 | |
1377 | |
1340 | coro_destroy (aTHX_ coro); |
1378 | coro_destruct (aTHX_ coro); |
1341 | |
1379 | |
1342 | load_perl (aTHX_ &temp); |
1380 | load_perl (aTHX_ &temp); |
1343 | |
1381 | |
1344 | coro->slot = 0; |
1382 | coro->slot = 0; |
1345 | } |
1383 | } |
… | |
… | |
1633 | else |
1671 | else |
1634 | coro->slot->runops = RUNOPS_DEFAULT; |
1672 | coro->slot->runops = RUNOPS_DEFAULT; |
1635 | } |
1673 | } |
1636 | } |
1674 | } |
1637 | |
1675 | |
|
|
1676 | #if 0 |
1638 | static int |
1677 | static int |
1639 | coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) |
1678 | coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) |
1640 | { |
1679 | { |
1641 | AV *padlist; |
1680 | AV *padlist; |
1642 | AV *av = (AV *)mg->mg_obj; |
1681 | AV *av = (AV *)mg->mg_obj; |
… | |
… | |
1648 | |
1687 | |
1649 | static MGVTBL coro_gensub_vtbl = { |
1688 | static MGVTBL coro_gensub_vtbl = { |
1650 | 0, 0, 0, 0, |
1689 | 0, 0, 0, 0, |
1651 | coro_gensub_free |
1690 | coro_gensub_free |
1652 | }; |
1691 | }; |
|
|
1692 | #endif |
1653 | |
1693 | |
1654 | /*****************************************************************************/ |
1694 | /*****************************************************************************/ |
1655 | /* PerlIO::cede */ |
1695 | /* PerlIO::cede */ |
1656 | |
1696 | |
1657 | typedef struct |
1697 | typedef struct |
… | |
… | |
1732 | PROTOTYPES: DISABLE |
1772 | PROTOTYPES: DISABLE |
1733 | |
1773 | |
1734 | BOOT: |
1774 | BOOT: |
1735 | { |
1775 | { |
1736 | #ifdef USE_ITHREADS |
1776 | #ifdef USE_ITHREADS |
1737 | MUTEX_INIT (&coro_mutex); |
1777 | MUTEX_INIT (&coro_lock); |
|
|
1778 | # if CORO_PTHREAD |
|
|
1779 | coro_thx = PERL_GET_CONTEXT; |
|
|
1780 | # endif |
1738 | #endif |
1781 | #endif |
1739 | BOOT_PAGESIZE; |
1782 | BOOT_PAGESIZE; |
1740 | |
1783 | |
1741 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1784 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1742 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1785 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
… | |
… | |
1829 | ta.next = 0; |
1872 | ta.next = 0; |
1830 | break; |
1873 | break; |
1831 | |
1874 | |
1832 | case 1: |
1875 | case 1: |
1833 | if (items != 2) |
1876 | if (items != 2) |
1834 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1877 | croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items); |
1835 | |
1878 | |
1836 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1879 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1837 | break; |
1880 | break; |
1838 | |
1881 | |
1839 | case 2: |
1882 | case 2: |
… | |
… | |
1873 | _exit (code); |
1916 | _exit (code); |
1874 | |
1917 | |
1875 | int |
1918 | int |
1876 | cctx_stacksize (int new_stacksize = 0) |
1919 | cctx_stacksize (int new_stacksize = 0) |
1877 | CODE: |
1920 | CODE: |
1878 | RETVAL = coro_stacksize; |
1921 | RETVAL = cctx_stacksize; |
1879 | if (new_stacksize) |
1922 | if (new_stacksize) |
|
|
1923 | { |
1880 | coro_stacksize = new_stacksize; |
1924 | cctx_stacksize = new_stacksize; |
|
|
1925 | ++cctx_gen; |
|
|
1926 | } |
|
|
1927 | OUTPUT: |
|
|
1928 | RETVAL |
|
|
1929 | |
|
|
1930 | int |
|
|
1931 | cctx_max_idle (int max_idle = 0) |
|
|
1932 | CODE: |
|
|
1933 | RETVAL = cctx_max_idle; |
|
|
1934 | if (max_idle > 1) |
|
|
1935 | cctx_max_idle = max_idle; |
1881 | OUTPUT: |
1936 | OUTPUT: |
1882 | RETVAL |
1937 | RETVAL |
1883 | |
1938 | |
1884 | int |
1939 | int |
1885 | cctx_count () |
1940 | cctx_count () |
… | |
… | |
1981 | CODE: |
2036 | CODE: |
1982 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
2037 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
1983 | OUTPUT: |
2038 | OUTPUT: |
1984 | RETVAL |
2039 | RETVAL |
1985 | |
2040 | |
1986 | IV |
2041 | UV |
1987 | rss (Coro::State coro) |
2042 | rss (Coro::State coro) |
1988 | PROTOTYPE: $ |
2043 | PROTOTYPE: $ |
1989 | ALIAS: |
2044 | ALIAS: |
1990 | usecount = 1 |
2045 | usecount = 1 |
1991 | CODE: |
2046 | CODE: |
… | |
… | |
2002 | CODE: |
2057 | CODE: |
2003 | struct coro *coro = SvSTATE (coro_current); |
2058 | struct coro *coro = SvSTATE (coro_current); |
2004 | coro->cctx->idle_sp = 0; |
2059 | coro->cctx->idle_sp = 0; |
2005 | |
2060 | |
2006 | void |
2061 | void |
2007 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
|
|
2008 | PROTOTYPE: $;$ |
|
|
2009 | CODE: |
|
|
2010 | SvREFCNT_dec (self->throw); |
|
|
2011 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
2012 | |
|
|
2013 | void |
|
|
2014 | swap_defsv (Coro::State self) |
2062 | swap_defsv (Coro::State self) |
2015 | PROTOTYPE: $ |
2063 | PROTOTYPE: $ |
2016 | ALIAS: |
2064 | ALIAS: |
2017 | swap_defav = 1 |
2065 | swap_defav = 1 |
2018 | CODE: |
2066 | CODE: |
… | |
… | |
2121 | CODE: |
2169 | CODE: |
2122 | RETVAL = coro_nready; |
2170 | RETVAL = coro_nready; |
2123 | OUTPUT: |
2171 | OUTPUT: |
2124 | RETVAL |
2172 | RETVAL |
2125 | |
2173 | |
|
|
2174 | void |
|
|
2175 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
|
|
2176 | PROTOTYPE: $;$ |
|
|
2177 | CODE: |
|
|
2178 | SvREFCNT_dec (self->throw); |
|
|
2179 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
2180 | |
2126 | # for async_pool speedup |
2181 | # for async_pool speedup |
2127 | void |
2182 | void |
2128 | _pool_1 (SV *cb) |
2183 | _pool_1 (SV *cb) |
2129 | CODE: |
2184 | CODE: |
2130 | { |
2185 | { |
… | |
… | |
2173 | sv_setsv (cb, &PL_sv_undef); |
2228 | sv_setsv (cb, &PL_sv_undef); |
2174 | |
2229 | |
2175 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2230 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2176 | coro->saved_deffh = 0; |
2231 | coro->saved_deffh = 0; |
2177 | |
2232 | |
2178 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
2233 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
2179 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
2234 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
2180 | { |
2235 | { |
2181 | SV *old = PL_diehook; |
2236 | SV *old = PL_diehook; |
2182 | PL_diehook = 0; |
2237 | PL_diehook = 0; |
2183 | SvREFCNT_dec (old); |
2238 | SvREFCNT_dec (old); |