… | |
… | |
4 | #include "perl.h" |
4 | #include "perl.h" |
5 | #include "XSUB.h" |
5 | #include "XSUB.h" |
6 | |
6 | |
7 | #include "patchlevel.h" |
7 | #include "patchlevel.h" |
8 | |
8 | |
9 | #if PERL_VERSION < 6 |
9 | #include <stdio.h> |
|
|
10 | #include <errno.h> |
|
|
11 | #include <assert.h> |
|
|
12 | |
|
|
13 | #ifdef HAVE_MMAP |
|
|
14 | # include <unistd.h> |
|
|
15 | # include <sys/mman.h> |
|
|
16 | # ifndef MAP_ANONYMOUS |
|
|
17 | # ifdef MAP_ANON |
|
|
18 | # define MAP_ANONYMOUS MAP_ANON |
|
|
19 | # else |
|
|
20 | # undef HAVE_MMAP |
|
|
21 | # endif |
|
|
22 | # endif |
|
|
23 | # include <limits.h> |
|
|
24 | # ifndef PAGESIZE |
|
|
25 | # define PAGESIZE pagesize |
|
|
26 | # define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) |
|
|
27 | static long pagesize; |
|
|
28 | # else |
|
|
29 | # define BOOT_PAGESIZE (void)0 |
|
|
30 | # endif |
|
|
31 | #else |
|
|
32 | # define PAGESIZE 0 |
|
|
33 | # define BOOT_PAGESIZE (void)0 |
|
|
34 | #endif |
|
|
35 | |
|
|
36 | #if USE_VALGRIND |
|
|
37 | # include <valgrind/valgrind.h> |
|
|
38 | #endif |
|
|
39 | |
|
|
40 | /* the maximum number of idle cctx that will be pooled */ |
|
|
41 | #define MAX_IDLE_CCTX 8 |
|
|
42 | |
|
|
43 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
|
|
44 | (PERL_REVISION > (a) \ |
|
|
45 | || (PERL_REVISION == (a) \ |
|
|
46 | && (PERL_VERSION > (b) \ |
|
|
47 | || (PERL_VERSION == (b) && PERLSUBVERSION >= (c))))) |
|
|
48 | |
|
|
49 | #if !PERL_VERSION_ATLEAST (5,6,0) |
10 | # ifndef PL_ppaddr |
50 | # ifndef PL_ppaddr |
11 | # define PL_ppaddr ppaddr |
51 | # define PL_ppaddr ppaddr |
12 | # endif |
52 | # endif |
13 | # ifndef call_sv |
53 | # ifndef call_sv |
14 | # define call_sv perl_call_sv |
54 | # define call_sv perl_call_sv |
… | |
… | |
25 | # ifndef IS_PADCONST |
65 | # ifndef IS_PADCONST |
26 | # define IS_PADCONST(v) 0 |
66 | # define IS_PADCONST(v) 0 |
27 | # endif |
67 | # endif |
28 | #endif |
68 | #endif |
29 | |
69 | |
30 | #include <stdio.h> |
70 | /* 5.8.7 */ |
31 | #include <errno.h> |
71 | #ifndef SvRV_set |
|
|
72 | # define SvRV_set(s,v) SvRV(s) = (v) |
|
|
73 | #endif |
32 | |
74 | |
33 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
75 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
34 | # undef STACKGUARD |
76 | # undef STACKGUARD |
35 | #endif |
77 | #endif |
36 | |
78 | |
37 | #ifndef STACKGUARD |
79 | #ifndef STACKGUARD |
38 | # define STACKGUARD 0 |
80 | # define STACKGUARD 0 |
39 | #endif |
81 | #endif |
40 | |
82 | |
41 | #ifdef HAVE_MMAP |
83 | /* prefer perl internal functions over our own? */ |
42 | # include <unistd.h> |
84 | #ifndef PREFER_PERL_FUNCTIONS |
43 | # include <sys/mman.h> |
85 | # define PREFER_PERL_FUNCTIONS 0 |
44 | # ifndef MAP_ANONYMOUS |
|
|
45 | # ifdef MAP_ANON |
|
|
46 | # define MAP_ANONYMOUS MAP_ANON |
|
|
47 | # else |
|
|
48 | # undef HAVE_MMAP |
|
|
49 | # endif |
|
|
50 | # endif |
|
|
51 | # include <limits.h> |
|
|
52 | # ifndef PAGESIZE |
|
|
53 | # define PAGESIZE pagesize |
|
|
54 | # define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) |
|
|
55 | static long pagesize; |
|
|
56 | # else |
|
|
57 | # define BOOT_PAGESIZE |
|
|
58 | # endif |
|
|
59 | #endif |
86 | #endif |
60 | |
87 | |
61 | /* The next macro should declare a variable stacklevel that contains and approximation |
88 | /* The next macro should declare a variable stacklevel that contains and approximation |
62 | * to the current C stack pointer. Its property is that it changes with each call |
89 | * to the current C stack pointer. Its property is that it changes with each call |
63 | * and should be unique. */ |
90 | * and should be unique. */ |
64 | #define dSTACKLEVEL int stacklevel |
91 | #define dSTACKLEVEL int stacklevel |
65 | #define STACKLEVEL ((void *)&stacklevel) |
92 | #define STACKLEVEL ((void *)&stacklevel) |
66 | |
93 | |
67 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
94 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
68 | |
95 | |
|
|
96 | #if __GNUC__ >= 3 |
|
|
97 | # define attribute(x) __attribute__(x) |
|
|
98 | # define BARRIER __asm__ __volatile__ ("" : : : "memory") |
|
|
99 | #else |
|
|
100 | # define attribute(x) |
|
|
101 | # define BARRIER |
|
|
102 | #endif |
|
|
103 | |
|
|
104 | #define NOINLINE attribute ((noinline)) |
|
|
105 | |
69 | #include "CoroAPI.h" |
106 | #include "CoroAPI.h" |
70 | |
|
|
71 | #define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ |
|
|
72 | |
107 | |
73 | #ifdef USE_ITHREADS |
108 | #ifdef USE_ITHREADS |
74 | static perl_mutex coro_mutex; |
109 | static perl_mutex coro_mutex; |
75 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
110 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
76 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
111 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
77 | #else |
112 | #else |
78 | # define LOCK (void)0 |
113 | # define LOCK (void)0 |
79 | # define UNLOCK (void)0 |
114 | # define UNLOCK (void)0 |
80 | #endif |
115 | #endif |
81 | |
116 | |
|
|
117 | struct io_state |
|
|
118 | { |
|
|
119 | int errorno; |
|
|
120 | I32 laststype; |
|
|
121 | int laststatval; |
|
|
122 | Stat_t statcache; |
|
|
123 | }; |
|
|
124 | |
82 | static struct CoroAPI coroapi; |
125 | static struct CoroAPI coroapi; |
83 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
126 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
84 | static HV *coro_state_stash, *coro_stash; |
127 | static HV *coro_state_stash, *coro_stash; |
85 | static SV *coro_mortal; /* will be freed after next transfer */ |
128 | static SV *coro_mortal; /* will be freed after next transfer */ |
86 | |
129 | |
|
|
130 | static struct coro_cctx *cctx_first; |
|
|
131 | static int cctx_count, cctx_idle; |
|
|
132 | |
87 | /* this is a structure representing a c-level coroutine */ |
133 | /* this is a structure representing a c-level coroutine */ |
88 | typedef struct coro_stack { |
134 | typedef struct coro_cctx { |
89 | struct coro_stack *next; |
135 | struct coro_cctx *next; |
90 | |
136 | |
91 | /* the stack */ |
137 | /* the stack */ |
92 | void *sptr; |
138 | void *sptr; |
93 | long ssize; /* positive == mmap, otherwise malloc */ |
139 | long ssize; /* positive == mmap, otherwise malloc */ |
94 | |
140 | |
95 | /* cpu state */ |
141 | /* cpu state */ |
96 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
142 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
|
|
143 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
97 | JMPENV *top_env; |
144 | JMPENV *top_env; |
98 | coro_context cctx; |
145 | coro_context cctx; |
99 | } coro_stack; |
146 | |
|
|
147 | int inuse; |
|
|
148 | |
|
|
149 | #if USE_VALGRIND |
|
|
150 | int valgrind_id; |
|
|
151 | #endif |
|
|
152 | } coro_cctx; |
|
|
153 | |
|
|
154 | enum { |
|
|
155 | CF_RUNNING = 0x0001, /* coroutine is running */ |
|
|
156 | CF_READY = 0x0002, /* coroutine is ready */ |
|
|
157 | CF_NEW = 0x0004, /* has never been switched to */ |
|
|
158 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
|
|
159 | }; |
100 | |
160 | |
101 | /* this is a structure representing a perl-level coroutine */ |
161 | /* this is a structure representing a perl-level coroutine */ |
102 | struct coro { |
162 | struct coro { |
103 | /* the c coroutine allocated to this perl coroutine, if any */ |
163 | /* the c coroutine allocated to this perl coroutine, if any */ |
104 | coro_stack *stack; |
164 | coro_cctx *cctx; |
105 | |
165 | |
106 | /* data associated with this coroutine (initial args) */ |
166 | /* data associated with this coroutine (initial args) */ |
107 | AV *args; |
167 | AV *args; |
108 | int refcnt; |
168 | int refcnt; |
|
|
169 | int save; /* CORO_SAVE flags */ |
|
|
170 | int flags; /* CF_ flags */ |
109 | |
171 | |
110 | /* optionally saved, might be zero */ |
172 | /* optionally saved, might be zero */ |
111 | AV *defav; |
173 | AV *defav; /* @_ */ |
112 | SV *defsv; |
174 | SV *defsv; /* $_ */ |
113 | SV *errsv; |
175 | SV *errsv; /* $@ */ |
|
|
176 | SV *irssv; /* $/ */ |
|
|
177 | SV *irssv_sv; /* real $/ cache */ |
114 | |
178 | |
115 | /* saved global state not related to stacks */ |
179 | #define VAR(name,type) type name; |
116 | U8 dowarn; |
180 | # include "state.h" |
117 | I32 in_eval; |
181 | #undef VAR |
118 | |
|
|
119 | /* the stacks and related info (callchain etc..) */ |
|
|
120 | PERL_SI *curstackinfo; |
|
|
121 | AV *curstack; |
|
|
122 | AV *mainstack; |
|
|
123 | SV **stack_sp; |
|
|
124 | OP *op; |
|
|
125 | SV **curpad; |
|
|
126 | AV *comppad; |
|
|
127 | CV *compcv; |
|
|
128 | SV **stack_base; |
|
|
129 | SV **stack_max; |
|
|
130 | SV **tmps_stack; |
|
|
131 | I32 tmps_floor; |
|
|
132 | I32 tmps_ix; |
|
|
133 | I32 tmps_max; |
|
|
134 | I32 *markstack; |
|
|
135 | I32 *markstack_ptr; |
|
|
136 | I32 *markstack_max; |
|
|
137 | I32 *scopestack; |
|
|
138 | I32 scopestack_ix; |
|
|
139 | I32 scopestack_max; |
|
|
140 | ANY *savestack; |
|
|
141 | I32 savestack_ix; |
|
|
142 | I32 savestack_max; |
|
|
143 | OP **retstack; |
|
|
144 | I32 retstack_ix; |
|
|
145 | I32 retstack_max; |
|
|
146 | PMOP *curpm; |
|
|
147 | COP *curcop; |
|
|
148 | |
182 | |
149 | /* coro process data */ |
183 | /* coro process data */ |
150 | int prio; |
184 | int prio; |
151 | }; |
185 | }; |
152 | |
186 | |
… | |
… | |
159 | AV *padlist = CvPADLIST (cv); |
193 | AV *padlist = CvPADLIST (cv); |
160 | AV *newpadlist, *newpad; |
194 | AV *newpadlist, *newpad; |
161 | |
195 | |
162 | newpadlist = newAV (); |
196 | newpadlist = newAV (); |
163 | AvREAL_off (newpadlist); |
197 | AvREAL_off (newpadlist); |
164 | #if PERL_VERSION < 9 |
198 | #if PERL_VERSION_ATLEAST (5,9,0) |
|
|
199 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); |
|
|
200 | #else |
165 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
201 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
166 | #else |
|
|
167 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); |
|
|
168 | #endif |
202 | #endif |
169 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
203 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
170 | --AvFILLp (padlist); |
204 | --AvFILLp (padlist); |
171 | |
205 | |
172 | av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); |
206 | av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); |
… | |
… | |
216 | |
250 | |
217 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
251 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
218 | |
252 | |
219 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
253 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
220 | |
254 | |
|
|
255 | #define CORO_MAGIC(cv) \ |
|
|
256 | SvMAGIC (cv) \ |
|
|
257 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
|
|
258 | ? SvMAGIC (cv) \ |
|
|
259 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
|
|
260 | : 0 |
|
|
261 | |
221 | /* the next two functions merely cache the padlists */ |
262 | /* the next two functions merely cache the padlists */ |
222 | static void |
263 | static void |
223 | get_padlist (CV *cv) |
264 | get_padlist (CV *cv) |
224 | { |
265 | { |
225 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
266 | MAGIC *mg = CORO_MAGIC (cv); |
|
|
267 | AV *av; |
226 | |
268 | |
227 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
269 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
228 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
270 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
229 | else |
271 | else |
230 | { |
272 | { |
231 | #if 0 |
273 | #if PREFER_PERL_FUNCTIONS |
232 | /* this is probably cleaner, but also slower? */ |
274 | /* this is probably cleaner, but also slower? */ |
233 | CV *cp = Perl_cv_clone (cv); |
275 | CV *cp = Perl_cv_clone (cv); |
234 | CvPADLIST (cv) = CvPADLIST (cp); |
276 | CvPADLIST (cv) = CvPADLIST (cp); |
235 | CvPADLIST (cp) = 0; |
277 | CvPADLIST (cp) = 0; |
236 | SvREFCNT_dec (cp); |
278 | SvREFCNT_dec (cp); |
… | |
… | |
241 | } |
283 | } |
242 | |
284 | |
243 | static void |
285 | static void |
244 | put_padlist (CV *cv) |
286 | put_padlist (CV *cv) |
245 | { |
287 | { |
246 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
288 | MAGIC *mg = CORO_MAGIC (cv); |
|
|
289 | AV *av; |
247 | |
290 | |
248 | if (!mg) |
291 | if (!mg) |
249 | { |
292 | { |
250 | sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); |
293 | sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); |
251 | mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
294 | mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
252 | mg->mg_virtual = &vtbl_coro; |
295 | mg->mg_virtual = &vtbl_coro; |
253 | mg->mg_obj = (SV *)newAV (); |
296 | mg->mg_obj = (SV *)newAV (); |
254 | } |
297 | } |
255 | |
298 | |
256 | av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv)); |
299 | av = (AV *)mg->mg_obj; |
|
|
300 | |
|
|
301 | if (AvFILLp (av) >= AvMAX (av)) |
|
|
302 | av_extend (av, AvMAX (av) + 1); |
|
|
303 | |
|
|
304 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
257 | } |
305 | } |
258 | |
306 | |
259 | #define SB do { |
307 | #define SB do { |
260 | #define SE } while (0) |
308 | #define SE } while (0) |
261 | |
309 | |
262 | #define LOAD(state) load_state((state)); |
|
|
263 | #define SAVE(state,flags) save_state((state),(flags)); |
|
|
264 | |
|
|
265 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
310 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
266 | |
311 | |
267 | static void |
312 | static void |
268 | load_state(Coro__State c) |
313 | load_perl (Coro__State c) |
269 | { |
314 | { |
270 | PL_dowarn = c->dowarn; |
315 | #define VAR(name,type) PL_ ## name = c->name; |
271 | PL_in_eval = c->in_eval; |
316 | # include "state.h" |
272 | |
317 | #undef VAR |
273 | PL_curstackinfo = c->curstackinfo; |
|
|
274 | PL_curstack = c->curstack; |
|
|
275 | PL_mainstack = c->mainstack; |
|
|
276 | PL_stack_sp = c->stack_sp; |
|
|
277 | PL_op = c->op; |
|
|
278 | PL_curpad = c->curpad; |
|
|
279 | PL_comppad = c->comppad; |
|
|
280 | PL_compcv = c->compcv; |
|
|
281 | PL_stack_base = c->stack_base; |
|
|
282 | PL_stack_max = c->stack_max; |
|
|
283 | PL_tmps_stack = c->tmps_stack; |
|
|
284 | PL_tmps_floor = c->tmps_floor; |
|
|
285 | PL_tmps_ix = c->tmps_ix; |
|
|
286 | PL_tmps_max = c->tmps_max; |
|
|
287 | PL_markstack = c->markstack; |
|
|
288 | PL_markstack_ptr = c->markstack_ptr; |
|
|
289 | PL_markstack_max = c->markstack_max; |
|
|
290 | PL_scopestack = c->scopestack; |
|
|
291 | PL_scopestack_ix = c->scopestack_ix; |
|
|
292 | PL_scopestack_max = c->scopestack_max; |
|
|
293 | PL_savestack = c->savestack; |
|
|
294 | PL_savestack_ix = c->savestack_ix; |
|
|
295 | PL_savestack_max = c->savestack_max; |
|
|
296 | #if PERL_VERSION < 9 |
|
|
297 | PL_retstack = c->retstack; |
|
|
298 | PL_retstack_ix = c->retstack_ix; |
|
|
299 | PL_retstack_max = c->retstack_max; |
|
|
300 | #endif |
|
|
301 | PL_curpm = c->curpm; |
|
|
302 | PL_curcop = c->curcop; |
|
|
303 | |
318 | |
304 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
319 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
305 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
320 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
306 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
321 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
|
|
322 | if (c->irssv) |
|
|
323 | { |
|
|
324 | if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) |
|
|
325 | SvREFCNT_dec (c->irssv); |
|
|
326 | else |
|
|
327 | { |
|
|
328 | REPLACE_SV (PL_rs, c->irssv); |
|
|
329 | if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); |
|
|
330 | sv_setsv (c->irssv_sv, PL_rs); |
|
|
331 | } |
|
|
332 | } |
307 | |
333 | |
308 | { |
334 | { |
309 | dSP; |
335 | dSP; |
310 | CV *cv; |
336 | CV *cv; |
311 | |
337 | |
312 | /* now do the ugly restore mess */ |
338 | /* now do the ugly restore mess */ |
313 | while ((cv = (CV *)POPs)) |
339 | while ((cv = (CV *)POPs)) |
314 | { |
340 | { |
315 | AV *padlist = (AV *)POPs; |
|
|
316 | |
|
|
317 | if (padlist) |
|
|
318 | { |
|
|
319 | put_padlist (cv); /* mark this padlist as available */ |
341 | put_padlist (cv); /* mark this padlist as available */ |
320 | CvPADLIST(cv) = padlist; |
342 | CvDEPTH (cv) = PTR2IV (POPs); |
321 | } |
343 | CvPADLIST (cv) = (AV *)POPs; |
322 | |
|
|
323 | ++CvDEPTH(cv); |
|
|
324 | } |
344 | } |
325 | |
345 | |
326 | PUTBACK; |
346 | PUTBACK; |
327 | } |
347 | } |
328 | } |
348 | } |
329 | |
349 | |
330 | static void |
350 | static void |
331 | save_state(Coro__State c, int flags) |
351 | save_perl (Coro__State c) |
332 | { |
352 | { |
333 | { |
353 | { |
334 | dSP; |
354 | dSP; |
335 | I32 cxix = cxstack_ix; |
355 | I32 cxix = cxstack_ix; |
336 | PERL_CONTEXT *ccstk = cxstack; |
356 | PERL_CONTEXT *ccstk = cxstack; |
… | |
… | |
339 | /* |
359 | /* |
340 | * the worst thing you can imagine happens first - we have to save |
360 | * the worst thing you can imagine happens first - we have to save |
341 | * (and reinitialize) all cv's in the whole callchain :( |
361 | * (and reinitialize) all cv's in the whole callchain :( |
342 | */ |
362 | */ |
343 | |
363 | |
|
|
364 | EXTEND (SP, 3 + 1); |
344 | PUSHs (Nullsv); |
365 | PUSHs (Nullsv); |
345 | /* this loop was inspired by pp_caller */ |
366 | /* this loop was inspired by pp_caller */ |
346 | for (;;) |
367 | for (;;) |
347 | { |
368 | { |
348 | while (cxix >= 0) |
369 | while (cxix >= 0) |
349 | { |
370 | { |
350 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
371 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
351 | |
372 | |
352 | if (CxTYPE(cx) == CXt_SUB) |
373 | if (CxTYPE (cx) == CXt_SUB) |
353 | { |
374 | { |
354 | CV *cv = cx->blk_sub.cv; |
375 | CV *cv = cx->blk_sub.cv; |
|
|
376 | |
355 | if (CvDEPTH(cv)) |
377 | if (CvDEPTH (cv)) |
356 | { |
378 | { |
357 | EXTEND (SP, CvDEPTH(cv)*2); |
379 | EXTEND (SP, 3); |
358 | |
|
|
359 | while (--CvDEPTH(cv)) |
|
|
360 | { |
|
|
361 | /* this tells the restore code to increment CvDEPTH */ |
|
|
362 | PUSHs (Nullsv); |
|
|
363 | PUSHs ((SV *)cv); |
|
|
364 | } |
|
|
365 | |
|
|
366 | PUSHs ((SV *)CvPADLIST(cv)); |
380 | PUSHs ((SV *)CvPADLIST (cv)); |
|
|
381 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
367 | PUSHs ((SV *)cv); |
382 | PUSHs ((SV *)cv); |
368 | |
383 | |
|
|
384 | CvDEPTH (cv) = 0; |
369 | get_padlist (cv); |
385 | get_padlist (cv); |
370 | } |
386 | } |
371 | } |
387 | } |
372 | #ifdef CXt_FORMAT |
|
|
373 | else if (CxTYPE(cx) == CXt_FORMAT) |
|
|
374 | { |
|
|
375 | /* I never used formats, so how should I know how these are implemented? */ |
|
|
376 | /* my bold guess is as a simple, plain sub... */ |
|
|
377 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
|
|
378 | } |
|
|
379 | #endif |
|
|
380 | } |
388 | } |
381 | |
389 | |
382 | if (top_si->si_type == PERLSI_MAIN) |
390 | if (top_si->si_type == PERLSI_MAIN) |
383 | break; |
391 | break; |
384 | |
392 | |
… | |
… | |
388 | } |
396 | } |
389 | |
397 | |
390 | PUTBACK; |
398 | PUTBACK; |
391 | } |
399 | } |
392 | |
400 | |
393 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
401 | c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
394 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
402 | c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
395 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
403 | c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
|
|
404 | c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; |
396 | |
405 | |
397 | c->dowarn = PL_dowarn; |
406 | #define VAR(name,type)c->name = PL_ ## name; |
398 | c->in_eval = PL_in_eval; |
407 | # include "state.h" |
399 | |
408 | #undef VAR |
400 | c->curstackinfo = PL_curstackinfo; |
|
|
401 | c->curstack = PL_curstack; |
|
|
402 | c->mainstack = PL_mainstack; |
|
|
403 | c->stack_sp = PL_stack_sp; |
|
|
404 | c->op = PL_op; |
|
|
405 | c->curpad = PL_curpad; |
|
|
406 | c->comppad = PL_comppad; |
|
|
407 | c->compcv = PL_compcv; |
|
|
408 | c->stack_base = PL_stack_base; |
|
|
409 | c->stack_max = PL_stack_max; |
|
|
410 | c->tmps_stack = PL_tmps_stack; |
|
|
411 | c->tmps_floor = PL_tmps_floor; |
|
|
412 | c->tmps_ix = PL_tmps_ix; |
|
|
413 | c->tmps_max = PL_tmps_max; |
|
|
414 | c->markstack = PL_markstack; |
|
|
415 | c->markstack_ptr = PL_markstack_ptr; |
|
|
416 | c->markstack_max = PL_markstack_max; |
|
|
417 | c->scopestack = PL_scopestack; |
|
|
418 | c->scopestack_ix = PL_scopestack_ix; |
|
|
419 | c->scopestack_max = PL_scopestack_max; |
|
|
420 | c->savestack = PL_savestack; |
|
|
421 | c->savestack_ix = PL_savestack_ix; |
|
|
422 | c->savestack_max = PL_savestack_max; |
|
|
423 | #if PERL_VERSION < 9 |
|
|
424 | c->retstack = PL_retstack; |
|
|
425 | c->retstack_ix = PL_retstack_ix; |
|
|
426 | c->retstack_max = PL_retstack_max; |
|
|
427 | #endif |
|
|
428 | c->curpm = PL_curpm; |
|
|
429 | c->curcop = PL_curcop; |
|
|
430 | } |
409 | } |
431 | |
410 | |
432 | /* |
411 | /* |
433 | * allocate various perl stacks. This is an exact copy |
412 | * allocate various perl stacks. This is an exact copy |
434 | * of perl.c:init_stacks, except that it uses less memory |
413 | * of perl.c:init_stacks, except that it uses less memory |
435 | * on the (sometimes correct) assumption that coroutines do |
414 | * on the (sometimes correct) assumption that coroutines do |
436 | * not usually need a lot of stackspace. |
415 | * not usually need a lot of stackspace. |
437 | */ |
416 | */ |
|
|
417 | #if PREFER_PERL_FUNCTIONS |
|
|
418 | # define coro_init_stacks init_stacks |
|
|
419 | #else |
438 | static void |
420 | static void |
439 | coro_init_stacks () |
421 | coro_init_stacks () |
440 | { |
422 | { |
441 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
423 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
442 | PL_curstackinfo->si_type = PERLSI_MAIN; |
424 | PL_curstackinfo->si_type = PERLSI_MAIN; |
443 | PL_curstack = PL_curstackinfo->si_stack; |
425 | PL_curstack = PL_curstackinfo->si_stack; |
444 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
426 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
445 | |
427 | |
446 | PL_stack_base = AvARRAY(PL_curstack); |
428 | PL_stack_base = AvARRAY(PL_curstack); |
447 | PL_stack_sp = PL_stack_base; |
429 | PL_stack_sp = PL_stack_base; |
448 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
430 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
449 | |
431 | |
450 | New(50,PL_tmps_stack,96,SV*); |
432 | New(50,PL_tmps_stack,128,SV*); |
451 | PL_tmps_floor = -1; |
433 | PL_tmps_floor = -1; |
452 | PL_tmps_ix = -1; |
434 | PL_tmps_ix = -1; |
453 | PL_tmps_max = 96; |
435 | PL_tmps_max = 128; |
454 | |
436 | |
455 | New(54,PL_markstack,16,I32); |
437 | New(54,PL_markstack,32,I32); |
456 | PL_markstack_ptr = PL_markstack; |
438 | PL_markstack_ptr = PL_markstack; |
457 | PL_markstack_max = PL_markstack + 16; |
439 | PL_markstack_max = PL_markstack + 32; |
458 | |
440 | |
459 | #ifdef SET_MARK_OFFSET |
441 | #ifdef SET_MARK_OFFSET |
460 | SET_MARK_OFFSET; |
442 | SET_MARK_OFFSET; |
461 | #endif |
443 | #endif |
462 | |
444 | |
463 | New(54,PL_scopestack,16,I32); |
445 | New(54,PL_scopestack,32,I32); |
464 | PL_scopestack_ix = 0; |
446 | PL_scopestack_ix = 0; |
465 | PL_scopestack_max = 16; |
447 | PL_scopestack_max = 32; |
466 | |
448 | |
467 | New(54,PL_savestack,96,ANY); |
449 | New(54,PL_savestack,64,ANY); |
468 | PL_savestack_ix = 0; |
450 | PL_savestack_ix = 0; |
469 | PL_savestack_max = 96; |
451 | PL_savestack_max = 64; |
470 | |
452 | |
471 | #if PERL_VERSION < 9 |
453 | #if !PERL_VERSION_ATLEAST (5,9,0) |
472 | New(54,PL_retstack,8,OP*); |
454 | New(54,PL_retstack,16,OP*); |
473 | PL_retstack_ix = 0; |
455 | PL_retstack_ix = 0; |
474 | PL_retstack_max = 8; |
456 | PL_retstack_max = 16; |
475 | #endif |
457 | #endif |
476 | } |
458 | } |
|
|
459 | #endif |
477 | |
460 | |
478 | /* |
461 | /* |
479 | * destroy the stacks, the callchain etc... |
462 | * destroy the stacks, the callchain etc... |
480 | */ |
463 | */ |
481 | static void |
464 | static void |
482 | destroy_stacks() |
465 | coro_destroy_stacks () |
483 | { |
466 | { |
484 | if (!IN_DESTRUCT) |
467 | if (!IN_DESTRUCT) |
485 | { |
468 | { |
486 | /* is this ugly, I ask? */ |
469 | /* restore all saved variables and stuff */ |
487 | LEAVE_SCOPE (0); |
470 | LEAVE_SCOPE (0); |
|
|
471 | assert (PL_tmps_floor == -1); |
488 | |
472 | |
489 | /* sure it is, but more important: is it correct?? :/ */ |
473 | /* free all temporaries */ |
490 | FREETMPS; |
474 | FREETMPS; |
|
|
475 | assert (PL_tmps_ix == -1); |
491 | |
476 | |
|
|
477 | /* unwind all extra stacks */ |
492 | /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/ |
478 | POPSTACK_TO (PL_mainstack); |
|
|
479 | |
|
|
480 | /* unwind main stack */ |
|
|
481 | dounwind (-1); |
493 | } |
482 | } |
494 | |
483 | |
495 | while (PL_curstackinfo->si_next) |
484 | while (PL_curstackinfo->si_next) |
496 | PL_curstackinfo = PL_curstackinfo->si_next; |
485 | PL_curstackinfo = PL_curstackinfo->si_next; |
497 | |
486 | |
498 | while (PL_curstackinfo) |
487 | while (PL_curstackinfo) |
499 | { |
488 | { |
500 | PERL_SI *p = PL_curstackinfo->si_prev; |
489 | PERL_SI *p = PL_curstackinfo->si_prev; |
501 | |
490 | |
502 | { /*D*//*remove*/ |
|
|
503 | dSP; |
|
|
504 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
|
|
505 | PUTBACK; /* possibly superfluous */ |
|
|
506 | } |
|
|
507 | |
|
|
508 | if (!IN_DESTRUCT) |
491 | if (!IN_DESTRUCT) |
509 | { |
|
|
510 | dounwind (-1);/*D*//*remove*/ |
|
|
511 | SvREFCNT_dec (PL_curstackinfo->si_stack); |
492 | SvREFCNT_dec (PL_curstackinfo->si_stack); |
512 | } |
|
|
513 | |
493 | |
514 | Safefree (PL_curstackinfo->si_cxstack); |
494 | Safefree (PL_curstackinfo->si_cxstack); |
515 | Safefree (PL_curstackinfo); |
495 | Safefree (PL_curstackinfo); |
516 | PL_curstackinfo = p; |
496 | PL_curstackinfo = p; |
517 | } |
497 | } |
518 | |
498 | |
519 | Safefree (PL_tmps_stack); |
499 | Safefree (PL_tmps_stack); |
520 | Safefree (PL_markstack); |
500 | Safefree (PL_markstack); |
521 | Safefree (PL_scopestack); |
501 | Safefree (PL_scopestack); |
522 | Safefree (PL_savestack); |
502 | Safefree (PL_savestack); |
523 | #if PERL_VERSION < 9 |
503 | #if !PERL_VERSION_ATLEAST (5,9,0) |
524 | Safefree (PL_retstack); |
504 | Safefree (PL_retstack); |
525 | #endif |
505 | #endif |
526 | } |
506 | } |
527 | |
507 | |
528 | static void |
508 | static void |
529 | setup_coro (struct coro *coro) |
509 | setup_coro (struct coro *coro) |
530 | { |
510 | { |
531 | /* |
511 | /* |
532 | * emulate part of the perl startup here. |
512 | * emulate part of the perl startup here. |
533 | */ |
513 | */ |
534 | dTHX; |
514 | |
|
|
515 | coro_init_stacks (); |
|
|
516 | |
|
|
517 | PL_curcop = &PL_compiling; |
|
|
518 | PL_in_eval = EVAL_NULL; |
|
|
519 | PL_curpm = 0; |
|
|
520 | PL_localizing = 0; |
|
|
521 | PL_dirty = 0; |
|
|
522 | PL_restartop = 0; |
|
|
523 | |
|
|
524 | { |
|
|
525 | dSP; |
|
|
526 | LOGOP myop; |
|
|
527 | |
|
|
528 | SvREFCNT_dec (GvAV (PL_defgv)); |
|
|
529 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
|
|
530 | |
|
|
531 | Zero (&myop, 1, LOGOP); |
|
|
532 | myop.op_next = Nullop; |
|
|
533 | myop.op_flags = OPf_WANT_VOID; |
|
|
534 | |
|
|
535 | PUSHMARK (SP); |
|
|
536 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
|
|
537 | PUTBACK; |
|
|
538 | PL_op = (OP *)&myop; |
|
|
539 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
|
|
540 | SPAGAIN; |
|
|
541 | } |
|
|
542 | |
|
|
543 | ENTER; /* necessary e.g. for dounwind */ |
|
|
544 | } |
|
|
545 | |
|
|
546 | static void |
|
|
547 | free_coro_mortal () |
|
|
548 | { |
|
|
549 | if (coro_mortal) |
|
|
550 | { |
|
|
551 | SvREFCNT_dec (coro_mortal); |
|
|
552 | coro_mortal = 0; |
|
|
553 | } |
|
|
554 | } |
|
|
555 | |
|
|
556 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
|
|
557 | static void NOINLINE |
|
|
558 | prepare_cctx (coro_cctx *cctx) |
|
|
559 | { |
535 | dSP; |
560 | dSP; |
536 | UNOP myop; |
561 | LOGOP myop; |
537 | SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE); |
|
|
538 | |
562 | |
539 | coro_init_stacks (); |
|
|
540 | /*PL_curcop = 0;*/ |
|
|
541 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
|
|
542 | SvREFCNT_dec (GvAV (PL_defgv)); |
|
|
543 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
|
|
544 | |
|
|
545 | SPAGAIN; |
|
|
546 | |
|
|
547 | Zero (&myop, 1, UNOP); |
563 | Zero (&myop, 1, LOGOP); |
548 | myop.op_next = Nullop; |
564 | myop.op_next = PL_op; |
549 | myop.op_flags = OPf_WANT_VOID; |
565 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
550 | |
566 | |
|
|
567 | PUSHMARK (SP); |
|
|
568 | EXTEND (SP, 2); |
|
|
569 | PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); |
|
|
570 | PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
|
|
571 | PUTBACK; |
551 | PL_op = (OP *)&myop; |
572 | PL_op = (OP *)&myop; |
552 | |
|
|
553 | PUSHMARK(SP); |
|
|
554 | XPUSHs (sub_init); |
|
|
555 | PUTBACK; |
|
|
556 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
573 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
557 | SPAGAIN; |
574 | SPAGAIN; |
558 | |
|
|
559 | ENTER; /* necessary e.g. for dounwind */ |
|
|
560 | } |
|
|
561 | |
|
|
562 | static void |
|
|
563 | free_coro_mortal () |
|
|
564 | { |
|
|
565 | if (coro_mortal) |
|
|
566 | { |
|
|
567 | SvREFCNT_dec (coro_mortal); |
|
|
568 | coro_mortal = 0; |
|
|
569 | } |
|
|
570 | } |
|
|
571 | |
|
|
572 | static void |
|
|
573 | prepare_cctx (coro_stack *cctx) |
|
|
574 | { |
|
|
575 | dSP; |
|
|
576 | UNOP myop; |
|
|
577 | |
|
|
578 | Zero (&myop, 1, UNOP); |
|
|
579 | myop.op_next = PL_op; |
|
|
580 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
|
|
581 | |
|
|
582 | PUSHMARK(SP); |
|
|
583 | EXTEND (SP, 2); |
|
|
584 | PUSHs (newSViv (PTR2IV (cctx))); |
|
|
585 | PUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE)); |
|
|
586 | PUTBACK; |
|
|
587 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
|
|
588 | SPAGAIN; |
|
|
589 | } |
575 | } |
590 | |
576 | |
591 | static void |
577 | static void |
592 | coro_run (void *arg) |
578 | coro_run (void *arg) |
593 | { |
579 | { |
|
|
580 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
|
|
581 | UNLOCK; |
|
|
582 | |
594 | /* |
583 | /* |
595 | * this is a _very_ stripped down perl interpreter ;) |
584 | * this is a _very_ stripped down perl interpreter ;) |
596 | */ |
585 | */ |
597 | UNLOCK; |
|
|
598 | |
|
|
599 | PL_top_env = &PL_start_env; |
586 | PL_top_env = &PL_start_env; |
|
|
587 | |
|
|
588 | /* inject call to cctx_init */ |
600 | prepare_cctx ((coro_stack *)arg); |
589 | prepare_cctx ((coro_cctx *)arg); |
601 | |
590 | |
602 | /* somebody will hit me for both perl_run and PL_restartop */ |
591 | /* somebody will hit me for both perl_run and PL_restartop */ |
603 | perl_run (PERL_GET_CONTEXT); |
592 | PL_restartop = PL_op; |
|
|
593 | perl_run (PL_curinterp); |
604 | |
594 | |
605 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr); |
595 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
606 | abort (); |
596 | abort (); |
607 | } |
597 | } |
608 | |
598 | |
609 | static coro_stack * |
599 | static coro_cctx * |
610 | stack_new () |
600 | cctx_new () |
611 | { |
601 | { |
612 | coro_stack *stack; |
602 | coro_cctx *cctx; |
613 | |
603 | |
|
|
604 | ++cctx_count; |
|
|
605 | |
614 | New (0, stack, 1, coro_stack); |
606 | Newz (0, cctx, 1, coro_cctx); |
615 | |
607 | |
616 | #if HAVE_MMAP |
608 | #if HAVE_MMAP |
617 | |
609 | |
618 | stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
610 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
619 | /* mmap suppsedly does allocate-on-write for us */ |
611 | /* mmap supposedly does allocate-on-write for us */ |
620 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
612 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
621 | |
613 | |
622 | if (stack->sptr == (void *)-1) |
614 | if (cctx->sptr == (void *)-1) |
623 | { |
615 | { |
624 | perror ("FATAL: unable to mmap stack for coroutine"); |
616 | perror ("FATAL: unable to mmap stack for coroutine"); |
625 | _exit (EXIT_FAILURE); |
617 | _exit (EXIT_FAILURE); |
626 | } |
618 | } |
627 | |
619 | |
628 | # if STACKGUARD |
620 | # if STACKGUARD |
629 | mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
621 | mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
630 | # endif |
622 | # endif |
631 | |
623 | |
632 | #else |
624 | #else |
633 | |
625 | |
634 | stack->ssize = STACKSIZE * (long)sizeof (long); |
626 | cctx->ssize = STACKSIZE * (long)sizeof (long); |
635 | New (0, stack->sptr, STACKSIZE, long); |
627 | New (0, cctx->sptr, STACKSIZE, long); |
636 | |
628 | |
637 | if (!stack->sptr) |
629 | if (!cctx->sptr) |
638 | { |
630 | { |
639 | perror (stderr, "FATAL: unable to malloc stack for coroutine"); |
631 | perror ("FATAL: unable to malloc stack for coroutine"); |
640 | _exit (EXIT_FAILURE); |
632 | _exit (EXIT_FAILURE); |
641 | } |
633 | } |
642 | |
634 | |
643 | #endif |
635 | #endif |
644 | |
636 | |
|
|
637 | #if USE_VALGRIND |
|
|
638 | cctx->valgrind_id = VALGRIND_STACK_REGISTER ( |
|
|
639 | STACKGUARD * PAGESIZE + (char *)cctx->sptr, |
|
|
640 | cctx->ssize + (char *)cctx->sptr |
|
|
641 | ); |
|
|
642 | #endif |
|
|
643 | |
645 | coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); |
644 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); |
646 | |
645 | |
647 | return stack; |
646 | return cctx; |
648 | } |
647 | } |
649 | |
648 | |
650 | static void |
649 | static void |
651 | stack_free (coro_stack *stack) |
650 | cctx_destroy (coro_cctx *cctx) |
652 | { |
651 | { |
653 | if (!stack) |
652 | if (!cctx) |
654 | return; |
653 | return; |
655 | |
654 | |
|
|
655 | --cctx_count; |
|
|
656 | |
|
|
657 | #if USE_VALGRIND |
|
|
658 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
|
|
659 | #endif |
|
|
660 | |
656 | #if HAVE_MMAP |
661 | #if HAVE_MMAP |
657 | munmap (stack->sptr, stack->ssize); |
662 | munmap (cctx->sptr, cctx->ssize); |
658 | #else |
663 | #else |
659 | Safefree (stack->sptr); |
664 | Safefree (cctx->sptr); |
660 | #endif |
665 | #endif |
661 | |
666 | |
662 | Safefree (stack); |
667 | Safefree (cctx); |
663 | } |
668 | } |
664 | |
669 | |
665 | static coro_stack *stack_first; |
|
|
666 | |
|
|
667 | static coro_stack * |
670 | static coro_cctx * |
668 | stack_get () |
671 | cctx_get () |
669 | { |
672 | { |
670 | coro_stack *stack; |
673 | coro_cctx *cctx; |
671 | |
674 | |
672 | if (stack_first) |
675 | if (cctx_first) |
673 | { |
676 | { |
674 | stack = stack_first; |
677 | cctx = cctx_first; |
675 | stack_first = stack->next; |
678 | cctx_first = cctx->next; |
|
|
679 | --cctx_idle; |
676 | } |
680 | } |
677 | else |
681 | else |
678 | { |
682 | { |
679 | stack = stack_new (); |
683 | cctx = cctx_new (); |
680 | PL_op = PL_op->op_next; |
684 | PL_op = PL_op->op_next; |
681 | } |
685 | } |
682 | |
686 | |
683 | return stack; |
687 | return cctx; |
684 | } |
688 | } |
685 | |
689 | |
686 | static void |
690 | static void |
687 | stack_put (coro_stack *stack) |
691 | cctx_put (coro_cctx *cctx) |
688 | { |
692 | { |
689 | stack->next = stack_first; |
693 | /* free another cctx if overlimit */ |
690 | stack_first = stack; |
694 | if (cctx_idle >= MAX_IDLE_CCTX) |
|
|
695 | { |
|
|
696 | coro_cctx *first = cctx_first; |
|
|
697 | cctx_first = first->next; |
|
|
698 | --cctx_idle; |
|
|
699 | |
|
|
700 | assert (!first->inuse); |
|
|
701 | cctx_destroy (first); |
|
|
702 | } |
|
|
703 | |
|
|
704 | ++cctx_idle; |
|
|
705 | cctx->next = cctx_first; |
|
|
706 | cctx_first = cctx; |
691 | } |
707 | } |
692 | |
708 | |
693 | /* never call directly, always through the coro_state_transfer global variable */ |
709 | /* never call directly, always through the coro_state_transfer global variable */ |
694 | static void |
710 | static void NOINLINE |
695 | transfer_impl (struct coro *prev, struct coro *next, int flags) |
711 | transfer (struct coro *prev, struct coro *next) |
696 | { |
712 | { |
697 | dSTACKLEVEL; |
713 | dSTACKLEVEL; |
698 | |
714 | |
699 | /* sometimes transfer is only called to set idle_sp */ |
715 | /* sometimes transfer is only called to set idle_sp */ |
700 | if (flags == TRANSFER_SET_STACKLEVEL) |
716 | if (!next) |
|
|
717 | { |
701 | ((coro_stack *)prev)->idle_sp = STACKLEVEL; |
718 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
|
|
719 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
|
|
720 | } |
702 | else if (prev != next) |
721 | else if (prev != next) |
703 | { |
722 | { |
704 | coro_stack *prev__stack; |
723 | coro_cctx *prev__cctx; |
|
|
724 | |
|
|
725 | if (prev->flags & CF_NEW) |
|
|
726 | { |
|
|
727 | /* create a new empty context */ |
|
|
728 | Newz (0, prev->cctx, 1, coro_cctx); |
|
|
729 | prev->cctx->inuse = 1; |
|
|
730 | prev->flags &= ~CF_NEW; |
|
|
731 | prev->flags |= CF_RUNNING; |
|
|
732 | } |
|
|
733 | |
|
|
734 | /*TODO: must not croak here */ |
|
|
735 | if (!prev->flags & CF_RUNNING) |
|
|
736 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
|
|
737 | |
|
|
738 | if (next->flags & CF_RUNNING) |
|
|
739 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
|
|
740 | |
|
|
741 | if (next->flags & CF_DESTROYED) |
|
|
742 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
|
|
743 | |
|
|
744 | prev->flags &= ~CF_RUNNING; |
|
|
745 | next->flags |= CF_RUNNING; |
705 | |
746 | |
706 | LOCK; |
747 | LOCK; |
707 | |
748 | |
708 | if (next->mainstack) |
749 | if (next->flags & CF_NEW) |
709 | { |
750 | { |
710 | /* coroutine already started */ |
751 | /* need to start coroutine */ |
711 | SAVE (prev, flags); |
752 | next->flags &= ~CF_NEW; |
712 | LOAD (next); |
753 | /* first get rid of the old state */ |
|
|
754 | save_perl (prev); |
|
|
755 | /* setup coroutine call */ |
|
|
756 | setup_coro (next); |
|
|
757 | /* need a new stack */ |
|
|
758 | assert (!next->cctx); |
713 | } |
759 | } |
714 | else |
760 | else |
715 | { |
761 | { |
716 | /* need to start coroutine */ |
762 | /* coroutine already started */ |
717 | /* first get rid of the old state */ |
763 | save_perl (prev); |
718 | SAVE (prev, -1); |
764 | load_perl (next); |
719 | /* setup coroutine call */ |
|
|
720 | setup_coro (next); |
|
|
721 | /* need a stack */ |
|
|
722 | next->stack = 0; |
|
|
723 | } |
765 | } |
724 | |
766 | |
725 | if (!prev->stack) |
|
|
726 | /* create a new empty context */ |
|
|
727 | Newz (0, prev->stack, 1, coro_stack); |
|
|
728 | |
|
|
729 | prev__stack = prev->stack; |
767 | prev__cctx = prev->cctx; |
730 | |
768 | |
731 | /* possibly "free" the stack */ |
769 | /* possibly "free" the cctx */ |
732 | if (prev__stack->idle_sp == STACKLEVEL) |
770 | if (prev__cctx->idle_sp == STACKLEVEL) |
733 | { |
771 | { |
734 | stack_put (prev__stack); |
772 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
|
|
773 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
|
|
774 | |
735 | prev->stack = 0; |
775 | prev->cctx = 0; |
|
|
776 | |
|
|
777 | cctx_put (prev__cctx); |
|
|
778 | prev__cctx->inuse = 0; |
736 | } |
779 | } |
737 | |
780 | |
738 | if (!next->stack) |
781 | if (!next->cctx) |
739 | next->stack = stack_get (); |
|
|
740 | |
|
|
741 | if (prev__stack != next->stack) |
|
|
742 | { |
782 | { |
743 | prev__stack->top_env = PL_top_env; |
783 | next->cctx = cctx_get (); |
744 | PL_top_env = next->stack->top_env; |
784 | assert (!next->cctx->inuse); |
745 | coro_transfer (&prev__stack->cctx, &next->stack->cctx); |
785 | next->cctx->inuse = 1; |
746 | } |
786 | } |
747 | |
787 | |
|
|
788 | if (prev__cctx != next->cctx) |
|
|
789 | { |
|
|
790 | prev__cctx->top_env = PL_top_env; |
|
|
791 | PL_top_env = next->cctx->top_env; |
|
|
792 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
|
|
793 | } |
|
|
794 | |
748 | free_coro_mortal (); |
795 | free_coro_mortal (); |
749 | |
|
|
750 | UNLOCK; |
796 | UNLOCK; |
751 | } |
797 | } |
752 | } |
798 | } |
753 | |
799 | |
754 | /* use this function pointer to call the above function */ |
|
|
755 | /* this is done to increase chances of the compiler not inlining the call */ |
|
|
756 | /* not static to make it even harder for the compiler (and theoretically impossible in most cases */ |
|
|
757 | void (*coro_state_transfer)(struct coro *prev, struct coro *next, int flags) = transfer_impl; |
|
|
758 | |
|
|
759 | struct transfer_args |
800 | struct transfer_args |
760 | { |
801 | { |
761 | struct coro *prev, *next; |
802 | struct coro *prev, *next; |
762 | int flags; |
|
|
763 | }; |
803 | }; |
764 | |
804 | |
765 | #define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags) |
805 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
766 | |
806 | |
767 | static void |
807 | static int |
768 | coro_state_destroy (struct coro *coro) |
808 | coro_state_destroy (struct coro *coro) |
769 | { |
809 | { |
770 | if (coro->refcnt--) |
810 | if (coro->flags & CF_DESTROYED) |
771 | return; |
811 | return 0; |
|
|
812 | |
|
|
813 | coro->flags |= CF_DESTROYED; |
772 | |
814 | |
773 | if (coro->mainstack && coro->mainstack != main_mainstack) |
815 | if (coro->mainstack && coro->mainstack != main_mainstack) |
774 | { |
816 | { |
|
|
817 | assert (!(coro->flags & CF_RUNNING)); |
|
|
818 | |
775 | struct coro temp; |
819 | struct coro temp; |
|
|
820 | Zero (&temp, 1, struct coro); |
|
|
821 | temp.save = CORO_SAVE_ALL; |
776 | |
822 | |
777 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
823 | if (coro->flags & CF_RUNNING) |
778 | LOAD (coro); |
824 | croak ("FATAL: tried to destroy currently running coroutine"); |
779 | |
825 | |
|
|
826 | save_perl (&temp); |
|
|
827 | load_perl (coro); |
|
|
828 | |
780 | destroy_stacks (); |
829 | coro_destroy_stacks (); |
781 | |
830 | |
782 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
831 | load_perl (&temp); /* this will get rid of defsv etc.. */ |
783 | |
832 | |
784 | coro->mainstack = 0; |
833 | coro->mainstack = 0; |
785 | } |
834 | } |
786 | |
835 | |
787 | stack_free (coro->stack); |
836 | cctx_destroy (coro->cctx); |
788 | SvREFCNT_dec (coro->args); |
837 | SvREFCNT_dec (coro->args); |
789 | Safefree (coro); |
838 | |
|
|
839 | return 1; |
790 | } |
840 | } |
791 | |
841 | |
792 | static int |
842 | static int |
793 | coro_state_clear (pTHX_ SV *sv, MAGIC *mg) |
843 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
794 | { |
844 | { |
795 | struct coro *coro = (struct coro *)mg->mg_ptr; |
845 | struct coro *coro = (struct coro *)mg->mg_ptr; |
796 | mg->mg_ptr = 0; |
846 | mg->mg_ptr = 0; |
797 | |
847 | |
|
|
848 | if (--coro->refcnt < 0) |
|
|
849 | { |
798 | coro_state_destroy (coro); |
850 | coro_state_destroy (coro); |
|
|
851 | Safefree (coro); |
|
|
852 | } |
799 | |
853 | |
800 | return 0; |
854 | return 0; |
801 | } |
855 | } |
802 | |
856 | |
803 | static int |
857 | static int |
… | |
… | |
808 | ++coro->refcnt; |
862 | ++coro->refcnt; |
809 | |
863 | |
810 | return 0; |
864 | return 0; |
811 | } |
865 | } |
812 | |
866 | |
813 | static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; |
867 | static MGVTBL coro_state_vtbl = { |
|
|
868 | 0, 0, 0, 0, |
|
|
869 | coro_state_free, |
|
|
870 | 0, |
|
|
871 | #ifdef MGf_DUP |
|
|
872 | coro_state_dup, |
|
|
873 | #else |
|
|
874 | # define MGf_DUP 0 |
|
|
875 | #endif |
|
|
876 | }; |
814 | |
877 | |
815 | static struct coro * |
878 | static struct coro * |
816 | SvSTATE (SV *coro) |
879 | SvSTATE (SV *coro) |
817 | { |
880 | { |
818 | HV *stash; |
881 | HV *stash; |
… | |
… | |
833 | assert (mg->mg_type == PERL_MAGIC_ext); |
896 | assert (mg->mg_type == PERL_MAGIC_ext); |
834 | return (struct coro *)mg->mg_ptr; |
897 | return (struct coro *)mg->mg_ptr; |
835 | } |
898 | } |
836 | |
899 | |
837 | static void |
900 | static void |
838 | prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags) |
901 | prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
839 | { |
902 | { |
840 | ta->prev = SvSTATE (prev); |
903 | ta->prev = SvSTATE (prev_sv); |
841 | ta->next = SvSTATE (next); |
904 | ta->next = SvSTATE (next_sv); |
842 | ta->flags = flags; |
|
|
843 | } |
905 | } |
844 | |
906 | |
845 | static void |
907 | static void |
846 | api_transfer (SV *prev, SV *next, int flags) |
908 | api_transfer (SV *prev_sv, SV *next_sv) |
847 | { |
909 | { |
848 | dTHX; |
|
|
849 | struct transfer_args ta; |
910 | struct transfer_args ta; |
850 | |
911 | |
851 | prepare_transfer (&ta, prev, next, flags); |
912 | prepare_transfer (&ta, prev_sv, next_sv); |
852 | TRANSFER (ta); |
913 | TRANSFER (ta); |
|
|
914 | } |
|
|
915 | |
|
|
916 | static int |
|
|
917 | api_save (SV *coro_sv, int new_save) |
|
|
918 | { |
|
|
919 | struct coro *coro = SvSTATE (coro_sv); |
|
|
920 | int old_save = coro->save; |
|
|
921 | |
|
|
922 | if (new_save >= 0) |
|
|
923 | coro->save = new_save; |
|
|
924 | |
|
|
925 | return old_save; |
853 | } |
926 | } |
854 | |
927 | |
855 | /** Coro ********************************************************************/ |
928 | /** Coro ********************************************************************/ |
856 | |
929 | |
857 | #define PRIO_MAX 3 |
930 | #define PRIO_MAX 3 |
… | |
… | |
860 | #define PRIO_LOW -1 |
933 | #define PRIO_LOW -1 |
861 | #define PRIO_IDLE -3 |
934 | #define PRIO_IDLE -3 |
862 | #define PRIO_MIN -4 |
935 | #define PRIO_MIN -4 |
863 | |
936 | |
864 | /* for Coro.pm */ |
937 | /* for Coro.pm */ |
865 | static GV *coro_current, *coro_idle; |
938 | static SV *coro_current; |
866 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
939 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
867 | static int coro_nready; |
940 | static int coro_nready; |
868 | |
941 | |
869 | static void |
942 | static void |
870 | coro_enq (SV *sv) |
943 | coro_enq (SV *coro_sv) |
871 | { |
944 | { |
872 | int prio; |
|
|
873 | |
|
|
874 | if (SvTYPE (sv) != SVt_PVHV) |
|
|
875 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
|
|
876 | |
|
|
877 | prio = SvSTATE (sv)->prio; |
|
|
878 | |
|
|
879 | av_push (coro_ready [prio - PRIO_MIN], sv); |
945 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
880 | coro_nready++; |
946 | coro_nready++; |
881 | } |
947 | } |
882 | |
948 | |
883 | static SV * |
949 | static SV * |
884 | coro_deq (int min_prio) |
950 | coro_deq (int min_prio) |
… | |
… | |
897 | } |
963 | } |
898 | |
964 | |
899 | return 0; |
965 | return 0; |
900 | } |
966 | } |
901 | |
967 | |
902 | static void |
968 | static int |
903 | api_ready (SV *coro) |
969 | api_ready (SV *coro_sv) |
904 | { |
970 | { |
905 | dTHX; |
971 | struct coro *coro; |
906 | |
972 | |
907 | if (SvROK (coro)) |
973 | if (SvROK (coro_sv)) |
908 | coro = SvRV (coro); |
974 | coro_sv = SvRV (coro_sv); |
|
|
975 | |
|
|
976 | coro = SvSTATE (coro_sv); |
|
|
977 | |
|
|
978 | if (coro->flags & CF_READY) |
|
|
979 | return 0; |
|
|
980 | |
|
|
981 | coro->flags |= CF_READY; |
909 | |
982 | |
910 | LOCK; |
983 | LOCK; |
911 | coro_enq (SvREFCNT_inc (coro)); |
984 | coro_enq (SvREFCNT_inc (coro_sv)); |
912 | UNLOCK; |
985 | UNLOCK; |
|
|
986 | |
|
|
987 | return 1; |
|
|
988 | } |
|
|
989 | |
|
|
990 | static int |
|
|
991 | api_is_ready (SV *coro_sv) |
|
|
992 | { |
|
|
993 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
913 | } |
994 | } |
914 | |
995 | |
915 | static void |
996 | static void |
916 | prepare_schedule (struct transfer_args *ta) |
997 | prepare_schedule (struct transfer_args *ta) |
917 | { |
998 | { |
918 | SV *current, *prev, *next; |
999 | SV *prev_sv, *next_sv; |
919 | |
|
|
920 | current = GvSV (coro_current); |
|
|
921 | |
1000 | |
922 | for (;;) |
1001 | for (;;) |
923 | { |
1002 | { |
924 | LOCK; |
1003 | LOCK; |
925 | next = coro_deq (PRIO_MIN); |
1004 | next_sv = coro_deq (PRIO_MIN); |
926 | UNLOCK; |
1005 | UNLOCK; |
927 | |
1006 | |
|
|
1007 | /* nothing to schedule: call the idle handler */ |
928 | if (next) |
1008 | if (!next_sv) |
929 | break; |
|
|
930 | |
|
|
931 | { |
1009 | { |
932 | dSP; |
1010 | dSP; |
933 | |
1011 | |
934 | ENTER; |
1012 | ENTER; |
935 | SAVETMPS; |
1013 | SAVETMPS; |
936 | |
1014 | |
937 | PUSHMARK (SP); |
1015 | PUSHMARK (SP); |
938 | PUTBACK; |
1016 | PUTBACK; |
939 | call_sv (GvSV (coro_idle), G_DISCARD); |
1017 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
940 | |
1018 | |
941 | FREETMPS; |
1019 | FREETMPS; |
942 | LEAVE; |
1020 | LEAVE; |
|
|
1021 | continue; |
943 | } |
1022 | } |
944 | } |
|
|
945 | |
1023 | |
946 | prev = SvRV (current); |
1024 | ta->next = SvSTATE (next_sv); |
947 | SvRV (current) = next; |
1025 | |
|
|
1026 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1027 | if (ta->next->flags & CF_DESTROYED) |
|
|
1028 | { |
|
|
1029 | SvREFCNT_dec (next_sv); |
|
|
1030 | continue; |
|
|
1031 | } |
|
|
1032 | |
|
|
1033 | break; |
|
|
1034 | } |
948 | |
1035 | |
949 | /* free this only after the transfer */ |
1036 | /* free this only after the transfer */ |
|
|
1037 | prev_sv = SvRV (coro_current); |
|
|
1038 | SvRV_set (coro_current, next_sv); |
|
|
1039 | ta->prev = SvSTATE (prev_sv); |
|
|
1040 | |
|
|
1041 | assert (ta->next->flags & CF_READY); |
|
|
1042 | ta->next->flags &= ~CF_READY; |
|
|
1043 | |
950 | LOCK; |
1044 | LOCK; |
951 | free_coro_mortal (); |
1045 | free_coro_mortal (); |
|
|
1046 | coro_mortal = prev_sv; |
952 | UNLOCK; |
1047 | UNLOCK; |
953 | coro_mortal = prev; |
|
|
954 | |
|
|
955 | ta->prev = SvSTATE (prev); |
|
|
956 | ta->next = SvSTATE (next); |
|
|
957 | ta->flags = TRANSFER_SAVE_ALL; |
|
|
958 | } |
1048 | } |
959 | |
1049 | |
960 | static void |
1050 | static void |
961 | prepare_cede (struct transfer_args *ta) |
1051 | prepare_cede (struct transfer_args *ta) |
962 | { |
1052 | { |
963 | LOCK; |
1053 | api_ready (coro_current); |
964 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
|
|
965 | UNLOCK; |
|
|
966 | |
|
|
967 | prepare_schedule (ta); |
1054 | prepare_schedule (ta); |
968 | } |
1055 | } |
969 | |
1056 | |
|
|
1057 | static int |
|
|
1058 | prepare_cede_notself (struct transfer_args *ta) |
|
|
1059 | { |
|
|
1060 | if (coro_nready) |
|
|
1061 | { |
|
|
1062 | SV *prev = SvRV (coro_current); |
|
|
1063 | prepare_schedule (ta); |
|
|
1064 | api_ready (prev); |
|
|
1065 | return 1; |
|
|
1066 | } |
|
|
1067 | else |
|
|
1068 | return 0; |
|
|
1069 | } |
|
|
1070 | |
970 | static void |
1071 | static void |
971 | api_schedule (void) |
1072 | api_schedule (void) |
972 | { |
1073 | { |
973 | dTHX; |
|
|
974 | struct transfer_args ta; |
1074 | struct transfer_args ta; |
975 | |
1075 | |
976 | prepare_schedule (&ta); |
1076 | prepare_schedule (&ta); |
977 | TRANSFER (ta); |
1077 | TRANSFER (ta); |
978 | } |
1078 | } |
979 | |
1079 | |
980 | static void |
1080 | static int |
981 | api_cede (void) |
1081 | api_cede (void) |
982 | { |
1082 | { |
983 | dTHX; |
|
|
984 | struct transfer_args ta; |
1083 | struct transfer_args ta; |
985 | |
1084 | |
986 | prepare_cede (&ta); |
1085 | prepare_cede (&ta); |
|
|
1086 | |
|
|
1087 | if (ta.prev != ta.next) |
|
|
1088 | { |
987 | TRANSFER (ta); |
1089 | TRANSFER (ta); |
|
|
1090 | return 1; |
|
|
1091 | } |
|
|
1092 | else |
|
|
1093 | return 0; |
|
|
1094 | } |
|
|
1095 | |
|
|
1096 | static int |
|
|
1097 | api_cede_notself (void) |
|
|
1098 | { |
|
|
1099 | struct transfer_args ta; |
|
|
1100 | |
|
|
1101 | if (prepare_cede_notself (&ta)) |
|
|
1102 | { |
|
|
1103 | TRANSFER (ta); |
|
|
1104 | return 1; |
|
|
1105 | } |
|
|
1106 | else |
|
|
1107 | return 0; |
988 | } |
1108 | } |
989 | |
1109 | |
990 | MODULE = Coro::State PACKAGE = Coro::State |
1110 | MODULE = Coro::State PACKAGE = Coro::State |
991 | |
1111 | |
992 | PROTOTYPES: DISABLE |
1112 | PROTOTYPES: DISABLE |
… | |
… | |
998 | #endif |
1118 | #endif |
999 | BOOT_PAGESIZE; |
1119 | BOOT_PAGESIZE; |
1000 | |
1120 | |
1001 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1121 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1002 | |
1122 | |
1003 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
1123 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1004 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
1124 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1005 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
1125 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
|
|
1126 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
|
|
1127 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
1006 | |
1128 | |
1007 | main_mainstack = PL_mainstack; |
1129 | main_mainstack = PL_mainstack; |
1008 | |
1130 | |
1009 | coroapi.ver = CORO_API_VERSION; |
1131 | coroapi.ver = CORO_API_VERSION; |
1010 | coroapi.transfer = api_transfer; |
1132 | coroapi.transfer = api_transfer; |
… | |
… | |
1020 | HV *hv; |
1142 | HV *hv; |
1021 | int i; |
1143 | int i; |
1022 | |
1144 | |
1023 | Newz (0, coro, 1, struct coro); |
1145 | Newz (0, coro, 1, struct coro); |
1024 | coro->args = newAV (); |
1146 | coro->args = newAV (); |
|
|
1147 | coro->save = CORO_SAVE_ALL; |
|
|
1148 | coro->flags = CF_NEW; |
1025 | |
1149 | |
1026 | hv = newHV (); |
1150 | hv = newHV (); |
1027 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1151 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1028 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1152 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1029 | |
1153 | |
1030 | for (i = 1; i < items; i++) |
1154 | for (i = 1; i < items; i++) |
1031 | av_push (coro->args, newSVsv (ST (i))); |
1155 | av_push (coro->args, newSVsv (ST (i))); |
1032 | |
|
|
1033 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
|
|
1034 | /*coro->stack = 0;*/ |
|
|
1035 | } |
1156 | } |
1036 | OUTPUT: |
1157 | OUTPUT: |
|
|
1158 | RETVAL |
|
|
1159 | |
|
|
1160 | int |
|
|
1161 | save (SV *coro, int new_save = -1) |
|
|
1162 | CODE: |
|
|
1163 | RETVAL = api_save (coro, new_save); |
|
|
1164 | OUTPUT: |
1037 | RETVAL |
1165 | RETVAL |
1038 | |
1166 | |
1039 | void |
1167 | void |
1040 | _set_stacklevel (...) |
1168 | _set_stacklevel (...) |
1041 | ALIAS: |
1169 | ALIAS: |
1042 | Coro::State::transfer = 1 |
1170 | Coro::State::transfer = 1 |
1043 | Coro::schedule = 2 |
1171 | Coro::schedule = 2 |
1044 | Coro::cede = 3 |
1172 | Coro::cede = 3 |
1045 | Coro::Cont::yield = 4 |
1173 | Coro::cede_notself = 4 |
1046 | CODE: |
1174 | CODE: |
1047 | { |
1175 | { |
1048 | struct transfer_args ta; |
1176 | struct transfer_args ta; |
1049 | |
1177 | |
1050 | switch (ix) |
1178 | switch (ix) |
1051 | { |
1179 | { |
1052 | case 0: |
1180 | case 0: |
1053 | ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); |
1181 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1054 | ta.next = 0; |
1182 | ta.next = 0; |
1055 | ta.flags = TRANSFER_SET_STACKLEVEL; |
|
|
1056 | break; |
1183 | break; |
1057 | |
1184 | |
1058 | case 1: |
1185 | case 1: |
1059 | if (items != 3) |
1186 | if (items != 2) |
1060 | croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); |
1187 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1061 | |
1188 | |
1062 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
1189 | prepare_transfer (&ta, ST (0), ST (1)); |
1063 | break; |
1190 | break; |
1064 | |
1191 | |
1065 | case 2: |
1192 | case 2: |
1066 | prepare_schedule (&ta); |
1193 | prepare_schedule (&ta); |
1067 | break; |
1194 | break; |
… | |
… | |
1069 | case 3: |
1196 | case 3: |
1070 | prepare_cede (&ta); |
1197 | prepare_cede (&ta); |
1071 | break; |
1198 | break; |
1072 | |
1199 | |
1073 | case 4: |
1200 | case 4: |
1074 | { |
1201 | if (!prepare_cede_notself (&ta)) |
1075 | SV *yieldstack; |
1202 | XSRETURN_EMPTY; |
1076 | SV *sv; |
|
|
1077 | AV *defav = GvAV (PL_defgv); |
|
|
1078 | |
1203 | |
1079 | yieldstack = *hv_fetch ( |
|
|
1080 | (HV *)SvRV (GvSV (coro_current)), |
|
|
1081 | "yieldstack", sizeof ("yieldstack") - 1, |
|
|
1082 | 0 |
|
|
1083 | ); |
|
|
1084 | |
|
|
1085 | /* set up @_ -- ugly */ |
|
|
1086 | av_clear (defav); |
|
|
1087 | av_fill (defav, items - 1); |
|
|
1088 | while (items--) |
|
|
1089 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
|
|
1090 | |
|
|
1091 | sv = av_pop ((AV *)SvRV (yieldstack)); |
|
|
1092 | ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0)); |
|
|
1093 | ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0)); |
|
|
1094 | ta.flags = 0; |
|
|
1095 | SvREFCNT_dec (sv); |
|
|
1096 | } |
|
|
1097 | break; |
1204 | break; |
1098 | |
|
|
1099 | } |
1205 | } |
1100 | |
1206 | |
|
|
1207 | BARRIER; |
1101 | TRANSFER (ta); |
1208 | TRANSFER (ta); |
1102 | } |
1209 | } |
1103 | |
1210 | |
1104 | void |
1211 | bool |
1105 | _clone_state_from (SV *dst, SV *src) |
1212 | _destroy (SV *coro_sv) |
1106 | CODE: |
1213 | CODE: |
1107 | { |
1214 | RETVAL = coro_state_destroy (SvSTATE (coro_sv)); |
1108 | struct coro *coro_src = SvSTATE (src); |
1215 | OUTPUT: |
1109 | |
1216 | RETVAL |
1110 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
|
|
1111 | |
|
|
1112 | ++coro_src->refcnt; |
|
|
1113 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
|
|
1114 | } |
|
|
1115 | |
|
|
1116 | void |
|
|
1117 | _nonlocal_goto (IV nextop) |
|
|
1118 | CODE: |
|
|
1119 | /* uuh, somebody will kill me again for this */ |
|
|
1120 | PL_op->op_next = INT2PTR (OP *, nextop); |
|
|
1121 | |
1217 | |
1122 | void |
1218 | void |
1123 | _exit (code) |
1219 | _exit (code) |
1124 | int code |
1220 | int code |
1125 | PROTOTYPE: $ |
1221 | PROTOTYPE: $ |
1126 | CODE: |
1222 | CODE: |
1127 | _exit (code); |
1223 | _exit (code); |
1128 | |
1224 | |
|
|
1225 | int |
|
|
1226 | cctx_count () |
|
|
1227 | CODE: |
|
|
1228 | RETVAL = cctx_count; |
|
|
1229 | OUTPUT: |
|
|
1230 | RETVAL |
|
|
1231 | |
|
|
1232 | int |
|
|
1233 | cctx_idle () |
|
|
1234 | CODE: |
|
|
1235 | RETVAL = cctx_idle; |
|
|
1236 | OUTPUT: |
|
|
1237 | RETVAL |
|
|
1238 | |
1129 | MODULE = Coro::State PACKAGE = Coro |
1239 | MODULE = Coro::State PACKAGE = Coro |
1130 | |
1240 | |
1131 | BOOT: |
1241 | BOOT: |
1132 | { |
1242 | { |
1133 | int i; |
1243 | int i; |
… | |
… | |
1139 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1249 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1140 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1250 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1141 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1251 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1142 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1252 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1143 | |
1253 | |
1144 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
1254 | coro_current = get_sv ("Coro::current", FALSE); |
1145 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
1255 | SvREADONLY_on (coro_current); |
1146 | |
1256 | |
1147 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1257 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1148 | coro_ready[i] = newAV (); |
1258 | coro_ready[i] = newAV (); |
1149 | |
1259 | |
1150 | { |
1260 | { |
1151 | SV *sv = perl_get_sv("Coro::API", 1); |
1261 | SV *sv = perl_get_sv("Coro::API", 1); |
1152 | |
1262 | |
1153 | coroapi.schedule = api_schedule; |
1263 | coroapi.schedule = api_schedule; |
|
|
1264 | coroapi.save = api_save; |
1154 | coroapi.cede = api_cede; |
1265 | coroapi.cede = api_cede; |
|
|
1266 | coroapi.cede_notself = api_cede_notself; |
1155 | coroapi.ready = api_ready; |
1267 | coroapi.ready = api_ready; |
|
|
1268 | coroapi.is_ready = api_is_ready; |
1156 | coroapi.nready = &coro_nready; |
1269 | coroapi.nready = &coro_nready; |
1157 | coroapi.current = coro_current; |
1270 | coroapi.current = coro_current; |
1158 | |
1271 | |
1159 | GCoroAPI = &coroapi; |
1272 | GCoroAPI = &coroapi; |
1160 | sv_setiv (sv, (IV)&coroapi); |
1273 | sv_setiv (sv, (IV)&coroapi); |
1161 | SvREADONLY_on (sv); |
1274 | SvREADONLY_on (sv); |
1162 | } |
1275 | } |
1163 | } |
1276 | } |
|
|
1277 | |
|
|
1278 | void |
|
|
1279 | _set_current (SV *current) |
|
|
1280 | PROTOTYPE: $ |
|
|
1281 | CODE: |
|
|
1282 | SvREFCNT_dec (SvRV (coro_current)); |
|
|
1283 | SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); |
1164 | |
1284 | |
1165 | int |
1285 | int |
1166 | prio (Coro::State coro, int newprio = 0) |
1286 | prio (Coro::State coro, int newprio = 0) |
1167 | ALIAS: |
1287 | ALIAS: |
1168 | nice = 1 |
1288 | nice = 1 |
… | |
… | |
1171 | RETVAL = coro->prio; |
1291 | RETVAL = coro->prio; |
1172 | |
1292 | |
1173 | if (items > 1) |
1293 | if (items > 1) |
1174 | { |
1294 | { |
1175 | if (ix) |
1295 | if (ix) |
1176 | newprio += coro->prio; |
1296 | newprio = coro->prio - newprio; |
1177 | |
1297 | |
1178 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
1298 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
1179 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
1299 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
1180 | |
1300 | |
1181 | coro->prio = newprio; |
1301 | coro->prio = newprio; |
1182 | } |
1302 | } |
1183 | } |
1303 | } |
|
|
1304 | OUTPUT: |
|
|
1305 | RETVAL |
1184 | |
1306 | |
1185 | void |
1307 | SV * |
1186 | ready (SV *self) |
1308 | ready (SV *self) |
1187 | PROTOTYPE: $ |
1309 | PROTOTYPE: $ |
1188 | CODE: |
1310 | CODE: |
1189 | api_ready (self); |
1311 | RETVAL = boolSV (api_ready (self)); |
|
|
1312 | OUTPUT: |
|
|
1313 | RETVAL |
|
|
1314 | |
|
|
1315 | SV * |
|
|
1316 | is_ready (SV *self) |
|
|
1317 | PROTOTYPE: $ |
|
|
1318 | CODE: |
|
|
1319 | RETVAL = boolSV (api_is_ready (self)); |
|
|
1320 | OUTPUT: |
|
|
1321 | RETVAL |
1190 | |
1322 | |
1191 | int |
1323 | int |
1192 | nready (...) |
1324 | nready (...) |
1193 | PROTOTYPE: |
1325 | PROTOTYPE: |
1194 | CODE: |
1326 | CODE: |
… | |
… | |
1200 | |
1332 | |
1201 | SV * |
1333 | SV * |
1202 | _get_state () |
1334 | _get_state () |
1203 | CODE: |
1335 | CODE: |
1204 | { |
1336 | { |
1205 | struct { |
1337 | struct io_state *data; |
1206 | int errorno; |
|
|
1207 | int laststype; |
|
|
1208 | int laststatval; |
|
|
1209 | Stat_t statcache; |
|
|
1210 | } data; |
|
|
1211 | |
1338 | |
|
|
1339 | RETVAL = newSV (sizeof (struct io_state)); |
|
|
1340 | data = (struct io_state *)SvPVX (RETVAL); |
|
|
1341 | SvCUR_set (RETVAL, sizeof (struct io_state)); |
|
|
1342 | SvPOK_only (RETVAL); |
|
|
1343 | |
1212 | data.errorno = errno; |
1344 | data->errorno = errno; |
1213 | data.laststype = PL_laststype; |
1345 | data->laststype = PL_laststype; |
1214 | data.laststatval = PL_laststatval; |
1346 | data->laststatval = PL_laststatval; |
1215 | data.statcache = PL_statcache; |
1347 | data->statcache = PL_statcache; |
1216 | |
|
|
1217 | RETVAL = newSVpvn ((char *)&data, sizeof data); |
|
|
1218 | } |
1348 | } |
1219 | OUTPUT: |
1349 | OUTPUT: |
1220 | RETVAL |
1350 | RETVAL |
1221 | |
1351 | |
1222 | void |
1352 | void |
1223 | _set_state (char *data_) |
1353 | _set_state (char *data_) |
1224 | PROTOTYPE: $ |
1354 | PROTOTYPE: $ |
1225 | CODE: |
1355 | CODE: |
1226 | { |
1356 | { |
1227 | struct { |
1357 | struct io_state *data = (void *)data_; |
1228 | int errorno; |
|
|
1229 | int laststype; |
|
|
1230 | int laststatval; |
|
|
1231 | Stat_t statcache; |
|
|
1232 | } *data = (void *)data_; |
|
|
1233 | |
1358 | |
1234 | errno = data->errorno; |
1359 | errno = data->errorno; |
1235 | PL_laststype = data->laststype; |
1360 | PL_laststype = data->laststype; |
1236 | PL_laststatval = data->laststatval; |
1361 | PL_laststatval = data->laststatval; |
1237 | PL_statcache = data->statcache; |
1362 | PL_statcache = data->statcache; |
1238 | } |
1363 | } |
|
|
1364 | |