… | |
… | |
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 | { |
|
|
170 | AV *res; |
153 | int errorno; |
171 | int errorno; |
154 | I32 laststype; |
172 | I32 laststype; /* U16 in 5.10.0 */ |
155 | int laststatval; |
173 | int laststatval; |
156 | Stat_t statcache; |
174 | Stat_t statcache; |
157 | }; |
175 | }; |
158 | |
176 | |
159 | static double (*nvtime)(); /* so why doesn't it take void? */ |
177 | static double (*nvtime)(); /* so why doesn't it take void? */ |
160 | |
178 | |
|
|
179 | static U32 cctx_gen; |
161 | static size_t coro_stacksize = CORO_STACKSIZE; |
180 | static size_t cctx_stacksize = CORO_STACKSIZE; |
162 | static struct CoroAPI coroapi; |
181 | static struct CoroAPI coroapi; |
163 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
182 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
164 | static JMPENV *main_top_env; |
183 | static JMPENV *main_top_env; |
165 | static HV *coro_state_stash, *coro_stash; |
184 | static HV *coro_state_stash, *coro_stash; |
166 | static volatile SV *coro_mortal; /* will be freed after next transfer */ |
185 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
167 | |
186 | |
168 | static GV *irsgv; /* $/ */ |
187 | static GV *irsgv; /* $/ */ |
169 | static GV *stdoutgv; /* *STDOUT */ |
188 | static GV *stdoutgv; /* *STDOUT */ |
170 | static SV *rv_diehook; |
189 | static SV *rv_diehook; |
171 | static SV *rv_warnhook; |
190 | static SV *rv_warnhook; |
… | |
… | |
203 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
222 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
204 | 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 */ |
205 | JMPENV *top_env; |
224 | JMPENV *top_env; |
206 | coro_context cctx; |
225 | coro_context cctx; |
207 | |
226 | |
|
|
227 | U32 gen; |
208 | #if CORO_USE_VALGRIND |
228 | #if CORO_USE_VALGRIND |
209 | int valgrind_id; |
229 | int valgrind_id; |
210 | #endif |
230 | #endif |
211 | unsigned char flags; |
231 | unsigned char flags; |
212 | } coro_cctx; |
232 | } coro_cctx; |
… | |
… | |
272 | #define PRIO_MIN -4 |
292 | #define PRIO_MIN -4 |
273 | |
293 | |
274 | /* for Coro.pm */ |
294 | /* for Coro.pm */ |
275 | static SV *coro_current; |
295 | static SV *coro_current; |
276 | static SV *coro_readyhook; |
296 | static SV *coro_readyhook; |
277 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
297 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
278 | static int coro_nready; |
298 | static int coro_nready; |
279 | static struct coro *coro_first; |
299 | static struct coro *coro_first; |
280 | |
300 | |
281 | /** lowlevel stuff **********************************************************/ |
301 | /** lowlevel stuff **********************************************************/ |
282 | |
302 | |
… | |
… | |
324 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
344 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
325 | #endif |
345 | #endif |
326 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
346 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
327 | --AvFILLp (padlist); |
347 | --AvFILLp (padlist); |
328 | |
348 | |
329 | av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); |
349 | av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); |
330 | av_store (newpadlist, 1, (SV *)newpad); |
350 | av_store (newpadlist, 1, (SV *)newpad); |
331 | |
351 | |
332 | return newpadlist; |
352 | return newpadlist; |
333 | } |
353 | } |
334 | |
354 | |
… | |
… | |
377 | static MGVTBL coro_cv_vtbl = { |
397 | static MGVTBL coro_cv_vtbl = { |
378 | 0, 0, 0, 0, |
398 | 0, 0, 0, 0, |
379 | coro_cv_free |
399 | coro_cv_free |
380 | }; |
400 | }; |
381 | |
401 | |
382 | #define CORO_MAGIC(sv,type) \ |
402 | #define CORO_MAGIC(sv, type) \ |
383 | SvMAGIC (sv) \ |
403 | SvMAGIC (sv) \ |
384 | ? SvMAGIC (sv)->mg_type == type \ |
404 | ? SvMAGIC (sv)->mg_type == type \ |
385 | ? SvMAGIC (sv) \ |
405 | ? SvMAGIC (sv) \ |
386 | : mg_find (sv, type) \ |
406 | : mg_find (sv, type) \ |
387 | : 0 |
407 | : 0 |
388 | |
408 | |
389 | #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) |
390 | #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) |
391 | |
411 | |
392 | static struct coro * |
412 | static struct coro * |
… | |
… | |
622 | |
642 | |
623 | /* |
643 | /* |
624 | * destroy the stacks, the callchain etc... |
644 | * destroy the stacks, the callchain etc... |
625 | */ |
645 | */ |
626 | static void |
646 | static void |
627 | coro_destroy_stacks (pTHX) |
647 | coro_destruct_stacks (pTHX) |
628 | { |
648 | { |
629 | while (PL_curstackinfo->si_next) |
649 | while (PL_curstackinfo->si_next) |
630 | PL_curstackinfo = PL_curstackinfo->si_next; |
650 | PL_curstackinfo = PL_curstackinfo->si_next; |
631 | |
651 | |
632 | while (PL_curstackinfo) |
652 | while (PL_curstackinfo) |
… | |
… | |
810 | GvSV (PL_defgv) = newSV (0); |
830 | GvSV (PL_defgv) = newSV (0); |
811 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
831 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
812 | GvSV (PL_errgv) = newSV (0); |
832 | GvSV (PL_errgv) = newSV (0); |
813 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
833 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
814 | PL_rs = newSVsv (GvSV (irsgv)); |
834 | PL_rs = newSVsv (GvSV (irsgv)); |
815 | PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); |
835 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
816 | |
836 | |
817 | { |
837 | { |
818 | dSP; |
838 | dSP; |
819 | LOGOP myop; |
839 | LOGOP myop; |
820 | |
840 | |
… | |
… | |
837 | */ |
857 | */ |
838 | ENTER; |
858 | ENTER; |
839 | } |
859 | } |
840 | |
860 | |
841 | static void |
861 | static void |
842 | coro_destroy (pTHX_ struct coro *coro) |
862 | coro_destruct (pTHX_ struct coro *coro) |
843 | { |
863 | { |
844 | if (!IN_DESTRUCT) |
864 | if (!IN_DESTRUCT) |
845 | { |
865 | { |
846 | /* restore all saved variables and stuff */ |
866 | /* restore all saved variables and stuff */ |
847 | LEAVE_SCOPE (0); |
867 | LEAVE_SCOPE (0); |
… | |
… | |
869 | SvREFCNT_dec (PL_warnhook); |
889 | SvREFCNT_dec (PL_warnhook); |
870 | |
890 | |
871 | SvREFCNT_dec (coro->saved_deffh); |
891 | SvREFCNT_dec (coro->saved_deffh); |
872 | SvREFCNT_dec (coro->throw); |
892 | SvREFCNT_dec (coro->throw); |
873 | |
893 | |
874 | coro_destroy_stacks (aTHX); |
894 | coro_destruct_stacks (aTHX); |
875 | } |
895 | } |
876 | |
896 | |
877 | static void |
897 | static void |
878 | free_coro_mortal (pTHX) |
898 | free_coro_mortal (pTHX) |
879 | { |
899 | { |
… | |
… | |
916 | : cx->blk_gimme == G_SCALAR ? bot + 1 |
936 | : cx->blk_gimme == G_SCALAR ? bot + 1 |
917 | : bot; |
937 | : bot; |
918 | |
938 | |
919 | av_extend (av, top - bot); |
939 | av_extend (av, top - bot); |
920 | while (bot < top) |
940 | while (bot < top) |
921 | av_push (av, SvREFCNT_inc (*bot++)); |
941 | av_push (av, SvREFCNT_inc_NN (*bot++)); |
922 | |
942 | |
923 | PL_runops = RUNOPS_DEFAULT; |
943 | PL_runops = RUNOPS_DEFAULT; |
924 | ENTER; |
944 | ENTER; |
925 | SAVETMPS; |
945 | SAVETMPS; |
926 | EXTEND (SP, 3); |
946 | EXTEND (SP, 3); |
… | |
… | |
1040 | * this is a _very_ stripped down perl interpreter ;) |
1060 | * this is a _very_ stripped down perl interpreter ;) |
1041 | */ |
1061 | */ |
1042 | static void |
1062 | static void |
1043 | cctx_run (void *arg) |
1063 | cctx_run (void *arg) |
1044 | { |
1064 | { |
|
|
1065 | #ifdef USE_ITHREADS |
|
|
1066 | # if CORO_PTHREAD |
|
|
1067 | PERL_SET_CONTEXT (coro_thx); |
|
|
1068 | # endif |
|
|
1069 | #endif |
|
|
1070 | { |
1045 | dTHX; |
1071 | dTHX; |
1046 | |
1072 | |
1047 | /* cctx_run is the alternative tail of transfer(), so unlock here. */ |
1073 | /* cctx_run is the alternative tail of transfer(), so unlock here. */ |
1048 | UNLOCK; |
1074 | UNLOCK; |
1049 | |
1075 | |
1050 | /* we now skip the entersub that lead to transfer() */ |
1076 | /* we now skip the entersub that lead to transfer() */ |
1051 | PL_op = PL_op->op_next; |
1077 | PL_op = PL_op->op_next; |
1052 | |
1078 | |
1053 | /* inject a fake subroutine call to cctx_init */ |
1079 | /* inject a fake subroutine call to cctx_init */ |
1054 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1080 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1055 | |
1081 | |
1056 | /* 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 */ |
1057 | PL_restartop = PL_op; |
1083 | PL_restartop = PL_op; |
1058 | perl_run (PL_curinterp); |
1084 | perl_run (PL_curinterp); |
1059 | |
1085 | |
1060 | /* |
1086 | /* |
1061 | * 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 |
1062 | * 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) |
1063 | * 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 |
1064 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1090 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1065 | * coroutine as Coro has no such concept |
1091 | * coroutine as Coro has no such concept |
1066 | */ |
1092 | */ |
1067 | PL_top_env = main_top_env; |
1093 | PL_top_env = main_top_env; |
1068 | 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 | } |
1069 | } |
1096 | } |
1070 | |
1097 | |
1071 | static coro_cctx * |
1098 | static coro_cctx * |
1072 | cctx_new () |
1099 | cctx_new () |
1073 | { |
1100 | { |
1074 | coro_cctx *cctx; |
1101 | coro_cctx *cctx; |
|
|
1102 | |
|
|
1103 | ++cctx_count; |
|
|
1104 | New (0, cctx, 1, coro_cctx); |
|
|
1105 | |
|
|
1106 | cctx->gen = cctx_gen; |
|
|
1107 | cctx->flags = 0; |
|
|
1108 | |
|
|
1109 | return cctx; |
|
|
1110 | } |
|
|
1111 | |
|
|
1112 | /* create a new cctx only suitable as source */ |
|
|
1113 | static coro_cctx * |
|
|
1114 | cctx_new_empty () |
|
|
1115 | { |
|
|
1116 | coro_cctx *cctx = cctx_new (); |
|
|
1117 | |
|
|
1118 | cctx->sptr = 0; |
|
|
1119 | cctx->idle_sp = 0; /* should never be a valid address */ |
|
|
1120 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
|
|
1121 | |
|
|
1122 | return cctx; |
|
|
1123 | } |
|
|
1124 | |
|
|
1125 | /* create a new cctx suitable as destination/running a perl interpreter */ |
|
|
1126 | static coro_cctx * |
|
|
1127 | cctx_new_run () |
|
|
1128 | { |
|
|
1129 | coro_cctx *cctx = cctx_new (); |
1075 | void *stack_start; |
1130 | void *stack_start; |
1076 | size_t stack_size; |
1131 | size_t stack_size; |
1077 | |
1132 | |
1078 | ++cctx_count; |
|
|
1079 | |
|
|
1080 | Newz (0, cctx, 1, coro_cctx); |
|
|
1081 | |
|
|
1082 | #if HAVE_MMAP |
1133 | #if HAVE_MMAP |
1083 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1134 | cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1084 | /* mmap supposedly does allocate-on-write for us */ |
1135 | /* mmap supposedly does allocate-on-write for us */ |
1085 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
1136 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
1086 | |
1137 | |
1087 | if (cctx->sptr != (void *)-1) |
1138 | if (cctx->sptr != (void *)-1) |
1088 | { |
1139 | { |
… | |
… | |
1094 | cctx->flags |= CC_MAPPED; |
1145 | cctx->flags |= CC_MAPPED; |
1095 | } |
1146 | } |
1096 | else |
1147 | else |
1097 | #endif |
1148 | #endif |
1098 | { |
1149 | { |
1099 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
1150 | cctx->ssize = cctx_stacksize * (long)sizeof (long); |
1100 | New (0, cctx->sptr, coro_stacksize, long); |
1151 | New (0, cctx->sptr, cctx_stacksize, long); |
1101 | |
1152 | |
1102 | if (!cctx->sptr) |
1153 | if (!cctx->sptr) |
1103 | { |
1154 | { |
1104 | perror ("FATAL: unable to allocate stack for coroutine"); |
1155 | perror ("FATAL: unable to allocate stack for coroutine"); |
1105 | _exit (EXIT_FAILURE); |
1156 | _exit (EXIT_FAILURE); |
… | |
… | |
1120 | { |
1171 | { |
1121 | if (!cctx) |
1172 | if (!cctx) |
1122 | return; |
1173 | return; |
1123 | |
1174 | |
1124 | --cctx_count; |
1175 | --cctx_count; |
|
|
1176 | coro_destroy (&cctx->cctx); |
1125 | |
1177 | |
|
|
1178 | /* coro_transfer creates new, empty cctx's */ |
|
|
1179 | if (cctx->sptr) |
|
|
1180 | { |
1126 | #if CORO_USE_VALGRIND |
1181 | #if CORO_USE_VALGRIND |
1127 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1182 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1128 | #endif |
1183 | #endif |
1129 | |
1184 | |
1130 | #if HAVE_MMAP |
1185 | #if HAVE_MMAP |
1131 | if (cctx->flags & CC_MAPPED) |
1186 | if (cctx->flags & CC_MAPPED) |
1132 | munmap (cctx->sptr, cctx->ssize); |
1187 | munmap (cctx->sptr, cctx->ssize); |
1133 | else |
1188 | else |
1134 | #endif |
1189 | #endif |
1135 | Safefree (cctx->sptr); |
1190 | Safefree (cctx->sptr); |
|
|
1191 | } |
1136 | |
1192 | |
1137 | Safefree (cctx); |
1193 | Safefree (cctx); |
1138 | } |
1194 | } |
1139 | |
1195 | |
1140 | /* wether this cctx should be destructed */ |
1196 | /* wether this cctx should be destructed */ |
1141 | #define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) |
1197 | #define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE)) |
1142 | |
1198 | |
1143 | static coro_cctx * |
1199 | static coro_cctx * |
1144 | cctx_get (pTHX) |
1200 | cctx_get (pTHX) |
1145 | { |
1201 | { |
1146 | while (expect_true (cctx_first)) |
1202 | while (expect_true (cctx_first)) |
… | |
… | |
1153 | return cctx; |
1209 | return cctx; |
1154 | |
1210 | |
1155 | cctx_destroy (cctx); |
1211 | cctx_destroy (cctx); |
1156 | } |
1212 | } |
1157 | |
1213 | |
1158 | return cctx_new (); |
1214 | return cctx_new_run (); |
1159 | } |
1215 | } |
1160 | |
1216 | |
1161 | static void |
1217 | static void |
1162 | cctx_put (coro_cctx *cctx) |
1218 | cctx_put (coro_cctx *cctx) |
1163 | { |
1219 | { |
|
|
1220 | assert (("cctx_put called on non-initialised cctx", cctx->sptr)); |
|
|
1221 | |
1164 | /* free another cctx if overlimit */ |
1222 | /* free another cctx if overlimit */ |
1165 | if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) |
1223 | if (expect_false (cctx_idle >= cctx_max_idle)) |
1166 | { |
1224 | { |
1167 | coro_cctx *first = cctx_first; |
1225 | coro_cctx *first = cctx_first; |
1168 | cctx_first = first->next; |
1226 | cctx_first = first->next; |
1169 | --cctx_idle; |
1227 | --cctx_idle; |
1170 | |
1228 | |
… | |
… | |
1202 | /* always use the TRANSFER macro */ |
1260 | /* always use the TRANSFER macro */ |
1203 | static void NOINLINE |
1261 | static void NOINLINE |
1204 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1262 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1205 | { |
1263 | { |
1206 | dSTACKLEVEL; |
1264 | dSTACKLEVEL; |
1207 | static volatile int has_throw; |
|
|
1208 | |
1265 | |
1209 | /* sometimes transfer is only called to set idle_sp */ |
1266 | /* sometimes transfer is only called to set idle_sp */ |
1210 | if (expect_false (!next)) |
1267 | if (expect_false (!next)) |
1211 | { |
1268 | { |
1212 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1269 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1213 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1270 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1214 | } |
1271 | } |
1215 | else if (expect_true (prev != next)) |
1272 | else if (expect_true (prev != next)) |
1216 | { |
1273 | { |
|
|
1274 | static volatile int has_throw; |
1217 | coro_cctx *prev__cctx; |
1275 | coro_cctx *prev__cctx; |
1218 | |
1276 | |
1219 | if (expect_false (prev->flags & CF_NEW)) |
1277 | if (expect_false (prev->flags & CF_NEW)) |
1220 | { |
1278 | { |
1221 | /* create a new empty context */ |
1279 | /* create a new empty/source context */ |
1222 | Newz (0, prev->cctx, 1, coro_cctx); |
1280 | prev->cctx = cctx_new_empty (); |
1223 | prev->flags &= ~CF_NEW; |
1281 | prev->flags &= ~CF_NEW; |
1224 | prev->flags |= CF_RUNNING; |
1282 | prev->flags |= CF_RUNNING; |
1225 | } |
1283 | } |
1226 | |
1284 | |
1227 | prev->flags &= ~CF_RUNNING; |
1285 | prev->flags &= ~CF_RUNNING; |
… | |
… | |
1334 | croak ("FATAL: tried to destroy currently running coroutine"); |
1392 | croak ("FATAL: tried to destroy currently running coroutine"); |
1335 | |
1393 | |
1336 | save_perl (aTHX_ &temp); |
1394 | save_perl (aTHX_ &temp); |
1337 | load_perl (aTHX_ coro); |
1395 | load_perl (aTHX_ coro); |
1338 | |
1396 | |
1339 | coro_destroy (aTHX_ coro); |
1397 | coro_destruct (aTHX_ coro); |
1340 | |
1398 | |
1341 | load_perl (aTHX_ &temp); |
1399 | load_perl (aTHX_ &temp); |
1342 | |
1400 | |
1343 | coro->slot = 0; |
1401 | coro->slot = 0; |
1344 | } |
1402 | } |
… | |
… | |
1450 | LOCK; |
1508 | LOCK; |
1451 | |
1509 | |
1452 | sv_hook = coro_nready ? 0 : coro_readyhook; |
1510 | sv_hook = coro_nready ? 0 : coro_readyhook; |
1453 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
1511 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
1454 | |
1512 | |
1455 | coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); |
1513 | coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv)); |
1456 | ++coro_nready; |
1514 | ++coro_nready; |
1457 | |
1515 | |
1458 | UNLOCK; |
1516 | UNLOCK; |
1459 | |
1517 | |
1460 | if (sv_hook) |
1518 | if (sv_hook) |
… | |
… | |
1615 | struct coro *coro = SvSTATE (coro_sv); |
1673 | struct coro *coro = SvSTATE (coro_sv); |
1616 | |
1674 | |
1617 | if (flags & CC_TRACE) |
1675 | if (flags & CC_TRACE) |
1618 | { |
1676 | { |
1619 | if (!coro->cctx) |
1677 | if (!coro->cctx) |
1620 | coro->cctx = cctx_new (); |
1678 | coro->cctx = cctx_new_run (); |
1621 | else if (!(coro->cctx->flags & CC_TRACE)) |
1679 | else if (!(coro->cctx->flags & CC_TRACE)) |
1622 | croak ("cannot enable tracing on coroutine with custom stack"); |
1680 | croak ("cannot enable tracing on coroutine with custom stack"); |
1623 | |
1681 | |
1624 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1682 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1625 | } |
1683 | } |
… | |
… | |
1632 | else |
1690 | else |
1633 | coro->slot->runops = RUNOPS_DEFAULT; |
1691 | coro->slot->runops = RUNOPS_DEFAULT; |
1634 | } |
1692 | } |
1635 | } |
1693 | } |
1636 | |
1694 | |
|
|
1695 | #if 0 |
1637 | static int |
1696 | static int |
1638 | coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) |
1697 | coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) |
1639 | { |
1698 | { |
1640 | AV *padlist; |
1699 | AV *padlist; |
1641 | AV *av = (AV *)mg->mg_obj; |
1700 | AV *av = (AV *)mg->mg_obj; |
… | |
… | |
1647 | |
1706 | |
1648 | static MGVTBL coro_gensub_vtbl = { |
1707 | static MGVTBL coro_gensub_vtbl = { |
1649 | 0, 0, 0, 0, |
1708 | 0, 0, 0, 0, |
1650 | coro_gensub_free |
1709 | coro_gensub_free |
1651 | }; |
1710 | }; |
|
|
1711 | #endif |
1652 | |
1712 | |
1653 | /*****************************************************************************/ |
1713 | /*****************************************************************************/ |
1654 | /* PerlIO::cede */ |
1714 | /* PerlIO::cede */ |
1655 | |
1715 | |
1656 | typedef struct |
1716 | typedef struct |
… | |
… | |
1731 | PROTOTYPES: DISABLE |
1791 | PROTOTYPES: DISABLE |
1732 | |
1792 | |
1733 | BOOT: |
1793 | BOOT: |
1734 | { |
1794 | { |
1735 | #ifdef USE_ITHREADS |
1795 | #ifdef USE_ITHREADS |
1736 | MUTEX_INIT (&coro_mutex); |
1796 | MUTEX_INIT (&coro_lock); |
|
|
1797 | # if CORO_PTHREAD |
|
|
1798 | coro_thx = PERL_GET_CONTEXT; |
|
|
1799 | # endif |
1737 | #endif |
1800 | #endif |
1738 | BOOT_PAGESIZE; |
1801 | BOOT_PAGESIZE; |
1739 | |
1802 | |
1740 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1803 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1741 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1804 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
… | |
… | |
1828 | ta.next = 0; |
1891 | ta.next = 0; |
1829 | break; |
1892 | break; |
1830 | |
1893 | |
1831 | case 1: |
1894 | case 1: |
1832 | if (items != 2) |
1895 | if (items != 2) |
1833 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1896 | croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items); |
1834 | |
1897 | |
1835 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1898 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1836 | break; |
1899 | break; |
1837 | |
1900 | |
1838 | case 2: |
1901 | case 2: |
… | |
… | |
1872 | _exit (code); |
1935 | _exit (code); |
1873 | |
1936 | |
1874 | int |
1937 | int |
1875 | cctx_stacksize (int new_stacksize = 0) |
1938 | cctx_stacksize (int new_stacksize = 0) |
1876 | CODE: |
1939 | CODE: |
1877 | RETVAL = coro_stacksize; |
1940 | RETVAL = cctx_stacksize; |
1878 | if (new_stacksize) |
1941 | if (new_stacksize) |
|
|
1942 | { |
1879 | coro_stacksize = new_stacksize; |
1943 | cctx_stacksize = new_stacksize; |
|
|
1944 | ++cctx_gen; |
|
|
1945 | } |
|
|
1946 | OUTPUT: |
|
|
1947 | RETVAL |
|
|
1948 | |
|
|
1949 | int |
|
|
1950 | cctx_max_idle (int max_idle = 0) |
|
|
1951 | CODE: |
|
|
1952 | RETVAL = cctx_max_idle; |
|
|
1953 | if (max_idle > 1) |
|
|
1954 | cctx_max_idle = max_idle; |
1880 | OUTPUT: |
1955 | OUTPUT: |
1881 | RETVAL |
1956 | RETVAL |
1882 | |
1957 | |
1883 | int |
1958 | int |
1884 | cctx_count () |
1959 | cctx_count () |
… | |
… | |
1980 | CODE: |
2055 | CODE: |
1981 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
2056 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
1982 | OUTPUT: |
2057 | OUTPUT: |
1983 | RETVAL |
2058 | RETVAL |
1984 | |
2059 | |
1985 | IV |
2060 | UV |
1986 | rss (Coro::State coro) |
2061 | rss (Coro::State coro) |
1987 | PROTOTYPE: $ |
2062 | PROTOTYPE: $ |
1988 | ALIAS: |
2063 | ALIAS: |
1989 | usecount = 1 |
2064 | usecount = 1 |
1990 | CODE: |
2065 | CODE: |
… | |
… | |
2001 | CODE: |
2076 | CODE: |
2002 | struct coro *coro = SvSTATE (coro_current); |
2077 | struct coro *coro = SvSTATE (coro_current); |
2003 | coro->cctx->idle_sp = 0; |
2078 | coro->cctx->idle_sp = 0; |
2004 | |
2079 | |
2005 | void |
2080 | void |
2006 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
|
|
2007 | PROTOTYPE: $;$ |
|
|
2008 | CODE: |
|
|
2009 | SvREFCNT_dec (self->throw); |
|
|
2010 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
2011 | |
|
|
2012 | void |
|
|
2013 | swap_defsv (Coro::State self) |
2081 | swap_defsv (Coro::State self) |
2014 | PROTOTYPE: $ |
2082 | PROTOTYPE: $ |
2015 | ALIAS: |
2083 | ALIAS: |
2016 | swap_defav = 1 |
2084 | swap_defav = 1 |
2017 | CODE: |
2085 | CODE: |
… | |
… | |
2071 | void |
2139 | void |
2072 | _set_current (SV *current) |
2140 | _set_current (SV *current) |
2073 | PROTOTYPE: $ |
2141 | PROTOTYPE: $ |
2074 | CODE: |
2142 | CODE: |
2075 | SvREFCNT_dec (SvRV (coro_current)); |
2143 | SvREFCNT_dec (SvRV (coro_current)); |
2076 | SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); |
2144 | SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); |
2077 | |
2145 | |
2078 | void |
2146 | void |
2079 | _set_readyhook (SV *hook) |
2147 | _set_readyhook (SV *hook) |
2080 | PROTOTYPE: $ |
2148 | PROTOTYPE: $ |
2081 | CODE: |
2149 | CODE: |
… | |
… | |
2120 | CODE: |
2188 | CODE: |
2121 | RETVAL = coro_nready; |
2189 | RETVAL = coro_nready; |
2122 | OUTPUT: |
2190 | OUTPUT: |
2123 | RETVAL |
2191 | RETVAL |
2124 | |
2192 | |
|
|
2193 | void |
|
|
2194 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
|
|
2195 | PROTOTYPE: $;$ |
|
|
2196 | CODE: |
|
|
2197 | SvREFCNT_dec (self->throw); |
|
|
2198 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
2199 | |
2125 | # for async_pool speedup |
2200 | # for async_pool speedup |
2126 | void |
2201 | void |
2127 | _pool_1 (SV *cb) |
2202 | _pool_1 (SV *cb) |
2128 | CODE: |
2203 | CODE: |
2129 | { |
2204 | { |
… | |
… | |
2141 | SvREFCNT_dec (old); |
2216 | SvREFCNT_dec (old); |
2142 | croak ("\3async_pool terminate\2\n"); |
2217 | croak ("\3async_pool terminate\2\n"); |
2143 | } |
2218 | } |
2144 | |
2219 | |
2145 | SvREFCNT_dec (coro->saved_deffh); |
2220 | SvREFCNT_dec (coro->saved_deffh); |
2146 | coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); |
2221 | coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv); |
2147 | |
2222 | |
2148 | hv_store (hv, "desc", sizeof ("desc") - 1, |
2223 | hv_store (hv, "desc", sizeof ("desc") - 1, |
2149 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
2224 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
2150 | |
2225 | |
2151 | invoke_av = (AV *)SvRV (invoke); |
2226 | invoke_av = (AV *)SvRV (invoke); |
… | |
… | |
2155 | |
2230 | |
2156 | if (len > 0) |
2231 | if (len > 0) |
2157 | { |
2232 | { |
2158 | av_fill (defav, len - 1); |
2233 | av_fill (defav, len - 1); |
2159 | for (i = 0; i < len; ++i) |
2234 | for (i = 0; i < len; ++i) |
2160 | av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); |
2235 | av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); |
2161 | } |
2236 | } |
2162 | |
2237 | |
2163 | SvREFCNT_dec (invoke); |
2238 | SvREFCNT_dec (invoke); |
2164 | } |
2239 | } |
2165 | |
2240 | |
… | |
… | |
2172 | sv_setsv (cb, &PL_sv_undef); |
2247 | sv_setsv (cb, &PL_sv_undef); |
2173 | |
2248 | |
2174 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2249 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2175 | coro->saved_deffh = 0; |
2250 | coro->saved_deffh = 0; |
2176 | |
2251 | |
2177 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
2252 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
2178 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
2253 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
2179 | { |
2254 | { |
2180 | SV *old = PL_diehook; |
2255 | SV *old = PL_diehook; |
2181 | PL_diehook = 0; |
2256 | PL_diehook = 0; |
2182 | SvREFCNT_dec (old); |
2257 | SvREFCNT_dec (old); |
… | |
… | |
2244 | #endif |
2319 | #endif |
2245 | |
2320 | |
2246 | |
2321 | |
2247 | MODULE = Coro::State PACKAGE = Coro::AIO |
2322 | MODULE = Coro::State PACKAGE = Coro::AIO |
2248 | |
2323 | |
2249 | SV * |
2324 | void |
2250 | _get_state () |
2325 | _get_state (SV *self) |
2251 | CODE: |
2326 | PPCODE: |
2252 | { |
2327 | { |
2253 | struct io_state *data; |
2328 | AV *defav = GvAV (PL_defgv); |
2254 | |
2329 | AV *av = newAV (); |
|
|
2330 | int i; |
2255 | RETVAL = newSV (sizeof (struct io_state)); |
2331 | SV *data_sv = newSV (sizeof (struct io_state)); |
2256 | data = (struct io_state *)SvPVX (RETVAL); |
2332 | struct io_state *data = (struct io_state *)SvPVX (data_sv); |
2257 | SvCUR_set (RETVAL, sizeof (struct io_state)); |
2333 | SvCUR_set (data_sv, sizeof (struct io_state)); |
2258 | SvPOK_only (RETVAL); |
2334 | SvPOK_only (data_sv); |
2259 | |
2335 | |
2260 | data->errorno = errno; |
2336 | data->errorno = errno; |
2261 | data->laststype = PL_laststype; |
2337 | data->laststype = PL_laststype; |
2262 | data->laststatval = PL_laststatval; |
2338 | data->laststatval = PL_laststatval; |
2263 | data->statcache = PL_statcache; |
2339 | data->statcache = PL_statcache; |
|
|
2340 | |
|
|
2341 | av_extend (av, AvFILLp (defav) + 1 + 1); |
|
|
2342 | |
|
|
2343 | for (i = 0; i <= AvFILLp (defav); ++i) |
|
|
2344 | av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i])); |
|
|
2345 | |
|
|
2346 | av_push (av, data_sv); |
|
|
2347 | |
|
|
2348 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
|
|
2349 | |
|
|
2350 | api_ready (self); |
2264 | } |
2351 | } |
2265 | OUTPUT: |
|
|
2266 | RETVAL |
|
|
2267 | |
2352 | |
2268 | void |
2353 | void |
2269 | _set_state (char *data_) |
2354 | _set_state (SV *state) |
2270 | PROTOTYPE: $ |
2355 | PROTOTYPE: $ |
2271 | CODE: |
2356 | PPCODE: |
2272 | { |
2357 | { |
2273 | struct io_state *data = (void *)data_; |
2358 | AV *av = (AV *)SvRV (state); |
|
|
2359 | struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]); |
|
|
2360 | int i; |
2274 | |
2361 | |
2275 | errno = data->errorno; |
2362 | errno = data->errorno; |
2276 | PL_laststype = data->laststype; |
2363 | PL_laststype = data->laststype; |
2277 | PL_laststatval = data->laststatval; |
2364 | PL_laststatval = data->laststatval; |
2278 | PL_statcache = data->statcache; |
2365 | PL_statcache = data->statcache; |
|
|
2366 | |
|
|
2367 | EXTEND (SP, AvFILLp (av)); |
|
|
2368 | for (i = 0; i < AvFILLp (av); ++i) |
|
|
2369 | PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i]))); |
2279 | } |
2370 | } |
2280 | |
2371 | |
2281 | |
2372 | |
2282 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2373 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2283 | |
2374 | |