… | |
… | |
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" |
… | |
… | |
135 | /* the stack */ |
137 | /* the stack */ |
136 | void *sptr; |
138 | void *sptr; |
137 | long ssize; /* positive == mmap, otherwise malloc */ |
139 | long ssize; /* positive == mmap, otherwise malloc */ |
138 | |
140 | |
139 | /* cpu state */ |
141 | /* cpu state */ |
140 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
142 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
|
|
143 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
141 | JMPENV *top_env; |
144 | JMPENV *top_env; |
142 | coro_context cctx; |
145 | coro_context cctx; |
143 | |
146 | |
144 | int inuse; |
147 | int inuse; |
145 | |
148 | |
… | |
… | |
147 | int valgrind_id; |
150 | int valgrind_id; |
148 | #endif |
151 | #endif |
149 | } coro_cctx; |
152 | } coro_cctx; |
150 | |
153 | |
151 | enum { |
154 | enum { |
152 | CF_RUNNING = 0x0001, /* coroutine is running */ |
155 | CF_RUNNING = 0x0001, /* coroutine is running */ |
153 | CF_READY = 0x0002, /* coroutine is ready */ |
156 | CF_READY = 0x0002, /* coroutine is ready */ |
154 | CF_NEW = 0x0004, /* ahs never been switched to */ |
157 | CF_NEW = 0x0004, /* has never been switched to */ |
|
|
158 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
155 | }; |
159 | }; |
156 | |
160 | |
157 | /* this is a structure representing a perl-level coroutine */ |
161 | /* this is a structure representing a perl-level coroutine */ |
158 | struct coro { |
162 | struct coro { |
159 | /* the c coroutine allocated to this perl coroutine, if any */ |
163 | /* the c coroutine allocated to this perl coroutine, if any */ |
… | |
… | |
468 | |
472 | |
469 | /* free all temporaries */ |
473 | /* free all temporaries */ |
470 | FREETMPS; |
474 | FREETMPS; |
471 | assert (PL_tmps_ix == -1); |
475 | assert (PL_tmps_ix == -1); |
472 | |
476 | |
|
|
477 | /* unwind all extra stacks */ |
473 | POPSTACK_TO (PL_mainstack); |
478 | POPSTACK_TO (PL_mainstack); |
|
|
479 | |
|
|
480 | /* unwind main stack */ |
|
|
481 | dounwind (-1); |
474 | } |
482 | } |
475 | |
483 | |
476 | while (PL_curstackinfo->si_next) |
484 | while (PL_curstackinfo->si_next) |
477 | PL_curstackinfo = PL_curstackinfo->si_next; |
485 | PL_curstackinfo = PL_curstackinfo->si_next; |
478 | |
486 | |
… | |
… | |
706 | |
714 | |
707 | /* sometimes transfer is only called to set idle_sp */ |
715 | /* sometimes transfer is only called to set idle_sp */ |
708 | if (!next) |
716 | if (!next) |
709 | { |
717 | { |
710 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
718 | ((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 */ |
719 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
712 | } |
720 | } |
713 | else if (prev != next) |
721 | else if (prev != next) |
714 | { |
722 | { |
715 | coro_cctx *prev__cctx; |
723 | coro_cctx *prev__cctx; |
716 | |
724 | |
… | |
… | |
727 | if (!prev->flags & CF_RUNNING) |
735 | if (!prev->flags & CF_RUNNING) |
728 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
736 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
729 | |
737 | |
730 | if (next->flags & CF_RUNNING) |
738 | if (next->flags & CF_RUNNING) |
731 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
739 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
|
|
740 | |
|
|
741 | if (next->flags & CF_DESTROYED) |
|
|
742 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
732 | |
743 | |
733 | prev->flags &= ~CF_RUNNING; |
744 | prev->flags &= ~CF_RUNNING; |
734 | next->flags |= CF_RUNNING; |
745 | next->flags |= CF_RUNNING; |
735 | |
746 | |
736 | LOCK; |
747 | LOCK; |
… | |
… | |
757 | |
768 | |
758 | /* possibly "free" the cctx */ |
769 | /* possibly "free" the cctx */ |
759 | if (prev__cctx->idle_sp == STACKLEVEL) |
770 | if (prev__cctx->idle_sp == STACKLEVEL) |
760 | { |
771 | { |
761 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
772 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
762 | assert (PL_top_env == prev__cctx->top_env); |
773 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
763 | |
774 | |
764 | prev->cctx = 0; |
775 | prev->cctx = 0; |
765 | |
776 | |
766 | cctx_put (prev__cctx); |
777 | cctx_put (prev__cctx); |
767 | prev__cctx->inuse = 0; |
778 | prev__cctx->inuse = 0; |
… | |
… | |
780 | PL_top_env = next->cctx->top_env; |
791 | PL_top_env = next->cctx->top_env; |
781 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
792 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
782 | } |
793 | } |
783 | |
794 | |
784 | free_coro_mortal (); |
795 | free_coro_mortal (); |
785 | |
|
|
786 | UNLOCK; |
796 | UNLOCK; |
787 | } |
797 | } |
788 | } |
798 | } |
789 | |
799 | |
790 | struct transfer_args |
800 | struct transfer_args |
… | |
… | |
792 | struct coro *prev, *next; |
802 | struct coro *prev, *next; |
793 | }; |
803 | }; |
794 | |
804 | |
795 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
805 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
796 | |
806 | |
797 | static void |
807 | static int |
798 | coro_state_destroy (struct coro *coro) |
808 | coro_state_destroy (struct coro *coro) |
799 | { |
809 | { |
800 | if (coro->refcnt--) |
810 | if (coro->flags & CF_DESTROYED) |
801 | return; |
811 | return 0; |
|
|
812 | |
|
|
813 | coro->flags |= CF_DESTROYED; |
802 | |
814 | |
803 | if (coro->mainstack && coro->mainstack != main_mainstack) |
815 | if (coro->mainstack && coro->mainstack != main_mainstack) |
804 | { |
816 | { |
|
|
817 | assert (!(coro->flags & CF_RUNNING)); |
|
|
818 | |
805 | struct coro temp; |
819 | struct coro temp; |
806 | Zero (&temp, 1, struct coro); |
820 | Zero (&temp, 1, struct coro); |
807 | temp.save = CORO_SAVE_ALL; |
821 | temp.save = CORO_SAVE_ALL; |
808 | |
822 | |
809 | if (coro->flags & CF_RUNNING) |
823 | if (coro->flags & CF_RUNNING) |
… | |
… | |
819 | coro->mainstack = 0; |
833 | coro->mainstack = 0; |
820 | } |
834 | } |
821 | |
835 | |
822 | cctx_destroy (coro->cctx); |
836 | cctx_destroy (coro->cctx); |
823 | SvREFCNT_dec (coro->args); |
837 | SvREFCNT_dec (coro->args); |
824 | Safefree (coro); |
838 | |
|
|
839 | return 1; |
825 | } |
840 | } |
826 | |
841 | |
827 | static int |
842 | static int |
828 | coro_state_clear (pTHX_ SV *sv, MAGIC *mg) |
843 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
829 | { |
844 | { |
830 | struct coro *coro = (struct coro *)mg->mg_ptr; |
845 | struct coro *coro = (struct coro *)mg->mg_ptr; |
831 | mg->mg_ptr = 0; |
846 | mg->mg_ptr = 0; |
832 | |
847 | |
|
|
848 | if (--coro->refcnt < 0) |
|
|
849 | { |
833 | coro_state_destroy (coro); |
850 | coro_state_destroy (coro); |
|
|
851 | Safefree (coro); |
|
|
852 | } |
834 | |
853 | |
835 | return 0; |
854 | return 0; |
836 | } |
855 | } |
837 | |
856 | |
838 | static int |
857 | static int |
… | |
… | |
845 | return 0; |
864 | return 0; |
846 | } |
865 | } |
847 | |
866 | |
848 | static MGVTBL coro_state_vtbl = { |
867 | static MGVTBL coro_state_vtbl = { |
849 | 0, 0, 0, 0, |
868 | 0, 0, 0, 0, |
850 | coro_state_clear, |
869 | coro_state_free, |
851 | 0, |
870 | 0, |
852 | #ifdef MGf_DUP |
871 | #ifdef MGf_DUP |
853 | coro_state_dup, |
872 | coro_state_dup, |
854 | #else |
873 | #else |
855 | # define MGf_DUP 0 |
874 | # define MGf_DUP 0 |
… | |
… | |
957 | coro = SvSTATE (coro_sv); |
976 | coro = SvSTATE (coro_sv); |
958 | |
977 | |
959 | if (coro->flags & CF_READY) |
978 | if (coro->flags & CF_READY) |
960 | return 0; |
979 | return 0; |
961 | |
980 | |
962 | #if 0 /* this is actually harmless */ |
|
|
963 | if (coro->flags & CF_RUNNING) |
|
|
964 | croak ("Coro::ready called on currently running coroutine"); |
|
|
965 | #endif |
|
|
966 | |
|
|
967 | coro->flags |= CF_READY; |
981 | coro->flags |= CF_READY; |
968 | |
982 | |
969 | LOCK; |
983 | LOCK; |
970 | coro_enq (SvREFCNT_inc (coro_sv)); |
984 | coro_enq (SvREFCNT_inc (coro_sv)); |
971 | UNLOCK; |
985 | UNLOCK; |
… | |
… | |
974 | } |
988 | } |
975 | |
989 | |
976 | static int |
990 | static int |
977 | api_is_ready (SV *coro_sv) |
991 | api_is_ready (SV *coro_sv) |
978 | { |
992 | { |
979 | return !!SvSTATE (coro_sv)->flags & CF_READY; |
993 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
980 | } |
994 | } |
981 | |
995 | |
982 | static void |
996 | static void |
983 | prepare_schedule (struct transfer_args *ta) |
997 | prepare_schedule (struct transfer_args *ta) |
984 | { |
998 | { |
985 | SV *prev, *next; |
999 | SV *prev_sv, *next_sv; |
986 | |
1000 | |
987 | for (;;) |
1001 | for (;;) |
988 | { |
1002 | { |
989 | LOCK; |
1003 | LOCK; |
990 | next = coro_deq (PRIO_MIN); |
1004 | next_sv = coro_deq (PRIO_MIN); |
991 | UNLOCK; |
1005 | UNLOCK; |
992 | |
1006 | |
|
|
1007 | /* nothing to schedule: call the idle handler */ |
993 | if (next) |
1008 | if (!next_sv) |
994 | break; |
|
|
995 | |
|
|
996 | { |
1009 | { |
997 | dSP; |
1010 | dSP; |
998 | |
1011 | |
999 | ENTER; |
1012 | ENTER; |
1000 | SAVETMPS; |
1013 | SAVETMPS; |
1001 | |
1014 | |
1002 | PUSHMARK (SP); |
1015 | PUSHMARK (SP); |
1003 | PUTBACK; |
1016 | PUTBACK; |
1004 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
1017 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
1005 | |
1018 | |
1006 | FREETMPS; |
1019 | FREETMPS; |
1007 | LEAVE; |
1020 | LEAVE; |
|
|
1021 | continue; |
1008 | } |
1022 | } |
1009 | } |
|
|
1010 | |
1023 | |
1011 | prev = SvRV (coro_current); |
1024 | ta->next = SvSTATE (next_sv); |
1012 | SvRV_set (coro_current, next); |
1025 | |
|
|
1026 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1027 | if (ta->next->flags & CF_DESTROYED) |
|
|
1028 | { |
|
|
1029 | SvREFCNT_dec (next_sv); |
|
|
1030 | continue; |
|
|
1031 | } |
|
|
1032 | |
|
|
1033 | break; |
|
|
1034 | } |
1013 | |
1035 | |
1014 | /* free this only after the transfer */ |
1036 | /* free this only after the transfer */ |
|
|
1037 | prev_sv = SvRV (coro_current); |
|
|
1038 | SvRV_set (coro_current, next_sv); |
|
|
1039 | ta->prev = SvSTATE (prev_sv); |
|
|
1040 | |
|
|
1041 | assert (ta->next->flags & CF_READY); |
|
|
1042 | ta->next->flags &= ~CF_READY; |
|
|
1043 | |
1015 | LOCK; |
1044 | LOCK; |
1016 | free_coro_mortal (); |
1045 | free_coro_mortal (); |
|
|
1046 | coro_mortal = prev_sv; |
1017 | UNLOCK; |
1047 | UNLOCK; |
1018 | coro_mortal = prev; |
|
|
1019 | |
|
|
1020 | assert (!SvROK(prev));//D |
|
|
1021 | assert (!SvROK(next));//D |
|
|
1022 | |
|
|
1023 | ta->prev = SvSTATE (prev); |
|
|
1024 | ta->next = SvSTATE (next); |
|
|
1025 | |
|
|
1026 | assert (ta->next->flags & CF_READY); |
|
|
1027 | ta->next->flags &= ~CF_READY; |
|
|
1028 | } |
1048 | } |
1029 | |
1049 | |
1030 | static void |
1050 | static void |
1031 | prepare_cede (struct transfer_args *ta) |
1051 | prepare_cede (struct transfer_args *ta) |
1032 | { |
1052 | { |
1033 | api_ready (coro_current); |
1053 | api_ready (coro_current); |
1034 | |
|
|
1035 | prepare_schedule (ta); |
1054 | prepare_schedule (ta); |
|
|
1055 | } |
|
|
1056 | |
|
|
1057 | static int |
|
|
1058 | prepare_cede_notself (struct transfer_args *ta) |
|
|
1059 | { |
|
|
1060 | if (coro_nready) |
|
|
1061 | { |
|
|
1062 | SV *prev = SvRV (coro_current); |
|
|
1063 | prepare_schedule (ta); |
|
|
1064 | api_ready (prev); |
|
|
1065 | return 1; |
|
|
1066 | } |
|
|
1067 | else |
|
|
1068 | return 0; |
1036 | } |
1069 | } |
1037 | |
1070 | |
1038 | static void |
1071 | static void |
1039 | api_schedule (void) |
1072 | api_schedule (void) |
1040 | { |
1073 | { |
… | |
… | |
1042 | |
1075 | |
1043 | prepare_schedule (&ta); |
1076 | prepare_schedule (&ta); |
1044 | TRANSFER (ta); |
1077 | TRANSFER (ta); |
1045 | } |
1078 | } |
1046 | |
1079 | |
1047 | static void |
1080 | static int |
1048 | api_cede (void) |
1081 | api_cede (void) |
1049 | { |
1082 | { |
1050 | struct transfer_args ta; |
1083 | struct transfer_args ta; |
1051 | |
1084 | |
1052 | prepare_cede (&ta); |
1085 | prepare_cede (&ta); |
|
|
1086 | |
|
|
1087 | if (ta.prev != ta.next) |
|
|
1088 | { |
1053 | TRANSFER (ta); |
1089 | TRANSFER (ta); |
|
|
1090 | return 1; |
|
|
1091 | } |
|
|
1092 | else |
|
|
1093 | return 0; |
|
|
1094 | } |
|
|
1095 | |
|
|
1096 | static int |
|
|
1097 | api_cede_notself (void) |
|
|
1098 | { |
|
|
1099 | struct transfer_args ta; |
|
|
1100 | |
|
|
1101 | if (prepare_cede_notself (&ta)) |
|
|
1102 | { |
|
|
1103 | TRANSFER (ta); |
|
|
1104 | return 1; |
|
|
1105 | } |
|
|
1106 | else |
|
|
1107 | return 0; |
1054 | } |
1108 | } |
1055 | |
1109 | |
1056 | MODULE = Coro::State PACKAGE = Coro::State |
1110 | MODULE = Coro::State PACKAGE = Coro::State |
1057 | |
1111 | |
1058 | PROTOTYPES: DISABLE |
1112 | PROTOTYPES: DISABLE |
… | |
… | |
1114 | _set_stacklevel (...) |
1168 | _set_stacklevel (...) |
1115 | ALIAS: |
1169 | ALIAS: |
1116 | Coro::State::transfer = 1 |
1170 | Coro::State::transfer = 1 |
1117 | Coro::schedule = 2 |
1171 | Coro::schedule = 2 |
1118 | Coro::cede = 3 |
1172 | Coro::cede = 3 |
|
|
1173 | Coro::cede_notself = 4 |
1119 | CODE: |
1174 | CODE: |
1120 | { |
1175 | { |
1121 | struct transfer_args ta; |
1176 | struct transfer_args ta; |
1122 | |
1177 | |
1123 | switch (ix) |
1178 | switch (ix) |
… | |
… | |
1139 | break; |
1194 | break; |
1140 | |
1195 | |
1141 | case 3: |
1196 | case 3: |
1142 | prepare_cede (&ta); |
1197 | prepare_cede (&ta); |
1143 | break; |
1198 | break; |
|
|
1199 | |
|
|
1200 | case 4: |
|
|
1201 | if (!prepare_cede_notself (&ta)) |
|
|
1202 | XSRETURN_EMPTY; |
|
|
1203 | |
|
|
1204 | break; |
1144 | } |
1205 | } |
1145 | |
1206 | |
|
|
1207 | BARRIER; |
1146 | TRANSFER (ta); |
1208 | TRANSFER (ta); |
1147 | } |
1209 | } |
1148 | |
1210 | |
1149 | void |
1211 | bool |
1150 | _clone_state_from (SV *dst, SV *src) |
1212 | _destroy (SV *coro_sv) |
1151 | CODE: |
1213 | CODE: |
1152 | { |
1214 | RETVAL = coro_state_destroy (SvSTATE (coro_sv)); |
1153 | struct coro *coro_src = SvSTATE (src); |
1215 | OUTPUT: |
1154 | |
1216 | RETVAL |
1155 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
|
|
1156 | |
|
|
1157 | ++coro_src->refcnt; |
|
|
1158 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
|
|
1159 | } |
|
|
1160 | |
1217 | |
1161 | void |
1218 | void |
1162 | _exit (code) |
1219 | _exit (code) |
1163 | int code |
1220 | int code |
1164 | PROTOTYPE: $ |
1221 | PROTOTYPE: $ |
… | |
… | |
1201 | coro_ready[i] = newAV (); |
1258 | coro_ready[i] = newAV (); |
1202 | |
1259 | |
1203 | { |
1260 | { |
1204 | SV *sv = perl_get_sv("Coro::API", 1); |
1261 | SV *sv = perl_get_sv("Coro::API", 1); |
1205 | |
1262 | |
1206 | coroapi.schedule = api_schedule; |
1263 | coroapi.schedule = api_schedule; |
1207 | coroapi.save = api_save; |
1264 | coroapi.save = api_save; |
1208 | coroapi.cede = api_cede; |
1265 | coroapi.cede = api_cede; |
|
|
1266 | coroapi.cede_notself = api_cede_notself; |
1209 | coroapi.ready = api_ready; |
1267 | coroapi.ready = api_ready; |
1210 | coroapi.is_ready = api_is_ready; |
1268 | coroapi.is_ready = api_is_ready; |
1211 | coroapi.nready = &coro_nready; |
1269 | coroapi.nready = &coro_nready; |
1212 | coroapi.current = coro_current; |
1270 | coroapi.current = coro_current; |
1213 | |
1271 | |
1214 | GCoroAPI = &coroapi; |
1272 | GCoroAPI = &coroapi; |
1215 | sv_setiv (sv, (IV)&coroapi); |
1273 | sv_setiv (sv, (IV)&coroapi); |
1216 | SvREADONLY_on (sv); |
1274 | SvREADONLY_on (sv); |
1217 | } |
1275 | } |
… | |
… | |
1233 | RETVAL = coro->prio; |
1291 | RETVAL = coro->prio; |
1234 | |
1292 | |
1235 | if (items > 1) |
1293 | if (items > 1) |
1236 | { |
1294 | { |
1237 | if (ix) |
1295 | if (ix) |
1238 | newprio += coro->prio; |
1296 | newprio = coro->prio - newprio; |
1239 | |
1297 | |
1240 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
1298 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
1241 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
1299 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
1242 | |
1300 | |
1243 | coro->prio = newprio; |
1301 | coro->prio = newprio; |
1244 | } |
1302 | } |
1245 | } |
1303 | } |
|
|
1304 | OUTPUT: |
|
|
1305 | RETVAL |
1246 | |
1306 | |
1247 | SV * |
1307 | SV * |
1248 | ready (SV *self) |
1308 | ready (SV *self) |
1249 | PROTOTYPE: $ |
1309 | PROTOTYPE: $ |
1250 | CODE: |
1310 | CODE: |