… | |
… | |
29 | # endif |
29 | # endif |
30 | #endif |
30 | #endif |
31 | |
31 | |
32 | #include <errno.h> |
32 | #include <errno.h> |
33 | |
33 | |
|
|
34 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
|
|
35 | # undef STACKGUARD |
|
|
36 | #endif |
|
|
37 | |
|
|
38 | #ifndef STACKGUARD |
|
|
39 | # define STACKGUARD 0 |
|
|
40 | #endif |
|
|
41 | |
34 | #ifdef HAVE_MMAP |
42 | #ifdef HAVE_MMAP |
35 | # include <unistd.h> |
43 | # include <unistd.h> |
36 | # include <sys/mman.h> |
44 | # include <sys/mman.h> |
37 | # ifndef MAP_ANONYMOUS |
45 | # ifndef MAP_ANONYMOUS |
38 | # ifdef MAP_ANON |
46 | # ifdef MAP_ANON |
39 | # define MAP_ANONYMOUS MAP_ANON |
47 | # define MAP_ANONYMOUS MAP_ANON |
40 | # else |
48 | # else |
41 | # undef HAVE_MMAP |
49 | # undef HAVE_MMAP |
42 | # endif |
50 | # endif |
43 | # endif |
51 | # endif |
|
|
52 | # include <limits.h> |
|
|
53 | # ifndef PAGESIZE |
|
|
54 | # define PAGESIZE pagesize |
|
|
55 | # define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) |
|
|
56 | static long pagesize; |
|
|
57 | # else |
|
|
58 | # define BOOT_PAGESIZE |
|
|
59 | # endif |
44 | #endif |
60 | #endif |
45 | |
61 | |
46 | #define SUB_INIT "Coro::State::initialize" |
62 | #define SUB_INIT "Coro::State::initialize" |
47 | #define UCORO_STATE "_coro_state" |
63 | #define UCORO_STATE "_coro_state" |
48 | |
64 | |
… | |
… | |
84 | void *sptr; |
100 | void *sptr; |
85 | long ssize; /* positive == mmap, otherwise malloc */ |
101 | long ssize; /* positive == mmap, otherwise malloc */ |
86 | } coro_stack; |
102 | } coro_stack; |
87 | |
103 | |
88 | struct coro { |
104 | struct coro { |
89 | /* the top-level JMPENV for each coroutine, needed to catch dies. */ |
|
|
90 | JMPENV start_env; |
|
|
91 | |
|
|
92 | /* the optional C context */ |
105 | /* the optional C context */ |
93 | coro_stack *stack; |
106 | coro_stack *stack; |
94 | void *cursp; |
107 | void *cursp; |
95 | int gencnt; |
108 | int gencnt; |
96 | |
109 | |
… | |
… | |
132 | I32 retstack_max; |
145 | I32 retstack_max; |
133 | PMOP *curpm; |
146 | PMOP *curpm; |
134 | COP *curcop; |
147 | COP *curcop; |
135 | JMPENV *top_env; |
148 | JMPENV *top_env; |
136 | |
149 | |
|
|
150 | /* coro process data */ |
|
|
151 | int prio; |
|
|
152 | |
137 | /* data associated with this coroutine (initial args) */ |
153 | /* data associated with this coroutine (initial args) */ |
138 | AV *args; |
154 | AV *args; |
|
|
155 | int refcnt; |
139 | }; |
156 | }; |
140 | |
157 | |
141 | typedef struct coro *Coro__State; |
158 | typedef struct coro *Coro__State; |
142 | typedef struct coro *Coro__State_or_hashref; |
159 | typedef struct coro *Coro__State_or_hashref; |
143 | |
160 | |
… | |
… | |
147 | AV *padlist = CvPADLIST (cv); |
164 | AV *padlist = CvPADLIST (cv); |
148 | AV *newpadlist, *newpad; |
165 | AV *newpadlist, *newpad; |
149 | |
166 | |
150 | newpadlist = newAV (); |
167 | newpadlist = newAV (); |
151 | AvREAL_off (newpadlist); |
168 | AvREAL_off (newpadlist); |
|
|
169 | #if PERL_VERSION < 9 |
152 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
170 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
|
|
171 | #else |
|
|
172 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); |
|
|
173 | #endif |
153 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
174 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
154 | --AvFILLp (padlist); |
175 | --AvFILLp (padlist); |
155 | |
176 | |
156 | av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); |
177 | av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); |
157 | av_store (newpadlist, 1, (SV *)newpad); |
178 | av_store (newpadlist, 1, (SV *)newpad); |
… | |
… | |
527 | stack->gencnt = ctx->gencnt = 0; |
548 | stack->gencnt = ctx->gencnt = 0; |
528 | |
549 | |
529 | if (alloc) |
550 | if (alloc) |
530 | { |
551 | { |
531 | #if HAVE_MMAP |
552 | #if HAVE_MMAP |
532 | stack->ssize = STACKSIZE * sizeof (long); /* mmap should do allocate-on-write for us */ |
553 | stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */ |
533 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
554 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
534 | if (stack->sptr == (void *)-1) |
555 | if (stack->sptr != (void *)-1) |
|
|
556 | { |
|
|
557 | # if STACKGUARD |
|
|
558 | mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
|
|
559 | # endif |
|
|
560 | } |
|
|
561 | else |
535 | #endif |
562 | #endif |
536 | { |
563 | { |
537 | stack->ssize = - (STACKSIZE * (long)sizeof (long)); |
564 | stack->ssize = - (STACKSIZE * (long)sizeof (long)); |
538 | New (0, stack->sptr, STACKSIZE, long); |
565 | New (0, stack->sptr, STACKSIZE, long); |
539 | } |
566 | } |
… | |
… | |
603 | } |
630 | } |
604 | else |
631 | else |
605 | { |
632 | { |
606 | UNOP myop; |
633 | UNOP myop; |
607 | |
634 | |
608 | PL_op = (OP *)&myop; |
|
|
609 | |
|
|
610 | Zero(&myop, 1, UNOP); |
635 | Zero(&myop, 1, UNOP); |
611 | myop.op_next = Nullop; |
636 | myop.op_next = Nullop; |
612 | myop.op_flags = OPf_WANT_VOID; |
637 | myop.op_flags = OPf_WANT_VOID; |
|
|
638 | |
|
|
639 | PL_op = (OP *)&myop; |
613 | |
640 | |
614 | PUSHMARK(SP); |
641 | PUSHMARK(SP); |
615 | XPUSHs (sub_init); |
642 | XPUSHs (sub_init); |
616 | /* |
643 | /* |
617 | * the next line is slightly wrong, as PL_op->op_next |
644 | * the next line is slightly wrong, as PL_op->op_next |
… | |
… | |
635 | * this is a _very_ stripped down perl interpreter ;) |
662 | * this is a _very_ stripped down perl interpreter ;) |
636 | */ |
663 | */ |
637 | dTHX; |
664 | dTHX; |
638 | Coro__State ctx = (Coro__State)arg; |
665 | Coro__State ctx = (Coro__State)arg; |
639 | |
666 | |
640 | PL_top_env = &ctx->start_env; |
667 | PL_top_env = &PL_start_env; |
641 | |
668 | |
642 | ctx->cursp = 0; |
669 | ctx->cursp = 0; |
643 | PL_op = PL_op->op_next; |
670 | PL_op = PL_op->op_next; |
644 | CALLRUNOPS(aTHX); |
671 | CALLRUNOPS(aTHX); |
645 | |
672 | |
646 | abort (); |
673 | abort (); |
647 | } |
674 | } |
648 | |
675 | |
|
|
676 | /* never call directly, always through the coro_state_transfer global variable */ |
649 | static void |
677 | static void |
650 | transfer (pTHX_ struct coro *prev, struct coro *next, int flags) |
678 | transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) |
651 | { |
679 | { |
652 | dSTACKLEVEL; |
680 | dSTACKLEVEL; |
653 | |
681 | |
654 | if (prev != next) |
682 | if (prev != next) |
655 | { |
683 | { |
|
|
684 | /* has this coro been created yet? */ |
656 | if (next->mainstack) |
685 | if (next->mainstack) |
657 | { |
686 | { |
658 | LOCK; |
687 | LOCK; |
659 | SAVE (prev, flags); |
688 | SAVE (prev, flags); |
660 | LOAD (next); |
689 | LOAD (next); |
… | |
… | |
699 | { |
728 | { |
700 | LOCK; |
729 | LOCK; |
701 | SAVE (prev, -1); /* first get rid of the old state */ |
730 | SAVE (prev, -1); /* first get rid of the old state */ |
702 | UNLOCK; |
731 | UNLOCK; |
703 | |
732 | |
|
|
733 | /* create the coroutine for the first time */ |
704 | if (flags & TRANSFER_SAVE_CCTXT) |
734 | if (flags & TRANSFER_SAVE_CCTXT) |
705 | { |
735 | { |
706 | if (!prev->stack) |
736 | if (!prev->stack) |
707 | allocate_stack (prev, 0); |
737 | allocate_stack (prev, 0); |
708 | |
738 | |
|
|
739 | /* the new coroutine starts with start_env again */ |
|
|
740 | PL_top_env = &PL_start_env; |
|
|
741 | |
709 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
742 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
710 | { |
743 | { |
711 | PL_top_env = &next->start_env; |
|
|
712 | |
|
|
713 | setup_coro (next); |
744 | setup_coro (next); |
714 | next->cursp = stacklevel; |
745 | next->cursp = stacklevel; |
715 | |
746 | |
716 | prev->stack->refcnt++; |
747 | prev->stack->refcnt++; |
717 | prev->stack->usecnt++; |
748 | prev->stack->usecnt++; |
… | |
… | |
745 | coro_mortal = 0; |
776 | coro_mortal = 0; |
746 | } |
777 | } |
747 | UNLOCK; |
778 | UNLOCK; |
748 | } |
779 | } |
749 | |
780 | |
|
|
781 | /* use this function pointer to call the above function */ |
|
|
782 | /* this is done to increase chances of the compiler not inlining the call */ |
|
|
783 | void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl; |
|
|
784 | |
750 | #define SV_CORO(sv,func) \ |
785 | #define SV_CORO(sv,func) \ |
751 | do { \ |
786 | do { \ |
752 | if (SvROK (sv)) \ |
787 | if (SvROK (sv)) \ |
753 | sv = SvRV (sv); \ |
788 | sv = SvRV (sv); \ |
754 | \ |
789 | \ |
755 | if (SvTYPE (sv) == SVt_PVHV) \ |
790 | if (SvTYPE (sv) == SVt_PVHV && SvSTASH (sv) != coro_state_stash) \ |
756 | { \ |
791 | { \ |
757 | HE *he = hv_fetch_ent ((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ |
792 | HE *he = hv_fetch_ent ((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ |
758 | \ |
793 | \ |
759 | if (!he) \ |
794 | if (!he) \ |
760 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ |
795 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ |
… | |
… | |
766 | if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ |
801 | if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ |
767 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
802 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
768 | \ |
803 | \ |
769 | } while(0) |
804 | } while(0) |
770 | |
805 | |
771 | #define SvSTATE(sv) INT2PTR (struct coro *, SvIV (sv)) |
806 | static void |
|
|
807 | coro_state_destroy (struct coro *coro) |
|
|
808 | { |
|
|
809 | if (coro->refcnt--) |
|
|
810 | return; |
772 | |
811 | |
|
|
812 | if (coro->mainstack && coro->mainstack != main_mainstack) |
|
|
813 | { |
|
|
814 | struct coro temp; |
|
|
815 | |
|
|
816 | SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); |
|
|
817 | LOAD (aTHX_ coro); |
|
|
818 | |
|
|
819 | destroy_stacks (aTHX); |
|
|
820 | |
|
|
821 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
|
|
822 | |
|
|
823 | coro->mainstack = 0; |
|
|
824 | } |
|
|
825 | |
|
|
826 | deallocate_stack (coro); |
|
|
827 | SvREFCNT_dec (coro->args); |
|
|
828 | Safefree (coro); |
|
|
829 | } |
|
|
830 | |
773 | static void |
831 | static int |
|
|
832 | coro_state_clear (SV *sv, MAGIC *mg) |
|
|
833 | { |
|
|
834 | struct coro *coro = (struct coro *)mg->mg_ptr; |
|
|
835 | mg->mg_ptr = 0; |
|
|
836 | |
|
|
837 | coro_state_destroy (coro); |
|
|
838 | |
|
|
839 | return 0; |
|
|
840 | } |
|
|
841 | |
|
|
842 | static int |
|
|
843 | coro_state_dup (MAGIC *mg, CLONE_PARAMS *params) |
|
|
844 | { |
|
|
845 | struct coro *coro = (struct coro *)mg->mg_ptr; |
|
|
846 | |
|
|
847 | ++coro->refcnt; |
|
|
848 | |
|
|
849 | return 0; |
|
|
850 | } |
|
|
851 | |
|
|
852 | static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; |
|
|
853 | |
|
|
854 | static struct coro * |
|
|
855 | SvSTATE (SV *coro) |
|
|
856 | { |
|
|
857 | MAGIC *mg = SvMAGIC (SvROK (coro) ? SvRV (coro) : coro); |
|
|
858 | assert (mg->mg_type == PERL_MAGIC_ext); |
|
|
859 | return (struct coro *)mg->mg_ptr; |
|
|
860 | } |
|
|
861 | |
|
|
862 | static void |
774 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
863 | api_transfer (pTHX_ SV *prev, SV *next, int flags) |
775 | { |
864 | { |
776 | SV_CORO (prev, "Coro::transfer"); |
865 | SV_CORO (prev, "Coro::transfer"); |
777 | SV_CORO (next, "Coro::transfer"); |
866 | SV_CORO (next, "Coro::transfer"); |
778 | |
867 | |
779 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
868 | coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
780 | } |
869 | } |
781 | |
870 | |
782 | /** Coro ********************************************************************/ |
871 | /** Coro ********************************************************************/ |
783 | |
872 | |
784 | #define PRIO_MAX 3 |
873 | #define PRIO_MAX 3 |
… | |
… | |
788 | #define PRIO_IDLE -3 |
877 | #define PRIO_IDLE -3 |
789 | #define PRIO_MIN -4 |
878 | #define PRIO_MIN -4 |
790 | |
879 | |
791 | /* for Coro.pm */ |
880 | /* for Coro.pm */ |
792 | static GV *coro_current, *coro_idle; |
881 | static GV *coro_current, *coro_idle; |
793 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
882 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
794 | static int coro_nready; |
883 | static int coro_nready; |
795 | |
884 | |
796 | static void |
885 | static void |
797 | coro_enq (pTHX_ SV *sv) |
886 | coro_enq (pTHX_ SV *sv) |
798 | { |
887 | { |
799 | SV **xprio; |
|
|
800 | int prio; |
888 | int prio; |
801 | |
889 | |
802 | if (SvTYPE (sv) != SVt_PVHV) |
890 | if (SvTYPE (sv) != SVt_PVHV) |
803 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
891 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
804 | |
892 | |
805 | xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
893 | { |
806 | prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
894 | SV *coro = sv; |
807 | |
895 | SV_CORO (coro, "omg"); |
808 | prio = prio > PRIO_MAX ? PRIO_MAX |
896 | prio = SvSTATE (coro)->prio; |
809 | : prio < PRIO_MIN ? PRIO_MIN |
897 | } |
810 | : prio; |
|
|
811 | |
898 | |
812 | av_push (coro_ready [prio - PRIO_MIN], sv); |
899 | av_push (coro_ready [prio - PRIO_MIN], sv); |
813 | coro_nready++; |
900 | coro_nready++; |
814 | } |
901 | } |
815 | |
902 | |
… | |
… | |
821 | min_prio -= PRIO_MIN; |
908 | min_prio -= PRIO_MIN; |
822 | if (min_prio < 0) |
909 | if (min_prio < 0) |
823 | min_prio = 0; |
910 | min_prio = 0; |
824 | |
911 | |
825 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
912 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
826 | if (av_len (coro_ready[prio]) >= 0) |
913 | if (AvFILLp (coro_ready [prio]) >= 0) |
827 | { |
914 | { |
828 | coro_nready--; |
915 | coro_nready--; |
829 | return av_shift (coro_ready[prio]); |
916 | return av_shift (coro_ready [prio]); |
830 | } |
917 | } |
831 | |
918 | |
832 | return 0; |
919 | return 0; |
833 | } |
920 | } |
834 | |
921 | |
… | |
… | |
868 | |
955 | |
869 | SV_CORO (next, "Coro::schedule"); |
956 | SV_CORO (next, "Coro::schedule"); |
870 | |
957 | |
871 | UNLOCK; |
958 | UNLOCK; |
872 | |
959 | |
873 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), |
960 | coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), |
874 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
961 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
875 | } |
962 | } |
876 | |
963 | |
877 | static void |
964 | static void |
878 | api_cede (void) |
965 | api_cede (void) |
879 | { |
966 | { |
… | |
… | |
886 | api_schedule (); |
973 | api_schedule (); |
887 | } |
974 | } |
888 | |
975 | |
889 | MODULE = Coro::State PACKAGE = Coro::State |
976 | MODULE = Coro::State PACKAGE = Coro::State |
890 | |
977 | |
891 | PROTOTYPES: ENABLE |
978 | PROTOTYPES: DISABLE |
892 | |
979 | |
893 | BOOT: |
980 | BOOT: |
894 | { /* {} necessary for stoopid perl-5.6.x */ |
981 | { /* {} necessary for stoopid perl-5.6.x */ |
895 | #ifdef USE_ITHREADS |
982 | #ifdef USE_ITHREADS |
896 | MUTEX_INIT (&coro_mutex); |
983 | MUTEX_INIT (&coro_mutex); |
897 | #endif |
984 | #endif |
|
|
985 | BOOT_PAGESIZE; |
898 | |
986 | |
899 | ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); |
987 | ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); |
900 | PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); |
988 | PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); |
901 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
989 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
902 | |
990 | |
… | |
… | |
907 | |
995 | |
908 | main_mainstack = PL_mainstack; |
996 | main_mainstack = PL_mainstack; |
909 | |
997 | |
910 | coroapi.ver = CORO_API_VERSION; |
998 | coroapi.ver = CORO_API_VERSION; |
911 | coroapi.transfer = api_transfer; |
999 | coroapi.transfer = api_transfer; |
912 | } |
|
|
913 | |
1000 | |
914 | Coro::State |
1001 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
915 | _newprocess(args) |
1002 | } |
916 | SV * args |
1003 | |
917 | PROTOTYPE: $ |
1004 | SV * |
|
|
1005 | new (char *klass, ...) |
918 | CODE: |
1006 | CODE: |
919 | Coro__State coro; |
1007 | { |
|
|
1008 | struct coro *coro; |
|
|
1009 | HV *hv; |
|
|
1010 | int i; |
920 | |
1011 | |
921 | if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) |
|
|
922 | croak ("Coro::State::_newprocess expects an arrayref"); |
|
|
923 | |
|
|
924 | Newz (0, coro, 1, struct coro); |
1012 | Newz (0, coro, 1, struct coro); |
|
|
1013 | coro->args = newAV (); |
925 | |
1014 | |
926 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
1015 | for (i = 1; i < items; i++) |
|
|
1016 | av_push (coro->args, newSVsv (ST (i))); |
|
|
1017 | |
927 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
1018 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
928 | /*coro->stack = 0;*/ |
1019 | /*coro->stack = 0;*/ |
929 | |
1020 | |
930 | /* same as JMPENV_BOOTSTRAP */ |
1021 | hv = newHV (); |
931 | /* we might be able to recycle start_env, but safe is safe */ |
1022 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
932 | /*Zero(&coro->start_env, 1, JMPENV);*/ |
1023 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
933 | coro->start_env.je_ret = -1; |
1024 | } |
934 | coro->start_env.je_mustcatch = TRUE; |
|
|
935 | |
|
|
936 | RETVAL = coro; |
|
|
937 | OUTPUT: |
1025 | OUTPUT: |
938 | RETVAL |
1026 | RETVAL |
939 | |
1027 | |
940 | void |
1028 | void |
941 | transfer(prev, next, flags) |
1029 | transfer(prev, next, flags) |
942 | SV *prev |
1030 | SV *prev |
943 | SV *next |
1031 | SV *next |
944 | int flags |
1032 | int flags |
945 | PROTOTYPE: @ |
|
|
946 | CODE: |
1033 | CODE: |
947 | PUTBACK; |
1034 | PUTBACK; |
948 | SV_CORO (next, "Coro::transfer"); |
1035 | SV_CORO (next, "Coro::transfer"); |
949 | SV_CORO (prev, "Coro::transfer"); |
1036 | SV_CORO (prev, "Coro::transfer"); |
950 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
1037 | coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
951 | SPAGAIN; |
1038 | SPAGAIN; |
952 | |
1039 | |
|
|
1040 | int |
|
|
1041 | prio (Coro::State coro, int newprio = 0) |
|
|
1042 | ALIAS: |
|
|
1043 | nice = 1 |
|
|
1044 | CODE: |
|
|
1045 | { |
|
|
1046 | RETVAL = coro->prio; |
|
|
1047 | |
|
|
1048 | if (items > 1) |
|
|
1049 | { |
|
|
1050 | if (ix) |
|
|
1051 | newprio += coro->prio; |
|
|
1052 | |
|
|
1053 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
|
|
1054 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
|
|
1055 | |
|
|
1056 | coro->prio = newprio; |
|
|
1057 | } |
|
|
1058 | } |
|
|
1059 | |
953 | void |
1060 | void |
954 | DESTROY(coro) |
1061 | _clone_state_from (SV *dst, SV *src) |
955 | Coro::State coro |
1062 | CODE: |
956 | CODE: |
1063 | { |
|
|
1064 | struct coro *coro_src = SvSTATE (src); |
957 | |
1065 | |
958 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1066 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
959 | { |
|
|
960 | struct coro temp; |
|
|
961 | |
1067 | |
962 | PUTBACK; |
1068 | ++coro_src->refcnt; |
963 | SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); |
1069 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
964 | LOAD (aTHX_ coro); |
1070 | } |
965 | SPAGAIN; |
|
|
966 | |
|
|
967 | destroy_stacks (aTHX); |
|
|
968 | |
|
|
969 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
|
|
970 | SPAGAIN; |
|
|
971 | |
|
|
972 | coro->mainstack = 0; |
|
|
973 | } |
|
|
974 | |
|
|
975 | deallocate_stack (coro); |
|
|
976 | SvREFCNT_dec (coro->args); |
|
|
977 | Safefree (coro); |
|
|
978 | |
1071 | |
979 | void |
1072 | void |
980 | _exit(code) |
1073 | _exit (code) |
981 | int code |
1074 | int code |
982 | PROTOTYPE: $ |
1075 | PROTOTYPE: $ |
983 | CODE: |
1076 | CODE: |
984 | _exit (code); |
1077 | _exit (code); |
985 | |
1078 | |
986 | MODULE = Coro::State PACKAGE = Coro::Cont |
1079 | MODULE = Coro::State PACKAGE = Coro::Cont |
987 | |
1080 | |
988 | # this is slightly dirty (should expose a c-level api) |
|
|
989 | |
|
|
990 | void |
1081 | void |
991 | yield(...) |
1082 | yield (...) |
992 | PROTOTYPE: @ |
1083 | PROTOTYPE: @ |
993 | CODE: |
1084 | CODE: |
994 | SV *yieldstack; |
1085 | SV *yieldstack; |
995 | SV *sv; |
1086 | SV *sv; |
996 | AV *defav = GvAV (PL_defgv); |
1087 | AV *defav = GvAV (PL_defgv); |
… | |
… | |
1007 | av_fill (defav, items - 1); |
1098 | av_fill (defav, items - 1); |
1008 | while (items--) |
1099 | while (items--) |
1009 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
1100 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
1010 | |
1101 | |
1011 | sv = av_pop ((AV *)SvRV (yieldstack)); |
1102 | sv = av_pop ((AV *)SvRV (yieldstack)); |
1012 | prev = INT2PTR (struct coro *, SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0)))); |
1103 | prev = SvSTATE ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); |
1013 | next = INT2PTR (struct coro *, SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)))); |
1104 | next = SvSTATE ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
1014 | SvREFCNT_dec (sv); |
1105 | SvREFCNT_dec (sv); |
1015 | |
1106 | |
1016 | transfer (aTHX_ prev, next, 0); |
1107 | coro_state_transfer (aTHX_ prev, next, 0); |
1017 | |
1108 | |
1018 | MODULE = Coro::State PACKAGE = Coro |
1109 | MODULE = Coro::State PACKAGE = Coro |
1019 | |
|
|
1020 | # this is slightly dirty (should expose a c-level api) |
|
|
1021 | |
1110 | |
1022 | BOOT: |
1111 | BOOT: |
1023 | { |
1112 | { |
1024 | int i; |
1113 | int i; |
1025 | HV *stash = gv_stashpv ("Coro", TRUE); |
1114 | HV *stash = gv_stashpv ("Coro", TRUE); |
… | |
… | |
1045 | coroapi.ready = api_ready; |
1134 | coroapi.ready = api_ready; |
1046 | coroapi.nready = &coro_nready; |
1135 | coroapi.nready = &coro_nready; |
1047 | coroapi.current = coro_current; |
1136 | coroapi.current = coro_current; |
1048 | |
1137 | |
1049 | GCoroAPI = &coroapi; |
1138 | GCoroAPI = &coroapi; |
1050 | sv_setiv(sv, (IV)&coroapi); |
1139 | sv_setiv (sv, (IV)&coroapi); |
1051 | SvREADONLY_on(sv); |
1140 | SvREADONLY_on (sv); |
1052 | } |
1141 | } |
1053 | } |
1142 | } |
1054 | |
1143 | |
1055 | #if !PERL_MICRO |
1144 | #if !PERL_MICRO |
1056 | |
1145 | |
1057 | void |
1146 | void |
1058 | ready(self) |
1147 | ready (self) |
1059 | SV * self |
1148 | SV * self |
1060 | PROTOTYPE: $ |
1149 | PROTOTYPE: $ |
1061 | CODE: |
1150 | CODE: |
1062 | api_ready (self); |
1151 | api_ready (self); |
1063 | |
1152 | |
1064 | #endif |
1153 | #endif |
1065 | |
1154 | |
1066 | int |
1155 | int |
1067 | nready(...) |
1156 | nready (...) |
1068 | PROTOTYPE: |
1157 | PROTOTYPE: |
1069 | CODE: |
1158 | CODE: |
1070 | RETVAL = coro_nready; |
1159 | RETVAL = coro_nready; |
1071 | OUTPUT: |
1160 | OUTPUT: |
1072 | RETVAL |
1161 | RETVAL |
1073 | |
1162 | |
1074 | void |
1163 | void |
1075 | schedule(...) |
1164 | schedule (...) |
1076 | PROTOTYPE: |
1165 | PROTOTYPE: |
1077 | CODE: |
1166 | CODE: |
1078 | api_schedule (); |
1167 | api_schedule (); |
1079 | |
1168 | |
1080 | void |
1169 | void |
1081 | cede(...) |
1170 | cede (...) |
1082 | PROTOTYPE: |
1171 | PROTOTYPE: |
1083 | CODE: |
1172 | CODE: |
1084 | api_cede (); |
1173 | api_cede (); |
1085 | |
1174 | |
1086 | # and these are hacks |
1175 | # and these are hacks |