… | |
… | |
18 | # endif |
18 | # endif |
19 | #endif |
19 | #endif |
20 | |
20 | |
21 | #define MAY_FLUSH /* increases codesize */ |
21 | #define MAY_FLUSH /* increases codesize */ |
22 | |
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 | |
|
|
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 delcare 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 */ |
… | |
… | |
525 | dSP; |
514 | dSP; |
526 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
515 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
527 | PUTBACK; /* possibly superfluous */ |
516 | PUTBACK; /* possibly superfluous */ |
528 | } |
517 | } |
529 | |
518 | |
|
|
519 | if (PL_main_cv != Nullcv) /* don't during destruction. hack? */ |
530 | dounwind(-1); |
520 | dounwind(-1); |
531 | |
521 | |
532 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
522 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
533 | Safefree(PL_curstackinfo->si_cxstack); |
523 | Safefree(PL_curstackinfo->si_cxstack); |
534 | Safefree(PL_curstackinfo); |
524 | Safefree(PL_curstackinfo); |
535 | PL_curstackinfo = p; |
525 | PL_curstackinfo = p; |
… | |
… | |
751 | } |
741 | } |
752 | |
742 | |
753 | static struct coro * |
743 | static struct coro * |
754 | sv_to_coro (SV *arg, const char *funcname, const char *varname) |
744 | sv_to_coro (SV *arg, const char *funcname, const char *varname) |
755 | { |
745 | { |
756 | if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV) |
746 | if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV) |
757 | { |
747 | { |
758 | HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash); |
748 | HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash); |
759 | |
749 | |
760 | if (!he) |
750 | if (!he) |
761 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname); |
751 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname); |
762 | |
752 | |
763 | arg = HeVAL(he); |
753 | arg = HeVAL(he); |
764 | } |
754 | } |
765 | |
755 | |
766 | /* must also be changed inside Coro::Cont::yield */ |
756 | /* must also be changed inside Coro::Cont::yield */ |
767 | if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) |
757 | if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) |
768 | return (struct coro *) SvIV((SV*)SvRV(arg)); |
758 | return (struct coro *) SvIV((SV*)SvRV(arg)); |
769 | else |
759 | else |
770 | croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname); |
760 | croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname); |
|
|
761 | } |
|
|
762 | |
|
|
763 | static void |
|
|
764 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
|
|
765 | { |
|
|
766 | transfer(aTHX_ sv_to_coro (prev, "Coro::transfer", "prev"), |
|
|
767 | sv_to_coro (next, "Coro::transfer", "next"), |
|
|
768 | flags); |
771 | } |
769 | } |
772 | |
770 | |
773 | /** Coro ********************************************************************/ |
771 | /** Coro ********************************************************************/ |
774 | |
772 | |
775 | #define PRIO_MAX 3 |
773 | #define PRIO_MAX 3 |
… | |
… | |
780 | #define PRIO_MIN -4 |
778 | #define PRIO_MIN -4 |
781 | |
779 | |
782 | /* for Coro.pm */ |
780 | /* for Coro.pm */ |
783 | static GV *coro_current, *coro_idle; |
781 | static GV *coro_current, *coro_idle; |
784 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
782 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
|
|
783 | static int coro_nready; |
785 | |
784 | |
786 | static void |
785 | static void |
787 | coro_enq (SV *sv) |
786 | coro_enq (SV *sv) |
788 | { |
787 | { |
789 | if (SvROK (sv)) |
788 | if (SvROK (sv)) |
… | |
… | |
797 | prio = prio > PRIO_MAX ? PRIO_MAX |
796 | prio = prio > PRIO_MAX ? PRIO_MAX |
798 | : prio < PRIO_MIN ? PRIO_MIN |
797 | : prio < PRIO_MIN ? PRIO_MIN |
799 | : prio; |
798 | : prio; |
800 | |
799 | |
801 | av_push (coro_ready [prio - PRIO_MIN], sv); |
800 | av_push (coro_ready [prio - PRIO_MIN], sv); |
|
|
801 | coro_nready++; |
802 | |
802 | |
803 | return; |
803 | return; |
804 | } |
804 | } |
805 | } |
805 | } |
806 | |
806 | |
… | |
… | |
816 | if (min_prio < 0) |
816 | if (min_prio < 0) |
817 | min_prio = 0; |
817 | min_prio = 0; |
818 | |
818 | |
819 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
819 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
820 | if (av_len (coro_ready[prio]) >= 0) |
820 | if (av_len (coro_ready[prio]) >= 0) |
|
|
821 | { |
|
|
822 | coro_nready--; |
821 | return av_shift (coro_ready[prio]); |
823 | return av_shift (coro_ready[prio]); |
|
|
824 | } |
822 | |
825 | |
823 | return 0; |
826 | return 0; |
|
|
827 | } |
|
|
828 | |
|
|
829 | static void |
|
|
830 | api_ready (SV *coro) |
|
|
831 | { |
|
|
832 | coro_enq (SvREFCNT_inc (coro)); |
|
|
833 | } |
|
|
834 | |
|
|
835 | static void |
|
|
836 | api_schedule (int cede) |
|
|
837 | { |
|
|
838 | SV *prev, *next; |
|
|
839 | |
|
|
840 | prev = GvSV (coro_current); |
|
|
841 | |
|
|
842 | if (cede) |
|
|
843 | coro_enq (SvREFCNT_inc (prev)); |
|
|
844 | |
|
|
845 | next = coro_deq (PRIO_MIN); |
|
|
846 | |
|
|
847 | if (!next) |
|
|
848 | next = SvREFCNT_inc (GvSV (coro_idle)); |
|
|
849 | |
|
|
850 | GvSV (coro_current) = SvREFCNT_inc (next); |
|
|
851 | transfer (sv_to_coro (prev, "Coro::schedule", "current coroutine"), |
|
|
852 | sv_to_coro (next, "Coro::schedule", "next coroutine"), |
|
|
853 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
|
|
854 | SvREFCNT_dec (next); |
|
|
855 | SvREFCNT_dec (prev); |
824 | } |
856 | } |
825 | |
857 | |
826 | MODULE = Coro::State PACKAGE = Coro::State |
858 | MODULE = Coro::State PACKAGE = Coro::State |
827 | |
859 | |
828 | PROTOTYPES: ENABLE |
860 | PROTOTYPES: ENABLE |
… | |
… | |
840 | |
872 | |
841 | if (!padlist_cache) |
873 | if (!padlist_cache) |
842 | padlist_cache = newHV (); |
874 | padlist_cache = newHV (); |
843 | |
875 | |
844 | main_mainstack = PL_mainstack; |
876 | main_mainstack = PL_mainstack; |
|
|
877 | |
|
|
878 | coroapi.ver = CORO_API_VERSION; |
|
|
879 | coroapi.transfer = api_transfer; |
845 | } |
880 | } |
846 | |
881 | |
847 | Coro::State |
882 | Coro::State |
848 | _newprocess(args) |
883 | _newprocess(args) |
849 | SV * args |
884 | SV * args |
… | |
… | |
967 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
1002 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
968 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
1003 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
969 | |
1004 | |
970 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1005 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
971 | coro_ready[i] = newAV (); |
1006 | coro_ready[i] = newAV (); |
|
|
1007 | |
|
|
1008 | { |
|
|
1009 | SV *sv = perl_get_sv("Coro::API", 1); |
|
|
1010 | |
|
|
1011 | coroapi.schedule = api_schedule; |
|
|
1012 | coroapi.ready = api_ready; |
|
|
1013 | coroapi.nready = &coro_nready; |
|
|
1014 | coroapi.current = coro_current; |
|
|
1015 | |
|
|
1016 | GCoroAPI = &coroapi; |
|
|
1017 | sv_setiv(sv, (IV)&coroapi); |
|
|
1018 | SvREADONLY_on(sv); |
|
|
1019 | } |
972 | } |
1020 | } |
973 | |
1021 | |
974 | void |
1022 | void |
975 | ready(self) |
1023 | ready(self) |
976 | SV * self |
1024 | SV * self |
977 | CODE: |
1025 | CODE: |
978 | coro_enq (SvREFCNT_inc (self)); |
1026 | api_ready (self); |
|
|
1027 | |
|
|
1028 | int |
|
|
1029 | nready(...) |
|
|
1030 | PROTOTYPE: |
|
|
1031 | CODE: |
|
|
1032 | RETVAL = coro_nready; |
|
|
1033 | OUTPUT: |
|
|
1034 | RETVAL |
979 | |
1035 | |
980 | void |
1036 | void |
981 | schedule(...) |
1037 | schedule(...) |
|
|
1038 | PROTOTYPE: |
982 | ALIAS: |
1039 | ALIAS: |
983 | cede = 1 |
1040 | cede = 1 |
984 | CODE: |
1041 | CODE: |
985 | SV *prev, *next; |
1042 | api_schedule (ix); |
986 | |
1043 | |
987 | prev = GvSV (coro_current); |
|
|
988 | |
|
|
989 | if (ix) |
|
|
990 | coro_enq (SvREFCNT_inc (prev)); |
|
|
991 | |
|
|
992 | next = coro_deq (PRIO_MIN); |
|
|
993 | |
|
|
994 | if (!next) |
|
|
995 | next = SvREFCNT_inc (GvSV (coro_idle)); |
|
|
996 | |
|
|
997 | GvSV (coro_current) = SvREFCNT_inc (next); |
|
|
998 | transfer (sv_to_coro (prev, "Coro::schedule", "current coroutine"), |
|
|
999 | sv_to_coro (next, "Coro::schedule", "next coroutine"), |
|
|
1000 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
|
|
1001 | SvREFCNT_dec (next); |
|
|
1002 | SvREFCNT_dec (prev); |
|
|
1003 | |
|
|