… | |
… | |
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 |
… | |
… | |
25 | |
25 | |
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 | |
|
|
31 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
30 | |
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 | |
… | |
… | |
99 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
101 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
100 | static HV *coro_state_stash; |
102 | static HV *coro_state_stash; |
101 | static SV *ucoro_state_sv; |
103 | static SV *ucoro_state_sv; |
102 | static U32 ucoro_state_hash; |
104 | static U32 ucoro_state_hash; |
103 | static HV *padlist_cache; |
105 | static HV *padlist_cache; |
|
|
106 | static SV *coro_mortal; /* will be freed after next transfer */ |
104 | |
107 | |
105 | /* mostly copied from op.c:cv_clone2 */ |
108 | /* mostly copied from op.c:cv_clone2 */ |
106 | STATIC AV * |
109 | STATIC AV * |
107 | clone_padlist (AV *protopadlist) |
110 | clone_padlist (AV *protopadlist) |
108 | { |
111 | { |
… | |
… | |
487 | * still there is a memleak of 128 bytes... |
490 | * still there is a memleak of 128 bytes... |
488 | */ |
491 | */ |
489 | STATIC void |
492 | STATIC void |
490 | destroy_stacks(pTHX) |
493 | destroy_stacks(pTHX) |
491 | { |
494 | { |
492 | int destruct = PL_main_cv != Nullcv; |
495 | if (!IN_DESTRUCT) |
493 | |
|
|
494 | if (destruct) |
|
|
495 | { |
496 | { |
496 | /* is this ugly, I ask? */ |
497 | /* is this ugly, I ask? */ |
497 | while (PL_scopestack_ix) |
498 | while (PL_scopestack_ix) |
498 | LEAVE; |
499 | LEAVE; |
499 | |
500 | |
… | |
… | |
513 | dSP; |
514 | dSP; |
514 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
515 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
515 | PUTBACK; /* possibly superfluous */ |
516 | PUTBACK; /* possibly superfluous */ |
516 | } |
517 | } |
517 | |
518 | |
518 | if (destruct) |
519 | if (!IN_DESTRUCT) |
519 | { |
520 | { |
520 | dounwind(-1); |
521 | dounwind(-1); |
521 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
522 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
522 | } |
523 | } |
523 | |
524 | |
… | |
… | |
543 | stack->refcnt = 1; |
544 | stack->refcnt = 1; |
544 | stack->usecnt = 1; |
545 | stack->usecnt = 1; |
545 | stack->gencnt = ctx->gencnt = 0; |
546 | stack->gencnt = ctx->gencnt = 0; |
546 | if (alloc) |
547 | if (alloc) |
547 | { |
548 | { |
548 | #ifdef HAVE_MMAP |
549 | #if HAVE_MMAP |
549 | stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ |
550 | 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); |
551 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
551 | if (stack->sptr == (void *)-1) |
552 | if (stack->sptr == (void *)-1) |
552 | #endif |
553 | #endif |
553 | { |
554 | { |
554 | /*FIXME*//*D*//* reasonable stack size! */ |
555 | /*FIXME*//*D*//* reasonable stack size! */ |
555 | stack->ssize = -4096 * sizeof (long); |
556 | stack->ssize = -4096 * sizeof (long); |
… | |
… | |
650 | { |
651 | { |
651 | /* |
652 | /* |
652 | * this is a _very_ stripped down perl interpreter ;) |
653 | * this is a _very_ stripped down perl interpreter ;) |
653 | */ |
654 | */ |
654 | Coro__State ctx = (Coro__State)arg; |
655 | Coro__State ctx = (Coro__State)arg; |
|
|
656 | JMPENV coro_start_env; |
655 | |
657 | |
656 | /*FIXME*//* must set up top_env here */ |
658 | /* same as JMPENV_BOOTSTRAP */ |
|
|
659 | Zero(&coro_start_env, 1, JMPENV); |
|
|
660 | coro_start_env.je_ret = -1; |
|
|
661 | coro_start_env.je_mustcatch = TRUE; |
|
|
662 | PL_top_env = &coro_start_env; |
|
|
663 | |
657 | ctx->cursp = 0; |
664 | ctx->cursp = 0; |
658 | PL_op = PL_op->op_next; |
665 | PL_op = PL_op->op_next; |
659 | CALLRUNOPS(aTHX); |
666 | CALLRUNOPS(aTHX); |
660 | |
667 | |
661 | abort (); |
668 | abort (); |
… | |
… | |
738 | } |
745 | } |
739 | } |
746 | } |
740 | else |
747 | else |
741 | setup_coro (next); |
748 | setup_coro (next); |
742 | } |
749 | } |
743 | } |
|
|
744 | |
750 | |
745 | /* |
751 | /* |
746 | * xnext is now either prev or next, depending on wether |
752 | * 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 |
753 | * we switched the c stack or not. that's why I use a global |
748 | * variable, that should become thread-specific at one point. |
754 | * variable, that should become thread-specific at one point. |
749 | */ |
755 | */ |
750 | xnext->cursp = stacklevel; |
756 | 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 | { |
757 | } |
|
|
758 | |
|
|
759 | if (coro_mortal) |
|
|
760 | { |
|
|
761 | SvREFCNT_dec (coro_mortal); |
|
|
762 | coro_mortal = 0; |
|
|
763 | } |
|
|
764 | } |
|
|
765 | |
|
|
766 | #define SV_CORO(sv,func) \ |
|
|
767 | do { \ |
|
|
768 | if (SvROK (sv)) \ |
|
|
769 | sv = SvRV (sv); \ |
|
|
770 | \ |
|
|
771 | if (SvTYPE(sv) == SVt_PVHV) \ |
|
|
772 | { \ |
758 | HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash); |
773 | HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ |
759 | |
774 | \ |
760 | if (!he) |
775 | if (!he) \ |
761 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname); |
776 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ |
762 | |
777 | \ |
763 | arg = HeVAL(he); |
778 | (sv) = SvRV (HeVAL(he)); \ |
764 | } |
779 | } \ |
765 | |
780 | \ |
766 | /* must also be changed inside Coro::Cont::yield */ |
781 | /* must also be changed inside Coro::Cont::yield */ \ |
767 | if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) |
782 | 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); |
783 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
771 | /*NORETURN*/ |
784 | \ |
772 | } |
785 | } while(0) |
|
|
786 | |
|
|
787 | #define SvSTATE(sv) (struct coro *)SvIV (sv) |
773 | |
788 | |
774 | static void |
789 | static void |
775 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
790 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
776 | { |
791 | { |
777 | transfer(aTHX_ |
792 | SV_CORO (prev, "Coro::transfer"); |
778 | sv_to_coro (prev, "Coro::transfer", "prev"), |
793 | SV_CORO (next, "Coro::transfer"); |
779 | sv_to_coro (next, "Coro::transfer", "next"), |
794 | |
780 | flags); |
795 | transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); |
781 | } |
796 | } |
782 | |
797 | |
783 | /** Coro ********************************************************************/ |
798 | /** Coro ********************************************************************/ |
784 | |
799 | |
785 | #define PRIO_MAX 3 |
800 | #define PRIO_MAX 3 |
… | |
… | |
795 | static int coro_nready; |
810 | static int coro_nready; |
796 | |
811 | |
797 | static void |
812 | static void |
798 | coro_enq (SV *sv) |
813 | coro_enq (SV *sv) |
799 | { |
814 | { |
800 | if (SvROK (sv)) |
|
|
801 | { |
|
|
802 | SV *hv = SvRV (sv); |
|
|
803 | if (SvTYPE (hv) == SVt_PVHV) |
815 | if (SvTYPE (sv) == SVt_PVHV) |
804 | { |
816 | { |
805 | SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0); |
817 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
806 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
818 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
807 | |
819 | |
808 | prio = prio > PRIO_MAX ? PRIO_MAX |
820 | prio = prio > PRIO_MAX ? PRIO_MAX |
809 | : prio < PRIO_MIN ? PRIO_MIN |
821 | : prio < PRIO_MIN ? PRIO_MIN |
810 | : prio; |
822 | : prio; |
811 | |
823 | |
812 | av_push (coro_ready [prio - PRIO_MIN], sv); |
824 | av_push (coro_ready [prio - PRIO_MIN], sv); |
813 | coro_nready++; |
825 | coro_nready++; |
814 | |
826 | |
815 | return; |
827 | return; |
816 | } |
|
|
817 | } |
828 | } |
818 | |
829 | |
819 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
830 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
820 | } |
831 | } |
821 | |
832 | |
… | |
… | |
839 | } |
850 | } |
840 | |
851 | |
841 | static void |
852 | static void |
842 | api_ready (SV *coro) |
853 | api_ready (SV *coro) |
843 | { |
854 | { |
|
|
855 | if (SvROK (coro)) |
|
|
856 | coro = SvRV (coro); |
|
|
857 | |
844 | coro_enq (SvREFCNT_inc (coro)); |
858 | coro_enq (SvREFCNT_inc (coro)); |
845 | } |
859 | } |
846 | |
860 | |
847 | static void |
861 | static void |
848 | api_schedule (int cede) |
862 | api_schedule (void) |
849 | { |
863 | { |
850 | SV *prev, *next; |
864 | SV *prev, *next; |
851 | |
865 | |
852 | prev = GvSV (coro_current); |
866 | prev = SvRV (GvSV (coro_current)); |
853 | |
|
|
854 | if (cede) |
|
|
855 | coro_enq (SvREFCNT_inc (prev)); |
|
|
856 | |
|
|
857 | next = coro_deq (PRIO_MIN); |
867 | next = coro_deq (PRIO_MIN); |
858 | |
868 | |
859 | if (!next) |
869 | if (!next) |
860 | next = SvREFCNT_inc (GvSV (coro_idle)); |
870 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
861 | |
871 | |
862 | GvSV (coro_current) = SvREFCNT_inc (next); |
872 | /* free this only after the transfer */ |
863 | transfer (aTHX_ |
873 | coro_mortal = prev; |
864 | sv_to_coro (prev, "Coro::schedule", "current coroutine"), |
874 | SV_CORO (prev, "Coro::schedule"); |
865 | sv_to_coro (next, "Coro::schedule", "next coroutine"), |
875 | |
|
|
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 | |