… | |
… | |
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 | |
… | |
… | |
66 | AV *curstack; |
68 | AV *curstack; |
67 | AV *mainstack; |
69 | AV *mainstack; |
68 | SV **stack_sp; |
70 | SV **stack_sp; |
69 | OP *op; |
71 | OP *op; |
70 | SV **curpad; |
72 | SV **curpad; |
|
|
73 | AV *comppad; |
71 | SV **stack_base; |
74 | SV **stack_base; |
72 | SV **stack_max; |
75 | SV **stack_max; |
73 | SV **tmps_stack; |
76 | SV **tmps_stack; |
74 | I32 tmps_floor; |
77 | I32 tmps_floor; |
75 | I32 tmps_ix; |
78 | I32 tmps_ix; |
… | |
… | |
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 | { |
… | |
… | |
287 | PL_curstack = c->curstack; |
291 | PL_curstack = c->curstack; |
288 | PL_mainstack = c->mainstack; |
292 | PL_mainstack = c->mainstack; |
289 | PL_stack_sp = c->stack_sp; |
293 | PL_stack_sp = c->stack_sp; |
290 | PL_op = c->op; |
294 | PL_op = c->op; |
291 | PL_curpad = c->curpad; |
295 | PL_curpad = c->curpad; |
|
|
296 | PL_comppad = c->comppad; |
292 | PL_stack_base = c->stack_base; |
297 | PL_stack_base = c->stack_base; |
293 | PL_stack_max = c->stack_max; |
298 | PL_stack_max = c->stack_max; |
294 | PL_tmps_stack = c->tmps_stack; |
299 | PL_tmps_stack = c->tmps_stack; |
295 | PL_tmps_floor = c->tmps_floor; |
300 | PL_tmps_floor = c->tmps_floor; |
296 | PL_tmps_ix = c->tmps_ix; |
301 | PL_tmps_ix = c->tmps_ix; |
… | |
… | |
416 | c->curstack = PL_curstack; |
421 | c->curstack = PL_curstack; |
417 | c->mainstack = PL_mainstack; |
422 | c->mainstack = PL_mainstack; |
418 | c->stack_sp = PL_stack_sp; |
423 | c->stack_sp = PL_stack_sp; |
419 | c->op = PL_op; |
424 | c->op = PL_op; |
420 | c->curpad = PL_curpad; |
425 | c->curpad = PL_curpad; |
|
|
426 | c->comppad = PL_comppad; |
421 | c->stack_base = PL_stack_base; |
427 | c->stack_base = PL_stack_base; |
422 | c->stack_max = PL_stack_max; |
428 | c->stack_max = PL_stack_max; |
423 | c->tmps_stack = PL_tmps_stack; |
429 | c->tmps_stack = PL_tmps_stack; |
424 | c->tmps_floor = PL_tmps_floor; |
430 | c->tmps_floor = PL_tmps_floor; |
425 | c->tmps_ix = PL_tmps_ix; |
431 | c->tmps_ix = PL_tmps_ix; |
… | |
… | |
487 | * still there is a memleak of 128 bytes... |
493 | * still there is a memleak of 128 bytes... |
488 | */ |
494 | */ |
489 | STATIC void |
495 | STATIC void |
490 | destroy_stacks(pTHX) |
496 | destroy_stacks(pTHX) |
491 | { |
497 | { |
|
|
498 | if (!IN_DESTRUCT) |
|
|
499 | { |
492 | /* is this ugly, I ask? */ |
500 | /* is this ugly, I ask? */ |
493 | while (PL_scopestack_ix) |
501 | while (PL_scopestack_ix) |
494 | LEAVE; |
502 | LEAVE; |
495 | |
503 | |
496 | /* sure it is, but more important: is it correct?? :/ */ |
504 | /* sure it is, but more important: is it correct?? :/ */ |
497 | while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ |
505 | while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ |
498 | FREETMPS; |
506 | FREETMPS; |
|
|
507 | } |
499 | |
508 | |
500 | while (PL_curstackinfo->si_next) |
509 | while (PL_curstackinfo->si_next) |
501 | PL_curstackinfo = PL_curstackinfo->si_next; |
510 | PL_curstackinfo = PL_curstackinfo->si_next; |
502 | |
511 | |
503 | while (PL_curstackinfo) |
512 | while (PL_curstackinfo) |
… | |
… | |
508 | dSP; |
517 | dSP; |
509 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
518 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
510 | PUTBACK; /* possibly superfluous */ |
519 | PUTBACK; /* possibly superfluous */ |
511 | } |
520 | } |
512 | |
521 | |
513 | if (PL_main_cv != Nullcv) /* don't during destruction. hack? */ |
522 | if (!IN_DESTRUCT) |
|
|
523 | { |
514 | dounwind(-1); |
524 | dounwind(-1); |
515 | |
|
|
516 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
525 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
|
|
526 | } |
|
|
527 | |
517 | Safefree(PL_curstackinfo->si_cxstack); |
528 | Safefree(PL_curstackinfo->si_cxstack); |
518 | Safefree(PL_curstackinfo); |
529 | Safefree(PL_curstackinfo); |
519 | PL_curstackinfo = p; |
530 | PL_curstackinfo = p; |
520 | } |
531 | } |
521 | |
532 | |
… | |
… | |
536 | stack->refcnt = 1; |
547 | stack->refcnt = 1; |
537 | stack->usecnt = 1; |
548 | stack->usecnt = 1; |
538 | stack->gencnt = ctx->gencnt = 0; |
549 | stack->gencnt = ctx->gencnt = 0; |
539 | if (alloc) |
550 | if (alloc) |
540 | { |
551 | { |
541 | #ifdef HAVE_MMAP |
552 | #if HAVE_MMAP |
542 | stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ |
553 | 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); |
554 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
544 | if (stack->sptr == (void *)-1) |
555 | if (stack->sptr == (void *)-1) |
545 | #endif |
556 | #endif |
546 | { |
557 | { |
547 | /*FIXME*//*D*//* reasonable stack size! */ |
558 | /*FIXME*//*D*//* reasonable stack size! */ |
548 | stack->ssize = -4096 * sizeof (long); |
559 | stack->ssize = -4096 * sizeof (long); |
… | |
… | |
629 | * that doesn't matter, though, since it is only |
640 | * that doesn't matter, though, since it is only |
630 | * pp_nextstate and we never return... |
641 | * pp_nextstate and we never return... |
631 | * ah yes, and I don't care anyways ;) |
642 | * ah yes, and I don't care anyways ;) |
632 | */ |
643 | */ |
633 | PUTBACK; |
644 | PUTBACK; |
634 | PL_op = pp_entersub(); |
645 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
635 | SPAGAIN; |
646 | SPAGAIN; |
636 | |
647 | |
637 | ENTER; /* necessary e.g. for dounwind */ |
648 | ENTER; /* necessary e.g. for dounwind */ |
638 | } |
649 | } |
639 | } |
650 | } |
… | |
… | |
643 | { |
654 | { |
644 | /* |
655 | /* |
645 | * this is a _very_ stripped down perl interpreter ;) |
656 | * this is a _very_ stripped down perl interpreter ;) |
646 | */ |
657 | */ |
647 | Coro__State ctx = (Coro__State)arg; |
658 | Coro__State ctx = (Coro__State)arg; |
|
|
659 | JMPENV coro_start_env; |
648 | |
660 | |
649 | /*FIXME*//* must set up top_env here */ |
661 | /* same as JMPENV_BOOTSTRAP */ |
|
|
662 | Zero(&coro_start_env, 1, JMPENV); |
|
|
663 | coro_start_env.je_ret = -1; |
|
|
664 | coro_start_env.je_mustcatch = TRUE; |
|
|
665 | PL_top_env = &coro_start_env; |
|
|
666 | |
650 | ctx->cursp = 0; |
667 | ctx->cursp = 0; |
651 | PL_op = PL_op->op_next; |
668 | PL_op = PL_op->op_next; |
652 | CALLRUNOPS(aTHX); |
669 | CALLRUNOPS(aTHX); |
653 | |
670 | |
654 | abort (); |
671 | abort (); |
… | |
… | |
731 | } |
748 | } |
732 | } |
749 | } |
733 | else |
750 | else |
734 | setup_coro (next); |
751 | setup_coro (next); |
735 | } |
752 | } |
736 | } |
|
|
737 | |
753 | |
738 | /* |
754 | /* |
739 | * xnext is now either prev or next, depending on wether |
755 | * 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 |
756 | * we switched the c stack or not. that's why I use a global |
741 | * variable, that should become thread-specific at one point. |
757 | * variable, that should become thread-specific at one point. |
742 | */ |
758 | */ |
743 | xnext->cursp = stacklevel; |
759 | 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 | { |
760 | } |
|
|
761 | |
|
|
762 | if (coro_mortal) |
|
|
763 | { |
|
|
764 | SvREFCNT_dec (coro_mortal); |
|
|
765 | coro_mortal = 0; |
|
|
766 | } |
|
|
767 | } |
|
|
768 | |
|
|
769 | #define SV_CORO(sv,func) \ |
|
|
770 | do { \ |
|
|
771 | if (SvROK (sv)) \ |
|
|
772 | sv = SvRV (sv); \ |
|
|
773 | \ |
|
|
774 | if (SvTYPE(sv) == SVt_PVHV) \ |
|
|
775 | { \ |
751 | HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash); |
776 | HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ |
752 | |
777 | \ |
753 | if (!he) |
778 | if (!he) \ |
754 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname); |
779 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ |
755 | |
780 | \ |
756 | arg = HeVAL(he); |
781 | (sv) = SvRV (HeVAL(he)); \ |
757 | } |
782 | } \ |
758 | |
783 | \ |
759 | /* must also be changed inside Coro::Cont::yield */ |
784 | /* must also be changed inside Coro::Cont::yield */ \ |
760 | if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) |
785 | 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); |
786 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
764 | /*NORETURN*/ |
787 | \ |
765 | } |
788 | } while(0) |
|
|
789 | |
|
|
790 | #define SvSTATE(sv) (struct coro *)SvIV (sv) |
766 | |
791 | |
767 | static void |
792 | static void |
768 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
793 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
769 | { |
794 | { |
770 | transfer(aTHX_ |
795 | SV_CORO (prev, "Coro::transfer"); |
771 | sv_to_coro (prev, "Coro::transfer", "prev"), |
796 | SV_CORO (next, "Coro::transfer"); |
772 | sv_to_coro (next, "Coro::transfer", "next"), |
797 | |
773 | flags); |
798 | transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); |
774 | } |
799 | } |
775 | |
800 | |
776 | /** Coro ********************************************************************/ |
801 | /** Coro ********************************************************************/ |
777 | |
802 | |
778 | #define PRIO_MAX 3 |
803 | #define PRIO_MAX 3 |
… | |
… | |
788 | static int coro_nready; |
813 | static int coro_nready; |
789 | |
814 | |
790 | static void |
815 | static void |
791 | coro_enq (SV *sv) |
816 | coro_enq (SV *sv) |
792 | { |
817 | { |
793 | if (SvROK (sv)) |
|
|
794 | { |
|
|
795 | SV *hv = SvRV (sv); |
|
|
796 | if (SvTYPE (hv) == SVt_PVHV) |
818 | if (SvTYPE (sv) == SVt_PVHV) |
797 | { |
819 | { |
798 | SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0); |
820 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
799 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
821 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
800 | |
822 | |
801 | prio = prio > PRIO_MAX ? PRIO_MAX |
823 | prio = prio > PRIO_MAX ? PRIO_MAX |
802 | : prio < PRIO_MIN ? PRIO_MIN |
824 | : prio < PRIO_MIN ? PRIO_MIN |
803 | : prio; |
825 | : prio; |
804 | |
826 | |
805 | av_push (coro_ready [prio - PRIO_MIN], sv); |
827 | av_push (coro_ready [prio - PRIO_MIN], sv); |
806 | coro_nready++; |
828 | coro_nready++; |
807 | |
829 | |
808 | return; |
830 | return; |
809 | } |
|
|
810 | } |
831 | } |
811 | |
832 | |
812 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
833 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
813 | } |
834 | } |
814 | |
835 | |
… | |
… | |
832 | } |
853 | } |
833 | |
854 | |
834 | static void |
855 | static void |
835 | api_ready (SV *coro) |
856 | api_ready (SV *coro) |
836 | { |
857 | { |
|
|
858 | if (SvROK (coro)) |
|
|
859 | coro = SvRV (coro); |
|
|
860 | |
837 | coro_enq (SvREFCNT_inc (coro)); |
861 | coro_enq (SvREFCNT_inc (coro)); |
838 | } |
862 | } |
839 | |
863 | |
840 | static void |
864 | static void |
841 | api_schedule (int cede) |
865 | api_schedule (void) |
842 | { |
866 | { |
843 | SV *prev, *next; |
867 | SV *prev, *next; |
844 | |
868 | |
845 | prev = GvSV (coro_current); |
869 | prev = SvRV (GvSV (coro_current)); |
846 | |
|
|
847 | if (cede) |
|
|
848 | coro_enq (SvREFCNT_inc (prev)); |
|
|
849 | |
|
|
850 | next = coro_deq (PRIO_MIN); |
870 | next = coro_deq (PRIO_MIN); |
851 | |
871 | |
852 | if (!next) |
872 | if (!next) |
853 | next = SvREFCNT_inc (GvSV (coro_idle)); |
873 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
854 | |
874 | |
855 | GvSV (coro_current) = SvREFCNT_inc (next); |
875 | /* free this only after the transfer */ |
856 | transfer (aTHX_ |
876 | coro_mortal = prev; |
857 | sv_to_coro (prev, "Coro::schedule", "current coroutine"), |
877 | SV_CORO (prev, "Coro::schedule"); |
858 | sv_to_coro (next, "Coro::schedule", "next coroutine"), |
878 | |
|
|
879 | SvRV (GvSV (coro_current)) = next; |
|
|
880 | |
|
|
881 | SV_CORO (next, "Coro::schedule"); |
|
|
882 | |
|
|
883 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), |
859 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
884 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
860 | SvREFCNT_dec (next); |
885 | } |
861 | SvREFCNT_dec (prev); |
886 | |
|
|
887 | static void |
|
|
888 | api_cede (void) |
|
|
889 | { |
|
|
890 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
|
|
891 | |
|
|
892 | api_schedule (); |
862 | } |
893 | } |
863 | |
894 | |
864 | MODULE = Coro::State PACKAGE = Coro::State |
895 | MODULE = Coro::State PACKAGE = Coro::State |
865 | |
896 | |
866 | PROTOTYPES: ENABLE |
897 | PROTOTYPES: ENABLE |
… | |
… | |
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 | |