… | |
… | |
142 | #define NOINLINE attribute ((noinline)) |
142 | #define NOINLINE attribute ((noinline)) |
143 | |
143 | |
144 | #include "CoroAPI.h" |
144 | #include "CoroAPI.h" |
145 | |
145 | |
146 | #ifdef USE_ITHREADS |
146 | #ifdef USE_ITHREADS |
|
|
147 | |
147 | static perl_mutex coro_lock; |
148 | static perl_mutex coro_lock; |
148 | # define LOCK do { MUTEX_LOCK (&coro_lock); } while (0) |
149 | # define LOCK do { MUTEX_LOCK (&coro_lock); } while (0) |
149 | # define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0) |
150 | # define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0) |
|
|
151 | # if CORO_PTHREAD |
|
|
152 | static void *coro_thx; |
|
|
153 | # endif |
|
|
154 | |
150 | #else |
155 | #else |
|
|
156 | |
151 | # define LOCK (void)0 |
157 | # define LOCK (void)0 |
152 | # define UNLOCK (void)0 |
158 | # define UNLOCK (void)0 |
|
|
159 | |
153 | #endif |
160 | #endif |
|
|
161 | |
|
|
162 | # undef LOCK |
|
|
163 | # define LOCK (void)0 |
|
|
164 | # undef UNLOCK |
|
|
165 | # define UNLOCK (void)0 |
154 | |
166 | |
155 | /* helper storage struct for Coro::AIO */ |
167 | /* helper storage struct for Coro::AIO */ |
156 | struct io_state |
168 | struct io_state |
157 | { |
169 | { |
158 | AV *res; |
170 | AV *res; |
… | |
… | |
169 | static struct CoroAPI coroapi; |
181 | static struct CoroAPI coroapi; |
170 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
182 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
171 | static JMPENV *main_top_env; |
183 | static JMPENV *main_top_env; |
172 | static HV *coro_state_stash, *coro_stash; |
184 | static HV *coro_state_stash, *coro_stash; |
173 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
185 | 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 */ |
174 | |
187 | |
175 | static GV *irsgv; /* $/ */ |
188 | static GV *irsgv; /* $/ */ |
176 | static GV *stdoutgv; /* *STDOUT */ |
189 | static GV *stdoutgv; /* *STDOUT */ |
177 | static SV *rv_diehook; |
190 | static SV *rv_diehook; |
178 | static SV *rv_warnhook; |
191 | static SV *rv_warnhook; |
… | |
… | |
280 | #define PRIO_MIN -4 |
293 | #define PRIO_MIN -4 |
281 | |
294 | |
282 | /* for Coro.pm */ |
295 | /* for Coro.pm */ |
283 | static SV *coro_current; |
296 | static SV *coro_current; |
284 | static SV *coro_readyhook; |
297 | static SV *coro_readyhook; |
285 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
298 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
286 | static int coro_nready; |
299 | static int coro_nready; |
287 | static struct coro *coro_first; |
300 | static struct coro *coro_first; |
288 | |
301 | |
289 | /** lowlevel stuff **********************************************************/ |
302 | /** lowlevel stuff **********************************************************/ |
290 | |
303 | |
… | |
… | |
1017 | } |
1030 | } |
1018 | |
1031 | |
1019 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
1032 | /* 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 */ |
1033 | /* _cctx_init should be careful, as it could be called at almost any time */ |
1021 | /* during execution of a perl program */ |
1034 | /* during execution of a perl program */ |
|
|
1035 | /* also initialises PL_top_env */ |
1022 | static void NOINLINE |
1036 | static void NOINLINE |
1023 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1037 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1024 | { |
1038 | { |
1025 | dSP; |
1039 | dSP; |
1026 | LOGOP myop; |
1040 | LOGOP myop; |
… | |
… | |
1042 | PL_op = (OP *)&myop; |
1056 | PL_op = (OP *)&myop; |
1043 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
1057 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
1044 | SPAGAIN; |
1058 | SPAGAIN; |
1045 | } |
1059 | } |
1046 | |
1060 | |
|
|
1061 | /* the tail of transfer: execute stuff we can onyl do afetr a transfer */ |
|
|
1062 | static void |
|
|
1063 | transfer_tail (void) |
|
|
1064 | { |
|
|
1065 | UNLOCK; |
|
|
1066 | |
|
|
1067 | if (expect_false (next_has_throw)) |
|
|
1068 | { |
|
|
1069 | struct coro *coro = SvSTATE (coro_current); |
|
|
1070 | |
|
|
1071 | if (coro->throw) |
|
|
1072 | { |
|
|
1073 | SV *exception = coro->throw; |
|
|
1074 | coro->throw = 0; |
|
|
1075 | sv_setsv (ERRSV, exception); |
|
|
1076 | croak (0); |
|
|
1077 | } |
|
|
1078 | } |
|
|
1079 | } |
|
|
1080 | |
1047 | /* |
1081 | /* |
1048 | * this is a _very_ stripped down perl interpreter ;) |
1082 | * this is a _very_ stripped down perl interpreter ;) |
1049 | */ |
1083 | */ |
1050 | static void |
1084 | static void |
1051 | cctx_run (void *arg) |
1085 | cctx_run (void *arg) |
1052 | { |
1086 | { |
|
|
1087 | #ifdef USE_ITHREADS |
|
|
1088 | # if CORO_PTHREAD |
|
|
1089 | PERL_SET_CONTEXT (coro_thx); |
|
|
1090 | # endif |
|
|
1091 | #endif |
|
|
1092 | { |
1053 | dTHX; |
1093 | dTHX; |
1054 | |
1094 | |
1055 | /* cctx_run is the alternative tail of transfer(), so unlock here. */ |
|
|
1056 | UNLOCK; |
|
|
1057 | |
|
|
1058 | /* we now skip the entersub that lead to transfer() */ |
1095 | /* we now skip the entersub that lead to transfer () */ |
1059 | PL_op = PL_op->op_next; |
1096 | PL_op = PL_op->op_next; |
1060 | |
1097 | |
1061 | /* inject a fake subroutine call to cctx_init */ |
1098 | /* inject a fake subroutine call to cctx_init */ |
1062 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1099 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1063 | |
1100 | |
|
|
1101 | /* cctx_run is the alternative tail of transfer () */ |
|
|
1102 | transfer_tail (); |
|
|
1103 | |
1064 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1104 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1065 | PL_restartop = PL_op; |
1105 | PL_restartop = PL_op; |
1066 | perl_run (PL_curinterp); |
1106 | perl_run (PL_curinterp); |
1067 | |
1107 | |
1068 | /* |
1108 | /* |
1069 | * If perl-run returns we assume exit() was being called or the coro |
1109 | * 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) |
1110 | * 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 |
1111 | * 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" |
1112 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1073 | * coroutine as Coro has no such concept |
1113 | * coroutine as Coro has no such concept |
1074 | */ |
1114 | */ |
1075 | PL_top_env = main_top_env; |
1115 | PL_top_env = main_top_env; |
1076 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1116 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
|
|
1117 | } |
1077 | } |
1118 | } |
1078 | |
1119 | |
1079 | static coro_cctx * |
1120 | static coro_cctx * |
1080 | cctx_new () |
1121 | cctx_new () |
1081 | { |
1122 | { |
1082 | coro_cctx *cctx; |
1123 | coro_cctx *cctx; |
|
|
1124 | |
|
|
1125 | ++cctx_count; |
|
|
1126 | New (0, cctx, 1, coro_cctx); |
|
|
1127 | |
|
|
1128 | cctx->gen = cctx_gen; |
|
|
1129 | cctx->flags = 0; |
|
|
1130 | cctx->idle_sp = 0; /* can be accessed by transfer between cctx_run and set_stacklevel */ |
|
|
1131 | |
|
|
1132 | return cctx; |
|
|
1133 | } |
|
|
1134 | |
|
|
1135 | /* create a new cctx only suitable as source */ |
|
|
1136 | static coro_cctx * |
|
|
1137 | cctx_new_empty () |
|
|
1138 | { |
|
|
1139 | coro_cctx *cctx = cctx_new (); |
|
|
1140 | |
|
|
1141 | cctx->sptr = 0; |
|
|
1142 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
|
|
1143 | |
|
|
1144 | return cctx; |
|
|
1145 | } |
|
|
1146 | |
|
|
1147 | /* create a new cctx suitable as destination/running a perl interpreter */ |
|
|
1148 | static coro_cctx * |
|
|
1149 | cctx_new_run () |
|
|
1150 | { |
|
|
1151 | coro_cctx *cctx = cctx_new (); |
1083 | void *stack_start; |
1152 | void *stack_start; |
1084 | size_t stack_size; |
1153 | size_t stack_size; |
1085 | |
|
|
1086 | ++cctx_count; |
|
|
1087 | Newz (0, cctx, 1, coro_cctx); |
|
|
1088 | |
|
|
1089 | cctx->gen = cctx_gen; |
|
|
1090 | |
1154 | |
1091 | #if HAVE_MMAP |
1155 | #if HAVE_MMAP |
1092 | cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1156 | cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1093 | /* mmap supposedly does allocate-on-write for us */ |
1157 | /* 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); |
1158 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
… | |
… | |
1167 | return cctx; |
1231 | return cctx; |
1168 | |
1232 | |
1169 | cctx_destroy (cctx); |
1233 | cctx_destroy (cctx); |
1170 | } |
1234 | } |
1171 | |
1235 | |
1172 | return cctx_new (); |
1236 | return cctx_new_run (); |
1173 | } |
1237 | } |
1174 | |
1238 | |
1175 | static void |
1239 | static void |
1176 | cctx_put (coro_cctx *cctx) |
1240 | cctx_put (coro_cctx *cctx) |
1177 | { |
1241 | { |
… | |
… | |
1227 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1291 | ((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 */ |
1292 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1229 | } |
1293 | } |
1230 | else if (expect_true (prev != next)) |
1294 | else if (expect_true (prev != next)) |
1231 | { |
1295 | { |
1232 | static volatile int has_throw; |
|
|
1233 | coro_cctx *prev__cctx; |
1296 | coro_cctx *prev__cctx; |
1234 | |
1297 | |
1235 | if (expect_false (prev->flags & CF_NEW)) |
1298 | if (expect_false (prev->flags & CF_NEW)) |
1236 | { |
1299 | { |
1237 | /* create a new empty/source context */ |
1300 | /* create a new empty/source context */ |
1238 | ++cctx_count; |
1301 | 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; |
1302 | prev->flags &= ~CF_NEW; |
1244 | prev->flags |= CF_RUNNING; |
1303 | prev->flags |= CF_RUNNING; |
1245 | } |
1304 | } |
1246 | |
1305 | |
1247 | prev->flags &= ~CF_RUNNING; |
1306 | prev->flags &= ~CF_RUNNING; |
… | |
… | |
1262 | else |
1321 | else |
1263 | load_perl (aTHX_ next); |
1322 | load_perl (aTHX_ next); |
1264 | |
1323 | |
1265 | prev__cctx = prev->cctx; |
1324 | prev__cctx = prev->cctx; |
1266 | |
1325 | |
|
|
1326 | if (prev__cctx->idle_sp == STACKLEVEL) asm volatile("");//D |
|
|
1327 | |
1267 | /* possibly "free" the cctx */ |
1328 | /* possibly "free" the cctx */ |
1268 | if (expect_true ( |
1329 | if (expect_true ( |
1269 | prev__cctx->idle_sp == STACKLEVEL |
1330 | prev__cctx->idle_sp == STACKLEVEL |
1270 | && !(prev__cctx->flags & CC_TRACE) |
1331 | && !(prev__cctx->flags & CC_TRACE) |
1271 | && !force_cctx |
1332 | && !force_cctx |
… | |
… | |
1288 | ++next->usecount; |
1349 | ++next->usecount; |
1289 | |
1350 | |
1290 | if (expect_true (!next->cctx)) |
1351 | if (expect_true (!next->cctx)) |
1291 | next->cctx = cctx_get (aTHX); |
1352 | next->cctx = cctx_get (aTHX); |
1292 | |
1353 | |
1293 | has_throw = !!next->throw; |
1354 | next_has_throw = !!next->throw; |
1294 | |
1355 | |
1295 | if (expect_false (prev__cctx != next->cctx)) |
1356 | if (expect_false (prev__cctx != next->cctx)) |
1296 | { |
1357 | { |
1297 | prev__cctx->top_env = PL_top_env; |
1358 | prev__cctx->top_env = PL_top_env; |
1298 | PL_top_env = next->cctx->top_env; |
1359 | PL_top_env = next->cctx->top_env; |
… | |
… | |
1300 | } |
1361 | } |
1301 | |
1362 | |
1302 | free_coro_mortal (aTHX); |
1363 | free_coro_mortal (aTHX); |
1303 | UNLOCK; |
1364 | UNLOCK; |
1304 | |
1365 | |
1305 | if (expect_false (has_throw)) |
1366 | transfer_tail (); |
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 | } |
1367 | } |
1318 | } |
1368 | } |
1319 | |
1369 | |
1320 | struct transfer_args |
1370 | struct transfer_args |
1321 | { |
1371 | { |
… | |
… | |
1635 | struct coro *coro = SvSTATE (coro_sv); |
1685 | struct coro *coro = SvSTATE (coro_sv); |
1636 | |
1686 | |
1637 | if (flags & CC_TRACE) |
1687 | if (flags & CC_TRACE) |
1638 | { |
1688 | { |
1639 | if (!coro->cctx) |
1689 | if (!coro->cctx) |
1640 | coro->cctx = cctx_new (); |
1690 | coro->cctx = cctx_new_run (); |
1641 | else if (!(coro->cctx->flags & CC_TRACE)) |
1691 | else if (!(coro->cctx->flags & CC_TRACE)) |
1642 | croak ("cannot enable tracing on coroutine with custom stack"); |
1692 | croak ("cannot enable tracing on coroutine with custom stack"); |
1643 | |
1693 | |
1644 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1694 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1645 | } |
1695 | } |
… | |
… | |
1754 | |
1804 | |
1755 | BOOT: |
1805 | BOOT: |
1756 | { |
1806 | { |
1757 | #ifdef USE_ITHREADS |
1807 | #ifdef USE_ITHREADS |
1758 | MUTEX_INIT (&coro_lock); |
1808 | MUTEX_INIT (&coro_lock); |
|
|
1809 | # if CORO_PTHREAD |
|
|
1810 | coro_thx = PERL_GET_CONTEXT; |
|
|
1811 | # endif |
1759 | #endif |
1812 | #endif |
1760 | BOOT_PAGESIZE; |
1813 | BOOT_PAGESIZE; |
1761 | |
1814 | |
1762 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1815 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1763 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1816 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |