… | |
… | |
98 | # define newSV(l) NEWSV(0,l) |
98 | # define newSV(l) NEWSV(0,l) |
99 | #endif |
99 | #endif |
100 | #ifndef CvISXSUB_on |
100 | #ifndef CvISXSUB_on |
101 | # define CvISXSUB_on(cv) (void)cv |
101 | # define CvISXSUB_on(cv) (void)cv |
102 | #endif |
102 | #endif |
|
|
103 | #ifndef CvISXSUB |
|
|
104 | # define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE) |
|
|
105 | #endif |
|
|
106 | #ifndef Newx |
|
|
107 | # define Newx(ptr,nitems,type) New (0,ptr,nitems,type) |
|
|
108 | #endif |
103 | |
109 | |
104 | /* 5.8.7 */ |
110 | /* 5.8.7 */ |
105 | #ifndef SvRV_set |
111 | #ifndef SvRV_set |
106 | # define SvRV_set(s,v) SvRV(s) = (v) |
112 | # define SvRV_set(s,v) SvRV(s) = (v) |
107 | #endif |
113 | #endif |
… | |
… | |
167 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
173 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
168 | static JMPENV *main_top_env; |
174 | static JMPENV *main_top_env; |
169 | static HV *coro_state_stash, *coro_stash; |
175 | static HV *coro_state_stash, *coro_stash; |
170 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
176 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
171 | |
177 | |
|
|
178 | static AV *av_destroy; /* destruction queue */ |
|
|
179 | static SV *sv_manager; /* the manager coro */ |
|
|
180 | |
172 | static GV *irsgv; /* $/ */ |
181 | static GV *irsgv; /* $/ */ |
173 | static GV *stdoutgv; /* *STDOUT */ |
182 | static GV *stdoutgv; /* *STDOUT */ |
174 | static SV *rv_diehook; |
183 | static SV *rv_diehook; |
175 | static SV *rv_warnhook; |
184 | static SV *rv_warnhook; |
176 | static HV *hv_sig; /* %SIG */ |
185 | static HV *hv_sig; /* %SIG */ |
177 | |
186 | |
178 | /* async_pool helper stuff */ |
187 | /* async_pool helper stuff */ |
179 | static SV *sv_pool_rss; |
188 | static SV *sv_pool_rss; |
180 | static SV *sv_pool_size; |
189 | static SV *sv_pool_size; |
181 | static SV *sv_async_pool_idle; |
190 | static SV *sv_async_pool_idle; /* description string */ |
182 | static AV *av_async_pool; |
191 | static AV *av_async_pool; /* idle pool */ |
183 | static SV *sv_Coro; |
192 | static SV *sv_Coro; /* class string */ |
184 | static CV *cv_pool_handler; |
193 | static CV *cv_pool_handler; |
185 | static CV *cv_coro_new; |
194 | static CV *cv_coro_state_new; |
186 | |
195 | |
187 | /* Coro::AnyEvent */ |
196 | /* Coro::AnyEvent */ |
188 | static SV *sv_activity; |
197 | static SV *sv_activity; |
189 | |
198 | |
190 | static struct coro_cctx *cctx_first; |
199 | static struct coro_cctx *cctx_first; |
… | |
… | |
219 | int valgrind_id; |
228 | int valgrind_id; |
220 | #endif |
229 | #endif |
221 | unsigned char flags; |
230 | unsigned char flags; |
222 | } coro_cctx; |
231 | } coro_cctx; |
223 | |
232 | |
|
|
233 | coro_cctx *cctx_current; /* the currently running cctx */ |
|
|
234 | |
|
|
235 | /*****************************************************************************/ |
|
|
236 | |
224 | enum { |
237 | enum { |
225 | CF_RUNNING = 0x0001, /* coroutine is running */ |
238 | CF_RUNNING = 0x0001, /* coroutine is running */ |
226 | CF_READY = 0x0002, /* coroutine is ready */ |
239 | CF_READY = 0x0002, /* coroutine is ready */ |
227 | CF_NEW = 0x0004, /* has never been switched to */ |
240 | CF_NEW = 0x0004, /* has never been switched to */ |
228 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
241 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
… | |
… | |
341 | GV *gvp; |
354 | GV *gvp; |
342 | return sv_2cv (sv, &st, &gvp, 0); |
355 | return sv_2cv (sv, &st, &gvp, 0); |
343 | } |
356 | } |
344 | |
357 | |
345 | static AV * |
358 | static AV * |
346 | coro_clone_padlist (pTHX_ CV *cv) |
359 | coro_derive_padlist (pTHX_ CV *cv) |
347 | { |
360 | { |
348 | AV *padlist = CvPADLIST (cv); |
361 | AV *padlist = CvPADLIST (cv); |
349 | AV *newpadlist, *newpad; |
362 | AV *newpadlist, *newpad; |
350 | |
363 | |
351 | newpadlist = newAV (); |
364 | newpadlist = newAV (); |
… | |
… | |
471 | CV *cp = Perl_cv_clone (aTHX_ cv); |
484 | CV *cp = Perl_cv_clone (aTHX_ cv); |
472 | CvPADLIST (cv) = CvPADLIST (cp); |
485 | CvPADLIST (cv) = CvPADLIST (cp); |
473 | CvPADLIST (cp) = 0; |
486 | CvPADLIST (cp) = 0; |
474 | SvREFCNT_dec (cp); |
487 | SvREFCNT_dec (cp); |
475 | #else |
488 | #else |
476 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
489 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
477 | #endif |
490 | #endif |
478 | } |
491 | } |
479 | } |
492 | } |
480 | |
493 | |
481 | static void |
494 | static void |
… | |
… | |
831 | slf_check_nop (pTHX_ struct CoroSLF *frame) |
844 | slf_check_nop (pTHX_ struct CoroSLF *frame) |
832 | { |
845 | { |
833 | return 0; |
846 | return 0; |
834 | } |
847 | } |
835 | |
848 | |
|
|
849 | static int |
|
|
850 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
|
|
851 | { |
|
|
852 | return 1; |
|
|
853 | } |
|
|
854 | |
836 | static UNOP coro_setup_op; |
855 | static UNOP coro_setup_op; |
837 | |
856 | |
838 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
857 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
839 | coro_setup (pTHX_ struct coro *coro) |
858 | coro_setup (pTHX_ struct coro *coro) |
840 | { |
859 | { |
… | |
… | |
889 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
908 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
890 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
909 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
891 | |
910 | |
892 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
911 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
893 | coro_setup_op.op_next = PL_op; |
912 | coro_setup_op.op_next = PL_op; |
894 | coro_setup_op.op_type = OP_CUSTOM; |
913 | coro_setup_op.op_type = OP_ENTERSUB; |
895 | coro_setup_op.op_ppaddr = pp_slf; |
914 | coro_setup_op.op_ppaddr = pp_slf; |
896 | /* no flags etc. required, as an init function won't be called */ |
915 | /* no flags etc. required, as an init function won't be called */ |
897 | |
916 | |
898 | PL_op = (OP *)&coro_setup_op; |
917 | PL_op = (OP *)&coro_setup_op; |
899 | |
918 | |
… | |
… | |
1014 | { |
1033 | { |
1015 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1034 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1016 | |
1035 | |
1017 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1036 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1018 | { |
1037 | { |
1019 | runops_proc_t old_runops = PL_runops; |
|
|
1020 | dSP; |
1038 | dSP; |
1021 | GV *gv = CvGV (cx->blk_sub.cv); |
1039 | GV *gv = CvGV (cx->blk_sub.cv); |
1022 | SV *fullname = sv_2mortal (newSV (0)); |
1040 | SV *fullname = sv_2mortal (newSV (0)); |
1023 | |
1041 | |
1024 | if (isGV (gv)) |
1042 | if (isGV (gv)) |
… | |
… | |
1071 | |
1089 | |
1072 | TAINT_NOT; |
1090 | TAINT_NOT; |
1073 | return 0; |
1091 | return 0; |
1074 | } |
1092 | } |
1075 | |
1093 | |
1076 | static struct coro_cctx *cctx_ssl_cctx; |
|
|
1077 | static struct CoroSLF cctx_ssl_frame; |
1094 | static struct CoroSLF cctx_ssl_frame; |
1078 | |
1095 | |
1079 | static void |
1096 | static void |
1080 | slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) |
1097 | slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) |
1081 | { |
1098 | { |
1082 | ta->prev = (struct coro *)cctx_ssl_cctx; |
|
|
1083 | ta->next = 0; |
1099 | ta->prev = 0; |
1084 | } |
1100 | } |
1085 | |
1101 | |
1086 | static int |
1102 | static int |
1087 | slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) |
1103 | slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) |
1088 | { |
1104 | { |
… | |
… | |
1091 | return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ |
1107 | return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ |
1092 | } |
1108 | } |
1093 | |
1109 | |
1094 | /* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ |
1110 | /* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ |
1095 | static void NOINLINE |
1111 | static void NOINLINE |
1096 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1112 | cctx_prepare (pTHX) |
1097 | { |
1113 | { |
1098 | PL_top_env = &PL_start_env; |
1114 | PL_top_env = &PL_start_env; |
1099 | |
1115 | |
1100 | if (cctx->flags & CC_TRACE) |
1116 | if (cctx_current->flags & CC_TRACE) |
1101 | PL_runops = runops_trace; |
1117 | PL_runops = runops_trace; |
1102 | |
1118 | |
1103 | /* we already must be executing an SLF op, there is no other valid way |
1119 | /* we already must be executing an SLF op, there is no other valid way |
1104 | * that can lead to creation of a new cctx */ |
1120 | * that can lead to creation of a new cctx */ |
1105 | assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", |
1121 | assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", |
1106 | slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); |
1122 | slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); |
1107 | |
1123 | |
1108 | /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ |
1124 | /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ |
1109 | cctx_ssl_cctx = cctx; |
|
|
1110 | cctx_ssl_frame = slf_frame; |
1125 | cctx_ssl_frame = slf_frame; |
1111 | |
1126 | |
1112 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1127 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1113 | slf_frame.check = slf_check_set_stacklevel; |
1128 | slf_frame.check = slf_check_set_stacklevel; |
1114 | } |
1129 | } |
… | |
… | |
1137 | /* normally we would need to skip the entersub here */ |
1152 | /* normally we would need to skip the entersub here */ |
1138 | /* not doing so will re-execute it, which is exactly what we want */ |
1153 | /* not doing so will re-execute it, which is exactly what we want */ |
1139 | /* PL_nop = PL_nop->op_next */ |
1154 | /* PL_nop = PL_nop->op_next */ |
1140 | |
1155 | |
1141 | /* inject a fake subroutine call to cctx_init */ |
1156 | /* inject a fake subroutine call to cctx_init */ |
1142 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1157 | cctx_prepare (aTHX); |
1143 | |
1158 | |
1144 | /* cctx_run is the alternative tail of transfer() */ |
1159 | /* cctx_run is the alternative tail of transfer() */ |
1145 | transfer_tail (aTHX); |
1160 | transfer_tail (aTHX); |
1146 | |
1161 | |
1147 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1162 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
… | |
… | |
1242 | cctx_destroy (coro_cctx *cctx) |
1257 | cctx_destroy (coro_cctx *cctx) |
1243 | { |
1258 | { |
1244 | if (!cctx) |
1259 | if (!cctx) |
1245 | return; |
1260 | return; |
1246 | |
1261 | |
|
|
1262 | assert (cctx != cctx_current);//D temporary |
|
|
1263 | |
1247 | --cctx_count; |
1264 | --cctx_count; |
1248 | coro_destroy (&cctx->cctx); |
1265 | coro_destroy (&cctx->cctx); |
1249 | |
1266 | |
1250 | /* coro_transfer creates new, empty cctx's */ |
1267 | /* coro_transfer creates new, empty cctx's */ |
1251 | if (cctx->sptr) |
1268 | if (cctx->sptr) |
… | |
… | |
1336 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1353 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1337 | { |
1354 | { |
1338 | dSTACKLEVEL; |
1355 | dSTACKLEVEL; |
1339 | |
1356 | |
1340 | /* sometimes transfer is only called to set idle_sp */ |
1357 | /* sometimes transfer is only called to set idle_sp */ |
1341 | if (expect_false (!next)) |
1358 | if (expect_false (!prev)) |
1342 | { |
1359 | { |
1343 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1360 | cctx_current->idle_sp = STACKLEVEL; |
1344 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1361 | assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1345 | } |
1362 | } |
1346 | else if (expect_true (prev != next)) |
1363 | else if (expect_true (prev != next)) |
1347 | { |
1364 | { |
1348 | coro_cctx *prev__cctx; |
1365 | coro_cctx *cctx_prev; |
1349 | |
1366 | |
1350 | if (expect_false (prev->flags & CF_NEW)) |
1367 | if (expect_false (prev->flags & CF_NEW)) |
1351 | { |
1368 | { |
1352 | /* create a new empty/source context */ |
1369 | /* create a new empty/source context */ |
1353 | prev->cctx = cctx_new_empty (); |
|
|
1354 | prev->flags &= ~CF_NEW; |
1370 | prev->flags &= ~CF_NEW; |
1355 | prev->flags |= CF_RUNNING; |
1371 | prev->flags |= CF_RUNNING; |
1356 | } |
1372 | } |
1357 | |
1373 | |
1358 | prev->flags &= ~CF_RUNNING; |
1374 | prev->flags &= ~CF_RUNNING; |
… | |
… | |
1369 | coro_setup (aTHX_ next); |
1385 | coro_setup (aTHX_ next); |
1370 | } |
1386 | } |
1371 | else |
1387 | else |
1372 | load_perl (aTHX_ next); |
1388 | load_perl (aTHX_ next); |
1373 | |
1389 | |
1374 | prev__cctx = prev->cctx; |
|
|
1375 | |
|
|
1376 | /* possibly untie and reuse the cctx */ |
1390 | /* possibly untie and reuse the cctx */ |
1377 | if (expect_true ( |
1391 | if (expect_true ( |
1378 | prev__cctx->idle_sp == STACKLEVEL |
1392 | cctx_current->idle_sp == STACKLEVEL |
1379 | && !(prev__cctx->flags & CC_TRACE) |
1393 | && !(cctx_current->flags & CC_TRACE) |
1380 | && !force_cctx |
1394 | && !force_cctx |
1381 | )) |
1395 | )) |
1382 | { |
1396 | { |
1383 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1397 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1384 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); |
1398 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te)); |
1385 | |
|
|
1386 | prev->cctx = 0; |
|
|
1387 | |
1399 | |
1388 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1400 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1389 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
1401 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
1390 | if (expect_false (CCTX_EXPIRED (prev__cctx))) |
1402 | if (expect_false (CCTX_EXPIRED (cctx_current))) |
1391 | if (!next->cctx) |
1403 | if (!next->cctx) |
1392 | next->cctx = cctx_get (aTHX); |
1404 | next->cctx = cctx_get (aTHX); |
1393 | |
1405 | |
1394 | cctx_put (prev__cctx); |
1406 | cctx_put (cctx_current); |
|
|
1407 | assert (!prev->cctx);//D temporary |
1395 | } |
1408 | } |
|
|
1409 | else |
|
|
1410 | prev->cctx = cctx_current; |
1396 | |
1411 | |
1397 | ++next->usecount; |
1412 | ++next->usecount; |
1398 | |
1413 | |
1399 | if (expect_true (!next->cctx)) |
1414 | cctx_prev = cctx_current; |
1400 | next->cctx = cctx_get (aTHX); |
1415 | cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX); |
1401 | |
1416 | |
1402 | if (expect_false (prev__cctx != next->cctx)) |
1417 | next->cctx = 0; |
|
|
1418 | |
|
|
1419 | if (expect_false (cctx_prev != cctx_current)) |
1403 | { |
1420 | { |
1404 | prev__cctx->top_env = PL_top_env; |
1421 | cctx_prev->top_env = PL_top_env; |
1405 | PL_top_env = next->cctx->top_env; |
1422 | PL_top_env = cctx_current->top_env; |
1406 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1423 | coro_transfer (&cctx_prev->cctx, &cctx_current->cctx); |
1407 | } |
1424 | } |
1408 | |
1425 | |
1409 | transfer_tail (aTHX); |
1426 | transfer_tail (aTHX); |
1410 | } |
1427 | } |
1411 | } |
1428 | } |
… | |
… | |
1609 | api_is_ready (pTHX_ SV *coro_sv) |
1626 | api_is_ready (pTHX_ SV *coro_sv) |
1610 | { |
1627 | { |
1611 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1628 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1612 | } |
1629 | } |
1613 | |
1630 | |
|
|
1631 | /* expects to own a reference to next->hv */ |
1614 | INLINE void |
1632 | INLINE void |
|
|
1633 | prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
|
|
1634 | { |
|
|
1635 | SV *prev_sv = SvRV (coro_current); |
|
|
1636 | |
|
|
1637 | ta->prev = SvSTATE_hv (prev_sv); |
|
|
1638 | ta->next = next; |
|
|
1639 | |
|
|
1640 | TRANSFER_CHECK (*ta); |
|
|
1641 | |
|
|
1642 | SvRV_set (coro_current, (SV *)next->hv); |
|
|
1643 | |
|
|
1644 | free_coro_mortal (aTHX); |
|
|
1645 | coro_mortal = prev_sv; |
|
|
1646 | } |
|
|
1647 | |
|
|
1648 | static void |
1615 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1649 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1616 | { |
1650 | { |
1617 | SV *prev_sv, *next_sv; |
|
|
1618 | |
|
|
1619 | for (;;) |
1651 | for (;;) |
1620 | { |
1652 | { |
1621 | next_sv = coro_deq (aTHX); |
1653 | SV *next_sv = coro_deq (aTHX); |
1622 | |
1654 | |
1623 | /* nothing to schedule: call the idle handler */ |
|
|
1624 | if (expect_false (!next_sv)) |
1655 | if (expect_true (next_sv)) |
1625 | { |
1656 | { |
|
|
1657 | struct coro *next = SvSTATE_hv (next_sv); |
|
|
1658 | |
|
|
1659 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1660 | if (expect_false (next->flags & CF_DESTROYED)) |
|
|
1661 | SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ |
|
|
1662 | else |
|
|
1663 | { |
|
|
1664 | next->flags &= ~CF_READY; |
|
|
1665 | --coro_nready; |
|
|
1666 | |
|
|
1667 | prepare_schedule_to (aTHX_ ta, next); |
|
|
1668 | break; |
|
|
1669 | } |
|
|
1670 | } |
|
|
1671 | else |
|
|
1672 | { |
|
|
1673 | /* nothing to schedule: call the idle handler */ |
1626 | dSP; |
1674 | dSP; |
1627 | |
1675 | |
1628 | ENTER; |
1676 | ENTER; |
1629 | SAVETMPS; |
1677 | SAVETMPS; |
1630 | |
1678 | |
… | |
… | |
1632 | PUTBACK; |
1680 | PUTBACK; |
1633 | call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); |
1681 | call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); |
1634 | |
1682 | |
1635 | FREETMPS; |
1683 | FREETMPS; |
1636 | LEAVE; |
1684 | LEAVE; |
1637 | continue; |
|
|
1638 | } |
1685 | } |
1639 | |
|
|
1640 | ta->next = SvSTATE_hv (next_sv); |
|
|
1641 | |
|
|
1642 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1643 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
|
|
1644 | { |
|
|
1645 | SvREFCNT_dec (next_sv); |
|
|
1646 | /* coro_nready has already been taken care of by destroy */ |
|
|
1647 | continue; |
|
|
1648 | } |
|
|
1649 | |
|
|
1650 | --coro_nready; |
|
|
1651 | break; |
|
|
1652 | } |
1686 | } |
1653 | |
|
|
1654 | /* free this only after the transfer */ |
|
|
1655 | prev_sv = SvRV (coro_current); |
|
|
1656 | ta->prev = SvSTATE_hv (prev_sv); |
|
|
1657 | TRANSFER_CHECK (*ta); |
|
|
1658 | assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); |
|
|
1659 | ta->next->flags &= ~CF_READY; |
|
|
1660 | SvRV_set (coro_current, next_sv); |
|
|
1661 | |
|
|
1662 | free_coro_mortal (aTHX); |
|
|
1663 | coro_mortal = prev_sv; |
|
|
1664 | } |
1687 | } |
1665 | |
1688 | |
1666 | INLINE void |
1689 | INLINE void |
1667 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1690 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1668 | { |
1691 | { |
… | |
… | |
1689 | { |
1712 | { |
1690 | struct coro_transfer_args ta; |
1713 | struct coro_transfer_args ta; |
1691 | |
1714 | |
1692 | prepare_schedule (aTHX_ &ta); |
1715 | prepare_schedule (aTHX_ &ta); |
1693 | TRANSFER (ta, 1); |
1716 | TRANSFER (ta, 1); |
|
|
1717 | } |
|
|
1718 | |
|
|
1719 | static void |
|
|
1720 | api_schedule_to (pTHX_ SV *coro_sv) |
|
|
1721 | { |
|
|
1722 | struct coro_transfer_args ta; |
|
|
1723 | struct coro *next = SvSTATE (coro_sv); |
|
|
1724 | |
|
|
1725 | SvREFCNT_inc_NN (coro_sv); |
|
|
1726 | prepare_schedule_to (aTHX_ &ta, next); |
1694 | } |
1727 | } |
1695 | |
1728 | |
1696 | static int |
1729 | static int |
1697 | api_cede (pTHX) |
1730 | api_cede (pTHX) |
1698 | { |
1731 | { |
… | |
… | |
1745 | if (coro->flags & CF_RUNNING) |
1778 | if (coro->flags & CF_RUNNING) |
1746 | PL_runops = RUNOPS_DEFAULT; |
1779 | PL_runops = RUNOPS_DEFAULT; |
1747 | else |
1780 | else |
1748 | coro->slot->runops = RUNOPS_DEFAULT; |
1781 | coro->slot->runops = RUNOPS_DEFAULT; |
1749 | } |
1782 | } |
|
|
1783 | } |
|
|
1784 | |
|
|
1785 | static void |
|
|
1786 | coro_call_on_destroy (pTHX_ struct coro *coro) |
|
|
1787 | { |
|
|
1788 | SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0); |
|
|
1789 | SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0); |
|
|
1790 | |
|
|
1791 | if (on_destroyp) |
|
|
1792 | { |
|
|
1793 | AV *on_destroy = (AV *)SvRV (*on_destroyp); |
|
|
1794 | |
|
|
1795 | while (AvFILLp (on_destroy) >= 0) |
|
|
1796 | { |
|
|
1797 | dSP; /* don't disturb outer sp */ |
|
|
1798 | SV *cb = av_pop (on_destroy); |
|
|
1799 | |
|
|
1800 | PUSHMARK (SP); |
|
|
1801 | |
|
|
1802 | if (statusp) |
|
|
1803 | { |
|
|
1804 | int i; |
|
|
1805 | AV *status = (AV *)SvRV (*statusp); |
|
|
1806 | EXTEND (SP, AvFILLp (status) + 1); |
|
|
1807 | |
|
|
1808 | for (i = 0; i <= AvFILLp (status); ++i) |
|
|
1809 | PUSHs (AvARRAY (status)[i]); |
|
|
1810 | } |
|
|
1811 | |
|
|
1812 | PUTBACK; |
|
|
1813 | call_sv (sv_2mortal (cb), G_VOID | G_DISCARD); |
|
|
1814 | } |
|
|
1815 | } |
|
|
1816 | } |
|
|
1817 | |
|
|
1818 | static void |
|
|
1819 | slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1820 | { |
|
|
1821 | int i; |
|
|
1822 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1823 | AV *av = newAV (); |
|
|
1824 | |
|
|
1825 | av_extend (av, items - 1); |
|
|
1826 | for (i = 0; i < items; ++i) |
|
|
1827 | av_push (av, SvREFCNT_inc_NN (arg [i])); |
|
|
1828 | |
|
|
1829 | hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); |
|
|
1830 | |
|
|
1831 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
|
|
1832 | api_ready (aTHX_ sv_manager); |
|
|
1833 | |
|
|
1834 | frame->prepare = prepare_schedule; |
|
|
1835 | frame->check = slf_check_repeat; |
1750 | } |
1836 | } |
1751 | |
1837 | |
1752 | /*****************************************************************************/ |
1838 | /*****************************************************************************/ |
1753 | /* async pool handler */ |
1839 | /* async pool handler */ |
1754 | |
1840 | |
… | |
… | |
1987 | static void |
2073 | static void |
1988 | slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2074 | slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1989 | { |
2075 | { |
1990 | frame->prepare = prepare_schedule; |
2076 | frame->prepare = prepare_schedule; |
1991 | frame->check = slf_check_nop; |
2077 | frame->check = slf_check_nop; |
|
|
2078 | } |
|
|
2079 | |
|
|
2080 | static void |
|
|
2081 | slf_prepare_schedule_to (pTHX_ struct coro_transfer_args *ta) |
|
|
2082 | { |
|
|
2083 | struct coro *next = (struct coro *)slf_frame.data; |
|
|
2084 | |
|
|
2085 | SvREFCNT_inc_NN (next->hv); |
|
|
2086 | prepare_schedule_to (aTHX_ ta, next); |
|
|
2087 | } |
|
|
2088 | |
|
|
2089 | static void |
|
|
2090 | slf_init_schedule_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2091 | { |
|
|
2092 | if (!items) |
|
|
2093 | croak ("Coro::schedule_to expects a coroutine argument, caught"); |
|
|
2094 | |
|
|
2095 | frame->data = (void *)SvSTATE (arg [0]); |
|
|
2096 | frame->prepare = slf_prepare_schedule_to; |
|
|
2097 | frame->check = slf_check_nop; |
|
|
2098 | } |
|
|
2099 | |
|
|
2100 | static void |
|
|
2101 | slf_init_cede_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2102 | { |
|
|
2103 | api_ready (aTHX_ SvRV (coro_current)); |
|
|
2104 | |
|
|
2105 | slf_init_schedule_to (aTHX_ frame, cv, arg, items); |
1992 | } |
2106 | } |
1993 | |
2107 | |
1994 | static void |
2108 | static void |
1995 | slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2109 | slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1996 | { |
2110 | { |
… | |
… | |
2142 | } |
2256 | } |
2143 | else |
2257 | else |
2144 | slf_argc = 0; |
2258 | slf_argc = 0; |
2145 | |
2259 | |
2146 | PL_op->op_ppaddr = pp_slf; |
2260 | PL_op->op_ppaddr = pp_slf; |
2147 | PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ |
2261 | /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ |
2148 | |
2262 | |
2149 | PL_op = (OP *)&slf_restore; |
2263 | PL_op = (OP *)&slf_restore; |
2150 | } |
2264 | } |
2151 | |
2265 | |
2152 | /*****************************************************************************/ |
2266 | /*****************************************************************************/ |
… | |
… | |
2616 | XSRETURN_EMPTY; |
2730 | XSRETURN_EMPTY; |
2617 | } |
2731 | } |
2618 | |
2732 | |
2619 | /*****************************************************************************/ |
2733 | /*****************************************************************************/ |
2620 | |
2734 | |
|
|
2735 | #if CORO_CLONE |
|
|
2736 | # include "clone.c" |
|
|
2737 | #endif |
|
|
2738 | |
2621 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2739 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2622 | |
2740 | |
2623 | PROTOTYPES: DISABLE |
2741 | PROTOTYPES: DISABLE |
2624 | |
2742 | |
2625 | BOOT: |
2743 | BOOT: |
… | |
… | |
2629 | coro_thx = PERL_GET_CONTEXT; |
2747 | coro_thx = PERL_GET_CONTEXT; |
2630 | # endif |
2748 | # endif |
2631 | #endif |
2749 | #endif |
2632 | BOOT_PAGESIZE; |
2750 | BOOT_PAGESIZE; |
2633 | |
2751 | |
|
|
2752 | cctx_current = cctx_new_empty (); |
|
|
2753 | |
2634 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
2754 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
2635 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
2755 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
2636 | |
2756 | |
2637 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
2757 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
2638 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
2758 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
… | |
… | |
2657 | |
2777 | |
2658 | { |
2778 | { |
2659 | SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); |
2779 | SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); |
2660 | |
2780 | |
2661 | if (!PL_custom_op_names) PL_custom_op_names = newHV (); |
2781 | if (!PL_custom_op_names) PL_custom_op_names = newHV (); |
2662 | hv_store_ent (PL_custom_op_names, slf, |
2782 | hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0); |
2663 | newSVpv ("coro_slf", 0), 0); |
|
|
2664 | |
2783 | |
2665 | if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); |
2784 | if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); |
2666 | hv_store_ent (PL_custom_op_descs, slf, |
2785 | hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0); |
2667 | newSVpv ("coro schedule like function", 0), 0); |
|
|
2668 | } |
2786 | } |
2669 | |
2787 | |
2670 | coroapi.ver = CORO_API_VERSION; |
2788 | coroapi.ver = CORO_API_VERSION; |
2671 | coroapi.rev = CORO_API_REVISION; |
2789 | coroapi.rev = CORO_API_REVISION; |
2672 | |
2790 | |
… | |
… | |
2765 | void |
2883 | void |
2766 | _exit (int code) |
2884 | _exit (int code) |
2767 | PROTOTYPE: $ |
2885 | PROTOTYPE: $ |
2768 | CODE: |
2886 | CODE: |
2769 | _exit (code); |
2887 | _exit (code); |
|
|
2888 | |
|
|
2889 | SV * |
|
|
2890 | clone (Coro::State coro) |
|
|
2891 | CODE: |
|
|
2892 | { |
|
|
2893 | #if CORO_CLONE |
|
|
2894 | struct coro *ncoro = coro_clone (coro); |
|
|
2895 | MAGIC *mg; |
|
|
2896 | /* TODO: too much duplication */ |
|
|
2897 | ncoro->hv = newHV (); |
|
|
2898 | mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); |
|
|
2899 | mg->mg_flags |= MGf_DUP; |
|
|
2900 | RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv)); |
|
|
2901 | #else |
|
|
2902 | croak ("Coro::State->clone has not been configured into this installation of Coro, realised"); |
|
|
2903 | #endif |
|
|
2904 | } |
|
|
2905 | OUTPUT: |
|
|
2906 | RETVAL |
2770 | |
2907 | |
2771 | int |
2908 | int |
2772 | cctx_stacksize (int new_stacksize = 0) |
2909 | cctx_stacksize (int new_stacksize = 0) |
2773 | PROTOTYPE: ;$ |
2910 | PROTOTYPE: ;$ |
2774 | CODE: |
2911 | CODE: |
… | |
… | |
2950 | |
3087 | |
2951 | BOOT: |
3088 | BOOT: |
2952 | { |
3089 | { |
2953 | int i; |
3090 | int i; |
2954 | |
3091 | |
2955 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
|
|
2956 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3092 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
2957 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3093 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
2958 | cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); |
3094 | cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); |
2959 | cv_coro_terminate = get_cv ( "Coro::terminate", GV_ADD); |
3095 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
2960 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); |
3096 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
2961 | SvREADONLY_on (coro_current); |
3097 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
|
|
3098 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
|
|
3099 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
2962 | |
3100 | |
2963 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3101 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
2964 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3102 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
2965 | cv_pool_handler = get_cv ("Coro::_pool_handler", 0); SvREADONLY_on (cv_pool_handler); |
3103 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
2966 | cv_coro_new = get_cv ("Coro::new", 0); SvREADONLY_on (cv_coro_new); |
3104 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
2967 | |
3105 | |
2968 | coro_stash = gv_stashpv ("Coro", TRUE); |
3106 | coro_stash = gv_stashpv ("Coro", TRUE); |
2969 | |
3107 | |
2970 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
3108 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
2971 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
3109 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
… | |
… | |
2979 | |
3117 | |
2980 | { |
3118 | { |
2981 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
3119 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
2982 | |
3120 | |
2983 | coroapi.schedule = api_schedule; |
3121 | coroapi.schedule = api_schedule; |
|
|
3122 | coroapi.schedule_to = api_schedule_to; |
2984 | coroapi.cede = api_cede; |
3123 | coroapi.cede = api_cede; |
2985 | coroapi.cede_notself = api_cede_notself; |
3124 | coroapi.cede_notself = api_cede_notself; |
2986 | coroapi.ready = api_ready; |
3125 | coroapi.ready = api_ready; |
2987 | coroapi.is_ready = api_is_ready; |
3126 | coroapi.is_ready = api_is_ready; |
2988 | coroapi.nready = coro_nready; |
3127 | coroapi.nready = coro_nready; |
… | |
… | |
2993 | SvREADONLY_on (sv); |
3132 | SvREADONLY_on (sv); |
2994 | } |
3133 | } |
2995 | } |
3134 | } |
2996 | |
3135 | |
2997 | void |
3136 | void |
|
|
3137 | terminate (...) |
|
|
3138 | CODE: |
|
|
3139 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
|
|
3140 | |
|
|
3141 | void |
2998 | schedule (...) |
3142 | schedule (...) |
2999 | CODE: |
3143 | CODE: |
3000 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
3144 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
|
|
3145 | |
|
|
3146 | void |
|
|
3147 | schedule_to (...) |
|
|
3148 | CODE: |
|
|
3149 | CORO_EXECUTE_SLF_XS (slf_init_schedule_to); |
|
|
3150 | |
|
|
3151 | void |
|
|
3152 | cede_to (...) |
|
|
3153 | CODE: |
|
|
3154 | CORO_EXECUTE_SLF_XS (slf_init_cede_to); |
3001 | |
3155 | |
3002 | void |
3156 | void |
3003 | cede (...) |
3157 | cede (...) |
3004 | CODE: |
3158 | CODE: |
3005 | CORO_EXECUTE_SLF_XS (slf_init_cede); |
3159 | CORO_EXECUTE_SLF_XS (slf_init_cede); |
3006 | |
3160 | |
3007 | void |
3161 | void |
3008 | cede_notself (...) |
3162 | cede_notself (...) |
3009 | CODE: |
3163 | CODE: |
3010 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
3164 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
|
|
3165 | |
|
|
3166 | void |
|
|
3167 | _cancel (Coro::State self) |
|
|
3168 | CODE: |
|
|
3169 | coro_state_destroy (aTHX_ self); |
|
|
3170 | coro_call_on_destroy (aTHX_ self); |
3011 | |
3171 | |
3012 | void |
3172 | void |
3013 | _set_current (SV *current) |
3173 | _set_current (SV *current) |
3014 | PROTOTYPE: $ |
3174 | PROTOTYPE: $ |
3015 | CODE: |
3175 | CODE: |
… | |
… | |
3086 | PUSHMARK (SP); |
3246 | PUSHMARK (SP); |
3087 | EXTEND (SP, 2); |
3247 | EXTEND (SP, 2); |
3088 | PUSHs (sv_Coro); |
3248 | PUSHs (sv_Coro); |
3089 | PUSHs ((SV *)cv_pool_handler); |
3249 | PUSHs ((SV *)cv_pool_handler); |
3090 | PUTBACK; |
3250 | PUTBACK; |
3091 | call_sv ((SV *)cv_coro_new, G_SCALAR); |
3251 | call_sv ((SV *)cv_coro_state_new, G_SCALAR); |
3092 | SPAGAIN; |
3252 | SPAGAIN; |
3093 | |
3253 | |
3094 | hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); |
3254 | hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); |
3095 | } |
3255 | } |
3096 | |
3256 | |