… | |
… | |
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; |
1075 | void *stack_start; |
1102 | void *stack_start; |
1076 | size_t stack_size; |
1103 | size_t stack_size; |
1077 | |
1104 | |
1078 | ++cctx_count; |
1105 | ++cctx_count; |
1079 | |
|
|
1080 | Newz (0, cctx, 1, coro_cctx); |
1106 | Newz (0, cctx, 1, coro_cctx); |
1081 | |
1107 | |
|
|
1108 | cctx->gen = cctx_gen; |
|
|
1109 | |
1082 | #if HAVE_MMAP |
1110 | #if HAVE_MMAP |
1083 | 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; |
1084 | /* mmap supposedly does allocate-on-write for us */ |
1112 | /* 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); |
1113 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
1086 | |
1114 | |
1087 | if (cctx->sptr != (void *)-1) |
1115 | if (cctx->sptr != (void *)-1) |
1088 | { |
1116 | { |
… | |
… | |
1094 | cctx->flags |= CC_MAPPED; |
1122 | cctx->flags |= CC_MAPPED; |
1095 | } |
1123 | } |
1096 | else |
1124 | else |
1097 | #endif |
1125 | #endif |
1098 | { |
1126 | { |
1099 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
1127 | cctx->ssize = cctx_stacksize * (long)sizeof (long); |
1100 | New (0, cctx->sptr, coro_stacksize, long); |
1128 | New (0, cctx->sptr, cctx_stacksize, long); |
1101 | |
1129 | |
1102 | if (!cctx->sptr) |
1130 | if (!cctx->sptr) |
1103 | { |
1131 | { |
1104 | perror ("FATAL: unable to allocate stack for coroutine"); |
1132 | perror ("FATAL: unable to allocate stack for coroutine"); |
1105 | _exit (EXIT_FAILURE); |
1133 | _exit (EXIT_FAILURE); |
… | |
… | |
1120 | { |
1148 | { |
1121 | if (!cctx) |
1149 | if (!cctx) |
1122 | return; |
1150 | return; |
1123 | |
1151 | |
1124 | --cctx_count; |
1152 | --cctx_count; |
|
|
1153 | coro_destroy (&cctx->cctx); |
1125 | |
1154 | |
|
|
1155 | /* coro_transfer creates new, empty cctx's */ |
|
|
1156 | if (cctx->sptr) |
|
|
1157 | { |
1126 | #if CORO_USE_VALGRIND |
1158 | #if CORO_USE_VALGRIND |
1127 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1159 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1128 | #endif |
1160 | #endif |
1129 | |
1161 | |
1130 | #if HAVE_MMAP |
1162 | #if HAVE_MMAP |
1131 | if (cctx->flags & CC_MAPPED) |
1163 | if (cctx->flags & CC_MAPPED) |
1132 | munmap (cctx->sptr, cctx->ssize); |
1164 | munmap (cctx->sptr, cctx->ssize); |
1133 | else |
1165 | else |
1134 | #endif |
1166 | #endif |
1135 | Safefree (cctx->sptr); |
1167 | Safefree (cctx->sptr); |
|
|
1168 | } |
1136 | |
1169 | |
1137 | Safefree (cctx); |
1170 | Safefree (cctx); |
1138 | } |
1171 | } |
1139 | |
1172 | |
1140 | /* wether this cctx should be destructed */ |
1173 | /* wether this cctx should be destructed */ |
1141 | #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)) |
1142 | |
1175 | |
1143 | static coro_cctx * |
1176 | static coro_cctx * |
1144 | cctx_get (pTHX) |
1177 | cctx_get (pTHX) |
1145 | { |
1178 | { |
1146 | while (expect_true (cctx_first)) |
1179 | while (expect_true (cctx_first)) |
… | |
… | |
1159 | } |
1192 | } |
1160 | |
1193 | |
1161 | static void |
1194 | static void |
1162 | cctx_put (coro_cctx *cctx) |
1195 | cctx_put (coro_cctx *cctx) |
1163 | { |
1196 | { |
|
|
1197 | assert (("cctx_put called on non-initialised cctx", cctx->sptr)); |
|
|
1198 | |
1164 | /* free another cctx if overlimit */ |
1199 | /* free another cctx if overlimit */ |
1165 | if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) |
1200 | if (expect_false (cctx_idle >= cctx_max_idle)) |
1166 | { |
1201 | { |
1167 | coro_cctx *first = cctx_first; |
1202 | coro_cctx *first = cctx_first; |
1168 | cctx_first = first->next; |
1203 | cctx_first = first->next; |
1169 | --cctx_idle; |
1204 | --cctx_idle; |
1170 | |
1205 | |
… | |
… | |
1202 | /* always use the TRANSFER macro */ |
1237 | /* always use the TRANSFER macro */ |
1203 | static void NOINLINE |
1238 | static void NOINLINE |
1204 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1239 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1205 | { |
1240 | { |
1206 | dSTACKLEVEL; |
1241 | dSTACKLEVEL; |
1207 | static volatile int has_throw; |
|
|
1208 | |
1242 | |
1209 | /* sometimes transfer is only called to set idle_sp */ |
1243 | /* sometimes transfer is only called to set idle_sp */ |
1210 | if (expect_false (!next)) |
1244 | if (expect_false (!next)) |
1211 | { |
1245 | { |
1212 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1246 | ((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 */ |
1247 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1214 | } |
1248 | } |
1215 | else if (expect_true (prev != next)) |
1249 | else if (expect_true (prev != next)) |
1216 | { |
1250 | { |
|
|
1251 | static volatile int has_throw; |
1217 | coro_cctx *prev__cctx; |
1252 | coro_cctx *prev__cctx; |
1218 | |
1253 | |
1219 | if (expect_false (prev->flags & CF_NEW)) |
1254 | if (expect_false (prev->flags & CF_NEW)) |
1220 | { |
1255 | { |
1221 | /* create a new empty context */ |
1256 | /* create a new empty/source context */ |
|
|
1257 | ++cctx_count; |
1222 | 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 | |
1223 | prev->flags &= ~CF_NEW; |
1262 | prev->flags &= ~CF_NEW; |
1224 | prev->flags |= CF_RUNNING; |
1263 | prev->flags |= CF_RUNNING; |
1225 | } |
1264 | } |
1226 | |
1265 | |
1227 | prev->flags &= ~CF_RUNNING; |
1266 | prev->flags &= ~CF_RUNNING; |
… | |
… | |
1334 | croak ("FATAL: tried to destroy currently running coroutine"); |
1373 | croak ("FATAL: tried to destroy currently running coroutine"); |
1335 | |
1374 | |
1336 | save_perl (aTHX_ &temp); |
1375 | save_perl (aTHX_ &temp); |
1337 | load_perl (aTHX_ coro); |
1376 | load_perl (aTHX_ coro); |
1338 | |
1377 | |
1339 | coro_destroy (aTHX_ coro); |
1378 | coro_destruct (aTHX_ coro); |
1340 | |
1379 | |
1341 | load_perl (aTHX_ &temp); |
1380 | load_perl (aTHX_ &temp); |
1342 | |
1381 | |
1343 | coro->slot = 0; |
1382 | coro->slot = 0; |
1344 | } |
1383 | } |
… | |
… | |
1450 | LOCK; |
1489 | LOCK; |
1451 | |
1490 | |
1452 | sv_hook = coro_nready ? 0 : coro_readyhook; |
1491 | sv_hook = coro_nready ? 0 : coro_readyhook; |
1453 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
1492 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
1454 | |
1493 | |
1455 | coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); |
1494 | coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv)); |
1456 | ++coro_nready; |
1495 | ++coro_nready; |
1457 | |
1496 | |
1458 | UNLOCK; |
1497 | UNLOCK; |
1459 | |
1498 | |
1460 | if (sv_hook) |
1499 | if (sv_hook) |
… | |
… | |
1632 | else |
1671 | else |
1633 | coro->slot->runops = RUNOPS_DEFAULT; |
1672 | coro->slot->runops = RUNOPS_DEFAULT; |
1634 | } |
1673 | } |
1635 | } |
1674 | } |
1636 | |
1675 | |
|
|
1676 | #if 0 |
1637 | static int |
1677 | static int |
1638 | coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) |
1678 | coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) |
1639 | { |
1679 | { |
1640 | AV *padlist; |
1680 | AV *padlist; |
1641 | AV *av = (AV *)mg->mg_obj; |
1681 | AV *av = (AV *)mg->mg_obj; |
… | |
… | |
1647 | |
1687 | |
1648 | static MGVTBL coro_gensub_vtbl = { |
1688 | static MGVTBL coro_gensub_vtbl = { |
1649 | 0, 0, 0, 0, |
1689 | 0, 0, 0, 0, |
1650 | coro_gensub_free |
1690 | coro_gensub_free |
1651 | }; |
1691 | }; |
|
|
1692 | #endif |
1652 | |
1693 | |
1653 | /*****************************************************************************/ |
1694 | /*****************************************************************************/ |
1654 | /* PerlIO::cede */ |
1695 | /* PerlIO::cede */ |
1655 | |
1696 | |
1656 | typedef struct |
1697 | typedef struct |
… | |
… | |
1688 | { |
1729 | { |
1689 | api_cede (); |
1730 | api_cede (); |
1690 | self->next = now + self->every; |
1731 | self->next = now + self->every; |
1691 | } |
1732 | } |
1692 | |
1733 | |
1693 | return PerlIOBuf_flush (f); |
1734 | return PerlIOBuf_flush (aTHX_ f); |
1694 | } |
1735 | } |
1695 | |
1736 | |
1696 | static PerlIO_funcs PerlIO_cede = |
1737 | static PerlIO_funcs PerlIO_cede = |
1697 | { |
1738 | { |
1698 | sizeof(PerlIO_funcs), |
1739 | sizeof(PerlIO_funcs), |
… | |
… | |
1731 | PROTOTYPES: DISABLE |
1772 | PROTOTYPES: DISABLE |
1732 | |
1773 | |
1733 | BOOT: |
1774 | BOOT: |
1734 | { |
1775 | { |
1735 | #ifdef USE_ITHREADS |
1776 | #ifdef USE_ITHREADS |
1736 | MUTEX_INIT (&coro_mutex); |
1777 | MUTEX_INIT (&coro_lock); |
|
|
1778 | # if CORO_PTHREAD |
|
|
1779 | coro_thx = PERL_GET_CONTEXT; |
|
|
1780 | # endif |
1737 | #endif |
1781 | #endif |
1738 | BOOT_PAGESIZE; |
1782 | BOOT_PAGESIZE; |
1739 | |
1783 | |
1740 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1784 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1741 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1785 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
… | |
… | |
1828 | ta.next = 0; |
1872 | ta.next = 0; |
1829 | break; |
1873 | break; |
1830 | |
1874 | |
1831 | case 1: |
1875 | case 1: |
1832 | if (items != 2) |
1876 | if (items != 2) |
1833 | 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); |
1834 | |
1878 | |
1835 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1879 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1836 | break; |
1880 | break; |
1837 | |
1881 | |
1838 | case 2: |
1882 | case 2: |
… | |
… | |
1872 | _exit (code); |
1916 | _exit (code); |
1873 | |
1917 | |
1874 | int |
1918 | int |
1875 | cctx_stacksize (int new_stacksize = 0) |
1919 | cctx_stacksize (int new_stacksize = 0) |
1876 | CODE: |
1920 | CODE: |
1877 | RETVAL = coro_stacksize; |
1921 | RETVAL = cctx_stacksize; |
1878 | if (new_stacksize) |
1922 | if (new_stacksize) |
|
|
1923 | { |
1879 | 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; |
1880 | OUTPUT: |
1936 | OUTPUT: |
1881 | RETVAL |
1937 | RETVAL |
1882 | |
1938 | |
1883 | int |
1939 | int |
1884 | cctx_count () |
1940 | cctx_count () |
… | |
… | |
1980 | CODE: |
2036 | CODE: |
1981 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
2037 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
1982 | OUTPUT: |
2038 | OUTPUT: |
1983 | RETVAL |
2039 | RETVAL |
1984 | |
2040 | |
1985 | IV |
2041 | UV |
1986 | rss (Coro::State coro) |
2042 | rss (Coro::State coro) |
1987 | PROTOTYPE: $ |
2043 | PROTOTYPE: $ |
1988 | ALIAS: |
2044 | ALIAS: |
1989 | usecount = 1 |
2045 | usecount = 1 |
1990 | CODE: |
2046 | CODE: |
… | |
… | |
2001 | CODE: |
2057 | CODE: |
2002 | struct coro *coro = SvSTATE (coro_current); |
2058 | struct coro *coro = SvSTATE (coro_current); |
2003 | coro->cctx->idle_sp = 0; |
2059 | coro->cctx->idle_sp = 0; |
2004 | |
2060 | |
2005 | void |
2061 | 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) |
2062 | swap_defsv (Coro::State self) |
2014 | PROTOTYPE: $ |
2063 | PROTOTYPE: $ |
2015 | ALIAS: |
2064 | ALIAS: |
2016 | swap_defav = 1 |
2065 | swap_defav = 1 |
2017 | CODE: |
2066 | CODE: |
… | |
… | |
2071 | void |
2120 | void |
2072 | _set_current (SV *current) |
2121 | _set_current (SV *current) |
2073 | PROTOTYPE: $ |
2122 | PROTOTYPE: $ |
2074 | CODE: |
2123 | CODE: |
2075 | SvREFCNT_dec (SvRV (coro_current)); |
2124 | SvREFCNT_dec (SvRV (coro_current)); |
2076 | SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); |
2125 | SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); |
2077 | |
2126 | |
2078 | void |
2127 | void |
2079 | _set_readyhook (SV *hook) |
2128 | _set_readyhook (SV *hook) |
2080 | PROTOTYPE: $ |
2129 | PROTOTYPE: $ |
2081 | CODE: |
2130 | CODE: |
… | |
… | |
2120 | CODE: |
2169 | CODE: |
2121 | RETVAL = coro_nready; |
2170 | RETVAL = coro_nready; |
2122 | OUTPUT: |
2171 | OUTPUT: |
2123 | RETVAL |
2172 | RETVAL |
2124 | |
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 | |
2125 | # for async_pool speedup |
2181 | # for async_pool speedup |
2126 | void |
2182 | void |
2127 | _pool_1 (SV *cb) |
2183 | _pool_1 (SV *cb) |
2128 | CODE: |
2184 | CODE: |
2129 | { |
2185 | { |
… | |
… | |
2141 | SvREFCNT_dec (old); |
2197 | SvREFCNT_dec (old); |
2142 | croak ("\3async_pool terminate\2\n"); |
2198 | croak ("\3async_pool terminate\2\n"); |
2143 | } |
2199 | } |
2144 | |
2200 | |
2145 | SvREFCNT_dec (coro->saved_deffh); |
2201 | SvREFCNT_dec (coro->saved_deffh); |
2146 | coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); |
2202 | coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv); |
2147 | |
2203 | |
2148 | hv_store (hv, "desc", sizeof ("desc") - 1, |
2204 | hv_store (hv, "desc", sizeof ("desc") - 1, |
2149 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
2205 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
2150 | |
2206 | |
2151 | invoke_av = (AV *)SvRV (invoke); |
2207 | invoke_av = (AV *)SvRV (invoke); |
… | |
… | |
2155 | |
2211 | |
2156 | if (len > 0) |
2212 | if (len > 0) |
2157 | { |
2213 | { |
2158 | av_fill (defav, len - 1); |
2214 | av_fill (defav, len - 1); |
2159 | for (i = 0; i < len; ++i) |
2215 | for (i = 0; i < len; ++i) |
2160 | av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); |
2216 | av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); |
2161 | } |
2217 | } |
2162 | |
2218 | |
2163 | SvREFCNT_dec (invoke); |
2219 | SvREFCNT_dec (invoke); |
2164 | } |
2220 | } |
2165 | |
2221 | |
… | |
… | |
2172 | sv_setsv (cb, &PL_sv_undef); |
2228 | sv_setsv (cb, &PL_sv_undef); |
2173 | |
2229 | |
2174 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2230 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2175 | coro->saved_deffh = 0; |
2231 | coro->saved_deffh = 0; |
2176 | |
2232 | |
2177 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
2233 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
2178 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
2234 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
2179 | { |
2235 | { |
2180 | SV *old = PL_diehook; |
2236 | SV *old = PL_diehook; |
2181 | PL_diehook = 0; |
2237 | PL_diehook = 0; |
2182 | SvREFCNT_dec (old); |
2238 | SvREFCNT_dec (old); |
… | |
… | |
2244 | #endif |
2300 | #endif |
2245 | |
2301 | |
2246 | |
2302 | |
2247 | MODULE = Coro::State PACKAGE = Coro::AIO |
2303 | MODULE = Coro::State PACKAGE = Coro::AIO |
2248 | |
2304 | |
2249 | SV * |
2305 | void |
2250 | _get_state () |
2306 | _get_state (SV *self) |
2251 | CODE: |
2307 | PPCODE: |
2252 | { |
2308 | { |
2253 | struct io_state *data; |
2309 | AV *defav = GvAV (PL_defgv); |
2254 | |
2310 | AV *av = newAV (); |
|
|
2311 | int i; |
2255 | RETVAL = newSV (sizeof (struct io_state)); |
2312 | SV *data_sv = newSV (sizeof (struct io_state)); |
2256 | data = (struct io_state *)SvPVX (RETVAL); |
2313 | struct io_state *data = (struct io_state *)SvPVX (data_sv); |
2257 | SvCUR_set (RETVAL, sizeof (struct io_state)); |
2314 | SvCUR_set (data_sv, sizeof (struct io_state)); |
2258 | SvPOK_only (RETVAL); |
2315 | SvPOK_only (data_sv); |
2259 | |
2316 | |
2260 | data->errorno = errno; |
2317 | data->errorno = errno; |
2261 | data->laststype = PL_laststype; |
2318 | data->laststype = PL_laststype; |
2262 | data->laststatval = PL_laststatval; |
2319 | data->laststatval = PL_laststatval; |
2263 | data->statcache = PL_statcache; |
2320 | data->statcache = PL_statcache; |
|
|
2321 | |
|
|
2322 | av_extend (av, AvFILLp (defav) + 1 + 1); |
|
|
2323 | |
|
|
2324 | for (i = 0; i <= AvFILLp (defav); ++i) |
|
|
2325 | av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i])); |
|
|
2326 | |
|
|
2327 | av_push (av, data_sv); |
|
|
2328 | |
|
|
2329 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
|
|
2330 | |
|
|
2331 | api_ready (self); |
2264 | } |
2332 | } |
2265 | OUTPUT: |
|
|
2266 | RETVAL |
|
|
2267 | |
2333 | |
2268 | void |
2334 | void |
2269 | _set_state (char *data_) |
2335 | _set_state (SV *state) |
2270 | PROTOTYPE: $ |
2336 | PROTOTYPE: $ |
2271 | CODE: |
2337 | PPCODE: |
2272 | { |
2338 | { |
2273 | struct io_state *data = (void *)data_; |
2339 | AV *av = (AV *)SvRV (state); |
|
|
2340 | struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]); |
|
|
2341 | int i; |
2274 | |
2342 | |
2275 | errno = data->errorno; |
2343 | errno = data->errorno; |
2276 | PL_laststype = data->laststype; |
2344 | PL_laststype = data->laststype; |
2277 | PL_laststatval = data->laststatval; |
2345 | PL_laststatval = data->laststatval; |
2278 | PL_statcache = data->statcache; |
2346 | PL_statcache = data->statcache; |
|
|
2347 | |
|
|
2348 | EXTEND (SP, AvFILLp (av)); |
|
|
2349 | for (i = 0; i < AvFILLp (av); ++i) |
|
|
2350 | PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i]))); |
2279 | } |
2351 | } |
2280 | |
2352 | |
2281 | |
2353 | |
2282 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2354 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2283 | |
2355 | |