1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | #include "libcoro/coro.c" |
5 | #include "libcoro/coro.c" |
|
|
6 | |
|
|
7 | #include <signal.h> |
6 | |
8 | |
7 | #ifdef HAVE_MMAP |
9 | #ifdef HAVE_MMAP |
8 | # include <unistd.h> |
10 | # include <unistd.h> |
9 | # include <sys/mman.h> |
11 | # include <sys/mman.h> |
10 | # ifndef MAP_ANON |
12 | # ifndef MAP_ANON |
… | |
… | |
53 | |
55 | |
54 | void *sptr; |
56 | void *sptr; |
55 | long ssize; /* positive == mmap, otherwise malloc */ |
57 | long ssize; /* positive == mmap, otherwise malloc */ |
56 | } coro_stack; |
58 | } coro_stack; |
57 | |
59 | |
58 | static coro_stack main_stack = { 1, 0, 0 }; |
|
|
59 | |
|
|
60 | struct coro { |
60 | struct coro { |
61 | /* the optional C context */ |
61 | /* the optional C context */ |
62 | coro_stack *stack; |
62 | coro_stack *stack; |
63 | void *cursp; |
63 | void *cursp; |
64 | int gencnt; |
64 | int gencnt; |
… | |
… | |
68 | SV *defsv; |
68 | SV *defsv; |
69 | SV *errsv; |
69 | SV *errsv; |
70 | |
70 | |
71 | /* saved global state not related to stacks */ |
71 | /* saved global state not related to stacks */ |
72 | U8 dowarn; |
72 | U8 dowarn; |
|
|
73 | I32 in_eval; |
73 | |
74 | |
74 | /* the stacks and related info (callchain etc..) */ |
75 | /* the stacks and related info (callchain etc..) */ |
75 | PERL_SI *curstackinfo; |
76 | PERL_SI *curstackinfo; |
76 | AV *curstack; |
77 | AV *curstack; |
77 | AV *mainstack; |
78 | AV *mainstack; |
… | |
… | |
95 | I32 savestack_max; |
96 | I32 savestack_max; |
96 | OP **retstack; |
97 | OP **retstack; |
97 | I32 retstack_ix; |
98 | I32 retstack_ix; |
98 | I32 retstack_max; |
99 | I32 retstack_max; |
99 | COP *curcop; |
100 | COP *curcop; |
100 | JMPENV start_env; |
|
|
101 | JMPENV *top_env; |
101 | JMPENV *top_env; |
102 | |
102 | |
103 | /* data associated with this coroutine (initial args) */ |
103 | /* data associated with this coroutine (initial args) */ |
104 | AV *args; |
104 | AV *args; |
105 | }; |
105 | }; |
… | |
… | |
290 | |
290 | |
291 | static void |
291 | static void |
292 | load_state(pTHX_ Coro__State c) |
292 | load_state(pTHX_ Coro__State c) |
293 | { |
293 | { |
294 | PL_dowarn = c->dowarn; |
294 | PL_dowarn = c->dowarn; |
|
|
295 | PL_in_eval = c->in_eval; |
295 | |
296 | |
296 | PL_curstackinfo = c->curstackinfo; |
297 | PL_curstackinfo = c->curstackinfo; |
297 | PL_curstack = c->curstack; |
298 | PL_curstack = c->curstack; |
298 | PL_mainstack = c->mainstack; |
299 | PL_mainstack = c->mainstack; |
299 | PL_stack_sp = c->stack_sp; |
300 | PL_stack_sp = c->stack_sp; |
… | |
… | |
316 | PL_savestack_max = c->savestack_max; |
317 | PL_savestack_max = c->savestack_max; |
317 | PL_retstack = c->retstack; |
318 | PL_retstack = c->retstack; |
318 | PL_retstack_ix = c->retstack_ix; |
319 | PL_retstack_ix = c->retstack_ix; |
319 | PL_retstack_max = c->retstack_max; |
320 | PL_retstack_max = c->retstack_max; |
320 | PL_curcop = c->curcop; |
321 | PL_curcop = c->curcop; |
321 | PL_start_env = c->start_env; |
|
|
322 | PL_top_env = c->top_env; |
322 | PL_top_env = c->top_env; |
323 | |
323 | |
324 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
324 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
325 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
325 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
326 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
326 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
… | |
… | |
425 | /* but if it's missing the defav contents magically get replaced sometimes */ |
425 | /* but if it's missing the defav contents magically get replaced sometimes */ |
426 | if (c->defav) |
426 | if (c->defav) |
427 | av_reify (c->defav); |
427 | av_reify (c->defav); |
428 | |
428 | |
429 | c->dowarn = PL_dowarn; |
429 | c->dowarn = PL_dowarn; |
|
|
430 | c->in_eval = PL_in_eval; |
430 | |
431 | |
431 | c->curstackinfo = PL_curstackinfo; |
432 | c->curstackinfo = PL_curstackinfo; |
432 | c->curstack = PL_curstack; |
433 | c->curstack = PL_curstack; |
433 | c->mainstack = PL_mainstack; |
434 | c->mainstack = PL_mainstack; |
434 | c->stack_sp = PL_stack_sp; |
435 | c->stack_sp = PL_stack_sp; |
… | |
… | |
451 | c->savestack_max = PL_savestack_max; |
452 | c->savestack_max = PL_savestack_max; |
452 | c->retstack = PL_retstack; |
453 | c->retstack = PL_retstack; |
453 | c->retstack_ix = PL_retstack_ix; |
454 | c->retstack_ix = PL_retstack_ix; |
454 | c->retstack_max = PL_retstack_max; |
455 | c->retstack_max = PL_retstack_max; |
455 | c->curcop = PL_curcop; |
456 | c->curcop = PL_curcop; |
456 | c->start_env = PL_start_env; |
|
|
457 | c->top_env = PL_top_env; |
457 | c->top_env = PL_top_env; |
458 | } |
458 | } |
459 | |
459 | |
460 | /* |
460 | /* |
461 | * allocate various perl stacks. This is an exact copy |
461 | * allocate various perl stacks. This is an exact copy |
… | |
… | |
553 | stack->usecnt = 1; |
553 | stack->usecnt = 1; |
554 | stack->gencnt = ctx->gencnt = 0; |
554 | stack->gencnt = ctx->gencnt = 0; |
555 | if (alloc) |
555 | if (alloc) |
556 | { |
556 | { |
557 | #ifdef HAVE_MMAP |
557 | #ifdef HAVE_MMAP |
558 | stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write */ |
558 | stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ |
559 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); |
559 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); |
560 | if (stack->sptr == (void *)-1) |
560 | if (stack->sptr == (void *)-1) |
561 | #endif |
561 | #endif |
562 | { |
562 | { |
563 | /*FIXME*//*D*//* reasonable stack size! */ |
563 | /*FIXME*//*D*//* reasonable stack size! */ |
… | |
… | |
605 | dSP; |
605 | dSP; |
606 | Coro__State ctx = (Coro__State)arg; |
606 | Coro__State ctx = (Coro__State)arg; |
607 | SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); |
607 | SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); |
608 | |
608 | |
609 | coro_init_stacks (aTHX); |
609 | coro_init_stacks (aTHX); |
610 | JMPENV_BOOTSTRAP; |
|
|
611 | SPAGAIN; |
|
|
612 | |
|
|
613 | /*PL_curcop = 0;*/ |
610 | /*PL_curcop = 0;*/ |
|
|
611 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
614 | SvREFCNT_dec (GvAV (PL_defgv)); |
612 | SvREFCNT_dec (GvAV (PL_defgv)); |
615 | GvAV (PL_defgv) = ctx->args; |
613 | GvAV (PL_defgv) = ctx->args; |
|
|
614 | |
|
|
615 | SPAGAIN; |
616 | |
616 | |
617 | if (ctx->stack) |
617 | if (ctx->stack) |
618 | { |
618 | { |
619 | ctx->cursp = 0; |
619 | ctx->cursp = 0; |
620 | |
620 | |
621 | PUSHMARK(SP); |
621 | PUSHMARK(SP); |
622 | PUTBACK; |
622 | PUTBACK; |
623 | (void) call_sv (sub_init, G_VOID|G_NOARGS); |
623 | (void) call_sv (sub_init, G_VOID|G_NOARGS|G_EVAL); |
|
|
624 | |
|
|
625 | if (SvTRUE (ERRSV)) |
|
|
626 | croak (NULL); |
|
|
627 | else |
624 | croak ("FATAL: CCTXT coroutine returned!"); |
628 | croak ("FATAL: CCTXT coroutine returned!"); |
625 | } |
629 | } |
626 | else |
630 | else |
627 | { |
631 | { |
628 | UNOP myop; |
632 | UNOP myop; |
629 | |
633 | |
… | |
… | |
656 | /* |
660 | /* |
657 | * this is a _very_ stripped down perl interpreter ;) |
661 | * this is a _very_ stripped down perl interpreter ;) |
658 | */ |
662 | */ |
659 | Coro__State ctx = (Coro__State)arg; |
663 | Coro__State ctx = (Coro__State)arg; |
660 | |
664 | |
|
|
665 | /*FIXME*//* must set up top_env here */ |
661 | ctx->cursp = 0; |
666 | ctx->cursp = 0; |
662 | PL_op = PL_op->op_next; |
667 | PL_op = PL_op->op_next; |
663 | CALLRUNOPS(aTHX); |
668 | CALLRUNOPS(aTHX); |
664 | /*NORETURN*/ |
669 | |
665 | abort (); |
670 | abort (); |
666 | } |
671 | } |
667 | |
672 | |
668 | STATIC void |
673 | STATIC void |
669 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
674 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
… | |
… | |
743 | } |
748 | } |
744 | |
749 | |
745 | next->cursp = stacklevel; |
750 | next->cursp = stacklevel; |
746 | } |
751 | } |
747 | |
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 | HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash); |
|
|
759 | |
|
|
760 | if (!he) |
|
|
761 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname); |
|
|
762 | |
|
|
763 | arg = HeVAL(he); |
|
|
764 | } |
|
|
765 | |
|
|
766 | /* must also be changed inside Coro::Cont::yield */ |
|
|
767 | if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) |
|
|
768 | return (struct coro *) SvIV((SV*)SvRV(arg)); |
|
|
769 | else |
|
|
770 | croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname); |
|
|
771 | } |
|
|
772 | |
|
|
773 | /** Coro ********************************************************************/ |
|
|
774 | |
|
|
775 | #define PRIO_MAX 3 |
|
|
776 | #define PRIO_HIGH 1 |
|
|
777 | #define PRIO_NORMAL 0 |
|
|
778 | #define PRIO_LOW -1 |
|
|
779 | #define PRIO_IDLE -3 |
|
|
780 | #define PRIO_MIN -4 |
|
|
781 | |
|
|
782 | /* for Coro.pm */ |
|
|
783 | static GV *coro_current, *coro_idle; |
|
|
784 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
|
|
785 | |
|
|
786 | static void |
|
|
787 | coro_enq (SV *sv) |
|
|
788 | { |
|
|
789 | if (SvROK (sv)) |
|
|
790 | { |
|
|
791 | SV *hv = SvRV (sv); |
|
|
792 | if (SvTYPE (hv) == SVt_PVHV) |
|
|
793 | { |
|
|
794 | SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0); |
|
|
795 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
|
|
796 | |
|
|
797 | prio = prio > PRIO_MAX ? PRIO_MAX |
|
|
798 | : prio < PRIO_MIN ? PRIO_MIN |
|
|
799 | : prio; |
|
|
800 | |
|
|
801 | av_push (coro_ready [prio - PRIO_MIN], sv); |
|
|
802 | |
|
|
803 | return; |
|
|
804 | } |
|
|
805 | } |
|
|
806 | |
|
|
807 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
|
|
808 | } |
|
|
809 | |
|
|
810 | static SV * |
|
|
811 | coro_deq (int min_prio) |
|
|
812 | { |
|
|
813 | int prio = PRIO_MAX - PRIO_MIN; |
|
|
814 | |
|
|
815 | min_prio -= PRIO_MIN; |
|
|
816 | if (min_prio < 0) |
|
|
817 | min_prio = 0; |
|
|
818 | |
|
|
819 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
|
|
820 | if (av_len (coro_ready[prio]) >= 0) |
|
|
821 | return av_shift (coro_ready[prio]); |
|
|
822 | |
|
|
823 | return 0; |
|
|
824 | } |
|
|
825 | |
748 | MODULE = Coro::State PACKAGE = Coro::State |
826 | MODULE = Coro::State PACKAGE = Coro::State |
749 | |
827 | |
750 | PROTOTYPES: ENABLE |
828 | PROTOTYPES: ENABLE |
751 | |
829 | |
752 | BOOT: |
830 | BOOT: |
… | |
… | |
785 | RETVAL = coro; |
863 | RETVAL = coro; |
786 | OUTPUT: |
864 | OUTPUT: |
787 | RETVAL |
865 | RETVAL |
788 | |
866 | |
789 | void |
867 | void |
790 | transfer(prev, next, flags = TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK) |
868 | transfer(prev, next, flags) |
791 | Coro::State_or_hashref prev |
869 | Coro::State_or_hashref prev |
792 | Coro::State_or_hashref next |
870 | Coro::State_or_hashref next |
793 | int flags |
871 | int flags |
794 | PROTOTYPE: @ |
872 | PROTOTYPE: @ |
795 | CODE: |
873 | CODE: |
|
|
874 | PUTBACK; |
796 | transfer (aTHX_ prev, next, flags); |
875 | transfer (aTHX_ prev, next, flags); |
|
|
876 | SPAGAIN; |
797 | |
877 | |
798 | void |
878 | void |
799 | DESTROY(coro) |
879 | DESTROY(coro) |
800 | Coro::State coro |
880 | Coro::State coro |
801 | CODE: |
881 | CODE: |
… | |
… | |
823 | CODE: |
903 | CODE: |
824 | #ifdef MAY_FLUSH |
904 | #ifdef MAY_FLUSH |
825 | flush_padlist_cache (); |
905 | flush_padlist_cache (); |
826 | #endif |
906 | #endif |
827 | |
907 | |
|
|
908 | void |
|
|
909 | _exit(code) |
|
|
910 | int code |
|
|
911 | PROTOTYPE: $ |
|
|
912 | CODE: |
|
|
913 | #if defined(__GLIBC__) || _POSIX_C_SOURCE |
|
|
914 | _exit (code); |
|
|
915 | #else |
|
|
916 | signal (SIGTERM, SIG_DFL); |
|
|
917 | raise (SIGTERM); |
|
|
918 | exit (code); |
|
|
919 | #endif |
|
|
920 | |
828 | MODULE = Coro::State PACKAGE = Coro::Cont |
921 | MODULE = Coro::State PACKAGE = Coro::Cont |
829 | |
922 | |
830 | # this is slightly dirty |
923 | # this is slightly dirty (should expose a c-level api) |
831 | |
924 | |
832 | void |
925 | void |
833 | yield(...) |
926 | yield(...) |
834 | PROTOTYPE: @ |
927 | PROTOTYPE: @ |
835 | CODE: |
928 | CODE: |
… | |
… | |
853 | next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
946 | next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
854 | SvREFCNT_dec (sv); |
947 | SvREFCNT_dec (sv); |
855 | |
948 | |
856 | transfer(aTHX_ prev, next, 0); |
949 | transfer(aTHX_ prev, next, 0); |
857 | |
950 | |
|
|
951 | MODULE = Coro::State PACKAGE = Coro |
|
|
952 | |
|
|
953 | # this is slightly dirty (should expose a c-level api) |
|
|
954 | |
|
|
955 | BOOT: |
|
|
956 | { |
|
|
957 | int i; |
|
|
958 | HV *stash = gv_stashpv ("Coro", TRUE); |
|
|
959 | |
|
|
960 | newCONSTSUB (stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
|
|
961 | newCONSTSUB (stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
|
|
962 | newCONSTSUB (stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
|
|
963 | newCONSTSUB (stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
|
|
964 | newCONSTSUB (stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
|
|
965 | newCONSTSUB (stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
|
|
966 | |
|
|
967 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
|
|
968 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
|
|
969 | |
|
|
970 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
|
|
971 | coro_ready[i] = newAV (); |
|
|
972 | } |
|
|
973 | |
|
|
974 | void |
|
|
975 | ready(self) |
|
|
976 | SV * self |
|
|
977 | CODE: |
|
|
978 | coro_enq (SvREFCNT_inc (self)); |
|
|
979 | |
|
|
980 | void |
|
|
981 | schedule(...) |
|
|
982 | ALIAS: |
|
|
983 | cede = 1 |
|
|
984 | CODE: |
|
|
985 | SV *prev, *next; |
|
|
986 | |
|
|
987 | prev = GvSV (coro_current); |
|
|
988 | |
|
|
989 | if (ix) |
|
|
990 | coro_enq (SvREFCNT_inc (prev)); |
|
|
991 | |
|
|
992 | next = coro_deq (PRIO_MIN); |
|
|
993 | |
|
|
994 | if (!next) |
|
|
995 | next = SvREFCNT_inc (GvSV (coro_idle)); |
|
|
996 | |
|
|
997 | GvSV (coro_current) = SvREFCNT_inc (next); |
|
|
998 | transfer (sv_to_coro (prev, "Coro::schedule", "current coroutine"), |
|
|
999 | sv_to_coro (next, "Coro::schedule", "next coroutine"), |
|
|
1000 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
|
|
1001 | SvREFCNT_dec (next); |
|
|
1002 | SvREFCNT_dec (prev); |
|
|
1003 | |