… | |
… | |
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 | { |
… | |
… | |
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 | |
519 | if (PL_main_cv != Nullcv) /* don't during destruction. hack? */ |
520 | if (!IN_DESTRUCT) |
|
|
521 | { |
520 | dounwind(-1); |
522 | dounwind(-1); |
521 | |
|
|
522 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
523 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
|
|
524 | } |
|
|
525 | |
523 | Safefree(PL_curstackinfo->si_cxstack); |
526 | Safefree(PL_curstackinfo->si_cxstack); |
524 | Safefree(PL_curstackinfo); |
527 | Safefree(PL_curstackinfo); |
525 | PL_curstackinfo = p; |
528 | PL_curstackinfo = p; |
526 | } |
529 | } |
527 | |
530 | |
… | |
… | |
542 | stack->refcnt = 1; |
545 | stack->refcnt = 1; |
543 | stack->usecnt = 1; |
546 | stack->usecnt = 1; |
544 | stack->gencnt = ctx->gencnt = 0; |
547 | stack->gencnt = ctx->gencnt = 0; |
545 | if (alloc) |
548 | if (alloc) |
546 | { |
549 | { |
547 | #ifdef HAVE_MMAP |
550 | #if HAVE_MMAP |
548 | 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 */ |
549 | 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); |
550 | if (stack->sptr == (void *)-1) |
553 | if (stack->sptr == (void *)-1) |
551 | #endif |
554 | #endif |
552 | { |
555 | { |
553 | /*FIXME*//*D*//* reasonable stack size! */ |
556 | /*FIXME*//*D*//* reasonable stack size! */ |
554 | stack->ssize = -4096 * sizeof (long); |
557 | stack->ssize = -4096 * sizeof (long); |
… | |
… | |
595 | dSP; |
598 | dSP; |
596 | Coro__State ctx = (Coro__State)arg; |
599 | Coro__State ctx = (Coro__State)arg; |
597 | SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); |
600 | SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); |
598 | |
601 | |
599 | coro_init_stacks (aTHX); |
602 | coro_init_stacks (aTHX); |
|
|
603 | PL_curpm = 0; /* segfault on first access */ |
600 | /*PL_curcop = 0;*/ |
604 | /*PL_curcop = 0;*/ |
601 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
605 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
602 | SvREFCNT_dec (GvAV (PL_defgv)); |
606 | SvREFCNT_dec (GvAV (PL_defgv)); |
603 | GvAV (PL_defgv) = ctx->args; |
607 | GvAV (PL_defgv) = ctx->args; |
604 | |
608 | |
… | |
… | |
649 | { |
653 | { |
650 | /* |
654 | /* |
651 | * this is a _very_ stripped down perl interpreter ;) |
655 | * this is a _very_ stripped down perl interpreter ;) |
652 | */ |
656 | */ |
653 | Coro__State ctx = (Coro__State)arg; |
657 | Coro__State ctx = (Coro__State)arg; |
|
|
658 | JMPENV coro_start_env; |
654 | |
659 | |
655 | /*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 | |
656 | ctx->cursp = 0; |
666 | ctx->cursp = 0; |
657 | PL_op = PL_op->op_next; |
667 | PL_op = PL_op->op_next; |
658 | CALLRUNOPS(aTHX); |
668 | CALLRUNOPS(aTHX); |
659 | |
669 | |
660 | abort (); |
670 | abort (); |
661 | } |
671 | } |
662 | |
672 | |
663 | STATIC void |
673 | STATIC void |
664 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
674 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
665 | { |
675 | { |
666 | dSP; |
|
|
667 | dSTACKLEVEL; |
676 | dSTACKLEVEL; |
|
|
677 | static struct coro *xnext; |
668 | |
678 | |
669 | if (prev != next) |
679 | if (prev != next) |
670 | { |
680 | { |
|
|
681 | xnext = next; |
|
|
682 | |
671 | if (next->mainstack) |
683 | if (next->mainstack) |
672 | { |
684 | { |
673 | SAVE (prev, flags); |
685 | SAVE (prev, flags); |
674 | LOAD (next); |
686 | LOAD (next); |
675 | |
687 | |
… | |
… | |
698 | continue_coro, (void *)next, |
710 | continue_coro, (void *)next, |
699 | next->stack->sptr, labs (next->stack->ssize)); |
711 | next->stack->sptr, labs (next->stack->ssize)); |
700 | } |
712 | } |
701 | |
713 | |
702 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
714 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
715 | /* don't add any code here */ |
703 | } |
716 | } |
704 | |
717 | |
705 | } |
718 | } |
706 | else if (next->tmps_ix == -2) |
719 | else if (next->tmps_ix == -2) |
707 | croak ("tried to transfer to running coroutine"); |
720 | croak ("tried to transfer to running coroutine"); |
… | |
… | |
728 | allocate_stack (next, 1); |
741 | allocate_stack (next, 1); |
729 | coro_create (&(next->stack->cctx), |
742 | coro_create (&(next->stack->cctx), |
730 | setup_coro, (void *)next, |
743 | setup_coro, (void *)next, |
731 | next->stack->sptr, labs (next->stack->ssize)); |
744 | next->stack->sptr, labs (next->stack->ssize)); |
732 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
745 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
746 | /* don't add any code here */ |
733 | } |
747 | } |
734 | } |
748 | } |
735 | else |
749 | else |
736 | setup_coro (next); |
750 | setup_coro (next); |
737 | } |
751 | } |
738 | } |
|
|
739 | |
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 | */ |
740 | next->cursp = stacklevel; |
758 | xnext->cursp = stacklevel; |
741 | } |
|
|
742 | |
|
|
743 | static struct coro * |
|
|
744 | sv_to_coro (SV *arg, const char *funcname, const char *varname) |
|
|
745 | { |
|
|
746 | if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV) |
|
|
747 | { |
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 | { \ |
748 | 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); \ |
749 | |
776 | \ |
750 | if (!he) |
777 | if (!he) \ |
751 | 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); \ |
752 | |
779 | \ |
753 | arg = HeVAL(he); |
780 | (sv) = SvRV (HeVAL(he)); \ |
754 | } |
781 | } \ |
755 | |
782 | \ |
756 | /* must also be changed inside Coro::Cont::yield */ |
783 | /* must also be changed inside Coro::Cont::yield */ \ |
757 | if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) |
784 | if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \ |
758 | return (struct coro *) SvIV((SV*)SvRV(arg)); |
|
|
759 | |
|
|
760 | 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); \ |
761 | /*NORETURN*/ |
786 | \ |
762 | } |
787 | } while(0) |
|
|
788 | |
|
|
789 | #define SvSTATE(sv) (struct coro *)SvIV (sv) |
763 | |
790 | |
764 | static void |
791 | static void |
765 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
792 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
766 | { |
793 | { |
767 | transfer(aTHX_ |
794 | SV_CORO (prev, "Coro::transfer"); |
768 | sv_to_coro (prev, "Coro::transfer", "prev"), |
795 | SV_CORO (next, "Coro::transfer"); |
769 | sv_to_coro (next, "Coro::transfer", "next"), |
796 | |
770 | flags); |
797 | transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); |
771 | } |
798 | } |
772 | |
799 | |
773 | /** Coro ********************************************************************/ |
800 | /** Coro ********************************************************************/ |
774 | |
801 | |
775 | #define PRIO_MAX 3 |
802 | #define PRIO_MAX 3 |
… | |
… | |
785 | static int coro_nready; |
812 | static int coro_nready; |
786 | |
813 | |
787 | static void |
814 | static void |
788 | coro_enq (SV *sv) |
815 | coro_enq (SV *sv) |
789 | { |
816 | { |
790 | if (SvROK (sv)) |
|
|
791 | { |
|
|
792 | SV *hv = SvRV (sv); |
|
|
793 | if (SvTYPE (hv) == SVt_PVHV) |
817 | if (SvTYPE (sv) == SVt_PVHV) |
794 | { |
818 | { |
795 | SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0); |
819 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
796 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
820 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
797 | |
821 | |
798 | prio = prio > PRIO_MAX ? PRIO_MAX |
822 | prio = prio > PRIO_MAX ? PRIO_MAX |
799 | : prio < PRIO_MIN ? PRIO_MIN |
823 | : prio < PRIO_MIN ? PRIO_MIN |
800 | : prio; |
824 | : prio; |
801 | |
825 | |
802 | av_push (coro_ready [prio - PRIO_MIN], sv); |
826 | av_push (coro_ready [prio - PRIO_MIN], sv); |
803 | coro_nready++; |
827 | coro_nready++; |
804 | |
828 | |
805 | return; |
829 | return; |
806 | } |
|
|
807 | } |
830 | } |
808 | |
831 | |
809 | 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"); |
810 | } |
833 | } |
811 | |
834 | |
… | |
… | |
829 | } |
852 | } |
830 | |
853 | |
831 | static void |
854 | static void |
832 | api_ready (SV *coro) |
855 | api_ready (SV *coro) |
833 | { |
856 | { |
|
|
857 | if (SvROK (coro)) |
|
|
858 | coro = SvRV (coro); |
|
|
859 | |
834 | coro_enq (SvREFCNT_inc (coro)); |
860 | coro_enq (SvREFCNT_inc (coro)); |
835 | } |
861 | } |
836 | |
862 | |
837 | static void |
863 | static void |
838 | api_schedule (int cede) |
864 | api_schedule (void) |
839 | { |
865 | { |
840 | SV *prev, *next; |
866 | SV *prev, *next; |
841 | |
867 | |
842 | prev = GvSV (coro_current); |
868 | prev = SvRV (GvSV (coro_current)); |
843 | |
|
|
844 | if (cede) |
|
|
845 | coro_enq (SvREFCNT_inc (prev)); |
|
|
846 | |
|
|
847 | next = coro_deq (PRIO_MIN); |
869 | next = coro_deq (PRIO_MIN); |
848 | |
870 | |
849 | if (!next) |
871 | if (!next) |
850 | next = SvREFCNT_inc (GvSV (coro_idle)); |
872 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
851 | |
873 | |
852 | GvSV (coro_current) = SvREFCNT_inc (next); |
874 | /* free this only after the transfer */ |
853 | transfer (aTHX_ |
875 | coro_mortal = prev; |
854 | sv_to_coro (prev, "Coro::schedule", "current coroutine"), |
876 | SV_CORO (prev, "Coro::schedule"); |
855 | sv_to_coro (next, "Coro::schedule", "next coroutine"), |
877 | |
|
|
878 | SvRV (GvSV (coro_current)) = next; |
|
|
879 | |
|
|
880 | SV_CORO (next, "Coro::schedule"); |
|
|
881 | |
|
|
882 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), |
856 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
883 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
857 | SvREFCNT_dec (next); |
884 | } |
858 | 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 (); |
859 | } |
892 | } |
860 | |
893 | |
861 | MODULE = Coro::State PACKAGE = Coro::State |
894 | MODULE = Coro::State PACKAGE = Coro::State |
862 | |
895 | |
863 | PROTOTYPES: ENABLE |
896 | PROTOTYPES: ENABLE |
… | |
… | |
869 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
902 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
870 | |
903 | |
871 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
904 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
872 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
905 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
873 | 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)); |
874 | newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); |
908 | newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); |
875 | |
909 | |
876 | if (!padlist_cache) |
910 | if (!padlist_cache) |
877 | padlist_cache = newHV (); |
911 | padlist_cache = newHV (); |
878 | |
912 | |
… | |
… | |
902 | OUTPUT: |
936 | OUTPUT: |
903 | RETVAL |
937 | RETVAL |
904 | |
938 | |
905 | void |
939 | void |
906 | transfer(prev, next, flags) |
940 | transfer(prev, next, flags) |
907 | Coro::State_or_hashref prev |
941 | SV *prev |
908 | Coro::State_or_hashref next |
942 | SV *next |
909 | int flags |
943 | int flags |
910 | PROTOTYPE: @ |
944 | PROTOTYPE: @ |
911 | CODE: |
945 | CODE: |
912 | PUTBACK; |
946 | PUTBACK; |
|
|
947 | SV_CORO (next, "Coro::transfer"); |
|
|
948 | SV_CORO (prev, "Coro::transfer"); |
913 | transfer (aTHX_ prev, next, flags); |
949 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
914 | SPAGAIN; |
950 | SPAGAIN; |
915 | |
951 | |
916 | void |
952 | void |
917 | DESTROY(coro) |
953 | DESTROY(coro) |
918 | Coro::State coro |
954 | Coro::State coro |
… | |
… | |
920 | |
956 | |
921 | if (coro->mainstack && coro->mainstack != main_mainstack) |
957 | if (coro->mainstack && coro->mainstack != main_mainstack) |
922 | { |
958 | { |
923 | struct coro temp; |
959 | struct coro temp; |
924 | |
960 | |
|
|
961 | PUTBACK; |
925 | SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
962 | SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
926 | LOAD(aTHX_ coro); |
963 | LOAD(aTHX_ coro); |
|
|
964 | SPAGAIN; |
927 | |
965 | |
928 | destroy_stacks (aTHX); |
966 | destroy_stacks (aTHX); |
929 | |
967 | |
930 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
968 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
|
|
969 | SPAGAIN; |
931 | |
970 | |
932 | coro->mainstack = 0; |
971 | coro->mainstack = 0; |
933 | } |
972 | } |
934 | |
973 | |
935 | deallocate_stack (coro); |
974 | deallocate_stack (coro); |
… | |
… | |
1010 | |
1049 | |
1011 | { |
1050 | { |
1012 | SV *sv = perl_get_sv("Coro::API", 1); |
1051 | SV *sv = perl_get_sv("Coro::API", 1); |
1013 | |
1052 | |
1014 | coroapi.schedule = api_schedule; |
1053 | coroapi.schedule = api_schedule; |
|
|
1054 | coroapi.cede = api_cede; |
1015 | coroapi.ready = api_ready; |
1055 | coroapi.ready = api_ready; |
1016 | coroapi.nready = &coro_nready; |
1056 | coroapi.nready = &coro_nready; |
1017 | coroapi.current = coro_current; |
1057 | coroapi.current = coro_current; |
1018 | |
1058 | |
1019 | GCoroAPI = &coroapi; |
1059 | GCoroAPI = &coroapi; |
… | |
… | |
1023 | } |
1063 | } |
1024 | |
1064 | |
1025 | void |
1065 | void |
1026 | ready(self) |
1066 | ready(self) |
1027 | SV * self |
1067 | SV * self |
|
|
1068 | PROTOTYPE: $ |
1028 | CODE: |
1069 | CODE: |
1029 | api_ready (self); |
1070 | api_ready (self); |
1030 | |
1071 | |
1031 | int |
1072 | int |
1032 | nready(...) |
1073 | nready(...) |
… | |
… | |
1037 | RETVAL |
1078 | RETVAL |
1038 | |
1079 | |
1039 | void |
1080 | void |
1040 | schedule(...) |
1081 | schedule(...) |
1041 | PROTOTYPE: |
1082 | PROTOTYPE: |
1042 | ALIAS: |
|
|
1043 | cede = 1 |
|
|
1044 | CODE: |
1083 | CODE: |
1045 | api_schedule (ix); |
1084 | api_schedule (); |
1046 | |
1085 | |
|
|
1086 | void |
|
|
1087 | cede(...) |
|
|
1088 | PROTOTYPE: |
|
|
1089 | CODE: |
|
|
1090 | api_cede (); |
|
|
1091 | |