… | |
… | |
4 | #define PERL_EXT |
4 | #define PERL_EXT |
5 | |
5 | |
6 | #include "EXTERN.h" |
6 | #include "EXTERN.h" |
7 | #include "perl.h" |
7 | #include "perl.h" |
8 | #include "XSUB.h" |
8 | #include "XSUB.h" |
|
|
9 | #include "perliol.h" |
9 | |
10 | |
10 | #include "patchlevel.h" |
11 | #include "patchlevel.h" |
11 | |
12 | |
12 | #include <stdio.h> |
13 | #include <stdio.h> |
13 | #include <errno.h> |
14 | #include <errno.h> |
… | |
… | |
45 | # define BOOT_PAGESIZE (void)0 |
46 | # define BOOT_PAGESIZE (void)0 |
46 | #endif |
47 | #endif |
47 | |
48 | |
48 | #if CORO_USE_VALGRIND |
49 | #if CORO_USE_VALGRIND |
49 | # include <valgrind/valgrind.h> |
50 | # include <valgrind/valgrind.h> |
50 | # define REGISTER_STACK(cctx,start,end) (cctx)->valgrind_id = VALGRIND_STACK_REGISTER ((start), (end)) |
|
|
51 | #else |
|
|
52 | # define REGISTER_STACK(cctx,start,end) |
|
|
53 | #endif |
51 | #endif |
54 | |
52 | |
55 | /* the maximum number of idle cctx that will be pooled */ |
53 | /* the maximum number of idle cctx that will be pooled */ |
56 | #define MAX_IDLE_CCTX 8 |
54 | static int cctx_max_idle = 4; |
57 | |
55 | |
58 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
56 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
59 | (PERL_REVISION > (a) \ |
57 | (PERL_REVISION > (a) \ |
60 | || (PERL_REVISION == (a) \ |
58 | || (PERL_REVISION == (a) \ |
61 | && (PERL_VERSION > (b) \ |
59 | && (PERL_VERSION > (b) \ |
… | |
… | |
80 | # ifndef IS_PADCONST |
78 | # ifndef IS_PADCONST |
81 | # define IS_PADCONST(v) 0 |
79 | # define IS_PADCONST(v) 0 |
82 | # endif |
80 | # endif |
83 | #endif |
81 | #endif |
84 | |
82 | |
|
|
83 | /* 5.11 */ |
|
|
84 | #ifndef CxHASARGS |
|
|
85 | # define CxHASARGS(cx) (cx)->blk_sub.hasargs |
|
|
86 | #endif |
|
|
87 | |
|
|
88 | /* 5.10.0 */ |
|
|
89 | #ifndef SvREFCNT_inc_NN |
|
|
90 | # define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv) |
|
|
91 | #endif |
|
|
92 | |
85 | /* 5.8.8 */ |
93 | /* 5.8.8 */ |
86 | #ifndef GV_NOTQUAL |
94 | #ifndef GV_NOTQUAL |
87 | # define GV_NOTQUAL 0 |
95 | # define GV_NOTQUAL 0 |
88 | #endif |
96 | #endif |
89 | #ifndef newSV |
97 | #ifndef newSV |
90 | # define newSV(l) NEWSV(0,l) |
98 | # define newSV(l) NEWSV(0,l) |
91 | #endif |
|
|
92 | |
|
|
93 | /* 5.11 */ |
|
|
94 | #ifndef CxHASARGS |
|
|
95 | # define CxHASARGS(cx) (cx)->blk_sub.hasargs |
|
|
96 | #endif |
99 | #endif |
97 | |
100 | |
98 | /* 5.8.7 */ |
101 | /* 5.8.7 */ |
99 | #ifndef SvRV_set |
102 | #ifndef SvRV_set |
100 | # define SvRV_set(s,v) SvRV(s) = (v) |
103 | # define SvRV_set(s,v) SvRV(s) = (v) |
… | |
… | |
120 | |
123 | |
121 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
124 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
122 | |
125 | |
123 | #if __GNUC__ >= 3 |
126 | #if __GNUC__ >= 3 |
124 | # define attribute(x) __attribute__(x) |
127 | # define attribute(x) __attribute__(x) |
125 | # define BARRIER __asm__ __volatile__ ("" : : : "memory") |
|
|
126 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
128 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
129 | # define INLINE static inline |
127 | #else |
130 | #else |
128 | # define attribute(x) |
131 | # define attribute(x) |
129 | # define BARRIER |
|
|
130 | # define expect(expr,value) (expr) |
132 | # define expect(expr,value) (expr) |
|
|
133 | # define INLINE static |
131 | #endif |
134 | #endif |
132 | |
135 | |
133 | #define expect_false(expr) expect ((expr) != 0, 0) |
136 | #define expect_false(expr) expect ((expr) != 0, 0) |
134 | #define expect_true(expr) expect ((expr) != 0, 1) |
137 | #define expect_true(expr) expect ((expr) != 0, 1) |
135 | |
138 | |
136 | #define NOINLINE attribute ((noinline)) |
139 | #define NOINLINE attribute ((noinline)) |
137 | |
140 | |
138 | #include "CoroAPI.h" |
141 | #include "CoroAPI.h" |
139 | |
142 | |
140 | #ifdef USE_ITHREADS |
143 | #ifdef USE_ITHREADS |
|
|
144 | |
141 | static perl_mutex coro_mutex; |
145 | static perl_mutex coro_lock; |
142 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
146 | # define LOCK do { MUTEX_LOCK (&coro_lock); } while (0) |
143 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
147 | # define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0) |
|
|
148 | # if CORO_PTHREAD |
|
|
149 | static void *coro_thx; |
|
|
150 | # endif |
|
|
151 | |
144 | #else |
152 | #else |
|
|
153 | |
145 | # define LOCK (void)0 |
154 | # define LOCK (void)0 |
146 | # define UNLOCK (void)0 |
155 | # define UNLOCK (void)0 |
|
|
156 | |
147 | #endif |
157 | #endif |
|
|
158 | |
|
|
159 | # undef LOCK |
|
|
160 | # define LOCK (void)0 |
|
|
161 | # undef UNLOCK |
|
|
162 | # define UNLOCK (void)0 |
148 | |
163 | |
149 | /* helper storage struct for Coro::AIO */ |
164 | /* helper storage struct for Coro::AIO */ |
150 | struct io_state |
165 | struct io_state |
151 | { |
166 | { |
|
|
167 | AV *res; |
152 | int errorno; |
168 | int errorno; |
153 | I32 laststype; |
169 | I32 laststype; /* U16 in 5.10.0 */ |
154 | int laststatval; |
170 | int laststatval; |
155 | Stat_t statcache; |
171 | Stat_t statcache; |
156 | }; |
172 | }; |
157 | |
173 | |
|
|
174 | static double (*nvtime)(); /* so why doesn't it take void? */ |
|
|
175 | |
|
|
176 | static U32 cctx_gen; |
158 | static size_t coro_stacksize = CORO_STACKSIZE; |
177 | static size_t cctx_stacksize = CORO_STACKSIZE; |
159 | static struct CoroAPI coroapi; |
178 | static struct CoroAPI coroapi; |
160 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
179 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
161 | static JMPENV *main_top_env; |
180 | static JMPENV *main_top_env; |
162 | static HV *coro_state_stash, *coro_stash; |
181 | static HV *coro_state_stash, *coro_stash; |
163 | static volatile SV *coro_mortal; /* will be freed after next transfer */ |
182 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
|
|
183 | static volatile struct coro *transfer_next; |
|
|
184 | |
|
|
185 | struct transfer_args |
|
|
186 | { |
|
|
187 | struct coro *prev, *next; |
|
|
188 | }; |
164 | |
189 | |
165 | static GV *irsgv; /* $/ */ |
190 | static GV *irsgv; /* $/ */ |
166 | static GV *stdoutgv; /* *STDOUT */ |
191 | static GV *stdoutgv; /* *STDOUT */ |
167 | static SV *rv_diehook; |
192 | static SV *rv_diehook; |
168 | static SV *rv_warnhook; |
193 | static SV *rv_warnhook; |
… | |
… | |
200 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
225 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
201 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
226 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
202 | JMPENV *top_env; |
227 | JMPENV *top_env; |
203 | coro_context cctx; |
228 | coro_context cctx; |
204 | |
229 | |
|
|
230 | U32 gen; |
205 | #if CORO_USE_VALGRIND |
231 | #if CORO_USE_VALGRIND |
206 | int valgrind_id; |
232 | int valgrind_id; |
207 | #endif |
233 | #endif |
208 | unsigned char flags; |
234 | unsigned char flags; |
209 | } coro_cctx; |
235 | } coro_cctx; |
… | |
… | |
269 | #define PRIO_MIN -4 |
295 | #define PRIO_MIN -4 |
270 | |
296 | |
271 | /* for Coro.pm */ |
297 | /* for Coro.pm */ |
272 | static SV *coro_current; |
298 | static SV *coro_current; |
273 | static SV *coro_readyhook; |
299 | static SV *coro_readyhook; |
274 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
300 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
275 | static int coro_nready; |
301 | static int coro_nready; |
276 | static struct coro *coro_first; |
302 | static struct coro *coro_first; |
277 | |
303 | |
278 | /** lowlevel stuff **********************************************************/ |
304 | /** lowlevel stuff **********************************************************/ |
279 | |
305 | |
… | |
… | |
321 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
347 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
322 | #endif |
348 | #endif |
323 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
349 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
324 | --AvFILLp (padlist); |
350 | --AvFILLp (padlist); |
325 | |
351 | |
326 | av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); |
352 | av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); |
327 | av_store (newpadlist, 1, (SV *)newpad); |
353 | av_store (newpadlist, 1, (SV *)newpad); |
328 | |
354 | |
329 | return newpadlist; |
355 | return newpadlist; |
330 | } |
356 | } |
331 | |
357 | |
… | |
… | |
374 | static MGVTBL coro_cv_vtbl = { |
400 | static MGVTBL coro_cv_vtbl = { |
375 | 0, 0, 0, 0, |
401 | 0, 0, 0, 0, |
376 | coro_cv_free |
402 | coro_cv_free |
377 | }; |
403 | }; |
378 | |
404 | |
379 | #define CORO_MAGIC(sv,type) \ |
405 | #define CORO_MAGIC(sv, type) \ |
380 | SvMAGIC (sv) \ |
406 | SvMAGIC (sv) \ |
381 | ? SvMAGIC (sv)->mg_type == type \ |
407 | ? SvMAGIC (sv)->mg_type == type \ |
382 | ? SvMAGIC (sv) \ |
408 | ? SvMAGIC (sv) \ |
383 | : mg_find (sv, type) \ |
409 | : mg_find (sv, type) \ |
384 | : 0 |
410 | : 0 |
385 | |
411 | |
386 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
412 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
387 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
413 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
388 | |
414 | |
389 | static struct coro * |
415 | INLINE struct coro * |
390 | SvSTATE_ (pTHX_ SV *coro) |
416 | SvSTATE_ (pTHX_ SV *coro) |
391 | { |
417 | { |
392 | HV *stash; |
418 | HV *stash; |
393 | MAGIC *mg; |
419 | MAGIC *mg; |
394 | |
420 | |
… | |
… | |
619 | |
645 | |
620 | /* |
646 | /* |
621 | * destroy the stacks, the callchain etc... |
647 | * destroy the stacks, the callchain etc... |
622 | */ |
648 | */ |
623 | static void |
649 | static void |
624 | coro_destroy_stacks (pTHX) |
650 | coro_destruct_stacks (pTHX) |
625 | { |
651 | { |
626 | while (PL_curstackinfo->si_next) |
652 | while (PL_curstackinfo->si_next) |
627 | PL_curstackinfo = PL_curstackinfo->si_next; |
653 | PL_curstackinfo = PL_curstackinfo->si_next; |
628 | |
654 | |
629 | while (PL_curstackinfo) |
655 | while (PL_curstackinfo) |
… | |
… | |
683 | #endif |
709 | #endif |
684 | } |
710 | } |
685 | } |
711 | } |
686 | |
712 | |
687 | return rss; |
713 | return rss; |
|
|
714 | } |
|
|
715 | |
|
|
716 | /** set stacklevel support **************************************************/ |
|
|
717 | |
|
|
718 | /* we sometimes need to create the effect of pp_set_stacklevel calling us */ |
|
|
719 | #define SSL_HEAD (void)0 |
|
|
720 | /* we somtimes need to create the effect of leaving via pp_set_stacklevel */ |
|
|
721 | #define SSL_TAIL set_stacklevel_tail (aTHX) |
|
|
722 | |
|
|
723 | INLINE void |
|
|
724 | set_stacklevel_tail (pTHX) |
|
|
725 | { |
|
|
726 | dSP; |
|
|
727 | SV **bot = SP; |
|
|
728 | |
|
|
729 | int gimme = GIMME_V; |
|
|
730 | |
|
|
731 | /* make sure we put something on the stack in scalar context */ |
|
|
732 | if (gimme == G_SCALAR) |
|
|
733 | { |
|
|
734 | if (sp == bot) |
|
|
735 | XPUSHs (&PL_sv_undef); |
|
|
736 | |
|
|
737 | SP = bot + 1; |
|
|
738 | } |
|
|
739 | |
|
|
740 | PUTBACK; |
688 | } |
741 | } |
689 | |
742 | |
690 | /** coroutine stack handling ************************************************/ |
743 | /** coroutine stack handling ************************************************/ |
691 | |
744 | |
692 | static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); |
745 | static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); |
… | |
… | |
807 | GvSV (PL_defgv) = newSV (0); |
860 | GvSV (PL_defgv) = newSV (0); |
808 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
861 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
809 | GvSV (PL_errgv) = newSV (0); |
862 | GvSV (PL_errgv) = newSV (0); |
810 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
863 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
811 | PL_rs = newSVsv (GvSV (irsgv)); |
864 | PL_rs = newSVsv (GvSV (irsgv)); |
812 | PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); |
865 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
813 | |
866 | |
814 | { |
867 | { |
815 | dSP; |
868 | dSP; |
816 | LOGOP myop; |
869 | UNOP myop; |
817 | |
870 | |
818 | Zero (&myop, 1, LOGOP); |
871 | Zero (&myop, 1, UNOP); |
819 | myop.op_next = Nullop; |
872 | myop.op_next = Nullop; |
820 | myop.op_flags = OPf_WANT_VOID; |
873 | myop.op_flags = OPf_WANT_VOID; |
821 | |
874 | |
822 | PUSHMARK (SP); |
875 | PUSHMARK (SP); |
823 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
876 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
… | |
… | |
826 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
879 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
827 | SPAGAIN; |
880 | SPAGAIN; |
828 | } |
881 | } |
829 | |
882 | |
830 | /* this newly created coroutine might be run on an existing cctx which most |
883 | /* this newly created coroutine might be run on an existing cctx which most |
831 | * likely was suspended in set_stacklevel, called from entersub. |
884 | * likely was suspended in set_stacklevel, called from pp_set_stacklevel, |
832 | * set_stacklevl doesn't do anything on return, but entersub does LEAVE, |
885 | * so we have to emulate entering pp_set_stacklevel here. |
833 | * so we ENTER here for symmetry |
|
|
834 | */ |
886 | */ |
835 | ENTER; |
887 | SSL_HEAD; |
836 | } |
888 | } |
837 | |
889 | |
838 | static void |
890 | static void |
839 | coro_destroy (pTHX_ struct coro *coro) |
891 | coro_destruct (pTHX_ struct coro *coro) |
840 | { |
892 | { |
841 | if (!IN_DESTRUCT) |
893 | if (!IN_DESTRUCT) |
842 | { |
894 | { |
843 | /* restore all saved variables and stuff */ |
895 | /* restore all saved variables and stuff */ |
844 | LEAVE_SCOPE (0); |
896 | LEAVE_SCOPE (0); |
… | |
… | |
866 | SvREFCNT_dec (PL_warnhook); |
918 | SvREFCNT_dec (PL_warnhook); |
867 | |
919 | |
868 | SvREFCNT_dec (coro->saved_deffh); |
920 | SvREFCNT_dec (coro->saved_deffh); |
869 | SvREFCNT_dec (coro->throw); |
921 | SvREFCNT_dec (coro->throw); |
870 | |
922 | |
871 | coro_destroy_stacks (aTHX); |
923 | coro_destruct_stacks (aTHX); |
872 | } |
924 | } |
873 | |
925 | |
874 | static void |
926 | INLINE void |
875 | free_coro_mortal (pTHX) |
927 | free_coro_mortal (pTHX) |
876 | { |
928 | { |
877 | if (expect_true (coro_mortal)) |
929 | if (expect_true (coro_mortal)) |
878 | { |
930 | { |
879 | SvREFCNT_dec (coro_mortal); |
931 | SvREFCNT_dec (coro_mortal); |
… | |
… | |
913 | : cx->blk_gimme == G_SCALAR ? bot + 1 |
965 | : cx->blk_gimme == G_SCALAR ? bot + 1 |
914 | : bot; |
966 | : bot; |
915 | |
967 | |
916 | av_extend (av, top - bot); |
968 | av_extend (av, top - bot); |
917 | while (bot < top) |
969 | while (bot < top) |
918 | av_push (av, SvREFCNT_inc (*bot++)); |
970 | av_push (av, SvREFCNT_inc_NN (*bot++)); |
919 | |
971 | |
920 | PL_runops = RUNOPS_DEFAULT; |
972 | PL_runops = RUNOPS_DEFAULT; |
921 | ENTER; |
973 | ENTER; |
922 | SAVETMPS; |
974 | SAVETMPS; |
923 | EXTEND (SP, 3); |
975 | EXTEND (SP, 3); |
… | |
… | |
1003 | |
1055 | |
1004 | TAINT_NOT; |
1056 | TAINT_NOT; |
1005 | return 0; |
1057 | return 0; |
1006 | } |
1058 | } |
1007 | |
1059 | |
|
|
1060 | static void |
|
|
1061 | prepare_set_stacklevel (struct transfer_args *ta, struct coro_cctx *cctx) |
|
|
1062 | { |
|
|
1063 | ta->prev = (struct coro *)cctx; |
|
|
1064 | ta->next = 0; |
|
|
1065 | } |
|
|
1066 | |
1008 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
1067 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
1009 | /* _cctx_init should be careful, as it could be called at almost any time */ |
1068 | /* _cctx_init should be careful, as it could be called at almost any time */ |
1010 | /* during execution of a perl program */ |
1069 | /* during execution of a perl program */ |
|
|
1070 | /* also initialises PL_top_env */ |
1011 | static void NOINLINE |
1071 | static void NOINLINE |
1012 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1072 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1013 | { |
1073 | { |
1014 | dSP; |
1074 | dSP; |
1015 | LOGOP myop; |
1075 | UNOP myop; |
1016 | |
1076 | |
1017 | PL_top_env = &PL_start_env; |
1077 | PL_top_env = &PL_start_env; |
1018 | |
1078 | |
1019 | if (cctx->flags & CC_TRACE) |
1079 | if (cctx->flags & CC_TRACE) |
1020 | PL_runops = runops_trace; |
1080 | PL_runops = runops_trace; |
1021 | |
1081 | |
1022 | Zero (&myop, 1, LOGOP); |
1082 | Zero (&myop, 1, UNOP); |
1023 | myop.op_next = PL_op; |
1083 | myop.op_next = PL_op; |
1024 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
1084 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
1025 | |
1085 | |
1026 | PUSHMARK (SP); |
1086 | PUSHMARK (SP); |
1027 | EXTEND (SP, 2); |
1087 | EXTEND (SP, 2); |
1028 | PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); |
1088 | PUSHs (sv_2mortal (newSViv ((IV)cctx))); |
1029 | PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
1089 | PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
1030 | PUTBACK; |
1090 | PUTBACK; |
1031 | PL_op = (OP *)&myop; |
1091 | PL_op = (OP *)&myop; |
1032 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
1092 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
1033 | SPAGAIN; |
1093 | SPAGAIN; |
1034 | } |
1094 | } |
1035 | |
1095 | |
|
|
1096 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
|
|
1097 | INLINE void |
|
|
1098 | transfer_tail (pTHX) |
|
|
1099 | { |
|
|
1100 | struct coro *next = (struct coro *)transfer_next; |
|
|
1101 | assert (!(transfer_next = 0)); /* just used for the side effect when asserts are enabled */ |
|
|
1102 | assert (("FATAL: next coroutine was zero in transfer_tail (please report)", next)); |
|
|
1103 | |
|
|
1104 | free_coro_mortal (aTHX); |
|
|
1105 | UNLOCK; |
|
|
1106 | |
|
|
1107 | if (expect_false (next->throw)) |
|
|
1108 | { |
|
|
1109 | SV *exception = sv_2mortal (next->throw); |
|
|
1110 | |
|
|
1111 | next->throw = 0; |
|
|
1112 | sv_setsv (ERRSV, exception); |
|
|
1113 | croak (0); |
|
|
1114 | } |
|
|
1115 | } |
|
|
1116 | |
1036 | /* |
1117 | /* |
1037 | * this is a _very_ stripped down perl interpreter ;) |
1118 | * this is a _very_ stripped down perl interpreter ;) |
1038 | */ |
1119 | */ |
1039 | static void |
1120 | static void |
1040 | cctx_run (void *arg) |
1121 | cctx_run (void *arg) |
1041 | { |
1122 | { |
|
|
1123 | #ifdef USE_ITHREADS |
|
|
1124 | # if CORO_PTHREAD |
|
|
1125 | PERL_SET_CONTEXT (coro_thx); |
|
|
1126 | # endif |
|
|
1127 | #endif |
|
|
1128 | { |
1042 | dTHX; |
1129 | dTHX; |
1043 | |
1130 | |
1044 | /* cctx_run is the alternative tail of transfer(), so unlock here. */ |
1131 | /* we are the alternative tail to pp_set_stacklevel */ |
1045 | UNLOCK; |
1132 | /* so do the same things here */ |
|
|
1133 | SSL_TAIL; |
1046 | |
1134 | |
1047 | /* we now skip the entersub that lead to transfer() */ |
1135 | /* we now skip the op that did lead to transfer() */ |
1048 | PL_op = PL_op->op_next; |
1136 | PL_op = PL_op->op_next; |
1049 | |
1137 | |
1050 | /* inject a fake subroutine call to cctx_init */ |
1138 | /* inject a fake subroutine call to cctx_init */ |
1051 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1139 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1052 | |
1140 | |
|
|
1141 | /* cctx_run is the alternative tail of transfer() */ |
|
|
1142 | transfer_tail (aTHX); |
|
|
1143 | |
1053 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1144 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1054 | PL_restartop = PL_op; |
1145 | PL_restartop = PL_op; |
1055 | perl_run (PL_curinterp); |
1146 | perl_run (PL_curinterp); |
1056 | |
1147 | |
1057 | /* |
1148 | /* |
1058 | * If perl-run returns we assume exit() was being called or the coro |
1149 | * If perl-run returns we assume exit() was being called or the coro |
1059 | * fell off the end, which seems to be the only valid (non-bug) |
1150 | * fell off the end, which seems to be the only valid (non-bug) |
1060 | * reason for perl_run to return. We try to exit by jumping to the |
1151 | * reason for perl_run to return. We try to exit by jumping to the |
1061 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1152 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1062 | * coroutine as Coro has no such concept |
1153 | * coroutine as Coro has no such concept |
1063 | */ |
1154 | */ |
1064 | PL_top_env = main_top_env; |
1155 | PL_top_env = main_top_env; |
1065 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1156 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
|
|
1157 | } |
1066 | } |
1158 | } |
1067 | |
1159 | |
1068 | static coro_cctx * |
1160 | static coro_cctx * |
1069 | cctx_new () |
1161 | cctx_new () |
1070 | { |
1162 | { |
1071 | coro_cctx *cctx; |
1163 | coro_cctx *cctx; |
|
|
1164 | |
|
|
1165 | ++cctx_count; |
|
|
1166 | New (0, cctx, 1, coro_cctx); |
|
|
1167 | |
|
|
1168 | cctx->gen = cctx_gen; |
|
|
1169 | cctx->flags = 0; |
|
|
1170 | cctx->idle_sp = 0; /* can be accessed by transfer between cctx_run and set_stacklevel, on throw */ |
|
|
1171 | |
|
|
1172 | return cctx; |
|
|
1173 | } |
|
|
1174 | |
|
|
1175 | /* create a new cctx only suitable as source */ |
|
|
1176 | static coro_cctx * |
|
|
1177 | cctx_new_empty () |
|
|
1178 | { |
|
|
1179 | coro_cctx *cctx = cctx_new (); |
|
|
1180 | |
|
|
1181 | cctx->sptr = 0; |
|
|
1182 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
|
|
1183 | |
|
|
1184 | return cctx; |
|
|
1185 | } |
|
|
1186 | |
|
|
1187 | /* create a new cctx suitable as destination/running a perl interpreter */ |
|
|
1188 | static coro_cctx * |
|
|
1189 | cctx_new_run () |
|
|
1190 | { |
|
|
1191 | coro_cctx *cctx = cctx_new (); |
1072 | void *stack_start; |
1192 | void *stack_start; |
1073 | size_t stack_size; |
1193 | size_t stack_size; |
1074 | |
1194 | |
1075 | ++cctx_count; |
|
|
1076 | |
|
|
1077 | Newz (0, cctx, 1, coro_cctx); |
|
|
1078 | |
|
|
1079 | #if HAVE_MMAP |
1195 | #if HAVE_MMAP |
1080 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1196 | cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1081 | /* mmap supposedly does allocate-on-write for us */ |
1197 | /* mmap supposedly does allocate-on-write for us */ |
1082 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
1198 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
1083 | |
1199 | |
1084 | if (cctx->sptr != (void *)-1) |
1200 | if (cctx->sptr != (void *)-1) |
1085 | { |
1201 | { |
1086 | # if CORO_STACKGUARD |
1202 | #if CORO_STACKGUARD |
1087 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
1203 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
1088 | # endif |
1204 | #endif |
1089 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
1205 | stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE; |
1090 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
1206 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
1091 | cctx->flags |= CC_MAPPED; |
1207 | cctx->flags |= CC_MAPPED; |
1092 | } |
1208 | } |
1093 | else |
1209 | else |
1094 | #endif |
1210 | #endif |
1095 | { |
1211 | { |
1096 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
1212 | cctx->ssize = cctx_stacksize * (long)sizeof (long); |
1097 | New (0, cctx->sptr, coro_stacksize, long); |
1213 | New (0, cctx->sptr, cctx_stacksize, long); |
1098 | |
1214 | |
1099 | if (!cctx->sptr) |
1215 | if (!cctx->sptr) |
1100 | { |
1216 | { |
1101 | perror ("FATAL: unable to allocate stack for coroutine"); |
1217 | perror ("FATAL: unable to allocate stack for coroutine, exiting."); |
1102 | _exit (EXIT_FAILURE); |
1218 | _exit (EXIT_FAILURE); |
1103 | } |
1219 | } |
1104 | |
1220 | |
1105 | stack_start = cctx->sptr; |
1221 | stack_start = cctx->sptr; |
1106 | stack_size = cctx->ssize; |
1222 | stack_size = cctx->ssize; |
1107 | } |
1223 | } |
1108 | |
1224 | |
1109 | REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); |
1225 | #if CORO_USE_VALGRIND |
|
|
1226 | cctx->valgrind_id = VALGRIND_STACK_REGISTER ((char *)stack_start, (char *)stack_start + stack_size); |
|
|
1227 | #endif |
|
|
1228 | |
1110 | coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); |
1229 | coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); |
1111 | |
1230 | |
1112 | return cctx; |
1231 | return cctx; |
1113 | } |
1232 | } |
1114 | |
1233 | |
… | |
… | |
1117 | { |
1236 | { |
1118 | if (!cctx) |
1237 | if (!cctx) |
1119 | return; |
1238 | return; |
1120 | |
1239 | |
1121 | --cctx_count; |
1240 | --cctx_count; |
|
|
1241 | coro_destroy (&cctx->cctx); |
1122 | |
1242 | |
|
|
1243 | /* coro_transfer creates new, empty cctx's */ |
|
|
1244 | if (cctx->sptr) |
|
|
1245 | { |
1123 | #if CORO_USE_VALGRIND |
1246 | #if CORO_USE_VALGRIND |
1124 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1247 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1125 | #endif |
1248 | #endif |
1126 | |
1249 | |
1127 | #if HAVE_MMAP |
1250 | #if HAVE_MMAP |
1128 | if (cctx->flags & CC_MAPPED) |
1251 | if (cctx->flags & CC_MAPPED) |
1129 | munmap (cctx->sptr, cctx->ssize); |
1252 | munmap (cctx->sptr, cctx->ssize); |
1130 | else |
1253 | else |
1131 | #endif |
1254 | #endif |
1132 | Safefree (cctx->sptr); |
1255 | Safefree (cctx->sptr); |
|
|
1256 | } |
1133 | |
1257 | |
1134 | Safefree (cctx); |
1258 | Safefree (cctx); |
1135 | } |
1259 | } |
1136 | |
1260 | |
1137 | /* wether this cctx should be destructed */ |
1261 | /* wether this cctx should be destructed */ |
1138 | #define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) |
1262 | #define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE)) |
1139 | |
1263 | |
1140 | static coro_cctx * |
1264 | static coro_cctx * |
1141 | cctx_get (pTHX) |
1265 | cctx_get (pTHX) |
1142 | { |
1266 | { |
1143 | while (expect_true (cctx_first)) |
1267 | while (expect_true (cctx_first)) |
… | |
… | |
1150 | return cctx; |
1274 | return cctx; |
1151 | |
1275 | |
1152 | cctx_destroy (cctx); |
1276 | cctx_destroy (cctx); |
1153 | } |
1277 | } |
1154 | |
1278 | |
1155 | return cctx_new (); |
1279 | return cctx_new_run (); |
1156 | } |
1280 | } |
1157 | |
1281 | |
1158 | static void |
1282 | static void |
1159 | cctx_put (coro_cctx *cctx) |
1283 | cctx_put (coro_cctx *cctx) |
1160 | { |
1284 | { |
|
|
1285 | assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr)); |
|
|
1286 | |
1161 | /* free another cctx if overlimit */ |
1287 | /* free another cctx if overlimit */ |
1162 | if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) |
1288 | if (expect_false (cctx_idle >= cctx_max_idle)) |
1163 | { |
1289 | { |
1164 | coro_cctx *first = cctx_first; |
1290 | coro_cctx *first = cctx_first; |
1165 | cctx_first = first->next; |
1291 | cctx_first = first->next; |
1166 | --cctx_idle; |
1292 | --cctx_idle; |
1167 | |
1293 | |
… | |
… | |
1199 | /* always use the TRANSFER macro */ |
1325 | /* always use the TRANSFER macro */ |
1200 | static void NOINLINE |
1326 | static void NOINLINE |
1201 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1327 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1202 | { |
1328 | { |
1203 | dSTACKLEVEL; |
1329 | dSTACKLEVEL; |
1204 | static volatile int has_throw; |
|
|
1205 | |
1330 | |
1206 | /* sometimes transfer is only called to set idle_sp */ |
1331 | /* sometimes transfer is only called to set idle_sp */ |
1207 | if (expect_false (!next)) |
1332 | if (expect_false (!next)) |
1208 | { |
1333 | { |
1209 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1334 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
… | |
… | |
1213 | { |
1338 | { |
1214 | coro_cctx *prev__cctx; |
1339 | coro_cctx *prev__cctx; |
1215 | |
1340 | |
1216 | if (expect_false (prev->flags & CF_NEW)) |
1341 | if (expect_false (prev->flags & CF_NEW)) |
1217 | { |
1342 | { |
1218 | /* create a new empty context */ |
1343 | /* create a new empty/source context */ |
1219 | Newz (0, prev->cctx, 1, coro_cctx); |
1344 | prev->cctx = cctx_new_empty (); |
1220 | prev->flags &= ~CF_NEW; |
1345 | prev->flags &= ~CF_NEW; |
1221 | prev->flags |= CF_RUNNING; |
1346 | prev->flags |= CF_RUNNING; |
1222 | } |
1347 | } |
1223 | |
1348 | |
1224 | prev->flags &= ~CF_RUNNING; |
1349 | prev->flags &= ~CF_RUNNING; |
… | |
… | |
1239 | else |
1364 | else |
1240 | load_perl (aTHX_ next); |
1365 | load_perl (aTHX_ next); |
1241 | |
1366 | |
1242 | prev__cctx = prev->cctx; |
1367 | prev__cctx = prev->cctx; |
1243 | |
1368 | |
1244 | /* possibly "free" the cctx */ |
1369 | /* possibly untie and reuse the cctx */ |
1245 | if (expect_true ( |
1370 | if (expect_true ( |
1246 | prev__cctx->idle_sp == STACKLEVEL |
1371 | prev__cctx->idle_sp == STACKLEVEL |
1247 | && !(prev__cctx->flags & CC_TRACE) |
1372 | && !(prev__cctx->flags & CC_TRACE) |
1248 | && !force_cctx |
1373 | && !force_cctx |
1249 | )) |
1374 | )) |
1250 | { |
1375 | { |
1251 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
1376 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
1252 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
1377 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); |
1253 | |
1378 | |
1254 | prev->cctx = 0; |
1379 | prev->cctx = 0; |
1255 | |
1380 | |
1256 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1381 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1257 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
1382 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
… | |
… | |
1265 | ++next->usecount; |
1390 | ++next->usecount; |
1266 | |
1391 | |
1267 | if (expect_true (!next->cctx)) |
1392 | if (expect_true (!next->cctx)) |
1268 | next->cctx = cctx_get (aTHX); |
1393 | next->cctx = cctx_get (aTHX); |
1269 | |
1394 | |
1270 | has_throw = !!next->throw; |
1395 | assert (("FATAL: transfer_next already nonzero in Coro (please report)", !transfer_next)); |
|
|
1396 | transfer_next = next; |
1271 | |
1397 | |
1272 | if (expect_false (prev__cctx != next->cctx)) |
1398 | if (expect_false (prev__cctx != next->cctx)) |
1273 | { |
1399 | { |
1274 | prev__cctx->top_env = PL_top_env; |
1400 | prev__cctx->top_env = PL_top_env; |
1275 | PL_top_env = next->cctx->top_env; |
1401 | PL_top_env = next->cctx->top_env; |
1276 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1402 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1277 | } |
1403 | } |
1278 | |
1404 | |
1279 | free_coro_mortal (aTHX); |
1405 | transfer_tail (aTHX); |
1280 | UNLOCK; |
|
|
1281 | |
|
|
1282 | if (expect_false (has_throw)) |
|
|
1283 | { |
|
|
1284 | struct coro *coro = SvSTATE (coro_current); |
|
|
1285 | |
|
|
1286 | if (coro->throw) |
|
|
1287 | { |
|
|
1288 | SV *exception = coro->throw; |
|
|
1289 | coro->throw = 0; |
|
|
1290 | sv_setsv (ERRSV, exception); |
|
|
1291 | croak (0); |
|
|
1292 | } |
|
|
1293 | } |
|
|
1294 | } |
1406 | } |
1295 | } |
1407 | } |
1296 | |
|
|
1297 | struct transfer_args |
|
|
1298 | { |
|
|
1299 | struct coro *prev, *next; |
|
|
1300 | }; |
|
|
1301 | |
1408 | |
1302 | #define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx)) |
1409 | #define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx)) |
1303 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
1410 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
1304 | |
1411 | |
1305 | /** high level stuff ********************************************************/ |
1412 | /** high level stuff ********************************************************/ |
… | |
… | |
1331 | croak ("FATAL: tried to destroy currently running coroutine"); |
1438 | croak ("FATAL: tried to destroy currently running coroutine"); |
1332 | |
1439 | |
1333 | save_perl (aTHX_ &temp); |
1440 | save_perl (aTHX_ &temp); |
1334 | load_perl (aTHX_ coro); |
1441 | load_perl (aTHX_ coro); |
1335 | |
1442 | |
1336 | coro_destroy (aTHX_ coro); |
1443 | coro_destruct (aTHX_ coro); |
1337 | |
1444 | |
1338 | load_perl (aTHX_ &temp); |
1445 | load_perl (aTHX_ &temp); |
1339 | |
1446 | |
1340 | coro->slot = 0; |
1447 | coro->slot = 0; |
1341 | } |
1448 | } |
… | |
… | |
1447 | LOCK; |
1554 | LOCK; |
1448 | |
1555 | |
1449 | sv_hook = coro_nready ? 0 : coro_readyhook; |
1556 | sv_hook = coro_nready ? 0 : coro_readyhook; |
1450 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
1557 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
1451 | |
1558 | |
1452 | coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); |
1559 | coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv)); |
1453 | ++coro_nready; |
1560 | ++coro_nready; |
1454 | |
1561 | |
1455 | UNLOCK; |
1562 | UNLOCK; |
1456 | |
1563 | |
1457 | if (sv_hook) |
1564 | if (sv_hook) |
… | |
… | |
1478 | |
1585 | |
1479 | static int |
1586 | static int |
1480 | api_is_ready (SV *coro_sv) |
1587 | api_is_ready (SV *coro_sv) |
1481 | { |
1588 | { |
1482 | dTHX; |
1589 | dTHX; |
|
|
1590 | |
1483 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1591 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1484 | } |
1592 | } |
1485 | |
1593 | |
1486 | static void |
1594 | INLINE void |
1487 | prepare_schedule (pTHX_ struct transfer_args *ta) |
1595 | prepare_schedule (pTHX_ struct transfer_args *ta) |
1488 | { |
1596 | { |
1489 | SV *prev_sv, *next_sv; |
1597 | SV *prev_sv, *next_sv; |
1490 | |
1598 | |
1491 | for (;;) |
1599 | for (;;) |
… | |
… | |
1517 | /* cannot transfer to destroyed coros, skip and look for next */ |
1625 | /* cannot transfer to destroyed coros, skip and look for next */ |
1518 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
1626 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
1519 | { |
1627 | { |
1520 | UNLOCK; |
1628 | UNLOCK; |
1521 | SvREFCNT_dec (next_sv); |
1629 | SvREFCNT_dec (next_sv); |
1522 | /* coro_nready is already taken care of by destroy */ |
1630 | /* coro_nready has already been taken care of by destroy */ |
1523 | continue; |
1631 | continue; |
1524 | } |
1632 | } |
1525 | |
1633 | |
1526 | --coro_nready; |
1634 | --coro_nready; |
1527 | UNLOCK; |
1635 | UNLOCK; |
… | |
… | |
1530 | |
1638 | |
1531 | /* free this only after the transfer */ |
1639 | /* free this only after the transfer */ |
1532 | prev_sv = SvRV (coro_current); |
1640 | prev_sv = SvRV (coro_current); |
1533 | ta->prev = SvSTATE (prev_sv); |
1641 | ta->prev = SvSTATE (prev_sv); |
1534 | TRANSFER_CHECK (*ta); |
1642 | TRANSFER_CHECK (*ta); |
1535 | assert (ta->next->flags & CF_READY); |
1643 | assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); |
1536 | ta->next->flags &= ~CF_READY; |
1644 | ta->next->flags &= ~CF_READY; |
1537 | SvRV_set (coro_current, next_sv); |
1645 | SvRV_set (coro_current, next_sv); |
1538 | |
1646 | |
1539 | LOCK; |
1647 | LOCK; |
1540 | free_coro_mortal (aTHX); |
1648 | free_coro_mortal (aTHX); |
1541 | coro_mortal = prev_sv; |
1649 | coro_mortal = prev_sv; |
1542 | UNLOCK; |
1650 | UNLOCK; |
1543 | } |
1651 | } |
1544 | |
1652 | |
1545 | static void |
1653 | INLINE void |
1546 | prepare_cede (pTHX_ struct transfer_args *ta) |
1654 | prepare_cede (pTHX_ struct transfer_args *ta) |
1547 | { |
1655 | { |
1548 | api_ready (coro_current); |
1656 | api_ready (coro_current); |
1549 | prepare_schedule (aTHX_ ta); |
1657 | prepare_schedule (aTHX_ ta); |
1550 | } |
1658 | } |
… | |
… | |
1612 | struct coro *coro = SvSTATE (coro_sv); |
1720 | struct coro *coro = SvSTATE (coro_sv); |
1613 | |
1721 | |
1614 | if (flags & CC_TRACE) |
1722 | if (flags & CC_TRACE) |
1615 | { |
1723 | { |
1616 | if (!coro->cctx) |
1724 | if (!coro->cctx) |
1617 | coro->cctx = cctx_new (); |
1725 | coro->cctx = cctx_new_run (); |
1618 | else if (!(coro->cctx->flags & CC_TRACE)) |
1726 | else if (!(coro->cctx->flags & CC_TRACE)) |
1619 | croak ("cannot enable tracing on coroutine with custom stack"); |
1727 | croak ("cannot enable tracing on coroutine with custom stack"); |
1620 | |
1728 | |
1621 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1729 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1622 | } |
1730 | } |
… | |
… | |
1629 | else |
1737 | else |
1630 | coro->slot->runops = RUNOPS_DEFAULT; |
1738 | coro->slot->runops = RUNOPS_DEFAULT; |
1631 | } |
1739 | } |
1632 | } |
1740 | } |
1633 | |
1741 | |
|
|
1742 | #if 0 |
1634 | static int |
1743 | static int |
1635 | coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) |
1744 | coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) |
1636 | { |
1745 | { |
1637 | AV *padlist; |
1746 | AV *padlist; |
1638 | AV *av = (AV *)mg->mg_obj; |
1747 | AV *av = (AV *)mg->mg_obj; |
… | |
… | |
1644 | |
1753 | |
1645 | static MGVTBL coro_gensub_vtbl = { |
1754 | static MGVTBL coro_gensub_vtbl = { |
1646 | 0, 0, 0, 0, |
1755 | 0, 0, 0, 0, |
1647 | coro_gensub_free |
1756 | coro_gensub_free |
1648 | }; |
1757 | }; |
|
|
1758 | #endif |
|
|
1759 | |
|
|
1760 | /*****************************************************************************/ |
|
|
1761 | /* PerlIO::cede */ |
|
|
1762 | |
|
|
1763 | typedef struct |
|
|
1764 | { |
|
|
1765 | PerlIOBuf base; |
|
|
1766 | NV next, every; |
|
|
1767 | } PerlIOCede; |
|
|
1768 | |
|
|
1769 | static IV |
|
|
1770 | PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) |
|
|
1771 | { |
|
|
1772 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
|
|
1773 | |
|
|
1774 | self->every = SvCUR (arg) ? SvNV (arg) : 0.01; |
|
|
1775 | self->next = nvtime () + self->every; |
|
|
1776 | |
|
|
1777 | return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab); |
|
|
1778 | } |
|
|
1779 | |
|
|
1780 | static SV * |
|
|
1781 | PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags) |
|
|
1782 | { |
|
|
1783 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
|
|
1784 | |
|
|
1785 | return newSVnv (self->every); |
|
|
1786 | } |
|
|
1787 | |
|
|
1788 | static IV |
|
|
1789 | PerlIOCede_flush (pTHX_ PerlIO *f) |
|
|
1790 | { |
|
|
1791 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
|
|
1792 | double now = nvtime (); |
|
|
1793 | |
|
|
1794 | if (now >= self->next) |
|
|
1795 | { |
|
|
1796 | api_cede (); |
|
|
1797 | self->next = now + self->every; |
|
|
1798 | } |
|
|
1799 | |
|
|
1800 | return PerlIOBuf_flush (aTHX_ f); |
|
|
1801 | } |
|
|
1802 | |
|
|
1803 | static PerlIO_funcs PerlIO_cede = |
|
|
1804 | { |
|
|
1805 | sizeof(PerlIO_funcs), |
|
|
1806 | "cede", |
|
|
1807 | sizeof(PerlIOCede), |
|
|
1808 | PERLIO_K_DESTRUCT | PERLIO_K_RAW, |
|
|
1809 | PerlIOCede_pushed, |
|
|
1810 | PerlIOBuf_popped, |
|
|
1811 | PerlIOBuf_open, |
|
|
1812 | PerlIOBase_binmode, |
|
|
1813 | PerlIOCede_getarg, |
|
|
1814 | PerlIOBase_fileno, |
|
|
1815 | PerlIOBuf_dup, |
|
|
1816 | PerlIOBuf_read, |
|
|
1817 | PerlIOBuf_unread, |
|
|
1818 | PerlIOBuf_write, |
|
|
1819 | PerlIOBuf_seek, |
|
|
1820 | PerlIOBuf_tell, |
|
|
1821 | PerlIOBuf_close, |
|
|
1822 | PerlIOCede_flush, |
|
|
1823 | PerlIOBuf_fill, |
|
|
1824 | PerlIOBase_eof, |
|
|
1825 | PerlIOBase_error, |
|
|
1826 | PerlIOBase_clearerr, |
|
|
1827 | PerlIOBase_setlinebuf, |
|
|
1828 | PerlIOBuf_get_base, |
|
|
1829 | PerlIOBuf_bufsiz, |
|
|
1830 | PerlIOBuf_get_ptr, |
|
|
1831 | PerlIOBuf_get_cnt, |
|
|
1832 | PerlIOBuf_set_ptrcnt, |
|
|
1833 | }; |
|
|
1834 | |
|
|
1835 | /*****************************************************************************/ |
|
|
1836 | |
|
|
1837 | static const CV *ssl_cv; /* for quick consistency check */ |
|
|
1838 | |
|
|
1839 | static UNOP ssl_restore; /* restore stack as entersub did, for first-re-run */ |
|
|
1840 | static SV *ssl_arg0; |
|
|
1841 | static SV *ssl_arg1; |
|
|
1842 | |
|
|
1843 | /* this restores the stack in the case we patched the entersub, to */ |
|
|
1844 | /* recreate the stack frame as perl will on following calls */ |
|
|
1845 | /* since entersub cleared the stack */ |
|
|
1846 | static OP * |
|
|
1847 | pp_restore (pTHX) |
|
|
1848 | { |
|
|
1849 | dSP; |
|
|
1850 | |
|
|
1851 | PUSHMARK (SP); |
|
|
1852 | |
|
|
1853 | EXTEND (SP, 3); |
|
|
1854 | if (ssl_arg0) PUSHs (sv_2mortal (ssl_arg0)), ssl_arg0 = 0; |
|
|
1855 | if (ssl_arg1) PUSHs (sv_2mortal (ssl_arg1)), ssl_arg1 = 0; |
|
|
1856 | PUSHs ((SV *)CvGV (ssl_cv)); |
|
|
1857 | |
|
|
1858 | RETURNOP (ssl_restore.op_first); |
|
|
1859 | } |
|
|
1860 | |
|
|
1861 | /* declare prototype */ |
|
|
1862 | XS(XS_Coro__State__set_stacklevel); |
|
|
1863 | |
|
|
1864 | #define OPpENTERSUB_SSL 15 |
|
|
1865 | |
|
|
1866 | static OP * |
|
|
1867 | pp_set_stacklevel (pTHX) |
|
|
1868 | { |
|
|
1869 | dSP; |
|
|
1870 | struct transfer_args ta; |
|
|
1871 | SV **arg = PL_stack_base + TOPMARK + 1; |
|
|
1872 | int items = SP - arg; /* args without function object */ |
|
|
1873 | |
|
|
1874 | /* do a quick consistency check on the "function" object, and if it isn't */ |
|
|
1875 | /* for us, divert to the real entersub */ |
|
|
1876 | if (SvTYPE (*sp) != SVt_PVGV || CvXSUB (GvCV (*sp)) != XS_Coro__State__set_stacklevel) |
|
|
1877 | return PL_ppaddr[OP_ENTERSUB](aTHX); |
|
|
1878 | |
|
|
1879 | /* pop args */ |
|
|
1880 | SP = PL_stack_base + POPMARK; |
|
|
1881 | |
|
|
1882 | if (!(PL_op->op_flags & OPf_STACKED)) |
|
|
1883 | { |
|
|
1884 | /* ampersand-form of call, use @_ instead of stack */ |
|
|
1885 | AV *av = GvAV (PL_defgv); |
|
|
1886 | arg = AvARRAY (av); |
|
|
1887 | items = AvFILLp (av) + 1; |
|
|
1888 | } |
|
|
1889 | |
|
|
1890 | PUTBACK; |
|
|
1891 | switch (PL_op->op_private & OPpENTERSUB_SSL) |
|
|
1892 | { |
|
|
1893 | case 0: |
|
|
1894 | prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (arg [0])); |
|
|
1895 | break; |
|
|
1896 | |
|
|
1897 | case 1: |
|
|
1898 | if (items != 2) |
|
|
1899 | croak ("Coro::State::transfer (prev, next) expects two arguments, not %d.", items); |
|
|
1900 | |
|
|
1901 | prepare_transfer (aTHX_ &ta, arg [0], arg [1]); |
|
|
1902 | break; |
|
|
1903 | |
|
|
1904 | case 2: |
|
|
1905 | prepare_schedule (aTHX_ &ta); |
|
|
1906 | break; |
|
|
1907 | |
|
|
1908 | case 3: |
|
|
1909 | prepare_cede (aTHX_ &ta); |
|
|
1910 | break; |
|
|
1911 | |
|
|
1912 | case 4: |
|
|
1913 | if (!prepare_cede_notself (aTHX_ &ta)) |
|
|
1914 | goto skip; |
|
|
1915 | |
|
|
1916 | break; |
|
|
1917 | } |
|
|
1918 | |
|
|
1919 | TRANSFER (ta, 0); |
|
|
1920 | SPAGAIN; |
|
|
1921 | |
|
|
1922 | skip: |
|
|
1923 | PUTBACK; |
|
|
1924 | SSL_TAIL; |
|
|
1925 | SPAGAIN; |
|
|
1926 | RETURN; |
|
|
1927 | } |
1649 | |
1928 | |
1650 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
1929 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
1651 | |
1930 | |
1652 | PROTOTYPES: DISABLE |
1931 | PROTOTYPES: DISABLE |
1653 | |
|
|
1654 | BOOT: |
|
|
1655 | { |
|
|
1656 | #ifdef USE_ITHREADS |
|
|
1657 | MUTEX_INIT (&coro_mutex); |
|
|
1658 | #endif |
|
|
1659 | BOOT_PAGESIZE; |
|
|
1660 | |
|
|
1661 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
|
|
1662 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
|
|
1663 | |
|
|
1664 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
|
|
1665 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
|
|
1666 | orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr; |
|
|
1667 | |
|
|
1668 | hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); |
|
|
1669 | rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); |
|
|
1670 | rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); |
|
|
1671 | |
|
|
1672 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
|
|
1673 | |
|
|
1674 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
|
|
1675 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
|
|
1676 | newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE)); |
|
|
1677 | newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL)); |
|
|
1678 | |
|
|
1679 | main_mainstack = PL_mainstack; |
|
|
1680 | main_top_env = PL_top_env; |
|
|
1681 | |
|
|
1682 | while (main_top_env->je_prev) |
|
|
1683 | main_top_env = main_top_env->je_prev; |
|
|
1684 | |
|
|
1685 | coroapi.ver = CORO_API_VERSION; |
|
|
1686 | coroapi.rev = CORO_API_REVISION; |
|
|
1687 | coroapi.transfer = api_transfer; |
|
|
1688 | |
|
|
1689 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
|
|
1690 | } |
|
|
1691 | |
|
|
1692 | SV * |
|
|
1693 | new (char *klass, ...) |
|
|
1694 | CODE: |
|
|
1695 | { |
|
|
1696 | struct coro *coro; |
|
|
1697 | MAGIC *mg; |
|
|
1698 | HV *hv; |
|
|
1699 | int i; |
|
|
1700 | |
|
|
1701 | Newz (0, coro, 1, struct coro); |
|
|
1702 | coro->args = newAV (); |
|
|
1703 | coro->flags = CF_NEW; |
|
|
1704 | |
|
|
1705 | if (coro_first) coro_first->prev = coro; |
|
|
1706 | coro->next = coro_first; |
|
|
1707 | coro_first = coro; |
|
|
1708 | |
|
|
1709 | coro->hv = hv = newHV (); |
|
|
1710 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
|
|
1711 | mg->mg_flags |= MGf_DUP; |
|
|
1712 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
|
|
1713 | |
|
|
1714 | av_extend (coro->args, items - 1); |
|
|
1715 | for (i = 1; i < items; i++) |
|
|
1716 | av_push (coro->args, newSVsv (ST (i))); |
|
|
1717 | } |
|
|
1718 | OUTPUT: |
|
|
1719 | RETVAL |
|
|
1720 | |
1932 | |
1721 | # these not obviously related functions are all rolled into the same xs |
1933 | # these not obviously related functions are all rolled into the same xs |
1722 | # function to increase chances that they all will call transfer with the same |
1934 | # function to increase chances that they all will call transfer with the same |
1723 | # stack offset |
1935 | # stack offset |
1724 | void |
1936 | void |
… | |
… | |
1728 | Coro::schedule = 2 |
1940 | Coro::schedule = 2 |
1729 | Coro::cede = 3 |
1941 | Coro::cede = 3 |
1730 | Coro::cede_notself = 4 |
1942 | Coro::cede_notself = 4 |
1731 | CODE: |
1943 | CODE: |
1732 | { |
1944 | { |
1733 | struct transfer_args ta; |
1945 | assert (("FATAL: ssl call recursion in Coro module (please report)", PL_op->op_ppaddr != pp_set_stacklevel)); |
1734 | |
1946 | |
1735 | PUTBACK; |
1947 | assert (("FATAL: ssl call with illegal CV value", CvGV (cv))); |
1736 | switch (ix) |
1948 | ssl_cv = cv; |
|
|
1949 | |
|
|
1950 | /* we patch the op, and then re-run the whole call */ |
|
|
1951 | /* we have to put some dummy argument on the stack for this to work */ |
|
|
1952 | ssl_restore.op_next = (OP *)&ssl_restore; |
|
|
1953 | ssl_restore.op_type = OP_NULL; |
|
|
1954 | ssl_restore.op_ppaddr = pp_restore; |
|
|
1955 | ssl_restore.op_first = PL_op; |
|
|
1956 | |
|
|
1957 | ssl_arg0 = items > 0 ? SvREFCNT_inc (ST (0)) : 0; |
|
|
1958 | ssl_arg1 = items > 1 ? SvREFCNT_inc (ST (1)) : 0; |
|
|
1959 | |
|
|
1960 | PL_op->op_ppaddr = pp_set_stacklevel; |
|
|
1961 | PL_op->op_private = PL_op->op_private & ~OPpENTERSUB_SSL | ix; /* we potentially share our private flags with entersub */ |
|
|
1962 | |
|
|
1963 | PL_op = (OP *)&ssl_restore; |
|
|
1964 | } |
|
|
1965 | |
|
|
1966 | BOOT: |
|
|
1967 | { |
|
|
1968 | #ifdef USE_ITHREADS |
|
|
1969 | MUTEX_INIT (&coro_lock); |
|
|
1970 | # if CORO_PTHREAD |
|
|
1971 | coro_thx = PERL_GET_CONTEXT; |
|
|
1972 | # endif |
|
|
1973 | #endif |
|
|
1974 | BOOT_PAGESIZE; |
|
|
1975 | |
|
|
1976 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
|
|
1977 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
|
|
1978 | |
|
|
1979 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
|
|
1980 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
|
|
1981 | orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr; |
|
|
1982 | |
|
|
1983 | hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); |
|
|
1984 | rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); |
|
|
1985 | rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); |
|
|
1986 | |
|
|
1987 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
|
|
1988 | |
|
|
1989 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
|
|
1990 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
|
|
1991 | newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE)); |
|
|
1992 | newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL)); |
|
|
1993 | |
|
|
1994 | main_mainstack = PL_mainstack; |
|
|
1995 | main_top_env = PL_top_env; |
|
|
1996 | |
|
|
1997 | while (main_top_env->je_prev) |
|
|
1998 | main_top_env = main_top_env->je_prev; |
|
|
1999 | |
|
|
2000 | coroapi.ver = CORO_API_VERSION; |
|
|
2001 | coroapi.rev = CORO_API_REVISION; |
|
|
2002 | coroapi.transfer = api_transfer; |
|
|
2003 | |
1737 | { |
2004 | { |
1738 | case 0: |
2005 | SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
1739 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
|
|
1740 | ta.next = 0; |
|
|
1741 | break; |
|
|
1742 | |
2006 | |
1743 | case 1: |
2007 | if (!svp) croak ("Time::HiRes is required"); |
1744 | if (items != 2) |
2008 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); |
1745 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
|
|
1746 | |
2009 | |
1747 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
2010 | nvtime = INT2PTR (double (*)(), SvIV (*svp)); |
1748 | break; |
|
|
1749 | |
|
|
1750 | case 2: |
|
|
1751 | prepare_schedule (aTHX_ &ta); |
|
|
1752 | break; |
|
|
1753 | |
|
|
1754 | case 3: |
|
|
1755 | prepare_cede (aTHX_ &ta); |
|
|
1756 | break; |
|
|
1757 | |
|
|
1758 | case 4: |
|
|
1759 | if (!prepare_cede_notself (aTHX_ &ta)) |
|
|
1760 | XSRETURN_EMPTY; |
|
|
1761 | |
|
|
1762 | break; |
|
|
1763 | } |
2011 | } |
1764 | SPAGAIN; |
|
|
1765 | |
2012 | |
1766 | BARRIER; |
2013 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1767 | PUTBACK; |
|
|
1768 | TRANSFER (ta, 0); |
|
|
1769 | SPAGAIN; /* might be the sp of a different coroutine now */ |
|
|
1770 | /* be extra careful not to ever do anything after TRANSFER */ |
|
|
1771 | } |
2014 | } |
|
|
2015 | |
|
|
2016 | SV * |
|
|
2017 | new (char *klass, ...) |
|
|
2018 | CODE: |
|
|
2019 | { |
|
|
2020 | struct coro *coro; |
|
|
2021 | MAGIC *mg; |
|
|
2022 | HV *hv; |
|
|
2023 | int i; |
|
|
2024 | |
|
|
2025 | Newz (0, coro, 1, struct coro); |
|
|
2026 | coro->args = newAV (); |
|
|
2027 | coro->flags = CF_NEW; |
|
|
2028 | |
|
|
2029 | if (coro_first) coro_first->prev = coro; |
|
|
2030 | coro->next = coro_first; |
|
|
2031 | coro_first = coro; |
|
|
2032 | |
|
|
2033 | coro->hv = hv = newHV (); |
|
|
2034 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
|
|
2035 | mg->mg_flags |= MGf_DUP; |
|
|
2036 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
|
|
2037 | |
|
|
2038 | av_extend (coro->args, items - 1); |
|
|
2039 | for (i = 1; i < items; i++) |
|
|
2040 | av_push (coro->args, newSVsv (ST (i))); |
|
|
2041 | } |
|
|
2042 | OUTPUT: |
|
|
2043 | RETVAL |
1772 | |
2044 | |
1773 | bool |
2045 | bool |
1774 | _destroy (SV *coro_sv) |
2046 | _destroy (SV *coro_sv) |
1775 | CODE: |
2047 | CODE: |
1776 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
2048 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
… | |
… | |
1784 | _exit (code); |
2056 | _exit (code); |
1785 | |
2057 | |
1786 | int |
2058 | int |
1787 | cctx_stacksize (int new_stacksize = 0) |
2059 | cctx_stacksize (int new_stacksize = 0) |
1788 | CODE: |
2060 | CODE: |
1789 | RETVAL = coro_stacksize; |
2061 | RETVAL = cctx_stacksize; |
1790 | if (new_stacksize) |
2062 | if (new_stacksize) |
|
|
2063 | { |
1791 | coro_stacksize = new_stacksize; |
2064 | cctx_stacksize = new_stacksize; |
|
|
2065 | ++cctx_gen; |
|
|
2066 | } |
|
|
2067 | OUTPUT: |
|
|
2068 | RETVAL |
|
|
2069 | |
|
|
2070 | int |
|
|
2071 | cctx_max_idle (int max_idle = 0) |
|
|
2072 | CODE: |
|
|
2073 | RETVAL = cctx_max_idle; |
|
|
2074 | if (max_idle > 1) |
|
|
2075 | cctx_max_idle = max_idle; |
1792 | OUTPUT: |
2076 | OUTPUT: |
1793 | RETVAL |
2077 | RETVAL |
1794 | |
2078 | |
1795 | int |
2079 | int |
1796 | cctx_count () |
2080 | cctx_count () |
… | |
… | |
1874 | RETVAL = boolSV (coro->flags & ix); |
2158 | RETVAL = boolSV (coro->flags & ix); |
1875 | OUTPUT: |
2159 | OUTPUT: |
1876 | RETVAL |
2160 | RETVAL |
1877 | |
2161 | |
1878 | void |
2162 | void |
|
|
2163 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
|
|
2164 | PROTOTYPE: $;$ |
|
|
2165 | CODE: |
|
|
2166 | SvREFCNT_dec (self->throw); |
|
|
2167 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
2168 | |
|
|
2169 | void |
1879 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
2170 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
1880 | |
2171 | |
1881 | SV * |
2172 | SV * |
1882 | has_cctx (Coro::State coro) |
2173 | has_cctx (Coro::State coro) |
1883 | PROTOTYPE: $ |
2174 | PROTOTYPE: $ |
… | |
… | |
1892 | CODE: |
2183 | CODE: |
1893 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
2184 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
1894 | OUTPUT: |
2185 | OUTPUT: |
1895 | RETVAL |
2186 | RETVAL |
1896 | |
2187 | |
1897 | IV |
2188 | UV |
1898 | rss (Coro::State coro) |
2189 | rss (Coro::State coro) |
1899 | PROTOTYPE: $ |
2190 | PROTOTYPE: $ |
1900 | ALIAS: |
2191 | ALIAS: |
1901 | usecount = 1 |
2192 | usecount = 1 |
1902 | CODE: |
2193 | CODE: |
… | |
… | |
1913 | CODE: |
2204 | CODE: |
1914 | struct coro *coro = SvSTATE (coro_current); |
2205 | struct coro *coro = SvSTATE (coro_current); |
1915 | coro->cctx->idle_sp = 0; |
2206 | coro->cctx->idle_sp = 0; |
1916 | |
2207 | |
1917 | void |
2208 | void |
1918 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
|
|
1919 | PROTOTYPE: $;$ |
|
|
1920 | CODE: |
|
|
1921 | SvREFCNT_dec (self->throw); |
|
|
1922 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
1923 | |
|
|
1924 | void |
|
|
1925 | swap_defsv (Coro::State self) |
2209 | swap_defsv (Coro::State self) |
1926 | PROTOTYPE: $ |
2210 | PROTOTYPE: $ |
1927 | ALIAS: |
2211 | ALIAS: |
1928 | swap_defav = 1 |
2212 | swap_defav = 1 |
1929 | CODE: |
2213 | CODE: |
… | |
… | |
1961 | |
2245 | |
1962 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
2246 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1963 | coro_ready[i] = newAV (); |
2247 | coro_ready[i] = newAV (); |
1964 | |
2248 | |
1965 | { |
2249 | { |
1966 | SV *sv = perl_get_sv ("Coro::API", TRUE); |
2250 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
1967 | perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */ |
|
|
1968 | |
2251 | |
1969 | coroapi.schedule = api_schedule; |
2252 | coroapi.schedule = api_schedule; |
1970 | coroapi.cede = api_cede; |
2253 | coroapi.cede = api_cede; |
1971 | coroapi.cede_notself = api_cede_notself; |
2254 | coroapi.cede_notself = api_cede_notself; |
1972 | coroapi.ready = api_ready; |
2255 | coroapi.ready = api_ready; |
… | |
… | |
1983 | void |
2266 | void |
1984 | _set_current (SV *current) |
2267 | _set_current (SV *current) |
1985 | PROTOTYPE: $ |
2268 | PROTOTYPE: $ |
1986 | CODE: |
2269 | CODE: |
1987 | SvREFCNT_dec (SvRV (coro_current)); |
2270 | SvREFCNT_dec (SvRV (coro_current)); |
1988 | SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); |
2271 | SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); |
1989 | |
2272 | |
1990 | void |
2273 | void |
1991 | _set_readyhook (SV *hook) |
2274 | _set_readyhook (SV *hook) |
1992 | PROTOTYPE: $ |
2275 | PROTOTYPE: $ |
1993 | CODE: |
2276 | CODE: |
… | |
… | |
2053 | SvREFCNT_dec (old); |
2336 | SvREFCNT_dec (old); |
2054 | croak ("\3async_pool terminate\2\n"); |
2337 | croak ("\3async_pool terminate\2\n"); |
2055 | } |
2338 | } |
2056 | |
2339 | |
2057 | SvREFCNT_dec (coro->saved_deffh); |
2340 | SvREFCNT_dec (coro->saved_deffh); |
2058 | coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); |
2341 | coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv); |
2059 | |
2342 | |
2060 | hv_store (hv, "desc", sizeof ("desc") - 1, |
2343 | hv_store (hv, "desc", sizeof ("desc") - 1, |
2061 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
2344 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
2062 | |
2345 | |
2063 | invoke_av = (AV *)SvRV (invoke); |
2346 | invoke_av = (AV *)SvRV (invoke); |
… | |
… | |
2067 | |
2350 | |
2068 | if (len > 0) |
2351 | if (len > 0) |
2069 | { |
2352 | { |
2070 | av_fill (defav, len - 1); |
2353 | av_fill (defav, len - 1); |
2071 | for (i = 0; i < len; ++i) |
2354 | for (i = 0; i < len; ++i) |
2072 | av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); |
2355 | av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); |
2073 | } |
2356 | } |
2074 | |
2357 | |
2075 | SvREFCNT_dec (invoke); |
2358 | SvREFCNT_dec (invoke); |
2076 | } |
2359 | } |
2077 | |
2360 | |
… | |
… | |
2084 | sv_setsv (cb, &PL_sv_undef); |
2367 | sv_setsv (cb, &PL_sv_undef); |
2085 | |
2368 | |
2086 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2369 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2087 | coro->saved_deffh = 0; |
2370 | coro->saved_deffh = 0; |
2088 | |
2371 | |
2089 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
2372 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
2090 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
2373 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
2091 | { |
2374 | { |
2092 | SV *old = PL_diehook; |
2375 | SV *old = PL_diehook; |
2093 | PL_diehook = 0; |
2376 | PL_diehook = 0; |
2094 | SvREFCNT_dec (old); |
2377 | SvREFCNT_dec (old); |
… | |
… | |
2156 | #endif |
2439 | #endif |
2157 | |
2440 | |
2158 | |
2441 | |
2159 | MODULE = Coro::State PACKAGE = Coro::AIO |
2442 | MODULE = Coro::State PACKAGE = Coro::AIO |
2160 | |
2443 | |
2161 | SV * |
2444 | void |
2162 | _get_state () |
2445 | _get_state (SV *self) |
2163 | CODE: |
2446 | PPCODE: |
2164 | { |
2447 | { |
2165 | struct io_state *data; |
2448 | AV *defav = GvAV (PL_defgv); |
2166 | |
2449 | AV *av = newAV (); |
|
|
2450 | int i; |
2167 | RETVAL = newSV (sizeof (struct io_state)); |
2451 | SV *data_sv = newSV (sizeof (struct io_state)); |
2168 | data = (struct io_state *)SvPVX (RETVAL); |
2452 | struct io_state *data = (struct io_state *)SvPVX (data_sv); |
2169 | SvCUR_set (RETVAL, sizeof (struct io_state)); |
2453 | SvCUR_set (data_sv, sizeof (struct io_state)); |
2170 | SvPOK_only (RETVAL); |
2454 | SvPOK_only (data_sv); |
2171 | |
2455 | |
2172 | data->errorno = errno; |
2456 | data->errorno = errno; |
2173 | data->laststype = PL_laststype; |
2457 | data->laststype = PL_laststype; |
2174 | data->laststatval = PL_laststatval; |
2458 | data->laststatval = PL_laststatval; |
2175 | data->statcache = PL_statcache; |
2459 | data->statcache = PL_statcache; |
|
|
2460 | |
|
|
2461 | av_extend (av, AvFILLp (defav) + 1 + 1); |
|
|
2462 | |
|
|
2463 | for (i = 0; i <= AvFILLp (defav); ++i) |
|
|
2464 | av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i])); |
|
|
2465 | |
|
|
2466 | av_push (av, data_sv); |
|
|
2467 | |
|
|
2468 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
|
|
2469 | |
|
|
2470 | api_ready (self); |
2176 | } |
2471 | } |
2177 | OUTPUT: |
|
|
2178 | RETVAL |
|
|
2179 | |
2472 | |
2180 | void |
2473 | void |
2181 | _set_state (char *data_) |
2474 | _set_state (SV *state) |
2182 | PROTOTYPE: $ |
2475 | PROTOTYPE: $ |
2183 | CODE: |
2476 | PPCODE: |
2184 | { |
2477 | { |
2185 | struct io_state *data = (void *)data_; |
2478 | AV *av = (AV *)SvRV (state); |
|
|
2479 | struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]); |
|
|
2480 | int i; |
2186 | |
2481 | |
2187 | errno = data->errorno; |
2482 | errno = data->errorno; |
2188 | PL_laststype = data->laststype; |
2483 | PL_laststype = data->laststype; |
2189 | PL_laststatval = data->laststatval; |
2484 | PL_laststatval = data->laststatval; |
2190 | PL_statcache = data->statcache; |
2485 | PL_statcache = data->statcache; |
|
|
2486 | |
|
|
2487 | EXTEND (SP, AvFILLp (av)); |
|
|
2488 | for (i = 0; i < AvFILLp (av); ++i) |
|
|
2489 | PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i]))); |
2191 | } |
2490 | } |
2192 | |
2491 | |
2193 | |
2492 | |
2194 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2493 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2195 | |
2494 | |
… | |
… | |
2219 | } |
2518 | } |
2220 | |
2519 | |
2221 | --incede; |
2520 | --incede; |
2222 | } |
2521 | } |
2223 | |
2522 | |
|
|
2523 | |
|
|
2524 | MODULE = Coro::State PACKAGE = PerlIO::cede |
|
|
2525 | |
|
|
2526 | BOOT: |
|
|
2527 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
|
|
2528 | |