… | |
… | |
93 | |
93 | |
94 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
94 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
95 | |
95 | |
96 | #if __GNUC__ >= 3 |
96 | #if __GNUC__ >= 3 |
97 | # define attribute(x) __attribute__(x) |
97 | # define attribute(x) __attribute__(x) |
|
|
98 | # define BARRIER __asm__ __volatile__ ("" : : : "memory") |
98 | #else |
99 | #else |
99 | # define attribute(x) |
100 | # define attribute(x) |
|
|
101 | # define BARRIER |
100 | #endif |
102 | #endif |
101 | |
103 | |
102 | #define NOINLINE attribute ((noinline)) |
104 | #define NOINLINE attribute ((noinline)) |
103 | |
105 | |
104 | #include "CoroAPI.h" |
106 | #include "CoroAPI.h" |
… | |
… | |
110 | #else |
112 | #else |
111 | # define LOCK (void)0 |
113 | # define LOCK (void)0 |
112 | # define UNLOCK (void)0 |
114 | # define UNLOCK (void)0 |
113 | #endif |
115 | #endif |
114 | |
116 | |
|
|
117 | /* helper storage struct for Coro::AIO */ |
115 | struct io_state |
118 | struct io_state |
116 | { |
119 | { |
117 | int errorno; |
120 | int errorno; |
118 | I32 laststype; |
121 | I32 laststype; |
119 | int laststatval; |
122 | int laststatval; |
… | |
… | |
135 | /* the stack */ |
138 | /* the stack */ |
136 | void *sptr; |
139 | void *sptr; |
137 | long ssize; /* positive == mmap, otherwise malloc */ |
140 | long ssize; /* positive == mmap, otherwise malloc */ |
138 | |
141 | |
139 | /* cpu state */ |
142 | /* cpu state */ |
140 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
143 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
|
|
144 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
141 | JMPENV *top_env; |
145 | JMPENV *top_env; |
142 | coro_context cctx; |
146 | coro_context cctx; |
143 | |
147 | |
144 | int inuse; |
148 | int inuse; |
145 | |
149 | |
… | |
… | |
147 | int valgrind_id; |
151 | int valgrind_id; |
148 | #endif |
152 | #endif |
149 | } coro_cctx; |
153 | } coro_cctx; |
150 | |
154 | |
151 | enum { |
155 | enum { |
152 | CF_RUNNING = 0x0001, /* coroutine is running */ |
156 | CF_RUNNING = 0x0001, /* coroutine is running */ |
153 | CF_READY = 0x0002, /* coroutine is ready */ |
157 | CF_READY = 0x0002, /* coroutine is ready */ |
154 | CF_NEW = 0x0004, /* ahs never been switched to */ |
158 | CF_NEW = 0x0004, /* has never been switched to */ |
|
|
159 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
155 | }; |
160 | }; |
156 | |
161 | |
157 | /* this is a structure representing a perl-level coroutine */ |
162 | /* this is a structure representing a perl-level coroutine */ |
158 | struct coro { |
163 | struct coro { |
159 | /* the c coroutine allocated to this perl coroutine, if any */ |
164 | /* the c coroutine allocated to this perl coroutine, if any */ |
… | |
… | |
180 | int prio; |
185 | int prio; |
181 | }; |
186 | }; |
182 | |
187 | |
183 | typedef struct coro *Coro__State; |
188 | typedef struct coro *Coro__State; |
184 | typedef struct coro *Coro__State_or_hashref; |
189 | typedef struct coro *Coro__State_or_hashref; |
|
|
190 | |
|
|
191 | /** Coro ********************************************************************/ |
|
|
192 | |
|
|
193 | #define PRIO_MAX 3 |
|
|
194 | #define PRIO_HIGH 1 |
|
|
195 | #define PRIO_NORMAL 0 |
|
|
196 | #define PRIO_LOW -1 |
|
|
197 | #define PRIO_IDLE -3 |
|
|
198 | #define PRIO_MIN -4 |
|
|
199 | |
|
|
200 | /* for Coro.pm */ |
|
|
201 | static SV *coro_current; |
|
|
202 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
|
|
203 | static int coro_nready; |
|
|
204 | |
|
|
205 | /** lowlevel stuff **********************************************************/ |
185 | |
206 | |
186 | static AV * |
207 | static AV * |
187 | coro_clone_padlist (CV *cv) |
208 | coro_clone_padlist (CV *cv) |
188 | { |
209 | { |
189 | AV *padlist = CvPADLIST (cv); |
210 | AV *padlist = CvPADLIST (cv); |
… | |
… | |
298 | av_extend (av, AvMAX (av) + 1); |
319 | av_extend (av, AvMAX (av) + 1); |
299 | |
320 | |
300 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
321 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
301 | } |
322 | } |
302 | |
323 | |
|
|
324 | /** load & save, init *******************************************************/ |
|
|
325 | |
303 | #define SB do { |
326 | #define SB do { |
304 | #define SE } while (0) |
327 | #define SE } while (0) |
305 | |
328 | |
306 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
329 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
307 | |
330 | |
… | |
… | |
468 | |
491 | |
469 | /* free all temporaries */ |
492 | /* free all temporaries */ |
470 | FREETMPS; |
493 | FREETMPS; |
471 | assert (PL_tmps_ix == -1); |
494 | assert (PL_tmps_ix == -1); |
472 | |
495 | |
|
|
496 | /* unwind all extra stacks */ |
473 | POPSTACK_TO (PL_mainstack); |
497 | POPSTACK_TO (PL_mainstack); |
|
|
498 | |
|
|
499 | /* unwind main stack */ |
|
|
500 | dounwind (-1); |
474 | } |
501 | } |
475 | |
502 | |
476 | while (PL_curstackinfo->si_next) |
503 | while (PL_curstackinfo->si_next) |
477 | PL_curstackinfo = PL_curstackinfo->si_next; |
504 | PL_curstackinfo = PL_curstackinfo->si_next; |
478 | |
505 | |
… | |
… | |
494 | Safefree (PL_savestack); |
521 | Safefree (PL_savestack); |
495 | #if !PERL_VERSION_ATLEAST (5,9,0) |
522 | #if !PERL_VERSION_ATLEAST (5,9,0) |
496 | Safefree (PL_retstack); |
523 | Safefree (PL_retstack); |
497 | #endif |
524 | #endif |
498 | } |
525 | } |
|
|
526 | |
|
|
527 | /** coroutine stack handling ************************************************/ |
499 | |
528 | |
500 | static void |
529 | static void |
501 | setup_coro (struct coro *coro) |
530 | setup_coro (struct coro *coro) |
502 | { |
531 | { |
503 | /* |
532 | /* |
… | |
… | |
696 | ++cctx_idle; |
725 | ++cctx_idle; |
697 | cctx->next = cctx_first; |
726 | cctx->next = cctx_first; |
698 | cctx_first = cctx; |
727 | cctx_first = cctx; |
699 | } |
728 | } |
700 | |
729 | |
|
|
730 | /** coroutine switching *****************************************************/ |
|
|
731 | |
701 | /* never call directly, always through the coro_state_transfer global variable */ |
732 | /* never call directly, always through the coro_state_transfer global variable */ |
702 | static void NOINLINE |
733 | static void NOINLINE |
703 | transfer (struct coro *prev, struct coro *next) |
734 | transfer (struct coro *prev, struct coro *next) |
704 | { |
735 | { |
705 | dSTACKLEVEL; |
736 | dSTACKLEVEL; |
706 | |
737 | |
707 | /* sometimes transfer is only called to set idle_sp */ |
738 | /* sometimes transfer is only called to set idle_sp */ |
708 | if (!next) |
739 | if (!next) |
709 | { |
740 | { |
710 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
741 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
711 | assert (((coro_cctx *)prev)->top_env = PL_top_env); /* just for the side effetc when assert is enabled */ |
742 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
712 | } |
743 | } |
713 | else if (prev != next) |
744 | else if (prev != next) |
714 | { |
745 | { |
715 | coro_cctx *prev__cctx; |
746 | coro_cctx *prev__cctx; |
716 | |
747 | |
… | |
… | |
727 | if (!prev->flags & CF_RUNNING) |
758 | if (!prev->flags & CF_RUNNING) |
728 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
759 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
729 | |
760 | |
730 | if (next->flags & CF_RUNNING) |
761 | if (next->flags & CF_RUNNING) |
731 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
762 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
|
|
763 | |
|
|
764 | if (next->flags & CF_DESTROYED) |
|
|
765 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
732 | |
766 | |
733 | prev->flags &= ~CF_RUNNING; |
767 | prev->flags &= ~CF_RUNNING; |
734 | next->flags |= CF_RUNNING; |
768 | next->flags |= CF_RUNNING; |
735 | |
769 | |
736 | LOCK; |
770 | LOCK; |
… | |
… | |
757 | |
791 | |
758 | /* possibly "free" the cctx */ |
792 | /* possibly "free" the cctx */ |
759 | if (prev__cctx->idle_sp == STACKLEVEL) |
793 | if (prev__cctx->idle_sp == STACKLEVEL) |
760 | { |
794 | { |
761 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
795 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
762 | assert (PL_top_env == prev__cctx->top_env); |
796 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
763 | |
797 | |
764 | prev->cctx = 0; |
798 | prev->cctx = 0; |
765 | |
799 | |
766 | cctx_put (prev__cctx); |
800 | cctx_put (prev__cctx); |
767 | prev__cctx->inuse = 0; |
801 | prev__cctx->inuse = 0; |
… | |
… | |
780 | PL_top_env = next->cctx->top_env; |
814 | PL_top_env = next->cctx->top_env; |
781 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
815 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
782 | } |
816 | } |
783 | |
817 | |
784 | free_coro_mortal (); |
818 | free_coro_mortal (); |
785 | |
|
|
786 | UNLOCK; |
819 | UNLOCK; |
787 | } |
820 | } |
788 | } |
821 | } |
789 | |
822 | |
790 | struct transfer_args |
823 | struct transfer_args |
… | |
… | |
792 | struct coro *prev, *next; |
825 | struct coro *prev, *next; |
793 | }; |
826 | }; |
794 | |
827 | |
795 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
828 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
796 | |
829 | |
|
|
830 | /** high level stuff ********************************************************/ |
|
|
831 | |
797 | static void |
832 | static int |
798 | coro_state_destroy (struct coro *coro) |
833 | coro_state_destroy (struct coro *coro) |
799 | { |
834 | { |
800 | if (coro->refcnt--) |
835 | if (coro->flags & CF_DESTROYED) |
801 | return; |
836 | return 0; |
|
|
837 | |
|
|
838 | coro->flags |= CF_DESTROYED; |
|
|
839 | |
|
|
840 | if (coro->flags & CF_READY) |
|
|
841 | { |
|
|
842 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
|
|
843 | /* alternative: look through all ready queues and remove the coro */ |
|
|
844 | LOCK; |
|
|
845 | --coro_nready; |
|
|
846 | UNLOCK; |
|
|
847 | } |
|
|
848 | else |
|
|
849 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
802 | |
850 | |
803 | if (coro->mainstack && coro->mainstack != main_mainstack) |
851 | if (coro->mainstack && coro->mainstack != main_mainstack) |
804 | { |
852 | { |
805 | struct coro temp; |
853 | struct coro temp; |
|
|
854 | |
|
|
855 | assert (!(coro->flags & CF_RUNNING)); |
|
|
856 | |
806 | Zero (&temp, 1, struct coro); |
857 | Zero (&temp, 1, struct coro); |
807 | temp.save = CORO_SAVE_ALL; |
858 | temp.save = CORO_SAVE_ALL; |
808 | |
859 | |
809 | if (coro->flags & CF_RUNNING) |
860 | if (coro->flags & CF_RUNNING) |
810 | croak ("FATAL: tried to destroy currently running coroutine"); |
861 | croak ("FATAL: tried to destroy currently running coroutine"); |
… | |
… | |
819 | coro->mainstack = 0; |
870 | coro->mainstack = 0; |
820 | } |
871 | } |
821 | |
872 | |
822 | cctx_destroy (coro->cctx); |
873 | cctx_destroy (coro->cctx); |
823 | SvREFCNT_dec (coro->args); |
874 | SvREFCNT_dec (coro->args); |
824 | Safefree (coro); |
875 | |
|
|
876 | return 1; |
825 | } |
877 | } |
826 | |
878 | |
827 | static int |
879 | static int |
828 | coro_state_clear (pTHX_ SV *sv, MAGIC *mg) |
880 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
829 | { |
881 | { |
830 | struct coro *coro = (struct coro *)mg->mg_ptr; |
882 | struct coro *coro = (struct coro *)mg->mg_ptr; |
831 | mg->mg_ptr = 0; |
883 | mg->mg_ptr = 0; |
832 | |
884 | |
|
|
885 | if (--coro->refcnt < 0) |
|
|
886 | { |
833 | coro_state_destroy (coro); |
887 | coro_state_destroy (coro); |
|
|
888 | Safefree (coro); |
|
|
889 | } |
834 | |
890 | |
835 | return 0; |
891 | return 0; |
836 | } |
892 | } |
837 | |
893 | |
838 | static int |
894 | static int |
… | |
… | |
845 | return 0; |
901 | return 0; |
846 | } |
902 | } |
847 | |
903 | |
848 | static MGVTBL coro_state_vtbl = { |
904 | static MGVTBL coro_state_vtbl = { |
849 | 0, 0, 0, 0, |
905 | 0, 0, 0, 0, |
850 | coro_state_clear, |
906 | coro_state_free, |
851 | 0, |
907 | 0, |
852 | #ifdef MGf_DUP |
908 | #ifdef MGf_DUP |
853 | coro_state_dup, |
909 | coro_state_dup, |
854 | #else |
910 | #else |
855 | # define MGf_DUP 0 |
911 | # define MGf_DUP 0 |
… | |
… | |
906 | return old_save; |
962 | return old_save; |
907 | } |
963 | } |
908 | |
964 | |
909 | /** Coro ********************************************************************/ |
965 | /** Coro ********************************************************************/ |
910 | |
966 | |
911 | #define PRIO_MAX 3 |
|
|
912 | #define PRIO_HIGH 1 |
|
|
913 | #define PRIO_NORMAL 0 |
|
|
914 | #define PRIO_LOW -1 |
|
|
915 | #define PRIO_IDLE -3 |
|
|
916 | #define PRIO_MIN -4 |
|
|
917 | |
|
|
918 | /* for Coro.pm */ |
|
|
919 | static SV *coro_current; |
|
|
920 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
|
|
921 | static int coro_nready; |
|
|
922 | |
|
|
923 | static void |
967 | static void |
924 | coro_enq (SV *coro_sv) |
968 | coro_enq (SV *coro_sv) |
925 | { |
969 | { |
926 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
970 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
927 | coro_nready++; |
|
|
928 | } |
971 | } |
929 | |
972 | |
930 | static SV * |
973 | static SV * |
931 | coro_deq (int min_prio) |
974 | coro_deq (int min_prio) |
932 | { |
975 | { |
… | |
… | |
936 | if (min_prio < 0) |
979 | if (min_prio < 0) |
937 | min_prio = 0; |
980 | min_prio = 0; |
938 | |
981 | |
939 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
982 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
940 | if (AvFILLp (coro_ready [prio]) >= 0) |
983 | if (AvFILLp (coro_ready [prio]) >= 0) |
941 | { |
|
|
942 | coro_nready--; |
|
|
943 | return av_shift (coro_ready [prio]); |
984 | return av_shift (coro_ready [prio]); |
944 | } |
|
|
945 | |
985 | |
946 | return 0; |
986 | return 0; |
947 | } |
987 | } |
948 | |
988 | |
949 | static int |
989 | static int |
… | |
… | |
961 | |
1001 | |
962 | coro->flags |= CF_READY; |
1002 | coro->flags |= CF_READY; |
963 | |
1003 | |
964 | LOCK; |
1004 | LOCK; |
965 | coro_enq (SvREFCNT_inc (coro_sv)); |
1005 | coro_enq (SvREFCNT_inc (coro_sv)); |
|
|
1006 | ++coro_nready; |
966 | UNLOCK; |
1007 | UNLOCK; |
967 | |
1008 | |
968 | return 1; |
1009 | return 1; |
969 | } |
1010 | } |
970 | |
1011 | |
971 | static int |
1012 | static int |
972 | api_is_ready (SV *coro_sv) |
1013 | api_is_ready (SV *coro_sv) |
973 | { |
1014 | { |
974 | return !!SvSTATE (coro_sv)->flags & CF_READY; |
1015 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
975 | } |
1016 | } |
976 | |
1017 | |
977 | static void |
1018 | static void |
978 | prepare_schedule (struct transfer_args *ta) |
1019 | prepare_schedule (struct transfer_args *ta) |
979 | { |
1020 | { |
980 | SV *prev, *next; |
1021 | SV *prev_sv, *next_sv; |
981 | |
1022 | |
982 | for (;;) |
1023 | for (;;) |
983 | { |
1024 | { |
984 | LOCK; |
1025 | LOCK; |
985 | next = coro_deq (PRIO_MIN); |
1026 | next_sv = coro_deq (PRIO_MIN); |
|
|
1027 | |
|
|
1028 | /* nothing to schedule: call the idle handler */ |
|
|
1029 | if (!next_sv) |
|
|
1030 | { |
|
|
1031 | dSP; |
|
|
1032 | UNLOCK; |
|
|
1033 | |
|
|
1034 | ENTER; |
|
|
1035 | SAVETMPS; |
|
|
1036 | |
|
|
1037 | PUSHMARK (SP); |
|
|
1038 | PUTBACK; |
|
|
1039 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
|
|
1040 | |
|
|
1041 | FREETMPS; |
|
|
1042 | LEAVE; |
|
|
1043 | continue; |
|
|
1044 | } |
|
|
1045 | |
|
|
1046 | ta->next = SvSTATE (next_sv); |
|
|
1047 | |
|
|
1048 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1049 | if (ta->next->flags & CF_DESTROYED) |
|
|
1050 | { |
|
|
1051 | UNLOCK; |
|
|
1052 | SvREFCNT_dec (next_sv); |
|
|
1053 | /* coro_nready is already taken care of by destroy */ |
|
|
1054 | continue; |
|
|
1055 | } |
|
|
1056 | |
|
|
1057 | --coro_nready; |
986 | UNLOCK; |
1058 | UNLOCK; |
987 | |
|
|
988 | if (next) |
|
|
989 | break; |
1059 | break; |
990 | |
|
|
991 | { |
|
|
992 | dSP; |
|
|
993 | |
|
|
994 | ENTER; |
|
|
995 | SAVETMPS; |
|
|
996 | |
|
|
997 | PUSHMARK (SP); |
|
|
998 | PUTBACK; |
|
|
999 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
|
|
1000 | |
|
|
1001 | FREETMPS; |
|
|
1002 | LEAVE; |
|
|
1003 | } |
1060 | } |
1004 | } |
|
|
1005 | |
|
|
1006 | prev = SvRV (coro_current); |
|
|
1007 | SvRV_set (coro_current, next); |
|
|
1008 | |
1061 | |
1009 | /* free this only after the transfer */ |
1062 | /* free this only after the transfer */ |
|
|
1063 | prev_sv = SvRV (coro_current); |
|
|
1064 | SvRV_set (coro_current, next_sv); |
|
|
1065 | ta->prev = SvSTATE (prev_sv); |
|
|
1066 | |
|
|
1067 | assert (ta->next->flags & CF_READY); |
|
|
1068 | ta->next->flags &= ~CF_READY; |
|
|
1069 | |
1010 | LOCK; |
1070 | LOCK; |
1011 | free_coro_mortal (); |
1071 | free_coro_mortal (); |
|
|
1072 | coro_mortal = prev_sv; |
1012 | UNLOCK; |
1073 | UNLOCK; |
1013 | coro_mortal = prev; |
|
|
1014 | |
|
|
1015 | assert (!SvROK(prev));//D |
|
|
1016 | assert (!SvROK(next));//D |
|
|
1017 | |
|
|
1018 | ta->prev = SvSTATE (prev); |
|
|
1019 | ta->next = SvSTATE (next); |
|
|
1020 | |
|
|
1021 | assert (ta->next->flags & CF_READY); |
|
|
1022 | ta->next->flags &= ~CF_READY; |
|
|
1023 | } |
1074 | } |
1024 | |
1075 | |
1025 | static void |
1076 | static void |
1026 | prepare_cede (struct transfer_args *ta) |
1077 | prepare_cede (struct transfer_args *ta) |
1027 | { |
1078 | { |
1028 | api_ready (coro_current); |
1079 | api_ready (coro_current); |
1029 | |
|
|
1030 | prepare_schedule (ta); |
1080 | prepare_schedule (ta); |
|
|
1081 | } |
|
|
1082 | |
|
|
1083 | static int |
|
|
1084 | prepare_cede_notself (struct transfer_args *ta) |
|
|
1085 | { |
|
|
1086 | if (coro_nready) |
|
|
1087 | { |
|
|
1088 | SV *prev = SvRV (coro_current); |
|
|
1089 | prepare_schedule (ta); |
|
|
1090 | api_ready (prev); |
|
|
1091 | return 1; |
|
|
1092 | } |
|
|
1093 | else |
|
|
1094 | return 0; |
1031 | } |
1095 | } |
1032 | |
1096 | |
1033 | static void |
1097 | static void |
1034 | api_schedule (void) |
1098 | api_schedule (void) |
1035 | { |
1099 | { |
… | |
… | |
1037 | |
1101 | |
1038 | prepare_schedule (&ta); |
1102 | prepare_schedule (&ta); |
1039 | TRANSFER (ta); |
1103 | TRANSFER (ta); |
1040 | } |
1104 | } |
1041 | |
1105 | |
1042 | static void |
1106 | static int |
1043 | api_cede (void) |
1107 | api_cede (void) |
1044 | { |
1108 | { |
1045 | struct transfer_args ta; |
1109 | struct transfer_args ta; |
1046 | |
1110 | |
1047 | prepare_cede (&ta); |
1111 | prepare_cede (&ta); |
|
|
1112 | |
|
|
1113 | if (ta.prev != ta.next) |
|
|
1114 | { |
1048 | TRANSFER (ta); |
1115 | TRANSFER (ta); |
|
|
1116 | return 1; |
|
|
1117 | } |
|
|
1118 | else |
|
|
1119 | return 0; |
|
|
1120 | } |
|
|
1121 | |
|
|
1122 | static int |
|
|
1123 | api_cede_notself (void) |
|
|
1124 | { |
|
|
1125 | struct transfer_args ta; |
|
|
1126 | |
|
|
1127 | if (prepare_cede_notself (&ta)) |
|
|
1128 | { |
|
|
1129 | TRANSFER (ta); |
|
|
1130 | return 1; |
|
|
1131 | } |
|
|
1132 | else |
|
|
1133 | return 0; |
1049 | } |
1134 | } |
1050 | |
1135 | |
1051 | MODULE = Coro::State PACKAGE = Coro::State |
1136 | MODULE = Coro::State PACKAGE = Coro::State |
1052 | |
1137 | |
1053 | PROTOTYPES: DISABLE |
1138 | PROTOTYPES: DISABLE |
… | |
… | |
1109 | _set_stacklevel (...) |
1194 | _set_stacklevel (...) |
1110 | ALIAS: |
1195 | ALIAS: |
1111 | Coro::State::transfer = 1 |
1196 | Coro::State::transfer = 1 |
1112 | Coro::schedule = 2 |
1197 | Coro::schedule = 2 |
1113 | Coro::cede = 3 |
1198 | Coro::cede = 3 |
|
|
1199 | Coro::cede_notself = 4 |
1114 | CODE: |
1200 | CODE: |
1115 | { |
1201 | { |
1116 | struct transfer_args ta; |
1202 | struct transfer_args ta; |
1117 | |
1203 | |
1118 | switch (ix) |
1204 | switch (ix) |
… | |
… | |
1134 | break; |
1220 | break; |
1135 | |
1221 | |
1136 | case 3: |
1222 | case 3: |
1137 | prepare_cede (&ta); |
1223 | prepare_cede (&ta); |
1138 | break; |
1224 | break; |
|
|
1225 | |
|
|
1226 | case 4: |
|
|
1227 | if (!prepare_cede_notself (&ta)) |
|
|
1228 | XSRETURN_EMPTY; |
|
|
1229 | |
|
|
1230 | break; |
1139 | } |
1231 | } |
1140 | |
1232 | |
|
|
1233 | BARRIER; |
1141 | TRANSFER (ta); |
1234 | TRANSFER (ta); |
1142 | } |
|
|
1143 | |
1235 | |
1144 | void |
1236 | if (GIMME_V != G_VOID && ta.next != ta.prev) |
1145 | _clone_state_from (SV *dst, SV *src) |
1237 | XSRETURN_YES; |
|
|
1238 | } |
|
|
1239 | |
|
|
1240 | bool |
|
|
1241 | _destroy (SV *coro_sv) |
1146 | CODE: |
1242 | CODE: |
1147 | { |
1243 | RETVAL = coro_state_destroy (SvSTATE (coro_sv)); |
1148 | struct coro *coro_src = SvSTATE (src); |
1244 | OUTPUT: |
1149 | |
1245 | RETVAL |
1150 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
|
|
1151 | |
|
|
1152 | ++coro_src->refcnt; |
|
|
1153 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
|
|
1154 | } |
|
|
1155 | |
1246 | |
1156 | void |
1247 | void |
1157 | _exit (code) |
1248 | _exit (code) |
1158 | int code |
1249 | int code |
1159 | PROTOTYPE: $ |
1250 | PROTOTYPE: $ |
… | |
… | |
1196 | coro_ready[i] = newAV (); |
1287 | coro_ready[i] = newAV (); |
1197 | |
1288 | |
1198 | { |
1289 | { |
1199 | SV *sv = perl_get_sv("Coro::API", 1); |
1290 | SV *sv = perl_get_sv("Coro::API", 1); |
1200 | |
1291 | |
1201 | coroapi.schedule = api_schedule; |
1292 | coroapi.schedule = api_schedule; |
1202 | coroapi.save = api_save; |
1293 | coroapi.save = api_save; |
1203 | coroapi.cede = api_cede; |
1294 | coroapi.cede = api_cede; |
|
|
1295 | coroapi.cede_notself = api_cede_notself; |
1204 | coroapi.ready = api_ready; |
1296 | coroapi.ready = api_ready; |
1205 | coroapi.is_ready = api_is_ready; |
1297 | coroapi.is_ready = api_is_ready; |
1206 | coroapi.nready = &coro_nready; |
1298 | coroapi.nready = &coro_nready; |
1207 | coroapi.current = coro_current; |
1299 | coroapi.current = coro_current; |
1208 | |
1300 | |
1209 | GCoroAPI = &coroapi; |
1301 | GCoroAPI = &coroapi; |
1210 | sv_setiv (sv, (IV)&coroapi); |
1302 | sv_setiv (sv, (IV)&coroapi); |
1211 | SvREADONLY_on (sv); |
1303 | SvREADONLY_on (sv); |
1212 | } |
1304 | } |
… | |
… | |
1236 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
1328 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
1237 | |
1329 | |
1238 | coro->prio = newprio; |
1330 | coro->prio = newprio; |
1239 | } |
1331 | } |
1240 | } |
1332 | } |
|
|
1333 | OUTPUT: |
|
|
1334 | RETVAL |
1241 | |
1335 | |
1242 | SV * |
1336 | SV * |
1243 | ready (SV *self) |
1337 | ready (SV *self) |
1244 | PROTOTYPE: $ |
1338 | PROTOTYPE: $ |
1245 | CODE: |
1339 | CODE: |