1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
|
|
5 | #include "patchlevel.h" |
|
|
6 | |
|
|
7 | #if PATCHLEVEL < 6 |
|
|
8 | # ifndef PL_ppaddr |
|
|
9 | # define PL_ppaddr ppaddr |
|
|
10 | # endif |
|
|
11 | # ifndef call_sv |
|
|
12 | # define call_sv perl_call_sv |
|
|
13 | # endif |
|
|
14 | # ifndef get_sv |
|
|
15 | # define get_sv perl_get_sv |
|
|
16 | # endif |
|
|
17 | # ifndef get_cv |
|
|
18 | # define get_cv perl_get_cv |
|
|
19 | # endif |
|
|
20 | # ifndef IS_PADGV |
|
|
21 | # define IS_PADGV(v) 0 |
|
|
22 | # endif |
|
|
23 | # ifndef IS_PADCONST |
|
|
24 | # define IS_PADCONST(v) 0 |
|
|
25 | # endif |
|
|
26 | #endif |
|
|
27 | |
5 | #include "libcoro/coro.c" |
28 | #include "libcoro/coro.c" |
6 | |
29 | |
7 | #include <signal.h> |
30 | #include <signal.h> |
8 | |
31 | |
9 | #ifdef HAVE_MMAP |
32 | #ifdef HAVE_MMAP |
10 | # include <unistd.h> |
33 | # include <unistd.h> |
11 | # include <sys/mman.h> |
34 | # include <sys/mman.h> |
12 | # ifndef MAP_ANON |
|
|
13 | # ifdef MAP_ANONYMOUS |
35 | # ifndef MAP_ANONYMOUS |
14 | # define MAP_ANON MAP_ANONYMOUS |
36 | # ifdef MAP_ANON |
|
|
37 | # define MAP_ANONYMOUS MAP_ANON |
15 | # else |
38 | # else |
16 | # undef HAVE_MMAP |
39 | # undef HAVE_MMAP |
17 | # endif |
40 | # endif |
18 | # endif |
41 | # endif |
19 | #endif |
42 | #endif |
… | |
… | |
47 | void *sptr; |
70 | void *sptr; |
48 | long ssize; /* positive == mmap, otherwise malloc */ |
71 | long ssize; /* positive == mmap, otherwise malloc */ |
49 | } coro_stack; |
72 | } coro_stack; |
50 | |
73 | |
51 | struct coro { |
74 | struct coro { |
|
|
75 | /* the top-level JMPENV for each coroutine, needed to catch dies. */ |
|
|
76 | JMPENV start_env; |
|
|
77 | |
52 | /* the optional C context */ |
78 | /* the optional C context */ |
53 | coro_stack *stack; |
79 | coro_stack *stack; |
54 | void *cursp; |
80 | void *cursp; |
55 | int gencnt; |
81 | int gencnt; |
56 | |
82 | |
… | |
… | |
68 | AV *curstack; |
94 | AV *curstack; |
69 | AV *mainstack; |
95 | AV *mainstack; |
70 | SV **stack_sp; |
96 | SV **stack_sp; |
71 | OP *op; |
97 | OP *op; |
72 | SV **curpad; |
98 | SV **curpad; |
|
|
99 | AV *comppad; |
73 | SV **stack_base; |
100 | SV **stack_base; |
74 | SV **stack_max; |
101 | SV **stack_max; |
75 | SV **tmps_stack; |
102 | SV **tmps_stack; |
76 | I32 tmps_floor; |
103 | I32 tmps_floor; |
77 | I32 tmps_ix; |
104 | I32 tmps_ix; |
… | |
… | |
101 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
128 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
102 | static HV *coro_state_stash; |
129 | static HV *coro_state_stash; |
103 | static SV *ucoro_state_sv; |
130 | static SV *ucoro_state_sv; |
104 | static U32 ucoro_state_hash; |
131 | static U32 ucoro_state_hash; |
105 | static HV *padlist_cache; |
132 | static HV *padlist_cache; |
|
|
133 | static SV *coro_mortal; /* will be freed after next transfer */ |
106 | |
134 | |
107 | /* mostly copied from op.c:cv_clone2 */ |
135 | /* mostly copied from op.c:cv_clone2 */ |
108 | STATIC AV * |
136 | STATIC AV * |
109 | clone_padlist (AV *protopadlist) |
137 | clone_padlist (AV *protopadlist) |
110 | { |
138 | { |
… | |
… | |
156 | sv = (SV *) newAV (); |
184 | sv = (SV *) newAV (); |
157 | else if (*name == '%') |
185 | else if (*name == '%') |
158 | sv = (SV *) newHV (); |
186 | sv = (SV *) newHV (); |
159 | else |
187 | else |
160 | sv = NEWSV (0, 0); |
188 | sv = NEWSV (0, 0); |
|
|
189 | #ifdef SvPADBUSY |
161 | if (!SvPADBUSY (sv)) |
190 | if (!SvPADBUSY (sv)) |
|
|
191 | #endif |
162 | SvPADMY_on (sv); |
192 | SvPADMY_on (sv); |
163 | npad[ix] = sv; |
193 | npad[ix] = sv; |
164 | } |
194 | } |
165 | } |
195 | } |
166 | else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix])) |
196 | else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix])) |
… | |
… | |
275 | #define SE } while (0) |
305 | #define SE } while (0) |
276 | |
306 | |
277 | #define LOAD(state) load_state(aTHX_ (state)); |
307 | #define LOAD(state) load_state(aTHX_ (state)); |
278 | #define SAVE(state,flags) save_state(aTHX_ (state),(flags)); |
308 | #define SAVE(state,flags) save_state(aTHX_ (state),(flags)); |
279 | |
309 | |
280 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE |
310 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
281 | |
311 | |
282 | static void |
312 | static void |
283 | load_state(pTHX_ Coro__State c) |
313 | load_state(pTHX_ Coro__State c) |
284 | { |
314 | { |
285 | PL_dowarn = c->dowarn; |
315 | PL_dowarn = c->dowarn; |
… | |
… | |
289 | PL_curstack = c->curstack; |
319 | PL_curstack = c->curstack; |
290 | PL_mainstack = c->mainstack; |
320 | PL_mainstack = c->mainstack; |
291 | PL_stack_sp = c->stack_sp; |
321 | PL_stack_sp = c->stack_sp; |
292 | PL_op = c->op; |
322 | PL_op = c->op; |
293 | PL_curpad = c->curpad; |
323 | PL_curpad = c->curpad; |
|
|
324 | PL_comppad = c->comppad; |
294 | PL_stack_base = c->stack_base; |
325 | PL_stack_base = c->stack_base; |
295 | PL_stack_max = c->stack_max; |
326 | PL_stack_max = c->stack_max; |
296 | PL_tmps_stack = c->tmps_stack; |
327 | PL_tmps_stack = c->tmps_stack; |
297 | PL_tmps_floor = c->tmps_floor; |
328 | PL_tmps_floor = c->tmps_floor; |
298 | PL_tmps_ix = c->tmps_ix; |
329 | PL_tmps_ix = c->tmps_ix; |
… | |
… | |
386 | PUSHs ((SV *)cv); |
417 | PUSHs ((SV *)cv); |
387 | |
418 | |
388 | get_padlist (cv); /* this is a monster */ |
419 | get_padlist (cv); /* this is a monster */ |
389 | } |
420 | } |
390 | } |
421 | } |
|
|
422 | #ifdef CXt_FORMAT |
391 | else if (CxTYPE(cx) == CXt_FORMAT) |
423 | else if (CxTYPE(cx) == CXt_FORMAT) |
392 | { |
424 | { |
393 | /* I never used formats, so how should I know how these are implemented? */ |
425 | /* I never used formats, so how should I know how these are implemented? */ |
394 | /* my bold guess is as a simple, plain sub... */ |
426 | /* my bold guess is as a simple, plain sub... */ |
395 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
427 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
396 | } |
428 | } |
|
|
429 | #endif |
397 | } |
430 | } |
398 | |
431 | |
399 | if (top_si->si_type == PERLSI_MAIN) |
432 | if (top_si->si_type == PERLSI_MAIN) |
400 | break; |
433 | break; |
401 | |
434 | |
… | |
… | |
418 | c->curstack = PL_curstack; |
451 | c->curstack = PL_curstack; |
419 | c->mainstack = PL_mainstack; |
452 | c->mainstack = PL_mainstack; |
420 | c->stack_sp = PL_stack_sp; |
453 | c->stack_sp = PL_stack_sp; |
421 | c->op = PL_op; |
454 | c->op = PL_op; |
422 | c->curpad = PL_curpad; |
455 | c->curpad = PL_curpad; |
|
|
456 | c->comppad = PL_comppad; |
423 | c->stack_base = PL_stack_base; |
457 | c->stack_base = PL_stack_base; |
424 | c->stack_max = PL_stack_max; |
458 | c->stack_max = PL_stack_max; |
425 | c->tmps_stack = PL_tmps_stack; |
459 | c->tmps_stack = PL_tmps_stack; |
426 | c->tmps_floor = PL_tmps_floor; |
460 | c->tmps_floor = PL_tmps_floor; |
427 | c->tmps_ix = PL_tmps_ix; |
461 | c->tmps_ix = PL_tmps_ix; |
… | |
… | |
467 | |
501 | |
468 | New(54,PL_markstack,16,I32); |
502 | New(54,PL_markstack,16,I32); |
469 | PL_markstack_ptr = PL_markstack; |
503 | PL_markstack_ptr = PL_markstack; |
470 | PL_markstack_max = PL_markstack + 16; |
504 | PL_markstack_max = PL_markstack + 16; |
471 | |
505 | |
|
|
506 | #ifdef SET_MARK_OFFSET |
472 | SET_MARK_OFFSET; |
507 | SET_MARK_OFFSET; |
|
|
508 | #endif |
473 | |
509 | |
474 | New(54,PL_scopestack,16,I32); |
510 | New(54,PL_scopestack,16,I32); |
475 | PL_scopestack_ix = 0; |
511 | PL_scopestack_ix = 0; |
476 | PL_scopestack_max = 16; |
512 | PL_scopestack_max = 16; |
477 | |
513 | |
… | |
… | |
484 | PL_retstack_max = 8; |
520 | PL_retstack_max = 8; |
485 | } |
521 | } |
486 | |
522 | |
487 | /* |
523 | /* |
488 | * destroy the stacks, the callchain etc... |
524 | * destroy the stacks, the callchain etc... |
489 | * still there is a memleak of 128 bytes... |
|
|
490 | */ |
525 | */ |
491 | STATIC void |
526 | STATIC void |
492 | destroy_stacks(pTHX) |
527 | destroy_stacks(pTHX) |
493 | { |
528 | { |
494 | if (!IN_DESTRUCT) |
529 | if (!IN_DESTRUCT) |
… | |
… | |
543 | stack->refcnt = 1; |
578 | stack->refcnt = 1; |
544 | stack->usecnt = 1; |
579 | stack->usecnt = 1; |
545 | stack->gencnt = ctx->gencnt = 0; |
580 | stack->gencnt = ctx->gencnt = 0; |
546 | if (alloc) |
581 | if (alloc) |
547 | { |
582 | { |
548 | #ifdef HAVE_MMAP |
583 | #if HAVE_MMAP |
549 | stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ |
584 | stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ |
550 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); |
585 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
551 | if (stack->sptr == (void *)-1) |
586 | if (stack->sptr == (void *)-1) |
552 | #endif |
587 | #endif |
553 | { |
588 | { |
554 | /*FIXME*//*D*//* reasonable stack size! */ |
589 | /*FIXME*//*D*//* reasonable stack size! */ |
555 | stack->ssize = -4096 * sizeof (long); |
590 | stack->ssize = -4096 * sizeof (long); |
… | |
… | |
575 | { |
610 | { |
576 | #ifdef HAVE_MMAP |
611 | #ifdef HAVE_MMAP |
577 | if (stack->ssize > 0 && stack->sptr) |
612 | if (stack->ssize > 0 && stack->sptr) |
578 | munmap (stack->sptr, stack->ssize); |
613 | munmap (stack->sptr, stack->ssize); |
579 | else |
614 | else |
580 | #else |
615 | #endif |
581 | Safefree (stack->sptr); |
616 | Safefree (stack->sptr); |
582 | #endif |
617 | |
583 | Safefree (stack); |
618 | Safefree (stack); |
584 | } |
619 | } |
585 | else if (ctx->gencnt == stack->gencnt) |
620 | else if (ctx->gencnt == stack->gencnt) |
586 | --stack->usecnt; |
621 | --stack->usecnt; |
587 | } |
622 | } |
… | |
… | |
599 | |
634 | |
600 | coro_init_stacks (aTHX); |
635 | coro_init_stacks (aTHX); |
601 | /*PL_curcop = 0;*/ |
636 | /*PL_curcop = 0;*/ |
602 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
637 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
603 | SvREFCNT_dec (GvAV (PL_defgv)); |
638 | SvREFCNT_dec (GvAV (PL_defgv)); |
604 | GvAV (PL_defgv) = ctx->args; |
639 | GvAV (PL_defgv) = ctx->args; ctx->args = 0; |
605 | |
640 | |
606 | SPAGAIN; |
641 | SPAGAIN; |
607 | |
642 | |
608 | if (ctx->stack) |
643 | if (ctx->stack) |
609 | { |
644 | { |
… | |
… | |
636 | * that doesn't matter, though, since it is only |
671 | * that doesn't matter, though, since it is only |
637 | * pp_nextstate and we never return... |
672 | * pp_nextstate and we never return... |
638 | * ah yes, and I don't care anyways ;) |
673 | * ah yes, and I don't care anyways ;) |
639 | */ |
674 | */ |
640 | PUTBACK; |
675 | PUTBACK; |
641 | PL_op = pp_entersub(); |
676 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
642 | SPAGAIN; |
677 | SPAGAIN; |
643 | |
678 | |
644 | ENTER; /* necessary e.g. for dounwind */ |
679 | ENTER; /* necessary e.g. for dounwind */ |
645 | } |
680 | } |
646 | } |
681 | } |
… | |
… | |
652 | * this is a _very_ stripped down perl interpreter ;) |
687 | * this is a _very_ stripped down perl interpreter ;) |
653 | */ |
688 | */ |
654 | Coro__State ctx = (Coro__State)arg; |
689 | Coro__State ctx = (Coro__State)arg; |
655 | JMPENV coro_start_env; |
690 | JMPENV coro_start_env; |
656 | |
691 | |
657 | /* same as JMPENV_BOOTSTRAP */ |
|
|
658 | Zero(&coro_start_env, 1, JMPENV); |
|
|
659 | coro_start_env.je_ret = -1; |
|
|
660 | coro_start_env.je_mustcatch = TRUE; |
|
|
661 | PL_top_env = &coro_start_env; |
692 | PL_top_env = &ctx->start_env; |
662 | |
693 | |
663 | ctx->cursp = 0; |
694 | ctx->cursp = 0; |
664 | PL_op = PL_op->op_next; |
695 | PL_op = PL_op->op_next; |
665 | CALLRUNOPS(aTHX); |
696 | CALLRUNOPS(aTHX); |
666 | |
697 | |
… | |
… | |
724 | if (!prev->stack) |
755 | if (!prev->stack) |
725 | allocate_stack (prev, 0); |
756 | allocate_stack (prev, 0); |
726 | |
757 | |
727 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
758 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
728 | { |
759 | { |
|
|
760 | PL_top_env = &next->start_env; |
|
|
761 | |
729 | setup_coro (next); |
762 | setup_coro (next); |
730 | |
763 | |
731 | prev->stack->refcnt++; |
764 | prev->stack->refcnt++; |
732 | prev->stack->usecnt++; |
765 | prev->stack->usecnt++; |
733 | next->stack = prev->stack; |
766 | next->stack = prev->stack; |
734 | next->gencnt = prev->gencnt; |
767 | next->gencnt = prev->gencnt; |
735 | } |
768 | } |
736 | else |
769 | else |
737 | { |
770 | { |
|
|
771 | assert (!next->stack); |
738 | allocate_stack (next, 1); |
772 | allocate_stack (next, 1); |
739 | coro_create (&(next->stack->cctx), |
773 | coro_create (&(next->stack->cctx), |
740 | setup_coro, (void *)next, |
774 | setup_coro, (void *)next, |
741 | next->stack->sptr, labs (next->stack->ssize)); |
775 | next->stack->sptr, labs (next->stack->ssize)); |
742 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
776 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
… | |
… | |
747 | setup_coro (next); |
781 | setup_coro (next); |
748 | } |
782 | } |
749 | |
783 | |
750 | /* |
784 | /* |
751 | * xnext is now either prev or next, depending on wether |
785 | * xnext is now either prev or next, depending on wether |
752 | * we switched the c stack or not. that's why i use a global |
786 | * we switched the c stack or not. that's why I use a global |
753 | * variable, that should become thread-specific at one point. |
787 | * variable, that should become thread-specific at one point. |
754 | */ |
788 | */ |
755 | xnext->cursp = stacklevel; |
789 | xnext->cursp = stacklevel; |
756 | } |
790 | } |
757 | } |
|
|
758 | |
791 | |
759 | static struct coro * |
792 | if (coro_mortal) |
760 | sv_to_coro (SV *arg, const char *funcname, const char *varname) |
|
|
761 | { |
|
|
762 | if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV) |
|
|
763 | { |
793 | { |
764 | HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash); |
794 | SvREFCNT_dec (coro_mortal); |
765 | |
795 | coro_mortal = 0; |
766 | if (!he) |
|
|
767 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname); |
|
|
768 | |
|
|
769 | arg = HeVAL(he); |
|
|
770 | } |
796 | } |
771 | |
797 | } |
|
|
798 | |
|
|
799 | #define SV_CORO(sv,func) \ |
|
|
800 | do { \ |
|
|
801 | if (SvROK (sv)) \ |
|
|
802 | sv = SvRV (sv); \ |
|
|
803 | \ |
|
|
804 | if (SvTYPE(sv) == SVt_PVHV) \ |
|
|
805 | { \ |
|
|
806 | HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ |
|
|
807 | \ |
|
|
808 | if (!he) \ |
|
|
809 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ |
|
|
810 | \ |
|
|
811 | (sv) = SvRV (HeVAL(he)); \ |
|
|
812 | } \ |
|
|
813 | \ |
772 | /* must also be changed inside Coro::Cont::yield */ |
814 | /* must also be changed inside Coro::Cont::yield */ \ |
773 | if (SvROK(arg) && SvOBJECT(SvRV(arg)) |
815 | if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \ |
774 | && SvSTASH(SvRV(arg)) == coro_state_stash) |
|
|
775 | return (struct coro *) SvIV((SV*)SvRV(arg)); |
|
|
776 | |
|
|
777 | croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname); |
816 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
778 | /*NORETURN*/ |
817 | \ |
779 | } |
818 | } while(0) |
|
|
819 | |
|
|
820 | #define SvSTATE(sv) (struct coro *)SvIV (sv) |
780 | |
821 | |
781 | static void |
822 | static void |
782 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
823 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
783 | { |
824 | { |
784 | transfer(aTHX_ |
825 | SV_CORO (prev, "Coro::transfer"); |
785 | sv_to_coro (prev, "Coro::transfer", "prev"), |
826 | SV_CORO (next, "Coro::transfer"); |
786 | sv_to_coro (next, "Coro::transfer", "next"), |
827 | |
787 | flags); |
828 | transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); |
788 | } |
829 | } |
789 | |
830 | |
790 | /** Coro ********************************************************************/ |
831 | /** Coro ********************************************************************/ |
791 | |
832 | |
792 | #define PRIO_MAX 3 |
833 | #define PRIO_MAX 3 |
… | |
… | |
802 | static int coro_nready; |
843 | static int coro_nready; |
803 | |
844 | |
804 | static void |
845 | static void |
805 | coro_enq (SV *sv) |
846 | coro_enq (SV *sv) |
806 | { |
847 | { |
807 | if (SvROK (sv)) |
|
|
808 | { |
|
|
809 | SV *hv = SvRV (sv); |
|
|
810 | if (SvTYPE (hv) == SVt_PVHV) |
848 | if (SvTYPE (sv) == SVt_PVHV) |
811 | { |
849 | { |
812 | SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0); |
850 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
813 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
851 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
814 | |
852 | |
815 | prio = prio > PRIO_MAX ? PRIO_MAX |
853 | prio = prio > PRIO_MAX ? PRIO_MAX |
816 | : prio < PRIO_MIN ? PRIO_MIN |
854 | : prio < PRIO_MIN ? PRIO_MIN |
817 | : prio; |
855 | : prio; |
818 | |
856 | |
819 | av_push (coro_ready [prio - PRIO_MIN], sv); |
857 | av_push (coro_ready [prio - PRIO_MIN], sv); |
820 | coro_nready++; |
858 | coro_nready++; |
821 | |
859 | |
822 | return; |
860 | return; |
823 | } |
|
|
824 | } |
861 | } |
825 | |
862 | |
826 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
863 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
827 | } |
864 | } |
828 | |
865 | |
… | |
… | |
846 | } |
883 | } |
847 | |
884 | |
848 | static void |
885 | static void |
849 | api_ready (SV *coro) |
886 | api_ready (SV *coro) |
850 | { |
887 | { |
|
|
888 | if (SvROK (coro)) |
|
|
889 | coro = SvRV (coro); |
|
|
890 | |
851 | coro_enq (SvREFCNT_inc (coro)); |
891 | coro_enq (SvREFCNT_inc (coro)); |
852 | } |
892 | } |
853 | |
893 | |
854 | static void |
894 | static void |
855 | api_schedule (int cede) |
895 | api_schedule (void) |
856 | { |
896 | { |
857 | SV *prev, *next; |
897 | SV *prev, *next; |
858 | |
898 | |
859 | prev = GvSV (coro_current); |
899 | prev = SvRV (GvSV (coro_current)); |
860 | |
|
|
861 | if (cede) |
|
|
862 | coro_enq (SvREFCNT_inc (prev)); |
|
|
863 | |
|
|
864 | next = coro_deq (PRIO_MIN); |
900 | next = coro_deq (PRIO_MIN); |
865 | |
901 | |
866 | if (!next) |
902 | if (!next) |
867 | next = SvREFCNT_inc (GvSV (coro_idle)); |
903 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
868 | |
904 | |
869 | GvSV (coro_current) = SvREFCNT_inc (next); |
905 | /* free this only after the transfer */ |
870 | transfer (aTHX_ |
906 | coro_mortal = prev; |
871 | sv_to_coro (prev, "Coro::schedule", "current coroutine"), |
907 | SV_CORO (prev, "Coro::schedule"); |
872 | sv_to_coro (next, "Coro::schedule", "next coroutine"), |
908 | |
|
|
909 | SvRV (GvSV (coro_current)) = next; |
|
|
910 | |
|
|
911 | SV_CORO (next, "Coro::schedule"); |
|
|
912 | |
|
|
913 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), |
873 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
914 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
874 | SvREFCNT_dec (next); |
915 | } |
875 | SvREFCNT_dec (prev); |
916 | |
|
|
917 | static void |
|
|
918 | api_cede (void) |
|
|
919 | { |
|
|
920 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
|
|
921 | |
|
|
922 | api_schedule (); |
876 | } |
923 | } |
877 | |
924 | |
878 | MODULE = Coro::State PACKAGE = Coro::State |
925 | MODULE = Coro::State PACKAGE = Coro::State |
879 | |
926 | |
880 | PROTOTYPES: ENABLE |
927 | PROTOTYPES: ENABLE |
… | |
… | |
907 | Coro__State coro; |
954 | Coro__State coro; |
908 | |
955 | |
909 | if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) |
956 | if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) |
910 | croak ("Coro::State::_newprocess expects an arrayref"); |
957 | croak ("Coro::State::_newprocess expects an arrayref"); |
911 | |
958 | |
912 | New (0, coro, 1, struct coro); |
959 | Newz (0, coro, 1, struct coro); |
913 | |
960 | |
914 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
961 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
915 | coro->mainstack = 0; /* actual work is done inside transfer */ |
962 | coro->mainstack = 0; /* actual work is done inside transfer */ |
916 | coro->stack = 0; |
963 | coro->stack = 0; |
917 | |
964 | |
|
|
965 | /* same as JMPENV_BOOTSTRAP */ |
|
|
966 | /* we might be able to recycle start_env, but safe is safe */ |
|
|
967 | //Zero(&coro->start_env, 1, JMPENV); |
|
|
968 | coro->start_env.je_ret = -1; |
|
|
969 | coro->start_env.je_mustcatch = TRUE; |
|
|
970 | |
918 | RETVAL = coro; |
971 | RETVAL = coro; |
919 | OUTPUT: |
972 | OUTPUT: |
920 | RETVAL |
973 | RETVAL |
921 | |
974 | |
922 | void |
975 | void |
923 | transfer(prev, next, flags) |
976 | transfer(prev, next, flags) |
924 | Coro::State_or_hashref prev |
977 | SV *prev |
925 | Coro::State_or_hashref next |
978 | SV *next |
926 | int flags |
979 | int flags |
927 | PROTOTYPE: @ |
980 | PROTOTYPE: @ |
928 | CODE: |
981 | CODE: |
929 | PUTBACK; |
982 | PUTBACK; |
|
|
983 | SV_CORO (next, "Coro::transfer"); |
|
|
984 | SV_CORO (prev, "Coro::transfer"); |
930 | transfer (aTHX_ prev, next, flags); |
985 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
931 | SPAGAIN; |
986 | SPAGAIN; |
932 | |
987 | |
933 | void |
988 | void |
934 | DESTROY(coro) |
989 | DESTROY(coro) |
935 | Coro::State coro |
990 | Coro::State coro |
… | |
… | |
951 | |
1006 | |
952 | coro->mainstack = 0; |
1007 | coro->mainstack = 0; |
953 | } |
1008 | } |
954 | |
1009 | |
955 | deallocate_stack (coro); |
1010 | deallocate_stack (coro); |
956 | |
1011 | SvREFCNT_dec (coro->args); |
957 | Safefree (coro); |
1012 | Safefree (coro); |
958 | |
1013 | |
959 | void |
1014 | void |
960 | flush() |
1015 | flush() |
961 | CODE: |
1016 | CODE: |
… | |
… | |
988 | SV *sv; |
1043 | SV *sv; |
989 | AV *defav = GvAV (PL_defgv); |
1044 | AV *defav = GvAV (PL_defgv); |
990 | struct coro *prev, *next; |
1045 | struct coro *prev, *next; |
991 | |
1046 | |
992 | if (!returnstk) |
1047 | if (!returnstk) |
993 | returnstk = SvRV (get_sv ("Coro::Cont::return", FALSE)); |
1048 | returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE)); |
994 | |
1049 | |
995 | /* set up @_ -- ugly */ |
1050 | /* set up @_ -- ugly */ |
996 | av_clear (defav); |
1051 | av_clear (defav); |
997 | av_fill (defav, items - 1); |
1052 | av_fill (defav, items - 1); |
998 | while (items--) |
1053 | while (items--) |
… | |
… | |
1030 | |
1085 | |
1031 | { |
1086 | { |
1032 | SV *sv = perl_get_sv("Coro::API", 1); |
1087 | SV *sv = perl_get_sv("Coro::API", 1); |
1033 | |
1088 | |
1034 | coroapi.schedule = api_schedule; |
1089 | coroapi.schedule = api_schedule; |
|
|
1090 | coroapi.cede = api_cede; |
1035 | coroapi.ready = api_ready; |
1091 | coroapi.ready = api_ready; |
1036 | coroapi.nready = &coro_nready; |
1092 | coroapi.nready = &coro_nready; |
1037 | coroapi.current = coro_current; |
1093 | coroapi.current = coro_current; |
1038 | |
1094 | |
1039 | GCoroAPI = &coroapi; |
1095 | GCoroAPI = &coroapi; |
… | |
… | |
1058 | RETVAL |
1114 | RETVAL |
1059 | |
1115 | |
1060 | void |
1116 | void |
1061 | schedule(...) |
1117 | schedule(...) |
1062 | PROTOTYPE: |
1118 | PROTOTYPE: |
1063 | ALIAS: |
|
|
1064 | cede = 1 |
|
|
1065 | CODE: |
1119 | CODE: |
1066 | api_schedule (ix); |
1120 | api_schedule (); |
1067 | |
1121 | |
|
|
1122 | void |
|
|
1123 | cede(...) |
|
|
1124 | PROTOTYPE: |
|
|
1125 | CODE: |
|
|
1126 | api_cede (); |
|
|
1127 | |