… | |
… | |
101 | # define CvISXSUB_on(cv) (void)cv |
101 | # define CvISXSUB_on(cv) (void)cv |
102 | #endif |
102 | #endif |
103 | #ifndef CvISXSUB |
103 | #ifndef CvISXSUB |
104 | # define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE) |
104 | # define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE) |
105 | #endif |
105 | #endif |
|
|
106 | #ifndef Newx |
|
|
107 | # define Newx(ptr,nitems,type) New (0,ptr,nitems,type) |
|
|
108 | #endif |
106 | |
109 | |
107 | /* 5.8.7 */ |
110 | /* 5.8.7 */ |
108 | #ifndef SvRV_set |
111 | #ifndef SvRV_set |
109 | # define SvRV_set(s,v) SvRV(s) = (v) |
112 | # define SvRV_set(s,v) SvRV(s) = (v) |
110 | #endif |
113 | #endif |
… | |
… | |
170 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
173 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
171 | static JMPENV *main_top_env; |
174 | static JMPENV *main_top_env; |
172 | static HV *coro_state_stash, *coro_stash; |
175 | static HV *coro_state_stash, *coro_stash; |
173 | 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 */ |
174 | |
177 | |
|
|
178 | static AV *av_destroy; /* destruction queue */ |
|
|
179 | static SV *sv_manager; /* the manager coro */ |
|
|
180 | |
175 | static GV *irsgv; /* $/ */ |
181 | static GV *irsgv; /* $/ */ |
176 | static GV *stdoutgv; /* *STDOUT */ |
182 | static GV *stdoutgv; /* *STDOUT */ |
177 | static SV *rv_diehook; |
183 | static SV *rv_diehook; |
178 | static SV *rv_warnhook; |
184 | static SV *rv_warnhook; |
179 | static HV *hv_sig; /* %SIG */ |
185 | static HV *hv_sig; /* %SIG */ |
180 | |
186 | |
181 | /* async_pool helper stuff */ |
187 | /* async_pool helper stuff */ |
182 | static SV *sv_pool_rss; |
188 | static SV *sv_pool_rss; |
183 | static SV *sv_pool_size; |
189 | static SV *sv_pool_size; |
184 | static SV *sv_async_pool_idle; |
190 | static SV *sv_async_pool_idle; /* description string */ |
185 | static AV *av_async_pool; |
191 | static AV *av_async_pool; /* idle pool */ |
186 | static SV *sv_Coro; |
192 | static SV *sv_Coro; /* class string */ |
187 | static CV *cv_pool_handler; |
193 | static CV *cv_pool_handler; |
188 | static CV *cv_coro_state_new; |
194 | static CV *cv_coro_state_new; |
189 | |
195 | |
190 | /* Coro::AnyEvent */ |
196 | /* Coro::AnyEvent */ |
191 | static SV *sv_activity; |
197 | static SV *sv_activity; |
… | |
… | |
222 | int valgrind_id; |
228 | int valgrind_id; |
223 | #endif |
229 | #endif |
224 | unsigned char flags; |
230 | unsigned char flags; |
225 | } coro_cctx; |
231 | } coro_cctx; |
226 | |
232 | |
|
|
233 | coro_cctx *cctx_current; /* the currently running cctx */ |
|
|
234 | |
|
|
235 | /*****************************************************************************/ |
|
|
236 | |
227 | enum { |
237 | enum { |
228 | CF_RUNNING = 0x0001, /* coroutine is running */ |
238 | CF_RUNNING = 0x0001, /* coroutine is running */ |
229 | CF_READY = 0x0002, /* coroutine is ready */ |
239 | CF_READY = 0x0002, /* coroutine is ready */ |
230 | CF_NEW = 0x0004, /* has never been switched to */ |
240 | CF_NEW = 0x0004, /* has never been switched to */ |
231 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
241 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
… | |
… | |
344 | GV *gvp; |
354 | GV *gvp; |
345 | return sv_2cv (sv, &st, &gvp, 0); |
355 | return sv_2cv (sv, &st, &gvp, 0); |
346 | } |
356 | } |
347 | |
357 | |
348 | static AV * |
358 | static AV * |
349 | coro_clone_padlist (pTHX_ CV *cv) |
359 | coro_derive_padlist (pTHX_ CV *cv) |
350 | { |
360 | { |
351 | AV *padlist = CvPADLIST (cv); |
361 | AV *padlist = CvPADLIST (cv); |
352 | AV *newpadlist, *newpad; |
362 | AV *newpadlist, *newpad; |
353 | |
363 | |
354 | newpadlist = newAV (); |
364 | newpadlist = newAV (); |
… | |
… | |
474 | CV *cp = Perl_cv_clone (aTHX_ cv); |
484 | CV *cp = Perl_cv_clone (aTHX_ cv); |
475 | CvPADLIST (cv) = CvPADLIST (cp); |
485 | CvPADLIST (cv) = CvPADLIST (cp); |
476 | CvPADLIST (cp) = 0; |
486 | CvPADLIST (cp) = 0; |
477 | SvREFCNT_dec (cp); |
487 | SvREFCNT_dec (cp); |
478 | #else |
488 | #else |
479 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
489 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
480 | #endif |
490 | #endif |
481 | } |
491 | } |
482 | } |
492 | } |
483 | |
493 | |
484 | static void |
494 | static void |
… | |
… | |
834 | slf_check_nop (pTHX_ struct CoroSLF *frame) |
844 | slf_check_nop (pTHX_ struct CoroSLF *frame) |
835 | { |
845 | { |
836 | return 0; |
846 | return 0; |
837 | } |
847 | } |
838 | |
848 | |
|
|
849 | static int |
|
|
850 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
|
|
851 | { |
|
|
852 | return 1; |
|
|
853 | } |
|
|
854 | |
839 | static UNOP coro_setup_op; |
855 | static UNOP coro_setup_op; |
840 | |
856 | |
841 | 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 */ |
842 | coro_setup (pTHX_ struct coro *coro) |
858 | coro_setup (pTHX_ struct coro *coro) |
843 | { |
859 | { |
… | |
… | |
892 | 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 */ |
893 | 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 */ |
894 | |
910 | |
895 | /* 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 */ |
896 | coro_setup_op.op_next = PL_op; |
912 | coro_setup_op.op_next = PL_op; |
897 | coro_setup_op.op_type = OP_CUSTOM; |
913 | coro_setup_op.op_type = OP_ENTERSUB; |
898 | coro_setup_op.op_ppaddr = pp_slf; |
914 | coro_setup_op.op_ppaddr = pp_slf; |
899 | /* 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 */ |
900 | |
916 | |
901 | PL_op = (OP *)&coro_setup_op; |
917 | PL_op = (OP *)&coro_setup_op; |
902 | |
918 | |
… | |
… | |
955 | static int |
971 | static int |
956 | runops_trace (pTHX) |
972 | runops_trace (pTHX) |
957 | { |
973 | { |
958 | COP *oldcop = 0; |
974 | COP *oldcop = 0; |
959 | int oldcxix = -2; |
975 | int oldcxix = -2; |
960 | struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */ |
|
|
961 | coro_cctx *cctx = coro->cctx; |
|
|
962 | |
976 | |
963 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
977 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
964 | { |
978 | { |
965 | PERL_ASYNC_CHECK (); |
979 | PERL_ASYNC_CHECK (); |
966 | |
980 | |
967 | if (cctx->flags & CC_TRACE_ALL) |
981 | if (cctx_current->flags & CC_TRACE_ALL) |
968 | { |
982 | { |
969 | if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) |
983 | if (PL_op->op_type == OP_LEAVESUB && cctx_current->flags & CC_TRACE_SUB) |
970 | { |
984 | { |
971 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
985 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
972 | SV **bot, **top; |
986 | SV **bot, **top; |
973 | AV *av = newAV (); /* return values */ |
987 | AV *av = newAV (); /* return values */ |
974 | SV **cb; |
988 | SV **cb; |
… | |
… | |
1011 | |
1025 | |
1012 | if (PL_curcop != &PL_compiling) |
1026 | if (PL_curcop != &PL_compiling) |
1013 | { |
1027 | { |
1014 | SV **cb; |
1028 | SV **cb; |
1015 | |
1029 | |
1016 | if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) |
1030 | if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB) |
1017 | { |
1031 | { |
1018 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1032 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1019 | |
1033 | |
1020 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1034 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1021 | { |
1035 | { |
1022 | runops_proc_t old_runops = PL_runops; |
|
|
1023 | dSP; |
1036 | dSP; |
1024 | GV *gv = CvGV (cx->blk_sub.cv); |
1037 | GV *gv = CvGV (cx->blk_sub.cv); |
1025 | SV *fullname = sv_2mortal (newSV (0)); |
1038 | SV *fullname = sv_2mortal (newSV (0)); |
1026 | |
1039 | |
1027 | if (isGV (gv)) |
1040 | if (isGV (gv)) |
… | |
… | |
1045 | } |
1058 | } |
1046 | |
1059 | |
1047 | oldcxix = cxstack_ix; |
1060 | oldcxix = cxstack_ix; |
1048 | } |
1061 | } |
1049 | |
1062 | |
1050 | if (cctx->flags & CC_TRACE_LINE) |
1063 | if (cctx_current->flags & CC_TRACE_LINE) |
1051 | { |
1064 | { |
1052 | dSP; |
1065 | dSP; |
1053 | |
1066 | |
1054 | PL_runops = RUNOPS_DEFAULT; |
1067 | PL_runops = RUNOPS_DEFAULT; |
1055 | ENTER; |
1068 | ENTER; |
… | |
… | |
1074 | |
1087 | |
1075 | TAINT_NOT; |
1088 | TAINT_NOT; |
1076 | return 0; |
1089 | return 0; |
1077 | } |
1090 | } |
1078 | |
1091 | |
1079 | static struct coro_cctx *cctx_ssl_cctx; |
|
|
1080 | static struct CoroSLF cctx_ssl_frame; |
1092 | static struct CoroSLF cctx_ssl_frame; |
1081 | |
1093 | |
1082 | static void |
1094 | static void |
1083 | slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) |
1095 | slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) |
1084 | { |
1096 | { |
1085 | ta->prev = (struct coro *)cctx_ssl_cctx; |
|
|
1086 | ta->next = 0; |
1097 | ta->prev = 0; |
1087 | } |
1098 | } |
1088 | |
1099 | |
1089 | static int |
1100 | static int |
1090 | slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) |
1101 | slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) |
1091 | { |
1102 | { |
… | |
… | |
1094 | return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ |
1105 | return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ |
1095 | } |
1106 | } |
1096 | |
1107 | |
1097 | /* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ |
1108 | /* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ |
1098 | static void NOINLINE |
1109 | static void NOINLINE |
1099 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1110 | cctx_prepare (pTHX) |
1100 | { |
1111 | { |
1101 | PL_top_env = &PL_start_env; |
1112 | PL_top_env = &PL_start_env; |
1102 | |
1113 | |
1103 | if (cctx->flags & CC_TRACE) |
1114 | if (cctx_current->flags & CC_TRACE) |
1104 | PL_runops = runops_trace; |
1115 | PL_runops = runops_trace; |
1105 | |
1116 | |
1106 | /* we already must be executing an SLF op, there is no other valid way |
1117 | /* we already must be executing an SLF op, there is no other valid way |
1107 | * that can lead to creation of a new cctx */ |
1118 | * that can lead to creation of a new cctx */ |
1108 | assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", |
1119 | assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", |
1109 | slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); |
1120 | slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); |
1110 | |
1121 | |
1111 | /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ |
1122 | /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ |
1112 | cctx_ssl_cctx = cctx; |
|
|
1113 | cctx_ssl_frame = slf_frame; |
1123 | cctx_ssl_frame = slf_frame; |
1114 | |
1124 | |
1115 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1125 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1116 | slf_frame.check = slf_check_set_stacklevel; |
1126 | slf_frame.check = slf_check_set_stacklevel; |
1117 | } |
1127 | } |
… | |
… | |
1140 | /* normally we would need to skip the entersub here */ |
1150 | /* normally we would need to skip the entersub here */ |
1141 | /* not doing so will re-execute it, which is exactly what we want */ |
1151 | /* not doing so will re-execute it, which is exactly what we want */ |
1142 | /* PL_nop = PL_nop->op_next */ |
1152 | /* PL_nop = PL_nop->op_next */ |
1143 | |
1153 | |
1144 | /* inject a fake subroutine call to cctx_init */ |
1154 | /* inject a fake subroutine call to cctx_init */ |
1145 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1155 | cctx_prepare (aTHX); |
1146 | |
1156 | |
1147 | /* cctx_run is the alternative tail of transfer() */ |
1157 | /* cctx_run is the alternative tail of transfer() */ |
1148 | transfer_tail (aTHX); |
1158 | transfer_tail (aTHX); |
1149 | |
1159 | |
1150 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1160 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
… | |
… | |
1245 | cctx_destroy (coro_cctx *cctx) |
1255 | cctx_destroy (coro_cctx *cctx) |
1246 | { |
1256 | { |
1247 | if (!cctx) |
1257 | if (!cctx) |
1248 | return; |
1258 | return; |
1249 | |
1259 | |
|
|
1260 | assert (cctx != cctx_current);//D temporary |
|
|
1261 | |
1250 | --cctx_count; |
1262 | --cctx_count; |
1251 | coro_destroy (&cctx->cctx); |
1263 | coro_destroy (&cctx->cctx); |
1252 | |
1264 | |
1253 | /* coro_transfer creates new, empty cctx's */ |
1265 | /* coro_transfer creates new, empty cctx's */ |
1254 | if (cctx->sptr) |
1266 | if (cctx->sptr) |
… | |
… | |
1339 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1351 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1340 | { |
1352 | { |
1341 | dSTACKLEVEL; |
1353 | dSTACKLEVEL; |
1342 | |
1354 | |
1343 | /* sometimes transfer is only called to set idle_sp */ |
1355 | /* sometimes transfer is only called to set idle_sp */ |
1344 | if (expect_false (!next)) |
1356 | if (expect_false (!prev)) |
1345 | { |
1357 | { |
1346 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1358 | cctx_current->idle_sp = STACKLEVEL; |
1347 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1359 | assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1348 | } |
1360 | } |
1349 | else if (expect_true (prev != next)) |
1361 | else if (expect_true (prev != next)) |
1350 | { |
1362 | { |
1351 | coro_cctx *prev__cctx; |
1363 | coro_cctx *cctx_prev; |
1352 | |
1364 | |
1353 | if (expect_false (prev->flags & CF_NEW)) |
1365 | if (expect_false (prev->flags & CF_NEW)) |
1354 | { |
1366 | { |
1355 | /* create a new empty/source context */ |
1367 | /* create a new empty/source context */ |
1356 | prev->cctx = cctx_new_empty (); |
|
|
1357 | prev->flags &= ~CF_NEW; |
1368 | prev->flags &= ~CF_NEW; |
1358 | prev->flags |= CF_RUNNING; |
1369 | prev->flags |= CF_RUNNING; |
1359 | } |
1370 | } |
1360 | |
1371 | |
1361 | prev->flags &= ~CF_RUNNING; |
1372 | prev->flags &= ~CF_RUNNING; |
… | |
… | |
1372 | coro_setup (aTHX_ next); |
1383 | coro_setup (aTHX_ next); |
1373 | } |
1384 | } |
1374 | else |
1385 | else |
1375 | load_perl (aTHX_ next); |
1386 | load_perl (aTHX_ next); |
1376 | |
1387 | |
1377 | prev__cctx = prev->cctx; |
|
|
1378 | |
|
|
1379 | /* possibly untie and reuse the cctx */ |
1388 | /* possibly untie and reuse the cctx */ |
1380 | if (expect_true ( |
1389 | if (expect_true ( |
1381 | prev__cctx->idle_sp == STACKLEVEL |
1390 | cctx_current->idle_sp == STACKLEVEL |
1382 | && !(prev__cctx->flags & CC_TRACE) |
1391 | && !(cctx_current->flags & CC_TRACE) |
1383 | && !force_cctx |
1392 | && !force_cctx |
1384 | )) |
1393 | )) |
1385 | { |
1394 | { |
1386 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1395 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1387 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); |
1396 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te)); |
1388 | |
|
|
1389 | prev->cctx = 0; |
|
|
1390 | |
1397 | |
1391 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1398 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1392 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
1399 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
1393 | if (expect_false (CCTX_EXPIRED (prev__cctx))) |
1400 | if (expect_false (CCTX_EXPIRED (cctx_current))) |
1394 | if (!next->cctx) |
1401 | if (!next->cctx) |
1395 | next->cctx = cctx_get (aTHX); |
1402 | next->cctx = cctx_get (aTHX); |
1396 | |
1403 | |
1397 | cctx_put (prev__cctx); |
1404 | cctx_put (cctx_current); |
|
|
1405 | assert (!prev->cctx);//D temporary |
1398 | } |
1406 | } |
|
|
1407 | else |
|
|
1408 | prev->cctx = cctx_current; |
1399 | |
1409 | |
1400 | ++next->usecount; |
1410 | ++next->usecount; |
1401 | |
1411 | |
1402 | if (expect_true (!next->cctx)) |
1412 | cctx_prev = cctx_current; |
1403 | next->cctx = cctx_get (aTHX); |
1413 | cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX); |
1404 | |
1414 | |
1405 | if (expect_false (prev__cctx != next->cctx)) |
1415 | next->cctx = 0; |
|
|
1416 | |
|
|
1417 | if (expect_false (cctx_prev != cctx_current)) |
1406 | { |
1418 | { |
1407 | prev__cctx->top_env = PL_top_env; |
1419 | cctx_prev->top_env = PL_top_env; |
1408 | PL_top_env = next->cctx->top_env; |
1420 | PL_top_env = cctx_current->top_env; |
1409 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1421 | coro_transfer (&cctx_prev->cctx, &cctx_current->cctx); |
1410 | } |
1422 | } |
1411 | |
1423 | |
1412 | transfer_tail (aTHX); |
1424 | transfer_tail (aTHX); |
1413 | } |
1425 | } |
1414 | } |
1426 | } |
… | |
… | |
1614 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1626 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1615 | } |
1627 | } |
1616 | |
1628 | |
1617 | /* expects to own a reference to next->hv */ |
1629 | /* expects to own a reference to next->hv */ |
1618 | INLINE void |
1630 | INLINE void |
1619 | prepare_cede_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
1631 | prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
1620 | { |
1632 | { |
1621 | SV *prev_sv = SvRV (coro_current); |
1633 | SV *prev_sv = SvRV (coro_current); |
1622 | |
1634 | |
1623 | ta->prev = SvSTATE_hv (prev_sv); |
1635 | ta->prev = SvSTATE_hv (prev_sv); |
1624 | ta->next = next; |
1636 | ta->next = next; |
1625 | |
1637 | |
1626 | TRANSFER_CHECK (*ta); |
1638 | TRANSFER_CHECK (*ta); |
1627 | |
1639 | |
1628 | SvRV_set (coro_current, next->hv); |
1640 | SvRV_set (coro_current, (SV *)next->hv); |
1629 | |
1641 | |
1630 | free_coro_mortal (aTHX); |
1642 | free_coro_mortal (aTHX); |
1631 | coro_mortal = prev_sv; |
1643 | coro_mortal = prev_sv; |
1632 | } |
1644 | } |
1633 | |
1645 | |
… | |
… | |
1636 | { |
1648 | { |
1637 | for (;;) |
1649 | for (;;) |
1638 | { |
1650 | { |
1639 | SV *next_sv = coro_deq (aTHX); |
1651 | SV *next_sv = coro_deq (aTHX); |
1640 | |
1652 | |
1641 | if (expect_false (!next_sv)) |
1653 | if (expect_true (next_sv)) |
1642 | { |
|
|
1643 | /* nothing to schedule: call the idle handler */ |
|
|
1644 | dSP; |
|
|
1645 | |
|
|
1646 | ENTER; |
|
|
1647 | SAVETMPS; |
|
|
1648 | |
|
|
1649 | PUSHMARK (SP); |
|
|
1650 | PUTBACK; |
|
|
1651 | call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); |
|
|
1652 | |
|
|
1653 | FREETMPS; |
|
|
1654 | LEAVE; |
|
|
1655 | } |
|
|
1656 | else |
|
|
1657 | { |
1654 | { |
1658 | struct coro *next = SvSTATE_hv (next_sv); |
1655 | struct coro *next = SvSTATE_hv (next_sv); |
1659 | |
1656 | |
1660 | /* cannot transfer to destroyed coros, skip and look for next */ |
1657 | /* cannot transfer to destroyed coros, skip and look for next */ |
1661 | if (expect_false (next->flags & CF_DESTROYED)) |
1658 | if (expect_false (next->flags & CF_DESTROYED)) |
… | |
… | |
1663 | else |
1660 | else |
1664 | { |
1661 | { |
1665 | next->flags &= ~CF_READY; |
1662 | next->flags &= ~CF_READY; |
1666 | --coro_nready; |
1663 | --coro_nready; |
1667 | |
1664 | |
1668 | return prepare_cede_to (aTHX_ ta, next); |
1665 | prepare_schedule_to (aTHX_ ta, next); |
|
|
1666 | break; |
1669 | } |
1667 | } |
1670 | } |
1668 | } |
|
|
1669 | else |
|
|
1670 | { |
|
|
1671 | /* nothing to schedule: call the idle handler */ |
|
|
1672 | dSP; |
|
|
1673 | |
|
|
1674 | ENTER; |
|
|
1675 | SAVETMPS; |
|
|
1676 | |
|
|
1677 | PUSHMARK (SP); |
|
|
1678 | PUTBACK; |
|
|
1679 | call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); |
|
|
1680 | |
|
|
1681 | FREETMPS; |
|
|
1682 | LEAVE; |
|
|
1683 | } |
1671 | } |
1684 | } |
1672 | } |
1685 | } |
1673 | |
1686 | |
1674 | INLINE void |
1687 | INLINE void |
1675 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1688 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
… | |
… | |
1697 | { |
1710 | { |
1698 | struct coro_transfer_args ta; |
1711 | struct coro_transfer_args ta; |
1699 | |
1712 | |
1700 | prepare_schedule (aTHX_ &ta); |
1713 | prepare_schedule (aTHX_ &ta); |
1701 | TRANSFER (ta, 1); |
1714 | TRANSFER (ta, 1); |
|
|
1715 | } |
|
|
1716 | |
|
|
1717 | static void |
|
|
1718 | api_schedule_to (pTHX_ SV *coro_sv) |
|
|
1719 | { |
|
|
1720 | struct coro_transfer_args ta; |
|
|
1721 | struct coro *next = SvSTATE (coro_sv); |
|
|
1722 | |
|
|
1723 | SvREFCNT_inc_NN (coro_sv); |
|
|
1724 | prepare_schedule_to (aTHX_ &ta, next); |
1702 | } |
1725 | } |
1703 | |
1726 | |
1704 | static int |
1727 | static int |
1705 | api_cede (pTHX) |
1728 | api_cede (pTHX) |
1706 | { |
1729 | { |
… | |
… | |
1753 | if (coro->flags & CF_RUNNING) |
1776 | if (coro->flags & CF_RUNNING) |
1754 | PL_runops = RUNOPS_DEFAULT; |
1777 | PL_runops = RUNOPS_DEFAULT; |
1755 | else |
1778 | else |
1756 | coro->slot->runops = RUNOPS_DEFAULT; |
1779 | coro->slot->runops = RUNOPS_DEFAULT; |
1757 | } |
1780 | } |
|
|
1781 | } |
|
|
1782 | |
|
|
1783 | static void |
|
|
1784 | coro_call_on_destroy (pTHX_ struct coro *coro) |
|
|
1785 | { |
|
|
1786 | SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0); |
|
|
1787 | SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0); |
|
|
1788 | |
|
|
1789 | if (on_destroyp) |
|
|
1790 | { |
|
|
1791 | AV *on_destroy = (AV *)SvRV (*on_destroyp); |
|
|
1792 | |
|
|
1793 | while (AvFILLp (on_destroy) >= 0) |
|
|
1794 | { |
|
|
1795 | dSP; /* don't disturb outer sp */ |
|
|
1796 | SV *cb = av_pop (on_destroy); |
|
|
1797 | |
|
|
1798 | PUSHMARK (SP); |
|
|
1799 | |
|
|
1800 | if (statusp) |
|
|
1801 | { |
|
|
1802 | int i; |
|
|
1803 | AV *status = (AV *)SvRV (*statusp); |
|
|
1804 | EXTEND (SP, AvFILLp (status) + 1); |
|
|
1805 | |
|
|
1806 | for (i = 0; i <= AvFILLp (status); ++i) |
|
|
1807 | PUSHs (AvARRAY (status)[i]); |
|
|
1808 | } |
|
|
1809 | |
|
|
1810 | PUTBACK; |
|
|
1811 | call_sv (sv_2mortal (cb), G_VOID | G_DISCARD); |
|
|
1812 | } |
|
|
1813 | } |
|
|
1814 | } |
|
|
1815 | |
|
|
1816 | static void |
|
|
1817 | slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1818 | { |
|
|
1819 | int i; |
|
|
1820 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1821 | AV *av = newAV (); |
|
|
1822 | |
|
|
1823 | av_extend (av, items - 1); |
|
|
1824 | for (i = 0; i < items; ++i) |
|
|
1825 | av_push (av, SvREFCNT_inc_NN (arg [i])); |
|
|
1826 | |
|
|
1827 | hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); |
|
|
1828 | |
|
|
1829 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
|
|
1830 | api_ready (aTHX_ sv_manager); |
|
|
1831 | |
|
|
1832 | frame->prepare = prepare_schedule; |
|
|
1833 | frame->check = slf_check_repeat; |
1758 | } |
1834 | } |
1759 | |
1835 | |
1760 | /*****************************************************************************/ |
1836 | /*****************************************************************************/ |
1761 | /* async pool handler */ |
1837 | /* async pool handler */ |
1762 | |
1838 | |
… | |
… | |
1995 | static void |
2071 | static void |
1996 | slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2072 | slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1997 | { |
2073 | { |
1998 | frame->prepare = prepare_schedule; |
2074 | frame->prepare = prepare_schedule; |
1999 | frame->check = slf_check_nop; |
2075 | frame->check = slf_check_nop; |
|
|
2076 | } |
|
|
2077 | |
|
|
2078 | static void |
|
|
2079 | slf_prepare_schedule_to (pTHX_ struct coro_transfer_args *ta) |
|
|
2080 | { |
|
|
2081 | struct coro *next = (struct coro *)slf_frame.data; |
|
|
2082 | |
|
|
2083 | SvREFCNT_inc_NN (next->hv); |
|
|
2084 | prepare_schedule_to (aTHX_ ta, next); |
|
|
2085 | } |
|
|
2086 | |
|
|
2087 | static void |
|
|
2088 | slf_init_schedule_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2089 | { |
|
|
2090 | if (!items) |
|
|
2091 | croak ("Coro::schedule_to expects a coroutine argument, caught"); |
|
|
2092 | |
|
|
2093 | frame->data = (void *)SvSTATE (arg [0]); |
|
|
2094 | frame->prepare = slf_prepare_schedule_to; |
|
|
2095 | frame->check = slf_check_nop; |
|
|
2096 | } |
|
|
2097 | |
|
|
2098 | static void |
|
|
2099 | slf_init_cede_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2100 | { |
|
|
2101 | api_ready (aTHX_ SvRV (coro_current)); |
|
|
2102 | |
|
|
2103 | slf_init_schedule_to (aTHX_ frame, cv, arg, items); |
2000 | } |
2104 | } |
2001 | |
2105 | |
2002 | static void |
2106 | static void |
2003 | slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2107 | slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2004 | { |
2108 | { |
… | |
… | |
2150 | } |
2254 | } |
2151 | else |
2255 | else |
2152 | slf_argc = 0; |
2256 | slf_argc = 0; |
2153 | |
2257 | |
2154 | PL_op->op_ppaddr = pp_slf; |
2258 | PL_op->op_ppaddr = pp_slf; |
2155 | PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ |
2259 | /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ |
2156 | |
2260 | |
2157 | PL_op = (OP *)&slf_restore; |
2261 | PL_op = (OP *)&slf_restore; |
2158 | } |
2262 | } |
2159 | |
2263 | |
2160 | /*****************************************************************************/ |
2264 | /*****************************************************************************/ |
… | |
… | |
2624 | XSRETURN_EMPTY; |
2728 | XSRETURN_EMPTY; |
2625 | } |
2729 | } |
2626 | |
2730 | |
2627 | /*****************************************************************************/ |
2731 | /*****************************************************************************/ |
2628 | |
2732 | |
|
|
2733 | #if CORO_CLONE |
|
|
2734 | # include "clone.c" |
|
|
2735 | #endif |
|
|
2736 | |
2629 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2737 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2630 | |
2738 | |
2631 | PROTOTYPES: DISABLE |
2739 | PROTOTYPES: DISABLE |
2632 | |
2740 | |
2633 | BOOT: |
2741 | BOOT: |
… | |
… | |
2637 | coro_thx = PERL_GET_CONTEXT; |
2745 | coro_thx = PERL_GET_CONTEXT; |
2638 | # endif |
2746 | # endif |
2639 | #endif |
2747 | #endif |
2640 | BOOT_PAGESIZE; |
2748 | BOOT_PAGESIZE; |
2641 | |
2749 | |
|
|
2750 | cctx_current = cctx_new_empty (); |
|
|
2751 | |
2642 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
2752 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
2643 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
2753 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
2644 | |
2754 | |
2645 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
2755 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
2646 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
2756 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
… | |
… | |
2665 | |
2775 | |
2666 | { |
2776 | { |
2667 | SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); |
2777 | SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); |
2668 | |
2778 | |
2669 | if (!PL_custom_op_names) PL_custom_op_names = newHV (); |
2779 | if (!PL_custom_op_names) PL_custom_op_names = newHV (); |
2670 | hv_store_ent (PL_custom_op_names, slf, |
2780 | hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0); |
2671 | newSVpv ("coro_slf", 0), 0); |
|
|
2672 | |
2781 | |
2673 | if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); |
2782 | if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); |
2674 | hv_store_ent (PL_custom_op_descs, slf, |
2783 | hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0); |
2675 | newSVpv ("coro schedule like function", 0), 0); |
|
|
2676 | } |
2784 | } |
2677 | |
2785 | |
2678 | coroapi.ver = CORO_API_VERSION; |
2786 | coroapi.ver = CORO_API_VERSION; |
2679 | coroapi.rev = CORO_API_REVISION; |
2787 | coroapi.rev = CORO_API_REVISION; |
2680 | |
2788 | |
… | |
… | |
2773 | void |
2881 | void |
2774 | _exit (int code) |
2882 | _exit (int code) |
2775 | PROTOTYPE: $ |
2883 | PROTOTYPE: $ |
2776 | CODE: |
2884 | CODE: |
2777 | _exit (code); |
2885 | _exit (code); |
|
|
2886 | |
|
|
2887 | SV * |
|
|
2888 | clone (Coro::State coro) |
|
|
2889 | CODE: |
|
|
2890 | { |
|
|
2891 | #if CORO_CLONE |
|
|
2892 | struct coro *ncoro = coro_clone (coro); |
|
|
2893 | MAGIC *mg; |
|
|
2894 | /* TODO: too much duplication */ |
|
|
2895 | ncoro->hv = newHV (); |
|
|
2896 | mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); |
|
|
2897 | mg->mg_flags |= MGf_DUP; |
|
|
2898 | RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv)); |
|
|
2899 | #else |
|
|
2900 | croak ("Coro::State->clone has not been configured into this installation of Coro, realised"); |
|
|
2901 | #endif |
|
|
2902 | } |
|
|
2903 | OUTPUT: |
|
|
2904 | RETVAL |
2778 | |
2905 | |
2779 | int |
2906 | int |
2780 | cctx_stacksize (int new_stacksize = 0) |
2907 | cctx_stacksize (int new_stacksize = 0) |
2781 | PROTOTYPE: ;$ |
2908 | PROTOTYPE: ;$ |
2782 | CODE: |
2909 | CODE: |
… | |
… | |
2903 | |
3030 | |
2904 | SV * |
3031 | SV * |
2905 | has_cctx (Coro::State coro) |
3032 | has_cctx (Coro::State coro) |
2906 | PROTOTYPE: $ |
3033 | PROTOTYPE: $ |
2907 | CODE: |
3034 | CODE: |
2908 | RETVAL = boolSV (!!coro->cctx); |
3035 | /* maybe manage the running flag differently */ |
|
|
3036 | RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING)); |
2909 | OUTPUT: |
3037 | OUTPUT: |
2910 | RETVAL |
3038 | RETVAL |
2911 | |
3039 | |
2912 | int |
3040 | int |
2913 | is_traced (Coro::State coro) |
3041 | is_traced (Coro::State coro) |
… | |
… | |
2933 | |
3061 | |
2934 | void |
3062 | void |
2935 | force_cctx () |
3063 | force_cctx () |
2936 | PROTOTYPE: |
3064 | PROTOTYPE: |
2937 | CODE: |
3065 | CODE: |
2938 | SvSTATE_current->cctx->idle_sp = 0; |
3066 | cctx_current->idle_sp = 0; |
2939 | |
3067 | |
2940 | void |
3068 | void |
2941 | swap_defsv (Coro::State self) |
3069 | swap_defsv (Coro::State self) |
2942 | PROTOTYPE: $ |
3070 | PROTOTYPE: $ |
2943 | ALIAS: |
3071 | ALIAS: |
… | |
… | |
2958 | |
3086 | |
2959 | BOOT: |
3087 | BOOT: |
2960 | { |
3088 | { |
2961 | int i; |
3089 | int i; |
2962 | |
3090 | |
2963 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
|
|
2964 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3091 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
2965 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3092 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
2966 | cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); |
3093 | cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); |
2967 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
3094 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
2968 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
3095 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
|
|
3096 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
|
|
3097 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
|
|
3098 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
2969 | |
3099 | |
2970 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3100 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
2971 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3101 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
2972 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3102 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
2973 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
3103 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
… | |
… | |
2986 | |
3116 | |
2987 | { |
3117 | { |
2988 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
3118 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
2989 | |
3119 | |
2990 | coroapi.schedule = api_schedule; |
3120 | coroapi.schedule = api_schedule; |
|
|
3121 | coroapi.schedule_to = api_schedule_to; |
2991 | coroapi.cede = api_cede; |
3122 | coroapi.cede = api_cede; |
2992 | coroapi.cede_notself = api_cede_notself; |
3123 | coroapi.cede_notself = api_cede_notself; |
2993 | coroapi.ready = api_ready; |
3124 | coroapi.ready = api_ready; |
2994 | coroapi.is_ready = api_is_ready; |
3125 | coroapi.is_ready = api_is_ready; |
2995 | coroapi.nready = coro_nready; |
3126 | coroapi.nready = coro_nready; |
… | |
… | |
3000 | SvREADONLY_on (sv); |
3131 | SvREADONLY_on (sv); |
3001 | } |
3132 | } |
3002 | } |
3133 | } |
3003 | |
3134 | |
3004 | void |
3135 | void |
|
|
3136 | terminate (...) |
|
|
3137 | CODE: |
|
|
3138 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
|
|
3139 | |
|
|
3140 | void |
3005 | schedule (...) |
3141 | schedule (...) |
3006 | CODE: |
3142 | CODE: |
3007 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
3143 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
|
|
3144 | |
|
|
3145 | void |
|
|
3146 | schedule_to (...) |
|
|
3147 | CODE: |
|
|
3148 | CORO_EXECUTE_SLF_XS (slf_init_schedule_to); |
|
|
3149 | |
|
|
3150 | void |
|
|
3151 | cede_to (...) |
|
|
3152 | CODE: |
|
|
3153 | CORO_EXECUTE_SLF_XS (slf_init_cede_to); |
3008 | |
3154 | |
3009 | void |
3155 | void |
3010 | cede (...) |
3156 | cede (...) |
3011 | CODE: |
3157 | CODE: |
3012 | CORO_EXECUTE_SLF_XS (slf_init_cede); |
3158 | CORO_EXECUTE_SLF_XS (slf_init_cede); |
3013 | |
3159 | |
3014 | void |
3160 | void |
3015 | cede_notself (...) |
3161 | cede_notself (...) |
3016 | CODE: |
3162 | CODE: |
3017 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
3163 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
|
|
3164 | |
|
|
3165 | void |
|
|
3166 | _cancel (Coro::State self) |
|
|
3167 | CODE: |
|
|
3168 | coro_state_destroy (aTHX_ self); |
|
|
3169 | coro_call_on_destroy (aTHX_ self); |
3018 | |
3170 | |
3019 | void |
3171 | void |
3020 | _set_current (SV *current) |
3172 | _set_current (SV *current) |
3021 | PROTOTYPE: $ |
3173 | PROTOTYPE: $ |
3022 | CODE: |
3174 | CODE: |