… | |
… | |
112 | #else |
112 | #else |
113 | # define LOCK (void)0 |
113 | # define LOCK (void)0 |
114 | # define UNLOCK (void)0 |
114 | # define UNLOCK (void)0 |
115 | #endif |
115 | #endif |
116 | |
116 | |
|
|
117 | /* helper storage struct for Coro::AIO */ |
117 | struct io_state |
118 | struct io_state |
118 | { |
119 | { |
119 | int errorno; |
120 | int errorno; |
120 | I32 laststype; |
121 | I32 laststype; |
121 | int laststatval; |
122 | int laststatval; |
… | |
… | |
150 | int valgrind_id; |
151 | int valgrind_id; |
151 | #endif |
152 | #endif |
152 | } coro_cctx; |
153 | } coro_cctx; |
153 | |
154 | |
154 | enum { |
155 | enum { |
155 | CF_RUNNING = 0x0001, /* coroutine is running */ |
156 | CF_RUNNING = 0x0001, /* coroutine is running */ |
156 | CF_READY = 0x0002, /* coroutine is ready */ |
157 | CF_READY = 0x0002, /* coroutine is ready */ |
157 | 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 */ |
158 | }; |
160 | }; |
159 | |
161 | |
160 | /* this is a structure representing a perl-level coroutine */ |
162 | /* this is a structure representing a perl-level coroutine */ |
161 | struct coro { |
163 | struct coro { |
162 | /* the c coroutine allocated to this perl coroutine, if any */ |
164 | /* the c coroutine allocated to this perl coroutine, if any */ |
… | |
… | |
183 | int prio; |
185 | int prio; |
184 | }; |
186 | }; |
185 | |
187 | |
186 | typedef struct coro *Coro__State; |
188 | typedef struct coro *Coro__State; |
187 | 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 **********************************************************/ |
188 | |
206 | |
189 | static AV * |
207 | static AV * |
190 | coro_clone_padlist (CV *cv) |
208 | coro_clone_padlist (CV *cv) |
191 | { |
209 | { |
192 | AV *padlist = CvPADLIST (cv); |
210 | AV *padlist = CvPADLIST (cv); |
… | |
… | |
301 | av_extend (av, AvMAX (av) + 1); |
319 | av_extend (av, AvMAX (av) + 1); |
302 | |
320 | |
303 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
321 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
304 | } |
322 | } |
305 | |
323 | |
|
|
324 | /** load & save, init *******************************************************/ |
|
|
325 | |
306 | #define SB do { |
326 | #define SB do { |
307 | #define SE } while (0) |
327 | #define SE } while (0) |
308 | |
328 | |
309 | #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 |
310 | |
330 | |
… | |
… | |
471 | |
491 | |
472 | /* free all temporaries */ |
492 | /* free all temporaries */ |
473 | FREETMPS; |
493 | FREETMPS; |
474 | assert (PL_tmps_ix == -1); |
494 | assert (PL_tmps_ix == -1); |
475 | |
495 | |
|
|
496 | /* unwind all extra stacks */ |
476 | POPSTACK_TO (PL_mainstack); |
497 | POPSTACK_TO (PL_mainstack); |
|
|
498 | |
|
|
499 | /* unwind main stack */ |
|
|
500 | dounwind (-1); |
477 | } |
501 | } |
478 | |
502 | |
479 | while (PL_curstackinfo->si_next) |
503 | while (PL_curstackinfo->si_next) |
480 | PL_curstackinfo = PL_curstackinfo->si_next; |
504 | PL_curstackinfo = PL_curstackinfo->si_next; |
481 | |
505 | |
… | |
… | |
497 | Safefree (PL_savestack); |
521 | Safefree (PL_savestack); |
498 | #if !PERL_VERSION_ATLEAST (5,9,0) |
522 | #if !PERL_VERSION_ATLEAST (5,9,0) |
499 | Safefree (PL_retstack); |
523 | Safefree (PL_retstack); |
500 | #endif |
524 | #endif |
501 | } |
525 | } |
|
|
526 | |
|
|
527 | /** coroutine stack handling ************************************************/ |
502 | |
528 | |
503 | static void |
529 | static void |
504 | setup_coro (struct coro *coro) |
530 | setup_coro (struct coro *coro) |
505 | { |
531 | { |
506 | /* |
532 | /* |
… | |
… | |
699 | ++cctx_idle; |
725 | ++cctx_idle; |
700 | cctx->next = cctx_first; |
726 | cctx->next = cctx_first; |
701 | cctx_first = cctx; |
727 | cctx_first = cctx; |
702 | } |
728 | } |
703 | |
729 | |
|
|
730 | /** coroutine switching *****************************************************/ |
|
|
731 | |
704 | /* never call directly, always through the coro_state_transfer global variable */ |
732 | /* never call directly, always through the coro_state_transfer global variable */ |
705 | static void NOINLINE |
733 | static void NOINLINE |
706 | transfer (struct coro *prev, struct coro *next) |
734 | transfer (struct coro *prev, struct coro *next) |
707 | { |
735 | { |
708 | dSTACKLEVEL; |
736 | dSTACKLEVEL; |
… | |
… | |
730 | if (!prev->flags & CF_RUNNING) |
758 | if (!prev->flags & CF_RUNNING) |
731 | 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"); |
732 | |
760 | |
733 | if (next->flags & CF_RUNNING) |
761 | if (next->flags & CF_RUNNING) |
734 | 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"); |
735 | |
766 | |
736 | prev->flags &= ~CF_RUNNING; |
767 | prev->flags &= ~CF_RUNNING; |
737 | next->flags |= CF_RUNNING; |
768 | next->flags |= CF_RUNNING; |
738 | |
769 | |
739 | LOCK; |
770 | LOCK; |
… | |
… | |
783 | PL_top_env = next->cctx->top_env; |
814 | PL_top_env = next->cctx->top_env; |
784 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
815 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
785 | } |
816 | } |
786 | |
817 | |
787 | free_coro_mortal (); |
818 | free_coro_mortal (); |
788 | |
|
|
789 | UNLOCK; |
819 | UNLOCK; |
790 | } |
820 | } |
791 | } |
821 | } |
792 | |
822 | |
793 | struct transfer_args |
823 | struct transfer_args |
… | |
… | |
795 | struct coro *prev, *next; |
825 | struct coro *prev, *next; |
796 | }; |
826 | }; |
797 | |
827 | |
798 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
828 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
799 | |
829 | |
|
|
830 | /** high level stuff ********************************************************/ |
|
|
831 | |
800 | static void |
832 | static int |
801 | coro_state_destroy (struct coro *coro) |
833 | coro_state_destroy (struct coro *coro) |
802 | { |
834 | { |
803 | if (coro->refcnt--) |
835 | if (coro->flags & CF_DESTROYED) |
804 | 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 it */ |
|
|
844 | LOCK; |
|
|
845 | --coro_nready; |
|
|
846 | UNLOCK; |
|
|
847 | } |
|
|
848 | else |
|
|
849 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
805 | |
850 | |
806 | if (coro->mainstack && coro->mainstack != main_mainstack) |
851 | if (coro->mainstack && coro->mainstack != main_mainstack) |
807 | { |
852 | { |
|
|
853 | assert (!(coro->flags & CF_RUNNING)); |
|
|
854 | |
808 | struct coro temp; |
855 | struct coro temp; |
809 | Zero (&temp, 1, struct coro); |
856 | Zero (&temp, 1, struct coro); |
810 | temp.save = CORO_SAVE_ALL; |
857 | temp.save = CORO_SAVE_ALL; |
811 | |
858 | |
812 | if (coro->flags & CF_RUNNING) |
859 | if (coro->flags & CF_RUNNING) |
… | |
… | |
822 | coro->mainstack = 0; |
869 | coro->mainstack = 0; |
823 | } |
870 | } |
824 | |
871 | |
825 | cctx_destroy (coro->cctx); |
872 | cctx_destroy (coro->cctx); |
826 | SvREFCNT_dec (coro->args); |
873 | SvREFCNT_dec (coro->args); |
827 | Safefree (coro); |
874 | |
|
|
875 | return 1; |
828 | } |
876 | } |
829 | |
877 | |
830 | static int |
878 | static int |
831 | coro_state_clear (pTHX_ SV *sv, MAGIC *mg) |
879 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
832 | { |
880 | { |
833 | struct coro *coro = (struct coro *)mg->mg_ptr; |
881 | struct coro *coro = (struct coro *)mg->mg_ptr; |
834 | mg->mg_ptr = 0; |
882 | mg->mg_ptr = 0; |
835 | |
883 | |
|
|
884 | if (--coro->refcnt < 0) |
|
|
885 | { |
836 | coro_state_destroy (coro); |
886 | coro_state_destroy (coro); |
|
|
887 | Safefree (coro); |
|
|
888 | } |
837 | |
889 | |
838 | return 0; |
890 | return 0; |
839 | } |
891 | } |
840 | |
892 | |
841 | static int |
893 | static int |
… | |
… | |
848 | return 0; |
900 | return 0; |
849 | } |
901 | } |
850 | |
902 | |
851 | static MGVTBL coro_state_vtbl = { |
903 | static MGVTBL coro_state_vtbl = { |
852 | 0, 0, 0, 0, |
904 | 0, 0, 0, 0, |
853 | coro_state_clear, |
905 | coro_state_free, |
854 | 0, |
906 | 0, |
855 | #ifdef MGf_DUP |
907 | #ifdef MGf_DUP |
856 | coro_state_dup, |
908 | coro_state_dup, |
857 | #else |
909 | #else |
858 | # define MGf_DUP 0 |
910 | # define MGf_DUP 0 |
… | |
… | |
909 | return old_save; |
961 | return old_save; |
910 | } |
962 | } |
911 | |
963 | |
912 | /** Coro ********************************************************************/ |
964 | /** Coro ********************************************************************/ |
913 | |
965 | |
914 | #define PRIO_MAX 3 |
|
|
915 | #define PRIO_HIGH 1 |
|
|
916 | #define PRIO_NORMAL 0 |
|
|
917 | #define PRIO_LOW -1 |
|
|
918 | #define PRIO_IDLE -3 |
|
|
919 | #define PRIO_MIN -4 |
|
|
920 | |
|
|
921 | /* for Coro.pm */ |
|
|
922 | static SV *coro_current; |
|
|
923 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
|
|
924 | static int coro_nready; |
|
|
925 | |
|
|
926 | static void |
966 | static void |
927 | coro_enq (SV *coro_sv) |
967 | coro_enq (SV *coro_sv) |
928 | { |
968 | { |
929 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
969 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
930 | coro_nready++; |
|
|
931 | } |
970 | } |
932 | |
971 | |
933 | static SV * |
972 | static SV * |
934 | coro_deq (int min_prio) |
973 | coro_deq (int min_prio) |
935 | { |
974 | { |
… | |
… | |
939 | if (min_prio < 0) |
978 | if (min_prio < 0) |
940 | min_prio = 0; |
979 | min_prio = 0; |
941 | |
980 | |
942 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
981 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
943 | if (AvFILLp (coro_ready [prio]) >= 0) |
982 | if (AvFILLp (coro_ready [prio]) >= 0) |
944 | { |
|
|
945 | coro_nready--; |
|
|
946 | return av_shift (coro_ready [prio]); |
983 | return av_shift (coro_ready [prio]); |
947 | } |
|
|
948 | |
984 | |
949 | return 0; |
985 | return 0; |
950 | } |
986 | } |
951 | |
987 | |
952 | static int |
988 | static int |
… | |
… | |
964 | |
1000 | |
965 | coro->flags |= CF_READY; |
1001 | coro->flags |= CF_READY; |
966 | |
1002 | |
967 | LOCK; |
1003 | LOCK; |
968 | coro_enq (SvREFCNT_inc (coro_sv)); |
1004 | coro_enq (SvREFCNT_inc (coro_sv)); |
|
|
1005 | ++coro_nready; |
969 | UNLOCK; |
1006 | UNLOCK; |
970 | |
1007 | |
971 | return 1; |
1008 | return 1; |
972 | } |
1009 | } |
973 | |
1010 | |
… | |
… | |
978 | } |
1015 | } |
979 | |
1016 | |
980 | static void |
1017 | static void |
981 | prepare_schedule (struct transfer_args *ta) |
1018 | prepare_schedule (struct transfer_args *ta) |
982 | { |
1019 | { |
983 | SV *prev, *next; |
1020 | SV *prev_sv, *next_sv; |
984 | |
1021 | |
985 | for (;;) |
1022 | for (;;) |
986 | { |
1023 | { |
987 | LOCK; |
1024 | LOCK; |
988 | next = coro_deq (PRIO_MIN); |
1025 | next_sv = coro_deq (PRIO_MIN); |
|
|
1026 | |
|
|
1027 | /* nothing to schedule: call the idle handler */ |
|
|
1028 | if (!next_sv) |
|
|
1029 | { |
|
|
1030 | UNLOCK; |
|
|
1031 | dSP; |
|
|
1032 | |
|
|
1033 | ENTER; |
|
|
1034 | SAVETMPS; |
|
|
1035 | |
|
|
1036 | PUSHMARK (SP); |
|
|
1037 | PUTBACK; |
|
|
1038 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
|
|
1039 | |
|
|
1040 | FREETMPS; |
|
|
1041 | LEAVE; |
|
|
1042 | continue; |
|
|
1043 | } |
|
|
1044 | |
|
|
1045 | ta->next = SvSTATE (next_sv); |
|
|
1046 | |
|
|
1047 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1048 | if (ta->next->flags & CF_DESTROYED) |
|
|
1049 | { |
|
|
1050 | UNLOCK; |
|
|
1051 | SvREFCNT_dec (next_sv); |
|
|
1052 | /* coro_nready is already taken care of by destroy */ |
|
|
1053 | continue; |
|
|
1054 | } |
|
|
1055 | |
|
|
1056 | --coro_nready; |
989 | UNLOCK; |
1057 | UNLOCK; |
990 | |
|
|
991 | if (next) |
|
|
992 | break; |
1058 | break; |
993 | |
|
|
994 | { |
|
|
995 | dSP; |
|
|
996 | |
|
|
997 | ENTER; |
|
|
998 | SAVETMPS; |
|
|
999 | |
|
|
1000 | PUSHMARK (SP); |
|
|
1001 | PUTBACK; |
|
|
1002 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
|
|
1003 | |
|
|
1004 | FREETMPS; |
|
|
1005 | LEAVE; |
|
|
1006 | } |
1059 | } |
1007 | } |
|
|
1008 | |
|
|
1009 | prev = SvRV (coro_current); |
|
|
1010 | SvRV_set (coro_current, next); |
|
|
1011 | |
1060 | |
1012 | /* free this only after the transfer */ |
1061 | /* free this only after the transfer */ |
|
|
1062 | prev_sv = SvRV (coro_current); |
|
|
1063 | SvRV_set (coro_current, next_sv); |
|
|
1064 | ta->prev = SvSTATE (prev_sv); |
|
|
1065 | |
|
|
1066 | assert (ta->next->flags & CF_READY); |
|
|
1067 | ta->next->flags &= ~CF_READY; |
|
|
1068 | |
1013 | LOCK; |
1069 | LOCK; |
1014 | free_coro_mortal (); |
1070 | free_coro_mortal (); |
|
|
1071 | coro_mortal = prev_sv; |
1015 | UNLOCK; |
1072 | UNLOCK; |
1016 | coro_mortal = prev; |
|
|
1017 | |
|
|
1018 | assert (!SvROK(prev));//D |
|
|
1019 | assert (!SvROK(next));//D |
|
|
1020 | |
|
|
1021 | ta->prev = SvSTATE (prev); |
|
|
1022 | ta->next = SvSTATE (next); |
|
|
1023 | |
|
|
1024 | assert (ta->next->flags & CF_READY); |
|
|
1025 | ta->next->flags &= ~CF_READY; |
|
|
1026 | } |
1073 | } |
1027 | |
1074 | |
1028 | static void |
1075 | static void |
1029 | prepare_cede (struct transfer_args *ta) |
1076 | prepare_cede (struct transfer_args *ta) |
1030 | { |
1077 | { |
… | |
… | |
1182 | break; |
1229 | break; |
1183 | } |
1230 | } |
1184 | |
1231 | |
1185 | BARRIER; |
1232 | BARRIER; |
1186 | TRANSFER (ta); |
1233 | TRANSFER (ta); |
1187 | } |
|
|
1188 | |
1234 | |
1189 | void |
1235 | if (GIMME_V != G_VOID && ta.next != ta.prev) |
1190 | _clone_state_from (SV *dst, SV *src) |
1236 | XSRETURN_YES; |
|
|
1237 | } |
|
|
1238 | |
|
|
1239 | bool |
|
|
1240 | _destroy (SV *coro_sv) |
1191 | CODE: |
1241 | CODE: |
1192 | { |
1242 | RETVAL = coro_state_destroy (SvSTATE (coro_sv)); |
1193 | struct coro *coro_src = SvSTATE (src); |
1243 | OUTPUT: |
1194 | |
1244 | RETVAL |
1195 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
|
|
1196 | |
|
|
1197 | ++coro_src->refcnt; |
|
|
1198 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
|
|
1199 | } |
|
|
1200 | |
1245 | |
1201 | void |
1246 | void |
1202 | _exit (code) |
1247 | _exit (code) |
1203 | int code |
1248 | int code |
1204 | PROTOTYPE: $ |
1249 | PROTOTYPE: $ |