… | |
… | |
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 | |
|
|
23 | /* perl-related */ |
|
|
24 | #define TRANSFER_SAVE_DEFAV 0x00000001 |
|
|
25 | #define TRANSFER_SAVE_DEFSV 0x00000002 |
|
|
26 | #define TRANSFER_SAVE_ERRSV 0x00000004 |
|
|
27 | /* c-related */ |
|
|
28 | #define TRANSFER_SAVE_CCTXT 0x00000008 |
|
|
29 | #ifdef CORO_LAZY_STACK |
|
|
30 | # define TRANSFER_LAZY_STACK 0x00000010 |
|
|
31 | #else |
|
|
32 | # define TRANSFER_LAZY_STACK 0x00000000 |
|
|
33 | #endif |
|
|
34 | |
|
|
35 | #define TRANSFER_SAVE_ALL (TRANSFER_SAVE_DEFAV|TRANSFER_SAVE_DEFSV \ |
|
|
36 | |TRANSFER_SAVE_ERRSV|TRANSFER_SAVE_CCTXT) |
|
|
37 | |
22 | |
38 | #define SUB_INIT "Coro::State::initialize" |
23 | #define SUB_INIT "Coro::State::initialize" |
39 | #define UCORO_STATE "_coro_state" |
24 | #define UCORO_STATE "_coro_state" |
40 | |
25 | |
41 | /* 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 |
42 | * to the current C stack pointer. It's property is that it changes with each call |
27 | * to the current C stack pointer. Its property is that it changes with each call |
43 | * and should be unique. */ |
28 | * and should be unique. */ |
44 | #define dSTACKLEVEL void *stacklevel = &stacklevel |
29 | #define dSTACKLEVEL void *stacklevel = &stacklevel |
45 | |
30 | |
46 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
31 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
|
|
32 | |
|
|
33 | #include "CoroAPI.h" |
|
|
34 | |
|
|
35 | static struct CoroAPI coroapi; |
47 | |
36 | |
48 | /* this is actually not only the c stack but also c registers etc... */ |
37 | /* this is actually not only the c stack but also c registers etc... */ |
49 | typedef struct { |
38 | typedef struct { |
50 | int refcnt; /* pointer reference counter */ |
39 | int refcnt; /* pointer reference counter */ |
51 | int usecnt; /* shared by how many coroutines */ |
40 | int usecnt; /* shared by how many coroutines */ |
… | |
… | |
111 | static HV *coro_state_stash; |
100 | static HV *coro_state_stash; |
112 | static SV *ucoro_state_sv; |
101 | static SV *ucoro_state_sv; |
113 | static U32 ucoro_state_hash; |
102 | static U32 ucoro_state_hash; |
114 | static HV *padlist_cache; |
103 | static HV *padlist_cache; |
115 | |
104 | |
116 | /* for Coro.pm */ |
|
|
117 | static GV *coro_current, *coro_idle; |
|
|
118 | static AV *coro_ready; |
|
|
119 | |
|
|
120 | /* mostly copied from op.c:cv_clone2 */ |
105 | /* mostly copied from op.c:cv_clone2 */ |
121 | STATIC AV * |
106 | STATIC AV * |
122 | clone_padlist (AV *protopadlist) |
107 | clone_padlist (AV *protopadlist) |
123 | { |
108 | { |
124 | AV *av; |
109 | AV *av; |
… | |
… | |
210 | |
195 | |
211 | return newpadlist; |
196 | return newpadlist; |
212 | } |
197 | } |
213 | |
198 | |
214 | #ifdef MAY_FLUSH |
199 | #ifdef MAY_FLUSH |
215 | STATIC AV * |
200 | STATIC void |
216 | free_padlist (AV *padlist) |
201 | free_padlist (AV *padlist) |
217 | { |
202 | { |
218 | /* may be during global destruction */ |
203 | /* may be during global destruction */ |
219 | if (SvREFCNT(padlist)) |
204 | if (SvREFCNT(padlist)) |
220 | { |
205 | { |
… | |
… | |
285 | #endif |
270 | #endif |
286 | |
271 | |
287 | #define SB do { |
272 | #define SB do { |
288 | #define SE } while (0) |
273 | #define SE } while (0) |
289 | |
274 | |
290 | #define LOAD(state) SB load_state(aTHX_ (state)); SPAGAIN; SE |
275 | #define LOAD(state) load_state(aTHX_ (state)); |
291 | #define SAVE(state,flags) SB PUTBACK; save_state(aTHX_ (state),(flags)); SE |
276 | #define SAVE(state,flags) save_state(aTHX_ (state),(flags)); |
292 | |
277 | |
293 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE |
278 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE |
294 | |
279 | |
295 | static void |
280 | static void |
296 | load_state(pTHX_ Coro__State c) |
281 | load_state(pTHX_ Coro__State c) |
… | |
… | |
370 | |
355 | |
371 | PUSHs (Nullsv); |
356 | PUSHs (Nullsv); |
372 | /* this loop was inspired by pp_caller */ |
357 | /* this loop was inspired by pp_caller */ |
373 | for (;;) |
358 | for (;;) |
374 | { |
359 | { |
375 | do |
360 | while (cxix >= 0) |
376 | { |
361 | { |
377 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
362 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
378 | |
363 | |
379 | if (CxTYPE(cx) == CXt_SUB) |
364 | if (CxTYPE(cx) == CXt_SUB) |
380 | { |
365 | { |
… | |
… | |
406 | /* I never used formats, so how should I know how these are implemented? */ |
391 | /* I never used formats, so how should I know how these are implemented? */ |
407 | /* my bold guess is as a simple, plain sub... */ |
392 | /* my bold guess is as a simple, plain sub... */ |
408 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
393 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
409 | } |
394 | } |
410 | } |
395 | } |
411 | while (cxix >= 0); |
|
|
412 | |
396 | |
413 | if (top_si->si_type == PERLSI_MAIN) |
397 | if (top_si->si_type == PERLSI_MAIN) |
414 | break; |
398 | break; |
415 | |
399 | |
416 | top_si = top_si->si_prev; |
400 | top_si = top_si->si_prev; |
… | |
… | |
422 | } |
406 | } |
423 | |
407 | |
424 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
408 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
425 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
409 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
426 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
410 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
427 | |
|
|
428 | /* I have not the slightest idea of why av_reify is necessary */ |
|
|
429 | /* but if it's missing the defav contents magically get replaced sometimes */ |
|
|
430 | if (c->defav) |
|
|
431 | av_reify (c->defav); |
|
|
432 | |
411 | |
433 | c->dowarn = PL_dowarn; |
412 | c->dowarn = PL_dowarn; |
434 | c->in_eval = PL_in_eval; |
413 | c->in_eval = PL_in_eval; |
435 | |
414 | |
436 | c->curstackinfo = PL_curstackinfo; |
415 | c->curstackinfo = PL_curstackinfo; |
… | |
… | |
508 | * still there is a memleak of 128 bytes... |
487 | * still there is a memleak of 128 bytes... |
509 | */ |
488 | */ |
510 | STATIC void |
489 | STATIC void |
511 | destroy_stacks(pTHX) |
490 | destroy_stacks(pTHX) |
512 | { |
491 | { |
|
|
492 | int destruct = PL_main_cv != Nullcv; |
|
|
493 | |
|
|
494 | if (destruct) |
|
|
495 | { |
513 | /* is this ugly, I ask? */ |
496 | /* is this ugly, I ask? */ |
514 | while (PL_scopestack_ix) |
497 | while (PL_scopestack_ix) |
515 | LEAVE; |
498 | LEAVE; |
516 | |
499 | |
517 | /* sure it is, but more important: is it correct?? :/ */ |
500 | /* sure it is, but more important: is it correct?? :/ */ |
518 | while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ |
501 | while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ |
519 | FREETMPS; |
502 | FREETMPS; |
|
|
503 | } |
520 | |
504 | |
521 | while (PL_curstackinfo->si_next) |
505 | while (PL_curstackinfo->si_next) |
522 | PL_curstackinfo = PL_curstackinfo->si_next; |
506 | PL_curstackinfo = PL_curstackinfo->si_next; |
523 | |
507 | |
524 | while (PL_curstackinfo) |
508 | while (PL_curstackinfo) |
… | |
… | |
529 | dSP; |
513 | dSP; |
530 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
514 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
531 | PUTBACK; /* possibly superfluous */ |
515 | PUTBACK; /* possibly superfluous */ |
532 | } |
516 | } |
533 | |
517 | |
|
|
518 | if (destruct) |
|
|
519 | { |
534 | dounwind(-1); |
520 | dounwind(-1); |
535 | |
|
|
536 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
521 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
|
|
522 | } |
|
|
523 | |
537 | Safefree(PL_curstackinfo->si_cxstack); |
524 | Safefree(PL_curstackinfo->si_cxstack); |
538 | Safefree(PL_curstackinfo); |
525 | Safefree(PL_curstackinfo); |
539 | PL_curstackinfo = p; |
526 | PL_curstackinfo = p; |
540 | } |
527 | } |
541 | |
528 | |
… | |
… | |
675 | } |
662 | } |
676 | |
663 | |
677 | STATIC void |
664 | STATIC void |
678 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
665 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
679 | { |
666 | { |
680 | dSP; |
|
|
681 | dSTACKLEVEL; |
667 | dSTACKLEVEL; |
|
|
668 | static struct coro *xnext; |
682 | |
669 | |
683 | if (prev != next) |
670 | if (prev != next) |
684 | { |
671 | { |
|
|
672 | xnext = next; |
|
|
673 | |
685 | if (next->mainstack) |
674 | if (next->mainstack) |
686 | { |
675 | { |
687 | SAVE (prev, flags); |
676 | SAVE (prev, flags); |
688 | LOAD (next); |
677 | LOAD (next); |
689 | |
678 | |
… | |
… | |
712 | continue_coro, (void *)next, |
701 | continue_coro, (void *)next, |
713 | next->stack->sptr, labs (next->stack->ssize)); |
702 | next->stack->sptr, labs (next->stack->ssize)); |
714 | } |
703 | } |
715 | |
704 | |
716 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
705 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
706 | /* don't add any code here */ |
717 | } |
707 | } |
718 | |
708 | |
719 | } |
709 | } |
720 | else if (next->tmps_ix == -2) |
710 | else if (next->tmps_ix == -2) |
721 | croak ("tried to transfer to running coroutine"); |
711 | croak ("tried to transfer to running coroutine"); |
… | |
… | |
742 | allocate_stack (next, 1); |
732 | allocate_stack (next, 1); |
743 | coro_create (&(next->stack->cctx), |
733 | coro_create (&(next->stack->cctx), |
744 | setup_coro, (void *)next, |
734 | setup_coro, (void *)next, |
745 | next->stack->sptr, labs (next->stack->ssize)); |
735 | next->stack->sptr, labs (next->stack->ssize)); |
746 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
736 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
737 | /* don't add any code here */ |
747 | } |
738 | } |
748 | } |
739 | } |
749 | else |
740 | else |
750 | setup_coro (next); |
741 | setup_coro (next); |
751 | } |
742 | } |
752 | } |
743 | } |
753 | |
744 | |
|
|
745 | /* |
|
|
746 | * xnext is now either prev or next, depending on wether |
|
|
747 | * we switched the c stack or not. that's why i use a global |
|
|
748 | * variable, that should become thread-specific at one point. |
|
|
749 | */ |
754 | next->cursp = stacklevel; |
750 | xnext->cursp = stacklevel; |
755 | } |
751 | } |
756 | |
752 | |
757 | static struct coro * |
753 | static struct coro * |
758 | sv_to_coro (SV *arg, const char *funcname, const char *varname) |
754 | sv_to_coro (SV *arg, const char *funcname, const char *varname) |
759 | { |
755 | { |
760 | if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV) |
756 | if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV) |
761 | { |
757 | { |
762 | HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash); |
758 | HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash); |
763 | |
759 | |
764 | if (!he) |
760 | if (!he) |
765 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname); |
761 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname); |
766 | |
762 | |
767 | arg = HeVAL(he); |
763 | arg = HeVAL(he); |
768 | } |
764 | } |
769 | |
765 | |
770 | /* must also be changed inside Coro::Cont::yield */ |
766 | /* must also be changed inside Coro::Cont::yield */ |
771 | if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) |
767 | if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) |
772 | return (struct coro *) SvIV((SV*)SvRV(arg)); |
768 | return (struct coro *) SvIV((SV*)SvRV(arg)); |
773 | else |
769 | |
774 | croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname); |
770 | croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname); |
|
|
771 | /*NORETURN*/ |
|
|
772 | } |
|
|
773 | |
|
|
774 | static void |
|
|
775 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
|
|
776 | { |
|
|
777 | transfer(aTHX_ |
|
|
778 | sv_to_coro (prev, "Coro::transfer", "prev"), |
|
|
779 | sv_to_coro (next, "Coro::transfer", "next"), |
|
|
780 | flags); |
|
|
781 | } |
|
|
782 | |
|
|
783 | /** Coro ********************************************************************/ |
|
|
784 | |
|
|
785 | #define PRIO_MAX 3 |
|
|
786 | #define PRIO_HIGH 1 |
|
|
787 | #define PRIO_NORMAL 0 |
|
|
788 | #define PRIO_LOW -1 |
|
|
789 | #define PRIO_IDLE -3 |
|
|
790 | #define PRIO_MIN -4 |
|
|
791 | |
|
|
792 | /* for Coro.pm */ |
|
|
793 | static GV *coro_current, *coro_idle; |
|
|
794 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
|
|
795 | static int coro_nready; |
|
|
796 | |
|
|
797 | static void |
|
|
798 | coro_enq (SV *sv) |
|
|
799 | { |
|
|
800 | if (SvROK (sv)) |
|
|
801 | { |
|
|
802 | SV *hv = SvRV (sv); |
|
|
803 | if (SvTYPE (hv) == SVt_PVHV) |
|
|
804 | { |
|
|
805 | SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0); |
|
|
806 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
|
|
807 | |
|
|
808 | prio = prio > PRIO_MAX ? PRIO_MAX |
|
|
809 | : prio < PRIO_MIN ? PRIO_MIN |
|
|
810 | : prio; |
|
|
811 | |
|
|
812 | av_push (coro_ready [prio - PRIO_MIN], sv); |
|
|
813 | coro_nready++; |
|
|
814 | |
|
|
815 | return; |
|
|
816 | } |
|
|
817 | } |
|
|
818 | |
|
|
819 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
|
|
820 | } |
|
|
821 | |
|
|
822 | static SV * |
|
|
823 | coro_deq (int min_prio) |
|
|
824 | { |
|
|
825 | int prio = PRIO_MAX - PRIO_MIN; |
|
|
826 | |
|
|
827 | min_prio -= PRIO_MIN; |
|
|
828 | if (min_prio < 0) |
|
|
829 | min_prio = 0; |
|
|
830 | |
|
|
831 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
|
|
832 | if (av_len (coro_ready[prio]) >= 0) |
|
|
833 | { |
|
|
834 | coro_nready--; |
|
|
835 | return av_shift (coro_ready[prio]); |
|
|
836 | } |
|
|
837 | |
|
|
838 | return 0; |
|
|
839 | } |
|
|
840 | |
|
|
841 | static void |
|
|
842 | api_ready (SV *coro) |
|
|
843 | { |
|
|
844 | coro_enq (SvREFCNT_inc (coro)); |
|
|
845 | } |
|
|
846 | |
|
|
847 | static void |
|
|
848 | api_schedule (int cede) |
|
|
849 | { |
|
|
850 | SV *prev, *next; |
|
|
851 | |
|
|
852 | prev = GvSV (coro_current); |
|
|
853 | |
|
|
854 | if (cede) |
|
|
855 | coro_enq (SvREFCNT_inc (prev)); |
|
|
856 | |
|
|
857 | next = coro_deq (PRIO_MIN); |
|
|
858 | |
|
|
859 | if (!next) |
|
|
860 | next = SvREFCNT_inc (GvSV (coro_idle)); |
|
|
861 | |
|
|
862 | GvSV (coro_current) = SvREFCNT_inc (next); |
|
|
863 | transfer (aTHX_ |
|
|
864 | sv_to_coro (prev, "Coro::schedule", "current coroutine"), |
|
|
865 | sv_to_coro (next, "Coro::schedule", "next coroutine"), |
|
|
866 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
|
|
867 | SvREFCNT_dec (next); |
|
|
868 | SvREFCNT_dec (prev); |
775 | } |
869 | } |
776 | |
870 | |
777 | MODULE = Coro::State PACKAGE = Coro::State |
871 | MODULE = Coro::State PACKAGE = Coro::State |
778 | |
872 | |
779 | PROTOTYPES: ENABLE |
873 | PROTOTYPES: ENABLE |
… | |
… | |
791 | |
885 | |
792 | if (!padlist_cache) |
886 | if (!padlist_cache) |
793 | padlist_cache = newHV (); |
887 | padlist_cache = newHV (); |
794 | |
888 | |
795 | main_mainstack = PL_mainstack; |
889 | main_mainstack = PL_mainstack; |
|
|
890 | |
|
|
891 | coroapi.ver = CORO_API_VERSION; |
|
|
892 | coroapi.transfer = api_transfer; |
796 | } |
893 | } |
797 | |
894 | |
798 | Coro::State |
895 | Coro::State |
799 | _newprocess(args) |
896 | _newprocess(args) |
800 | SV * args |
897 | SV * args |
… | |
… | |
833 | |
930 | |
834 | if (coro->mainstack && coro->mainstack != main_mainstack) |
931 | if (coro->mainstack && coro->mainstack != main_mainstack) |
835 | { |
932 | { |
836 | struct coro temp; |
933 | struct coro temp; |
837 | |
934 | |
|
|
935 | PUTBACK; |
838 | SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
936 | SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
839 | LOAD(aTHX_ coro); |
937 | LOAD(aTHX_ coro); |
|
|
938 | SPAGAIN; |
840 | |
939 | |
841 | destroy_stacks (aTHX); |
940 | destroy_stacks (aTHX); |
842 | |
941 | |
843 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
942 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
|
|
943 | SPAGAIN; |
844 | |
944 | |
845 | coro->mainstack = 0; |
945 | coro->mainstack = 0; |
846 | } |
946 | } |
847 | |
947 | |
848 | deallocate_stack (coro); |
948 | deallocate_stack (coro); |
… | |
… | |
903 | |
1003 | |
904 | # this is slightly dirty (should expose a c-level api) |
1004 | # this is slightly dirty (should expose a c-level api) |
905 | |
1005 | |
906 | BOOT: |
1006 | BOOT: |
907 | { |
1007 | { |
|
|
1008 | int i; |
|
|
1009 | HV *stash = gv_stashpv ("Coro", TRUE); |
|
|
1010 | |
|
|
1011 | newCONSTSUB (stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
|
|
1012 | newCONSTSUB (stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
|
|
1013 | newCONSTSUB (stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
|
|
1014 | newCONSTSUB (stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
|
|
1015 | newCONSTSUB (stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
|
|
1016 | newCONSTSUB (stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
|
|
1017 | |
908 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
1018 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
909 | coro_ready = newAV (); |
|
|
910 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
1019 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
|
|
1020 | |
|
|
1021 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
|
|
1022 | coro_ready[i] = newAV (); |
|
|
1023 | |
|
|
1024 | { |
|
|
1025 | SV *sv = perl_get_sv("Coro::API", 1); |
|
|
1026 | |
|
|
1027 | coroapi.schedule = api_schedule; |
|
|
1028 | coroapi.ready = api_ready; |
|
|
1029 | coroapi.nready = &coro_nready; |
|
|
1030 | coroapi.current = coro_current; |
|
|
1031 | |
|
|
1032 | GCoroAPI = &coroapi; |
|
|
1033 | sv_setiv(sv, (IV)&coroapi); |
|
|
1034 | SvREADONLY_on(sv); |
|
|
1035 | } |
911 | } |
1036 | } |
912 | |
1037 | |
913 | void |
1038 | void |
914 | ready(self) |
1039 | ready(self) |
915 | SV * self |
1040 | SV * self |
916 | CODE: |
1041 | CODE: |
917 | av_push (coro_ready, SvREFCNT_inc (self)); |
1042 | api_ready (self); |
|
|
1043 | |
|
|
1044 | int |
|
|
1045 | nready(...) |
|
|
1046 | PROTOTYPE: |
|
|
1047 | CODE: |
|
|
1048 | RETVAL = coro_nready; |
|
|
1049 | OUTPUT: |
|
|
1050 | RETVAL |
918 | |
1051 | |
919 | void |
1052 | void |
920 | schedule(...) |
1053 | schedule(...) |
|
|
1054 | PROTOTYPE: |
921 | ALIAS: |
1055 | ALIAS: |
922 | cede = 1 |
1056 | cede = 1 |
923 | CODE: |
1057 | CODE: |
924 | SV *prev = GvSV (coro_current); |
1058 | api_schedule (ix); |
925 | SV *next = av_shift (coro_ready); |
|
|
926 | |
1059 | |
927 | if (next == &PL_sv_undef) |
|
|
928 | next = SvREFCNT_inc (GvSV (coro_idle)); |
|
|
929 | |
|
|
930 | if (ix) |
|
|
931 | av_push (coro_ready, SvREFCNT_inc (prev)); |
|
|
932 | |
|
|
933 | GvSV (coro_current) = SvREFCNT_inc (next); |
|
|
934 | transfer (sv_to_coro (prev, "Coro::schedule", "current coroutine"), |
|
|
935 | sv_to_coro (next, "Coro::schedule", "next coroutine"), |
|
|
936 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
|
|
937 | SvREFCNT_dec (next); |
|
|
938 | SvREFCNT_dec (prev); |
|
|
939 | |
|
|