… | |
… | |
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 | |
… | |
… | |
85 | I32 savestack_max; |
87 | I32 savestack_max; |
86 | OP **retstack; |
88 | OP **retstack; |
87 | I32 retstack_ix; |
89 | I32 retstack_ix; |
88 | I32 retstack_max; |
90 | I32 retstack_max; |
89 | COP *curcop; |
91 | COP *curcop; |
|
|
92 | PMOP *curpm; |
90 | JMPENV *top_env; |
93 | JMPENV *top_env; |
91 | |
94 | |
92 | /* data associated with this coroutine (initial args) */ |
95 | /* data associated with this coroutine (initial args) */ |
93 | AV *args; |
96 | AV *args; |
94 | }; |
97 | }; |
… | |
… | |
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 | { |
|
|
496 | if (!IN_DESTRUCT) |
|
|
497 | { |
492 | /* is this ugly, I ask? */ |
498 | /* is this ugly, I ask? */ |
493 | while (PL_scopestack_ix) |
499 | while (PL_scopestack_ix) |
494 | LEAVE; |
500 | LEAVE; |
495 | |
501 | |
496 | /* sure it is, but more important: is it correct?? :/ */ |
502 | /* sure it is, but more important: is it correct?? :/ */ |
497 | while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ |
503 | while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ |
498 | FREETMPS; |
504 | FREETMPS; |
|
|
505 | } |
499 | |
506 | |
500 | while (PL_curstackinfo->si_next) |
507 | while (PL_curstackinfo->si_next) |
501 | PL_curstackinfo = PL_curstackinfo->si_next; |
508 | PL_curstackinfo = PL_curstackinfo->si_next; |
502 | |
509 | |
503 | while (PL_curstackinfo) |
510 | while (PL_curstackinfo) |
… | |
… | |
508 | dSP; |
515 | dSP; |
509 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
516 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
510 | PUTBACK; /* possibly superfluous */ |
517 | PUTBACK; /* possibly superfluous */ |
511 | } |
518 | } |
512 | |
519 | |
513 | if (PL_main_cv != Nullcv) /* don't during destruction. hack? */ |
520 | if (!IN_DESTRUCT) |
|
|
521 | { |
514 | dounwind(-1); |
522 | dounwind(-1); |
515 | |
|
|
516 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
523 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
|
|
524 | } |
|
|
525 | |
517 | Safefree(PL_curstackinfo->si_cxstack); |
526 | Safefree(PL_curstackinfo->si_cxstack); |
518 | Safefree(PL_curstackinfo); |
527 | Safefree(PL_curstackinfo); |
519 | PL_curstackinfo = p; |
528 | PL_curstackinfo = p; |
520 | } |
529 | } |
521 | |
530 | |
… | |
… | |
536 | stack->refcnt = 1; |
545 | stack->refcnt = 1; |
537 | stack->usecnt = 1; |
546 | stack->usecnt = 1; |
538 | stack->gencnt = ctx->gencnt = 0; |
547 | stack->gencnt = ctx->gencnt = 0; |
539 | if (alloc) |
548 | if (alloc) |
540 | { |
549 | { |
541 | #ifdef HAVE_MMAP |
550 | #if HAVE_MMAP |
542 | 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 */ |
543 | 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); |
544 | if (stack->sptr == (void *)-1) |
553 | if (stack->sptr == (void *)-1) |
545 | #endif |
554 | #endif |
546 | { |
555 | { |
547 | /*FIXME*//*D*//* reasonable stack size! */ |
556 | /*FIXME*//*D*//* reasonable stack size! */ |
548 | stack->ssize = -4096 * sizeof (long); |
557 | stack->ssize = -4096 * sizeof (long); |
… | |
… | |
589 | dSP; |
598 | dSP; |
590 | Coro__State ctx = (Coro__State)arg; |
599 | Coro__State ctx = (Coro__State)arg; |
591 | SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); |
600 | SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); |
592 | |
601 | |
593 | coro_init_stacks (aTHX); |
602 | coro_init_stacks (aTHX); |
|
|
603 | PL_curpm = 0; /* segfault on first access */ |
594 | /*PL_curcop = 0;*/ |
604 | /*PL_curcop = 0;*/ |
595 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
605 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
596 | SvREFCNT_dec (GvAV (PL_defgv)); |
606 | SvREFCNT_dec (GvAV (PL_defgv)); |
597 | GvAV (PL_defgv) = ctx->args; |
607 | GvAV (PL_defgv) = ctx->args; |
598 | |
608 | |
… | |
… | |
643 | { |
653 | { |
644 | /* |
654 | /* |
645 | * this is a _very_ stripped down perl interpreter ;) |
655 | * this is a _very_ stripped down perl interpreter ;) |
646 | */ |
656 | */ |
647 | Coro__State ctx = (Coro__State)arg; |
657 | Coro__State ctx = (Coro__State)arg; |
|
|
658 | JMPENV coro_start_env; |
648 | |
659 | |
649 | /*FIXME*//* must set up top_env here */ |
660 | /* same as JMPENV_BOOTSTRAP */ |
|
|
661 | Zero(&coro_start_env, 1, JMPENV); |
|
|
662 | coro_start_env.je_ret = -1; |
|
|
663 | coro_start_env.je_mustcatch = TRUE; |
|
|
664 | PL_top_env = &coro_start_env; |
|
|
665 | |
650 | ctx->cursp = 0; |
666 | ctx->cursp = 0; |
651 | PL_op = PL_op->op_next; |
667 | PL_op = PL_op->op_next; |
652 | CALLRUNOPS(aTHX); |
668 | CALLRUNOPS(aTHX); |
653 | |
669 | |
654 | abort (); |
670 | abort (); |
… | |
… | |
731 | } |
747 | } |
732 | } |
748 | } |
733 | else |
749 | else |
734 | setup_coro (next); |
750 | setup_coro (next); |
735 | } |
751 | } |
736 | } |
|
|
737 | |
752 | |
738 | /* |
753 | /* |
739 | * xnext is now either prev or next, depending on wether |
754 | * xnext is now either prev or next, depending on wether |
740 | * we switched the c stack or not. that's why i use a global |
755 | * we switched the c stack or not. that's why i use a global |
741 | * variable, that should become thread-specific at one point. |
756 | * variable, that should become thread-specific at one point. |
742 | */ |
757 | */ |
743 | xnext->cursp = stacklevel; |
758 | xnext->cursp = stacklevel; |
744 | } |
|
|
745 | |
|
|
746 | static struct coro * |
|
|
747 | sv_to_coro (SV *arg, const char *funcname, const char *varname) |
|
|
748 | { |
|
|
749 | if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV) |
|
|
750 | { |
759 | } |
|
|
760 | |
|
|
761 | if (coro_mortal) |
|
|
762 | { |
|
|
763 | SvREFCNT_dec (coro_mortal); |
|
|
764 | coro_mortal = 0; |
|
|
765 | } |
|
|
766 | } |
|
|
767 | |
|
|
768 | #define SV_CORO(sv,func) \ |
|
|
769 | do { \ |
|
|
770 | if (SvROK (sv)) \ |
|
|
771 | sv = SvRV (sv); \ |
|
|
772 | \ |
|
|
773 | if (SvTYPE(sv) == SVt_PVHV) \ |
|
|
774 | { \ |
751 | HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash); |
775 | HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ |
752 | |
776 | \ |
753 | if (!he) |
777 | if (!he) \ |
754 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname); |
778 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ |
755 | |
779 | \ |
756 | arg = HeVAL(he); |
780 | (sv) = SvRV (HeVAL(he)); \ |
757 | } |
781 | } \ |
758 | |
782 | \ |
759 | /* must also be changed inside Coro::Cont::yield */ |
783 | /* must also be changed inside Coro::Cont::yield */ \ |
760 | if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) |
784 | if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \ |
761 | return (struct coro *) SvIV((SV*)SvRV(arg)); |
|
|
762 | |
|
|
763 | croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname); |
785 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
764 | /*NORETURN*/ |
786 | \ |
765 | } |
787 | } while(0) |
|
|
788 | |
|
|
789 | #define SvSTATE(sv) (struct coro *)SvIV (sv) |
766 | |
790 | |
767 | static void |
791 | static void |
768 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
792 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
769 | { |
793 | { |
770 | transfer(aTHX_ |
794 | SV_CORO (prev, "Coro::transfer"); |
771 | sv_to_coro (prev, "Coro::transfer", "prev"), |
795 | SV_CORO (next, "Coro::transfer"); |
772 | sv_to_coro (next, "Coro::transfer", "next"), |
796 | |
773 | flags); |
797 | transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); |
774 | } |
798 | } |
775 | |
799 | |
776 | /** Coro ********************************************************************/ |
800 | /** Coro ********************************************************************/ |
777 | |
801 | |
778 | #define PRIO_MAX 3 |
802 | #define PRIO_MAX 3 |
… | |
… | |
788 | static int coro_nready; |
812 | static int coro_nready; |
789 | |
813 | |
790 | static void |
814 | static void |
791 | coro_enq (SV *sv) |
815 | coro_enq (SV *sv) |
792 | { |
816 | { |
793 | if (SvROK (sv)) |
|
|
794 | { |
|
|
795 | SV *hv = SvRV (sv); |
|
|
796 | if (SvTYPE (hv) == SVt_PVHV) |
817 | if (SvTYPE (sv) == SVt_PVHV) |
797 | { |
818 | { |
798 | SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0); |
819 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
799 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
820 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
800 | |
821 | |
801 | prio = prio > PRIO_MAX ? PRIO_MAX |
822 | prio = prio > PRIO_MAX ? PRIO_MAX |
802 | : prio < PRIO_MIN ? PRIO_MIN |
823 | : prio < PRIO_MIN ? PRIO_MIN |
803 | : prio; |
824 | : prio; |
804 | |
825 | |
805 | av_push (coro_ready [prio - PRIO_MIN], sv); |
826 | av_push (coro_ready [prio - PRIO_MIN], sv); |
806 | coro_nready++; |
827 | coro_nready++; |
807 | |
828 | |
808 | return; |
829 | return; |
809 | } |
|
|
810 | } |
830 | } |
811 | |
831 | |
812 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
832 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
813 | } |
833 | } |
814 | |
834 | |
… | |
… | |
832 | } |
852 | } |
833 | |
853 | |
834 | static void |
854 | static void |
835 | api_ready (SV *coro) |
855 | api_ready (SV *coro) |
836 | { |
856 | { |
|
|
857 | if (SvROK (coro)) |
|
|
858 | coro = SvRV (coro); |
|
|
859 | |
837 | coro_enq (SvREFCNT_inc (coro)); |
860 | coro_enq (SvREFCNT_inc (coro)); |
838 | } |
861 | } |
839 | |
862 | |
840 | static void |
863 | static void |
841 | api_schedule (int cede) |
864 | api_schedule (void) |
842 | { |
865 | { |
843 | SV *prev, *next; |
866 | SV *prev, *next; |
844 | |
867 | |
845 | prev = GvSV (coro_current); |
868 | prev = SvRV (GvSV (coro_current)); |
846 | |
|
|
847 | if (cede) |
|
|
848 | coro_enq (SvREFCNT_inc (prev)); |
|
|
849 | |
|
|
850 | next = coro_deq (PRIO_MIN); |
869 | next = coro_deq (PRIO_MIN); |
851 | |
870 | |
852 | if (!next) |
871 | if (!next) |
853 | next = SvREFCNT_inc (GvSV (coro_idle)); |
872 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
854 | |
873 | |
855 | GvSV (coro_current) = SvREFCNT_inc (next); |
874 | /* free this only after the transfer */ |
856 | transfer (aTHX_ |
875 | coro_mortal = prev; |
857 | sv_to_coro (prev, "Coro::schedule", "current coroutine"), |
876 | SV_CORO (prev, "Coro::schedule"); |
858 | sv_to_coro (next, "Coro::schedule", "next coroutine"), |
877 | |
|
|
878 | SvRV (GvSV (coro_current)) = next; |
|
|
879 | |
|
|
880 | SV_CORO (next, "Coro::schedule"); |
|
|
881 | |
|
|
882 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), |
859 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
883 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
860 | SvREFCNT_dec (next); |
884 | } |
861 | SvREFCNT_dec (prev); |
885 | |
|
|
886 | static void |
|
|
887 | api_cede (void) |
|
|
888 | { |
|
|
889 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
|
|
890 | |
|
|
891 | api_schedule (); |
862 | } |
892 | } |
863 | |
893 | |
864 | MODULE = Coro::State PACKAGE = Coro::State |
894 | MODULE = Coro::State PACKAGE = Coro::State |
865 | |
895 | |
866 | PROTOTYPES: ENABLE |
896 | PROTOTYPES: ENABLE |
… | |
… | |
872 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
902 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
873 | |
903 | |
874 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
904 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
875 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
905 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
876 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
906 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
|
|
907 | newCONSTSUB (coro_state_stash, "SAVE_CURPM", newSViv (TRANSFER_SAVE_CURPM)); |
877 | newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); |
908 | newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); |
878 | |
909 | |
879 | if (!padlist_cache) |
910 | if (!padlist_cache) |
880 | padlist_cache = newHV (); |
911 | padlist_cache = newHV (); |
881 | |
912 | |
… | |
… | |
905 | OUTPUT: |
936 | OUTPUT: |
906 | RETVAL |
937 | RETVAL |
907 | |
938 | |
908 | void |
939 | void |
909 | transfer(prev, next, flags) |
940 | transfer(prev, next, flags) |
910 | Coro::State_or_hashref prev |
941 | SV *prev |
911 | Coro::State_or_hashref next |
942 | SV *next |
912 | int flags |
943 | int flags |
913 | PROTOTYPE: @ |
944 | PROTOTYPE: @ |
914 | CODE: |
945 | CODE: |
915 | PUTBACK; |
946 | PUTBACK; |
|
|
947 | SV_CORO (next, "Coro::transfer"); |
|
|
948 | SV_CORO (prev, "Coro::transfer"); |
916 | transfer (aTHX_ prev, next, flags); |
949 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
917 | SPAGAIN; |
950 | SPAGAIN; |
918 | |
951 | |
919 | void |
952 | void |
920 | DESTROY(coro) |
953 | DESTROY(coro) |
921 | Coro::State coro |
954 | Coro::State coro |
… | |
… | |
1016 | |
1049 | |
1017 | { |
1050 | { |
1018 | SV *sv = perl_get_sv("Coro::API", 1); |
1051 | SV *sv = perl_get_sv("Coro::API", 1); |
1019 | |
1052 | |
1020 | coroapi.schedule = api_schedule; |
1053 | coroapi.schedule = api_schedule; |
|
|
1054 | coroapi.cede = api_cede; |
1021 | coroapi.ready = api_ready; |
1055 | coroapi.ready = api_ready; |
1022 | coroapi.nready = &coro_nready; |
1056 | coroapi.nready = &coro_nready; |
1023 | coroapi.current = coro_current; |
1057 | coroapi.current = coro_current; |
1024 | |
1058 | |
1025 | GCoroAPI = &coroapi; |
1059 | GCoroAPI = &coroapi; |
… | |
… | |
1029 | } |
1063 | } |
1030 | |
1064 | |
1031 | void |
1065 | void |
1032 | ready(self) |
1066 | ready(self) |
1033 | SV * self |
1067 | SV * self |
|
|
1068 | PROTOTYPE: $ |
1034 | CODE: |
1069 | CODE: |
1035 | api_ready (self); |
1070 | api_ready (self); |
1036 | |
1071 | |
1037 | int |
1072 | int |
1038 | nready(...) |
1073 | nready(...) |
… | |
… | |
1043 | RETVAL |
1078 | RETVAL |
1044 | |
1079 | |
1045 | void |
1080 | void |
1046 | schedule(...) |
1081 | schedule(...) |
1047 | PROTOTYPE: |
1082 | PROTOTYPE: |
1048 | ALIAS: |
|
|
1049 | cede = 1 |
|
|
1050 | CODE: |
1083 | CODE: |
1051 | api_schedule (ix); |
1084 | api_schedule (); |
1052 | |
1085 | |
|
|
1086 | void |
|
|
1087 | cede(...) |
|
|
1088 | PROTOTYPE: |
|
|
1089 | CODE: |
|
|
1090 | api_cede (); |
|
|
1091 | |