… | |
… | |
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 | |
… | |
… | |
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") |
128 | # define BARRIER __asm__ __volatile__ ("" : : : "memory") |
132 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
129 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
130 | # define INLINE static inline |
133 | #else |
131 | #else |
134 | # define attribute(x) |
132 | # define attribute(x) |
135 | # define BARRIER |
133 | # define BARRIER |
136 | # define expect(expr,value) (expr) |
134 | # define expect(expr,value) (expr) |
|
|
135 | # define INLINE static |
137 | #endif |
136 | #endif |
138 | |
137 | |
139 | #define expect_false(expr) expect ((expr) != 0, 0) |
138 | #define expect_false(expr) expect ((expr) != 0, 0) |
140 | #define expect_true(expr) expect ((expr) != 0, 1) |
139 | #define expect_true(expr) expect ((expr) != 0, 1) |
141 | |
140 | |
… | |
… | |
181 | static struct CoroAPI coroapi; |
180 | static struct CoroAPI coroapi; |
182 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
181 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
183 | static JMPENV *main_top_env; |
182 | static JMPENV *main_top_env; |
184 | static HV *coro_state_stash, *coro_stash; |
183 | static HV *coro_state_stash, *coro_stash; |
185 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
184 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
186 | static volatile char next_has_throw; /* speedup flag for next->throw check */ |
185 | static volatile struct coro *transfer_next; |
|
|
186 | |
|
|
187 | struct transfer_args |
|
|
188 | { |
|
|
189 | struct coro *prev, *next; |
|
|
190 | }; |
187 | |
191 | |
188 | static GV *irsgv; /* $/ */ |
192 | static GV *irsgv; /* $/ */ |
189 | static GV *stdoutgv; /* *STDOUT */ |
193 | static GV *stdoutgv; /* *STDOUT */ |
190 | static SV *rv_diehook; |
194 | static SV *rv_diehook; |
191 | static SV *rv_warnhook; |
195 | static SV *rv_warnhook; |
… | |
… | |
408 | : 0 |
412 | : 0 |
409 | |
413 | |
410 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
414 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
411 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
415 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
412 | |
416 | |
413 | static struct coro * |
417 | INLINE struct coro * |
414 | SvSTATE_ (pTHX_ SV *coro) |
418 | SvSTATE_ (pTHX_ SV *coro) |
415 | { |
419 | { |
416 | HV *stash; |
420 | HV *stash; |
417 | MAGIC *mg; |
421 | MAGIC *mg; |
418 | |
422 | |
… | |
… | |
722 | #define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ |
726 | #define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ |
723 | SvPV_nolen((SV*)((mg)->mg_ptr)) : \ |
727 | SvPV_nolen((SV*)((mg)->mg_ptr)) : \ |
724 | (const char*)(mg)->mg_ptr) |
728 | (const char*)(mg)->mg_ptr) |
725 | #endif |
729 | #endif |
726 | |
730 | |
|
|
731 | /* we sometimes need to create the effect of entersub calling us */ |
|
|
732 | #define ENTERSUB_HEAD ENTER; SAVETMPS |
|
|
733 | /* we somtimes need to create the effect of leaving via entersub */ |
|
|
734 | #define ENTERSUB_TAIL LEAVE |
|
|
735 | |
727 | /* |
736 | /* |
728 | * This overrides the default magic get method of %SIG elements. |
737 | * This overrides the default magic get method of %SIG elements. |
729 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
738 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
730 | * and instead of tryign to save and restore the hash elements, we just provide |
739 | * and instead of tryign to save and restore the hash elements, we just provide |
731 | * readback here. |
740 | * readback here. |
… | |
… | |
835 | PL_rs = newSVsv (GvSV (irsgv)); |
844 | PL_rs = newSVsv (GvSV (irsgv)); |
836 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
845 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
837 | |
846 | |
838 | { |
847 | { |
839 | dSP; |
848 | dSP; |
840 | LOGOP myop; |
849 | UNOP myop; |
841 | |
850 | |
842 | Zero (&myop, 1, LOGOP); |
851 | Zero (&myop, 1, UNOP); |
843 | myop.op_next = Nullop; |
852 | myop.op_next = Nullop; |
844 | myop.op_flags = OPf_WANT_VOID; |
853 | myop.op_flags = OPf_WANT_VOID; |
845 | |
854 | |
846 | PUSHMARK (SP); |
855 | PUSHMARK (SP); |
847 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
856 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
… | |
… | |
851 | SPAGAIN; |
860 | SPAGAIN; |
852 | } |
861 | } |
853 | |
862 | |
854 | /* this newly created coroutine might be run on an existing cctx which most |
863 | /* this newly created coroutine might be run on an existing cctx which most |
855 | * likely was suspended in set_stacklevel, called from entersub. |
864 | * likely was suspended in set_stacklevel, called from entersub. |
856 | * set_stacklevl doesn't do anything on return, but entersub does LEAVE, |
865 | * set_stacklevel doesn't do anything on return, but entersub does LEAVE, |
857 | * so we ENTER here for symmetry |
866 | * so we ENTER here for symmetry. |
858 | */ |
867 | */ |
859 | ENTER; |
868 | ENTERSUB_HEAD; |
860 | } |
869 | } |
861 | |
870 | |
862 | static void |
871 | static void |
863 | coro_destruct (pTHX_ struct coro *coro) |
872 | coro_destruct (pTHX_ struct coro *coro) |
864 | { |
873 | { |
… | |
… | |
893 | SvREFCNT_dec (coro->throw); |
902 | SvREFCNT_dec (coro->throw); |
894 | |
903 | |
895 | coro_destruct_stacks (aTHX); |
904 | coro_destruct_stacks (aTHX); |
896 | } |
905 | } |
897 | |
906 | |
898 | static void |
907 | INLINE void |
899 | free_coro_mortal (pTHX) |
908 | free_coro_mortal (pTHX) |
900 | { |
909 | { |
901 | if (expect_true (coro_mortal)) |
910 | if (expect_true (coro_mortal)) |
902 | { |
911 | { |
903 | SvREFCNT_dec (coro_mortal); |
912 | SvREFCNT_dec (coro_mortal); |
… | |
… | |
1027 | |
1036 | |
1028 | TAINT_NOT; |
1037 | TAINT_NOT; |
1029 | return 0; |
1038 | return 0; |
1030 | } |
1039 | } |
1031 | |
1040 | |
|
|
1041 | static void |
|
|
1042 | prepare_set_stacklevel (struct transfer_args *ta, struct coro_cctx *cctx) |
|
|
1043 | { |
|
|
1044 | ta->prev = (struct coro *)cctx; |
|
|
1045 | ta->next = 0; |
|
|
1046 | } |
|
|
1047 | |
1032 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
1048 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
1033 | /* _cctx_init should be careful, as it could be called at almost any time */ |
1049 | /* _cctx_init should be careful, as it could be called at almost any time */ |
1034 | /* during execution of a perl program */ |
1050 | /* during execution of a perl program */ |
1035 | /* also initialises PL_top_env */ |
1051 | /* also initialises PL_top_env */ |
1036 | static void NOINLINE |
1052 | static void NOINLINE |
1037 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1053 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1038 | { |
1054 | { |
1039 | dSP; |
1055 | dSP; |
1040 | LOGOP myop; |
1056 | UNOP myop; |
1041 | |
1057 | |
1042 | PL_top_env = &PL_start_env; |
1058 | PL_top_env = &PL_start_env; |
1043 | |
1059 | |
1044 | if (cctx->flags & CC_TRACE) |
1060 | if (cctx->flags & CC_TRACE) |
1045 | PL_runops = runops_trace; |
1061 | PL_runops = runops_trace; |
1046 | |
1062 | |
1047 | Zero (&myop, 1, LOGOP); |
1063 | Zero (&myop, 1, UNOP); |
1048 | myop.op_next = PL_op; |
1064 | myop.op_next = PL_op; |
1049 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
1065 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
1050 | |
1066 | |
1051 | PUSHMARK (SP); |
1067 | PUSHMARK (SP); |
1052 | EXTEND (SP, 2); |
1068 | EXTEND (SP, 2); |
1053 | PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); |
1069 | PUSHs (sv_2mortal (newSViv ((IV)cctx))); |
1054 | PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
1070 | PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
1055 | PUTBACK; |
1071 | PUTBACK; |
1056 | PL_op = (OP *)&myop; |
1072 | PL_op = (OP *)&myop; |
1057 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
1073 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
1058 | SPAGAIN; |
1074 | SPAGAIN; |
1059 | } |
1075 | } |
1060 | |
1076 | |
1061 | /* the tail of transfer: execute stuff we can onyl do afetr a transfer */ |
1077 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1062 | static void |
1078 | INLINE void |
1063 | transfer_tail (void) |
1079 | transfer_tail (pTHX) |
1064 | { |
1080 | { |
|
|
1081 | struct coro *next = (struct coro *)transfer_next; |
|
|
1082 | assert (!(transfer_next = 0)); /* just used for the side effect when asserts are enabled */ |
|
|
1083 | assert (("FATAL: transfer_next was zero in transfer_tail (please report)", next)); |
|
|
1084 | |
|
|
1085 | free_coro_mortal (aTHX); |
1065 | UNLOCK; |
1086 | UNLOCK; |
1066 | |
1087 | |
1067 | if (expect_false (next_has_throw)) |
1088 | if (expect_false (next->throw)) |
1068 | { |
1089 | { |
1069 | struct coro *coro = SvSTATE (coro_current); |
|
|
1070 | |
|
|
1071 | if (coro->throw) |
|
|
1072 | { |
|
|
1073 | SV *exception = coro->throw; |
1090 | SV *exception = sv_2mortal (next->throw); |
|
|
1091 | |
1074 | coro->throw = 0; |
1092 | next->throw = 0; |
1075 | sv_setsv (ERRSV, exception); |
1093 | sv_setsv (ERRSV, exception); |
1076 | croak (0); |
1094 | croak (0); |
1077 | } |
|
|
1078 | } |
1095 | } |
1079 | } |
1096 | } |
1080 | |
1097 | |
1081 | /* |
1098 | /* |
1082 | * this is a _very_ stripped down perl interpreter ;) |
1099 | * this is a _very_ stripped down perl interpreter ;) |
… | |
… | |
1090 | # endif |
1107 | # endif |
1091 | #endif |
1108 | #endif |
1092 | { |
1109 | { |
1093 | dTHX; |
1110 | dTHX; |
1094 | |
1111 | |
|
|
1112 | /* entersub called ENTER, but we never 'returned', undo that here */ |
|
|
1113 | ENTERSUB_TAIL; |
|
|
1114 | |
1095 | /* we now skip the entersub that lead to transfer () */ |
1115 | /* we now skip the entersub that did lead to transfer() */ |
1096 | PL_op = PL_op->op_next; |
1116 | PL_op = PL_op->op_next; |
1097 | |
1117 | |
1098 | /* inject a fake subroutine call to cctx_init */ |
1118 | /* inject a fake subroutine call to cctx_init */ |
1099 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1119 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1100 | |
1120 | |
1101 | /* cctx_run is the alternative tail of transfer () */ |
1121 | /* cctx_run is the alternative tail of transfer() */ |
1102 | transfer_tail (); |
1122 | transfer_tail (aTHX); |
1103 | |
1123 | |
1104 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1124 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1105 | PL_restartop = PL_op; |
1125 | PL_restartop = PL_op; |
1106 | perl_run (PL_curinterp); |
1126 | perl_run (PL_curinterp); |
1107 | |
1127 | |
… | |
… | |
1125 | ++cctx_count; |
1145 | ++cctx_count; |
1126 | New (0, cctx, 1, coro_cctx); |
1146 | New (0, cctx, 1, coro_cctx); |
1127 | |
1147 | |
1128 | cctx->gen = cctx_gen; |
1148 | cctx->gen = cctx_gen; |
1129 | cctx->flags = 0; |
1149 | cctx->flags = 0; |
1130 | cctx->idle_sp = 0; /* can be accessed by transfer between cctx_run and set_stacklevel */ |
1150 | cctx->idle_sp = 0; /* can be accessed by transfer between cctx_run and set_stacklevel, on throw */ |
1131 | |
1151 | |
1132 | return cctx; |
1152 | return cctx; |
1133 | } |
1153 | } |
1134 | |
1154 | |
1135 | /* create a new cctx only suitable as source */ |
1155 | /* create a new cctx only suitable as source */ |
… | |
… | |
1157 | /* mmap supposedly does allocate-on-write for us */ |
1177 | /* mmap supposedly does allocate-on-write for us */ |
1158 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
1178 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
1159 | |
1179 | |
1160 | if (cctx->sptr != (void *)-1) |
1180 | if (cctx->sptr != (void *)-1) |
1161 | { |
1181 | { |
1162 | # if CORO_STACKGUARD |
1182 | #if CORO_STACKGUARD |
1163 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
1183 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
1164 | # endif |
1184 | #endif |
1165 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
1185 | stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE; |
1166 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
1186 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
1167 | cctx->flags |= CC_MAPPED; |
1187 | cctx->flags |= CC_MAPPED; |
1168 | } |
1188 | } |
1169 | else |
1189 | else |
1170 | #endif |
1190 | #endif |
1171 | { |
1191 | { |
… | |
… | |
1180 | |
1200 | |
1181 | stack_start = cctx->sptr; |
1201 | stack_start = cctx->sptr; |
1182 | stack_size = cctx->ssize; |
1202 | stack_size = cctx->ssize; |
1183 | } |
1203 | } |
1184 | |
1204 | |
1185 | REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); |
1205 | #if CORO_USE_VALGRIND |
|
|
1206 | cctx->valgrind_id = VALGRIND_STACK_REGISTER ((char *)stack_start, (char *)stack_start + stack_size); |
|
|
1207 | #endif |
|
|
1208 | |
1186 | coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); |
1209 | coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); |
1187 | |
1210 | |
1188 | return cctx; |
1211 | return cctx; |
1189 | } |
1212 | } |
1190 | |
1213 | |
… | |
… | |
1198 | coro_destroy (&cctx->cctx); |
1221 | coro_destroy (&cctx->cctx); |
1199 | |
1222 | |
1200 | /* coro_transfer creates new, empty cctx's */ |
1223 | /* coro_transfer creates new, empty cctx's */ |
1201 | if (cctx->sptr) |
1224 | if (cctx->sptr) |
1202 | { |
1225 | { |
1203 | #if CORO_USE_VALGRIND |
1226 | #if CORO_USE_VALGRIND |
1204 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1227 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1205 | #endif |
1228 | #endif |
1206 | |
1229 | |
1207 | #if HAVE_MMAP |
1230 | #if HAVE_MMAP |
1208 | if (cctx->flags & CC_MAPPED) |
1231 | if (cctx->flags & CC_MAPPED) |
1209 | munmap (cctx->sptr, cctx->ssize); |
1232 | munmap (cctx->sptr, cctx->ssize); |
1210 | else |
1233 | else |
… | |
… | |
1237 | } |
1260 | } |
1238 | |
1261 | |
1239 | static void |
1262 | static void |
1240 | cctx_put (coro_cctx *cctx) |
1263 | cctx_put (coro_cctx *cctx) |
1241 | { |
1264 | { |
1242 | assert (("cctx_put called on non-initialised cctx", cctx->sptr)); |
1265 | assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr)); |
1243 | |
1266 | |
1244 | /* free another cctx if overlimit */ |
1267 | /* free another cctx if overlimit */ |
1245 | if (expect_false (cctx_idle >= cctx_max_idle)) |
1268 | if (expect_false (cctx_idle >= cctx_max_idle)) |
1246 | { |
1269 | { |
1247 | coro_cctx *first = cctx_first; |
1270 | coro_cctx *first = cctx_first; |
… | |
… | |
1321 | else |
1344 | else |
1322 | load_perl (aTHX_ next); |
1345 | load_perl (aTHX_ next); |
1323 | |
1346 | |
1324 | prev__cctx = prev->cctx; |
1347 | prev__cctx = prev->cctx; |
1325 | |
1348 | |
1326 | if (prev__cctx->idle_sp == STACKLEVEL) asm volatile("");//D |
|
|
1327 | |
|
|
1328 | /* possibly "free" the cctx */ |
1349 | /* possibly untie and reuse the cctx */ |
1329 | if (expect_true ( |
1350 | if (expect_true ( |
1330 | prev__cctx->idle_sp == STACKLEVEL |
1351 | prev__cctx->idle_sp == STACKLEVEL |
1331 | && !(prev__cctx->flags & CC_TRACE) |
1352 | && !(prev__cctx->flags & CC_TRACE) |
1332 | && !force_cctx |
1353 | && !force_cctx |
1333 | )) |
1354 | )) |
1334 | { |
1355 | { |
1335 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
1356 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
1336 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
1357 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); |
1337 | |
1358 | |
1338 | prev->cctx = 0; |
1359 | prev->cctx = 0; |
1339 | |
1360 | |
1340 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1361 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1341 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
1362 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
… | |
… | |
1349 | ++next->usecount; |
1370 | ++next->usecount; |
1350 | |
1371 | |
1351 | if (expect_true (!next->cctx)) |
1372 | if (expect_true (!next->cctx)) |
1352 | next->cctx = cctx_get (aTHX); |
1373 | next->cctx = cctx_get (aTHX); |
1353 | |
1374 | |
1354 | next_has_throw = !!next->throw; |
1375 | assert (("FATAL: transfer_next already nonzero in Coro (please report)", !transfer_next)); |
|
|
1376 | transfer_next = next; |
1355 | |
1377 | |
1356 | if (expect_false (prev__cctx != next->cctx)) |
1378 | if (expect_false (prev__cctx != next->cctx)) |
1357 | { |
1379 | { |
1358 | prev__cctx->top_env = PL_top_env; |
1380 | prev__cctx->top_env = PL_top_env; |
1359 | PL_top_env = next->cctx->top_env; |
1381 | PL_top_env = next->cctx->top_env; |
1360 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1382 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1361 | } |
1383 | } |
1362 | |
1384 | |
1363 | free_coro_mortal (aTHX); |
|
|
1364 | UNLOCK; |
|
|
1365 | |
|
|
1366 | transfer_tail (); |
1385 | transfer_tail (aTHX); |
1367 | } |
1386 | } |
1368 | } |
1387 | } |
1369 | |
|
|
1370 | struct transfer_args |
|
|
1371 | { |
|
|
1372 | struct coro *prev, *next; |
|
|
1373 | }; |
|
|
1374 | |
1388 | |
1375 | #define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx)) |
1389 | #define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx)) |
1376 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
1390 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
1377 | |
1391 | |
1378 | /** high level stuff ********************************************************/ |
1392 | /** high level stuff ********************************************************/ |
… | |
… | |
1551 | |
1565 | |
1552 | static int |
1566 | static int |
1553 | api_is_ready (SV *coro_sv) |
1567 | api_is_ready (SV *coro_sv) |
1554 | { |
1568 | { |
1555 | dTHX; |
1569 | dTHX; |
|
|
1570 | |
1556 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1571 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1557 | } |
1572 | } |
1558 | |
1573 | |
1559 | static void |
1574 | INLINE void |
1560 | prepare_schedule (pTHX_ struct transfer_args *ta) |
1575 | prepare_schedule (pTHX_ struct transfer_args *ta) |
1561 | { |
1576 | { |
1562 | SV *prev_sv, *next_sv; |
1577 | SV *prev_sv, *next_sv; |
1563 | |
1578 | |
1564 | for (;;) |
1579 | for (;;) |
… | |
… | |
1590 | /* cannot transfer to destroyed coros, skip and look for next */ |
1605 | /* cannot transfer to destroyed coros, skip and look for next */ |
1591 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
1606 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
1592 | { |
1607 | { |
1593 | UNLOCK; |
1608 | UNLOCK; |
1594 | SvREFCNT_dec (next_sv); |
1609 | SvREFCNT_dec (next_sv); |
1595 | /* coro_nready is already taken care of by destroy */ |
1610 | /* coro_nready has already been taken care of by destroy */ |
1596 | continue; |
1611 | continue; |
1597 | } |
1612 | } |
1598 | |
1613 | |
1599 | --coro_nready; |
1614 | --coro_nready; |
1600 | UNLOCK; |
1615 | UNLOCK; |
… | |
… | |
1603 | |
1618 | |
1604 | /* free this only after the transfer */ |
1619 | /* free this only after the transfer */ |
1605 | prev_sv = SvRV (coro_current); |
1620 | prev_sv = SvRV (coro_current); |
1606 | ta->prev = SvSTATE (prev_sv); |
1621 | ta->prev = SvSTATE (prev_sv); |
1607 | TRANSFER_CHECK (*ta); |
1622 | TRANSFER_CHECK (*ta); |
1608 | assert (ta->next->flags & CF_READY); |
1623 | assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); |
1609 | ta->next->flags &= ~CF_READY; |
1624 | ta->next->flags &= ~CF_READY; |
1610 | SvRV_set (coro_current, next_sv); |
1625 | SvRV_set (coro_current, next_sv); |
1611 | |
1626 | |
1612 | LOCK; |
1627 | LOCK; |
1613 | free_coro_mortal (aTHX); |
1628 | free_coro_mortal (aTHX); |
1614 | coro_mortal = prev_sv; |
1629 | coro_mortal = prev_sv; |
1615 | UNLOCK; |
1630 | UNLOCK; |
1616 | } |
1631 | } |
1617 | |
1632 | |
1618 | static void |
1633 | INLINE void |
1619 | prepare_cede (pTHX_ struct transfer_args *ta) |
1634 | prepare_cede (pTHX_ struct transfer_args *ta) |
1620 | { |
1635 | { |
1621 | api_ready (coro_current); |
1636 | api_ready (coro_current); |
1622 | prepare_schedule (aTHX_ ta); |
1637 | prepare_schedule (aTHX_ ta); |
1623 | } |
1638 | } |
… | |
… | |
1897 | |
1912 | |
1898 | PUTBACK; |
1913 | PUTBACK; |
1899 | switch (ix) |
1914 | switch (ix) |
1900 | { |
1915 | { |
1901 | case 0: |
1916 | case 0: |
1902 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1917 | prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (ST (0))); |
1903 | ta.next = 0; |
|
|
1904 | break; |
1918 | break; |
1905 | |
1919 | |
1906 | case 1: |
1920 | case 1: |
1907 | if (items != 2) |
1921 | if (items != 2) |
1908 | croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items); |
1922 | croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items); |
… | |
… | |
2047 | is_destroyed = CF_DESTROYED |
2061 | is_destroyed = CF_DESTROYED |
2048 | CODE: |
2062 | CODE: |
2049 | RETVAL = boolSV (coro->flags & ix); |
2063 | RETVAL = boolSV (coro->flags & ix); |
2050 | OUTPUT: |
2064 | OUTPUT: |
2051 | RETVAL |
2065 | RETVAL |
|
|
2066 | |
|
|
2067 | void |
|
|
2068 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
|
|
2069 | PROTOTYPE: $;$ |
|
|
2070 | CODE: |
|
|
2071 | SvREFCNT_dec (self->throw); |
|
|
2072 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
2052 | |
2073 | |
2053 | void |
2074 | void |
2054 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
2075 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
2055 | |
2076 | |
2056 | SV * |
2077 | SV * |
… | |
… | |
2129 | |
2150 | |
2130 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
2151 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
2131 | coro_ready[i] = newAV (); |
2152 | coro_ready[i] = newAV (); |
2132 | |
2153 | |
2133 | { |
2154 | { |
2134 | SV *sv = perl_get_sv ("Coro::API", TRUE); |
2155 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
2135 | perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */ |
|
|
2136 | |
2156 | |
2137 | coroapi.schedule = api_schedule; |
2157 | coroapi.schedule = api_schedule; |
2138 | coroapi.cede = api_cede; |
2158 | coroapi.cede = api_cede; |
2139 | coroapi.cede_notself = api_cede_notself; |
2159 | coroapi.cede_notself = api_cede_notself; |
2140 | coroapi.ready = api_ready; |
2160 | coroapi.ready = api_ready; |
… | |
… | |
2200 | CODE: |
2220 | CODE: |
2201 | RETVAL = coro_nready; |
2221 | RETVAL = coro_nready; |
2202 | OUTPUT: |
2222 | OUTPUT: |
2203 | RETVAL |
2223 | RETVAL |
2204 | |
2224 | |
2205 | void |
|
|
2206 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
|
|
2207 | PROTOTYPE: $;$ |
|
|
2208 | CODE: |
|
|
2209 | SvREFCNT_dec (self->throw); |
|
|
2210 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
2211 | |
|
|
2212 | # for async_pool speedup |
2225 | # for async_pool speedup |
2213 | void |
2226 | void |
2214 | _pool_1 (SV *cb) |
2227 | _pool_1 (SV *cb) |
2215 | CODE: |
2228 | CODE: |
2216 | { |
2229 | { |
… | |
… | |
2415 | |
2428 | |
2416 | MODULE = Coro::State PACKAGE = PerlIO::cede |
2429 | MODULE = Coro::State PACKAGE = PerlIO::cede |
2417 | |
2430 | |
2418 | BOOT: |
2431 | BOOT: |
2419 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
2432 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
|
|
2433 | |