… | |
… | |
46 | # define BOOT_PAGESIZE (void)0 |
46 | # define BOOT_PAGESIZE (void)0 |
47 | #endif |
47 | #endif |
48 | |
48 | |
49 | #if CORO_USE_VALGRIND |
49 | #if CORO_USE_VALGRIND |
50 | # include <valgrind/valgrind.h> |
50 | # include <valgrind/valgrind.h> |
51 | # define REGISTER_STACK(cctx,start,end) (cctx)->valgrind_id = VALGRIND_STACK_REGISTER ((start), (end)) |
|
|
52 | #else |
|
|
53 | # define REGISTER_STACK(cctx,start,end) |
|
|
54 | #endif |
51 | #endif |
55 | |
52 | |
56 | /* the maximum number of idle cctx that will be pooled */ |
53 | /* the maximum number of idle cctx that will be pooled */ |
57 | static int cctx_max_idle = 4; |
54 | static int cctx_max_idle = 4; |
58 | |
55 | |
… | |
… | |
126 | |
123 | |
127 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
124 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
128 | |
125 | |
129 | #if __GNUC__ >= 3 |
126 | #if __GNUC__ >= 3 |
130 | # define attribute(x) __attribute__(x) |
127 | # define attribute(x) __attribute__(x) |
131 | # define BARRIER __asm__ __volatile__ ("" : : : "memory") |
|
|
132 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
128 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
129 | # define INLINE static inline |
133 | #else |
130 | #else |
134 | # define attribute(x) |
131 | # define attribute(x) |
135 | # define BARRIER |
|
|
136 | # define expect(expr,value) (expr) |
132 | # define expect(expr,value) (expr) |
|
|
133 | # define INLINE static |
137 | #endif |
134 | #endif |
138 | |
135 | |
139 | #define expect_false(expr) expect ((expr) != 0, 0) |
136 | #define expect_false(expr) expect ((expr) != 0, 0) |
140 | #define expect_true(expr) expect ((expr) != 0, 1) |
137 | #define expect_true(expr) expect ((expr) != 0, 1) |
141 | |
138 | |
142 | #define NOINLINE attribute ((noinline)) |
139 | #define NOINLINE attribute ((noinline)) |
143 | |
140 | |
144 | #include "CoroAPI.h" |
141 | #include "CoroAPI.h" |
145 | |
142 | |
146 | #ifdef USE_ITHREADS |
143 | #ifdef USE_ITHREADS |
|
|
144 | |
147 | static perl_mutex coro_lock; |
145 | static perl_mutex coro_lock; |
148 | # define LOCK do { MUTEX_LOCK (&coro_lock); } while (0) |
146 | # define LOCK do { MUTEX_LOCK (&coro_lock); } while (0) |
149 | # define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0) |
147 | # define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0) |
|
|
148 | # if CORO_PTHREAD |
|
|
149 | static void *coro_thx; |
|
|
150 | # endif |
|
|
151 | |
150 | #else |
152 | #else |
|
|
153 | |
151 | # define LOCK (void)0 |
154 | # define LOCK (void)0 |
152 | # define UNLOCK (void)0 |
155 | # define UNLOCK (void)0 |
|
|
156 | |
153 | #endif |
157 | #endif |
|
|
158 | |
|
|
159 | # undef LOCK |
|
|
160 | # define LOCK (void)0 |
|
|
161 | # undef UNLOCK |
|
|
162 | # define UNLOCK (void)0 |
154 | |
163 | |
155 | /* helper storage struct for Coro::AIO */ |
164 | /* helper storage struct for Coro::AIO */ |
156 | struct io_state |
165 | struct io_state |
157 | { |
166 | { |
158 | AV *res; |
167 | AV *res; |
… | |
… | |
169 | static struct CoroAPI coroapi; |
178 | static struct CoroAPI coroapi; |
170 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
179 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
171 | static JMPENV *main_top_env; |
180 | static JMPENV *main_top_env; |
172 | static HV *coro_state_stash, *coro_stash; |
181 | static HV *coro_state_stash, *coro_stash; |
173 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
182 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
|
|
183 | static volatile struct coro *transfer_next; |
|
|
184 | |
|
|
185 | struct transfer_args |
|
|
186 | { |
|
|
187 | struct coro *prev, *next; |
|
|
188 | }; |
174 | |
189 | |
175 | static GV *irsgv; /* $/ */ |
190 | static GV *irsgv; /* $/ */ |
176 | static GV *stdoutgv; /* *STDOUT */ |
191 | static GV *stdoutgv; /* *STDOUT */ |
177 | static SV *rv_diehook; |
192 | static SV *rv_diehook; |
178 | static SV *rv_warnhook; |
193 | static SV *rv_warnhook; |
… | |
… | |
280 | #define PRIO_MIN -4 |
295 | #define PRIO_MIN -4 |
281 | |
296 | |
282 | /* for Coro.pm */ |
297 | /* for Coro.pm */ |
283 | static SV *coro_current; |
298 | static SV *coro_current; |
284 | static SV *coro_readyhook; |
299 | static SV *coro_readyhook; |
285 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
300 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
286 | static int coro_nready; |
301 | static int coro_nready; |
287 | static struct coro *coro_first; |
302 | static struct coro *coro_first; |
288 | |
303 | |
289 | /** lowlevel stuff **********************************************************/ |
304 | /** lowlevel stuff **********************************************************/ |
290 | |
305 | |
… | |
… | |
395 | : 0 |
410 | : 0 |
396 | |
411 | |
397 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
412 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
398 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
413 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
399 | |
414 | |
400 | static struct coro * |
415 | INLINE struct coro * |
401 | SvSTATE_ (pTHX_ SV *coro) |
416 | SvSTATE_ (pTHX_ SV *coro) |
402 | { |
417 | { |
403 | HV *stash; |
418 | HV *stash; |
404 | MAGIC *mg; |
419 | MAGIC *mg; |
405 | |
420 | |
… | |
… | |
696 | } |
711 | } |
697 | |
712 | |
698 | return rss; |
713 | return rss; |
699 | } |
714 | } |
700 | |
715 | |
|
|
716 | /** set stacklevel support **************************************************/ |
|
|
717 | |
|
|
718 | /* we sometimes need to create the effect of pp_set_stacklevel calling us */ |
|
|
719 | #define SSL_HEAD (void)0 |
|
|
720 | /* we sometimes need to create the effect of leaving via pp_set_stacklevel */ |
|
|
721 | #define SSL_TAIL set_stacklevel_tail (aTHX) |
|
|
722 | |
|
|
723 | INLINE void |
|
|
724 | set_stacklevel_tail (pTHX) |
|
|
725 | { |
|
|
726 | dSP; |
|
|
727 | SV **bot = SP; |
|
|
728 | |
|
|
729 | int gimme = GIMME_V; |
|
|
730 | |
|
|
731 | /* make sure we put something on the stack in scalar context */ |
|
|
732 | if (gimme == G_SCALAR) |
|
|
733 | { |
|
|
734 | if (sp == bot) |
|
|
735 | XPUSHs (&PL_sv_undef); |
|
|
736 | |
|
|
737 | SP = bot + 1; |
|
|
738 | } |
|
|
739 | |
|
|
740 | PUTBACK; |
|
|
741 | } |
|
|
742 | |
701 | /** coroutine stack handling ************************************************/ |
743 | /** coroutine stack handling ************************************************/ |
702 | |
744 | |
703 | static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); |
745 | static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); |
704 | static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); |
746 | static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); |
705 | static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg); |
747 | static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg); |
… | |
… | |
822 | PL_rs = newSVsv (GvSV (irsgv)); |
864 | PL_rs = newSVsv (GvSV (irsgv)); |
823 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
865 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
824 | |
866 | |
825 | { |
867 | { |
826 | dSP; |
868 | dSP; |
827 | LOGOP myop; |
869 | UNOP myop; |
828 | |
870 | |
829 | Zero (&myop, 1, LOGOP); |
871 | Zero (&myop, 1, UNOP); |
830 | myop.op_next = Nullop; |
872 | myop.op_next = Nullop; |
831 | myop.op_flags = OPf_WANT_VOID; |
873 | myop.op_flags = OPf_WANT_VOID; |
832 | |
874 | |
833 | PUSHMARK (SP); |
875 | PUSHMARK (SP); |
834 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
876 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
… | |
… | |
837 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
879 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
838 | SPAGAIN; |
880 | SPAGAIN; |
839 | } |
881 | } |
840 | |
882 | |
841 | /* this newly created coroutine might be run on an existing cctx which most |
883 | /* this newly created coroutine might be run on an existing cctx which most |
842 | * likely was suspended in set_stacklevel, called from entersub. |
884 | * likely was suspended in set_stacklevel, called from pp_set_stacklevel, |
843 | * set_stacklevl doesn't do anything on return, but entersub does LEAVE, |
885 | * so we have to emulate entering pp_set_stacklevel here. |
844 | * so we ENTER here for symmetry |
|
|
845 | */ |
886 | */ |
846 | ENTER; |
887 | SSL_HEAD; |
847 | } |
888 | } |
848 | |
889 | |
849 | static void |
890 | static void |
850 | coro_destruct (pTHX_ struct coro *coro) |
891 | coro_destruct (pTHX_ struct coro *coro) |
851 | { |
892 | { |
… | |
… | |
880 | SvREFCNT_dec (coro->throw); |
921 | SvREFCNT_dec (coro->throw); |
881 | |
922 | |
882 | coro_destruct_stacks (aTHX); |
923 | coro_destruct_stacks (aTHX); |
883 | } |
924 | } |
884 | |
925 | |
885 | static void |
926 | INLINE void |
886 | free_coro_mortal (pTHX) |
927 | free_coro_mortal (pTHX) |
887 | { |
928 | { |
888 | if (expect_true (coro_mortal)) |
929 | if (expect_true (coro_mortal)) |
889 | { |
930 | { |
890 | SvREFCNT_dec (coro_mortal); |
931 | SvREFCNT_dec (coro_mortal); |
… | |
… | |
1014 | |
1055 | |
1015 | TAINT_NOT; |
1056 | TAINT_NOT; |
1016 | return 0; |
1057 | return 0; |
1017 | } |
1058 | } |
1018 | |
1059 | |
|
|
1060 | static void |
|
|
1061 | prepare_set_stacklevel (struct transfer_args *ta, struct coro_cctx *cctx) |
|
|
1062 | { |
|
|
1063 | ta->prev = (struct coro *)cctx; |
|
|
1064 | ta->next = 0; |
|
|
1065 | } |
|
|
1066 | |
1019 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
1067 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
1020 | /* _cctx_init should be careful, as it could be called at almost any time */ |
1068 | /* _cctx_init should be careful, as it could be called at almost any time */ |
1021 | /* during execution of a perl program */ |
1069 | /* during execution of a perl program */ |
|
|
1070 | /* also initialises PL_top_env */ |
1022 | static void NOINLINE |
1071 | static void NOINLINE |
1023 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1072 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1024 | { |
1073 | { |
1025 | dSP; |
1074 | dSP; |
1026 | LOGOP myop; |
1075 | UNOP myop; |
1027 | |
1076 | |
1028 | PL_top_env = &PL_start_env; |
1077 | PL_top_env = &PL_start_env; |
1029 | |
1078 | |
1030 | if (cctx->flags & CC_TRACE) |
1079 | if (cctx->flags & CC_TRACE) |
1031 | PL_runops = runops_trace; |
1080 | PL_runops = runops_trace; |
1032 | |
1081 | |
1033 | Zero (&myop, 1, LOGOP); |
1082 | Zero (&myop, 1, UNOP); |
1034 | myop.op_next = PL_op; |
1083 | myop.op_next = PL_op; |
1035 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
1084 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
1036 | |
1085 | |
1037 | PUSHMARK (SP); |
1086 | PUSHMARK (SP); |
1038 | EXTEND (SP, 2); |
1087 | EXTEND (SP, 2); |
1039 | PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); |
1088 | PUSHs (sv_2mortal (newSViv ((IV)cctx))); |
1040 | PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
1089 | PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
1041 | PUTBACK; |
1090 | PUTBACK; |
1042 | PL_op = (OP *)&myop; |
1091 | PL_op = (OP *)&myop; |
1043 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
1092 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
1044 | SPAGAIN; |
1093 | SPAGAIN; |
1045 | } |
1094 | } |
1046 | |
1095 | |
|
|
1096 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
|
|
1097 | INLINE void |
|
|
1098 | transfer_tail (pTHX) |
|
|
1099 | { |
|
|
1100 | struct coro *next = (struct coro *)transfer_next; |
|
|
1101 | assert (!(transfer_next = 0)); /* just used for the side effect when asserts are enabled */ |
|
|
1102 | assert (("FATAL: next coroutine was zero in transfer_tail (please report)", next)); |
|
|
1103 | |
|
|
1104 | free_coro_mortal (aTHX); |
|
|
1105 | UNLOCK; |
|
|
1106 | |
|
|
1107 | if (expect_false (next->throw)) |
|
|
1108 | { |
|
|
1109 | SV *exception = sv_2mortal (next->throw); |
|
|
1110 | |
|
|
1111 | next->throw = 0; |
|
|
1112 | sv_setsv (ERRSV, exception); |
|
|
1113 | croak (0); |
|
|
1114 | } |
|
|
1115 | } |
|
|
1116 | |
1047 | /* |
1117 | /* |
1048 | * this is a _very_ stripped down perl interpreter ;) |
1118 | * this is a _very_ stripped down perl interpreter ;) |
1049 | */ |
1119 | */ |
1050 | static void |
1120 | static void |
1051 | cctx_run (void *arg) |
1121 | cctx_run (void *arg) |
1052 | { |
1122 | { |
|
|
1123 | #ifdef USE_ITHREADS |
|
|
1124 | # if CORO_PTHREAD |
|
|
1125 | PERL_SET_CONTEXT (coro_thx); |
|
|
1126 | # endif |
|
|
1127 | #endif |
|
|
1128 | { |
1053 | dTHX; |
1129 | dTHX; |
1054 | |
1130 | |
1055 | /* cctx_run is the alternative tail of transfer(), so unlock here. */ |
1131 | /* we are the alternative tail to pp_set_stacklevel */ |
1056 | UNLOCK; |
1132 | /* so do the same things here */ |
|
|
1133 | SSL_TAIL; |
1057 | |
1134 | |
1058 | /* we now skip the entersub that lead to transfer() */ |
1135 | /* we now skip the op that did lead to transfer() */ |
1059 | PL_op = PL_op->op_next; |
1136 | PL_op = PL_op->op_next; |
1060 | |
1137 | |
1061 | /* inject a fake subroutine call to cctx_init */ |
1138 | /* inject a fake subroutine call to cctx_init */ |
1062 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1139 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1063 | |
1140 | |
|
|
1141 | /* cctx_run is the alternative tail of transfer() */ |
|
|
1142 | transfer_tail (aTHX); |
|
|
1143 | |
1064 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1144 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1065 | PL_restartop = PL_op; |
1145 | PL_restartop = PL_op; |
1066 | perl_run (PL_curinterp); |
1146 | perl_run (PL_curinterp); |
1067 | |
1147 | |
1068 | /* |
1148 | /* |
1069 | * If perl-run returns we assume exit() was being called or the coro |
1149 | * If perl-run returns we assume exit() was being called or the coro |
1070 | * fell off the end, which seems to be the only valid (non-bug) |
1150 | * fell off the end, which seems to be the only valid (non-bug) |
1071 | * reason for perl_run to return. We try to exit by jumping to the |
1151 | * reason for perl_run to return. We try to exit by jumping to the |
1072 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1152 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1073 | * coroutine as Coro has no such concept |
1153 | * coroutine as Coro has no such concept |
1074 | */ |
1154 | */ |
1075 | PL_top_env = main_top_env; |
1155 | PL_top_env = main_top_env; |
1076 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1156 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
|
|
1157 | } |
1077 | } |
1158 | } |
1078 | |
1159 | |
1079 | static coro_cctx * |
1160 | static coro_cctx * |
1080 | cctx_new () |
1161 | cctx_new () |
1081 | { |
1162 | { |
1082 | coro_cctx *cctx; |
1163 | coro_cctx *cctx; |
|
|
1164 | |
|
|
1165 | ++cctx_count; |
|
|
1166 | New (0, cctx, 1, coro_cctx); |
|
|
1167 | |
|
|
1168 | cctx->gen = cctx_gen; |
|
|
1169 | cctx->flags = 0; |
|
|
1170 | cctx->idle_sp = 0; /* can be accessed by transfer between cctx_run and set_stacklevel, on throw */ |
|
|
1171 | |
|
|
1172 | return cctx; |
|
|
1173 | } |
|
|
1174 | |
|
|
1175 | /* create a new cctx only suitable as source */ |
|
|
1176 | static coro_cctx * |
|
|
1177 | cctx_new_empty () |
|
|
1178 | { |
|
|
1179 | coro_cctx *cctx = cctx_new (); |
|
|
1180 | |
|
|
1181 | cctx->sptr = 0; |
|
|
1182 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
|
|
1183 | |
|
|
1184 | return cctx; |
|
|
1185 | } |
|
|
1186 | |
|
|
1187 | /* create a new cctx suitable as destination/running a perl interpreter */ |
|
|
1188 | static coro_cctx * |
|
|
1189 | cctx_new_run () |
|
|
1190 | { |
|
|
1191 | coro_cctx *cctx = cctx_new (); |
1083 | void *stack_start; |
1192 | void *stack_start; |
1084 | size_t stack_size; |
1193 | size_t stack_size; |
1085 | |
|
|
1086 | ++cctx_count; |
|
|
1087 | Newz (0, cctx, 1, coro_cctx); |
|
|
1088 | |
|
|
1089 | cctx->gen = cctx_gen; |
|
|
1090 | |
1194 | |
1091 | #if HAVE_MMAP |
1195 | #if HAVE_MMAP |
1092 | cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1196 | cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1093 | /* mmap supposedly does allocate-on-write for us */ |
1197 | /* mmap supposedly does allocate-on-write for us */ |
1094 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
1198 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
1095 | |
1199 | |
1096 | if (cctx->sptr != (void *)-1) |
1200 | if (cctx->sptr != (void *)-1) |
1097 | { |
1201 | { |
1098 | # if CORO_STACKGUARD |
1202 | #if CORO_STACKGUARD |
1099 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
1203 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
1100 | # endif |
1204 | #endif |
1101 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
1205 | stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE; |
1102 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
1206 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
1103 | cctx->flags |= CC_MAPPED; |
1207 | cctx->flags |= CC_MAPPED; |
1104 | } |
1208 | } |
1105 | else |
1209 | else |
1106 | #endif |
1210 | #endif |
1107 | { |
1211 | { |
1108 | cctx->ssize = cctx_stacksize * (long)sizeof (long); |
1212 | cctx->ssize = cctx_stacksize * (long)sizeof (long); |
1109 | New (0, cctx->sptr, cctx_stacksize, long); |
1213 | New (0, cctx->sptr, cctx_stacksize, long); |
1110 | |
1214 | |
1111 | if (!cctx->sptr) |
1215 | if (!cctx->sptr) |
1112 | { |
1216 | { |
1113 | perror ("FATAL: unable to allocate stack for coroutine"); |
1217 | perror ("FATAL: unable to allocate stack for coroutine, exiting."); |
1114 | _exit (EXIT_FAILURE); |
1218 | _exit (EXIT_FAILURE); |
1115 | } |
1219 | } |
1116 | |
1220 | |
1117 | stack_start = cctx->sptr; |
1221 | stack_start = cctx->sptr; |
1118 | stack_size = cctx->ssize; |
1222 | stack_size = cctx->ssize; |
1119 | } |
1223 | } |
1120 | |
1224 | |
1121 | REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); |
1225 | #if CORO_USE_VALGRIND |
|
|
1226 | cctx->valgrind_id = VALGRIND_STACK_REGISTER ((char *)stack_start, (char *)stack_start + stack_size); |
|
|
1227 | #endif |
|
|
1228 | |
1122 | coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); |
1229 | coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); |
1123 | |
1230 | |
1124 | return cctx; |
1231 | return cctx; |
1125 | } |
1232 | } |
1126 | |
1233 | |
… | |
… | |
1134 | coro_destroy (&cctx->cctx); |
1241 | coro_destroy (&cctx->cctx); |
1135 | |
1242 | |
1136 | /* coro_transfer creates new, empty cctx's */ |
1243 | /* coro_transfer creates new, empty cctx's */ |
1137 | if (cctx->sptr) |
1244 | if (cctx->sptr) |
1138 | { |
1245 | { |
1139 | #if CORO_USE_VALGRIND |
1246 | #if CORO_USE_VALGRIND |
1140 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1247 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1141 | #endif |
1248 | #endif |
1142 | |
1249 | |
1143 | #if HAVE_MMAP |
1250 | #if HAVE_MMAP |
1144 | if (cctx->flags & CC_MAPPED) |
1251 | if (cctx->flags & CC_MAPPED) |
1145 | munmap (cctx->sptr, cctx->ssize); |
1252 | munmap (cctx->sptr, cctx->ssize); |
1146 | else |
1253 | else |
… | |
… | |
1167 | return cctx; |
1274 | return cctx; |
1168 | |
1275 | |
1169 | cctx_destroy (cctx); |
1276 | cctx_destroy (cctx); |
1170 | } |
1277 | } |
1171 | |
1278 | |
1172 | return cctx_new (); |
1279 | return cctx_new_run (); |
1173 | } |
1280 | } |
1174 | |
1281 | |
1175 | static void |
1282 | static void |
1176 | cctx_put (coro_cctx *cctx) |
1283 | cctx_put (coro_cctx *cctx) |
1177 | { |
1284 | { |
1178 | assert (("cctx_put called on non-initialised cctx", cctx->sptr)); |
1285 | assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr)); |
1179 | |
1286 | |
1180 | /* free another cctx if overlimit */ |
1287 | /* free another cctx if overlimit */ |
1181 | if (expect_false (cctx_idle >= cctx_max_idle)) |
1288 | if (expect_false (cctx_idle >= cctx_max_idle)) |
1182 | { |
1289 | { |
1183 | coro_cctx *first = cctx_first; |
1290 | coro_cctx *first = cctx_first; |
… | |
… | |
1227 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1334 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1228 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1335 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1229 | } |
1336 | } |
1230 | else if (expect_true (prev != next)) |
1337 | else if (expect_true (prev != next)) |
1231 | { |
1338 | { |
1232 | static volatile int has_throw; |
|
|
1233 | coro_cctx *prev__cctx; |
1339 | coro_cctx *prev__cctx; |
1234 | |
1340 | |
1235 | if (expect_false (prev->flags & CF_NEW)) |
1341 | if (expect_false (prev->flags & CF_NEW)) |
1236 | { |
1342 | { |
1237 | /* create a new empty/source context */ |
1343 | /* create a new empty/source context */ |
1238 | ++cctx_count; |
1344 | prev->cctx = cctx_new_empty (); |
1239 | New (0, prev->cctx, 1, coro_cctx); |
|
|
1240 | prev->cctx->sptr = 0; |
|
|
1241 | coro_create (&prev->cctx->cctx, 0, 0, 0, 0); |
|
|
1242 | |
|
|
1243 | prev->flags &= ~CF_NEW; |
1345 | prev->flags &= ~CF_NEW; |
1244 | prev->flags |= CF_RUNNING; |
1346 | prev->flags |= CF_RUNNING; |
1245 | } |
1347 | } |
1246 | |
1348 | |
1247 | prev->flags &= ~CF_RUNNING; |
1349 | prev->flags &= ~CF_RUNNING; |
… | |
… | |
1262 | else |
1364 | else |
1263 | load_perl (aTHX_ next); |
1365 | load_perl (aTHX_ next); |
1264 | |
1366 | |
1265 | prev__cctx = prev->cctx; |
1367 | prev__cctx = prev->cctx; |
1266 | |
1368 | |
1267 | /* possibly "free" the cctx */ |
1369 | /* possibly untie and reuse the cctx */ |
1268 | if (expect_true ( |
1370 | if (expect_true ( |
1269 | prev__cctx->idle_sp == STACKLEVEL |
1371 | prev__cctx->idle_sp == STACKLEVEL |
1270 | && !(prev__cctx->flags & CC_TRACE) |
1372 | && !(prev__cctx->flags & CC_TRACE) |
1271 | && !force_cctx |
1373 | && !force_cctx |
1272 | )) |
1374 | )) |
1273 | { |
1375 | { |
1274 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
1376 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
1275 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
1377 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); |
1276 | |
1378 | |
1277 | prev->cctx = 0; |
1379 | prev->cctx = 0; |
1278 | |
1380 | |
1279 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1381 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1280 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
1382 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
… | |
… | |
1288 | ++next->usecount; |
1390 | ++next->usecount; |
1289 | |
1391 | |
1290 | if (expect_true (!next->cctx)) |
1392 | if (expect_true (!next->cctx)) |
1291 | next->cctx = cctx_get (aTHX); |
1393 | next->cctx = cctx_get (aTHX); |
1292 | |
1394 | |
1293 | has_throw = !!next->throw; |
1395 | assert (("FATAL: transfer_next already nonzero in Coro (please report)", !transfer_next)); |
|
|
1396 | transfer_next = next; |
1294 | |
1397 | |
1295 | if (expect_false (prev__cctx != next->cctx)) |
1398 | if (expect_false (prev__cctx != next->cctx)) |
1296 | { |
1399 | { |
1297 | prev__cctx->top_env = PL_top_env; |
1400 | prev__cctx->top_env = PL_top_env; |
1298 | PL_top_env = next->cctx->top_env; |
1401 | PL_top_env = next->cctx->top_env; |
1299 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1402 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1300 | } |
1403 | } |
1301 | |
1404 | |
1302 | free_coro_mortal (aTHX); |
1405 | transfer_tail (aTHX); |
1303 | UNLOCK; |
|
|
1304 | |
|
|
1305 | if (expect_false (has_throw)) |
|
|
1306 | { |
|
|
1307 | struct coro *coro = SvSTATE (coro_current); |
|
|
1308 | |
|
|
1309 | if (coro->throw) |
|
|
1310 | { |
|
|
1311 | SV *exception = coro->throw; |
|
|
1312 | coro->throw = 0; |
|
|
1313 | sv_setsv (ERRSV, exception); |
|
|
1314 | croak (0); |
|
|
1315 | } |
|
|
1316 | } |
|
|
1317 | } |
1406 | } |
1318 | } |
1407 | } |
1319 | |
|
|
1320 | struct transfer_args |
|
|
1321 | { |
|
|
1322 | struct coro *prev, *next; |
|
|
1323 | }; |
|
|
1324 | |
1408 | |
1325 | #define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx)) |
1409 | #define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx)) |
1326 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
1410 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
1327 | |
1411 | |
1328 | /** high level stuff ********************************************************/ |
1412 | /** high level stuff ********************************************************/ |
… | |
… | |
1501 | |
1585 | |
1502 | static int |
1586 | static int |
1503 | api_is_ready (SV *coro_sv) |
1587 | api_is_ready (SV *coro_sv) |
1504 | { |
1588 | { |
1505 | dTHX; |
1589 | dTHX; |
|
|
1590 | |
1506 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1591 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1507 | } |
1592 | } |
1508 | |
1593 | |
1509 | static void |
1594 | INLINE void |
1510 | prepare_schedule (pTHX_ struct transfer_args *ta) |
1595 | prepare_schedule (pTHX_ struct transfer_args *ta) |
1511 | { |
1596 | { |
1512 | SV *prev_sv, *next_sv; |
1597 | SV *prev_sv, *next_sv; |
1513 | |
1598 | |
1514 | for (;;) |
1599 | for (;;) |
… | |
… | |
1540 | /* cannot transfer to destroyed coros, skip and look for next */ |
1625 | /* cannot transfer to destroyed coros, skip and look for next */ |
1541 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
1626 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
1542 | { |
1627 | { |
1543 | UNLOCK; |
1628 | UNLOCK; |
1544 | SvREFCNT_dec (next_sv); |
1629 | SvREFCNT_dec (next_sv); |
1545 | /* coro_nready is already taken care of by destroy */ |
1630 | /* coro_nready has already been taken care of by destroy */ |
1546 | continue; |
1631 | continue; |
1547 | } |
1632 | } |
1548 | |
1633 | |
1549 | --coro_nready; |
1634 | --coro_nready; |
1550 | UNLOCK; |
1635 | UNLOCK; |
… | |
… | |
1553 | |
1638 | |
1554 | /* free this only after the transfer */ |
1639 | /* free this only after the transfer */ |
1555 | prev_sv = SvRV (coro_current); |
1640 | prev_sv = SvRV (coro_current); |
1556 | ta->prev = SvSTATE (prev_sv); |
1641 | ta->prev = SvSTATE (prev_sv); |
1557 | TRANSFER_CHECK (*ta); |
1642 | TRANSFER_CHECK (*ta); |
1558 | assert (ta->next->flags & CF_READY); |
1643 | assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); |
1559 | ta->next->flags &= ~CF_READY; |
1644 | ta->next->flags &= ~CF_READY; |
1560 | SvRV_set (coro_current, next_sv); |
1645 | SvRV_set (coro_current, next_sv); |
1561 | |
1646 | |
1562 | LOCK; |
1647 | LOCK; |
1563 | free_coro_mortal (aTHX); |
1648 | free_coro_mortal (aTHX); |
1564 | coro_mortal = prev_sv; |
1649 | coro_mortal = prev_sv; |
1565 | UNLOCK; |
1650 | UNLOCK; |
1566 | } |
1651 | } |
1567 | |
1652 | |
1568 | static void |
1653 | INLINE void |
1569 | prepare_cede (pTHX_ struct transfer_args *ta) |
1654 | prepare_cede (pTHX_ struct transfer_args *ta) |
1570 | { |
1655 | { |
1571 | api_ready (coro_current); |
1656 | api_ready (coro_current); |
1572 | prepare_schedule (aTHX_ ta); |
1657 | prepare_schedule (aTHX_ ta); |
1573 | } |
1658 | } |
1574 | |
1659 | |
1575 | static int |
1660 | static void |
1576 | prepare_cede_notself (pTHX_ struct transfer_args *ta) |
1661 | prepare_cede_notself (pTHX_ struct transfer_args *ta) |
1577 | { |
1662 | { |
|
|
1663 | SV *prev = SvRV (coro_current); |
|
|
1664 | |
1578 | if (coro_nready) |
1665 | if (coro_nready) |
1579 | { |
1666 | { |
1580 | SV *prev = SvRV (coro_current); |
|
|
1581 | prepare_schedule (aTHX_ ta); |
1667 | prepare_schedule (aTHX_ ta); |
1582 | api_ready (prev); |
1668 | api_ready (prev); |
|
|
1669 | } |
|
|
1670 | else |
|
|
1671 | ta->prev = ta->next = SvSTATE (prev); |
|
|
1672 | } |
|
|
1673 | |
|
|
1674 | static void |
|
|
1675 | api_schedule (void) |
|
|
1676 | { |
|
|
1677 | dTHX; |
|
|
1678 | struct transfer_args ta; |
|
|
1679 | |
|
|
1680 | prepare_schedule (aTHX_ &ta); |
|
|
1681 | TRANSFER (ta, 1); |
|
|
1682 | } |
|
|
1683 | |
|
|
1684 | static int |
|
|
1685 | api_cede (void) |
|
|
1686 | { |
|
|
1687 | dTHX; |
|
|
1688 | struct transfer_args ta; |
|
|
1689 | |
|
|
1690 | prepare_cede (aTHX_ &ta); |
|
|
1691 | |
|
|
1692 | if (expect_true (ta.prev != ta.next)) |
|
|
1693 | { |
|
|
1694 | TRANSFER (ta, 1); |
1583 | return 1; |
1695 | return 1; |
1584 | } |
1696 | } |
1585 | else |
1697 | else |
1586 | return 0; |
1698 | return 0; |
1587 | } |
1699 | } |
1588 | |
1700 | |
1589 | static void |
|
|
1590 | api_schedule (void) |
|
|
1591 | { |
|
|
1592 | dTHX; |
|
|
1593 | struct transfer_args ta; |
|
|
1594 | |
|
|
1595 | prepare_schedule (aTHX_ &ta); |
|
|
1596 | TRANSFER (ta, 1); |
|
|
1597 | } |
|
|
1598 | |
|
|
1599 | static int |
1701 | static int |
1600 | api_cede (void) |
1702 | api_cede_notself (void) |
1601 | { |
1703 | { |
|
|
1704 | if (coro_nready) |
|
|
1705 | { |
1602 | dTHX; |
1706 | dTHX; |
1603 | struct transfer_args ta; |
1707 | struct transfer_args ta; |
1604 | |
1708 | |
1605 | prepare_cede (aTHX_ &ta); |
1709 | prepare_cede_notself (aTHX_ &ta); |
1606 | |
|
|
1607 | if (expect_true (ta.prev != ta.next)) |
|
|
1608 | { |
|
|
1609 | TRANSFER (ta, 1); |
1710 | TRANSFER (ta, 1); |
1610 | return 1; |
1711 | return 1; |
1611 | } |
1712 | } |
1612 | else |
1713 | else |
1613 | return 0; |
1714 | return 0; |
1614 | } |
1715 | } |
1615 | |
1716 | |
1616 | static int |
|
|
1617 | api_cede_notself (void) |
|
|
1618 | { |
|
|
1619 | dTHX; |
|
|
1620 | struct transfer_args ta; |
|
|
1621 | |
|
|
1622 | if (prepare_cede_notself (aTHX_ &ta)) |
|
|
1623 | { |
|
|
1624 | TRANSFER (ta, 1); |
|
|
1625 | return 1; |
|
|
1626 | } |
|
|
1627 | else |
|
|
1628 | return 0; |
|
|
1629 | } |
|
|
1630 | |
|
|
1631 | static void |
1717 | static void |
1632 | api_trace (SV *coro_sv, int flags) |
1718 | api_trace (SV *coro_sv, int flags) |
1633 | { |
1719 | { |
1634 | dTHX; |
1720 | dTHX; |
1635 | struct coro *coro = SvSTATE (coro_sv); |
1721 | struct coro *coro = SvSTATE (coro_sv); |
1636 | |
1722 | |
1637 | if (flags & CC_TRACE) |
1723 | if (flags & CC_TRACE) |
1638 | { |
1724 | { |
1639 | if (!coro->cctx) |
1725 | if (!coro->cctx) |
1640 | coro->cctx = cctx_new (); |
1726 | coro->cctx = cctx_new_run (); |
1641 | else if (!(coro->cctx->flags & CC_TRACE)) |
1727 | else if (!(coro->cctx->flags & CC_TRACE)) |
1642 | croak ("cannot enable tracing on coroutine with custom stack"); |
1728 | croak ("cannot enable tracing on coroutine with custom stack"); |
1643 | |
1729 | |
1644 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1730 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1645 | } |
1731 | } |
… | |
… | |
1745 | PerlIOBuf_get_ptr, |
1831 | PerlIOBuf_get_ptr, |
1746 | PerlIOBuf_get_cnt, |
1832 | PerlIOBuf_get_cnt, |
1747 | PerlIOBuf_set_ptrcnt, |
1833 | PerlIOBuf_set_ptrcnt, |
1748 | }; |
1834 | }; |
1749 | |
1835 | |
|
|
1836 | /*****************************************************************************/ |
|
|
1837 | |
|
|
1838 | static const CV *ssl_cv; /* for quick consistency check */ |
|
|
1839 | |
|
|
1840 | static UNOP ssl_restore; /* restore stack as entersub did, for first-re-run */ |
|
|
1841 | static SV *ssl_arg0; |
|
|
1842 | static SV *ssl_arg1; |
|
|
1843 | |
|
|
1844 | /* this restores the stack in the case we patched the entersub, to */ |
|
|
1845 | /* recreate the stack frame as perl will on following calls */ |
|
|
1846 | /* since entersub cleared the stack */ |
|
|
1847 | static OP * |
|
|
1848 | pp_restore (pTHX) |
|
|
1849 | { |
|
|
1850 | dSP; |
|
|
1851 | |
|
|
1852 | PUSHMARK (SP); |
|
|
1853 | |
|
|
1854 | EXTEND (SP, 3); |
|
|
1855 | if (ssl_arg0) PUSHs (sv_2mortal (ssl_arg0)), ssl_arg0 = 0; |
|
|
1856 | if (ssl_arg1) PUSHs (sv_2mortal (ssl_arg1)), ssl_arg1 = 0; |
|
|
1857 | PUSHs ((SV *)CvGV (ssl_cv)); |
|
|
1858 | |
|
|
1859 | RETURNOP (ssl_restore.op_first); |
|
|
1860 | } |
|
|
1861 | |
|
|
1862 | #define OPpENTERSUB_SSL 15 /* the part of op_private entersub hopefully doesn't use */ |
|
|
1863 | |
|
|
1864 | /* declare prototype */ |
|
|
1865 | XS(XS_Coro__State__set_stacklevel); |
|
|
1866 | |
|
|
1867 | /* |
|
|
1868 | * these not obviously related functions are all rolled into one |
|
|
1869 | * function to increase chances that they all will call transfer with the same |
|
|
1870 | * stack offset |
|
|
1871 | */ |
|
|
1872 | static OP * |
|
|
1873 | pp_set_stacklevel (pTHX) |
|
|
1874 | { |
|
|
1875 | dSP; |
|
|
1876 | struct transfer_args ta; |
|
|
1877 | SV **arg = PL_stack_base + TOPMARK + 1; |
|
|
1878 | int items = SP - arg; /* args without function object */ |
|
|
1879 | |
|
|
1880 | /* do a quick consistency check on the "function" object, and if it isn't */ |
|
|
1881 | /* for us, divert to the real entersub */ |
|
|
1882 | if (SvTYPE (*sp) != SVt_PVGV || CvXSUB (GvCV (*sp)) != XS_Coro__State__set_stacklevel) |
|
|
1883 | return PL_ppaddr[OP_ENTERSUB](aTHX); |
|
|
1884 | |
|
|
1885 | /* pop args */ |
|
|
1886 | SP = PL_stack_base + POPMARK; |
|
|
1887 | |
|
|
1888 | if (!(PL_op->op_flags & OPf_STACKED)) |
|
|
1889 | { |
|
|
1890 | /* ampersand-form of call, use @_ instead of stack */ |
|
|
1891 | AV *av = GvAV (PL_defgv); |
|
|
1892 | arg = AvARRAY (av); |
|
|
1893 | items = AvFILLp (av) + 1; |
|
|
1894 | } |
|
|
1895 | |
|
|
1896 | PUTBACK; |
|
|
1897 | switch (PL_op->op_private & OPpENTERSUB_SSL) |
|
|
1898 | { |
|
|
1899 | case 0: |
|
|
1900 | prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (arg [0])); |
|
|
1901 | break; |
|
|
1902 | |
|
|
1903 | case 1: |
|
|
1904 | if (items != 2) |
|
|
1905 | croak ("Coro::State::transfer (prev, next) expects two arguments, not %d.", items); |
|
|
1906 | |
|
|
1907 | prepare_transfer (aTHX_ &ta, arg [0], arg [1]); |
|
|
1908 | break; |
|
|
1909 | |
|
|
1910 | case 2: |
|
|
1911 | prepare_schedule (aTHX_ &ta); |
|
|
1912 | break; |
|
|
1913 | |
|
|
1914 | case 3: |
|
|
1915 | prepare_cede (aTHX_ &ta); |
|
|
1916 | break; |
|
|
1917 | |
|
|
1918 | case 4: |
|
|
1919 | prepare_cede_notself (aTHX_ &ta); |
|
|
1920 | break; |
|
|
1921 | } |
|
|
1922 | |
|
|
1923 | TRANSFER (ta, 0); |
|
|
1924 | SPAGAIN; |
|
|
1925 | |
|
|
1926 | skip: |
|
|
1927 | PUTBACK; |
|
|
1928 | SSL_TAIL; |
|
|
1929 | SPAGAIN; |
|
|
1930 | RETURN; |
|
|
1931 | } |
|
|
1932 | |
|
|
1933 | static void |
|
|
1934 | coro_ssl_patch (pTHX_ CV *cv, int ix, SV **args, int items) |
|
|
1935 | { |
|
|
1936 | assert (("FATAL: ssl call recursion in Coro module (please report)", PL_op->op_ppaddr != pp_set_stacklevel)); |
|
|
1937 | |
|
|
1938 | assert (("FATAL: ssl call with illegal CV value", CvGV (cv))); |
|
|
1939 | ssl_cv = cv; |
|
|
1940 | |
|
|
1941 | /* we patch the op, and then re-run the whole call */ |
|
|
1942 | /* we have to put some dummy argument on the stack for this to work */ |
|
|
1943 | ssl_restore.op_next = (OP *)&ssl_restore; |
|
|
1944 | ssl_restore.op_type = OP_NULL; |
|
|
1945 | ssl_restore.op_ppaddr = pp_restore; |
|
|
1946 | ssl_restore.op_first = PL_op; |
|
|
1947 | |
|
|
1948 | ssl_arg0 = items > 0 ? SvREFCNT_inc (args [0]) : 0; |
|
|
1949 | ssl_arg1 = items > 1 ? SvREFCNT_inc (args [1]) : 0; |
|
|
1950 | |
|
|
1951 | PL_op->op_ppaddr = pp_set_stacklevel; |
|
|
1952 | PL_op->op_private = PL_op->op_private & ~OPpENTERSUB_SSL | ix; /* we potentially share our private flags with entersub */ |
|
|
1953 | |
|
|
1954 | PL_op = (OP *)&ssl_restore; |
|
|
1955 | } |
1750 | |
1956 | |
1751 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
1957 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
1752 | |
1958 | |
1753 | PROTOTYPES: DISABLE |
1959 | PROTOTYPES: DISABLE |
1754 | |
1960 | |
1755 | BOOT: |
1961 | BOOT: |
1756 | { |
1962 | { |
1757 | #ifdef USE_ITHREADS |
1963 | #ifdef USE_ITHREADS |
1758 | MUTEX_INIT (&coro_lock); |
1964 | MUTEX_INIT (&coro_lock); |
|
|
1965 | # if CORO_PTHREAD |
|
|
1966 | coro_thx = PERL_GET_CONTEXT; |
|
|
1967 | # endif |
1759 | #endif |
1968 | #endif |
1760 | BOOT_PAGESIZE; |
1969 | BOOT_PAGESIZE; |
1761 | |
1970 | |
1762 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1971 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1763 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1972 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
… | |
… | |
1826 | av_push (coro->args, newSVsv (ST (i))); |
2035 | av_push (coro->args, newSVsv (ST (i))); |
1827 | } |
2036 | } |
1828 | OUTPUT: |
2037 | OUTPUT: |
1829 | RETVAL |
2038 | RETVAL |
1830 | |
2039 | |
1831 | # these not obviously related functions are all rolled into the same xs |
|
|
1832 | # function to increase chances that they all will call transfer with the same |
|
|
1833 | # stack offset |
|
|
1834 | void |
2040 | void |
1835 | _set_stacklevel (...) |
2041 | _set_stacklevel (...) |
1836 | ALIAS: |
2042 | ALIAS: |
1837 | Coro::State::transfer = 1 |
2043 | Coro::State::transfer = 1 |
1838 | Coro::schedule = 2 |
2044 | Coro::schedule = 2 |
1839 | Coro::cede = 3 |
2045 | Coro::cede = 3 |
1840 | Coro::cede_notself = 4 |
2046 | Coro::cede_notself = 4 |
1841 | CODE: |
2047 | CODE: |
1842 | { |
2048 | coro_ssl_patch (aTHX_ cv, ix, &ST (0), items); |
1843 | struct transfer_args ta; |
|
|
1844 | |
|
|
1845 | PUTBACK; |
|
|
1846 | switch (ix) |
|
|
1847 | { |
|
|
1848 | case 0: |
|
|
1849 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
|
|
1850 | ta.next = 0; |
|
|
1851 | break; |
|
|
1852 | |
|
|
1853 | case 1: |
|
|
1854 | if (items != 2) |
|
|
1855 | croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items); |
|
|
1856 | |
|
|
1857 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
|
|
1858 | break; |
|
|
1859 | |
|
|
1860 | case 2: |
|
|
1861 | prepare_schedule (aTHX_ &ta); |
|
|
1862 | break; |
|
|
1863 | |
|
|
1864 | case 3: |
|
|
1865 | prepare_cede (aTHX_ &ta); |
|
|
1866 | break; |
|
|
1867 | |
|
|
1868 | case 4: |
|
|
1869 | if (!prepare_cede_notself (aTHX_ &ta)) |
|
|
1870 | XSRETURN_EMPTY; |
|
|
1871 | |
|
|
1872 | break; |
|
|
1873 | } |
|
|
1874 | SPAGAIN; |
|
|
1875 | |
|
|
1876 | BARRIER; |
|
|
1877 | PUTBACK; |
|
|
1878 | TRANSFER (ta, 0); |
|
|
1879 | SPAGAIN; /* might be the sp of a different coroutine now */ |
|
|
1880 | /* be extra careful not to ever do anything after TRANSFER */ |
|
|
1881 | } |
|
|
1882 | |
2049 | |
1883 | bool |
2050 | bool |
1884 | _destroy (SV *coro_sv) |
2051 | _destroy (SV *coro_sv) |
1885 | CODE: |
2052 | CODE: |
1886 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
2053 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
… | |
… | |
1994 | is_destroyed = CF_DESTROYED |
2161 | is_destroyed = CF_DESTROYED |
1995 | CODE: |
2162 | CODE: |
1996 | RETVAL = boolSV (coro->flags & ix); |
2163 | RETVAL = boolSV (coro->flags & ix); |
1997 | OUTPUT: |
2164 | OUTPUT: |
1998 | RETVAL |
2165 | RETVAL |
|
|
2166 | |
|
|
2167 | void |
|
|
2168 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
|
|
2169 | PROTOTYPE: $;$ |
|
|
2170 | CODE: |
|
|
2171 | SvREFCNT_dec (self->throw); |
|
|
2172 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
1999 | |
2173 | |
2000 | void |
2174 | void |
2001 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
2175 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
2002 | |
2176 | |
2003 | SV * |
2177 | SV * |
… | |
… | |
2076 | |
2250 | |
2077 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
2251 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
2078 | coro_ready[i] = newAV (); |
2252 | coro_ready[i] = newAV (); |
2079 | |
2253 | |
2080 | { |
2254 | { |
2081 | SV *sv = perl_get_sv ("Coro::API", TRUE); |
2255 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
2082 | perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */ |
|
|
2083 | |
2256 | |
2084 | coroapi.schedule = api_schedule; |
2257 | coroapi.schedule = api_schedule; |
2085 | coroapi.cede = api_cede; |
2258 | coroapi.cede = api_cede; |
2086 | coroapi.cede_notself = api_cede_notself; |
2259 | coroapi.cede_notself = api_cede_notself; |
2087 | coroapi.ready = api_ready; |
2260 | coroapi.ready = api_ready; |
… | |
… | |
2147 | CODE: |
2320 | CODE: |
2148 | RETVAL = coro_nready; |
2321 | RETVAL = coro_nready; |
2149 | OUTPUT: |
2322 | OUTPUT: |
2150 | RETVAL |
2323 | RETVAL |
2151 | |
2324 | |
2152 | void |
|
|
2153 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
|
|
2154 | PROTOTYPE: $;$ |
|
|
2155 | CODE: |
|
|
2156 | SvREFCNT_dec (self->throw); |
|
|
2157 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
2158 | |
|
|
2159 | # for async_pool speedup |
2325 | # for async_pool speedup |
2160 | void |
2326 | void |
2161 | _pool_1 (SV *cb) |
2327 | _pool_1 (SV *cb) |
2162 | CODE: |
2328 | CODE: |
2163 | { |
2329 | { |
… | |
… | |
2362 | |
2528 | |
2363 | MODULE = Coro::State PACKAGE = PerlIO::cede |
2529 | MODULE = Coro::State PACKAGE = PerlIO::cede |
2364 | |
2530 | |
2365 | BOOT: |
2531 | BOOT: |
2366 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
2532 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
|
|
2533 | |