… | |
… | |
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 |
… | |
… | |
68 | AV *curstack; |
68 | AV *curstack; |
69 | AV *mainstack; |
69 | AV *mainstack; |
70 | SV **stack_sp; |
70 | SV **stack_sp; |
71 | OP *op; |
71 | OP *op; |
72 | SV **curpad; |
72 | SV **curpad; |
|
|
73 | AV *comppad; |
73 | SV **stack_base; |
74 | SV **stack_base; |
74 | SV **stack_max; |
75 | SV **stack_max; |
75 | SV **tmps_stack; |
76 | SV **tmps_stack; |
76 | I32 tmps_floor; |
77 | I32 tmps_floor; |
77 | I32 tmps_ix; |
78 | I32 tmps_ix; |
… | |
… | |
101 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
102 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
102 | static HV *coro_state_stash; |
103 | static HV *coro_state_stash; |
103 | static SV *ucoro_state_sv; |
104 | static SV *ucoro_state_sv; |
104 | static U32 ucoro_state_hash; |
105 | static U32 ucoro_state_hash; |
105 | static HV *padlist_cache; |
106 | static HV *padlist_cache; |
|
|
107 | static SV *coro_mortal; /* will be freed after next transfer */ |
106 | |
108 | |
107 | /* mostly copied from op.c:cv_clone2 */ |
109 | /* mostly copied from op.c:cv_clone2 */ |
108 | STATIC AV * |
110 | STATIC AV * |
109 | clone_padlist (AV *protopadlist) |
111 | clone_padlist (AV *protopadlist) |
110 | { |
112 | { |
… | |
… | |
289 | PL_curstack = c->curstack; |
291 | PL_curstack = c->curstack; |
290 | PL_mainstack = c->mainstack; |
292 | PL_mainstack = c->mainstack; |
291 | PL_stack_sp = c->stack_sp; |
293 | PL_stack_sp = c->stack_sp; |
292 | PL_op = c->op; |
294 | PL_op = c->op; |
293 | PL_curpad = c->curpad; |
295 | PL_curpad = c->curpad; |
|
|
296 | PL_comppad = c->comppad; |
294 | PL_stack_base = c->stack_base; |
297 | PL_stack_base = c->stack_base; |
295 | PL_stack_max = c->stack_max; |
298 | PL_stack_max = c->stack_max; |
296 | PL_tmps_stack = c->tmps_stack; |
299 | PL_tmps_stack = c->tmps_stack; |
297 | PL_tmps_floor = c->tmps_floor; |
300 | PL_tmps_floor = c->tmps_floor; |
298 | PL_tmps_ix = c->tmps_ix; |
301 | PL_tmps_ix = c->tmps_ix; |
… | |
… | |
418 | c->curstack = PL_curstack; |
421 | c->curstack = PL_curstack; |
419 | c->mainstack = PL_mainstack; |
422 | c->mainstack = PL_mainstack; |
420 | c->stack_sp = PL_stack_sp; |
423 | c->stack_sp = PL_stack_sp; |
421 | c->op = PL_op; |
424 | c->op = PL_op; |
422 | c->curpad = PL_curpad; |
425 | c->curpad = PL_curpad; |
|
|
426 | c->comppad = PL_comppad; |
423 | c->stack_base = PL_stack_base; |
427 | c->stack_base = PL_stack_base; |
424 | c->stack_max = PL_stack_max; |
428 | c->stack_max = PL_stack_max; |
425 | c->tmps_stack = PL_tmps_stack; |
429 | c->tmps_stack = PL_tmps_stack; |
426 | c->tmps_floor = PL_tmps_floor; |
430 | c->tmps_floor = PL_tmps_floor; |
427 | c->tmps_ix = PL_tmps_ix; |
431 | c->tmps_ix = PL_tmps_ix; |
… | |
… | |
543 | stack->refcnt = 1; |
547 | stack->refcnt = 1; |
544 | stack->usecnt = 1; |
548 | stack->usecnt = 1; |
545 | stack->gencnt = ctx->gencnt = 0; |
549 | stack->gencnt = ctx->gencnt = 0; |
546 | if (alloc) |
550 | if (alloc) |
547 | { |
551 | { |
548 | #ifdef HAVE_MMAP |
552 | #if HAVE_MMAP |
549 | stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ |
553 | stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ |
550 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); |
554 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
551 | if (stack->sptr == (void *)-1) |
555 | if (stack->sptr == (void *)-1) |
552 | #endif |
556 | #endif |
553 | { |
557 | { |
554 | /*FIXME*//*D*//* reasonable stack size! */ |
558 | /*FIXME*//*D*//* reasonable stack size! */ |
555 | stack->ssize = -4096 * sizeof (long); |
559 | stack->ssize = -4096 * sizeof (long); |
… | |
… | |
636 | * that doesn't matter, though, since it is only |
640 | * that doesn't matter, though, since it is only |
637 | * pp_nextstate and we never return... |
641 | * pp_nextstate and we never return... |
638 | * ah yes, and I don't care anyways ;) |
642 | * ah yes, and I don't care anyways ;) |
639 | */ |
643 | */ |
640 | PUTBACK; |
644 | PUTBACK; |
641 | PL_op = pp_entersub(); |
645 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
642 | SPAGAIN; |
646 | SPAGAIN; |
643 | |
647 | |
644 | ENTER; /* necessary e.g. for dounwind */ |
648 | ENTER; /* necessary e.g. for dounwind */ |
645 | } |
649 | } |
646 | } |
650 | } |
… | |
… | |
747 | setup_coro (next); |
751 | setup_coro (next); |
748 | } |
752 | } |
749 | |
753 | |
750 | /* |
754 | /* |
751 | * xnext is now either prev or next, depending on wether |
755 | * xnext is now either prev or next, depending on wether |
752 | * we switched the c stack or not. that's why i use a global |
756 | * we switched the c stack or not. that's why I use a global |
753 | * variable, that should become thread-specific at one point. |
757 | * variable, that should become thread-specific at one point. |
754 | */ |
758 | */ |
755 | xnext->cursp = stacklevel; |
759 | xnext->cursp = stacklevel; |
756 | } |
760 | } |
757 | } |
|
|
758 | |
761 | |
759 | static struct coro * |
762 | if (coro_mortal) |
760 | sv_to_coro (SV *arg, const char *funcname, const char *varname) |
763 | { |
761 | { |
764 | SvREFCNT_dec (coro_mortal); |
762 | if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV) |
765 | coro_mortal = 0; |
763 | { |
766 | } |
|
|
767 | } |
|
|
768 | |
|
|
769 | #define SV_CORO(sv,func) \ |
|
|
770 | do { \ |
|
|
771 | if (SvROK (sv)) \ |
|
|
772 | sv = SvRV (sv); \ |
|
|
773 | \ |
|
|
774 | if (SvTYPE(sv) == SVt_PVHV) \ |
|
|
775 | { \ |
764 | HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash); |
776 | HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ |
765 | |
777 | \ |
766 | if (!he) |
778 | if (!he) \ |
767 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname); |
779 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ |
768 | |
780 | \ |
769 | arg = HeVAL(he); |
781 | (sv) = SvRV (HeVAL(he)); \ |
770 | } |
782 | } \ |
771 | |
783 | \ |
772 | /* must also be changed inside Coro::Cont::yield */ |
784 | /* must also be changed inside Coro::Cont::yield */ \ |
773 | if (SvROK(arg) && SvOBJECT(SvRV(arg)) |
785 | if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \ |
774 | && SvSTASH(SvRV(arg)) == coro_state_stash) |
|
|
775 | return (struct coro *) SvIV((SV*)SvRV(arg)); |
|
|
776 | |
|
|
777 | croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname); |
786 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
778 | /*NORETURN*/ |
787 | \ |
779 | } |
788 | } while(0) |
|
|
789 | |
|
|
790 | #define SvSTATE(sv) (struct coro *)SvIV (sv) |
780 | |
791 | |
781 | static void |
792 | static void |
782 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
793 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
783 | { |
794 | { |
784 | transfer(aTHX_ |
795 | SV_CORO (prev, "Coro::transfer"); |
785 | sv_to_coro (prev, "Coro::transfer", "prev"), |
796 | SV_CORO (next, "Coro::transfer"); |
786 | sv_to_coro (next, "Coro::transfer", "next"), |
797 | |
787 | flags); |
798 | transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); |
788 | } |
799 | } |
789 | |
800 | |
790 | /** Coro ********************************************************************/ |
801 | /** Coro ********************************************************************/ |
791 | |
802 | |
792 | #define PRIO_MAX 3 |
803 | #define PRIO_MAX 3 |
… | |
… | |
802 | static int coro_nready; |
813 | static int coro_nready; |
803 | |
814 | |
804 | static void |
815 | static void |
805 | coro_enq (SV *sv) |
816 | coro_enq (SV *sv) |
806 | { |
817 | { |
807 | if (SvROK (sv)) |
|
|
808 | { |
|
|
809 | SV *hv = SvRV (sv); |
|
|
810 | if (SvTYPE (hv) == SVt_PVHV) |
818 | if (SvTYPE (sv) == SVt_PVHV) |
811 | { |
819 | { |
812 | SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0); |
820 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
813 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
821 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
814 | |
822 | |
815 | prio = prio > PRIO_MAX ? PRIO_MAX |
823 | prio = prio > PRIO_MAX ? PRIO_MAX |
816 | : prio < PRIO_MIN ? PRIO_MIN |
824 | : prio < PRIO_MIN ? PRIO_MIN |
817 | : prio; |
825 | : prio; |
818 | |
826 | |
819 | av_push (coro_ready [prio - PRIO_MIN], sv); |
827 | av_push (coro_ready [prio - PRIO_MIN], sv); |
820 | coro_nready++; |
828 | coro_nready++; |
821 | |
829 | |
822 | return; |
830 | return; |
823 | } |
|
|
824 | } |
831 | } |
825 | |
832 | |
826 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
833 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
827 | } |
834 | } |
828 | |
835 | |
… | |
… | |
846 | } |
853 | } |
847 | |
854 | |
848 | static void |
855 | static void |
849 | api_ready (SV *coro) |
856 | api_ready (SV *coro) |
850 | { |
857 | { |
|
|
858 | if (SvROK (coro)) |
|
|
859 | coro = SvRV (coro); |
|
|
860 | |
851 | coro_enq (SvREFCNT_inc (coro)); |
861 | coro_enq (SvREFCNT_inc (coro)); |
852 | } |
862 | } |
853 | |
863 | |
854 | static void |
864 | static void |
855 | api_schedule (int cede) |
865 | api_schedule (void) |
856 | { |
866 | { |
857 | SV *prev, *next; |
867 | SV *prev, *next; |
858 | |
868 | |
859 | prev = GvSV (coro_current); |
869 | prev = SvRV (GvSV (coro_current)); |
860 | |
|
|
861 | if (cede) |
|
|
862 | coro_enq (SvREFCNT_inc (prev)); |
|
|
863 | |
|
|
864 | next = coro_deq (PRIO_MIN); |
870 | next = coro_deq (PRIO_MIN); |
865 | |
871 | |
866 | if (!next) |
872 | if (!next) |
867 | next = SvREFCNT_inc (GvSV (coro_idle)); |
873 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
868 | |
874 | |
869 | GvSV (coro_current) = SvREFCNT_inc (next); |
875 | /* free this only after the transfer */ |
870 | transfer (aTHX_ |
876 | coro_mortal = prev; |
871 | sv_to_coro (prev, "Coro::schedule", "current coroutine"), |
877 | SV_CORO (prev, "Coro::schedule"); |
872 | sv_to_coro (next, "Coro::schedule", "next coroutine"), |
878 | |
|
|
879 | SvRV (GvSV (coro_current)) = next; |
|
|
880 | |
|
|
881 | SV_CORO (next, "Coro::schedule"); |
|
|
882 | |
|
|
883 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), |
873 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
884 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
874 | SvREFCNT_dec (next); |
885 | } |
875 | SvREFCNT_dec (prev); |
886 | |
|
|
887 | static void |
|
|
888 | api_cede (void) |
|
|
889 | { |
|
|
890 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
|
|
891 | |
|
|
892 | api_schedule (); |
876 | } |
893 | } |
877 | |
894 | |
878 | MODULE = Coro::State PACKAGE = Coro::State |
895 | MODULE = Coro::State PACKAGE = Coro::State |
879 | |
896 | |
880 | PROTOTYPES: ENABLE |
897 | PROTOTYPES: ENABLE |
… | |
… | |
919 | OUTPUT: |
936 | OUTPUT: |
920 | RETVAL |
937 | RETVAL |
921 | |
938 | |
922 | void |
939 | void |
923 | transfer(prev, next, flags) |
940 | transfer(prev, next, flags) |
924 | Coro::State_or_hashref prev |
941 | SV *prev |
925 | Coro::State_or_hashref next |
942 | SV *next |
926 | int flags |
943 | int flags |
927 | PROTOTYPE: @ |
944 | PROTOTYPE: @ |
928 | CODE: |
945 | CODE: |
929 | PUTBACK; |
946 | PUTBACK; |
|
|
947 | SV_CORO (next, "Coro::transfer"); |
|
|
948 | SV_CORO (prev, "Coro::transfer"); |
930 | transfer (aTHX_ prev, next, flags); |
949 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
931 | SPAGAIN; |
950 | SPAGAIN; |
932 | |
951 | |
933 | void |
952 | void |
934 | DESTROY(coro) |
953 | DESTROY(coro) |
935 | Coro::State coro |
954 | Coro::State coro |
… | |
… | |
1030 | |
1049 | |
1031 | { |
1050 | { |
1032 | SV *sv = perl_get_sv("Coro::API", 1); |
1051 | SV *sv = perl_get_sv("Coro::API", 1); |
1033 | |
1052 | |
1034 | coroapi.schedule = api_schedule; |
1053 | coroapi.schedule = api_schedule; |
|
|
1054 | coroapi.cede = api_cede; |
1035 | coroapi.ready = api_ready; |
1055 | coroapi.ready = api_ready; |
1036 | coroapi.nready = &coro_nready; |
1056 | coroapi.nready = &coro_nready; |
1037 | coroapi.current = coro_current; |
1057 | coroapi.current = coro_current; |
1038 | |
1058 | |
1039 | GCoroAPI = &coroapi; |
1059 | GCoroAPI = &coroapi; |
… | |
… | |
1058 | RETVAL |
1078 | RETVAL |
1059 | |
1079 | |
1060 | void |
1080 | void |
1061 | schedule(...) |
1081 | schedule(...) |
1062 | PROTOTYPE: |
1082 | PROTOTYPE: |
1063 | ALIAS: |
|
|
1064 | cede = 1 |
|
|
1065 | CODE: |
1083 | CODE: |
1066 | api_schedule (ix); |
1084 | api_schedule (); |
1067 | |
1085 | |
|
|
1086 | void |
|
|
1087 | cede(...) |
|
|
1088 | PROTOTYPE: |
|
|
1089 | CODE: |
|
|
1090 | api_cede (); |
|
|
1091 | |