1 | #include "libcoro/coro.c" |
1 | #include "libcoro/coro.c" |
|
|
2 | |
|
|
3 | #define PERL_NO_GET_CONTEXT |
2 | |
4 | |
3 | #include "EXTERN.h" |
5 | #include "EXTERN.h" |
4 | #include "perl.h" |
6 | #include "perl.h" |
5 | #include "XSUB.h" |
7 | #include "XSUB.h" |
6 | |
8 | |
… | |
… | |
31 | #else |
33 | #else |
32 | # define PAGESIZE 0 |
34 | # define PAGESIZE 0 |
33 | # define BOOT_PAGESIZE (void)0 |
35 | # define BOOT_PAGESIZE (void)0 |
34 | #endif |
36 | #endif |
35 | |
37 | |
36 | #if USE_VALGRIND |
38 | #if CORO_USE_VALGRIND |
37 | # include <valgrind/valgrind.h> |
39 | # include <valgrind/valgrind.h> |
|
|
40 | # define REGISTER_STACK(cctx,start,end) (cctx)->valgrind_id = VALGRIND_STACK_REGISTER ((start), (end)) |
|
|
41 | #else |
|
|
42 | # define REGISTER_STACK(cctx,start,end) |
38 | #endif |
43 | #endif |
39 | |
44 | |
40 | /* the maximum number of idle cctx that will be pooled */ |
45 | /* the maximum number of idle cctx that will be pooled */ |
41 | #define MAX_IDLE_CCTX 8 |
46 | #define MAX_IDLE_CCTX 8 |
42 | |
47 | |
… | |
… | |
71 | #ifndef SvRV_set |
76 | #ifndef SvRV_set |
72 | # define SvRV_set(s,v) SvRV(s) = (v) |
77 | # define SvRV_set(s,v) SvRV(s) = (v) |
73 | #endif |
78 | #endif |
74 | |
79 | |
75 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
80 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
76 | # undef STACKGUARD |
81 | # undef CORO_STACKGUARD |
77 | #endif |
82 | #endif |
78 | |
83 | |
79 | #ifndef STACKGUARD |
84 | #ifndef CORO_STACKGUARD |
80 | # define STACKGUARD 0 |
85 | # define CORO_STACKGUARD 0 |
81 | #endif |
86 | #endif |
82 | |
87 | |
83 | /* prefer perl internal functions over our own? */ |
88 | /* prefer perl internal functions over our own? */ |
84 | #ifndef PREFER_PERL_FUNCTIONS |
89 | #ifndef CORO_PREFER_PERL_FUNCTIONS |
85 | # define PREFER_PERL_FUNCTIONS 0 |
90 | # define CORO_PREFER_PERL_FUNCTIONS 0 |
86 | #endif |
91 | #endif |
87 | |
92 | |
88 | /* The next macro should declare a variable stacklevel that contains and approximation |
93 | /* The next macro should declare a variable stacklevel that contains and approximation |
89 | * to the current C stack pointer. Its property is that it changes with each call |
94 | * to the current C stack pointer. Its property is that it changes with each call |
90 | * and should be unique. */ |
95 | * and should be unique. */ |
… | |
… | |
121 | I32 laststype; |
126 | I32 laststype; |
122 | int laststatval; |
127 | int laststatval; |
123 | Stat_t statcache; |
128 | Stat_t statcache; |
124 | }; |
129 | }; |
125 | |
130 | |
|
|
131 | static size_t coro_stacksize = CORO_STACKSIZE; |
126 | static struct CoroAPI coroapi; |
132 | static struct CoroAPI coroapi; |
127 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
133 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
|
|
134 | static JMPENV *main_top_env; |
128 | static HV *coro_state_stash, *coro_stash; |
135 | static HV *coro_state_stash, *coro_stash; |
129 | static SV *coro_mortal; /* will be freed after next transfer */ |
136 | static SV *coro_mortal; /* will be freed after next transfer */ |
130 | |
137 | |
131 | static struct coro_cctx *cctx_first; |
138 | static struct coro_cctx *cctx_first; |
132 | static int cctx_count, cctx_idle; |
139 | static int cctx_count, cctx_idle; |
… | |
… | |
135 | typedef struct coro_cctx { |
142 | typedef struct coro_cctx { |
136 | struct coro_cctx *next; |
143 | struct coro_cctx *next; |
137 | |
144 | |
138 | /* the stack */ |
145 | /* the stack */ |
139 | void *sptr; |
146 | void *sptr; |
140 | long ssize; /* positive == mmap, otherwise malloc */ |
147 | size_t ssize; |
141 | |
148 | |
142 | /* cpu state */ |
149 | /* cpu state */ |
143 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
150 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
144 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
151 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
145 | JMPENV *top_env; |
152 | JMPENV *top_env; |
146 | coro_context cctx; |
153 | coro_context cctx; |
147 | |
154 | |
148 | int inuse; |
|
|
149 | |
|
|
150 | #if USE_VALGRIND |
155 | #if CORO_USE_VALGRIND |
151 | int valgrind_id; |
156 | int valgrind_id; |
152 | #endif |
157 | #endif |
|
|
158 | char inuse, mapped; |
153 | } coro_cctx; |
159 | } coro_cctx; |
154 | |
160 | |
155 | enum { |
161 | enum { |
156 | CF_RUNNING = 0x0001, /* coroutine is running */ |
162 | CF_RUNNING = 0x0001, /* coroutine is running */ |
157 | CF_READY = 0x0002, /* coroutine is ready */ |
163 | CF_READY = 0x0002, /* coroutine is ready */ |
… | |
… | |
172 | |
178 | |
173 | /* optionally saved, might be zero */ |
179 | /* optionally saved, might be zero */ |
174 | AV *defav; /* @_ */ |
180 | AV *defav; /* @_ */ |
175 | SV *defsv; /* $_ */ |
181 | SV *defsv; /* $_ */ |
176 | SV *errsv; /* $@ */ |
182 | SV *errsv; /* $@ */ |
|
|
183 | GV *deffh; /* default filehandle */ |
177 | SV *irssv; /* $/ */ |
184 | SV *irssv; /* $/ */ |
178 | SV *irssv_sv; /* real $/ cache */ |
185 | SV *irssv_sv; /* real $/ cache */ |
179 | |
186 | |
180 | #define VAR(name,type) type name; |
187 | #define VAR(name,type) type name; |
181 | # include "state.h" |
188 | # include "state.h" |
… | |
… | |
203 | static int coro_nready; |
210 | static int coro_nready; |
204 | |
211 | |
205 | /** lowlevel stuff **********************************************************/ |
212 | /** lowlevel stuff **********************************************************/ |
206 | |
213 | |
207 | static AV * |
214 | static AV * |
208 | coro_clone_padlist (CV *cv) |
215 | coro_clone_padlist (pTHX_ CV *cv) |
209 | { |
216 | { |
210 | AV *padlist = CvPADLIST (cv); |
217 | AV *padlist = CvPADLIST (cv); |
211 | AV *newpadlist, *newpad; |
218 | AV *newpadlist, *newpad; |
212 | |
219 | |
213 | newpadlist = newAV (); |
220 | newpadlist = newAV (); |
… | |
… | |
225 | |
232 | |
226 | return newpadlist; |
233 | return newpadlist; |
227 | } |
234 | } |
228 | |
235 | |
229 | static void |
236 | static void |
230 | free_padlist (AV *padlist) |
237 | free_padlist (pTHX_ AV *padlist) |
231 | { |
238 | { |
232 | /* may be during global destruction */ |
239 | /* may be during global destruction */ |
233 | if (SvREFCNT (padlist)) |
240 | if (SvREFCNT (padlist)) |
234 | { |
241 | { |
235 | I32 i = AvFILLp (padlist); |
242 | I32 i = AvFILLp (padlist); |
… | |
… | |
256 | AV *padlist; |
263 | AV *padlist; |
257 | AV *av = (AV *)mg->mg_obj; |
264 | AV *av = (AV *)mg->mg_obj; |
258 | |
265 | |
259 | /* casting is fun. */ |
266 | /* casting is fun. */ |
260 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
267 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
261 | free_padlist (padlist); |
268 | free_padlist (aTHX_ padlist); |
262 | |
269 | |
263 | SvREFCNT_dec (av); |
270 | SvREFCNT_dec (av); |
264 | |
271 | |
265 | return 0; |
272 | return 0; |
266 | } |
273 | } |
… | |
… | |
276 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
283 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
277 | : 0 |
284 | : 0 |
278 | |
285 | |
279 | /* the next two functions merely cache the padlists */ |
286 | /* the next two functions merely cache the padlists */ |
280 | static void |
287 | static void |
281 | get_padlist (CV *cv) |
288 | get_padlist (pTHX_ CV *cv) |
282 | { |
289 | { |
283 | MAGIC *mg = CORO_MAGIC (cv); |
290 | MAGIC *mg = CORO_MAGIC (cv); |
284 | AV *av; |
291 | AV *av; |
285 | |
292 | |
286 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
293 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
287 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
294 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
288 | else |
295 | else |
289 | { |
296 | { |
290 | #if PREFER_PERL_FUNCTIONS |
297 | #if CORO_PREFER_PERL_FUNCTIONS |
291 | /* this is probably cleaner, but also slower? */ |
298 | /* this is probably cleaner, but also slower? */ |
292 | CV *cp = Perl_cv_clone (cv); |
299 | CV *cp = Perl_cv_clone (cv); |
293 | CvPADLIST (cv) = CvPADLIST (cp); |
300 | CvPADLIST (cv) = CvPADLIST (cp); |
294 | CvPADLIST (cp) = 0; |
301 | CvPADLIST (cp) = 0; |
295 | SvREFCNT_dec (cp); |
302 | SvREFCNT_dec (cp); |
296 | #else |
303 | #else |
297 | CvPADLIST (cv) = coro_clone_padlist (cv); |
304 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
298 | #endif |
305 | #endif |
299 | } |
306 | } |
300 | } |
307 | } |
301 | |
308 | |
302 | static void |
309 | static void |
303 | put_padlist (CV *cv) |
310 | put_padlist (pTHX_ CV *cv) |
304 | { |
311 | { |
305 | MAGIC *mg = CORO_MAGIC (cv); |
312 | MAGIC *mg = CORO_MAGIC (cv); |
306 | AV *av; |
313 | AV *av; |
307 | |
314 | |
308 | if (!mg) |
315 | if (!mg) |
… | |
… | |
327 | #define SE } while (0) |
334 | #define SE } while (0) |
328 | |
335 | |
329 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
336 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
330 | |
337 | |
331 | static void |
338 | static void |
332 | load_perl (Coro__State c) |
339 | load_perl (pTHX_ Coro__State c) |
333 | { |
340 | { |
334 | #define VAR(name,type) PL_ ## name = c->name; |
341 | #define VAR(name,type) PL_ ## name = c->name; |
335 | # include "state.h" |
342 | # include "state.h" |
336 | #undef VAR |
343 | #undef VAR |
337 | |
344 | |
338 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
345 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
339 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
346 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
340 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
347 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
|
|
348 | if (c->deffh) REPLACE_SV (PL_defoutgv , c->deffh); |
|
|
349 | |
341 | if (c->irssv) |
350 | if (c->irssv) |
342 | { |
351 | { |
343 | if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) |
352 | if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) |
|
|
353 | { |
344 | SvREFCNT_dec (c->irssv); |
354 | SvREFCNT_dec (c->irssv); |
|
|
355 | c->irssv = 0; |
|
|
356 | } |
345 | else |
357 | else |
346 | { |
358 | { |
347 | REPLACE_SV (PL_rs, c->irssv); |
359 | REPLACE_SV (PL_rs, c->irssv); |
348 | if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); |
360 | if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); |
349 | sv_setsv (c->irssv_sv, PL_rs); |
361 | sv_setsv (c->irssv_sv, PL_rs); |
… | |
… | |
355 | CV *cv; |
367 | CV *cv; |
356 | |
368 | |
357 | /* now do the ugly restore mess */ |
369 | /* now do the ugly restore mess */ |
358 | while ((cv = (CV *)POPs)) |
370 | while ((cv = (CV *)POPs)) |
359 | { |
371 | { |
360 | put_padlist (cv); /* mark this padlist as available */ |
372 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
361 | CvDEPTH (cv) = PTR2IV (POPs); |
373 | CvDEPTH (cv) = PTR2IV (POPs); |
362 | CvPADLIST (cv) = (AV *)POPs; |
374 | CvPADLIST (cv) = (AV *)POPs; |
363 | } |
375 | } |
364 | |
376 | |
365 | PUTBACK; |
377 | PUTBACK; |
366 | } |
378 | } |
|
|
379 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
367 | } |
380 | } |
368 | |
381 | |
369 | static void |
382 | static void |
370 | save_perl (Coro__State c) |
383 | save_perl (pTHX_ Coro__State c) |
371 | { |
384 | { |
|
|
385 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
372 | { |
386 | { |
373 | dSP; |
387 | dSP; |
374 | I32 cxix = cxstack_ix; |
388 | I32 cxix = cxstack_ix; |
375 | PERL_CONTEXT *ccstk = cxstack; |
389 | PERL_CONTEXT *ccstk = cxstack; |
376 | PERL_SI *top_si = PL_curstackinfo; |
390 | PERL_SI *top_si = PL_curstackinfo; |
… | |
… | |
399 | PUSHs ((SV *)CvPADLIST (cv)); |
413 | PUSHs ((SV *)CvPADLIST (cv)); |
400 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
414 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
401 | PUSHs ((SV *)cv); |
415 | PUSHs ((SV *)cv); |
402 | |
416 | |
403 | CvDEPTH (cv) = 0; |
417 | CvDEPTH (cv) = 0; |
404 | get_padlist (cv); |
418 | get_padlist (aTHX_ cv); |
405 | } |
419 | } |
406 | } |
420 | } |
407 | } |
421 | } |
408 | |
422 | |
409 | if (top_si->si_type == PERLSI_MAIN) |
423 | if (top_si->si_type == PERLSI_MAIN) |
… | |
… | |
418 | } |
432 | } |
419 | |
433 | |
420 | c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
434 | c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
421 | c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
435 | c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
422 | c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
436 | c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
|
|
437 | c->deffh = c->save & CORO_SAVE_DEFFH ? (GV *)SvREFCNT_inc (PL_defoutgv) : 0; |
423 | c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; |
438 | c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; |
424 | |
439 | |
425 | #define VAR(name,type)c->name = PL_ ## name; |
440 | #define VAR(name,type)c->name = PL_ ## name; |
426 | # include "state.h" |
441 | # include "state.h" |
427 | #undef VAR |
442 | #undef VAR |
… | |
… | |
431 | * allocate various perl stacks. This is an exact copy |
446 | * allocate various perl stacks. This is an exact copy |
432 | * of perl.c:init_stacks, except that it uses less memory |
447 | * of perl.c:init_stacks, except that it uses less memory |
433 | * on the (sometimes correct) assumption that coroutines do |
448 | * on the (sometimes correct) assumption that coroutines do |
434 | * not usually need a lot of stackspace. |
449 | * not usually need a lot of stackspace. |
435 | */ |
450 | */ |
436 | #if PREFER_PERL_FUNCTIONS |
451 | #if CORO_PREFER_PERL_FUNCTIONS |
437 | # define coro_init_stacks init_stacks |
452 | # define coro_init_stacks init_stacks |
438 | #else |
453 | #else |
439 | static void |
454 | static void |
440 | coro_init_stacks () |
455 | coro_init_stacks (pTHX) |
441 | { |
456 | { |
442 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
457 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
443 | PL_curstackinfo->si_type = PERLSI_MAIN; |
458 | PL_curstackinfo->si_type = PERLSI_MAIN; |
444 | PL_curstack = PL_curstackinfo->si_stack; |
459 | PL_curstack = PL_curstackinfo->si_stack; |
445 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
460 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
… | |
… | |
479 | |
494 | |
480 | /* |
495 | /* |
481 | * destroy the stacks, the callchain etc... |
496 | * destroy the stacks, the callchain etc... |
482 | */ |
497 | */ |
483 | static void |
498 | static void |
484 | coro_destroy_stacks () |
499 | coro_destroy_stacks (pTHX) |
485 | { |
500 | { |
486 | if (!IN_DESTRUCT) |
501 | if (!IN_DESTRUCT) |
487 | { |
502 | { |
488 | /* restore all saved variables and stuff */ |
503 | /* restore all saved variables and stuff */ |
489 | LEAVE_SCOPE (0); |
504 | LEAVE_SCOPE (0); |
… | |
… | |
525 | } |
540 | } |
526 | |
541 | |
527 | /** coroutine stack handling ************************************************/ |
542 | /** coroutine stack handling ************************************************/ |
528 | |
543 | |
529 | static void |
544 | static void |
530 | setup_coro (struct coro *coro) |
545 | setup_coro (pTHX_ struct coro *coro) |
531 | { |
546 | { |
532 | /* |
547 | /* |
533 | * emulate part of the perl startup here. |
548 | * emulate part of the perl startup here. |
534 | */ |
549 | */ |
535 | |
|
|
536 | coro_init_stacks (); |
550 | coro_init_stacks (aTHX); |
537 | |
551 | |
538 | PL_curcop = &PL_compiling; |
552 | PL_curcop = &PL_compiling; |
539 | PL_in_eval = EVAL_NULL; |
553 | PL_in_eval = EVAL_NULL; |
|
|
554 | PL_comppad = 0; |
540 | PL_curpm = 0; |
555 | PL_curpm = 0; |
541 | PL_localizing = 0; |
556 | PL_localizing = 0; |
542 | PL_dirty = 0; |
557 | PL_dirty = 0; |
543 | PL_restartop = 0; |
558 | PL_restartop = 0; |
544 | |
559 | |
… | |
… | |
552 | Zero (&myop, 1, LOGOP); |
567 | Zero (&myop, 1, LOGOP); |
553 | myop.op_next = Nullop; |
568 | myop.op_next = Nullop; |
554 | myop.op_flags = OPf_WANT_VOID; |
569 | myop.op_flags = OPf_WANT_VOID; |
555 | |
570 | |
556 | PUSHMARK (SP); |
571 | PUSHMARK (SP); |
557 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
572 | XPUSHs (av_shift (GvAV (PL_defgv))); |
558 | PUTBACK; |
573 | PUTBACK; |
559 | PL_op = (OP *)&myop; |
574 | PL_op = (OP *)&myop; |
560 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
575 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
561 | SPAGAIN; |
576 | SPAGAIN; |
562 | } |
577 | } |
563 | |
578 | |
564 | ENTER; /* necessary e.g. for dounwind */ |
579 | ENTER; /* necessary e.g. for dounwind */ |
565 | } |
580 | } |
566 | |
581 | |
567 | static void |
582 | static void |
568 | free_coro_mortal () |
583 | free_coro_mortal (pTHX) |
569 | { |
584 | { |
570 | if (coro_mortal) |
585 | if (coro_mortal) |
571 | { |
586 | { |
572 | SvREFCNT_dec (coro_mortal); |
587 | SvREFCNT_dec (coro_mortal); |
573 | coro_mortal = 0; |
588 | coro_mortal = 0; |
574 | } |
589 | } |
575 | } |
590 | } |
576 | |
591 | |
577 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
592 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
|
|
593 | /* _cctx_init shoukld be careful, as it could be called at almost any time */ |
|
|
594 | /* during execution of a pelr program */ |
578 | static void NOINLINE |
595 | static void NOINLINE |
579 | prepare_cctx (coro_cctx *cctx) |
596 | prepare_cctx (pTHX_ coro_cctx *cctx) |
580 | { |
597 | { |
581 | dSP; |
598 | dSP; |
582 | LOGOP myop; |
599 | LOGOP myop; |
583 | |
600 | |
584 | Zero (&myop, 1, LOGOP); |
601 | Zero (&myop, 1, LOGOP); |
… | |
… | |
593 | PL_op = (OP *)&myop; |
610 | PL_op = (OP *)&myop; |
594 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
611 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
595 | SPAGAIN; |
612 | SPAGAIN; |
596 | } |
613 | } |
597 | |
614 | |
|
|
615 | /* |
|
|
616 | * this is a _very_ stripped down perl interpreter ;) |
|
|
617 | */ |
598 | static void |
618 | static void |
599 | coro_run (void *arg) |
619 | coro_run (void *arg) |
600 | { |
620 | { |
|
|
621 | dTHX; |
|
|
622 | |
601 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
623 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
602 | UNLOCK; |
624 | UNLOCK; |
603 | |
625 | |
604 | /* |
|
|
605 | * this is a _very_ stripped down perl interpreter ;) |
|
|
606 | */ |
|
|
607 | PL_top_env = &PL_start_env; |
626 | PL_top_env = &PL_start_env; |
608 | |
627 | |
609 | /* inject call to cctx_init */ |
628 | /* inject a fake subroutine call to cctx_init */ |
610 | prepare_cctx ((coro_cctx *)arg); |
629 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
611 | |
630 | |
612 | /* somebody will hit me for both perl_run and PL_restartop */ |
631 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
613 | PL_restartop = PL_op; |
632 | PL_restartop = PL_op; |
614 | perl_run (PL_curinterp); |
633 | perl_run (PL_curinterp); |
615 | |
634 | |
616 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
635 | /* |
617 | abort (); |
636 | * If perl-run returns we assume exit() was being called or the coro |
|
|
637 | * fell off the end, which seems to be the only valid (non-bug) |
|
|
638 | * reason for perl_run to return. We try to exit by jumping to the |
|
|
639 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
|
|
640 | * coroutine as Coro has no such concept |
|
|
641 | */ |
|
|
642 | PL_top_env = main_top_env; |
|
|
643 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
618 | } |
644 | } |
619 | |
645 | |
620 | static coro_cctx * |
646 | static coro_cctx * |
621 | cctx_new () |
647 | cctx_new () |
622 | { |
648 | { |
623 | coro_cctx *cctx; |
649 | coro_cctx *cctx; |
|
|
650 | void *stack_start; |
|
|
651 | size_t stack_size; |
624 | |
652 | |
625 | ++cctx_count; |
653 | ++cctx_count; |
626 | |
654 | |
627 | Newz (0, cctx, 1, coro_cctx); |
655 | Newz (0, cctx, 1, coro_cctx); |
628 | |
656 | |
629 | #if HAVE_MMAP |
657 | #if HAVE_MMAP |
630 | |
658 | |
631 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
659 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
632 | /* mmap supposedly does allocate-on-write for us */ |
660 | /* mmap supposedly does allocate-on-write for us */ |
633 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
661 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
634 | |
662 | |
635 | if (cctx->sptr == (void *)-1) |
663 | if (cctx->sptr != (void *)-1) |
636 | { |
|
|
637 | perror ("FATAL: unable to mmap stack for coroutine"); |
|
|
638 | _exit (EXIT_FAILURE); |
|
|
639 | } |
664 | { |
640 | |
|
|
641 | # if STACKGUARD |
665 | # if CORO_STACKGUARD |
642 | mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
666 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
643 | # endif |
667 | # endif |
|
|
668 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
|
|
669 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
|
|
670 | cctx->mapped = 1; |
|
|
671 | } |
|
|
672 | else |
|
|
673 | #endif |
|
|
674 | { |
|
|
675 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
|
|
676 | New (0, cctx->sptr, coro_stacksize, long); |
644 | |
677 | |
645 | #else |
|
|
646 | |
|
|
647 | cctx->ssize = STACKSIZE * (long)sizeof (long); |
|
|
648 | New (0, cctx->sptr, STACKSIZE, long); |
|
|
649 | |
|
|
650 | if (!cctx->sptr) |
678 | if (!cctx->sptr) |
651 | { |
679 | { |
652 | perror ("FATAL: unable to malloc stack for coroutine"); |
680 | perror ("FATAL: unable to allocate stack for coroutine"); |
653 | _exit (EXIT_FAILURE); |
681 | _exit (EXIT_FAILURE); |
654 | } |
682 | } |
655 | |
683 | |
656 | #endif |
684 | stack_start = cctx->sptr; |
|
|
685 | stack_size = cctx->ssize; |
|
|
686 | } |
657 | |
687 | |
658 | #if USE_VALGRIND |
688 | REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); |
659 | cctx->valgrind_id = VALGRIND_STACK_REGISTER ( |
|
|
660 | STACKGUARD * PAGESIZE + (char *)cctx->sptr, |
|
|
661 | cctx->ssize + (char *)cctx->sptr |
|
|
662 | ); |
|
|
663 | #endif |
|
|
664 | |
|
|
665 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); |
689 | coro_create (&cctx->cctx, coro_run, (void *)cctx, stack_start, stack_size); |
666 | |
690 | |
667 | return cctx; |
691 | return cctx; |
668 | } |
692 | } |
669 | |
693 | |
670 | static void |
694 | static void |
… | |
… | |
673 | if (!cctx) |
697 | if (!cctx) |
674 | return; |
698 | return; |
675 | |
699 | |
676 | --cctx_count; |
700 | --cctx_count; |
677 | |
701 | |
678 | #if USE_VALGRIND |
702 | #if CORO_USE_VALGRIND |
679 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
703 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
680 | #endif |
704 | #endif |
681 | |
705 | |
682 | #if HAVE_MMAP |
706 | #if HAVE_MMAP |
|
|
707 | if (cctx->mapped) |
683 | munmap (cctx->sptr, cctx->ssize); |
708 | munmap (cctx->sptr, cctx->ssize); |
684 | #else |
709 | else |
|
|
710 | #endif |
685 | Safefree (cctx->sptr); |
711 | Safefree (cctx->sptr); |
686 | #endif |
|
|
687 | |
712 | |
688 | Safefree (cctx); |
713 | Safefree (cctx); |
689 | } |
714 | } |
690 | |
715 | |
691 | static coro_cctx * |
716 | static coro_cctx * |
692 | cctx_get () |
717 | cctx_get (pTHX) |
693 | { |
718 | { |
694 | coro_cctx *cctx; |
|
|
695 | |
|
|
696 | if (cctx_first) |
719 | while (cctx_first) |
697 | { |
720 | { |
698 | cctx = cctx_first; |
721 | coro_cctx *cctx = cctx_first; |
699 | cctx_first = cctx->next; |
722 | cctx_first = cctx->next; |
700 | --cctx_idle; |
723 | --cctx_idle; |
|
|
724 | |
|
|
725 | if (cctx->ssize >= coro_stacksize) |
|
|
726 | return cctx; |
|
|
727 | |
|
|
728 | cctx_destroy (cctx); |
701 | } |
729 | } |
702 | else |
730 | |
703 | { |
|
|
704 | cctx = cctx_new (); |
|
|
705 | PL_op = PL_op->op_next; |
731 | PL_op = PL_op->op_next; |
706 | } |
|
|
707 | |
|
|
708 | return cctx; |
732 | return cctx_new (); |
709 | } |
733 | } |
710 | |
734 | |
711 | static void |
735 | static void |
712 | cctx_put (coro_cctx *cctx) |
736 | cctx_put (coro_cctx *cctx) |
713 | { |
737 | { |
… | |
… | |
729 | |
753 | |
730 | /** coroutine switching *****************************************************/ |
754 | /** coroutine switching *****************************************************/ |
731 | |
755 | |
732 | /* never call directly, always through the coro_state_transfer global variable */ |
756 | /* never call directly, always through the coro_state_transfer global variable */ |
733 | static void NOINLINE |
757 | static void NOINLINE |
734 | transfer (struct coro *prev, struct coro *next) |
758 | transfer (pTHX_ struct coro *prev, struct coro *next) |
735 | { |
759 | { |
736 | dSTACKLEVEL; |
760 | dSTACKLEVEL; |
737 | |
761 | |
738 | /* sometimes transfer is only called to set idle_sp */ |
762 | /* sometimes transfer is only called to set idle_sp */ |
739 | if (!next) |
763 | if (!next) |
… | |
… | |
772 | if (next->flags & CF_NEW) |
796 | if (next->flags & CF_NEW) |
773 | { |
797 | { |
774 | /* need to start coroutine */ |
798 | /* need to start coroutine */ |
775 | next->flags &= ~CF_NEW; |
799 | next->flags &= ~CF_NEW; |
776 | /* first get rid of the old state */ |
800 | /* first get rid of the old state */ |
777 | save_perl (prev); |
801 | save_perl (aTHX_ prev); |
778 | /* setup coroutine call */ |
802 | /* setup coroutine call */ |
779 | setup_coro (next); |
803 | setup_coro (aTHX_ next); |
780 | /* need a new stack */ |
804 | /* need a new stack */ |
781 | assert (!next->cctx); |
805 | assert (!next->cctx); |
782 | } |
806 | } |
783 | else |
807 | else |
784 | { |
808 | { |
785 | /* coroutine already started */ |
809 | /* coroutine already started */ |
786 | save_perl (prev); |
810 | save_perl (aTHX_ prev); |
787 | load_perl (next); |
811 | load_perl (aTHX_ next); |
788 | } |
812 | } |
789 | |
813 | |
790 | prev__cctx = prev->cctx; |
814 | prev__cctx = prev->cctx; |
791 | |
815 | |
792 | /* possibly "free" the cctx */ |
816 | /* possibly "free" the cctx */ |
… | |
… | |
801 | prev__cctx->inuse = 0; |
825 | prev__cctx->inuse = 0; |
802 | } |
826 | } |
803 | |
827 | |
804 | if (!next->cctx) |
828 | if (!next->cctx) |
805 | { |
829 | { |
806 | next->cctx = cctx_get (); |
830 | next->cctx = cctx_get (aTHX); |
807 | assert (!next->cctx->inuse); |
831 | assert (!next->cctx->inuse); |
808 | next->cctx->inuse = 1; |
832 | next->cctx->inuse = 1; |
809 | } |
833 | } |
810 | |
834 | |
811 | if (prev__cctx != next->cctx) |
835 | if (prev__cctx != next->cctx) |
… | |
… | |
813 | prev__cctx->top_env = PL_top_env; |
837 | prev__cctx->top_env = PL_top_env; |
814 | PL_top_env = next->cctx->top_env; |
838 | PL_top_env = next->cctx->top_env; |
815 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
839 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
816 | } |
840 | } |
817 | |
841 | |
818 | free_coro_mortal (); |
842 | free_coro_mortal (aTHX); |
819 | UNLOCK; |
843 | UNLOCK; |
820 | } |
844 | } |
821 | } |
845 | } |
822 | |
846 | |
823 | struct transfer_args |
847 | struct transfer_args |
824 | { |
848 | { |
825 | struct coro *prev, *next; |
849 | struct coro *prev, *next; |
826 | }; |
850 | }; |
827 | |
851 | |
828 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
852 | #define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) |
829 | |
853 | |
830 | /** high level stuff ********************************************************/ |
854 | /** high level stuff ********************************************************/ |
831 | |
855 | |
832 | static int |
856 | static int |
833 | coro_state_destroy (struct coro *coro) |
857 | coro_state_destroy (pTHX_ struct coro *coro) |
834 | { |
858 | { |
835 | if (coro->flags & CF_DESTROYED) |
859 | if (coro->flags & CF_DESTROYED) |
836 | return 0; |
860 | return 0; |
837 | |
861 | |
838 | coro->flags |= CF_DESTROYED; |
862 | coro->flags |= CF_DESTROYED; |
839 | |
863 | |
840 | if (coro->flags & CF_READY) |
864 | if (coro->flags & CF_READY) |
841 | { |
865 | { |
842 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
866 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
843 | /* alternative: look through all ready queues and remove it */ |
867 | /* alternative: look through all ready queues and remove the coro */ |
844 | LOCK; |
868 | LOCK; |
845 | --coro_nready; |
869 | --coro_nready; |
846 | UNLOCK; |
870 | UNLOCK; |
847 | } |
871 | } |
848 | else |
872 | else |
849 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
873 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
850 | |
874 | |
851 | if (coro->mainstack && coro->mainstack != main_mainstack) |
875 | if (coro->mainstack && coro->mainstack != main_mainstack) |
852 | { |
876 | { |
|
|
877 | struct coro temp; |
|
|
878 | |
853 | assert (!(coro->flags & CF_RUNNING)); |
879 | assert (!(coro->flags & CF_RUNNING)); |
854 | |
880 | |
855 | struct coro temp; |
|
|
856 | Zero (&temp, 1, struct coro); |
881 | Zero (&temp, 1, struct coro); |
857 | temp.save = CORO_SAVE_ALL; |
882 | temp.save = CORO_SAVE_DEF; |
858 | |
883 | |
859 | if (coro->flags & CF_RUNNING) |
884 | if (coro->flags & CF_RUNNING) |
860 | croak ("FATAL: tried to destroy currently running coroutine"); |
885 | croak ("FATAL: tried to destroy currently running coroutine"); |
861 | |
886 | |
862 | save_perl (&temp); |
887 | save_perl (aTHX_ &temp); |
863 | load_perl (coro); |
888 | load_perl (aTHX_ coro); |
864 | |
889 | |
865 | coro_destroy_stacks (); |
890 | coro_destroy_stacks (aTHX); |
866 | |
891 | |
867 | load_perl (&temp); /* this will get rid of defsv etc.. */ |
892 | load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ |
868 | |
893 | |
869 | coro->mainstack = 0; |
894 | coro->mainstack = 0; |
870 | } |
895 | } |
871 | |
896 | |
872 | cctx_destroy (coro->cctx); |
897 | cctx_destroy (coro->cctx); |
… | |
… | |
881 | struct coro *coro = (struct coro *)mg->mg_ptr; |
906 | struct coro *coro = (struct coro *)mg->mg_ptr; |
882 | mg->mg_ptr = 0; |
907 | mg->mg_ptr = 0; |
883 | |
908 | |
884 | if (--coro->refcnt < 0) |
909 | if (--coro->refcnt < 0) |
885 | { |
910 | { |
886 | coro_state_destroy (coro); |
911 | coro_state_destroy (aTHX_ coro); |
887 | Safefree (coro); |
912 | Safefree (coro); |
888 | } |
913 | } |
889 | |
914 | |
890 | return 0; |
915 | return 0; |
891 | } |
916 | } |
… | |
… | |
910 | # define MGf_DUP 0 |
935 | # define MGf_DUP 0 |
911 | #endif |
936 | #endif |
912 | }; |
937 | }; |
913 | |
938 | |
914 | static struct coro * |
939 | static struct coro * |
915 | SvSTATE (SV *coro) |
940 | SvSTATE_ (pTHX_ SV *coro) |
916 | { |
941 | { |
917 | HV *stash; |
942 | HV *stash; |
918 | MAGIC *mg; |
943 | MAGIC *mg; |
919 | |
944 | |
920 | if (SvROK (coro)) |
945 | if (SvROK (coro)) |
… | |
… | |
926 | /* very slow, but rare, check */ |
951 | /* very slow, but rare, check */ |
927 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
952 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
928 | croak ("Coro::State object required"); |
953 | croak ("Coro::State object required"); |
929 | } |
954 | } |
930 | |
955 | |
931 | mg = SvMAGIC (coro); |
956 | mg = CORO_MAGIC (coro); |
932 | assert (mg->mg_type == PERL_MAGIC_ext); |
|
|
933 | return (struct coro *)mg->mg_ptr; |
957 | return (struct coro *)mg->mg_ptr; |
934 | } |
958 | } |
935 | |
959 | |
|
|
960 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
961 | |
936 | static void |
962 | static void |
937 | prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
963 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
938 | { |
964 | { |
939 | ta->prev = SvSTATE (prev_sv); |
965 | ta->prev = SvSTATE (prev_sv); |
940 | ta->next = SvSTATE (next_sv); |
966 | ta->next = SvSTATE (next_sv); |
941 | } |
967 | } |
942 | |
968 | |
943 | static void |
969 | static void |
944 | api_transfer (SV *prev_sv, SV *next_sv) |
970 | api_transfer (SV *prev_sv, SV *next_sv) |
945 | { |
971 | { |
|
|
972 | dTHX; |
946 | struct transfer_args ta; |
973 | struct transfer_args ta; |
947 | |
974 | |
948 | prepare_transfer (&ta, prev_sv, next_sv); |
975 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
949 | TRANSFER (ta); |
976 | TRANSFER (ta); |
950 | } |
977 | } |
951 | |
978 | |
952 | static int |
979 | static int |
953 | api_save (SV *coro_sv, int new_save) |
980 | api_save (SV *coro_sv, int new_save) |
954 | { |
981 | { |
|
|
982 | dTHX; |
955 | struct coro *coro = SvSTATE (coro_sv); |
983 | struct coro *coro = SvSTATE (coro_sv); |
956 | int old_save = coro->save; |
984 | int old_save = coro->save; |
957 | |
985 | |
958 | if (new_save >= 0) |
986 | if (new_save >= 0) |
959 | coro->save = new_save; |
987 | coro->save = new_save; |
… | |
… | |
962 | } |
990 | } |
963 | |
991 | |
964 | /** Coro ********************************************************************/ |
992 | /** Coro ********************************************************************/ |
965 | |
993 | |
966 | static void |
994 | static void |
967 | coro_enq (SV *coro_sv) |
995 | coro_enq (pTHX_ SV *coro_sv) |
968 | { |
996 | { |
969 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
997 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
970 | } |
998 | } |
971 | |
999 | |
972 | static SV * |
1000 | static SV * |
973 | coro_deq (int min_prio) |
1001 | coro_deq (pTHX_ int min_prio) |
974 | { |
1002 | { |
975 | int prio = PRIO_MAX - PRIO_MIN; |
1003 | int prio = PRIO_MAX - PRIO_MIN; |
976 | |
1004 | |
977 | min_prio -= PRIO_MIN; |
1005 | min_prio -= PRIO_MIN; |
978 | if (min_prio < 0) |
1006 | if (min_prio < 0) |
… | |
… | |
986 | } |
1014 | } |
987 | |
1015 | |
988 | static int |
1016 | static int |
989 | api_ready (SV *coro_sv) |
1017 | api_ready (SV *coro_sv) |
990 | { |
1018 | { |
|
|
1019 | dTHX; |
991 | struct coro *coro; |
1020 | struct coro *coro; |
992 | |
1021 | |
993 | if (SvROK (coro_sv)) |
1022 | if (SvROK (coro_sv)) |
994 | coro_sv = SvRV (coro_sv); |
1023 | coro_sv = SvRV (coro_sv); |
995 | |
1024 | |
… | |
… | |
999 | return 0; |
1028 | return 0; |
1000 | |
1029 | |
1001 | coro->flags |= CF_READY; |
1030 | coro->flags |= CF_READY; |
1002 | |
1031 | |
1003 | LOCK; |
1032 | LOCK; |
1004 | coro_enq (SvREFCNT_inc (coro_sv)); |
1033 | coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); |
1005 | ++coro_nready; |
1034 | ++coro_nready; |
1006 | UNLOCK; |
1035 | UNLOCK; |
1007 | |
1036 | |
1008 | return 1; |
1037 | return 1; |
1009 | } |
1038 | } |
1010 | |
1039 | |
1011 | static int |
1040 | static int |
1012 | api_is_ready (SV *coro_sv) |
1041 | api_is_ready (SV *coro_sv) |
1013 | { |
1042 | { |
|
|
1043 | dTHX; |
1014 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1044 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1015 | } |
1045 | } |
1016 | |
1046 | |
1017 | static void |
1047 | static void |
1018 | prepare_schedule (struct transfer_args *ta) |
1048 | prepare_schedule (pTHX_ struct transfer_args *ta) |
1019 | { |
1049 | { |
1020 | SV *prev_sv, *next_sv; |
1050 | SV *prev_sv, *next_sv; |
1021 | |
1051 | |
1022 | for (;;) |
1052 | for (;;) |
1023 | { |
1053 | { |
1024 | LOCK; |
1054 | LOCK; |
1025 | next_sv = coro_deq (PRIO_MIN); |
1055 | next_sv = coro_deq (aTHX_ PRIO_MIN); |
1026 | UNLOCK; |
|
|
1027 | |
1056 | |
1028 | /* nothing to schedule: call the idle handler */ |
1057 | /* nothing to schedule: call the idle handler */ |
1029 | if (!next_sv) |
1058 | if (!next_sv) |
1030 | { |
1059 | { |
1031 | dSP; |
1060 | dSP; |
|
|
1061 | UNLOCK; |
1032 | |
1062 | |
1033 | ENTER; |
1063 | ENTER; |
1034 | SAVETMPS; |
1064 | SAVETMPS; |
1035 | |
1065 | |
1036 | PUSHMARK (SP); |
1066 | PUSHMARK (SP); |
… | |
… | |
1045 | ta->next = SvSTATE (next_sv); |
1075 | ta->next = SvSTATE (next_sv); |
1046 | |
1076 | |
1047 | /* cannot transfer to destroyed coros, skip and look for next */ |
1077 | /* cannot transfer to destroyed coros, skip and look for next */ |
1048 | if (ta->next->flags & CF_DESTROYED) |
1078 | if (ta->next->flags & CF_DESTROYED) |
1049 | { |
1079 | { |
|
|
1080 | UNLOCK; |
1050 | SvREFCNT_dec (next_sv); |
1081 | SvREFCNT_dec (next_sv); |
1051 | /* coro_nready is already taken care of by destroy */ |
1082 | /* coro_nready is already taken care of by destroy */ |
1052 | continue; |
1083 | continue; |
1053 | } |
1084 | } |
1054 | |
1085 | |
1055 | LOCK; |
|
|
1056 | --coro_nready; |
1086 | --coro_nready; |
1057 | UNLOCK; |
1087 | UNLOCK; |
1058 | break; |
1088 | break; |
1059 | } |
1089 | } |
1060 | |
1090 | |
… | |
… | |
1065 | |
1095 | |
1066 | assert (ta->next->flags & CF_READY); |
1096 | assert (ta->next->flags & CF_READY); |
1067 | ta->next->flags &= ~CF_READY; |
1097 | ta->next->flags &= ~CF_READY; |
1068 | |
1098 | |
1069 | LOCK; |
1099 | LOCK; |
1070 | free_coro_mortal (); |
1100 | free_coro_mortal (aTHX); |
1071 | coro_mortal = prev_sv; |
1101 | coro_mortal = prev_sv; |
1072 | UNLOCK; |
1102 | UNLOCK; |
1073 | } |
1103 | } |
1074 | |
1104 | |
1075 | static void |
1105 | static void |
1076 | prepare_cede (struct transfer_args *ta) |
1106 | prepare_cede (pTHX_ struct transfer_args *ta) |
1077 | { |
1107 | { |
1078 | api_ready (coro_current); |
1108 | api_ready (coro_current); |
1079 | prepare_schedule (ta); |
1109 | prepare_schedule (aTHX_ ta); |
1080 | } |
1110 | } |
1081 | |
1111 | |
1082 | static int |
1112 | static int |
1083 | prepare_cede_notself (struct transfer_args *ta) |
1113 | prepare_cede_notself (pTHX_ struct transfer_args *ta) |
1084 | { |
1114 | { |
1085 | if (coro_nready) |
1115 | if (coro_nready) |
1086 | { |
1116 | { |
1087 | SV *prev = SvRV (coro_current); |
1117 | SV *prev = SvRV (coro_current); |
1088 | prepare_schedule (ta); |
1118 | prepare_schedule (aTHX_ ta); |
1089 | api_ready (prev); |
1119 | api_ready (prev); |
1090 | return 1; |
1120 | return 1; |
1091 | } |
1121 | } |
1092 | else |
1122 | else |
1093 | return 0; |
1123 | return 0; |
1094 | } |
1124 | } |
1095 | |
1125 | |
1096 | static void |
1126 | static void |
1097 | api_schedule (void) |
1127 | api_schedule (void) |
1098 | { |
1128 | { |
|
|
1129 | dTHX; |
1099 | struct transfer_args ta; |
1130 | struct transfer_args ta; |
1100 | |
1131 | |
1101 | prepare_schedule (&ta); |
1132 | prepare_schedule (aTHX_ &ta); |
1102 | TRANSFER (ta); |
1133 | TRANSFER (ta); |
1103 | } |
1134 | } |
1104 | |
1135 | |
1105 | static int |
1136 | static int |
1106 | api_cede (void) |
1137 | api_cede (void) |
1107 | { |
1138 | { |
|
|
1139 | dTHX; |
1108 | struct transfer_args ta; |
1140 | struct transfer_args ta; |
1109 | |
1141 | |
1110 | prepare_cede (&ta); |
1142 | prepare_cede (aTHX_ &ta); |
1111 | |
1143 | |
1112 | if (ta.prev != ta.next) |
1144 | if (ta.prev != ta.next) |
1113 | { |
1145 | { |
1114 | TRANSFER (ta); |
1146 | TRANSFER (ta); |
1115 | return 1; |
1147 | return 1; |
… | |
… | |
1119 | } |
1151 | } |
1120 | |
1152 | |
1121 | static int |
1153 | static int |
1122 | api_cede_notself (void) |
1154 | api_cede_notself (void) |
1123 | { |
1155 | { |
|
|
1156 | dTHX; |
1124 | struct transfer_args ta; |
1157 | struct transfer_args ta; |
1125 | |
1158 | |
1126 | if (prepare_cede_notself (&ta)) |
1159 | if (prepare_cede_notself (aTHX_ &ta)) |
1127 | { |
1160 | { |
1128 | TRANSFER (ta); |
1161 | TRANSFER (ta); |
1129 | return 1; |
1162 | return 1; |
1130 | } |
1163 | } |
1131 | else |
1164 | else |
… | |
… | |
1147 | |
1180 | |
1148 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1181 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1149 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1182 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1150 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
1183 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
1151 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
1184 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
|
|
1185 | newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH)); |
|
|
1186 | newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF)); |
1152 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
1187 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
1153 | |
1188 | |
1154 | main_mainstack = PL_mainstack; |
1189 | main_mainstack = PL_mainstack; |
|
|
1190 | main_top_env = PL_top_env; |
|
|
1191 | |
|
|
1192 | while (main_top_env->je_prev) |
|
|
1193 | main_top_env = main_top_env->je_prev; |
1155 | |
1194 | |
1156 | coroapi.ver = CORO_API_VERSION; |
1195 | coroapi.ver = CORO_API_VERSION; |
1157 | coroapi.transfer = api_transfer; |
1196 | coroapi.transfer = api_transfer; |
1158 | |
1197 | |
1159 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1198 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
… | |
… | |
1167 | HV *hv; |
1206 | HV *hv; |
1168 | int i; |
1207 | int i; |
1169 | |
1208 | |
1170 | Newz (0, coro, 1, struct coro); |
1209 | Newz (0, coro, 1, struct coro); |
1171 | coro->args = newAV (); |
1210 | coro->args = newAV (); |
1172 | coro->save = CORO_SAVE_ALL; |
1211 | coro->save = CORO_SAVE_DEF; |
1173 | coro->flags = CF_NEW; |
1212 | coro->flags = CF_NEW; |
1174 | |
1213 | |
1175 | hv = newHV (); |
1214 | hv = newHV (); |
1176 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1215 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1177 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1216 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
… | |
… | |
1184 | |
1223 | |
1185 | int |
1224 | int |
1186 | save (SV *coro, int new_save = -1) |
1225 | save (SV *coro, int new_save = -1) |
1187 | CODE: |
1226 | CODE: |
1188 | RETVAL = api_save (coro, new_save); |
1227 | RETVAL = api_save (coro, new_save); |
|
|
1228 | OUTPUT: |
|
|
1229 | RETVAL |
|
|
1230 | |
|
|
1231 | int |
|
|
1232 | save_also (SV *coro_sv, int save_also) |
|
|
1233 | CODE: |
|
|
1234 | { |
|
|
1235 | struct coro *coro = SvSTATE (coro_sv); |
|
|
1236 | RETVAL = coro->save; |
|
|
1237 | coro->save |= save_also; |
|
|
1238 | } |
1189 | OUTPUT: |
1239 | OUTPUT: |
1190 | RETVAL |
1240 | RETVAL |
1191 | |
1241 | |
1192 | void |
1242 | void |
1193 | _set_stacklevel (...) |
1243 | _set_stacklevel (...) |
… | |
… | |
1209 | |
1259 | |
1210 | case 1: |
1260 | case 1: |
1211 | if (items != 2) |
1261 | if (items != 2) |
1212 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1262 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1213 | |
1263 | |
1214 | prepare_transfer (&ta, ST (0), ST (1)); |
1264 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1215 | break; |
1265 | break; |
1216 | |
1266 | |
1217 | case 2: |
1267 | case 2: |
1218 | prepare_schedule (&ta); |
1268 | prepare_schedule (aTHX_ &ta); |
1219 | break; |
1269 | break; |
1220 | |
1270 | |
1221 | case 3: |
1271 | case 3: |
1222 | prepare_cede (&ta); |
1272 | prepare_cede (aTHX_ &ta); |
1223 | break; |
1273 | break; |
1224 | |
1274 | |
1225 | case 4: |
1275 | case 4: |
1226 | if (!prepare_cede_notself (&ta)) |
1276 | if (!prepare_cede_notself (aTHX_ &ta)) |
1227 | XSRETURN_EMPTY; |
1277 | XSRETURN_EMPTY; |
1228 | |
1278 | |
1229 | break; |
1279 | break; |
1230 | } |
1280 | } |
1231 | |
1281 | |
… | |
… | |
1237 | } |
1287 | } |
1238 | |
1288 | |
1239 | bool |
1289 | bool |
1240 | _destroy (SV *coro_sv) |
1290 | _destroy (SV *coro_sv) |
1241 | CODE: |
1291 | CODE: |
1242 | RETVAL = coro_state_destroy (SvSTATE (coro_sv)); |
1292 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
1243 | OUTPUT: |
1293 | OUTPUT: |
1244 | RETVAL |
1294 | RETVAL |
1245 | |
1295 | |
1246 | void |
1296 | void |
1247 | _exit (code) |
1297 | _exit (code) |
1248 | int code |
1298 | int code |
1249 | PROTOTYPE: $ |
1299 | PROTOTYPE: $ |
1250 | CODE: |
1300 | CODE: |
1251 | _exit (code); |
1301 | _exit (code); |
|
|
1302 | |
|
|
1303 | int |
|
|
1304 | cctx_stacksize (int new_stacksize = 0) |
|
|
1305 | CODE: |
|
|
1306 | RETVAL = coro_stacksize; |
|
|
1307 | if (new_stacksize) |
|
|
1308 | coro_stacksize = new_stacksize; |
|
|
1309 | OUTPUT: |
|
|
1310 | RETVAL |
1252 | |
1311 | |
1253 | int |
1312 | int |
1254 | cctx_count () |
1313 | cctx_count () |
1255 | CODE: |
1314 | CODE: |
1256 | RETVAL = cctx_count; |
1315 | RETVAL = cctx_count; |