… | |
… | |
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 |
… | |
… | |
26 | /* The next macro should declare 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 IN_DESTRUCT (PL_main_cv == Nullcv) |
|
|
32 | |
31 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
33 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
32 | |
34 | |
33 | #include "CoroAPI.h" |
35 | #include "CoroAPI.h" |
34 | |
36 | |
35 | static struct CoroAPI coroapi; |
37 | static struct CoroAPI coroapi; |
… | |
… | |
54 | |
56 | |
55 | /* optionally saved, might be zero */ |
57 | /* optionally saved, might be zero */ |
56 | AV *defav; |
58 | AV *defav; |
57 | SV *defsv; |
59 | SV *defsv; |
58 | SV *errsv; |
60 | SV *errsv; |
|
|
61 | /* TODO: PL_pmop might be nice && effortless to save. */ |
59 | |
62 | |
60 | /* saved global state not related to stacks */ |
63 | /* saved global state not related to stacks */ |
61 | U8 dowarn; |
64 | U8 dowarn; |
62 | I32 in_eval; |
65 | I32 in_eval; |
63 | |
66 | |
… | |
… | |
99 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
102 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
100 | static HV *coro_state_stash; |
103 | static HV *coro_state_stash; |
101 | static SV *ucoro_state_sv; |
104 | static SV *ucoro_state_sv; |
102 | static U32 ucoro_state_hash; |
105 | static U32 ucoro_state_hash; |
103 | static HV *padlist_cache; |
106 | static HV *padlist_cache; |
|
|
107 | static SV *coro_mortal; /* will be freed after next transfer */ |
104 | |
108 | |
105 | /* mostly copied from op.c:cv_clone2 */ |
109 | /* mostly copied from op.c:cv_clone2 */ |
106 | STATIC AV * |
110 | STATIC AV * |
107 | clone_padlist (AV *protopadlist) |
111 | clone_padlist (AV *protopadlist) |
108 | { |
112 | { |
… | |
… | |
487 | * still there is a memleak of 128 bytes... |
491 | * still there is a memleak of 128 bytes... |
488 | */ |
492 | */ |
489 | STATIC void |
493 | STATIC void |
490 | destroy_stacks(pTHX) |
494 | destroy_stacks(pTHX) |
491 | { |
495 | { |
492 | int destruct = PL_main_cv != Nullcv; |
496 | if (!IN_DESTRUCT) |
493 | |
|
|
494 | if (destruct) |
|
|
495 | { |
497 | { |
496 | /* is this ugly, I ask? */ |
498 | /* is this ugly, I ask? */ |
497 | while (PL_scopestack_ix) |
499 | while (PL_scopestack_ix) |
498 | LEAVE; |
500 | LEAVE; |
499 | |
501 | |
… | |
… | |
513 | dSP; |
515 | dSP; |
514 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
516 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
515 | PUTBACK; /* possibly superfluous */ |
517 | PUTBACK; /* possibly superfluous */ |
516 | } |
518 | } |
517 | |
519 | |
518 | if (destruct) |
520 | if (!IN_DESTRUCT) |
519 | { |
521 | { |
520 | dounwind(-1); |
522 | dounwind(-1); |
521 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
523 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
522 | } |
524 | } |
523 | |
525 | |
… | |
… | |
543 | stack->refcnt = 1; |
545 | stack->refcnt = 1; |
544 | stack->usecnt = 1; |
546 | stack->usecnt = 1; |
545 | stack->gencnt = ctx->gencnt = 0; |
547 | stack->gencnt = ctx->gencnt = 0; |
546 | if (alloc) |
548 | if (alloc) |
547 | { |
549 | { |
548 | #ifdef HAVE_MMAP |
550 | #if HAVE_MMAP |
549 | stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ |
551 | 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); |
552 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
551 | if (stack->sptr == (void *)-1) |
553 | if (stack->sptr == (void *)-1) |
552 | #endif |
554 | #endif |
553 | { |
555 | { |
554 | /*FIXME*//*D*//* reasonable stack size! */ |
556 | /*FIXME*//*D*//* reasonable stack size! */ |
555 | stack->ssize = -4096 * sizeof (long); |
557 | stack->ssize = -4096 * sizeof (long); |
… | |
… | |
650 | { |
652 | { |
651 | /* |
653 | /* |
652 | * this is a _very_ stripped down perl interpreter ;) |
654 | * this is a _very_ stripped down perl interpreter ;) |
653 | */ |
655 | */ |
654 | Coro__State ctx = (Coro__State)arg; |
656 | Coro__State ctx = (Coro__State)arg; |
|
|
657 | JMPENV coro_start_env; |
655 | |
658 | |
656 | /*FIXME*//* must set up top_env here */ |
659 | /* same as JMPENV_BOOTSTRAP */ |
|
|
660 | Zero(&coro_start_env, 1, JMPENV); |
|
|
661 | coro_start_env.je_ret = -1; |
|
|
662 | coro_start_env.je_mustcatch = TRUE; |
|
|
663 | PL_top_env = &coro_start_env; |
|
|
664 | |
657 | ctx->cursp = 0; |
665 | ctx->cursp = 0; |
658 | PL_op = PL_op->op_next; |
666 | PL_op = PL_op->op_next; |
659 | CALLRUNOPS(aTHX); |
667 | CALLRUNOPS(aTHX); |
660 | |
668 | |
661 | abort (); |
669 | abort (); |
… | |
… | |
738 | } |
746 | } |
739 | } |
747 | } |
740 | else |
748 | else |
741 | setup_coro (next); |
749 | setup_coro (next); |
742 | } |
750 | } |
743 | } |
|
|
744 | |
751 | |
745 | /* |
752 | /* |
746 | * xnext is now either prev or next, depending on wether |
753 | * 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 |
754 | * we switched the c stack or not. that's why i use a global |
748 | * variable, that should become thread-specific at one point. |
755 | * variable, that should become thread-specific at one point. |
749 | */ |
756 | */ |
750 | xnext->cursp = stacklevel; |
757 | xnext->cursp = stacklevel; |
751 | } |
|
|
752 | |
|
|
753 | static struct coro * |
|
|
754 | sv_to_coro (SV *arg, const char *funcname, const char *varname) |
|
|
755 | { |
|
|
756 | if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV) |
|
|
757 | { |
758 | } |
|
|
759 | |
|
|
760 | if (coro_mortal) |
|
|
761 | { |
|
|
762 | SvREFCNT_dec (coro_mortal); |
|
|
763 | coro_mortal = 0; |
|
|
764 | } |
|
|
765 | } |
|
|
766 | |
|
|
767 | #define SV_CORO(sv,func) \ |
|
|
768 | do { \ |
|
|
769 | if (SvROK (sv)) \ |
|
|
770 | sv = SvRV (sv); \ |
|
|
771 | \ |
|
|
772 | if (SvTYPE(sv) == SVt_PVHV) \ |
|
|
773 | { \ |
758 | HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash); |
774 | HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ |
759 | |
775 | \ |
760 | if (!he) |
776 | if (!he) \ |
761 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname); |
777 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ |
762 | |
778 | \ |
763 | arg = HeVAL(he); |
779 | (sv) = SvRV (HeVAL(he)); \ |
764 | } |
780 | } \ |
765 | |
781 | \ |
766 | /* must also be changed inside Coro::Cont::yield */ |
782 | /* must also be changed inside Coro::Cont::yield */ \ |
767 | if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) |
783 | if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \ |
768 | return (struct coro *) SvIV((SV*)SvRV(arg)); |
|
|
769 | |
|
|
770 | croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname); |
784 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
771 | /*NORETURN*/ |
785 | \ |
772 | } |
786 | } while(0) |
|
|
787 | |
|
|
788 | #define SvSTATE(sv) (struct coro *)SvIV (sv) |
773 | |
789 | |
774 | static void |
790 | static void |
775 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
791 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
776 | { |
792 | { |
777 | transfer(aTHX_ |
793 | SV_CORO (prev, "Coro::transfer"); |
778 | sv_to_coro (prev, "Coro::transfer", "prev"), |
794 | SV_CORO (next, "Coro::transfer"); |
779 | sv_to_coro (next, "Coro::transfer", "next"), |
795 | |
780 | flags); |
796 | transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); |
781 | } |
797 | } |
782 | |
798 | |
783 | /** Coro ********************************************************************/ |
799 | /** Coro ********************************************************************/ |
784 | |
800 | |
785 | #define PRIO_MAX 3 |
801 | #define PRIO_MAX 3 |
… | |
… | |
795 | static int coro_nready; |
811 | static int coro_nready; |
796 | |
812 | |
797 | static void |
813 | static void |
798 | coro_enq (SV *sv) |
814 | coro_enq (SV *sv) |
799 | { |
815 | { |
800 | if (SvROK (sv)) |
|
|
801 | { |
|
|
802 | SV *hv = SvRV (sv); |
|
|
803 | if (SvTYPE (hv) == SVt_PVHV) |
816 | if (SvTYPE (sv) == SVt_PVHV) |
804 | { |
817 | { |
805 | SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0); |
818 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
806 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
819 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
807 | |
820 | |
808 | prio = prio > PRIO_MAX ? PRIO_MAX |
821 | prio = prio > PRIO_MAX ? PRIO_MAX |
809 | : prio < PRIO_MIN ? PRIO_MIN |
822 | : prio < PRIO_MIN ? PRIO_MIN |
810 | : prio; |
823 | : prio; |
811 | |
824 | |
812 | av_push (coro_ready [prio - PRIO_MIN], sv); |
825 | av_push (coro_ready [prio - PRIO_MIN], sv); |
813 | coro_nready++; |
826 | coro_nready++; |
814 | |
827 | |
815 | return; |
828 | return; |
816 | } |
|
|
817 | } |
829 | } |
818 | |
830 | |
819 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
831 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
820 | } |
832 | } |
821 | |
833 | |
… | |
… | |
839 | } |
851 | } |
840 | |
852 | |
841 | static void |
853 | static void |
842 | api_ready (SV *coro) |
854 | api_ready (SV *coro) |
843 | { |
855 | { |
|
|
856 | if (SvROK (coro)) |
|
|
857 | coro = SvRV (coro); |
|
|
858 | |
844 | coro_enq (SvREFCNT_inc (coro)); |
859 | coro_enq (SvREFCNT_inc (coro)); |
845 | } |
860 | } |
846 | |
861 | |
847 | static void |
862 | static void |
848 | api_schedule (int cede) |
863 | api_schedule (void) |
849 | { |
864 | { |
850 | SV *prev, *next; |
865 | SV *prev, *next; |
851 | |
866 | |
852 | prev = GvSV (coro_current); |
867 | prev = SvRV (GvSV (coro_current)); |
853 | |
|
|
854 | if (cede) |
|
|
855 | coro_enq (SvREFCNT_inc (prev)); |
|
|
856 | |
|
|
857 | next = coro_deq (PRIO_MIN); |
868 | next = coro_deq (PRIO_MIN); |
858 | |
869 | |
859 | if (!next) |
870 | if (!next) |
860 | next = SvREFCNT_inc (GvSV (coro_idle)); |
871 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
861 | |
872 | |
862 | GvSV (coro_current) = SvREFCNT_inc (next); |
873 | coro_mortal = prev; |
863 | transfer (aTHX_ |
874 | SV_CORO (prev, "Coro::schedule"); |
864 | sv_to_coro (prev, "Coro::schedule", "current coroutine"), |
875 | |
865 | sv_to_coro (next, "Coro::schedule", "next coroutine"), |
876 | SvRV (GvSV (coro_current)) = next; |
|
|
877 | |
|
|
878 | SV_CORO (next, "Coro::schedule"); |
|
|
879 | |
|
|
880 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), |
866 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
881 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
867 | SvREFCNT_dec (next); |
882 | } |
868 | SvREFCNT_dec (prev); |
883 | |
|
|
884 | static void |
|
|
885 | api_cede (void) |
|
|
886 | { |
|
|
887 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
|
|
888 | |
|
|
889 | api_schedule (); |
869 | } |
890 | } |
870 | |
891 | |
871 | MODULE = Coro::State PACKAGE = Coro::State |
892 | MODULE = Coro::State PACKAGE = Coro::State |
872 | |
893 | |
873 | PROTOTYPES: ENABLE |
894 | PROTOTYPES: ENABLE |
… | |
… | |
912 | OUTPUT: |
933 | OUTPUT: |
913 | RETVAL |
934 | RETVAL |
914 | |
935 | |
915 | void |
936 | void |
916 | transfer(prev, next, flags) |
937 | transfer(prev, next, flags) |
917 | Coro::State_or_hashref prev |
938 | SV *prev |
918 | Coro::State_or_hashref next |
939 | SV *next |
919 | int flags |
940 | int flags |
920 | PROTOTYPE: @ |
941 | PROTOTYPE: @ |
921 | CODE: |
942 | CODE: |
922 | PUTBACK; |
943 | PUTBACK; |
|
|
944 | SV_CORO (next, "Coro::transfer"); |
|
|
945 | SV_CORO (prev, "Coro::transfer"); |
923 | transfer (aTHX_ prev, next, flags); |
946 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
924 | SPAGAIN; |
947 | SPAGAIN; |
925 | |
948 | |
926 | void |
949 | void |
927 | DESTROY(coro) |
950 | DESTROY(coro) |
928 | Coro::State coro |
951 | Coro::State coro |
… | |
… | |
1023 | |
1046 | |
1024 | { |
1047 | { |
1025 | SV *sv = perl_get_sv("Coro::API", 1); |
1048 | SV *sv = perl_get_sv("Coro::API", 1); |
1026 | |
1049 | |
1027 | coroapi.schedule = api_schedule; |
1050 | coroapi.schedule = api_schedule; |
|
|
1051 | coroapi.cede = api_cede; |
1028 | coroapi.ready = api_ready; |
1052 | coroapi.ready = api_ready; |
1029 | coroapi.nready = &coro_nready; |
1053 | coroapi.nready = &coro_nready; |
1030 | coroapi.current = coro_current; |
1054 | coroapi.current = coro_current; |
1031 | |
1055 | |
1032 | GCoroAPI = &coroapi; |
1056 | GCoroAPI = &coroapi; |
… | |
… | |
1036 | } |
1060 | } |
1037 | |
1061 | |
1038 | void |
1062 | void |
1039 | ready(self) |
1063 | ready(self) |
1040 | SV * self |
1064 | SV * self |
|
|
1065 | PROTOTYPE: $ |
1041 | CODE: |
1066 | CODE: |
1042 | api_ready (self); |
1067 | api_ready (self); |
1043 | |
1068 | |
1044 | int |
1069 | int |
1045 | nready(...) |
1070 | nready(...) |
… | |
… | |
1050 | RETVAL |
1075 | RETVAL |
1051 | |
1076 | |
1052 | void |
1077 | void |
1053 | schedule(...) |
1078 | schedule(...) |
1054 | PROTOTYPE: |
1079 | PROTOTYPE: |
1055 | ALIAS: |
|
|
1056 | cede = 1 |
|
|
1057 | CODE: |
1080 | CODE: |
1058 | api_schedule (ix); |
1081 | api_schedule (); |
1059 | |
1082 | |
|
|
1083 | void |
|
|
1084 | cede(...) |
|
|
1085 | PROTOTYPE: |
|
|
1086 | CODE: |
|
|
1087 | api_cede (); |
|
|
1088 | |