… | |
… | |
7 | #include <signal.h> |
7 | #include <signal.h> |
8 | |
8 | |
9 | #ifdef HAVE_MMAP |
9 | #ifdef HAVE_MMAP |
10 | # include <unistd.h> |
10 | # include <unistd.h> |
11 | # include <sys/mman.h> |
11 | # include <sys/mman.h> |
12 | # ifndef MAP_ANON |
|
|
13 | # ifdef MAP_ANONYMOUS |
12 | # ifndef MAP_ANONYMOUS |
14 | # define MAP_ANON MAP_ANONYMOUS |
13 | # ifdef MAP_ANON |
|
|
14 | # define MAP_ANONYMOUS MAP_ANON |
15 | # else |
15 | # else |
16 | # undef HAVE_MMAP |
16 | # undef HAVE_MMAP |
17 | # endif |
17 | # endif |
18 | # endif |
18 | # endif |
19 | #endif |
19 | #endif |
20 | |
20 | |
21 | #define MAY_FLUSH /* increases codesize */ |
21 | #define MAY_FLUSH /* increases codesize and is rarely used */ |
22 | |
22 | |
23 | #define SUB_INIT "Coro::State::initialize" |
23 | #define SUB_INIT "Coro::State::initialize" |
24 | #define UCORO_STATE "_coro_state" |
24 | #define UCORO_STATE "_coro_state" |
25 | |
25 | |
26 | /* The next macro should delcare a variable stacklevel that contains and approximation |
26 | /* The next macro should declare a variable stacklevel that contains and approximation |
27 | * to the current C stack pointer. Its property is that it changes with each call |
27 | * to the current C stack pointer. Its property is that it changes with each call |
28 | * and should be unique. */ |
28 | * and should be unique. */ |
29 | #define dSTACKLEVEL void *stacklevel = &stacklevel |
29 | #define dSTACKLEVEL void *stacklevel = &stacklevel |
|
|
30 | |
|
|
31 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
30 | |
32 | |
31 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
33 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
32 | |
34 | |
33 | #include "CoroAPI.h" |
35 | #include "CoroAPI.h" |
34 | |
36 | |
… | |
… | |
85 | I32 savestack_max; |
87 | I32 savestack_max; |
86 | OP **retstack; |
88 | OP **retstack; |
87 | I32 retstack_ix; |
89 | I32 retstack_ix; |
88 | I32 retstack_max; |
90 | I32 retstack_max; |
89 | COP *curcop; |
91 | COP *curcop; |
|
|
92 | PMOP *curpm; |
90 | JMPENV *top_env; |
93 | JMPENV *top_env; |
91 | |
94 | |
92 | /* data associated with this coroutine (initial args) */ |
95 | /* data associated with this coroutine (initial args) */ |
93 | AV *args; |
96 | AV *args; |
94 | }; |
97 | }; |
… | |
… | |
99 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
102 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
100 | static HV *coro_state_stash; |
103 | static HV *coro_state_stash; |
101 | static SV *ucoro_state_sv; |
104 | static SV *ucoro_state_sv; |
102 | static U32 ucoro_state_hash; |
105 | static U32 ucoro_state_hash; |
103 | static HV *padlist_cache; |
106 | static HV *padlist_cache; |
|
|
107 | static SV *coro_mortal; /* will be freed after next transfer */ |
104 | |
108 | |
105 | /* mostly copied from op.c:cv_clone2 */ |
109 | /* mostly copied from op.c:cv_clone2 */ |
106 | STATIC AV * |
110 | STATIC AV * |
107 | clone_padlist (AV *protopadlist) |
111 | clone_padlist (AV *protopadlist) |
108 | { |
112 | { |
… | |
… | |
195 | |
199 | |
196 | return newpadlist; |
200 | return newpadlist; |
197 | } |
201 | } |
198 | |
202 | |
199 | #ifdef MAY_FLUSH |
203 | #ifdef MAY_FLUSH |
200 | STATIC AV * |
204 | STATIC void |
201 | free_padlist (AV *padlist) |
205 | free_padlist (AV *padlist) |
202 | { |
206 | { |
203 | /* may be during global destruction */ |
207 | /* may be during global destruction */ |
204 | if (SvREFCNT(padlist)) |
208 | if (SvREFCNT(padlist)) |
205 | { |
209 | { |
… | |
… | |
270 | #endif |
274 | #endif |
271 | |
275 | |
272 | #define SB do { |
276 | #define SB do { |
273 | #define SE } while (0) |
277 | #define SE } while (0) |
274 | |
278 | |
275 | #define LOAD(state) SB load_state(aTHX_ (state)); SPAGAIN; SE |
279 | #define LOAD(state) load_state(aTHX_ (state)); |
276 | #define SAVE(state,flags) SB PUTBACK; save_state(aTHX_ (state),(flags)); SE |
280 | #define SAVE(state,flags) save_state(aTHX_ (state),(flags)); |
277 | |
281 | |
278 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE |
282 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE |
279 | |
283 | |
280 | static void |
284 | static void |
281 | load_state(pTHX_ Coro__State c) |
285 | load_state(pTHX_ Coro__State c) |
… | |
… | |
355 | |
359 | |
356 | PUSHs (Nullsv); |
360 | PUSHs (Nullsv); |
357 | /* this loop was inspired by pp_caller */ |
361 | /* this loop was inspired by pp_caller */ |
358 | for (;;) |
362 | for (;;) |
359 | { |
363 | { |
360 | do |
364 | while (cxix >= 0) |
361 | { |
365 | { |
362 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
366 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
363 | |
367 | |
364 | if (CxTYPE(cx) == CXt_SUB) |
368 | if (CxTYPE(cx) == CXt_SUB) |
365 | { |
369 | { |
… | |
… | |
391 | /* I never used formats, so how should I know how these are implemented? */ |
395 | /* I never used formats, so how should I know how these are implemented? */ |
392 | /* my bold guess is as a simple, plain sub... */ |
396 | /* my bold guess is as a simple, plain sub... */ |
393 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
397 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
394 | } |
398 | } |
395 | } |
399 | } |
396 | while (cxix >= 0); |
|
|
397 | |
400 | |
398 | if (top_si->si_type == PERLSI_MAIN) |
401 | if (top_si->si_type == PERLSI_MAIN) |
399 | break; |
402 | break; |
400 | |
403 | |
401 | top_si = top_si->si_prev; |
404 | top_si = top_si->si_prev; |
… | |
… | |
407 | } |
410 | } |
408 | |
411 | |
409 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
412 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
410 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
413 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
411 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
414 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
412 | |
|
|
413 | /* I have not the slightest idea of why av_reify is necessary */ |
|
|
414 | /* but if it's missing the defav contents magically get replaced sometimes */ |
|
|
415 | if (c->defav) |
|
|
416 | av_reify (c->defav); |
|
|
417 | |
415 | |
418 | c->dowarn = PL_dowarn; |
416 | c->dowarn = PL_dowarn; |
419 | c->in_eval = PL_in_eval; |
417 | c->in_eval = PL_in_eval; |
420 | |
418 | |
421 | c->curstackinfo = PL_curstackinfo; |
419 | c->curstackinfo = PL_curstackinfo; |
… | |
… | |
493 | * still there is a memleak of 128 bytes... |
491 | * still there is a memleak of 128 bytes... |
494 | */ |
492 | */ |
495 | STATIC void |
493 | STATIC void |
496 | destroy_stacks(pTHX) |
494 | destroy_stacks(pTHX) |
497 | { |
495 | { |
|
|
496 | if (!IN_DESTRUCT) |
|
|
497 | { |
498 | /* is this ugly, I ask? */ |
498 | /* is this ugly, I ask? */ |
499 | while (PL_scopestack_ix) |
499 | while (PL_scopestack_ix) |
500 | LEAVE; |
500 | LEAVE; |
501 | |
501 | |
502 | /* sure it is, but more important: is it correct?? :/ */ |
502 | /* sure it is, but more important: is it correct?? :/ */ |
503 | while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ |
503 | while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ |
504 | FREETMPS; |
504 | FREETMPS; |
|
|
505 | } |
505 | |
506 | |
506 | while (PL_curstackinfo->si_next) |
507 | while (PL_curstackinfo->si_next) |
507 | PL_curstackinfo = PL_curstackinfo->si_next; |
508 | PL_curstackinfo = PL_curstackinfo->si_next; |
508 | |
509 | |
509 | while (PL_curstackinfo) |
510 | while (PL_curstackinfo) |
… | |
… | |
514 | dSP; |
515 | dSP; |
515 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
516 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
516 | PUTBACK; /* possibly superfluous */ |
517 | PUTBACK; /* possibly superfluous */ |
517 | } |
518 | } |
518 | |
519 | |
|
|
520 | if (!IN_DESTRUCT) |
|
|
521 | { |
519 | dounwind(-1); |
522 | dounwind(-1); |
520 | |
|
|
521 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
523 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
|
|
524 | } |
|
|
525 | |
522 | Safefree(PL_curstackinfo->si_cxstack); |
526 | Safefree(PL_curstackinfo->si_cxstack); |
523 | Safefree(PL_curstackinfo); |
527 | Safefree(PL_curstackinfo); |
524 | PL_curstackinfo = p; |
528 | PL_curstackinfo = p; |
525 | } |
529 | } |
526 | |
530 | |
… | |
… | |
541 | stack->refcnt = 1; |
545 | stack->refcnt = 1; |
542 | stack->usecnt = 1; |
546 | stack->usecnt = 1; |
543 | stack->gencnt = ctx->gencnt = 0; |
547 | stack->gencnt = ctx->gencnt = 0; |
544 | if (alloc) |
548 | if (alloc) |
545 | { |
549 | { |
546 | #ifdef HAVE_MMAP |
550 | #if HAVE_MMAP |
547 | stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ |
551 | stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ |
548 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); |
552 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
549 | if (stack->sptr == (void *)-1) |
553 | if (stack->sptr == (void *)-1) |
550 | #endif |
554 | #endif |
551 | { |
555 | { |
552 | /*FIXME*//*D*//* reasonable stack size! */ |
556 | /*FIXME*//*D*//* reasonable stack size! */ |
553 | stack->ssize = -4096 * sizeof (long); |
557 | stack->ssize = -4096 * sizeof (long); |
… | |
… | |
594 | dSP; |
598 | dSP; |
595 | Coro__State ctx = (Coro__State)arg; |
599 | Coro__State ctx = (Coro__State)arg; |
596 | SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); |
600 | SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); |
597 | |
601 | |
598 | coro_init_stacks (aTHX); |
602 | coro_init_stacks (aTHX); |
|
|
603 | PL_curpm = 0; /* segfault on first access */ |
599 | /*PL_curcop = 0;*/ |
604 | /*PL_curcop = 0;*/ |
600 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
605 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
601 | SvREFCNT_dec (GvAV (PL_defgv)); |
606 | SvREFCNT_dec (GvAV (PL_defgv)); |
602 | GvAV (PL_defgv) = ctx->args; |
607 | GvAV (PL_defgv) = ctx->args; |
603 | |
608 | |
… | |
… | |
648 | { |
653 | { |
649 | /* |
654 | /* |
650 | * this is a _very_ stripped down perl interpreter ;) |
655 | * this is a _very_ stripped down perl interpreter ;) |
651 | */ |
656 | */ |
652 | Coro__State ctx = (Coro__State)arg; |
657 | Coro__State ctx = (Coro__State)arg; |
|
|
658 | JMPENV coro_start_env; |
653 | |
659 | |
654 | /*FIXME*//* must set up top_env here */ |
660 | /* same as JMPENV_BOOTSTRAP */ |
|
|
661 | Zero(&coro_start_env, 1, JMPENV); |
|
|
662 | coro_start_env.je_ret = -1; |
|
|
663 | coro_start_env.je_mustcatch = TRUE; |
|
|
664 | PL_top_env = &coro_start_env; |
|
|
665 | |
655 | ctx->cursp = 0; |
666 | ctx->cursp = 0; |
656 | PL_op = PL_op->op_next; |
667 | PL_op = PL_op->op_next; |
657 | CALLRUNOPS(aTHX); |
668 | CALLRUNOPS(aTHX); |
658 | |
669 | |
659 | abort (); |
670 | abort (); |
660 | } |
671 | } |
661 | |
672 | |
662 | STATIC void |
673 | STATIC void |
663 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
674 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
664 | { |
675 | { |
665 | dSP; |
|
|
666 | dSTACKLEVEL; |
676 | dSTACKLEVEL; |
|
|
677 | static struct coro *xnext; |
667 | |
678 | |
668 | if (prev != next) |
679 | if (prev != next) |
669 | { |
680 | { |
|
|
681 | xnext = next; |
|
|
682 | |
670 | if (next->mainstack) |
683 | if (next->mainstack) |
671 | { |
684 | { |
672 | SAVE (prev, flags); |
685 | SAVE (prev, flags); |
673 | LOAD (next); |
686 | LOAD (next); |
674 | |
687 | |
… | |
… | |
697 | continue_coro, (void *)next, |
710 | continue_coro, (void *)next, |
698 | next->stack->sptr, labs (next->stack->ssize)); |
711 | next->stack->sptr, labs (next->stack->ssize)); |
699 | } |
712 | } |
700 | |
713 | |
701 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
714 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
715 | /* don't add any code here */ |
702 | } |
716 | } |
703 | |
717 | |
704 | } |
718 | } |
705 | else if (next->tmps_ix == -2) |
719 | else if (next->tmps_ix == -2) |
706 | croak ("tried to transfer to running coroutine"); |
720 | croak ("tried to transfer to running coroutine"); |
… | |
… | |
727 | allocate_stack (next, 1); |
741 | allocate_stack (next, 1); |
728 | coro_create (&(next->stack->cctx), |
742 | coro_create (&(next->stack->cctx), |
729 | setup_coro, (void *)next, |
743 | setup_coro, (void *)next, |
730 | next->stack->sptr, labs (next->stack->ssize)); |
744 | next->stack->sptr, labs (next->stack->ssize)); |
731 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
745 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
746 | /* don't add any code here */ |
732 | } |
747 | } |
733 | } |
748 | } |
734 | else |
749 | else |
735 | setup_coro (next); |
750 | setup_coro (next); |
736 | } |
751 | } |
737 | } |
|
|
738 | |
752 | |
|
|
753 | /* |
|
|
754 | * xnext is now either prev or next, depending on wether |
|
|
755 | * we switched the c stack or not. that's why i use a global |
|
|
756 | * variable, that should become thread-specific at one point. |
|
|
757 | */ |
739 | next->cursp = stacklevel; |
758 | xnext->cursp = stacklevel; |
740 | } |
|
|
741 | |
|
|
742 | static struct coro * |
|
|
743 | sv_to_coro (SV *arg, const char *funcname, const char *varname) |
|
|
744 | { |
|
|
745 | if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV) |
|
|
746 | { |
759 | } |
|
|
760 | |
|
|
761 | if (coro_mortal) |
|
|
762 | { |
|
|
763 | SvREFCNT_dec (coro_mortal); |
|
|
764 | coro_mortal = 0; |
|
|
765 | } |
|
|
766 | } |
|
|
767 | |
|
|
768 | #define SV_CORO(sv,func) \ |
|
|
769 | do { \ |
|
|
770 | if (SvROK (sv)) \ |
|
|
771 | sv = SvRV (sv); \ |
|
|
772 | \ |
|
|
773 | if (SvTYPE(sv) == SVt_PVHV) \ |
|
|
774 | { \ |
747 | HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash); |
775 | HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ |
748 | |
776 | \ |
749 | if (!he) |
777 | if (!he) \ |
750 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname); |
778 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ |
751 | |
779 | \ |
752 | arg = HeVAL(he); |
780 | (sv) = SvRV (HeVAL(he)); \ |
753 | } |
781 | } \ |
754 | |
782 | \ |
755 | /* must also be changed inside Coro::Cont::yield */ |
783 | /* must also be changed inside Coro::Cont::yield */ \ |
756 | if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) |
784 | if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \ |
757 | return (struct coro *) SvIV((SV*)SvRV(arg)); |
|
|
758 | else |
|
|
759 | croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname); |
785 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
760 | } |
786 | \ |
|
|
787 | } while(0) |
|
|
788 | |
|
|
789 | #define SvSTATE(sv) (struct coro *)SvIV (sv) |
761 | |
790 | |
762 | static void |
791 | static void |
763 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
792 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
764 | { |
793 | { |
765 | transfer(aTHX_ sv_to_coro (prev, "Coro::transfer", "prev"), |
794 | SV_CORO (prev, "Coro::transfer"); |
766 | sv_to_coro (next, "Coro::transfer", "next"), |
795 | SV_CORO (next, "Coro::transfer"); |
767 | flags); |
796 | |
|
|
797 | transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); |
768 | } |
798 | } |
769 | |
799 | |
770 | /** Coro ********************************************************************/ |
800 | /** Coro ********************************************************************/ |
771 | |
801 | |
772 | #define PRIO_MAX 3 |
802 | #define PRIO_MAX 3 |
… | |
… | |
777 | #define PRIO_MIN -4 |
807 | #define PRIO_MIN -4 |
778 | |
808 | |
779 | /* for Coro.pm */ |
809 | /* for Coro.pm */ |
780 | static GV *coro_current, *coro_idle; |
810 | static GV *coro_current, *coro_idle; |
781 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
811 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
|
|
812 | static int coro_nready; |
782 | |
813 | |
783 | static void |
814 | static void |
784 | coro_enq (SV *sv) |
815 | coro_enq (SV *sv) |
785 | { |
816 | { |
786 | if (SvROK (sv)) |
|
|
787 | { |
|
|
788 | SV *hv = SvRV (sv); |
|
|
789 | if (SvTYPE (hv) == SVt_PVHV) |
817 | if (SvTYPE (sv) == SVt_PVHV) |
790 | { |
818 | { |
791 | SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0); |
819 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
792 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
820 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
793 | |
821 | |
794 | prio = prio > PRIO_MAX ? PRIO_MAX |
822 | prio = prio > PRIO_MAX ? PRIO_MAX |
795 | : prio < PRIO_MIN ? PRIO_MIN |
823 | : prio < PRIO_MIN ? PRIO_MIN |
796 | : prio; |
824 | : prio; |
797 | |
825 | |
798 | av_push (coro_ready [prio - PRIO_MIN], sv); |
826 | av_push (coro_ready [prio - PRIO_MIN], sv); |
|
|
827 | coro_nready++; |
799 | |
828 | |
800 | return; |
829 | return; |
801 | } |
|
|
802 | } |
830 | } |
803 | |
831 | |
804 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
832 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
805 | } |
833 | } |
806 | |
834 | |
… | |
… | |
813 | if (min_prio < 0) |
841 | if (min_prio < 0) |
814 | min_prio = 0; |
842 | min_prio = 0; |
815 | |
843 | |
816 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
844 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
817 | if (av_len (coro_ready[prio]) >= 0) |
845 | if (av_len (coro_ready[prio]) >= 0) |
|
|
846 | { |
|
|
847 | coro_nready--; |
818 | return av_shift (coro_ready[prio]); |
848 | return av_shift (coro_ready[prio]); |
|
|
849 | } |
819 | |
850 | |
820 | return 0; |
851 | return 0; |
821 | } |
852 | } |
822 | |
853 | |
823 | static void |
854 | static void |
824 | api_ready (SV *coro) |
855 | api_ready (SV *coro) |
825 | { |
856 | { |
|
|
857 | if (SvROK (coro)) |
|
|
858 | coro = SvRV (coro); |
|
|
859 | |
826 | coro_enq (SvREFCNT_inc (coro)); |
860 | coro_enq (SvREFCNT_inc (coro)); |
827 | } |
861 | } |
828 | |
862 | |
829 | static void |
863 | static void |
830 | api_schedule (int cede) |
864 | api_schedule (void) |
831 | { |
865 | { |
832 | SV *prev, *next; |
866 | SV *prev, *next; |
833 | |
867 | |
834 | prev = GvSV (coro_current); |
868 | prev = SvRV (GvSV (coro_current)); |
835 | |
|
|
836 | if (cede) |
|
|
837 | coro_enq (SvREFCNT_inc (prev)); |
|
|
838 | |
|
|
839 | next = coro_deq (PRIO_MIN); |
869 | next = coro_deq (PRIO_MIN); |
840 | |
870 | |
841 | if (!next) |
871 | if (!next) |
842 | next = SvREFCNT_inc (GvSV (coro_idle)); |
872 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
843 | |
873 | |
844 | GvSV (coro_current) = SvREFCNT_inc (next); |
874 | /* free this only after the transfer */ |
845 | transfer (sv_to_coro (prev, "Coro::schedule", "current coroutine"), |
875 | coro_mortal = prev; |
846 | sv_to_coro (next, "Coro::schedule", "next coroutine"), |
876 | SV_CORO (prev, "Coro::schedule"); |
|
|
877 | |
|
|
878 | SvRV (GvSV (coro_current)) = next; |
|
|
879 | |
|
|
880 | SV_CORO (next, "Coro::schedule"); |
|
|
881 | |
|
|
882 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), |
847 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
883 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
848 | SvREFCNT_dec (next); |
884 | } |
849 | SvREFCNT_dec (prev); |
885 | |
|
|
886 | static void |
|
|
887 | api_cede (void) |
|
|
888 | { |
|
|
889 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
|
|
890 | |
|
|
891 | api_schedule (); |
850 | } |
892 | } |
851 | |
893 | |
852 | MODULE = Coro::State PACKAGE = Coro::State |
894 | MODULE = Coro::State PACKAGE = Coro::State |
853 | |
895 | |
854 | PROTOTYPES: ENABLE |
896 | PROTOTYPES: ENABLE |
… | |
… | |
860 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
902 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
861 | |
903 | |
862 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
904 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
863 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
905 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
864 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
906 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
|
|
907 | newCONSTSUB (coro_state_stash, "SAVE_CURPM", newSViv (TRANSFER_SAVE_CURPM)); |
865 | newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); |
908 | newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); |
866 | |
909 | |
867 | if (!padlist_cache) |
910 | if (!padlist_cache) |
868 | padlist_cache = newHV (); |
911 | padlist_cache = newHV (); |
869 | |
912 | |
870 | main_mainstack = PL_mainstack; |
913 | main_mainstack = PL_mainstack; |
871 | |
914 | |
872 | { |
|
|
873 | SV *sv = perl_get_sv("Coro::API", 1); |
|
|
874 | |
|
|
875 | coroapi.ver = CORO_API_VERSION - 1; |
915 | coroapi.ver = CORO_API_VERSION; |
876 | coroapi.transfer = api_transfer; |
916 | coroapi.transfer = api_transfer; |
877 | coroapi.schedule = api_schedule; |
|
|
878 | coroapi.ready = api_ready; |
|
|
879 | |
|
|
880 | GCoroAPI = &coroapi; |
|
|
881 | sv_setiv(sv, (IV)&coroapi); |
|
|
882 | SvREADONLY_on(sv); |
|
|
883 | } |
|
|
884 | } |
917 | } |
885 | |
918 | |
886 | Coro::State |
919 | Coro::State |
887 | _newprocess(args) |
920 | _newprocess(args) |
888 | SV * args |
921 | SV * args |
… | |
… | |
903 | OUTPUT: |
936 | OUTPUT: |
904 | RETVAL |
937 | RETVAL |
905 | |
938 | |
906 | void |
939 | void |
907 | transfer(prev, next, flags) |
940 | transfer(prev, next, flags) |
908 | Coro::State_or_hashref prev |
941 | SV *prev |
909 | Coro::State_or_hashref next |
942 | SV *next |
910 | int flags |
943 | int flags |
911 | PROTOTYPE: @ |
944 | PROTOTYPE: @ |
912 | CODE: |
945 | CODE: |
913 | PUTBACK; |
946 | PUTBACK; |
|
|
947 | SV_CORO (next, "Coro::transfer"); |
|
|
948 | SV_CORO (prev, "Coro::transfer"); |
914 | transfer (aTHX_ prev, next, flags); |
949 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
915 | SPAGAIN; |
950 | SPAGAIN; |
916 | |
951 | |
917 | void |
952 | void |
918 | DESTROY(coro) |
953 | DESTROY(coro) |
919 | Coro::State coro |
954 | Coro::State coro |
… | |
… | |
921 | |
956 | |
922 | if (coro->mainstack && coro->mainstack != main_mainstack) |
957 | if (coro->mainstack && coro->mainstack != main_mainstack) |
923 | { |
958 | { |
924 | struct coro temp; |
959 | struct coro temp; |
925 | |
960 | |
|
|
961 | PUTBACK; |
926 | SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
962 | SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
927 | LOAD(aTHX_ coro); |
963 | LOAD(aTHX_ coro); |
|
|
964 | SPAGAIN; |
928 | |
965 | |
929 | destroy_stacks (aTHX); |
966 | destroy_stacks (aTHX); |
930 | |
967 | |
931 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
968 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
|
|
969 | SPAGAIN; |
932 | |
970 | |
933 | coro->mainstack = 0; |
971 | coro->mainstack = 0; |
934 | } |
972 | } |
935 | |
973 | |
936 | deallocate_stack (coro); |
974 | deallocate_stack (coro); |
… | |
… | |
1006 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
1044 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
1007 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
1045 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
1008 | |
1046 | |
1009 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1047 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1010 | coro_ready[i] = newAV (); |
1048 | coro_ready[i] = newAV (); |
|
|
1049 | |
|
|
1050 | { |
|
|
1051 | SV *sv = perl_get_sv("Coro::API", 1); |
|
|
1052 | |
|
|
1053 | coroapi.schedule = api_schedule; |
|
|
1054 | coroapi.cede = api_cede; |
|
|
1055 | coroapi.ready = api_ready; |
|
|
1056 | coroapi.nready = &coro_nready; |
|
|
1057 | coroapi.current = coro_current; |
|
|
1058 | |
|
|
1059 | GCoroAPI = &coroapi; |
|
|
1060 | sv_setiv(sv, (IV)&coroapi); |
|
|
1061 | SvREADONLY_on(sv); |
|
|
1062 | } |
1011 | } |
1063 | } |
1012 | |
1064 | |
1013 | void |
1065 | void |
1014 | ready(self) |
1066 | ready(self) |
1015 | SV * self |
1067 | SV * self |
|
|
1068 | PROTOTYPE: $ |
1016 | CODE: |
1069 | CODE: |
1017 | api_ready (self); |
1070 | api_ready (self); |
1018 | |
1071 | |
|
|
1072 | int |
|
|
1073 | nready(...) |
|
|
1074 | PROTOTYPE: |
|
|
1075 | CODE: |
|
|
1076 | RETVAL = coro_nready; |
|
|
1077 | OUTPUT: |
|
|
1078 | RETVAL |
|
|
1079 | |
1019 | void |
1080 | void |
1020 | schedule(...) |
1081 | schedule(...) |
1021 | ALIAS: |
1082 | PROTOTYPE: |
1022 | cede = 1 |
|
|
1023 | CODE: |
1083 | CODE: |
1024 | api_schedule (ix); |
1084 | api_schedule (); |
1025 | |
1085 | |
|
|
1086 | void |
|
|
1087 | cede(...) |
|
|
1088 | PROTOTYPE: |
|
|
1089 | CODE: |
|
|
1090 | api_cede (); |
|
|
1091 | |