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 | |
… | |
… | |
124 | I32 laststype; |
126 | I32 laststype; |
125 | int laststatval; |
127 | int laststatval; |
126 | Stat_t statcache; |
128 | Stat_t statcache; |
127 | }; |
129 | }; |
128 | |
130 | |
|
|
131 | static size_t coro_stacksize = CORO_STACKSIZE; |
129 | static struct CoroAPI coroapi; |
132 | static struct CoroAPI coroapi; |
130 | 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; |
131 | static HV *coro_state_stash, *coro_stash; |
135 | static HV *coro_state_stash, *coro_stash; |
132 | static SV *coro_mortal; /* will be freed after next transfer */ |
136 | static SV *coro_mortal; /* will be freed after next transfer */ |
133 | |
137 | |
134 | static struct coro_cctx *cctx_first; |
138 | static struct coro_cctx *cctx_first; |
135 | static int cctx_count, cctx_idle; |
139 | static int cctx_count, cctx_idle; |
… | |
… | |
138 | typedef struct coro_cctx { |
142 | typedef struct coro_cctx { |
139 | struct coro_cctx *next; |
143 | struct coro_cctx *next; |
140 | |
144 | |
141 | /* the stack */ |
145 | /* the stack */ |
142 | void *sptr; |
146 | void *sptr; |
143 | ssize_t ssize; /* positive == mmap, otherwise malloc */ |
147 | size_t ssize; |
144 | |
148 | |
145 | /* cpu state */ |
149 | /* cpu state */ |
146 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
150 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
147 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
151 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
148 | JMPENV *top_env; |
152 | JMPENV *top_env; |
149 | coro_context cctx; |
153 | coro_context cctx; |
150 | |
154 | |
151 | int inuse; |
|
|
152 | |
|
|
153 | #if CORO_USE_VALGRIND |
155 | #if CORO_USE_VALGRIND |
154 | int valgrind_id; |
156 | int valgrind_id; |
155 | #endif |
157 | #endif |
|
|
158 | char inuse, mapped; |
156 | } coro_cctx; |
159 | } coro_cctx; |
157 | |
160 | |
158 | enum { |
161 | enum { |
159 | CF_RUNNING = 0x0001, /* coroutine is running */ |
162 | CF_RUNNING = 0x0001, /* coroutine is running */ |
160 | CF_READY = 0x0002, /* coroutine is ready */ |
163 | CF_READY = 0x0002, /* coroutine is ready */ |
… | |
… | |
175 | |
178 | |
176 | /* optionally saved, might be zero */ |
179 | /* optionally saved, might be zero */ |
177 | AV *defav; /* @_ */ |
180 | AV *defav; /* @_ */ |
178 | SV *defsv; /* $_ */ |
181 | SV *defsv; /* $_ */ |
179 | SV *errsv; /* $@ */ |
182 | SV *errsv; /* $@ */ |
|
|
183 | GV *deffh; /* default filehandle */ |
180 | SV *irssv; /* $/ */ |
184 | SV *irssv; /* $/ */ |
181 | SV *irssv_sv; /* real $/ cache */ |
185 | SV *irssv_sv; /* real $/ cache */ |
182 | |
186 | |
183 | #define VAR(name,type) type name; |
187 | #define VAR(name,type) type name; |
184 | # include "state.h" |
188 | # include "state.h" |
… | |
… | |
206 | static int coro_nready; |
210 | static int coro_nready; |
207 | |
211 | |
208 | /** lowlevel stuff **********************************************************/ |
212 | /** lowlevel stuff **********************************************************/ |
209 | |
213 | |
210 | static AV * |
214 | static AV * |
211 | coro_clone_padlist (CV *cv) |
215 | coro_clone_padlist (pTHX_ CV *cv) |
212 | { |
216 | { |
213 | AV *padlist = CvPADLIST (cv); |
217 | AV *padlist = CvPADLIST (cv); |
214 | AV *newpadlist, *newpad; |
218 | AV *newpadlist, *newpad; |
215 | |
219 | |
216 | newpadlist = newAV (); |
220 | newpadlist = newAV (); |
… | |
… | |
228 | |
232 | |
229 | return newpadlist; |
233 | return newpadlist; |
230 | } |
234 | } |
231 | |
235 | |
232 | static void |
236 | static void |
233 | free_padlist (AV *padlist) |
237 | free_padlist (pTHX_ AV *padlist) |
234 | { |
238 | { |
235 | /* may be during global destruction */ |
239 | /* may be during global destruction */ |
236 | if (SvREFCNT (padlist)) |
240 | if (SvREFCNT (padlist)) |
237 | { |
241 | { |
238 | I32 i = AvFILLp (padlist); |
242 | I32 i = AvFILLp (padlist); |
… | |
… | |
259 | AV *padlist; |
263 | AV *padlist; |
260 | AV *av = (AV *)mg->mg_obj; |
264 | AV *av = (AV *)mg->mg_obj; |
261 | |
265 | |
262 | /* casting is fun. */ |
266 | /* casting is fun. */ |
263 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
267 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
264 | free_padlist (padlist); |
268 | free_padlist (aTHX_ padlist); |
265 | |
269 | |
266 | SvREFCNT_dec (av); |
270 | SvREFCNT_dec (av); |
267 | |
271 | |
268 | return 0; |
272 | return 0; |
269 | } |
273 | } |
… | |
… | |
279 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
283 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
280 | : 0 |
284 | : 0 |
281 | |
285 | |
282 | /* the next two functions merely cache the padlists */ |
286 | /* the next two functions merely cache the padlists */ |
283 | static void |
287 | static void |
284 | get_padlist (CV *cv) |
288 | get_padlist (pTHX_ CV *cv) |
285 | { |
289 | { |
286 | MAGIC *mg = CORO_MAGIC (cv); |
290 | MAGIC *mg = CORO_MAGIC (cv); |
287 | AV *av; |
291 | AV *av; |
288 | |
292 | |
289 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
293 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
… | |
… | |
295 | CV *cp = Perl_cv_clone (cv); |
299 | CV *cp = Perl_cv_clone (cv); |
296 | CvPADLIST (cv) = CvPADLIST (cp); |
300 | CvPADLIST (cv) = CvPADLIST (cp); |
297 | CvPADLIST (cp) = 0; |
301 | CvPADLIST (cp) = 0; |
298 | SvREFCNT_dec (cp); |
302 | SvREFCNT_dec (cp); |
299 | #else |
303 | #else |
300 | CvPADLIST (cv) = coro_clone_padlist (cv); |
304 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
301 | #endif |
305 | #endif |
302 | } |
306 | } |
303 | } |
307 | } |
304 | |
308 | |
305 | static void |
309 | static void |
306 | put_padlist (CV *cv) |
310 | put_padlist (pTHX_ CV *cv) |
307 | { |
311 | { |
308 | MAGIC *mg = CORO_MAGIC (cv); |
312 | MAGIC *mg = CORO_MAGIC (cv); |
309 | AV *av; |
313 | AV *av; |
310 | |
314 | |
311 | if (!mg) |
315 | if (!mg) |
… | |
… | |
330 | #define SE } while (0) |
334 | #define SE } while (0) |
331 | |
335 | |
332 | #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 |
333 | |
337 | |
334 | static void |
338 | static void |
335 | load_perl (Coro__State c) |
339 | load_perl (pTHX_ Coro__State c) |
336 | { |
340 | { |
337 | #define VAR(name,type) PL_ ## name = c->name; |
341 | #define VAR(name,type) PL_ ## name = c->name; |
338 | # include "state.h" |
342 | # include "state.h" |
339 | #undef VAR |
343 | #undef VAR |
340 | |
344 | |
341 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
345 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
342 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
346 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
343 | 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 | |
344 | if (c->irssv) |
350 | if (c->irssv) |
345 | { |
351 | { |
346 | if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) |
352 | if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) |
|
|
353 | { |
347 | SvREFCNT_dec (c->irssv); |
354 | SvREFCNT_dec (c->irssv); |
|
|
355 | c->irssv = 0; |
|
|
356 | } |
348 | else |
357 | else |
349 | { |
358 | { |
350 | REPLACE_SV (PL_rs, c->irssv); |
359 | REPLACE_SV (PL_rs, c->irssv); |
351 | if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); |
360 | if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); |
352 | sv_setsv (c->irssv_sv, PL_rs); |
361 | sv_setsv (c->irssv_sv, PL_rs); |
… | |
… | |
358 | CV *cv; |
367 | CV *cv; |
359 | |
368 | |
360 | /* now do the ugly restore mess */ |
369 | /* now do the ugly restore mess */ |
361 | while ((cv = (CV *)POPs)) |
370 | while ((cv = (CV *)POPs)) |
362 | { |
371 | { |
363 | put_padlist (cv); /* mark this padlist as available */ |
372 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
364 | CvDEPTH (cv) = PTR2IV (POPs); |
373 | CvDEPTH (cv) = PTR2IV (POPs); |
365 | CvPADLIST (cv) = (AV *)POPs; |
374 | CvPADLIST (cv) = (AV *)POPs; |
366 | } |
375 | } |
367 | |
376 | |
368 | PUTBACK; |
377 | PUTBACK; |
369 | } |
378 | } |
370 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
379 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
371 | } |
380 | } |
372 | |
381 | |
373 | static void |
382 | static void |
374 | save_perl (Coro__State c) |
383 | save_perl (pTHX_ Coro__State c) |
375 | { |
384 | { |
376 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
385 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
377 | { |
386 | { |
378 | dSP; |
387 | dSP; |
379 | I32 cxix = cxstack_ix; |
388 | I32 cxix = cxstack_ix; |
… | |
… | |
404 | PUSHs ((SV *)CvPADLIST (cv)); |
413 | PUSHs ((SV *)CvPADLIST (cv)); |
405 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
414 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
406 | PUSHs ((SV *)cv); |
415 | PUSHs ((SV *)cv); |
407 | |
416 | |
408 | CvDEPTH (cv) = 0; |
417 | CvDEPTH (cv) = 0; |
409 | get_padlist (cv); |
418 | get_padlist (aTHX_ cv); |
410 | } |
419 | } |
411 | } |
420 | } |
412 | } |
421 | } |
413 | |
422 | |
414 | if (top_si->si_type == PERLSI_MAIN) |
423 | if (top_si->si_type == PERLSI_MAIN) |
… | |
… | |
423 | } |
432 | } |
424 | |
433 | |
425 | 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; |
426 | c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
435 | c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
427 | 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; |
428 | 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; |
429 | |
439 | |
430 | #define VAR(name,type)c->name = PL_ ## name; |
440 | #define VAR(name,type)c->name = PL_ ## name; |
431 | # include "state.h" |
441 | # include "state.h" |
432 | #undef VAR |
442 | #undef VAR |
… | |
… | |
440 | */ |
450 | */ |
441 | #if CORO_PREFER_PERL_FUNCTIONS |
451 | #if CORO_PREFER_PERL_FUNCTIONS |
442 | # define coro_init_stacks init_stacks |
452 | # define coro_init_stacks init_stacks |
443 | #else |
453 | #else |
444 | static void |
454 | static void |
445 | coro_init_stacks () |
455 | coro_init_stacks (pTHX) |
446 | { |
456 | { |
447 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
457 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
448 | PL_curstackinfo->si_type = PERLSI_MAIN; |
458 | PL_curstackinfo->si_type = PERLSI_MAIN; |
449 | PL_curstack = PL_curstackinfo->si_stack; |
459 | PL_curstack = PL_curstackinfo->si_stack; |
450 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
460 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
… | |
… | |
484 | |
494 | |
485 | /* |
495 | /* |
486 | * destroy the stacks, the callchain etc... |
496 | * destroy the stacks, the callchain etc... |
487 | */ |
497 | */ |
488 | static void |
498 | static void |
489 | coro_destroy_stacks () |
499 | coro_destroy_stacks (pTHX) |
490 | { |
500 | { |
491 | if (!IN_DESTRUCT) |
501 | if (!IN_DESTRUCT) |
492 | { |
502 | { |
493 | /* restore all saved variables and stuff */ |
503 | /* restore all saved variables and stuff */ |
494 | LEAVE_SCOPE (0); |
504 | LEAVE_SCOPE (0); |
… | |
… | |
530 | } |
540 | } |
531 | |
541 | |
532 | /** coroutine stack handling ************************************************/ |
542 | /** coroutine stack handling ************************************************/ |
533 | |
543 | |
534 | static void |
544 | static void |
535 | setup_coro (struct coro *coro) |
545 | setup_coro (pTHX_ struct coro *coro) |
536 | { |
546 | { |
537 | /* |
547 | /* |
538 | * emulate part of the perl startup here. |
548 | * emulate part of the perl startup here. |
539 | */ |
549 | */ |
540 | |
|
|
541 | coro_init_stacks (); |
550 | coro_init_stacks (aTHX); |
542 | |
551 | |
543 | PL_curcop = &PL_compiling; |
552 | PL_curcop = &PL_compiling; |
544 | PL_in_eval = EVAL_NULL; |
553 | PL_in_eval = EVAL_NULL; |
545 | PL_comppad = 0; |
554 | PL_comppad = 0; |
546 | PL_curpm = 0; |
555 | PL_curpm = 0; |
… | |
… | |
558 | Zero (&myop, 1, LOGOP); |
567 | Zero (&myop, 1, LOGOP); |
559 | myop.op_next = Nullop; |
568 | myop.op_next = Nullop; |
560 | myop.op_flags = OPf_WANT_VOID; |
569 | myop.op_flags = OPf_WANT_VOID; |
561 | |
570 | |
562 | PUSHMARK (SP); |
571 | PUSHMARK (SP); |
563 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
572 | XPUSHs (av_shift (GvAV (PL_defgv))); |
564 | PUTBACK; |
573 | PUTBACK; |
565 | PL_op = (OP *)&myop; |
574 | PL_op = (OP *)&myop; |
566 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
575 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
567 | SPAGAIN; |
576 | SPAGAIN; |
568 | } |
577 | } |
569 | |
578 | |
570 | ENTER; /* necessary e.g. for dounwind */ |
579 | ENTER; /* necessary e.g. for dounwind */ |
571 | } |
580 | } |
572 | |
581 | |
573 | static void |
582 | static void |
574 | free_coro_mortal () |
583 | free_coro_mortal (pTHX) |
575 | { |
584 | { |
576 | if (coro_mortal) |
585 | if (coro_mortal) |
577 | { |
586 | { |
578 | SvREFCNT_dec (coro_mortal); |
587 | SvREFCNT_dec (coro_mortal); |
579 | coro_mortal = 0; |
588 | coro_mortal = 0; |
580 | } |
589 | } |
581 | } |
590 | } |
582 | |
591 | |
583 | /* 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 should be careful, as it could be called at almost any time */ |
|
|
594 | /* during execution of a perl program */ |
584 | static void NOINLINE |
595 | static void NOINLINE |
585 | prepare_cctx (coro_cctx *cctx) |
596 | prepare_cctx (pTHX_ coro_cctx *cctx) |
586 | { |
597 | { |
587 | dSP; |
598 | dSP; |
588 | LOGOP myop; |
599 | LOGOP myop; |
589 | |
600 | |
590 | Zero (&myop, 1, LOGOP); |
601 | Zero (&myop, 1, LOGOP); |
… | |
… | |
599 | PL_op = (OP *)&myop; |
610 | PL_op = (OP *)&myop; |
600 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
611 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
601 | SPAGAIN; |
612 | SPAGAIN; |
602 | } |
613 | } |
603 | |
614 | |
|
|
615 | /* |
|
|
616 | * this is a _very_ stripped down perl interpreter ;) |
|
|
617 | */ |
604 | static void |
618 | static void |
605 | coro_run (void *arg) |
619 | coro_run (void *arg) |
606 | { |
620 | { |
|
|
621 | dTHX; |
|
|
622 | |
607 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
623 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
608 | UNLOCK; |
624 | UNLOCK; |
609 | |
625 | |
610 | /* |
|
|
611 | * this is a _very_ stripped down perl interpreter ;) |
|
|
612 | */ |
|
|
613 | PL_top_env = &PL_start_env; |
626 | PL_top_env = &PL_start_env; |
614 | |
627 | |
615 | /* inject call to cctx_init */ |
628 | /* inject a fake subroutine call to cctx_init */ |
616 | prepare_cctx ((coro_cctx *)arg); |
629 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
617 | |
630 | |
618 | /* 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 */ |
619 | PL_restartop = PL_op; |
632 | PL_restartop = PL_op; |
620 | perl_run (PL_curinterp); |
633 | perl_run (PL_curinterp); |
621 | |
634 | |
622 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
635 | /* |
623 | 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 */ |
624 | } |
644 | } |
625 | |
645 | |
626 | static coro_cctx * |
646 | static coro_cctx * |
627 | cctx_new () |
647 | cctx_new () |
628 | { |
648 | { |
629 | coro_cctx *cctx; |
649 | coro_cctx *cctx; |
|
|
650 | void *stack_start; |
|
|
651 | size_t stack_size; |
630 | |
652 | |
631 | ++cctx_count; |
653 | ++cctx_count; |
632 | |
654 | |
633 | Newz (0, cctx, 1, coro_cctx); |
655 | Newz (0, cctx, 1, coro_cctx); |
634 | |
656 | |
635 | #if HAVE_MMAP |
657 | #if HAVE_MMAP |
636 | |
658 | |
637 | cctx->ssize = ((CORO_STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
659 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
638 | /* mmap supposedly does allocate-on-write for us */ |
660 | /* mmap supposedly does allocate-on-write for us */ |
639 | 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); |
640 | |
662 | |
641 | if (cctx->sptr != (void *)-1) |
663 | if (cctx->sptr != (void *)-1) |
642 | { |
664 | { |
643 | # if CORO_STACKGUARD |
665 | # if CORO_STACKGUARD |
644 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
666 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
645 | # endif |
667 | # endif |
646 | REGISTER_STACK ( |
|
|
647 | cctx, |
|
|
648 | CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr, |
668 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
649 | cctx->ssize + (char *)cctx->sptr |
669 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
650 | ); |
670 | cctx->mapped = 1; |
651 | |
|
|
652 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); |
|
|
653 | } |
671 | } |
654 | else |
672 | else |
655 | #endif |
673 | #endif |
656 | { |
674 | { |
657 | cctx->ssize = -CORO_STACKSIZE * (long)sizeof (long); |
675 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
658 | New (0, cctx->sptr, CORO_STACKSIZE, long); |
676 | New (0, cctx->sptr, coro_stacksize, long); |
659 | |
677 | |
660 | if (!cctx->sptr) |
678 | if (!cctx->sptr) |
661 | { |
679 | { |
662 | perror ("FATAL: unable to allocate stack for coroutine"); |
680 | perror ("FATAL: unable to allocate stack for coroutine"); |
663 | _exit (EXIT_FAILURE); |
681 | _exit (EXIT_FAILURE); |
664 | } |
682 | } |
665 | |
683 | |
666 | REGISTER_STACK ( |
684 | stack_start = cctx->sptr; |
667 | cctx, |
685 | stack_size = cctx->ssize; |
668 | (char *)cctx->sptr, |
686 | } |
669 | (char *)cctx->sptr - cctx->ssize |
|
|
670 | ); |
|
|
671 | |
687 | |
|
|
688 | REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); |
672 | 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); |
673 | } |
|
|
674 | |
690 | |
675 | return cctx; |
691 | return cctx; |
676 | } |
692 | } |
677 | |
693 | |
678 | static void |
694 | static void |
… | |
… | |
686 | #if CORO_USE_VALGRIND |
702 | #if CORO_USE_VALGRIND |
687 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
703 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
688 | #endif |
704 | #endif |
689 | |
705 | |
690 | #if HAVE_MMAP |
706 | #if HAVE_MMAP |
691 | if (cctx->ssize > 0) |
707 | if (cctx->mapped) |
692 | munmap (cctx->sptr, cctx->ssize); |
708 | munmap (cctx->sptr, cctx->ssize); |
693 | else |
709 | else |
694 | #endif |
710 | #endif |
695 | Safefree (cctx->sptr); |
711 | Safefree (cctx->sptr); |
696 | |
712 | |
697 | Safefree (cctx); |
713 | Safefree (cctx); |
698 | } |
714 | } |
699 | |
715 | |
700 | static coro_cctx * |
716 | static coro_cctx * |
701 | cctx_get () |
717 | cctx_get (pTHX) |
702 | { |
718 | { |
703 | coro_cctx *cctx; |
|
|
704 | |
|
|
705 | if (cctx_first) |
719 | while (cctx_first) |
706 | { |
720 | { |
707 | cctx = cctx_first; |
721 | coro_cctx *cctx = cctx_first; |
708 | cctx_first = cctx->next; |
722 | cctx_first = cctx->next; |
709 | --cctx_idle; |
723 | --cctx_idle; |
|
|
724 | |
|
|
725 | if (cctx->ssize >= coro_stacksize) |
|
|
726 | return cctx; |
|
|
727 | |
|
|
728 | cctx_destroy (cctx); |
710 | } |
729 | } |
711 | else |
730 | |
712 | { |
|
|
713 | cctx = cctx_new (); |
|
|
714 | PL_op = PL_op->op_next; |
731 | PL_op = PL_op->op_next; |
715 | } |
|
|
716 | |
|
|
717 | return cctx; |
732 | return cctx_new (); |
718 | } |
733 | } |
719 | |
734 | |
720 | static void |
735 | static void |
721 | cctx_put (coro_cctx *cctx) |
736 | cctx_put (coro_cctx *cctx) |
722 | { |
737 | { |
… | |
… | |
738 | |
753 | |
739 | /** coroutine switching *****************************************************/ |
754 | /** coroutine switching *****************************************************/ |
740 | |
755 | |
741 | /* never call directly, always through the coro_state_transfer global variable */ |
756 | /* never call directly, always through the coro_state_transfer global variable */ |
742 | static void NOINLINE |
757 | static void NOINLINE |
743 | transfer (struct coro *prev, struct coro *next) |
758 | transfer (pTHX_ struct coro *prev, struct coro *next) |
744 | { |
759 | { |
745 | dSTACKLEVEL; |
760 | dSTACKLEVEL; |
746 | |
761 | |
747 | /* sometimes transfer is only called to set idle_sp */ |
762 | /* sometimes transfer is only called to set idle_sp */ |
748 | if (!next) |
763 | if (!next) |
… | |
… | |
781 | if (next->flags & CF_NEW) |
796 | if (next->flags & CF_NEW) |
782 | { |
797 | { |
783 | /* need to start coroutine */ |
798 | /* need to start coroutine */ |
784 | next->flags &= ~CF_NEW; |
799 | next->flags &= ~CF_NEW; |
785 | /* first get rid of the old state */ |
800 | /* first get rid of the old state */ |
786 | save_perl (prev); |
801 | save_perl (aTHX_ prev); |
787 | /* setup coroutine call */ |
802 | /* setup coroutine call */ |
788 | setup_coro (next); |
803 | setup_coro (aTHX_ next); |
789 | /* need a new stack */ |
804 | /* need a new stack */ |
790 | assert (!next->cctx); |
805 | assert (!next->cctx); |
791 | } |
806 | } |
792 | else |
807 | else |
793 | { |
808 | { |
794 | /* coroutine already started */ |
809 | /* coroutine already started */ |
795 | save_perl (prev); |
810 | save_perl (aTHX_ prev); |
796 | load_perl (next); |
811 | load_perl (aTHX_ next); |
797 | } |
812 | } |
798 | |
813 | |
799 | prev__cctx = prev->cctx; |
814 | prev__cctx = prev->cctx; |
800 | |
815 | |
801 | /* possibly "free" the cctx */ |
816 | /* possibly "free" the cctx */ |
… | |
… | |
810 | prev__cctx->inuse = 0; |
825 | prev__cctx->inuse = 0; |
811 | } |
826 | } |
812 | |
827 | |
813 | if (!next->cctx) |
828 | if (!next->cctx) |
814 | { |
829 | { |
815 | next->cctx = cctx_get (); |
830 | next->cctx = cctx_get (aTHX); |
816 | assert (!next->cctx->inuse); |
831 | assert (!next->cctx->inuse); |
817 | next->cctx->inuse = 1; |
832 | next->cctx->inuse = 1; |
818 | } |
833 | } |
819 | |
834 | |
820 | if (prev__cctx != next->cctx) |
835 | if (prev__cctx != next->cctx) |
… | |
… | |
822 | prev__cctx->top_env = PL_top_env; |
837 | prev__cctx->top_env = PL_top_env; |
823 | PL_top_env = next->cctx->top_env; |
838 | PL_top_env = next->cctx->top_env; |
824 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
839 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
825 | } |
840 | } |
826 | |
841 | |
827 | free_coro_mortal (); |
842 | free_coro_mortal (aTHX); |
828 | UNLOCK; |
843 | UNLOCK; |
829 | } |
844 | } |
830 | } |
845 | } |
831 | |
846 | |
832 | struct transfer_args |
847 | struct transfer_args |
833 | { |
848 | { |
834 | struct coro *prev, *next; |
849 | struct coro *prev, *next; |
835 | }; |
850 | }; |
836 | |
851 | |
837 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
852 | #define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) |
838 | |
853 | |
839 | /** high level stuff ********************************************************/ |
854 | /** high level stuff ********************************************************/ |
840 | |
855 | |
841 | static int |
856 | static int |
842 | coro_state_destroy (struct coro *coro) |
857 | coro_state_destroy (pTHX_ struct coro *coro) |
843 | { |
858 | { |
844 | if (coro->flags & CF_DESTROYED) |
859 | if (coro->flags & CF_DESTROYED) |
845 | return 0; |
860 | return 0; |
846 | |
861 | |
847 | coro->flags |= CF_DESTROYED; |
862 | coro->flags |= CF_DESTROYED; |
… | |
… | |
862 | struct coro temp; |
877 | struct coro temp; |
863 | |
878 | |
864 | assert (!(coro->flags & CF_RUNNING)); |
879 | assert (!(coro->flags & CF_RUNNING)); |
865 | |
880 | |
866 | Zero (&temp, 1, struct coro); |
881 | Zero (&temp, 1, struct coro); |
867 | temp.save = CORO_SAVE_ALL; |
882 | temp.save = CORO_SAVE_DEF; |
868 | |
883 | |
869 | if (coro->flags & CF_RUNNING) |
884 | if (coro->flags & CF_RUNNING) |
870 | croak ("FATAL: tried to destroy currently running coroutine"); |
885 | croak ("FATAL: tried to destroy currently running coroutine"); |
871 | |
886 | |
872 | save_perl (&temp); |
887 | save_perl (aTHX_ &temp); |
873 | load_perl (coro); |
888 | load_perl (aTHX_ coro); |
874 | |
889 | |
875 | coro_destroy_stacks (); |
890 | coro_destroy_stacks (aTHX); |
876 | |
891 | |
877 | load_perl (&temp); /* this will get rid of defsv etc.. */ |
892 | load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ |
878 | |
893 | |
879 | coro->mainstack = 0; |
894 | coro->mainstack = 0; |
880 | } |
895 | } |
881 | |
896 | |
882 | cctx_destroy (coro->cctx); |
897 | cctx_destroy (coro->cctx); |
… | |
… | |
891 | struct coro *coro = (struct coro *)mg->mg_ptr; |
906 | struct coro *coro = (struct coro *)mg->mg_ptr; |
892 | mg->mg_ptr = 0; |
907 | mg->mg_ptr = 0; |
893 | |
908 | |
894 | if (--coro->refcnt < 0) |
909 | if (--coro->refcnt < 0) |
895 | { |
910 | { |
896 | coro_state_destroy (coro); |
911 | coro_state_destroy (aTHX_ coro); |
897 | Safefree (coro); |
912 | Safefree (coro); |
898 | } |
913 | } |
899 | |
914 | |
900 | return 0; |
915 | return 0; |
901 | } |
916 | } |
… | |
… | |
920 | # define MGf_DUP 0 |
935 | # define MGf_DUP 0 |
921 | #endif |
936 | #endif |
922 | }; |
937 | }; |
923 | |
938 | |
924 | static struct coro * |
939 | static struct coro * |
925 | SvSTATE (SV *coro) |
940 | SvSTATE_ (pTHX_ SV *coro) |
926 | { |
941 | { |
927 | HV *stash; |
942 | HV *stash; |
928 | MAGIC *mg; |
943 | MAGIC *mg; |
929 | |
944 | |
930 | if (SvROK (coro)) |
945 | if (SvROK (coro)) |
… | |
… | |
936 | /* very slow, but rare, check */ |
951 | /* very slow, but rare, check */ |
937 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
952 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
938 | croak ("Coro::State object required"); |
953 | croak ("Coro::State object required"); |
939 | } |
954 | } |
940 | |
955 | |
941 | mg = SvMAGIC (coro); |
956 | mg = CORO_MAGIC (coro); |
942 | assert (mg->mg_type == PERL_MAGIC_ext); |
|
|
943 | return (struct coro *)mg->mg_ptr; |
957 | return (struct coro *)mg->mg_ptr; |
944 | } |
958 | } |
945 | |
959 | |
|
|
960 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
961 | |
946 | static void |
962 | static void |
947 | 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) |
948 | { |
964 | { |
949 | ta->prev = SvSTATE (prev_sv); |
965 | ta->prev = SvSTATE (prev_sv); |
950 | ta->next = SvSTATE (next_sv); |
966 | ta->next = SvSTATE (next_sv); |
951 | } |
967 | } |
952 | |
968 | |
953 | static void |
969 | static void |
954 | api_transfer (SV *prev_sv, SV *next_sv) |
970 | api_transfer (SV *prev_sv, SV *next_sv) |
955 | { |
971 | { |
|
|
972 | dTHX; |
956 | struct transfer_args ta; |
973 | struct transfer_args ta; |
957 | |
974 | |
958 | prepare_transfer (&ta, prev_sv, next_sv); |
975 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
959 | TRANSFER (ta); |
976 | TRANSFER (ta); |
960 | } |
977 | } |
961 | |
978 | |
962 | static int |
979 | static int |
963 | api_save (SV *coro_sv, int new_save) |
980 | api_save (SV *coro_sv, int new_save) |
964 | { |
981 | { |
|
|
982 | dTHX; |
965 | struct coro *coro = SvSTATE (coro_sv); |
983 | struct coro *coro = SvSTATE (coro_sv); |
966 | int old_save = coro->save; |
984 | int old_save = coro->save; |
967 | |
985 | |
968 | if (new_save >= 0) |
986 | if (new_save >= 0) |
969 | coro->save = new_save; |
987 | coro->save = new_save; |
… | |
… | |
972 | } |
990 | } |
973 | |
991 | |
974 | /** Coro ********************************************************************/ |
992 | /** Coro ********************************************************************/ |
975 | |
993 | |
976 | static void |
994 | static void |
977 | coro_enq (SV *coro_sv) |
995 | coro_enq (pTHX_ SV *coro_sv) |
978 | { |
996 | { |
979 | 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); |
980 | } |
998 | } |
981 | |
999 | |
982 | static SV * |
1000 | static SV * |
983 | coro_deq (int min_prio) |
1001 | coro_deq (pTHX_ int min_prio) |
984 | { |
1002 | { |
985 | int prio = PRIO_MAX - PRIO_MIN; |
1003 | int prio = PRIO_MAX - PRIO_MIN; |
986 | |
1004 | |
987 | min_prio -= PRIO_MIN; |
1005 | min_prio -= PRIO_MIN; |
988 | if (min_prio < 0) |
1006 | if (min_prio < 0) |
… | |
… | |
996 | } |
1014 | } |
997 | |
1015 | |
998 | static int |
1016 | static int |
999 | api_ready (SV *coro_sv) |
1017 | api_ready (SV *coro_sv) |
1000 | { |
1018 | { |
|
|
1019 | dTHX; |
1001 | struct coro *coro; |
1020 | struct coro *coro; |
1002 | |
1021 | |
1003 | if (SvROK (coro_sv)) |
1022 | if (SvROK (coro_sv)) |
1004 | coro_sv = SvRV (coro_sv); |
1023 | coro_sv = SvRV (coro_sv); |
1005 | |
1024 | |
… | |
… | |
1009 | return 0; |
1028 | return 0; |
1010 | |
1029 | |
1011 | coro->flags |= CF_READY; |
1030 | coro->flags |= CF_READY; |
1012 | |
1031 | |
1013 | LOCK; |
1032 | LOCK; |
1014 | coro_enq (SvREFCNT_inc (coro_sv)); |
1033 | coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); |
1015 | ++coro_nready; |
1034 | ++coro_nready; |
1016 | UNLOCK; |
1035 | UNLOCK; |
1017 | |
1036 | |
1018 | return 1; |
1037 | return 1; |
1019 | } |
1038 | } |
1020 | |
1039 | |
1021 | static int |
1040 | static int |
1022 | api_is_ready (SV *coro_sv) |
1041 | api_is_ready (SV *coro_sv) |
1023 | { |
1042 | { |
|
|
1043 | dTHX; |
1024 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1044 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1025 | } |
1045 | } |
1026 | |
1046 | |
1027 | static void |
1047 | static void |
1028 | prepare_schedule (struct transfer_args *ta) |
1048 | prepare_schedule (pTHX_ struct transfer_args *ta) |
1029 | { |
1049 | { |
1030 | SV *prev_sv, *next_sv; |
1050 | SV *prev_sv, *next_sv; |
1031 | |
1051 | |
1032 | for (;;) |
1052 | for (;;) |
1033 | { |
1053 | { |
1034 | LOCK; |
1054 | LOCK; |
1035 | next_sv = coro_deq (PRIO_MIN); |
1055 | next_sv = coro_deq (aTHX_ PRIO_MIN); |
1036 | |
1056 | |
1037 | /* nothing to schedule: call the idle handler */ |
1057 | /* nothing to schedule: call the idle handler */ |
1038 | if (!next_sv) |
1058 | if (!next_sv) |
1039 | { |
1059 | { |
1040 | dSP; |
1060 | dSP; |
… | |
… | |
1075 | |
1095 | |
1076 | assert (ta->next->flags & CF_READY); |
1096 | assert (ta->next->flags & CF_READY); |
1077 | ta->next->flags &= ~CF_READY; |
1097 | ta->next->flags &= ~CF_READY; |
1078 | |
1098 | |
1079 | LOCK; |
1099 | LOCK; |
1080 | free_coro_mortal (); |
1100 | free_coro_mortal (aTHX); |
1081 | coro_mortal = prev_sv; |
1101 | coro_mortal = prev_sv; |
1082 | UNLOCK; |
1102 | UNLOCK; |
1083 | } |
1103 | } |
1084 | |
1104 | |
1085 | static void |
1105 | static void |
1086 | prepare_cede (struct transfer_args *ta) |
1106 | prepare_cede (pTHX_ struct transfer_args *ta) |
1087 | { |
1107 | { |
1088 | api_ready (coro_current); |
1108 | api_ready (coro_current); |
1089 | prepare_schedule (ta); |
1109 | prepare_schedule (aTHX_ ta); |
1090 | } |
1110 | } |
1091 | |
1111 | |
1092 | static int |
1112 | static int |
1093 | prepare_cede_notself (struct transfer_args *ta) |
1113 | prepare_cede_notself (pTHX_ struct transfer_args *ta) |
1094 | { |
1114 | { |
1095 | if (coro_nready) |
1115 | if (coro_nready) |
1096 | { |
1116 | { |
1097 | SV *prev = SvRV (coro_current); |
1117 | SV *prev = SvRV (coro_current); |
1098 | prepare_schedule (ta); |
1118 | prepare_schedule (aTHX_ ta); |
1099 | api_ready (prev); |
1119 | api_ready (prev); |
1100 | return 1; |
1120 | return 1; |
1101 | } |
1121 | } |
1102 | else |
1122 | else |
1103 | return 0; |
1123 | return 0; |
1104 | } |
1124 | } |
1105 | |
1125 | |
1106 | static void |
1126 | static void |
1107 | api_schedule (void) |
1127 | api_schedule (void) |
1108 | { |
1128 | { |
|
|
1129 | dTHX; |
1109 | struct transfer_args ta; |
1130 | struct transfer_args ta; |
1110 | |
1131 | |
1111 | prepare_schedule (&ta); |
1132 | prepare_schedule (aTHX_ &ta); |
1112 | TRANSFER (ta); |
1133 | TRANSFER (ta); |
1113 | } |
1134 | } |
1114 | |
1135 | |
1115 | static int |
1136 | static int |
1116 | api_cede (void) |
1137 | api_cede (void) |
1117 | { |
1138 | { |
|
|
1139 | dTHX; |
1118 | struct transfer_args ta; |
1140 | struct transfer_args ta; |
1119 | |
1141 | |
1120 | prepare_cede (&ta); |
1142 | prepare_cede (aTHX_ &ta); |
1121 | |
1143 | |
1122 | if (ta.prev != ta.next) |
1144 | if (ta.prev != ta.next) |
1123 | { |
1145 | { |
1124 | TRANSFER (ta); |
1146 | TRANSFER (ta); |
1125 | return 1; |
1147 | return 1; |
… | |
… | |
1129 | } |
1151 | } |
1130 | |
1152 | |
1131 | static int |
1153 | static int |
1132 | api_cede_notself (void) |
1154 | api_cede_notself (void) |
1133 | { |
1155 | { |
|
|
1156 | dTHX; |
1134 | struct transfer_args ta; |
1157 | struct transfer_args ta; |
1135 | |
1158 | |
1136 | if (prepare_cede_notself (&ta)) |
1159 | if (prepare_cede_notself (aTHX_ &ta)) |
1137 | { |
1160 | { |
1138 | TRANSFER (ta); |
1161 | TRANSFER (ta); |
1139 | return 1; |
1162 | return 1; |
1140 | } |
1163 | } |
1141 | else |
1164 | else |
… | |
… | |
1157 | |
1180 | |
1158 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1181 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1159 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1182 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1160 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
1183 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
1161 | 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)); |
1162 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
1187 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
1163 | |
1188 | |
1164 | 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; |
1165 | |
1194 | |
1166 | coroapi.ver = CORO_API_VERSION; |
1195 | coroapi.ver = CORO_API_VERSION; |
1167 | coroapi.transfer = api_transfer; |
1196 | coroapi.transfer = api_transfer; |
1168 | |
1197 | |
1169 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1198 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
… | |
… | |
1177 | HV *hv; |
1206 | HV *hv; |
1178 | int i; |
1207 | int i; |
1179 | |
1208 | |
1180 | Newz (0, coro, 1, struct coro); |
1209 | Newz (0, coro, 1, struct coro); |
1181 | coro->args = newAV (); |
1210 | coro->args = newAV (); |
1182 | coro->save = CORO_SAVE_ALL; |
1211 | coro->save = CORO_SAVE_DEF; |
1183 | coro->flags = CF_NEW; |
1212 | coro->flags = CF_NEW; |
1184 | |
1213 | |
1185 | hv = newHV (); |
1214 | hv = newHV (); |
1186 | 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; |
1187 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1216 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
… | |
… | |
1194 | |
1223 | |
1195 | int |
1224 | int |
1196 | save (SV *coro, int new_save = -1) |
1225 | save (SV *coro, int new_save = -1) |
1197 | CODE: |
1226 | CODE: |
1198 | 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 | } |
1199 | OUTPUT: |
1239 | OUTPUT: |
1200 | RETVAL |
1240 | RETVAL |
1201 | |
1241 | |
1202 | void |
1242 | void |
1203 | _set_stacklevel (...) |
1243 | _set_stacklevel (...) |
… | |
… | |
1219 | |
1259 | |
1220 | case 1: |
1260 | case 1: |
1221 | if (items != 2) |
1261 | if (items != 2) |
1222 | 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); |
1223 | |
1263 | |
1224 | prepare_transfer (&ta, ST (0), ST (1)); |
1264 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1225 | break; |
1265 | break; |
1226 | |
1266 | |
1227 | case 2: |
1267 | case 2: |
1228 | prepare_schedule (&ta); |
1268 | prepare_schedule (aTHX_ &ta); |
1229 | break; |
1269 | break; |
1230 | |
1270 | |
1231 | case 3: |
1271 | case 3: |
1232 | prepare_cede (&ta); |
1272 | prepare_cede (aTHX_ &ta); |
1233 | break; |
1273 | break; |
1234 | |
1274 | |
1235 | case 4: |
1275 | case 4: |
1236 | if (!prepare_cede_notself (&ta)) |
1276 | if (!prepare_cede_notself (aTHX_ &ta)) |
1237 | XSRETURN_EMPTY; |
1277 | XSRETURN_EMPTY; |
1238 | |
1278 | |
1239 | break; |
1279 | break; |
1240 | } |
1280 | } |
1241 | |
1281 | |
… | |
… | |
1247 | } |
1287 | } |
1248 | |
1288 | |
1249 | bool |
1289 | bool |
1250 | _destroy (SV *coro_sv) |
1290 | _destroy (SV *coro_sv) |
1251 | CODE: |
1291 | CODE: |
1252 | RETVAL = coro_state_destroy (SvSTATE (coro_sv)); |
1292 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
1253 | OUTPUT: |
1293 | OUTPUT: |
1254 | RETVAL |
1294 | RETVAL |
1255 | |
1295 | |
1256 | void |
1296 | void |
1257 | _exit (code) |
1297 | _exit (code) |
1258 | int code |
1298 | int code |
1259 | PROTOTYPE: $ |
1299 | PROTOTYPE: $ |
1260 | CODE: |
1300 | CODE: |
1261 | _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 |
1262 | |
1311 | |
1263 | int |
1312 | int |
1264 | cctx_count () |
1313 | cctx_count () |
1265 | CODE: |
1314 | CODE: |
1266 | RETVAL = cctx_count; |
1315 | RETVAL = cctx_count; |