… | |
… | |
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 |
… | |
… | |
47 | void *sptr; |
47 | void *sptr; |
48 | long ssize; /* positive == mmap, otherwise malloc */ |
48 | long ssize; /* positive == mmap, otherwise malloc */ |
49 | } coro_stack; |
49 | } coro_stack; |
50 | |
50 | |
51 | struct coro { |
51 | struct coro { |
|
|
52 | /* the top-level JMPENV for each coroutine, needed to catch dies. */ |
|
|
53 | JMPENV start_env; |
|
|
54 | |
52 | /* the optional C context */ |
55 | /* the optional C context */ |
53 | coro_stack *stack; |
56 | coro_stack *stack; |
54 | void *cursp; |
57 | void *cursp; |
55 | int gencnt; |
58 | int gencnt; |
56 | |
59 | |
… | |
… | |
68 | AV *curstack; |
71 | AV *curstack; |
69 | AV *mainstack; |
72 | AV *mainstack; |
70 | SV **stack_sp; |
73 | SV **stack_sp; |
71 | OP *op; |
74 | OP *op; |
72 | SV **curpad; |
75 | SV **curpad; |
|
|
76 | AV *comppad; |
73 | SV **stack_base; |
77 | SV **stack_base; |
74 | SV **stack_max; |
78 | SV **stack_max; |
75 | SV **tmps_stack; |
79 | SV **tmps_stack; |
76 | I32 tmps_floor; |
80 | I32 tmps_floor; |
77 | I32 tmps_ix; |
81 | I32 tmps_ix; |
… | |
… | |
101 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
105 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
102 | static HV *coro_state_stash; |
106 | static HV *coro_state_stash; |
103 | static SV *ucoro_state_sv; |
107 | static SV *ucoro_state_sv; |
104 | static U32 ucoro_state_hash; |
108 | static U32 ucoro_state_hash; |
105 | static HV *padlist_cache; |
109 | static HV *padlist_cache; |
|
|
110 | static SV *coro_mortal; /* will be freed after next transfer */ |
106 | |
111 | |
107 | /* mostly copied from op.c:cv_clone2 */ |
112 | /* mostly copied from op.c:cv_clone2 */ |
108 | STATIC AV * |
113 | STATIC AV * |
109 | clone_padlist (AV *protopadlist) |
114 | clone_padlist (AV *protopadlist) |
110 | { |
115 | { |
… | |
… | |
289 | PL_curstack = c->curstack; |
294 | PL_curstack = c->curstack; |
290 | PL_mainstack = c->mainstack; |
295 | PL_mainstack = c->mainstack; |
291 | PL_stack_sp = c->stack_sp; |
296 | PL_stack_sp = c->stack_sp; |
292 | PL_op = c->op; |
297 | PL_op = c->op; |
293 | PL_curpad = c->curpad; |
298 | PL_curpad = c->curpad; |
|
|
299 | PL_comppad = c->comppad; |
294 | PL_stack_base = c->stack_base; |
300 | PL_stack_base = c->stack_base; |
295 | PL_stack_max = c->stack_max; |
301 | PL_stack_max = c->stack_max; |
296 | PL_tmps_stack = c->tmps_stack; |
302 | PL_tmps_stack = c->tmps_stack; |
297 | PL_tmps_floor = c->tmps_floor; |
303 | PL_tmps_floor = c->tmps_floor; |
298 | PL_tmps_ix = c->tmps_ix; |
304 | PL_tmps_ix = c->tmps_ix; |
… | |
… | |
418 | c->curstack = PL_curstack; |
424 | c->curstack = PL_curstack; |
419 | c->mainstack = PL_mainstack; |
425 | c->mainstack = PL_mainstack; |
420 | c->stack_sp = PL_stack_sp; |
426 | c->stack_sp = PL_stack_sp; |
421 | c->op = PL_op; |
427 | c->op = PL_op; |
422 | c->curpad = PL_curpad; |
428 | c->curpad = PL_curpad; |
|
|
429 | c->comppad = PL_comppad; |
423 | c->stack_base = PL_stack_base; |
430 | c->stack_base = PL_stack_base; |
424 | c->stack_max = PL_stack_max; |
431 | c->stack_max = PL_stack_max; |
425 | c->tmps_stack = PL_tmps_stack; |
432 | c->tmps_stack = PL_tmps_stack; |
426 | c->tmps_floor = PL_tmps_floor; |
433 | c->tmps_floor = PL_tmps_floor; |
427 | c->tmps_ix = PL_tmps_ix; |
434 | c->tmps_ix = PL_tmps_ix; |
… | |
… | |
543 | stack->refcnt = 1; |
550 | stack->refcnt = 1; |
544 | stack->usecnt = 1; |
551 | stack->usecnt = 1; |
545 | stack->gencnt = ctx->gencnt = 0; |
552 | stack->gencnt = ctx->gencnt = 0; |
546 | if (alloc) |
553 | if (alloc) |
547 | { |
554 | { |
548 | #ifdef HAVE_MMAP |
555 | #if HAVE_MMAP |
549 | stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ |
556 | 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); |
557 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
551 | if (stack->sptr == (void *)-1) |
558 | if (stack->sptr == (void *)-1) |
552 | #endif |
559 | #endif |
553 | { |
560 | { |
554 | /*FIXME*//*D*//* reasonable stack size! */ |
561 | /*FIXME*//*D*//* reasonable stack size! */ |
555 | stack->ssize = -4096 * sizeof (long); |
562 | stack->ssize = -4096 * sizeof (long); |
… | |
… | |
636 | * that doesn't matter, though, since it is only |
643 | * that doesn't matter, though, since it is only |
637 | * pp_nextstate and we never return... |
644 | * pp_nextstate and we never return... |
638 | * ah yes, and I don't care anyways ;) |
645 | * ah yes, and I don't care anyways ;) |
639 | */ |
646 | */ |
640 | PUTBACK; |
647 | PUTBACK; |
641 | PL_op = pp_entersub(); |
648 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
642 | SPAGAIN; |
649 | SPAGAIN; |
643 | |
650 | |
644 | ENTER; /* necessary e.g. for dounwind */ |
651 | ENTER; /* necessary e.g. for dounwind */ |
645 | } |
652 | } |
646 | } |
653 | } |
… | |
… | |
652 | * this is a _very_ stripped down perl interpreter ;) |
659 | * this is a _very_ stripped down perl interpreter ;) |
653 | */ |
660 | */ |
654 | Coro__State ctx = (Coro__State)arg; |
661 | Coro__State ctx = (Coro__State)arg; |
655 | JMPENV coro_start_env; |
662 | JMPENV coro_start_env; |
656 | |
663 | |
657 | /* same as JMPENV_BOOTSTRAP */ |
|
|
658 | Zero(&coro_start_env, 1, JMPENV); |
|
|
659 | coro_start_env.je_ret = -1; |
|
|
660 | coro_start_env.je_mustcatch = TRUE; |
|
|
661 | PL_top_env = &coro_start_env; |
664 | PL_top_env = &ctx->start_env; |
662 | |
665 | |
663 | ctx->cursp = 0; |
666 | ctx->cursp = 0; |
664 | PL_op = PL_op->op_next; |
667 | PL_op = PL_op->op_next; |
665 | CALLRUNOPS(aTHX); |
668 | CALLRUNOPS(aTHX); |
666 | |
669 | |
… | |
… | |
724 | if (!prev->stack) |
727 | if (!prev->stack) |
725 | allocate_stack (prev, 0); |
728 | allocate_stack (prev, 0); |
726 | |
729 | |
727 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
730 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
728 | { |
731 | { |
|
|
732 | PL_top_env = &next->start_env; |
|
|
733 | |
729 | setup_coro (next); |
734 | setup_coro (next); |
730 | |
735 | |
731 | prev->stack->refcnt++; |
736 | prev->stack->refcnt++; |
732 | prev->stack->usecnt++; |
737 | prev->stack->usecnt++; |
733 | next->stack = prev->stack; |
738 | next->stack = prev->stack; |
… | |
… | |
747 | setup_coro (next); |
752 | setup_coro (next); |
748 | } |
753 | } |
749 | |
754 | |
750 | /* |
755 | /* |
751 | * xnext is now either prev or next, depending on wether |
756 | * 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 |
757 | * we switched the c stack or not. that's why I use a global |
753 | * variable, that should become thread-specific at one point. |
758 | * variable, that should become thread-specific at one point. |
754 | */ |
759 | */ |
755 | xnext->cursp = stacklevel; |
760 | xnext->cursp = stacklevel; |
756 | } |
761 | } |
757 | } |
|
|
758 | |
762 | |
759 | static struct coro * |
763 | if (coro_mortal) |
760 | sv_to_coro (SV *arg, const char *funcname, const char *varname) |
764 | { |
761 | { |
765 | SvREFCNT_dec (coro_mortal); |
762 | if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV) |
766 | coro_mortal = 0; |
763 | { |
767 | } |
|
|
768 | } |
|
|
769 | |
|
|
770 | #define SV_CORO(sv,func) \ |
|
|
771 | do { \ |
|
|
772 | if (SvROK (sv)) \ |
|
|
773 | sv = SvRV (sv); \ |
|
|
774 | \ |
|
|
775 | if (SvTYPE(sv) == SVt_PVHV) \ |
|
|
776 | { \ |
764 | HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash); |
777 | HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ |
765 | |
778 | \ |
766 | if (!he) |
779 | if (!he) \ |
767 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname); |
780 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ |
768 | |
781 | \ |
769 | arg = HeVAL(he); |
782 | (sv) = SvRV (HeVAL(he)); \ |
770 | } |
783 | } \ |
771 | |
784 | \ |
772 | /* must also be changed inside Coro::Cont::yield */ |
785 | /* must also be changed inside Coro::Cont::yield */ \ |
773 | if (SvROK(arg) && SvOBJECT(SvRV(arg)) |
786 | 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); |
787 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
778 | /*NORETURN*/ |
788 | \ |
779 | } |
789 | } while(0) |
|
|
790 | |
|
|
791 | #define SvSTATE(sv) (struct coro *)SvIV (sv) |
780 | |
792 | |
781 | static void |
793 | static void |
782 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
794 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
783 | { |
795 | { |
784 | transfer(aTHX_ |
796 | SV_CORO (prev, "Coro::transfer"); |
785 | sv_to_coro (prev, "Coro::transfer", "prev"), |
797 | SV_CORO (next, "Coro::transfer"); |
786 | sv_to_coro (next, "Coro::transfer", "next"), |
798 | |
787 | flags); |
799 | transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); |
788 | } |
800 | } |
789 | |
801 | |
790 | /** Coro ********************************************************************/ |
802 | /** Coro ********************************************************************/ |
791 | |
803 | |
792 | #define PRIO_MAX 3 |
804 | #define PRIO_MAX 3 |
… | |
… | |
802 | static int coro_nready; |
814 | static int coro_nready; |
803 | |
815 | |
804 | static void |
816 | static void |
805 | coro_enq (SV *sv) |
817 | coro_enq (SV *sv) |
806 | { |
818 | { |
807 | if (SvROK (sv)) |
|
|
808 | { |
|
|
809 | SV *hv = SvRV (sv); |
|
|
810 | if (SvTYPE (hv) == SVt_PVHV) |
819 | if (SvTYPE (sv) == SVt_PVHV) |
811 | { |
820 | { |
812 | SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0); |
821 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
813 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
822 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
814 | |
823 | |
815 | prio = prio > PRIO_MAX ? PRIO_MAX |
824 | prio = prio > PRIO_MAX ? PRIO_MAX |
816 | : prio < PRIO_MIN ? PRIO_MIN |
825 | : prio < PRIO_MIN ? PRIO_MIN |
817 | : prio; |
826 | : prio; |
818 | |
827 | |
819 | av_push (coro_ready [prio - PRIO_MIN], sv); |
828 | av_push (coro_ready [prio - PRIO_MIN], sv); |
820 | coro_nready++; |
829 | coro_nready++; |
821 | |
830 | |
822 | return; |
831 | return; |
823 | } |
|
|
824 | } |
832 | } |
825 | |
833 | |
826 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
834 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
827 | } |
835 | } |
828 | |
836 | |
… | |
… | |
846 | } |
854 | } |
847 | |
855 | |
848 | static void |
856 | static void |
849 | api_ready (SV *coro) |
857 | api_ready (SV *coro) |
850 | { |
858 | { |
|
|
859 | if (SvROK (coro)) |
|
|
860 | coro = SvRV (coro); |
|
|
861 | |
851 | coro_enq (SvREFCNT_inc (coro)); |
862 | coro_enq (SvREFCNT_inc (coro)); |
852 | } |
863 | } |
853 | |
864 | |
854 | static void |
865 | static void |
855 | api_schedule (int cede) |
866 | api_schedule (void) |
856 | { |
867 | { |
857 | SV *prev, *next; |
868 | SV *prev, *next; |
858 | |
869 | |
859 | prev = GvSV (coro_current); |
870 | prev = SvRV (GvSV (coro_current)); |
860 | |
|
|
861 | if (cede) |
|
|
862 | coro_enq (SvREFCNT_inc (prev)); |
|
|
863 | |
|
|
864 | next = coro_deq (PRIO_MIN); |
871 | next = coro_deq (PRIO_MIN); |
865 | |
872 | |
866 | if (!next) |
873 | if (!next) |
867 | next = SvREFCNT_inc (GvSV (coro_idle)); |
874 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
868 | |
875 | |
869 | GvSV (coro_current) = SvREFCNT_inc (next); |
876 | /* free this only after the transfer */ |
870 | transfer (aTHX_ |
877 | coro_mortal = prev; |
871 | sv_to_coro (prev, "Coro::schedule", "current coroutine"), |
878 | SV_CORO (prev, "Coro::schedule"); |
872 | sv_to_coro (next, "Coro::schedule", "next coroutine"), |
879 | |
|
|
880 | SvRV (GvSV (coro_current)) = next; |
|
|
881 | |
|
|
882 | SV_CORO (next, "Coro::schedule"); |
|
|
883 | |
|
|
884 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), |
873 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
885 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
874 | SvREFCNT_dec (next); |
886 | } |
875 | SvREFCNT_dec (prev); |
887 | |
|
|
888 | static void |
|
|
889 | api_cede (void) |
|
|
890 | { |
|
|
891 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
|
|
892 | |
|
|
893 | api_schedule (); |
876 | } |
894 | } |
877 | |
895 | |
878 | MODULE = Coro::State PACKAGE = Coro::State |
896 | MODULE = Coro::State PACKAGE = Coro::State |
879 | |
897 | |
880 | PROTOTYPES: ENABLE |
898 | PROTOTYPES: ENABLE |
… | |
… | |
913 | |
931 | |
914 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
932 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
915 | coro->mainstack = 0; /* actual work is done inside transfer */ |
933 | coro->mainstack = 0; /* actual work is done inside transfer */ |
916 | coro->stack = 0; |
934 | coro->stack = 0; |
917 | |
935 | |
|
|
936 | /* same as JMPENV_BOOTSTRAP */ |
|
|
937 | /* we might be able to recycle start_env, but safe is safe */ |
|
|
938 | Zero(&coro->start_env, 1, JMPENV); |
|
|
939 | coro->start_env.je_ret = -1; |
|
|
940 | coro->start_env.je_mustcatch = TRUE; |
|
|
941 | |
918 | RETVAL = coro; |
942 | RETVAL = coro; |
919 | OUTPUT: |
943 | OUTPUT: |
920 | RETVAL |
944 | RETVAL |
921 | |
945 | |
922 | void |
946 | void |
923 | transfer(prev, next, flags) |
947 | transfer(prev, next, flags) |
924 | Coro::State_or_hashref prev |
948 | SV *prev |
925 | Coro::State_or_hashref next |
949 | SV *next |
926 | int flags |
950 | int flags |
927 | PROTOTYPE: @ |
951 | PROTOTYPE: @ |
928 | CODE: |
952 | CODE: |
929 | PUTBACK; |
953 | PUTBACK; |
|
|
954 | SV_CORO (next, "Coro::transfer"); |
|
|
955 | SV_CORO (prev, "Coro::transfer"); |
930 | transfer (aTHX_ prev, next, flags); |
956 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
931 | SPAGAIN; |
957 | SPAGAIN; |
932 | |
958 | |
933 | void |
959 | void |
934 | DESTROY(coro) |
960 | DESTROY(coro) |
935 | Coro::State coro |
961 | Coro::State coro |
… | |
… | |
1030 | |
1056 | |
1031 | { |
1057 | { |
1032 | SV *sv = perl_get_sv("Coro::API", 1); |
1058 | SV *sv = perl_get_sv("Coro::API", 1); |
1033 | |
1059 | |
1034 | coroapi.schedule = api_schedule; |
1060 | coroapi.schedule = api_schedule; |
|
|
1061 | coroapi.cede = api_cede; |
1035 | coroapi.ready = api_ready; |
1062 | coroapi.ready = api_ready; |
1036 | coroapi.nready = &coro_nready; |
1063 | coroapi.nready = &coro_nready; |
1037 | coroapi.current = coro_current; |
1064 | coroapi.current = coro_current; |
1038 | |
1065 | |
1039 | GCoroAPI = &coroapi; |
1066 | GCoroAPI = &coroapi; |
… | |
… | |
1058 | RETVAL |
1085 | RETVAL |
1059 | |
1086 | |
1060 | void |
1087 | void |
1061 | schedule(...) |
1088 | schedule(...) |
1062 | PROTOTYPE: |
1089 | PROTOTYPE: |
1063 | ALIAS: |
|
|
1064 | cede = 1 |
|
|
1065 | CODE: |
1090 | CODE: |
1066 | api_schedule (ix); |
1091 | api_schedule (); |
1067 | |
1092 | |
|
|
1093 | void |
|
|
1094 | cede(...) |
|
|
1095 | PROTOTYPE: |
|
|
1096 | CODE: |
|
|
1097 | api_cede (); |
|
|
1098 | |