… | |
… | |
3 | #include "EXTERN.h" |
3 | #include "EXTERN.h" |
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 | |
|
|
9 | #if USE_VALGRIND |
|
|
10 | # include <valgrind/valgrind.h> |
|
|
11 | #endif |
8 | |
12 | |
9 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
13 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
10 | (PERL_REVISION > (a) \ |
14 | (PERL_REVISION > (a) \ |
11 | || (PERL_REVISION == (a) \ |
15 | || (PERL_REVISION == (a) \ |
12 | && (PERL_VERSION > (b) \ |
16 | && (PERL_VERSION > (b) \ |
… | |
… | |
58 | # ifndef PAGESIZE |
62 | # ifndef PAGESIZE |
59 | # define PAGESIZE pagesize |
63 | # define PAGESIZE pagesize |
60 | # define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) |
64 | # define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) |
61 | static long pagesize; |
65 | static long pagesize; |
62 | # else |
66 | # else |
63 | # define BOOT_PAGESIZE |
67 | # define BOOT_PAGESIZE (void)0 |
64 | # endif |
68 | # endif |
|
|
69 | #else |
|
|
70 | # define PAGESIZE 0 |
|
|
71 | # define BOOT_PAGESIZE (void)0 |
65 | #endif |
72 | #endif |
66 | |
73 | |
67 | /* 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 |
68 | * 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 |
69 | * and should be unique. */ |
76 | * and should be unique. */ |
… | |
… | |
96 | static struct CoroAPI coroapi; |
103 | static struct CoroAPI coroapi; |
97 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
104 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
98 | static HV *coro_state_stash, *coro_stash; |
105 | static HV *coro_state_stash, *coro_stash; |
99 | static SV *coro_mortal; /* will be freed after next transfer */ |
106 | static SV *coro_mortal; /* will be freed after next transfer */ |
100 | |
107 | |
|
|
108 | static struct coro_cctx *cctx_first; |
|
|
109 | static int cctx_count, cctx_idle; |
|
|
110 | |
101 | /* this is a structure representing a c-level coroutine */ |
111 | /* this is a structure representing a c-level coroutine */ |
102 | typedef struct coro_stack { |
112 | typedef struct coro_cctx { |
103 | struct coro_stack *next; |
113 | struct coro_cctx *next; |
104 | |
114 | |
105 | /* the stack */ |
115 | /* the stack */ |
106 | void *sptr; |
116 | void *sptr; |
107 | long ssize; /* positive == mmap, otherwise malloc */ |
117 | long ssize; /* positive == mmap, otherwise malloc */ |
108 | |
118 | |
109 | /* cpu state */ |
119 | /* cpu state */ |
110 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
120 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
111 | JMPENV *top_env; |
121 | JMPENV *top_env; |
112 | coro_context cctx; |
122 | coro_context cctx; |
113 | } coro_stack; |
123 | |
|
|
124 | int inuse; |
|
|
125 | |
|
|
126 | #if USE_VALGRIND |
|
|
127 | int valgrind_id; |
|
|
128 | #endif |
|
|
129 | } coro_cctx; |
|
|
130 | |
|
|
131 | enum { |
|
|
132 | CF_RUNNING, /* coroutine is running */ |
|
|
133 | CF_READY, /* coroutine is ready */ |
|
|
134 | }; |
114 | |
135 | |
115 | /* this is a structure representing a perl-level coroutine */ |
136 | /* this is a structure representing a perl-level coroutine */ |
116 | struct coro { |
137 | struct coro { |
117 | /* the c coroutine allocated to this perl coroutine, if any */ |
138 | /* the c coroutine allocated to this perl coroutine, if any */ |
118 | coro_stack *stack; |
139 | coro_cctx *cctx; |
119 | |
140 | |
120 | /* data associated with this coroutine (initial args) */ |
141 | /* data associated with this coroutine (initial args) */ |
121 | AV *args; |
142 | AV *args; |
122 | int refcnt; |
143 | int refcnt; |
|
|
144 | int flags; |
123 | |
145 | |
124 | /* optionally saved, might be zero */ |
146 | /* optionally saved, might be zero */ |
125 | AV *defav; |
147 | AV *defav; |
126 | SV *defsv; |
148 | SV *defsv; |
127 | SV *errsv; |
149 | SV *errsv; |
128 | |
150 | |
129 | /* saved global state not related to stacks */ |
151 | #define VAR(name,type) type name; |
130 | U8 dowarn; |
152 | # include "state.h" |
131 | I32 in_eval; |
153 | #undef VAR |
132 | |
|
|
133 | /* the stacks and related info (callchain etc..) */ |
|
|
134 | PERL_SI *curstackinfo; |
|
|
135 | AV *curstack; |
|
|
136 | AV *mainstack; |
|
|
137 | SV **stack_sp; |
|
|
138 | OP *op; |
|
|
139 | SV **curpad; |
|
|
140 | AV *comppad; |
|
|
141 | CV *compcv; |
|
|
142 | SV **stack_base; |
|
|
143 | SV **stack_max; |
|
|
144 | SV **tmps_stack; |
|
|
145 | I32 tmps_floor; |
|
|
146 | I32 tmps_ix; |
|
|
147 | I32 tmps_max; |
|
|
148 | I32 *markstack; |
|
|
149 | I32 *markstack_ptr; |
|
|
150 | I32 *markstack_max; |
|
|
151 | I32 *scopestack; |
|
|
152 | I32 scopestack_ix; |
|
|
153 | I32 scopestack_max; |
|
|
154 | ANY *savestack; |
|
|
155 | I32 savestack_ix; |
|
|
156 | I32 savestack_max; |
|
|
157 | OP **retstack; |
|
|
158 | I32 retstack_ix; |
|
|
159 | I32 retstack_max; |
|
|
160 | PMOP *curpm; |
|
|
161 | COP *curcop; |
|
|
162 | |
154 | |
163 | /* coro process data */ |
155 | /* coro process data */ |
164 | int prio; |
156 | int prio; |
165 | }; |
157 | }; |
166 | |
158 | |
… | |
… | |
230 | |
222 | |
231 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
223 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
232 | |
224 | |
233 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
225 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
234 | |
226 | |
|
|
227 | #define CORO_MAGIC(cv) \ |
|
|
228 | SvMAGIC (cv) \ |
|
|
229 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
|
|
230 | ? SvMAGIC (cv) \ |
|
|
231 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
|
|
232 | : 0 |
|
|
233 | |
235 | /* the next two functions merely cache the padlists */ |
234 | /* the next two functions merely cache the padlists */ |
236 | static void |
235 | static void |
237 | get_padlist (CV *cv) |
236 | get_padlist (CV *cv) |
238 | { |
237 | { |
239 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
238 | MAGIC *mg = CORO_MAGIC (cv); |
|
|
239 | AV *av; |
240 | |
240 | |
241 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
241 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
242 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
242 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
243 | else |
243 | else |
244 | { |
244 | { |
245 | #if 0 |
245 | #if 0 |
246 | /* this is probably cleaner, but also slower? */ |
246 | /* this is probably cleaner, but also slower? */ |
247 | CV *cp = Perl_cv_clone (cv); |
247 | CV *cp = Perl_cv_clone (cv); |
… | |
… | |
255 | } |
255 | } |
256 | |
256 | |
257 | static void |
257 | static void |
258 | put_padlist (CV *cv) |
258 | put_padlist (CV *cv) |
259 | { |
259 | { |
260 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
260 | MAGIC *mg = CORO_MAGIC (cv); |
|
|
261 | AV *av; |
261 | |
262 | |
262 | if (!mg) |
263 | if (!mg) |
263 | { |
264 | { |
264 | sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); |
265 | sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); |
265 | mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
266 | mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
266 | mg->mg_virtual = &vtbl_coro; |
267 | mg->mg_virtual = &vtbl_coro; |
267 | mg->mg_obj = (SV *)newAV (); |
268 | mg->mg_obj = (SV *)newAV (); |
268 | } |
269 | } |
269 | |
270 | |
270 | av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv)); |
271 | av = (AV *)mg->mg_obj; |
|
|
272 | |
|
|
273 | if (AvFILLp (av) >= AvMAX (av)) |
|
|
274 | av_extend (av, AvMAX (av) + 1); |
|
|
275 | |
|
|
276 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
271 | } |
277 | } |
272 | |
278 | |
273 | #define SB do { |
279 | #define SB do { |
274 | #define SE } while (0) |
280 | #define SE } while (0) |
275 | |
281 | |
… | |
… | |
279 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
285 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
280 | |
286 | |
281 | static void |
287 | static void |
282 | load_state(Coro__State c) |
288 | load_state(Coro__State c) |
283 | { |
289 | { |
284 | PL_dowarn = c->dowarn; |
290 | #define VAR(name,type) PL_ ## name = c->name; |
285 | PL_in_eval = c->in_eval; |
291 | # include "state.h" |
286 | |
292 | #undef VAR |
287 | PL_curstackinfo = c->curstackinfo; |
|
|
288 | PL_curstack = c->curstack; |
|
|
289 | PL_mainstack = c->mainstack; |
|
|
290 | PL_stack_sp = c->stack_sp; |
|
|
291 | PL_op = c->op; |
|
|
292 | PL_curpad = c->curpad; |
|
|
293 | PL_comppad = c->comppad; |
|
|
294 | PL_compcv = c->compcv; |
|
|
295 | PL_stack_base = c->stack_base; |
|
|
296 | PL_stack_max = c->stack_max; |
|
|
297 | PL_tmps_stack = c->tmps_stack; |
|
|
298 | PL_tmps_floor = c->tmps_floor; |
|
|
299 | PL_tmps_ix = c->tmps_ix; |
|
|
300 | PL_tmps_max = c->tmps_max; |
|
|
301 | PL_markstack = c->markstack; |
|
|
302 | PL_markstack_ptr = c->markstack_ptr; |
|
|
303 | PL_markstack_max = c->markstack_max; |
|
|
304 | PL_scopestack = c->scopestack; |
|
|
305 | PL_scopestack_ix = c->scopestack_ix; |
|
|
306 | PL_scopestack_max = c->scopestack_max; |
|
|
307 | PL_savestack = c->savestack; |
|
|
308 | PL_savestack_ix = c->savestack_ix; |
|
|
309 | PL_savestack_max = c->savestack_max; |
|
|
310 | #if !PERL_VERSION_ATLEAST (5,9,0) |
|
|
311 | PL_retstack = c->retstack; |
|
|
312 | PL_retstack_ix = c->retstack_ix; |
|
|
313 | PL_retstack_max = c->retstack_max; |
|
|
314 | #endif |
|
|
315 | PL_curpm = c->curpm; |
|
|
316 | PL_curcop = c->curcop; |
|
|
317 | |
293 | |
318 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
294 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
319 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
295 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
320 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
296 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
321 | |
297 | |
… | |
… | |
324 | CV *cv; |
300 | CV *cv; |
325 | |
301 | |
326 | /* now do the ugly restore mess */ |
302 | /* now do the ugly restore mess */ |
327 | while ((cv = (CV *)POPs)) |
303 | while ((cv = (CV *)POPs)) |
328 | { |
304 | { |
329 | AV *padlist = (AV *)POPs; |
|
|
330 | |
|
|
331 | if (padlist) |
|
|
332 | { |
|
|
333 | put_padlist (cv); /* mark this padlist as available */ |
305 | put_padlist (cv); /* mark this padlist as available */ |
334 | CvPADLIST(cv) = padlist; |
306 | CvDEPTH (cv) = PTR2IV (POPs); |
335 | } |
307 | CvPADLIST (cv) = (AV *)POPs; |
336 | |
|
|
337 | ++CvDEPTH(cv); |
|
|
338 | } |
308 | } |
339 | |
309 | |
340 | PUTBACK; |
310 | PUTBACK; |
341 | } |
311 | } |
342 | } |
312 | } |
… | |
… | |
364 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
334 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
365 | |
335 | |
366 | if (CxTYPE(cx) == CXt_SUB) |
336 | if (CxTYPE(cx) == CXt_SUB) |
367 | { |
337 | { |
368 | CV *cv = cx->blk_sub.cv; |
338 | CV *cv = cx->blk_sub.cv; |
|
|
339 | |
369 | if (CvDEPTH(cv)) |
340 | if (CvDEPTH (cv)) |
370 | { |
341 | { |
371 | EXTEND (SP, CvDEPTH(cv)*2); |
342 | EXTEND (SP, 3); |
372 | |
|
|
373 | while (--CvDEPTH(cv)) |
|
|
374 | { |
|
|
375 | /* this tells the restore code to increment CvDEPTH */ |
|
|
376 | PUSHs (Nullsv); |
|
|
377 | PUSHs ((SV *)cv); |
|
|
378 | } |
|
|
379 | |
343 | |
380 | PUSHs ((SV *)CvPADLIST(cv)); |
344 | PUSHs ((SV *)CvPADLIST(cv)); |
|
|
345 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
381 | PUSHs ((SV *)cv); |
346 | PUSHs ((SV *)cv); |
382 | |
347 | |
|
|
348 | CvDEPTH (cv) = 0; |
383 | get_padlist (cv); |
349 | get_padlist (cv); |
384 | } |
350 | } |
385 | } |
351 | } |
386 | #ifdef CXt_FORMAT |
352 | #ifdef CXt_FORMAT |
387 | else if (CxTYPE(cx) == CXt_FORMAT) |
353 | else if (CxTYPE(cx) == CXt_FORMAT) |
… | |
… | |
406 | |
372 | |
407 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
373 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
408 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
374 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
409 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
375 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
410 | |
376 | |
411 | c->dowarn = PL_dowarn; |
377 | #define VAR(name,type)c->name = PL_ ## name; |
412 | c->in_eval = PL_in_eval; |
378 | # include "state.h" |
413 | |
379 | #undef VAR |
414 | c->curstackinfo = PL_curstackinfo; |
|
|
415 | c->curstack = PL_curstack; |
|
|
416 | c->mainstack = PL_mainstack; |
|
|
417 | c->stack_sp = PL_stack_sp; |
|
|
418 | c->op = PL_op; |
|
|
419 | c->curpad = PL_curpad; |
|
|
420 | c->comppad = PL_comppad; |
|
|
421 | c->compcv = PL_compcv; |
|
|
422 | c->stack_base = PL_stack_base; |
|
|
423 | c->stack_max = PL_stack_max; |
|
|
424 | c->tmps_stack = PL_tmps_stack; |
|
|
425 | c->tmps_floor = PL_tmps_floor; |
|
|
426 | c->tmps_ix = PL_tmps_ix; |
|
|
427 | c->tmps_max = PL_tmps_max; |
|
|
428 | c->markstack = PL_markstack; |
|
|
429 | c->markstack_ptr = PL_markstack_ptr; |
|
|
430 | c->markstack_max = PL_markstack_max; |
|
|
431 | c->scopestack = PL_scopestack; |
|
|
432 | c->scopestack_ix = PL_scopestack_ix; |
|
|
433 | c->scopestack_max = PL_scopestack_max; |
|
|
434 | c->savestack = PL_savestack; |
|
|
435 | c->savestack_ix = PL_savestack_ix; |
|
|
436 | c->savestack_max = PL_savestack_max; |
|
|
437 | #if !PERL_VERSION_ATLEAST (5,9,0) |
|
|
438 | c->retstack = PL_retstack; |
|
|
439 | c->retstack_ix = PL_retstack_ix; |
|
|
440 | c->retstack_max = PL_retstack_max; |
|
|
441 | #endif |
|
|
442 | c->curpm = PL_curpm; |
|
|
443 | c->curcop = PL_curcop; |
|
|
444 | } |
380 | } |
445 | |
381 | |
446 | /* |
382 | /* |
447 | * allocate various perl stacks. This is an exact copy |
383 | * allocate various perl stacks. This is an exact copy |
448 | * of perl.c:init_stacks, except that it uses less memory |
384 | * of perl.c:init_stacks, except that it uses less memory |
… | |
… | |
491 | |
427 | |
492 | /* |
428 | /* |
493 | * destroy the stacks, the callchain etc... |
429 | * destroy the stacks, the callchain etc... |
494 | */ |
430 | */ |
495 | static void |
431 | static void |
496 | coro_destroy_stacks() |
432 | coro_destroy_stacks () |
497 | { |
433 | { |
498 | if (!IN_DESTRUCT) |
434 | if (!IN_DESTRUCT) |
499 | { |
435 | { |
500 | /* is this ugly, I ask? */ |
436 | /* is this ugly, I ask? */ |
501 | LEAVE_SCOPE (0); |
437 | LEAVE_SCOPE (0); |
… | |
… | |
546 | * emulate part of the perl startup here. |
482 | * emulate part of the perl startup here. |
547 | */ |
483 | */ |
548 | |
484 | |
549 | coro_init_stacks (); |
485 | coro_init_stacks (); |
550 | |
486 | |
|
|
487 | PL_curcop = 0; |
|
|
488 | PL_in_eval = 0; |
|
|
489 | PL_curpm = 0; |
|
|
490 | |
551 | { |
491 | { |
552 | dSP; |
492 | dSP; |
553 | LOGOP myop; |
493 | LOGOP myop; |
554 | |
494 | |
555 | /*PL_curcop = 0;*/ |
495 | /* I have no idea why this is needed, but it is */ |
556 | PL_in_eval = 0; |
496 | PUSHMARK (SP); |
|
|
497 | |
557 | SvREFCNT_dec (GvAV (PL_defgv)); |
498 | SvREFCNT_dec (GvAV (PL_defgv)); |
558 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
499 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
559 | |
|
|
560 | SPAGAIN; |
|
|
561 | |
500 | |
562 | Zero (&myop, 1, LOGOP); |
501 | Zero (&myop, 1, LOGOP); |
563 | myop.op_next = Nullop; |
502 | myop.op_next = Nullop; |
564 | myop.op_flags = OPf_WANT_VOID; |
503 | myop.op_flags = OPf_WANT_VOID; |
565 | |
504 | |
566 | PL_op = (OP *)&myop; |
505 | PL_op = (OP *)&myop; |
567 | |
506 | |
568 | PUSHMARK (SP); |
507 | PUSHMARK (SP); |
569 | PUSHMARK (SP); |
|
|
570 | XPUSHs ((SV *)get_cv ("Coro::State::coro_init", FALSE)); |
508 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
571 | PUTBACK; |
509 | PUTBACK; |
572 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
510 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
573 | SPAGAIN; |
511 | SPAGAIN; |
574 | |
512 | |
575 | ENTER; /* necessary e.g. for dounwind */ |
513 | ENTER; /* necessary e.g. for dounwind */ |
… | |
… | |
585 | coro_mortal = 0; |
523 | coro_mortal = 0; |
586 | } |
524 | } |
587 | } |
525 | } |
588 | |
526 | |
589 | static void NOINLINE |
527 | static void NOINLINE |
590 | prepare_cctx (coro_stack *cctx) |
528 | prepare_cctx (coro_cctx *cctx) |
591 | { |
529 | { |
592 | dSP; |
530 | dSP; |
593 | LOGOP myop; |
531 | LOGOP myop; |
594 | |
532 | |
595 | Zero (&myop, 1, LOGOP); |
533 | Zero (&myop, 1, LOGOP); |
596 | myop.op_next = PL_op; |
534 | myop.op_next = PL_op; |
597 | myop.op_flags = OPf_WANT_VOID; |
535 | myop.op_flags = OPf_WANT_VOID; |
598 | |
536 | |
599 | sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV (cctx)); |
537 | sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx)); |
600 | |
538 | |
601 | PUSHMARK (SP); |
539 | PUSHMARK (SP); |
602 | XPUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE)); |
540 | XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
603 | PUTBACK; |
541 | PUTBACK; |
604 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
542 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
605 | SPAGAIN; |
543 | SPAGAIN; |
606 | } |
544 | } |
607 | |
545 | |
… | |
… | |
613 | |
551 | |
614 | /* |
552 | /* |
615 | * this is a _very_ stripped down perl interpreter ;) |
553 | * this is a _very_ stripped down perl interpreter ;) |
616 | */ |
554 | */ |
617 | PL_top_env = &PL_start_env; |
555 | PL_top_env = &PL_start_env; |
|
|
556 | |
618 | /* inject call to cctx_init */ |
557 | /* inject call to cctx_init */ |
619 | prepare_cctx ((coro_stack *)arg); |
558 | prepare_cctx ((coro_cctx *)arg); |
620 | |
559 | |
621 | /* somebody will hit me for both perl_run and PL_restartop */ |
560 | /* somebody will hit me for both perl_run and PL_restartop */ |
622 | perl_run (PL_curinterp); |
561 | perl_run (PL_curinterp); |
623 | |
562 | |
624 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr); |
563 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
625 | abort (); |
564 | abort (); |
626 | } |
565 | } |
627 | |
566 | |
628 | static coro_stack * |
567 | static coro_cctx * |
629 | stack_new () |
568 | cctx_new () |
630 | { |
569 | { |
631 | coro_stack *stack; |
570 | coro_cctx *cctx; |
632 | |
571 | |
|
|
572 | ++cctx_count; |
|
|
573 | |
633 | New (0, stack, 1, coro_stack); |
574 | New (0, cctx, 1, coro_cctx); |
634 | |
575 | |
635 | #if HAVE_MMAP |
576 | #if HAVE_MMAP |
636 | |
577 | |
637 | stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
578 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
638 | /* mmap suppsedly does allocate-on-write for us */ |
579 | /* mmap suppsedly does allocate-on-write for us */ |
639 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
580 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
640 | |
581 | |
641 | if (stack->sptr == (void *)-1) |
582 | if (cctx->sptr == (void *)-1) |
642 | { |
583 | { |
643 | perror ("FATAL: unable to mmap stack for coroutine"); |
584 | perror ("FATAL: unable to mmap stack for coroutine"); |
644 | _exit (EXIT_FAILURE); |
585 | _exit (EXIT_FAILURE); |
645 | } |
586 | } |
646 | |
587 | |
647 | # if STACKGUARD |
588 | # if STACKGUARD |
648 | mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
589 | mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
649 | # endif |
590 | # endif |
650 | |
591 | |
651 | #else |
592 | #else |
652 | |
593 | |
653 | stack->ssize = STACKSIZE * (long)sizeof (long); |
594 | cctx->ssize = STACKSIZE * (long)sizeof (long); |
654 | New (0, stack->sptr, STACKSIZE, long); |
595 | New (0, cctx->sptr, STACKSIZE, long); |
655 | |
596 | |
656 | if (!stack->sptr) |
597 | if (!cctx->sptr) |
657 | { |
598 | { |
658 | perror (stderr, "FATAL: unable to malloc stack for coroutine"); |
599 | perror ("FATAL: unable to malloc stack for coroutine"); |
659 | _exit (EXIT_FAILURE); |
600 | _exit (EXIT_FAILURE); |
660 | } |
601 | } |
661 | |
602 | |
662 | #endif |
603 | #endif |
663 | |
604 | |
|
|
605 | #if USE_VALGRIND |
|
|
606 | cctx->valgrind_id = VALGRIND_STACK_REGISTER ( |
|
|
607 | STACKGUARD * PAGESIZE + (char *)cctx->sptr, |
|
|
608 | cctx->ssize + (char *)cctx->sptr |
|
|
609 | ); |
|
|
610 | #endif |
|
|
611 | |
664 | coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); |
612 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); |
665 | |
613 | |
666 | return stack; |
614 | return cctx; |
667 | } |
615 | } |
668 | |
616 | |
669 | static void |
617 | static void |
670 | stack_free (coro_stack *stack) |
618 | cctx_free (coro_cctx *cctx) |
671 | { |
619 | { |
672 | if (!stack) |
620 | if (!cctx) |
673 | return; |
621 | return; |
674 | |
622 | |
|
|
623 | --cctx_count; |
|
|
624 | |
|
|
625 | #if USE_VALGRIND |
|
|
626 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
|
|
627 | #endif |
|
|
628 | |
675 | #if HAVE_MMAP |
629 | #if HAVE_MMAP |
676 | munmap (stack->sptr, stack->ssize); |
630 | munmap (cctx->sptr, cctx->ssize); |
677 | #else |
631 | #else |
678 | Safefree (stack->sptr); |
632 | Safefree (cctx->sptr); |
679 | #endif |
633 | #endif |
680 | |
634 | |
681 | Safefree (stack); |
635 | Safefree (cctx); |
682 | } |
636 | } |
683 | |
637 | |
684 | static coro_stack *stack_first; |
|
|
685 | static int cctx_count, cctx_idle; |
|
|
686 | |
|
|
687 | static coro_stack * |
638 | static coro_cctx * |
688 | stack_get () |
639 | cctx_get () |
689 | { |
640 | { |
690 | coro_stack *stack; |
641 | coro_cctx *cctx; |
691 | |
642 | |
692 | if (stack_first) |
643 | if (cctx_first) |
693 | { |
644 | { |
694 | --cctx_idle; |
645 | --cctx_idle; |
695 | stack = stack_first; |
646 | cctx = cctx_first; |
696 | stack_first = stack->next; |
647 | cctx_first = cctx->next; |
697 | } |
648 | } |
698 | else |
649 | else |
699 | { |
650 | { |
700 | ++cctx_count; |
651 | cctx = cctx_new (); |
701 | stack = stack_new (); |
|
|
702 | PL_op = PL_op->op_next; |
652 | PL_op = PL_op->op_next; |
703 | } |
653 | } |
704 | |
654 | |
705 | return stack; |
655 | return cctx; |
706 | } |
656 | } |
707 | |
657 | |
708 | static void |
658 | static void |
709 | stack_put (coro_stack *stack) |
659 | cctx_put (coro_cctx *cctx) |
710 | { |
660 | { |
711 | ++cctx_idle; |
661 | ++cctx_idle; |
712 | stack->next = stack_first; |
662 | cctx->next = cctx_first; |
713 | stack_first = stack; |
663 | cctx_first = cctx; |
714 | } |
664 | } |
715 | |
665 | |
716 | /* never call directly, always through the coro_state_transfer global variable */ |
666 | /* never call directly, always through the coro_state_transfer global variable */ |
717 | static void NOINLINE |
667 | static void NOINLINE |
718 | transfer (struct coro *prev, struct coro *next, int flags) |
668 | transfer (struct coro *prev, struct coro *next, int flags) |
719 | { |
669 | { |
720 | dSTACKLEVEL; |
670 | dSTACKLEVEL; |
721 | |
671 | |
722 | /* sometimes transfer is only called to set idle_sp */ |
672 | /* sometimes transfer is only called to set idle_sp */ |
723 | if (flags == TRANSFER_SET_STACKLEVEL) |
673 | if (flags == TRANSFER_SET_STACKLEVEL) |
724 | ((coro_stack *)prev)->idle_sp = STACKLEVEL; |
674 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
725 | else if (prev != next) |
675 | else if (prev != next) |
726 | { |
676 | { |
727 | coro_stack *prev__stack; |
677 | coro_cctx *prev__cctx; |
|
|
678 | |
|
|
679 | if (!prev->cctx) |
|
|
680 | { |
|
|
681 | /* create a new empty context */ |
|
|
682 | Newz (0, prev->cctx, 1, coro_cctx); |
|
|
683 | prev->cctx->inuse = 1; |
|
|
684 | prev->flags |= CF_RUNNING; |
|
|
685 | } |
|
|
686 | |
|
|
687 | assert ( prev->flags & CF_RUNNING); |
|
|
688 | assert (!next->flags & CF_RUNNING); |
|
|
689 | |
|
|
690 | prev->flags &= ~CF_RUNNING; |
|
|
691 | next->flags |= CF_RUNNING; |
728 | |
692 | |
729 | LOCK; |
693 | LOCK; |
730 | |
694 | |
731 | if (next->mainstack) |
695 | if (next->mainstack) |
732 | { |
696 | { |
… | |
… | |
740 | /* first get rid of the old state */ |
704 | /* first get rid of the old state */ |
741 | SAVE (prev, -1); |
705 | SAVE (prev, -1); |
742 | /* setup coroutine call */ |
706 | /* setup coroutine call */ |
743 | setup_coro (next); |
707 | setup_coro (next); |
744 | /* need a stack */ |
708 | /* need a stack */ |
|
|
709 | assert (!next->stack); |
745 | next->stack = 0; |
710 | next->cctx = 0; |
746 | } |
711 | } |
747 | |
712 | |
748 | if (!prev->stack) |
|
|
749 | /* create a new empty context */ |
|
|
750 | Newz (0, prev->stack, 1, coro_stack); |
|
|
751 | |
|
|
752 | prev__stack = prev->stack; |
713 | prev__cctx = prev->cctx; |
753 | |
714 | |
754 | /* possibly "free" the stack */ |
715 | /* possibly "free" the cctx */ |
755 | if (prev__stack->idle_sp == STACKLEVEL) |
716 | if (prev__cctx->idle_sp == STACKLEVEL) |
756 | { |
717 | { |
|
|
718 | assert (PL_top_env == prev__cctx->top_env);//D |
757 | stack_put (prev__stack); |
719 | cctx_put (prev__cctx); |
758 | prev->stack = 0; |
720 | prev->cctx = 0; |
759 | } |
721 | } |
760 | |
722 | |
761 | if (!next->stack) |
723 | if (!next->cctx) |
762 | next->stack = stack_get (); |
724 | next->cctx = cctx_get (); |
763 | |
725 | |
764 | if (prev__stack != next->stack) |
726 | if (prev__cctx != next->cctx) |
765 | { |
727 | { |
|
|
728 | assert ( prev__cctx->inuse); |
|
|
729 | assert (!next->cctx->inuse); |
|
|
730 | |
|
|
731 | prev__cctx->inuse = 0; |
|
|
732 | next->cctx->inuse = 1; |
|
|
733 | |
766 | prev__stack->top_env = PL_top_env; |
734 | prev__cctx->top_env = PL_top_env; |
767 | PL_top_env = next->stack->top_env; |
735 | PL_top_env = next->cctx->top_env; |
768 | coro_transfer (&prev__stack->cctx, &next->stack->cctx); |
736 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
769 | } |
737 | } |
770 | |
738 | |
771 | free_coro_mortal (); |
739 | free_coro_mortal (); |
772 | |
740 | |
773 | UNLOCK; |
741 | UNLOCK; |
… | |
… | |
800 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
768 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
801 | |
769 | |
802 | coro->mainstack = 0; |
770 | coro->mainstack = 0; |
803 | } |
771 | } |
804 | |
772 | |
805 | stack_free (coro->stack); |
773 | cctx_free (coro->cctx); |
806 | SvREFCNT_dec (coro->args); |
774 | SvREFCNT_dec (coro->args); |
807 | Safefree (coro); |
775 | Safefree (coro); |
808 | } |
776 | } |
809 | |
777 | |
810 | static int |
778 | static int |
… | |
… | |
924 | } |
892 | } |
925 | |
893 | |
926 | return 0; |
894 | return 0; |
927 | } |
895 | } |
928 | |
896 | |
929 | static void |
897 | static int |
930 | api_ready (SV *coro) |
898 | api_ready (SV *coro_sv) |
931 | { |
899 | { |
932 | dTHX; |
900 | struct coro *coro; |
933 | |
901 | |
934 | if (SvROK (coro)) |
902 | if (SvROK (coro_sv)) |
935 | coro = SvRV (coro); |
903 | coro_sv = SvRV (coro_sv); |
|
|
904 | |
|
|
905 | coro = SvSTATE (coro_sv); |
|
|
906 | if (coro->flags & CF_READY) |
|
|
907 | return 0; |
|
|
908 | |
|
|
909 | coro->flags |= CF_READY; |
936 | |
910 | |
937 | LOCK; |
911 | LOCK; |
938 | coro_enq (SvREFCNT_inc (coro)); |
912 | coro_enq (SvREFCNT_inc (coro_sv)); |
939 | UNLOCK; |
913 | UNLOCK; |
|
|
914 | |
|
|
915 | return 1; |
|
|
916 | } |
|
|
917 | |
|
|
918 | static int |
|
|
919 | api_is_ready (SV *coro_sv) |
|
|
920 | { |
|
|
921 | struct coro *coro; |
|
|
922 | |
|
|
923 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
940 | } |
924 | } |
941 | |
925 | |
942 | static void |
926 | static void |
943 | prepare_schedule (struct transfer_args *ta) |
927 | prepare_schedule (struct transfer_args *ta) |
944 | { |
928 | { |
… | |
… | |
980 | coro_mortal = prev; |
964 | coro_mortal = prev; |
981 | |
965 | |
982 | ta->prev = SvSTATE (prev); |
966 | ta->prev = SvSTATE (prev); |
983 | ta->next = SvSTATE (next); |
967 | ta->next = SvSTATE (next); |
984 | ta->flags = TRANSFER_SAVE_ALL; |
968 | ta->flags = TRANSFER_SAVE_ALL; |
|
|
969 | |
|
|
970 | ta->next->flags &= ~CF_READY; |
985 | } |
971 | } |
986 | |
972 | |
987 | static void |
973 | static void |
988 | prepare_cede (struct transfer_args *ta) |
974 | prepare_cede (struct transfer_args *ta) |
989 | { |
975 | { |
990 | LOCK; |
976 | api_ready (GvSV (coro_current)); |
991 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
|
|
992 | UNLOCK; |
|
|
993 | |
977 | |
994 | prepare_schedule (ta); |
978 | prepare_schedule (ta); |
995 | } |
979 | } |
996 | |
980 | |
997 | static void |
981 | static void |
… | |
… | |
1054 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1038 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1055 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1039 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1056 | |
1040 | |
1057 | for (i = 1; i < items; i++) |
1041 | for (i = 1; i < items; i++) |
1058 | av_push (coro->args, newSVsv (ST (i))); |
1042 | av_push (coro->args, newSVsv (ST (i))); |
1059 | |
|
|
1060 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
|
|
1061 | /*coro->stack = 0;*/ |
|
|
1062 | } |
1043 | } |
1063 | OUTPUT: |
1044 | OUTPUT: |
1064 | RETVAL |
1045 | RETVAL |
1065 | |
1046 | |
1066 | void |
1047 | void |
… | |
… | |
1075 | struct transfer_args ta; |
1056 | struct transfer_args ta; |
1076 | |
1057 | |
1077 | switch (ix) |
1058 | switch (ix) |
1078 | { |
1059 | { |
1079 | case 0: |
1060 | case 0: |
1080 | ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); |
1061 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1081 | ta.next = 0; |
1062 | ta.next = 0; |
1082 | ta.flags = TRANSFER_SET_STACKLEVEL; |
1063 | ta.flags = TRANSFER_SET_STACKLEVEL; |
1083 | break; |
1064 | break; |
1084 | |
1065 | |
1085 | case 1: |
1066 | case 1: |
… | |
… | |
1145 | int code |
1126 | int code |
1146 | PROTOTYPE: $ |
1127 | PROTOTYPE: $ |
1147 | CODE: |
1128 | CODE: |
1148 | _exit (code); |
1129 | _exit (code); |
1149 | |
1130 | |
|
|
1131 | int |
|
|
1132 | cctx_count () |
|
|
1133 | CODE: |
|
|
1134 | RETVAL = cctx_count; |
|
|
1135 | OUTPUT: |
|
|
1136 | RETVAL |
|
|
1137 | |
|
|
1138 | int |
|
|
1139 | cctx_idle () |
|
|
1140 | CODE: |
|
|
1141 | RETVAL = cctx_idle; |
|
|
1142 | OUTPUT: |
|
|
1143 | RETVAL |
|
|
1144 | |
1150 | MODULE = Coro::State PACKAGE = Coro |
1145 | MODULE = Coro::State PACKAGE = Coro |
1151 | |
1146 | |
1152 | BOOT: |
1147 | BOOT: |
1153 | { |
1148 | { |
1154 | int i; |
1149 | int i; |
… | |
… | |
1172 | SV *sv = perl_get_sv("Coro::API", 1); |
1167 | SV *sv = perl_get_sv("Coro::API", 1); |
1173 | |
1168 | |
1174 | coroapi.schedule = api_schedule; |
1169 | coroapi.schedule = api_schedule; |
1175 | coroapi.cede = api_cede; |
1170 | coroapi.cede = api_cede; |
1176 | coroapi.ready = api_ready; |
1171 | coroapi.ready = api_ready; |
|
|
1172 | coroapi.is_ready = api_is_ready; |
1177 | coroapi.nready = &coro_nready; |
1173 | coroapi.nready = &coro_nready; |
1178 | coroapi.current = coro_current; |
1174 | coroapi.current = coro_current; |
1179 | |
1175 | |
1180 | GCoroAPI = &coroapi; |
1176 | GCoroAPI = &coroapi; |
1181 | sv_setiv (sv, (IV)&coroapi); |
1177 | sv_setiv (sv, (IV)&coroapi); |
… | |
… | |
1201 | |
1197 | |
1202 | coro->prio = newprio; |
1198 | coro->prio = newprio; |
1203 | } |
1199 | } |
1204 | } |
1200 | } |
1205 | |
1201 | |
1206 | void |
1202 | SV * |
1207 | ready (SV *self) |
1203 | ready (SV *self) |
1208 | PROTOTYPE: $ |
1204 | PROTOTYPE: $ |
1209 | CODE: |
1205 | CODE: |
1210 | api_ready (self); |
1206 | RETVAL = boolSV (api_ready (self)); |
|
|
1207 | OUTPUT: |
|
|
1208 | RETVAL |
|
|
1209 | |
|
|
1210 | SV * |
|
|
1211 | is_ready (SV *self) |
|
|
1212 | PROTOTYPE: $ |
|
|
1213 | CODE: |
|
|
1214 | RETVAL = boolSV (api_is_ready (self)); |
|
|
1215 | OUTPUT: |
|
|
1216 | RETVAL |
1211 | |
1217 | |
1212 | int |
1218 | int |
1213 | nready (...) |
1219 | nready (...) |
1214 | PROTOTYPE: |
1220 | PROTOTYPE: |
1215 | CODE: |
1221 | CODE: |