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 */ |
131 | static HV *coro_state_stash, *coro_stash; |
134 | static HV *coro_state_stash, *coro_stash; |
132 | static SV *coro_mortal; /* will be freed after next transfer */ |
135 | static SV *coro_mortal; /* will be freed after next transfer */ |
133 | |
136 | |
… | |
… | |
138 | typedef struct coro_cctx { |
141 | typedef struct coro_cctx { |
139 | struct coro_cctx *next; |
142 | struct coro_cctx *next; |
140 | |
143 | |
141 | /* the stack */ |
144 | /* the stack */ |
142 | void *sptr; |
145 | void *sptr; |
143 | ssize_t ssize; /* positive == mmap, otherwise malloc */ |
146 | size_t ssize; |
144 | |
147 | |
145 | /* cpu state */ |
148 | /* cpu state */ |
146 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
149 | 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 */ |
150 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
148 | JMPENV *top_env; |
151 | JMPENV *top_env; |
149 | coro_context cctx; |
152 | coro_context cctx; |
150 | |
153 | |
151 | int inuse; |
|
|
152 | |
|
|
153 | #if CORO_USE_VALGRIND |
154 | #if CORO_USE_VALGRIND |
154 | int valgrind_id; |
155 | int valgrind_id; |
155 | #endif |
156 | #endif |
|
|
157 | char inuse, mapped; |
156 | } coro_cctx; |
158 | } coro_cctx; |
157 | |
159 | |
158 | enum { |
160 | enum { |
159 | CF_RUNNING = 0x0001, /* coroutine is running */ |
161 | CF_RUNNING = 0x0001, /* coroutine is running */ |
160 | CF_READY = 0x0002, /* coroutine is ready */ |
162 | CF_READY = 0x0002, /* coroutine is ready */ |
… | |
… | |
206 | static int coro_nready; |
208 | static int coro_nready; |
207 | |
209 | |
208 | /** lowlevel stuff **********************************************************/ |
210 | /** lowlevel stuff **********************************************************/ |
209 | |
211 | |
210 | static AV * |
212 | static AV * |
211 | coro_clone_padlist (CV *cv) |
213 | coro_clone_padlist (pTHX_ CV *cv) |
212 | { |
214 | { |
213 | AV *padlist = CvPADLIST (cv); |
215 | AV *padlist = CvPADLIST (cv); |
214 | AV *newpadlist, *newpad; |
216 | AV *newpadlist, *newpad; |
215 | |
217 | |
216 | newpadlist = newAV (); |
218 | newpadlist = newAV (); |
… | |
… | |
228 | |
230 | |
229 | return newpadlist; |
231 | return newpadlist; |
230 | } |
232 | } |
231 | |
233 | |
232 | static void |
234 | static void |
233 | free_padlist (AV *padlist) |
235 | free_padlist (pTHX_ AV *padlist) |
234 | { |
236 | { |
235 | /* may be during global destruction */ |
237 | /* may be during global destruction */ |
236 | if (SvREFCNT (padlist)) |
238 | if (SvREFCNT (padlist)) |
237 | { |
239 | { |
238 | I32 i = AvFILLp (padlist); |
240 | I32 i = AvFILLp (padlist); |
… | |
… | |
259 | AV *padlist; |
261 | AV *padlist; |
260 | AV *av = (AV *)mg->mg_obj; |
262 | AV *av = (AV *)mg->mg_obj; |
261 | |
263 | |
262 | /* casting is fun. */ |
264 | /* casting is fun. */ |
263 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
265 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
264 | free_padlist (padlist); |
266 | free_padlist (aTHX_ padlist); |
265 | |
267 | |
266 | SvREFCNT_dec (av); |
268 | SvREFCNT_dec (av); |
267 | |
269 | |
268 | return 0; |
270 | return 0; |
269 | } |
271 | } |
… | |
… | |
279 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
281 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
280 | : 0 |
282 | : 0 |
281 | |
283 | |
282 | /* the next two functions merely cache the padlists */ |
284 | /* the next two functions merely cache the padlists */ |
283 | static void |
285 | static void |
284 | get_padlist (CV *cv) |
286 | get_padlist (pTHX_ CV *cv) |
285 | { |
287 | { |
286 | MAGIC *mg = CORO_MAGIC (cv); |
288 | MAGIC *mg = CORO_MAGIC (cv); |
287 | AV *av; |
289 | AV *av; |
288 | |
290 | |
289 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
291 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
… | |
… | |
295 | CV *cp = Perl_cv_clone (cv); |
297 | CV *cp = Perl_cv_clone (cv); |
296 | CvPADLIST (cv) = CvPADLIST (cp); |
298 | CvPADLIST (cv) = CvPADLIST (cp); |
297 | CvPADLIST (cp) = 0; |
299 | CvPADLIST (cp) = 0; |
298 | SvREFCNT_dec (cp); |
300 | SvREFCNT_dec (cp); |
299 | #else |
301 | #else |
300 | CvPADLIST (cv) = coro_clone_padlist (cv); |
302 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
301 | #endif |
303 | #endif |
302 | } |
304 | } |
303 | } |
305 | } |
304 | |
306 | |
305 | static void |
307 | static void |
306 | put_padlist (CV *cv) |
308 | put_padlist (pTHX_ CV *cv) |
307 | { |
309 | { |
308 | MAGIC *mg = CORO_MAGIC (cv); |
310 | MAGIC *mg = CORO_MAGIC (cv); |
309 | AV *av; |
311 | AV *av; |
310 | |
312 | |
311 | if (!mg) |
313 | if (!mg) |
… | |
… | |
330 | #define SE } while (0) |
332 | #define SE } while (0) |
331 | |
333 | |
332 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
334 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
333 | |
335 | |
334 | static void |
336 | static void |
335 | load_perl (Coro__State c) |
337 | load_perl (pTHX_ Coro__State c) |
336 | { |
338 | { |
337 | #define VAR(name,type) PL_ ## name = c->name; |
339 | #define VAR(name,type) PL_ ## name = c->name; |
338 | # include "state.h" |
340 | # include "state.h" |
339 | #undef VAR |
341 | #undef VAR |
340 | |
342 | |
… | |
… | |
358 | CV *cv; |
360 | CV *cv; |
359 | |
361 | |
360 | /* now do the ugly restore mess */ |
362 | /* now do the ugly restore mess */ |
361 | while ((cv = (CV *)POPs)) |
363 | while ((cv = (CV *)POPs)) |
362 | { |
364 | { |
363 | put_padlist (cv); /* mark this padlist as available */ |
365 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
364 | CvDEPTH (cv) = PTR2IV (POPs); |
366 | CvDEPTH (cv) = PTR2IV (POPs); |
365 | CvPADLIST (cv) = (AV *)POPs; |
367 | CvPADLIST (cv) = (AV *)POPs; |
366 | } |
368 | } |
367 | |
369 | |
368 | PUTBACK; |
370 | PUTBACK; |
369 | } |
371 | } |
370 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
372 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
371 | } |
373 | } |
372 | |
374 | |
373 | static void |
375 | static void |
374 | save_perl (Coro__State c) |
376 | save_perl (pTHX_ Coro__State c) |
375 | { |
377 | { |
376 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
378 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
377 | { |
379 | { |
378 | dSP; |
380 | dSP; |
379 | I32 cxix = cxstack_ix; |
381 | I32 cxix = cxstack_ix; |
… | |
… | |
404 | PUSHs ((SV *)CvPADLIST (cv)); |
406 | PUSHs ((SV *)CvPADLIST (cv)); |
405 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
407 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
406 | PUSHs ((SV *)cv); |
408 | PUSHs ((SV *)cv); |
407 | |
409 | |
408 | CvDEPTH (cv) = 0; |
410 | CvDEPTH (cv) = 0; |
409 | get_padlist (cv); |
411 | get_padlist (aTHX_ cv); |
410 | } |
412 | } |
411 | } |
413 | } |
412 | } |
414 | } |
413 | |
415 | |
414 | if (top_si->si_type == PERLSI_MAIN) |
416 | if (top_si->si_type == PERLSI_MAIN) |
… | |
… | |
440 | */ |
442 | */ |
441 | #if CORO_PREFER_PERL_FUNCTIONS |
443 | #if CORO_PREFER_PERL_FUNCTIONS |
442 | # define coro_init_stacks init_stacks |
444 | # define coro_init_stacks init_stacks |
443 | #else |
445 | #else |
444 | static void |
446 | static void |
445 | coro_init_stacks () |
447 | coro_init_stacks (pTHX) |
446 | { |
448 | { |
447 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
449 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
448 | PL_curstackinfo->si_type = PERLSI_MAIN; |
450 | PL_curstackinfo->si_type = PERLSI_MAIN; |
449 | PL_curstack = PL_curstackinfo->si_stack; |
451 | PL_curstack = PL_curstackinfo->si_stack; |
450 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
452 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
… | |
… | |
484 | |
486 | |
485 | /* |
487 | /* |
486 | * destroy the stacks, the callchain etc... |
488 | * destroy the stacks, the callchain etc... |
487 | */ |
489 | */ |
488 | static void |
490 | static void |
489 | coro_destroy_stacks () |
491 | coro_destroy_stacks (pTHX) |
490 | { |
492 | { |
491 | if (!IN_DESTRUCT) |
493 | if (!IN_DESTRUCT) |
492 | { |
494 | { |
493 | /* restore all saved variables and stuff */ |
495 | /* restore all saved variables and stuff */ |
494 | LEAVE_SCOPE (0); |
496 | LEAVE_SCOPE (0); |
… | |
… | |
530 | } |
532 | } |
531 | |
533 | |
532 | /** coroutine stack handling ************************************************/ |
534 | /** coroutine stack handling ************************************************/ |
533 | |
535 | |
534 | static void |
536 | static void |
535 | setup_coro (struct coro *coro) |
537 | setup_coro (pTHX_ struct coro *coro) |
536 | { |
538 | { |
537 | /* |
539 | /* |
538 | * emulate part of the perl startup here. |
540 | * emulate part of the perl startup here. |
539 | */ |
541 | */ |
540 | |
|
|
541 | coro_init_stacks (); |
542 | coro_init_stacks (aTHX); |
542 | |
543 | |
543 | PL_curcop = &PL_compiling; |
544 | PL_curcop = &PL_compiling; |
544 | PL_in_eval = EVAL_NULL; |
545 | PL_in_eval = EVAL_NULL; |
545 | PL_comppad = 0; |
546 | PL_comppad = 0; |
546 | PL_curpm = 0; |
547 | PL_curpm = 0; |
… | |
… | |
569 | |
570 | |
570 | ENTER; /* necessary e.g. for dounwind */ |
571 | ENTER; /* necessary e.g. for dounwind */ |
571 | } |
572 | } |
572 | |
573 | |
573 | static void |
574 | static void |
574 | free_coro_mortal () |
575 | free_coro_mortal (pTHX) |
575 | { |
576 | { |
576 | if (coro_mortal) |
577 | if (coro_mortal) |
577 | { |
578 | { |
578 | SvREFCNT_dec (coro_mortal); |
579 | SvREFCNT_dec (coro_mortal); |
579 | coro_mortal = 0; |
580 | coro_mortal = 0; |
580 | } |
581 | } |
581 | } |
582 | } |
582 | |
583 | |
583 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
584 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
584 | static void NOINLINE |
585 | static void NOINLINE |
585 | prepare_cctx (coro_cctx *cctx) |
586 | prepare_cctx (pTHX_ coro_cctx *cctx) |
586 | { |
587 | { |
587 | dSP; |
588 | dSP; |
588 | LOGOP myop; |
589 | LOGOP myop; |
589 | |
590 | |
590 | Zero (&myop, 1, LOGOP); |
591 | Zero (&myop, 1, LOGOP); |
… | |
… | |
602 | } |
603 | } |
603 | |
604 | |
604 | static void |
605 | static void |
605 | coro_run (void *arg) |
606 | coro_run (void *arg) |
606 | { |
607 | { |
|
|
608 | dTHX; |
|
|
609 | |
607 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
610 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
608 | UNLOCK; |
611 | UNLOCK; |
609 | |
612 | |
610 | /* |
613 | /* |
611 | * this is a _very_ stripped down perl interpreter ;) |
614 | * this is a _very_ stripped down perl interpreter ;) |
612 | */ |
615 | */ |
613 | PL_top_env = &PL_start_env; |
616 | PL_top_env = &PL_start_env; |
614 | |
617 | |
615 | /* inject call to cctx_init */ |
618 | /* inject call to cctx_init */ |
616 | prepare_cctx ((coro_cctx *)arg); |
619 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
617 | |
620 | |
618 | /* somebody will hit me for both perl_run and PL_restartop */ |
621 | /* somebody will hit me for both perl_run and PL_restartop */ |
619 | PL_restartop = PL_op; |
622 | PL_restartop = PL_op; |
620 | perl_run (PL_curinterp); |
623 | perl_run (PL_curinterp); |
621 | |
624 | |
… | |
… | |
625 | |
628 | |
626 | static coro_cctx * |
629 | static coro_cctx * |
627 | cctx_new () |
630 | cctx_new () |
628 | { |
631 | { |
629 | coro_cctx *cctx; |
632 | coro_cctx *cctx; |
|
|
633 | void *stack_start; |
|
|
634 | size_t stack_size; |
630 | |
635 | |
631 | ++cctx_count; |
636 | ++cctx_count; |
632 | |
637 | |
633 | Newz (0, cctx, 1, coro_cctx); |
638 | Newz (0, cctx, 1, coro_cctx); |
634 | |
639 | |
635 | #if HAVE_MMAP |
640 | #if HAVE_MMAP |
636 | |
641 | |
637 | cctx->ssize = ((CORO_STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
642 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
638 | /* mmap supposedly does allocate-on-write for us */ |
643 | /* 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); |
644 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
640 | |
645 | |
641 | if (cctx->sptr != (void *)-1) |
646 | if (cctx->sptr != (void *)-1) |
642 | { |
647 | { |
643 | # if CORO_STACKGUARD |
648 | # if CORO_STACKGUARD |
644 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
649 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
645 | # endif |
650 | # endif |
646 | REGISTER_STACK ( |
|
|
647 | cctx, |
|
|
648 | CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr, |
651 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
649 | cctx->ssize + (char *)cctx->sptr |
652 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
650 | ); |
653 | cctx->mapped = 1; |
651 | |
|
|
652 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); |
|
|
653 | } |
654 | } |
654 | else |
655 | else |
655 | #endif |
656 | #endif |
656 | { |
657 | { |
657 | cctx->ssize = -CORO_STACKSIZE * (long)sizeof (long); |
658 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
658 | New (0, cctx->sptr, CORO_STACKSIZE, long); |
659 | New (0, cctx->sptr, coro_stacksize, long); |
659 | |
660 | |
660 | if (!cctx->sptr) |
661 | if (!cctx->sptr) |
661 | { |
662 | { |
662 | perror ("FATAL: unable to allocate stack for coroutine"); |
663 | perror ("FATAL: unable to allocate stack for coroutine"); |
663 | _exit (EXIT_FAILURE); |
664 | _exit (EXIT_FAILURE); |
664 | } |
665 | } |
665 | |
666 | |
666 | REGISTER_STACK ( |
667 | stack_start = cctx->sptr; |
667 | cctx, |
668 | stack_size = cctx->ssize; |
668 | (char *)cctx->sptr, |
669 | } |
669 | (char *)cctx->sptr - cctx->ssize |
|
|
670 | ); |
|
|
671 | |
670 | |
|
|
671 | REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); |
672 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, -cctx->ssize); |
672 | coro_create (&cctx->cctx, coro_run, (void *)cctx, stack_start, stack_size); |
673 | } |
|
|
674 | |
673 | |
675 | return cctx; |
674 | return cctx; |
676 | } |
675 | } |
677 | |
676 | |
678 | static void |
677 | static void |
… | |
… | |
686 | #if CORO_USE_VALGRIND |
685 | #if CORO_USE_VALGRIND |
687 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
686 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
688 | #endif |
687 | #endif |
689 | |
688 | |
690 | #if HAVE_MMAP |
689 | #if HAVE_MMAP |
691 | if (cctx->ssize > 0) |
690 | if (cctx->mapped) |
692 | munmap (cctx->sptr, cctx->ssize); |
691 | munmap (cctx->sptr, cctx->ssize); |
693 | else |
692 | else |
694 | #endif |
693 | #endif |
695 | Safefree (cctx->sptr); |
694 | Safefree (cctx->sptr); |
696 | |
695 | |
697 | Safefree (cctx); |
696 | Safefree (cctx); |
698 | } |
697 | } |
699 | |
698 | |
700 | static coro_cctx * |
699 | static coro_cctx * |
701 | cctx_get () |
700 | cctx_get (pTHX) |
702 | { |
701 | { |
703 | coro_cctx *cctx; |
|
|
704 | |
|
|
705 | if (cctx_first) |
702 | while (cctx_first) |
706 | { |
703 | { |
707 | cctx = cctx_first; |
704 | coro_cctx *cctx = cctx_first; |
708 | cctx_first = cctx->next; |
705 | cctx_first = cctx->next; |
709 | --cctx_idle; |
706 | --cctx_idle; |
|
|
707 | |
|
|
708 | if (cctx->ssize >= coro_stacksize) |
|
|
709 | return cctx; |
|
|
710 | |
|
|
711 | cctx_destroy (cctx); |
710 | } |
712 | } |
711 | else |
713 | |
712 | { |
|
|
713 | cctx = cctx_new (); |
|
|
714 | PL_op = PL_op->op_next; |
714 | PL_op = PL_op->op_next; |
715 | } |
|
|
716 | |
|
|
717 | return cctx; |
715 | return cctx_new (); |
718 | } |
716 | } |
719 | |
717 | |
720 | static void |
718 | static void |
721 | cctx_put (coro_cctx *cctx) |
719 | cctx_put (coro_cctx *cctx) |
722 | { |
720 | { |
… | |
… | |
738 | |
736 | |
739 | /** coroutine switching *****************************************************/ |
737 | /** coroutine switching *****************************************************/ |
740 | |
738 | |
741 | /* never call directly, always through the coro_state_transfer global variable */ |
739 | /* never call directly, always through the coro_state_transfer global variable */ |
742 | static void NOINLINE |
740 | static void NOINLINE |
743 | transfer (struct coro *prev, struct coro *next) |
741 | transfer (pTHX_ struct coro *prev, struct coro *next) |
744 | { |
742 | { |
745 | dSTACKLEVEL; |
743 | dSTACKLEVEL; |
746 | |
744 | |
747 | /* sometimes transfer is only called to set idle_sp */ |
745 | /* sometimes transfer is only called to set idle_sp */ |
748 | if (!next) |
746 | if (!next) |
… | |
… | |
781 | if (next->flags & CF_NEW) |
779 | if (next->flags & CF_NEW) |
782 | { |
780 | { |
783 | /* need to start coroutine */ |
781 | /* need to start coroutine */ |
784 | next->flags &= ~CF_NEW; |
782 | next->flags &= ~CF_NEW; |
785 | /* first get rid of the old state */ |
783 | /* first get rid of the old state */ |
786 | save_perl (prev); |
784 | save_perl (aTHX_ prev); |
787 | /* setup coroutine call */ |
785 | /* setup coroutine call */ |
788 | setup_coro (next); |
786 | setup_coro (aTHX_ next); |
789 | /* need a new stack */ |
787 | /* need a new stack */ |
790 | assert (!next->cctx); |
788 | assert (!next->cctx); |
791 | } |
789 | } |
792 | else |
790 | else |
793 | { |
791 | { |
794 | /* coroutine already started */ |
792 | /* coroutine already started */ |
795 | save_perl (prev); |
793 | save_perl (aTHX_ prev); |
796 | load_perl (next); |
794 | load_perl (aTHX_ next); |
797 | } |
795 | } |
798 | |
796 | |
799 | prev__cctx = prev->cctx; |
797 | prev__cctx = prev->cctx; |
800 | |
798 | |
801 | /* possibly "free" the cctx */ |
799 | /* possibly "free" the cctx */ |
… | |
… | |
810 | prev__cctx->inuse = 0; |
808 | prev__cctx->inuse = 0; |
811 | } |
809 | } |
812 | |
810 | |
813 | if (!next->cctx) |
811 | if (!next->cctx) |
814 | { |
812 | { |
815 | next->cctx = cctx_get (); |
813 | next->cctx = cctx_get (aTHX); |
816 | assert (!next->cctx->inuse); |
814 | assert (!next->cctx->inuse); |
817 | next->cctx->inuse = 1; |
815 | next->cctx->inuse = 1; |
818 | } |
816 | } |
819 | |
817 | |
820 | if (prev__cctx != next->cctx) |
818 | if (prev__cctx != next->cctx) |
… | |
… | |
822 | prev__cctx->top_env = PL_top_env; |
820 | prev__cctx->top_env = PL_top_env; |
823 | PL_top_env = next->cctx->top_env; |
821 | PL_top_env = next->cctx->top_env; |
824 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
822 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
825 | } |
823 | } |
826 | |
824 | |
827 | free_coro_mortal (); |
825 | free_coro_mortal (aTHX); |
828 | UNLOCK; |
826 | UNLOCK; |
829 | } |
827 | } |
830 | } |
828 | } |
831 | |
829 | |
832 | struct transfer_args |
830 | struct transfer_args |
833 | { |
831 | { |
834 | struct coro *prev, *next; |
832 | struct coro *prev, *next; |
835 | }; |
833 | }; |
836 | |
834 | |
837 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
835 | #define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) |
838 | |
836 | |
839 | /** high level stuff ********************************************************/ |
837 | /** high level stuff ********************************************************/ |
840 | |
838 | |
841 | static int |
839 | static int |
842 | coro_state_destroy (struct coro *coro) |
840 | coro_state_destroy (pTHX_ struct coro *coro) |
843 | { |
841 | { |
844 | if (coro->flags & CF_DESTROYED) |
842 | if (coro->flags & CF_DESTROYED) |
845 | return 0; |
843 | return 0; |
846 | |
844 | |
847 | coro->flags |= CF_DESTROYED; |
845 | coro->flags |= CF_DESTROYED; |
… | |
… | |
867 | temp.save = CORO_SAVE_ALL; |
865 | temp.save = CORO_SAVE_ALL; |
868 | |
866 | |
869 | if (coro->flags & CF_RUNNING) |
867 | if (coro->flags & CF_RUNNING) |
870 | croak ("FATAL: tried to destroy currently running coroutine"); |
868 | croak ("FATAL: tried to destroy currently running coroutine"); |
871 | |
869 | |
872 | save_perl (&temp); |
870 | save_perl (aTHX_ &temp); |
873 | load_perl (coro); |
871 | load_perl (aTHX_ coro); |
874 | |
872 | |
875 | coro_destroy_stacks (); |
873 | coro_destroy_stacks (aTHX); |
876 | |
874 | |
877 | load_perl (&temp); /* this will get rid of defsv etc.. */ |
875 | load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ |
878 | |
876 | |
879 | coro->mainstack = 0; |
877 | coro->mainstack = 0; |
880 | } |
878 | } |
881 | |
879 | |
882 | cctx_destroy (coro->cctx); |
880 | cctx_destroy (coro->cctx); |
… | |
… | |
891 | struct coro *coro = (struct coro *)mg->mg_ptr; |
889 | struct coro *coro = (struct coro *)mg->mg_ptr; |
892 | mg->mg_ptr = 0; |
890 | mg->mg_ptr = 0; |
893 | |
891 | |
894 | if (--coro->refcnt < 0) |
892 | if (--coro->refcnt < 0) |
895 | { |
893 | { |
896 | coro_state_destroy (coro); |
894 | coro_state_destroy (aTHX_ coro); |
897 | Safefree (coro); |
895 | Safefree (coro); |
898 | } |
896 | } |
899 | |
897 | |
900 | return 0; |
898 | return 0; |
901 | } |
899 | } |
… | |
… | |
920 | # define MGf_DUP 0 |
918 | # define MGf_DUP 0 |
921 | #endif |
919 | #endif |
922 | }; |
920 | }; |
923 | |
921 | |
924 | static struct coro * |
922 | static struct coro * |
925 | SvSTATE (SV *coro) |
923 | SvSTATE_ (pTHX_ SV *coro) |
926 | { |
924 | { |
927 | HV *stash; |
925 | HV *stash; |
928 | MAGIC *mg; |
926 | MAGIC *mg; |
929 | |
927 | |
930 | if (SvROK (coro)) |
928 | if (SvROK (coro)) |
… | |
… | |
940 | |
938 | |
941 | mg = CORO_MAGIC (coro); |
939 | mg = CORO_MAGIC (coro); |
942 | return (struct coro *)mg->mg_ptr; |
940 | return (struct coro *)mg->mg_ptr; |
943 | } |
941 | } |
944 | |
942 | |
|
|
943 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
944 | |
945 | static void |
945 | static void |
946 | prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
946 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
947 | { |
947 | { |
948 | ta->prev = SvSTATE (prev_sv); |
948 | ta->prev = SvSTATE (prev_sv); |
949 | ta->next = SvSTATE (next_sv); |
949 | ta->next = SvSTATE (next_sv); |
950 | } |
950 | } |
951 | |
951 | |
952 | static void |
952 | static void |
953 | api_transfer (SV *prev_sv, SV *next_sv) |
953 | api_transfer (SV *prev_sv, SV *next_sv) |
954 | { |
954 | { |
|
|
955 | dTHX; |
955 | struct transfer_args ta; |
956 | struct transfer_args ta; |
956 | |
957 | |
957 | prepare_transfer (&ta, prev_sv, next_sv); |
958 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
958 | TRANSFER (ta); |
959 | TRANSFER (ta); |
959 | } |
960 | } |
960 | |
961 | |
961 | static int |
962 | static int |
962 | api_save (SV *coro_sv, int new_save) |
963 | api_save (SV *coro_sv, int new_save) |
963 | { |
964 | { |
|
|
965 | dTHX; |
964 | struct coro *coro = SvSTATE (coro_sv); |
966 | struct coro *coro = SvSTATE (coro_sv); |
965 | int old_save = coro->save; |
967 | int old_save = coro->save; |
966 | |
968 | |
967 | if (new_save >= 0) |
969 | if (new_save >= 0) |
968 | coro->save = new_save; |
970 | coro->save = new_save; |
… | |
… | |
971 | } |
973 | } |
972 | |
974 | |
973 | /** Coro ********************************************************************/ |
975 | /** Coro ********************************************************************/ |
974 | |
976 | |
975 | static void |
977 | static void |
976 | coro_enq (SV *coro_sv) |
978 | coro_enq (pTHX_ SV *coro_sv) |
977 | { |
979 | { |
978 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
980 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
979 | } |
981 | } |
980 | |
982 | |
981 | static SV * |
983 | static SV * |
982 | coro_deq (int min_prio) |
984 | coro_deq (pTHX_ int min_prio) |
983 | { |
985 | { |
984 | int prio = PRIO_MAX - PRIO_MIN; |
986 | int prio = PRIO_MAX - PRIO_MIN; |
985 | |
987 | |
986 | min_prio -= PRIO_MIN; |
988 | min_prio -= PRIO_MIN; |
987 | if (min_prio < 0) |
989 | if (min_prio < 0) |
… | |
… | |
995 | } |
997 | } |
996 | |
998 | |
997 | static int |
999 | static int |
998 | api_ready (SV *coro_sv) |
1000 | api_ready (SV *coro_sv) |
999 | { |
1001 | { |
|
|
1002 | dTHX; |
1000 | struct coro *coro; |
1003 | struct coro *coro; |
1001 | |
1004 | |
1002 | if (SvROK (coro_sv)) |
1005 | if (SvROK (coro_sv)) |
1003 | coro_sv = SvRV (coro_sv); |
1006 | coro_sv = SvRV (coro_sv); |
1004 | |
1007 | |
… | |
… | |
1008 | return 0; |
1011 | return 0; |
1009 | |
1012 | |
1010 | coro->flags |= CF_READY; |
1013 | coro->flags |= CF_READY; |
1011 | |
1014 | |
1012 | LOCK; |
1015 | LOCK; |
1013 | coro_enq (SvREFCNT_inc (coro_sv)); |
1016 | coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); |
1014 | ++coro_nready; |
1017 | ++coro_nready; |
1015 | UNLOCK; |
1018 | UNLOCK; |
1016 | |
1019 | |
1017 | return 1; |
1020 | return 1; |
1018 | } |
1021 | } |
1019 | |
1022 | |
1020 | static int |
1023 | static int |
1021 | api_is_ready (SV *coro_sv) |
1024 | api_is_ready (SV *coro_sv) |
1022 | { |
1025 | { |
|
|
1026 | dTHX; |
1023 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1027 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1024 | } |
1028 | } |
1025 | |
1029 | |
1026 | static void |
1030 | static void |
1027 | prepare_schedule (struct transfer_args *ta) |
1031 | prepare_schedule (pTHX_ struct transfer_args *ta) |
1028 | { |
1032 | { |
1029 | SV *prev_sv, *next_sv; |
1033 | SV *prev_sv, *next_sv; |
1030 | |
1034 | |
1031 | for (;;) |
1035 | for (;;) |
1032 | { |
1036 | { |
1033 | LOCK; |
1037 | LOCK; |
1034 | next_sv = coro_deq (PRIO_MIN); |
1038 | next_sv = coro_deq (aTHX_ PRIO_MIN); |
1035 | |
1039 | |
1036 | /* nothing to schedule: call the idle handler */ |
1040 | /* nothing to schedule: call the idle handler */ |
1037 | if (!next_sv) |
1041 | if (!next_sv) |
1038 | { |
1042 | { |
1039 | dSP; |
1043 | dSP; |
… | |
… | |
1074 | |
1078 | |
1075 | assert (ta->next->flags & CF_READY); |
1079 | assert (ta->next->flags & CF_READY); |
1076 | ta->next->flags &= ~CF_READY; |
1080 | ta->next->flags &= ~CF_READY; |
1077 | |
1081 | |
1078 | LOCK; |
1082 | LOCK; |
1079 | free_coro_mortal (); |
1083 | free_coro_mortal (aTHX); |
1080 | coro_mortal = prev_sv; |
1084 | coro_mortal = prev_sv; |
1081 | UNLOCK; |
1085 | UNLOCK; |
1082 | } |
1086 | } |
1083 | |
1087 | |
1084 | static void |
1088 | static void |
1085 | prepare_cede (struct transfer_args *ta) |
1089 | prepare_cede (pTHX_ struct transfer_args *ta) |
1086 | { |
1090 | { |
1087 | api_ready (coro_current); |
1091 | api_ready (coro_current); |
1088 | prepare_schedule (ta); |
1092 | prepare_schedule (aTHX_ ta); |
1089 | } |
1093 | } |
1090 | |
1094 | |
1091 | static int |
1095 | static int |
1092 | prepare_cede_notself (struct transfer_args *ta) |
1096 | prepare_cede_notself (pTHX_ struct transfer_args *ta) |
1093 | { |
1097 | { |
1094 | if (coro_nready) |
1098 | if (coro_nready) |
1095 | { |
1099 | { |
1096 | SV *prev = SvRV (coro_current); |
1100 | SV *prev = SvRV (coro_current); |
1097 | prepare_schedule (ta); |
1101 | prepare_schedule (aTHX_ ta); |
1098 | api_ready (prev); |
1102 | api_ready (prev); |
1099 | return 1; |
1103 | return 1; |
1100 | } |
1104 | } |
1101 | else |
1105 | else |
1102 | return 0; |
1106 | return 0; |
1103 | } |
1107 | } |
1104 | |
1108 | |
1105 | static void |
1109 | static void |
1106 | api_schedule (void) |
1110 | api_schedule (void) |
1107 | { |
1111 | { |
|
|
1112 | dTHX; |
1108 | struct transfer_args ta; |
1113 | struct transfer_args ta; |
1109 | |
1114 | |
1110 | prepare_schedule (&ta); |
1115 | prepare_schedule (aTHX_ &ta); |
1111 | TRANSFER (ta); |
1116 | TRANSFER (ta); |
1112 | } |
1117 | } |
1113 | |
1118 | |
1114 | static int |
1119 | static int |
1115 | api_cede (void) |
1120 | api_cede (void) |
1116 | { |
1121 | { |
|
|
1122 | dTHX; |
1117 | struct transfer_args ta; |
1123 | struct transfer_args ta; |
1118 | |
1124 | |
1119 | prepare_cede (&ta); |
1125 | prepare_cede (aTHX_ &ta); |
1120 | |
1126 | |
1121 | if (ta.prev != ta.next) |
1127 | if (ta.prev != ta.next) |
1122 | { |
1128 | { |
1123 | TRANSFER (ta); |
1129 | TRANSFER (ta); |
1124 | return 1; |
1130 | return 1; |
… | |
… | |
1128 | } |
1134 | } |
1129 | |
1135 | |
1130 | static int |
1136 | static int |
1131 | api_cede_notself (void) |
1137 | api_cede_notself (void) |
1132 | { |
1138 | { |
|
|
1139 | dTHX; |
1133 | struct transfer_args ta; |
1140 | struct transfer_args ta; |
1134 | |
1141 | |
1135 | if (prepare_cede_notself (&ta)) |
1142 | if (prepare_cede_notself (aTHX_ &ta)) |
1136 | { |
1143 | { |
1137 | TRANSFER (ta); |
1144 | TRANSFER (ta); |
1138 | return 1; |
1145 | return 1; |
1139 | } |
1146 | } |
1140 | else |
1147 | else |
… | |
… | |
1218 | |
1225 | |
1219 | case 1: |
1226 | case 1: |
1220 | if (items != 2) |
1227 | if (items != 2) |
1221 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1228 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1222 | |
1229 | |
1223 | prepare_transfer (&ta, ST (0), ST (1)); |
1230 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1224 | break; |
1231 | break; |
1225 | |
1232 | |
1226 | case 2: |
1233 | case 2: |
1227 | prepare_schedule (&ta); |
1234 | prepare_schedule (aTHX_ &ta); |
1228 | break; |
1235 | break; |
1229 | |
1236 | |
1230 | case 3: |
1237 | case 3: |
1231 | prepare_cede (&ta); |
1238 | prepare_cede (aTHX_ &ta); |
1232 | break; |
1239 | break; |
1233 | |
1240 | |
1234 | case 4: |
1241 | case 4: |
1235 | if (!prepare_cede_notself (&ta)) |
1242 | if (!prepare_cede_notself (aTHX_ &ta)) |
1236 | XSRETURN_EMPTY; |
1243 | XSRETURN_EMPTY; |
1237 | |
1244 | |
1238 | break; |
1245 | break; |
1239 | } |
1246 | } |
1240 | |
1247 | |
… | |
… | |
1246 | } |
1253 | } |
1247 | |
1254 | |
1248 | bool |
1255 | bool |
1249 | _destroy (SV *coro_sv) |
1256 | _destroy (SV *coro_sv) |
1250 | CODE: |
1257 | CODE: |
1251 | RETVAL = coro_state_destroy (SvSTATE (coro_sv)); |
1258 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
1252 | OUTPUT: |
1259 | OUTPUT: |
1253 | RETVAL |
1260 | RETVAL |
1254 | |
1261 | |
1255 | void |
1262 | void |
1256 | _exit (code) |
1263 | _exit (code) |
1257 | int code |
1264 | int code |
1258 | PROTOTYPE: $ |
1265 | PROTOTYPE: $ |
1259 | CODE: |
1266 | CODE: |
1260 | _exit (code); |
1267 | _exit (code); |
|
|
1268 | |
|
|
1269 | int |
|
|
1270 | cctx_stacksize (int new_stacksize = 0) |
|
|
1271 | CODE: |
|
|
1272 | RETVAL = coro_stacksize; |
|
|
1273 | if (new_stacksize) |
|
|
1274 | coro_stacksize = new_stacksize; |
|
|
1275 | OUTPUT: |
|
|
1276 | RETVAL |
1261 | |
1277 | |
1262 | int |
1278 | int |
1263 | cctx_count () |
1279 | cctx_count () |
1264 | CODE: |
1280 | CODE: |
1265 | RETVAL = cctx_count; |
1281 | RETVAL = cctx_count; |