… | |
… | |
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 | |
30 | |
31 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
31 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
… | |
… | |
195 | |
195 | |
196 | return newpadlist; |
196 | return newpadlist; |
197 | } |
197 | } |
198 | |
198 | |
199 | #ifdef MAY_FLUSH |
199 | #ifdef MAY_FLUSH |
200 | STATIC AV * |
200 | STATIC void |
201 | free_padlist (AV *padlist) |
201 | free_padlist (AV *padlist) |
202 | { |
202 | { |
203 | /* may be during global destruction */ |
203 | /* may be during global destruction */ |
204 | if (SvREFCNT(padlist)) |
204 | if (SvREFCNT(padlist)) |
205 | { |
205 | { |
… | |
… | |
270 | #endif |
270 | #endif |
271 | |
271 | |
272 | #define SB do { |
272 | #define SB do { |
273 | #define SE } while (0) |
273 | #define SE } while (0) |
274 | |
274 | |
275 | #define LOAD(state) SB load_state(aTHX_ (state)); SPAGAIN; SE |
275 | #define LOAD(state) load_state(aTHX_ (state)); |
276 | #define SAVE(state,flags) SB PUTBACK; save_state(aTHX_ (state),(flags)); SE |
276 | #define SAVE(state,flags) save_state(aTHX_ (state),(flags)); |
277 | |
277 | |
278 | #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 |
279 | |
279 | |
280 | static void |
280 | static void |
281 | load_state(pTHX_ Coro__State c) |
281 | load_state(pTHX_ Coro__State c) |
… | |
… | |
355 | |
355 | |
356 | PUSHs (Nullsv); |
356 | PUSHs (Nullsv); |
357 | /* this loop was inspired by pp_caller */ |
357 | /* this loop was inspired by pp_caller */ |
358 | for (;;) |
358 | for (;;) |
359 | { |
359 | { |
360 | do |
360 | while (cxix >= 0) |
361 | { |
361 | { |
362 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
362 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
363 | |
363 | |
364 | if (CxTYPE(cx) == CXt_SUB) |
364 | if (CxTYPE(cx) == CXt_SUB) |
365 | { |
365 | { |
… | |
… | |
391 | /* 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? */ |
392 | /* my bold guess is as a simple, plain sub... */ |
392 | /* my bold guess is as a simple, plain sub... */ |
393 | 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"); |
394 | } |
394 | } |
395 | } |
395 | } |
396 | while (cxix >= 0); |
|
|
397 | |
396 | |
398 | if (top_si->si_type == PERLSI_MAIN) |
397 | if (top_si->si_type == PERLSI_MAIN) |
399 | break; |
398 | break; |
400 | |
399 | |
401 | top_si = top_si->si_prev; |
400 | top_si = top_si->si_prev; |
… | |
… | |
407 | } |
406 | } |
408 | |
407 | |
409 | 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; |
410 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
409 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
411 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
410 | 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 | |
411 | |
418 | c->dowarn = PL_dowarn; |
412 | c->dowarn = PL_dowarn; |
419 | c->in_eval = PL_in_eval; |
413 | c->in_eval = PL_in_eval; |
420 | |
414 | |
421 | c->curstackinfo = PL_curstackinfo; |
415 | c->curstackinfo = PL_curstackinfo; |
… | |
… | |
493 | * still there is a memleak of 128 bytes... |
487 | * still there is a memleak of 128 bytes... |
494 | */ |
488 | */ |
495 | STATIC void |
489 | STATIC void |
496 | destroy_stacks(pTHX) |
490 | destroy_stacks(pTHX) |
497 | { |
491 | { |
|
|
492 | int destruct = PL_main_cv != Nullcv; |
|
|
493 | |
|
|
494 | if (destruct) |
|
|
495 | { |
498 | /* is this ugly, I ask? */ |
496 | /* is this ugly, I ask? */ |
499 | while (PL_scopestack_ix) |
497 | while (PL_scopestack_ix) |
500 | LEAVE; |
498 | LEAVE; |
501 | |
499 | |
502 | /* sure it is, but more important: is it correct?? :/ */ |
500 | /* sure it is, but more important: is it correct?? :/ */ |
503 | 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 */ |
504 | FREETMPS; |
502 | FREETMPS; |
|
|
503 | } |
505 | |
504 | |
506 | while (PL_curstackinfo->si_next) |
505 | while (PL_curstackinfo->si_next) |
507 | PL_curstackinfo = PL_curstackinfo->si_next; |
506 | PL_curstackinfo = PL_curstackinfo->si_next; |
508 | |
507 | |
509 | while (PL_curstackinfo) |
508 | while (PL_curstackinfo) |
… | |
… | |
514 | dSP; |
513 | dSP; |
515 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
514 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
516 | PUTBACK; /* possibly superfluous */ |
515 | PUTBACK; /* possibly superfluous */ |
517 | } |
516 | } |
518 | |
517 | |
519 | if (PL_main_cv != Nullcv) /* don't during destruction. hack? */ |
518 | if (destruct) |
|
|
519 | { |
520 | dounwind(-1); |
520 | dounwind(-1); |
521 | |
|
|
522 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
521 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
|
|
522 | } |
|
|
523 | |
523 | Safefree(PL_curstackinfo->si_cxstack); |
524 | Safefree(PL_curstackinfo->si_cxstack); |
524 | Safefree(PL_curstackinfo); |
525 | Safefree(PL_curstackinfo); |
525 | PL_curstackinfo = p; |
526 | PL_curstackinfo = p; |
526 | } |
527 | } |
527 | |
528 | |
… | |
… | |
661 | } |
662 | } |
662 | |
663 | |
663 | STATIC void |
664 | STATIC void |
664 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
665 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
665 | { |
666 | { |
666 | dSP; |
|
|
667 | dSTACKLEVEL; |
667 | dSTACKLEVEL; |
|
|
668 | static struct coro *xnext; |
668 | |
669 | |
669 | if (prev != next) |
670 | if (prev != next) |
670 | { |
671 | { |
|
|
672 | xnext = next; |
|
|
673 | |
671 | if (next->mainstack) |
674 | if (next->mainstack) |
672 | { |
675 | { |
673 | SAVE (prev, flags); |
676 | SAVE (prev, flags); |
674 | LOAD (next); |
677 | LOAD (next); |
675 | |
678 | |
… | |
… | |
698 | continue_coro, (void *)next, |
701 | continue_coro, (void *)next, |
699 | next->stack->sptr, labs (next->stack->ssize)); |
702 | next->stack->sptr, labs (next->stack->ssize)); |
700 | } |
703 | } |
701 | |
704 | |
702 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
705 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
706 | /* don't add any code here */ |
703 | } |
707 | } |
704 | |
708 | |
705 | } |
709 | } |
706 | else if (next->tmps_ix == -2) |
710 | else if (next->tmps_ix == -2) |
707 | croak ("tried to transfer to running coroutine"); |
711 | croak ("tried to transfer to running coroutine"); |
… | |
… | |
728 | allocate_stack (next, 1); |
732 | allocate_stack (next, 1); |
729 | coro_create (&(next->stack->cctx), |
733 | coro_create (&(next->stack->cctx), |
730 | setup_coro, (void *)next, |
734 | setup_coro, (void *)next, |
731 | next->stack->sptr, labs (next->stack->ssize)); |
735 | next->stack->sptr, labs (next->stack->ssize)); |
732 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
736 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
737 | /* don't add any code here */ |
733 | } |
738 | } |
734 | } |
739 | } |
735 | else |
740 | else |
736 | setup_coro (next); |
741 | setup_coro (next); |
737 | } |
742 | } |
738 | } |
743 | } |
739 | |
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 | */ |
740 | next->cursp = stacklevel; |
750 | xnext->cursp = stacklevel; |
741 | } |
751 | } |
742 | |
752 | |
743 | static struct coro * |
753 | static struct coro * |
744 | sv_to_coro (SV *arg, const char *funcname, const char *varname) |
754 | sv_to_coro (SV *arg, const char *funcname, const char *varname) |
745 | { |
755 | { |
… | |
… | |
754 | } |
764 | } |
755 | |
765 | |
756 | /* must also be changed inside Coro::Cont::yield */ |
766 | /* must also be changed inside Coro::Cont::yield */ |
757 | if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) |
767 | if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) |
758 | return (struct coro *) SvIV((SV*)SvRV(arg)); |
768 | return (struct coro *) SvIV((SV*)SvRV(arg)); |
759 | else |
769 | |
760 | 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*/ |
761 | } |
772 | } |
762 | |
773 | |
763 | static void |
774 | static void |
764 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
775 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
765 | { |
776 | { |
|
|
777 | transfer(aTHX_ |
766 | transfer(aTHX_ sv_to_coro (prev, "Coro::transfer", "prev"), |
778 | sv_to_coro (prev, "Coro::transfer", "prev"), |
767 | sv_to_coro (next, "Coro::transfer", "next"), |
779 | sv_to_coro (next, "Coro::transfer", "next"), |
768 | flags); |
780 | flags); |
769 | } |
781 | } |
770 | |
782 | |
771 | /** Coro ********************************************************************/ |
783 | /** Coro ********************************************************************/ |
772 | |
784 | |
773 | #define PRIO_MAX 3 |
785 | #define PRIO_MAX 3 |
… | |
… | |
778 | #define PRIO_MIN -4 |
790 | #define PRIO_MIN -4 |
779 | |
791 | |
780 | /* for Coro.pm */ |
792 | /* for Coro.pm */ |
781 | static GV *coro_current, *coro_idle; |
793 | static GV *coro_current, *coro_idle; |
782 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
794 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
|
|
795 | static int coro_nready; |
783 | |
796 | |
784 | static void |
797 | static void |
785 | coro_enq (SV *sv) |
798 | coro_enq (SV *sv) |
786 | { |
799 | { |
787 | if (SvROK (sv)) |
800 | if (SvROK (sv)) |
… | |
… | |
795 | prio = prio > PRIO_MAX ? PRIO_MAX |
808 | prio = prio > PRIO_MAX ? PRIO_MAX |
796 | : prio < PRIO_MIN ? PRIO_MIN |
809 | : prio < PRIO_MIN ? PRIO_MIN |
797 | : prio; |
810 | : prio; |
798 | |
811 | |
799 | av_push (coro_ready [prio - PRIO_MIN], sv); |
812 | av_push (coro_ready [prio - PRIO_MIN], sv); |
|
|
813 | coro_nready++; |
800 | |
814 | |
801 | return; |
815 | return; |
802 | } |
816 | } |
803 | } |
817 | } |
804 | |
818 | |
… | |
… | |
814 | if (min_prio < 0) |
828 | if (min_prio < 0) |
815 | min_prio = 0; |
829 | min_prio = 0; |
816 | |
830 | |
817 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
831 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
818 | if (av_len (coro_ready[prio]) >= 0) |
832 | if (av_len (coro_ready[prio]) >= 0) |
|
|
833 | { |
|
|
834 | coro_nready--; |
819 | return av_shift (coro_ready[prio]); |
835 | return av_shift (coro_ready[prio]); |
|
|
836 | } |
820 | |
837 | |
821 | return 0; |
838 | return 0; |
822 | } |
839 | } |
823 | |
840 | |
824 | static void |
841 | static void |
… | |
… | |
841 | |
858 | |
842 | if (!next) |
859 | if (!next) |
843 | next = SvREFCNT_inc (GvSV (coro_idle)); |
860 | next = SvREFCNT_inc (GvSV (coro_idle)); |
844 | |
861 | |
845 | GvSV (coro_current) = SvREFCNT_inc (next); |
862 | GvSV (coro_current) = SvREFCNT_inc (next); |
|
|
863 | transfer (aTHX_ |
846 | transfer (sv_to_coro (prev, "Coro::schedule", "current coroutine"), |
864 | sv_to_coro (prev, "Coro::schedule", "current coroutine"), |
847 | sv_to_coro (next, "Coro::schedule", "next coroutine"), |
865 | sv_to_coro (next, "Coro::schedule", "next coroutine"), |
848 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
866 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
849 | SvREFCNT_dec (next); |
867 | SvREFCNT_dec (next); |
850 | SvREFCNT_dec (prev); |
868 | SvREFCNT_dec (prev); |
851 | } |
869 | } |
… | |
… | |
868 | if (!padlist_cache) |
886 | if (!padlist_cache) |
869 | padlist_cache = newHV (); |
887 | padlist_cache = newHV (); |
870 | |
888 | |
871 | main_mainstack = PL_mainstack; |
889 | main_mainstack = PL_mainstack; |
872 | |
890 | |
873 | { |
|
|
874 | SV *sv = perl_get_sv("Coro::API", 1); |
|
|
875 | |
|
|
876 | coroapi.ver = CORO_API_VERSION - 1; |
891 | coroapi.ver = CORO_API_VERSION; |
877 | coroapi.transfer = api_transfer; |
892 | coroapi.transfer = api_transfer; |
878 | coroapi.schedule = api_schedule; |
|
|
879 | coroapi.ready = api_ready; |
|
|
880 | |
|
|
881 | GCoroAPI = &coroapi; |
|
|
882 | sv_setiv(sv, (IV)&coroapi); |
|
|
883 | SvREADONLY_on(sv); |
|
|
884 | } |
|
|
885 | } |
893 | } |
886 | |
894 | |
887 | Coro::State |
895 | Coro::State |
888 | _newprocess(args) |
896 | _newprocess(args) |
889 | SV * args |
897 | SV * args |
… | |
… | |
922 | |
930 | |
923 | if (coro->mainstack && coro->mainstack != main_mainstack) |
931 | if (coro->mainstack && coro->mainstack != main_mainstack) |
924 | { |
932 | { |
925 | struct coro temp; |
933 | struct coro temp; |
926 | |
934 | |
|
|
935 | PUTBACK; |
927 | SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
936 | SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
928 | LOAD(aTHX_ coro); |
937 | LOAD(aTHX_ coro); |
|
|
938 | SPAGAIN; |
929 | |
939 | |
930 | destroy_stacks (aTHX); |
940 | destroy_stacks (aTHX); |
931 | |
941 | |
932 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
942 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
|
|
943 | SPAGAIN; |
933 | |
944 | |
934 | coro->mainstack = 0; |
945 | coro->mainstack = 0; |
935 | } |
946 | } |
936 | |
947 | |
937 | deallocate_stack (coro); |
948 | deallocate_stack (coro); |
… | |
… | |
1007 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
1018 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
1008 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
1019 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
1009 | |
1020 | |
1010 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1021 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1011 | coro_ready[i] = newAV (); |
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 | } |
1012 | } |
1036 | } |
1013 | |
1037 | |
1014 | void |
1038 | void |
1015 | ready(self) |
1039 | ready(self) |
1016 | SV * self |
1040 | SV * self |
1017 | CODE: |
1041 | CODE: |
1018 | api_ready (self); |
1042 | api_ready (self); |
1019 | |
1043 | |
|
|
1044 | int |
|
|
1045 | nready(...) |
|
|
1046 | PROTOTYPE: |
|
|
1047 | CODE: |
|
|
1048 | RETVAL = coro_nready; |
|
|
1049 | OUTPUT: |
|
|
1050 | RETVAL |
|
|
1051 | |
1020 | void |
1052 | void |
1021 | schedule(...) |
1053 | schedule(...) |
|
|
1054 | PROTOTYPE: |
1022 | ALIAS: |
1055 | ALIAS: |
1023 | cede = 1 |
1056 | cede = 1 |
1024 | CODE: |
1057 | CODE: |
1025 | api_schedule (ix); |
1058 | api_schedule (ix); |
1026 | |
1059 | |