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