… | |
… | |
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 | #if USE_VALGRIND |
|
|
10 | # include <valgrind/valgrind.h> |
|
|
11 | #endif |
|
|
12 | |
|
|
13 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
|
|
14 | (PERL_REVISION > (a) \ |
|
|
15 | || (PERL_REVISION == (a) \ |
|
|
16 | && (PERL_VERSION > (b) \ |
|
|
17 | || (PERL_VERSION == (b) && PERLSUBVERSION >= (c))))) |
|
|
18 | |
|
|
19 | #if !PERL_VERSION_ATLEAST (5,6,0) |
10 | # ifndef PL_ppaddr |
20 | # ifndef PL_ppaddr |
11 | # define PL_ppaddr ppaddr |
21 | # define PL_ppaddr ppaddr |
12 | # endif |
22 | # endif |
13 | # ifndef call_sv |
23 | # ifndef call_sv |
14 | # define call_sv perl_call_sv |
24 | # define call_sv perl_call_sv |
… | |
… | |
52 | # ifndef PAGESIZE |
62 | # ifndef PAGESIZE |
53 | # define PAGESIZE pagesize |
63 | # define PAGESIZE pagesize |
54 | # define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) |
64 | # define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) |
55 | static long pagesize; |
65 | static long pagesize; |
56 | # else |
66 | # else |
57 | # define BOOT_PAGESIZE |
67 | # define BOOT_PAGESIZE (void)0 |
58 | # endif |
68 | # endif |
|
|
69 | #else |
|
|
70 | # define PAGESIZE 0 |
|
|
71 | # define BOOT_PAGESIZE (void)0 |
59 | #endif |
72 | #endif |
60 | |
73 | |
61 | /* The next macro should declare a variable stacklevel that contains and approximation |
74 | /* 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 |
75 | * to the current C stack pointer. Its property is that it changes with each call |
63 | * and should be unique. */ |
76 | * and should be unique. */ |
64 | #define dSTACKLEVEL int stacklevel |
77 | #define dSTACKLEVEL int stacklevel |
65 | #define STACKLEVEL ((void *)&stacklevel) |
78 | #define STACKLEVEL ((void *)&stacklevel) |
66 | |
79 | |
67 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
80 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
|
|
81 | |
|
|
82 | #if __GNUC__ >= 3 |
|
|
83 | # define attribute(x) __attribute__(x) |
|
|
84 | #else |
|
|
85 | # define attribute(x) |
|
|
86 | #endif |
|
|
87 | |
|
|
88 | #define NOINLINE attribute ((noinline)) |
68 | |
89 | |
69 | #include "CoroAPI.h" |
90 | #include "CoroAPI.h" |
70 | |
91 | |
71 | #define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ |
92 | #define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ |
72 | |
93 | |
… | |
… | |
82 | static struct CoroAPI coroapi; |
103 | static struct CoroAPI coroapi; |
83 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
104 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
84 | static HV *coro_state_stash, *coro_stash; |
105 | static HV *coro_state_stash, *coro_stash; |
85 | static SV *coro_mortal; /* will be freed after next transfer */ |
106 | static SV *coro_mortal; /* will be freed after next transfer */ |
86 | |
107 | |
|
|
108 | static struct coro_cctx *cctx_first; |
|
|
109 | static int cctx_count, cctx_idle; |
|
|
110 | |
87 | /* this is a structure representing a c-level coroutine */ |
111 | /* this is a structure representing a c-level coroutine */ |
88 | typedef struct coro_stack { |
112 | typedef struct coro_cctx { |
89 | struct coro_stack *next; |
113 | struct coro_cctx *next; |
90 | |
114 | |
91 | /* the stack */ |
115 | /* the stack */ |
92 | void *sptr; |
116 | void *sptr; |
93 | long ssize; /* positive == mmap, otherwise malloc */ |
117 | long ssize; /* positive == mmap, otherwise malloc */ |
94 | |
118 | |
95 | /* cpu state */ |
119 | /* cpu state */ |
96 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
120 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
97 | JMPENV *top_env; |
121 | JMPENV *top_env; |
98 | coro_context cctx; |
122 | coro_context cctx; |
99 | } coro_stack; |
123 | |
|
|
124 | #if USE_VALGRIND |
|
|
125 | int valgrind_id; |
|
|
126 | #endif |
|
|
127 | } coro_cctx; |
100 | |
128 | |
101 | /* this is a structure representing a perl-level coroutine */ |
129 | /* this is a structure representing a perl-level coroutine */ |
102 | struct coro { |
130 | struct coro { |
103 | /* the c coroutine allocated to this perl coroutine, if any */ |
131 | /* the c coroutine allocated to this perl coroutine, if any */ |
104 | coro_stack *stack; |
132 | coro_cctx *cctx; |
105 | |
133 | |
106 | /* data associated with this coroutine (initial args) */ |
134 | /* data associated with this coroutine (initial args) */ |
107 | AV *args; |
135 | AV *args; |
108 | int refcnt; |
136 | int refcnt; |
109 | |
137 | |
110 | /* optionally saved, might be zero */ |
138 | /* optionally saved, might be zero */ |
111 | AV *defav; |
139 | AV *defav; |
112 | SV *defsv; |
140 | SV *defsv; |
113 | SV *errsv; |
141 | SV *errsv; |
114 | |
142 | |
115 | /* saved global state not related to stacks */ |
143 | #define VAR(name,type) type name; |
116 | U8 dowarn; |
144 | # include "state.h" |
117 | I32 in_eval; |
145 | #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 | |
146 | |
149 | /* coro process data */ |
147 | /* coro process data */ |
150 | int prio; |
148 | int prio; |
151 | }; |
149 | }; |
152 | |
150 | |
… | |
… | |
159 | AV *padlist = CvPADLIST (cv); |
157 | AV *padlist = CvPADLIST (cv); |
160 | AV *newpadlist, *newpad; |
158 | AV *newpadlist, *newpad; |
161 | |
159 | |
162 | newpadlist = newAV (); |
160 | newpadlist = newAV (); |
163 | AvREAL_off (newpadlist); |
161 | AvREAL_off (newpadlist); |
164 | #if PERL_VERSION < 9 |
162 | #if PERL_VERSION_ATLEAST (5,9,0) |
|
|
163 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); |
|
|
164 | #else |
165 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
165 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
166 | #else |
|
|
167 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); |
|
|
168 | #endif |
166 | #endif |
169 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
167 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
170 | --AvFILLp (padlist); |
168 | --AvFILLp (padlist); |
171 | |
169 | |
172 | av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); |
170 | av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); |
… | |
… | |
216 | |
214 | |
217 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
215 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
218 | |
216 | |
219 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
217 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
220 | |
218 | |
|
|
219 | #define CORO_MAGIC(cv) \ |
|
|
220 | SvMAGIC (cv) \ |
|
|
221 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
|
|
222 | ? SvMAGIC (cv) \ |
|
|
223 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
|
|
224 | : 0 |
|
|
225 | |
221 | /* the next two functions merely cache the padlists */ |
226 | /* the next two functions merely cache the padlists */ |
222 | static void |
227 | static void |
223 | get_padlist (CV *cv) |
228 | get_padlist (CV *cv) |
224 | { |
229 | { |
225 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
230 | MAGIC *mg = CORO_MAGIC (cv); |
|
|
231 | AV *av; |
226 | |
232 | |
227 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
233 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
228 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
234 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
229 | else |
235 | else |
230 | { |
236 | { |
231 | #if 0 |
237 | #if 0 |
232 | /* this is probably cleaner, but also slower? */ |
238 | /* this is probably cleaner, but also slower? */ |
233 | CV *cp = Perl_cv_clone (cv); |
239 | CV *cp = Perl_cv_clone (cv); |
… | |
… | |
241 | } |
247 | } |
242 | |
248 | |
243 | static void |
249 | static void |
244 | put_padlist (CV *cv) |
250 | put_padlist (CV *cv) |
245 | { |
251 | { |
246 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
252 | MAGIC *mg = CORO_MAGIC (cv); |
|
|
253 | AV *av; |
247 | |
254 | |
248 | if (!mg) |
255 | if (!mg) |
249 | { |
256 | { |
250 | sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); |
257 | sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); |
251 | mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
258 | mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
252 | mg->mg_virtual = &vtbl_coro; |
259 | mg->mg_virtual = &vtbl_coro; |
253 | mg->mg_obj = (SV *)newAV (); |
260 | mg->mg_obj = (SV *)newAV (); |
254 | } |
261 | } |
255 | |
262 | |
256 | av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv)); |
263 | av = (AV *)mg->mg_obj; |
|
|
264 | |
|
|
265 | if (AvFILLp (av) >= AvMAX (av)) |
|
|
266 | av_extend (av, AvMAX (av) + 1); |
|
|
267 | |
|
|
268 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
257 | } |
269 | } |
258 | |
270 | |
259 | #define SB do { |
271 | #define SB do { |
260 | #define SE } while (0) |
272 | #define SE } while (0) |
261 | |
273 | |
… | |
… | |
265 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
277 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
266 | |
278 | |
267 | static void |
279 | static void |
268 | load_state(Coro__State c) |
280 | load_state(Coro__State c) |
269 | { |
281 | { |
270 | PL_dowarn = c->dowarn; |
282 | #define VAR(name,type) PL_ ## name = c->name; |
271 | PL_in_eval = c->in_eval; |
283 | # include "state.h" |
272 | |
284 | #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 | |
285 | |
304 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
286 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
305 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
287 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
306 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
288 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
307 | |
289 | |
… | |
… | |
310 | CV *cv; |
292 | CV *cv; |
311 | |
293 | |
312 | /* now do the ugly restore mess */ |
294 | /* now do the ugly restore mess */ |
313 | while ((cv = (CV *)POPs)) |
295 | while ((cv = (CV *)POPs)) |
314 | { |
296 | { |
315 | AV *padlist = (AV *)POPs; |
|
|
316 | |
|
|
317 | if (padlist) |
|
|
318 | { |
|
|
319 | put_padlist (cv); /* mark this padlist as available */ |
297 | put_padlist (cv); /* mark this padlist as available */ |
320 | CvPADLIST(cv) = padlist; |
298 | CvDEPTH (cv) = PTR2IV (POPs); |
321 | } |
299 | CvPADLIST (cv) = (AV *)POPs; |
322 | |
|
|
323 | ++CvDEPTH(cv); |
|
|
324 | } |
300 | } |
325 | |
301 | |
326 | PUTBACK; |
302 | PUTBACK; |
327 | } |
303 | } |
328 | } |
304 | } |
… | |
… | |
350 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
326 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
351 | |
327 | |
352 | if (CxTYPE(cx) == CXt_SUB) |
328 | if (CxTYPE(cx) == CXt_SUB) |
353 | { |
329 | { |
354 | CV *cv = cx->blk_sub.cv; |
330 | CV *cv = cx->blk_sub.cv; |
|
|
331 | |
355 | if (CvDEPTH(cv)) |
332 | if (CvDEPTH (cv)) |
356 | { |
333 | { |
357 | EXTEND (SP, CvDEPTH(cv)*2); |
334 | 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 | |
335 | |
366 | PUSHs ((SV *)CvPADLIST(cv)); |
336 | PUSHs ((SV *)CvPADLIST(cv)); |
|
|
337 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
367 | PUSHs ((SV *)cv); |
338 | PUSHs ((SV *)cv); |
368 | |
339 | |
|
|
340 | CvDEPTH (cv) = 0; |
369 | get_padlist (cv); |
341 | get_padlist (cv); |
370 | } |
342 | } |
371 | } |
343 | } |
372 | #ifdef CXt_FORMAT |
344 | #ifdef CXt_FORMAT |
373 | else if (CxTYPE(cx) == CXt_FORMAT) |
345 | else if (CxTYPE(cx) == CXt_FORMAT) |
… | |
… | |
392 | |
364 | |
393 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
365 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
394 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
366 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
395 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
367 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
396 | |
368 | |
397 | c->dowarn = PL_dowarn; |
369 | #define VAR(name,type)c->name = PL_ ## name; |
398 | c->in_eval = PL_in_eval; |
370 | # include "state.h" |
399 | |
371 | #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 | } |
372 | } |
431 | |
373 | |
432 | /* |
374 | /* |
433 | * allocate various perl stacks. This is an exact copy |
375 | * allocate various perl stacks. This is an exact copy |
434 | * of perl.c:init_stacks, except that it uses less memory |
376 | * of perl.c:init_stacks, except that it uses less memory |
… | |
… | |
466 | |
408 | |
467 | New(54,PL_savestack,96,ANY); |
409 | New(54,PL_savestack,96,ANY); |
468 | PL_savestack_ix = 0; |
410 | PL_savestack_ix = 0; |
469 | PL_savestack_max = 96; |
411 | PL_savestack_max = 96; |
470 | |
412 | |
471 | #if PERL_VERSION < 9 |
413 | #if !PERL_VERSION_ATLEAST (5,9,0) |
472 | New(54,PL_retstack,8,OP*); |
414 | New(54,PL_retstack,8,OP*); |
473 | PL_retstack_ix = 0; |
415 | PL_retstack_ix = 0; |
474 | PL_retstack_max = 8; |
416 | PL_retstack_max = 8; |
475 | #endif |
417 | #endif |
476 | } |
418 | } |
477 | |
419 | |
478 | /* |
420 | /* |
479 | * destroy the stacks, the callchain etc... |
421 | * destroy the stacks, the callchain etc... |
480 | */ |
422 | */ |
481 | static void |
423 | static void |
482 | destroy_stacks() |
424 | coro_destroy_stacks () |
483 | { |
425 | { |
484 | if (!IN_DESTRUCT) |
426 | if (!IN_DESTRUCT) |
485 | { |
427 | { |
486 | /* is this ugly, I ask? */ |
428 | /* is this ugly, I ask? */ |
487 | LEAVE_SCOPE (0); |
429 | LEAVE_SCOPE (0); |
… | |
… | |
518 | |
460 | |
519 | Safefree (PL_tmps_stack); |
461 | Safefree (PL_tmps_stack); |
520 | Safefree (PL_markstack); |
462 | Safefree (PL_markstack); |
521 | Safefree (PL_scopestack); |
463 | Safefree (PL_scopestack); |
522 | Safefree (PL_savestack); |
464 | Safefree (PL_savestack); |
523 | #if PERL_VERSION < 9 |
465 | #if !PERL_VERSION_ATLEAST (5,9,0) |
524 | Safefree (PL_retstack); |
466 | Safefree (PL_retstack); |
525 | #endif |
467 | #endif |
526 | } |
468 | } |
527 | |
469 | |
528 | static void |
470 | static void |
529 | setup_coro (struct coro *coro) |
471 | setup_coro (struct coro *coro) |
530 | { |
472 | { |
531 | /* |
473 | /* |
532 | * emulate part of the perl startup here. |
474 | * emulate part of the perl startup here. |
533 | */ |
475 | */ |
534 | dTHX; |
|
|
535 | dSP; |
|
|
536 | UNOP myop; |
|
|
537 | SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE); |
|
|
538 | |
476 | |
539 | coro_init_stacks (); |
477 | coro_init_stacks (); |
|
|
478 | |
540 | /*PL_curcop = 0;*/ |
479 | PL_curcop = 0; |
541 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
480 | PL_in_eval = 0; |
|
|
481 | PL_curpm = 0; |
|
|
482 | |
|
|
483 | { |
|
|
484 | dSP; |
|
|
485 | LOGOP myop; |
|
|
486 | |
|
|
487 | /* I have no idea why this is needed, but it is */ |
|
|
488 | PUSHMARK (SP); |
|
|
489 | |
542 | SvREFCNT_dec (GvAV (PL_defgv)); |
490 | SvREFCNT_dec (GvAV (PL_defgv)); |
543 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
491 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
544 | |
492 | |
545 | SPAGAIN; |
|
|
546 | |
|
|
547 | Zero (&myop, 1, UNOP); |
493 | Zero (&myop, 1, LOGOP); |
548 | myop.op_next = Nullop; |
494 | myop.op_next = Nullop; |
549 | myop.op_flags = OPf_WANT_VOID; |
495 | myop.op_flags = OPf_WANT_VOID; |
550 | |
496 | |
551 | PL_op = (OP *)&myop; |
497 | PL_op = (OP *)&myop; |
552 | |
498 | |
553 | PUSHMARK(SP); |
499 | PUSHMARK (SP); |
554 | XPUSHs (sub_init); |
500 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
555 | PUTBACK; |
501 | PUTBACK; |
556 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
502 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
557 | SPAGAIN; |
503 | SPAGAIN; |
558 | |
504 | |
559 | ENTER; /* necessary e.g. for dounwind */ |
505 | ENTER; /* necessary e.g. for dounwind */ |
|
|
506 | } |
560 | } |
507 | } |
561 | |
508 | |
562 | static void |
509 | static void |
563 | free_coro_mortal () |
510 | free_coro_mortal () |
564 | { |
511 | { |
… | |
… | |
567 | SvREFCNT_dec (coro_mortal); |
514 | SvREFCNT_dec (coro_mortal); |
568 | coro_mortal = 0; |
515 | coro_mortal = 0; |
569 | } |
516 | } |
570 | } |
517 | } |
571 | |
518 | |
572 | static void |
519 | static void NOINLINE |
573 | prepare_cctx (coro_stack *cctx) |
520 | prepare_cctx (coro_cctx *cctx) |
574 | { |
521 | { |
575 | dSP; |
522 | dSP; |
576 | UNOP myop; |
523 | LOGOP myop; |
577 | |
524 | |
578 | Zero (&myop, 1, UNOP); |
525 | Zero (&myop, 1, LOGOP); |
579 | myop.op_next = PL_op; |
526 | myop.op_next = PL_op; |
580 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
527 | myop.op_flags = OPf_WANT_VOID; |
581 | |
528 | |
|
|
529 | sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx)); |
|
|
530 | |
582 | PUSHMARK(SP); |
531 | PUSHMARK (SP); |
583 | EXTEND (SP, 2); |
|
|
584 | PUSHs (newSViv (PTR2IV (cctx))); |
|
|
585 | PUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE)); |
532 | XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
586 | PUTBACK; |
533 | PUTBACK; |
587 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
534 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
588 | SPAGAIN; |
535 | SPAGAIN; |
589 | } |
536 | } |
590 | |
537 | |
591 | static void |
538 | static void |
592 | coro_run (void *arg) |
539 | coro_run (void *arg) |
593 | { |
540 | { |
|
|
541 | /* coro_run is the alternative epilogue of transfer() */ |
|
|
542 | UNLOCK; |
|
|
543 | |
594 | /* |
544 | /* |
595 | * this is a _very_ stripped down perl interpreter ;) |
545 | * this is a _very_ stripped down perl interpreter ;) |
596 | */ |
546 | */ |
597 | UNLOCK; |
|
|
598 | |
|
|
599 | PL_top_env = &PL_start_env; |
547 | PL_top_env = &PL_start_env; |
|
|
548 | |
|
|
549 | /* inject call to cctx_init */ |
600 | prepare_cctx ((coro_stack *)arg); |
550 | prepare_cctx ((coro_cctx *)arg); |
601 | |
551 | |
602 | /* somebody will hit me for both perl_run and PL_restartop */ |
552 | /* somebody will hit me for both perl_run and PL_restartop */ |
603 | perl_run (PERL_GET_CONTEXT); |
553 | perl_run (PL_curinterp); |
604 | |
554 | |
605 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr); |
555 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
606 | abort (); |
556 | abort (); |
607 | } |
557 | } |
608 | |
558 | |
609 | static coro_stack * |
559 | static coro_cctx * |
610 | stack_new () |
560 | cctx_new () |
611 | { |
561 | { |
612 | coro_stack *stack; |
562 | coro_cctx *cctx; |
613 | |
563 | |
|
|
564 | ++cctx_count; |
|
|
565 | |
614 | New (0, stack, 1, coro_stack); |
566 | New (0, cctx, 1, coro_cctx); |
615 | |
567 | |
616 | #if HAVE_MMAP |
568 | #if HAVE_MMAP |
617 | |
569 | |
618 | stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
570 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
619 | /* mmap suppsedly does allocate-on-write for us */ |
571 | /* mmap suppsedly 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); |
572 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
621 | |
573 | |
622 | if (stack->sptr == (void *)-1) |
574 | if (cctx->sptr == (void *)-1) |
623 | { |
575 | { |
624 | perror ("FATAL: unable to mmap stack for coroutine"); |
576 | perror ("FATAL: unable to mmap stack for coroutine"); |
625 | _exit (EXIT_FAILURE); |
577 | _exit (EXIT_FAILURE); |
626 | } |
578 | } |
627 | |
579 | |
628 | # if STACKGUARD |
580 | # if STACKGUARD |
629 | mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
581 | mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
630 | # endif |
582 | # endif |
631 | |
583 | |
632 | #else |
584 | #else |
633 | |
585 | |
634 | stack->ssize = STACKSIZE * (long)sizeof (long); |
586 | cctx->ssize = STACKSIZE * (long)sizeof (long); |
635 | New (0, stack->sptr, STACKSIZE, long); |
587 | New (0, cctx->sptr, STACKSIZE, long); |
636 | |
588 | |
637 | if (!stack->sptr) |
589 | if (!cctx->sptr) |
638 | { |
590 | { |
639 | perror (stderr, "FATAL: unable to malloc stack for coroutine"); |
591 | perror ("FATAL: unable to malloc stack for coroutine"); |
640 | _exit (EXIT_FAILURE); |
592 | _exit (EXIT_FAILURE); |
641 | } |
593 | } |
642 | |
594 | |
643 | #endif |
595 | #endif |
644 | |
596 | |
|
|
597 | #if USE_VALGRIND |
|
|
598 | cctx->valgrind_id = VALGRIND_STACK_REGISTER ( |
|
|
599 | STACKGUARD * PAGESIZE + (char *)cctx->sptr, |
|
|
600 | cctx->ssize + (char *)cctx->sptr |
|
|
601 | ); |
|
|
602 | #endif |
|
|
603 | |
645 | coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); |
604 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); |
646 | |
605 | |
647 | return stack; |
606 | return cctx; |
648 | } |
607 | } |
649 | |
608 | |
650 | static void |
609 | static void |
651 | stack_free (coro_stack *stack) |
610 | cctx_free (coro_cctx *cctx) |
652 | { |
611 | { |
653 | if (!stack) |
612 | if (!cctx) |
654 | return; |
613 | return; |
655 | |
614 | |
|
|
615 | --cctx_count; |
|
|
616 | |
|
|
617 | #if USE_VALGRIND |
|
|
618 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
|
|
619 | #endif |
|
|
620 | |
656 | #if HAVE_MMAP |
621 | #if HAVE_MMAP |
657 | munmap (stack->sptr, stack->ssize); |
622 | munmap (cctx->sptr, cctx->ssize); |
658 | #else |
623 | #else |
659 | Safefree (stack->sptr); |
624 | Safefree (cctx->sptr); |
660 | #endif |
625 | #endif |
661 | |
626 | |
662 | Safefree (stack); |
627 | Safefree (cctx); |
663 | } |
628 | } |
664 | |
629 | |
665 | static coro_stack *stack_first; |
|
|
666 | |
|
|
667 | static coro_stack * |
630 | static coro_cctx * |
668 | stack_get () |
631 | cctx_get () |
669 | { |
632 | { |
670 | coro_stack *stack; |
633 | coro_cctx *cctx; |
671 | |
634 | |
672 | if (stack_first) |
635 | if (cctx_first) |
673 | { |
636 | { |
674 | stack = stack_first; |
637 | --cctx_idle; |
|
|
638 | cctx = cctx_first; |
675 | stack_first = stack->next; |
639 | cctx_first = cctx->next; |
676 | } |
640 | } |
677 | else |
641 | else |
678 | { |
642 | { |
679 | stack = stack_new (); |
643 | cctx = cctx_new (); |
680 | PL_op = PL_op->op_next; |
644 | PL_op = PL_op->op_next; |
681 | } |
645 | } |
682 | |
646 | |
683 | return stack; |
647 | return cctx; |
684 | } |
648 | } |
685 | |
649 | |
686 | static void |
650 | static void |
687 | stack_put (coro_stack *stack) |
651 | cctx_put (coro_cctx *cctx) |
688 | { |
652 | { |
689 | stack->next = stack_first; |
653 | ++cctx_idle; |
690 | stack_first = stack; |
654 | cctx->next = cctx_first; |
|
|
655 | cctx_first = cctx; |
691 | } |
656 | } |
692 | |
657 | |
693 | /* never call directly, always through the coro_state_transfer global variable */ |
658 | /* never call directly, always through the coro_state_transfer global variable */ |
694 | static void |
659 | static void NOINLINE |
695 | transfer_impl (struct coro *prev, struct coro *next, int flags) |
660 | transfer (struct coro *prev, struct coro *next, int flags) |
696 | { |
661 | { |
697 | dSTACKLEVEL; |
662 | dSTACKLEVEL; |
698 | |
663 | |
699 | /* sometimes transfer is only called to set idle_sp */ |
664 | /* sometimes transfer is only called to set idle_sp */ |
700 | if (flags == TRANSFER_SET_STACKLEVEL) |
665 | if (flags == TRANSFER_SET_STACKLEVEL) |
701 | ((coro_stack *)prev)->idle_sp = STACKLEVEL; |
666 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
702 | else if (prev != next) |
667 | else if (prev != next) |
703 | { |
668 | { |
704 | coro_stack *prev__stack; |
669 | coro_cctx *prev__cctx; |
705 | |
670 | |
706 | LOCK; |
671 | LOCK; |
707 | |
672 | |
708 | if (next->mainstack) |
673 | if (next->mainstack) |
709 | { |
674 | { |
… | |
… | |
717 | /* first get rid of the old state */ |
682 | /* first get rid of the old state */ |
718 | SAVE (prev, -1); |
683 | SAVE (prev, -1); |
719 | /* setup coroutine call */ |
684 | /* setup coroutine call */ |
720 | setup_coro (next); |
685 | setup_coro (next); |
721 | /* need a stack */ |
686 | /* need a stack */ |
722 | next->stack = 0; |
687 | next->cctx = 0; |
723 | } |
688 | } |
724 | |
689 | |
725 | if (!prev->stack) |
690 | if (!prev->cctx) |
726 | /* create a new empty context */ |
691 | /* create a new empty context */ |
727 | Newz (0, prev->stack, 1, coro_stack); |
692 | Newz (0, prev->cctx, 1, coro_cctx); |
728 | |
693 | |
729 | prev__stack = prev->stack; |
694 | prev__cctx = prev->cctx; |
730 | |
695 | |
731 | /* possibly "free" the stack */ |
696 | /* possibly "free" the cctx */ |
732 | if (prev__stack->idle_sp == STACKLEVEL) |
697 | if (prev__cctx->idle_sp == STACKLEVEL) |
733 | { |
698 | { |
734 | stack_put (prev__stack); |
699 | cctx_put (prev__cctx); |
735 | prev->stack = 0; |
700 | prev->cctx = 0; |
736 | } |
701 | } |
737 | |
702 | |
738 | if (!next->stack) |
703 | if (!next->cctx) |
739 | next->stack = stack_get (); |
704 | next->cctx = cctx_get (); |
740 | |
705 | |
741 | if (prev__stack != next->stack) |
706 | if (prev__cctx != next->cctx) |
742 | { |
707 | { |
743 | prev__stack->top_env = PL_top_env; |
708 | prev__cctx->top_env = PL_top_env; |
744 | PL_top_env = next->stack->top_env; |
709 | PL_top_env = next->cctx->top_env; |
745 | coro_transfer (&prev__stack->cctx, &next->stack->cctx); |
710 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
746 | } |
711 | } |
747 | |
712 | |
748 | free_coro_mortal (); |
713 | free_coro_mortal (); |
749 | |
714 | |
750 | UNLOCK; |
715 | UNLOCK; |
751 | } |
716 | } |
752 | } |
717 | } |
753 | |
|
|
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 | |
718 | |
759 | struct transfer_args |
719 | struct transfer_args |
760 | { |
720 | { |
761 | struct coro *prev, *next; |
721 | struct coro *prev, *next; |
762 | int flags; |
722 | int flags; |
763 | }; |
723 | }; |
764 | |
724 | |
765 | #define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags) |
725 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags) |
766 | |
726 | |
767 | static void |
727 | static void |
768 | coro_state_destroy (struct coro *coro) |
728 | coro_state_destroy (struct coro *coro) |
769 | { |
729 | { |
770 | if (coro->refcnt--) |
730 | if (coro->refcnt--) |
… | |
… | |
775 | struct coro temp; |
735 | struct coro temp; |
776 | |
736 | |
777 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
737 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
778 | LOAD (coro); |
738 | LOAD (coro); |
779 | |
739 | |
780 | destroy_stacks (); |
740 | coro_destroy_stacks (); |
781 | |
741 | |
782 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
742 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
783 | |
743 | |
784 | coro->mainstack = 0; |
744 | coro->mainstack = 0; |
785 | } |
745 | } |
786 | |
746 | |
787 | stack_free (coro->stack); |
747 | cctx_free (coro->cctx); |
788 | SvREFCNT_dec (coro->args); |
748 | SvREFCNT_dec (coro->args); |
789 | Safefree (coro); |
749 | Safefree (coro); |
790 | } |
750 | } |
791 | |
751 | |
792 | static int |
752 | static int |
… | |
… | |
808 | ++coro->refcnt; |
768 | ++coro->refcnt; |
809 | |
769 | |
810 | return 0; |
770 | return 0; |
811 | } |
771 | } |
812 | |
772 | |
813 | static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; |
773 | static MGVTBL coro_state_vtbl = { |
|
|
774 | 0, 0, 0, 0, |
|
|
775 | coro_state_clear, |
|
|
776 | 0, |
|
|
777 | #ifdef MGf_DUP |
|
|
778 | coro_state_dup, |
|
|
779 | #else |
|
|
780 | # define MGf_DUP 0 |
|
|
781 | #endif |
|
|
782 | }; |
814 | |
783 | |
815 | static struct coro * |
784 | static struct coro * |
816 | SvSTATE (SV *coro) |
785 | SvSTATE (SV *coro) |
817 | { |
786 | { |
818 | HV *stash; |
787 | HV *stash; |
… | |
… | |
1027 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
996 | 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)); |
997 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1029 | |
998 | |
1030 | for (i = 1; i < items; i++) |
999 | for (i = 1; i < items; i++) |
1031 | av_push (coro->args, newSVsv (ST (i))); |
1000 | av_push (coro->args, newSVsv (ST (i))); |
1032 | |
|
|
1033 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
|
|
1034 | /*coro->stack = 0;*/ |
|
|
1035 | } |
1001 | } |
1036 | OUTPUT: |
1002 | OUTPUT: |
1037 | RETVAL |
1003 | RETVAL |
1038 | |
1004 | |
1039 | void |
1005 | void |
… | |
… | |
1048 | struct transfer_args ta; |
1014 | struct transfer_args ta; |
1049 | |
1015 | |
1050 | switch (ix) |
1016 | switch (ix) |
1051 | { |
1017 | { |
1052 | case 0: |
1018 | case 0: |
1053 | ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); |
1019 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1054 | ta.next = 0; |
1020 | ta.next = 0; |
1055 | ta.flags = TRANSFER_SET_STACKLEVEL; |
1021 | ta.flags = TRANSFER_SET_STACKLEVEL; |
1056 | break; |
1022 | break; |
1057 | |
1023 | |
1058 | case 1: |
1024 | case 1: |
… | |
… | |
1112 | ++coro_src->refcnt; |
1078 | ++coro_src->refcnt; |
1113 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
1079 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
1114 | } |
1080 | } |
1115 | |
1081 | |
1116 | void |
1082 | 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 | |
|
|
1122 | void |
|
|
1123 | _exit (code) |
1083 | _exit (code) |
1124 | int code |
1084 | int code |
1125 | PROTOTYPE: $ |
1085 | PROTOTYPE: $ |
1126 | CODE: |
1086 | CODE: |
1127 | _exit (code); |
1087 | _exit (code); |
|
|
1088 | |
|
|
1089 | int |
|
|
1090 | cctx_count () |
|
|
1091 | CODE: |
|
|
1092 | RETVAL = cctx_count; |
|
|
1093 | OUTPUT: |
|
|
1094 | RETVAL |
|
|
1095 | |
|
|
1096 | int |
|
|
1097 | cctx_idle () |
|
|
1098 | CODE: |
|
|
1099 | RETVAL = cctx_idle; |
|
|
1100 | OUTPUT: |
|
|
1101 | RETVAL |
1128 | |
1102 | |
1129 | MODULE = Coro::State PACKAGE = Coro |
1103 | MODULE = Coro::State PACKAGE = Coro |
1130 | |
1104 | |
1131 | BOOT: |
1105 | BOOT: |
1132 | { |
1106 | { |