1 | #define CORO_CLONE 1 //D |
|
|
2 | |
|
|
3 | #include "libcoro/coro.c" |
1 | #include "libcoro/coro.c" |
4 | |
2 | |
5 | #define PERL_NO_GET_CONTEXT |
3 | #define PERL_NO_GET_CONTEXT |
6 | #define PERL_EXT |
4 | #define PERL_EXT |
7 | |
5 | |
… | |
… | |
230 | int valgrind_id; |
228 | int valgrind_id; |
231 | #endif |
229 | #endif |
232 | unsigned char flags; |
230 | unsigned char flags; |
233 | } coro_cctx; |
231 | } coro_cctx; |
234 | |
232 | |
|
|
233 | coro_cctx *cctx_current; /* the currently running cctx */ |
|
|
234 | |
|
|
235 | /*****************************************************************************/ |
|
|
236 | |
235 | enum { |
237 | enum { |
236 | CF_RUNNING = 0x0001, /* coroutine is running */ |
238 | CF_RUNNING = 0x0001, /* coroutine is running */ |
237 | CF_READY = 0x0002, /* coroutine is ready */ |
239 | CF_READY = 0x0002, /* coroutine is ready */ |
238 | CF_NEW = 0x0004, /* has never been switched to */ |
240 | CF_NEW = 0x0004, /* has never been switched to */ |
239 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
241 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
… | |
… | |
969 | static int |
971 | static int |
970 | runops_trace (pTHX) |
972 | runops_trace (pTHX) |
971 | { |
973 | { |
972 | COP *oldcop = 0; |
974 | COP *oldcop = 0; |
973 | int oldcxix = -2; |
975 | int oldcxix = -2; |
974 | struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */ |
|
|
975 | coro_cctx *cctx = coro->cctx; |
|
|
976 | |
976 | |
977 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
977 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
978 | { |
978 | { |
979 | PERL_ASYNC_CHECK (); |
979 | PERL_ASYNC_CHECK (); |
980 | |
980 | |
981 | if (cctx->flags & CC_TRACE_ALL) |
981 | if (cctx_current->flags & CC_TRACE_ALL) |
982 | { |
982 | { |
983 | 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) |
984 | { |
984 | { |
985 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
985 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
986 | SV **bot, **top; |
986 | SV **bot, **top; |
987 | AV *av = newAV (); /* return values */ |
987 | AV *av = newAV (); /* return values */ |
988 | SV **cb; |
988 | SV **cb; |
… | |
… | |
1025 | |
1025 | |
1026 | if (PL_curcop != &PL_compiling) |
1026 | if (PL_curcop != &PL_compiling) |
1027 | { |
1027 | { |
1028 | SV **cb; |
1028 | SV **cb; |
1029 | |
1029 | |
1030 | if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) |
1030 | if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB) |
1031 | { |
1031 | { |
1032 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1032 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1033 | |
1033 | |
1034 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1034 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1035 | { |
1035 | { |
… | |
… | |
1058 | } |
1058 | } |
1059 | |
1059 | |
1060 | oldcxix = cxstack_ix; |
1060 | oldcxix = cxstack_ix; |
1061 | } |
1061 | } |
1062 | |
1062 | |
1063 | if (cctx->flags & CC_TRACE_LINE) |
1063 | if (cctx_current->flags & CC_TRACE_LINE) |
1064 | { |
1064 | { |
1065 | dSP; |
1065 | dSP; |
1066 | |
1066 | |
1067 | PL_runops = RUNOPS_DEFAULT; |
1067 | PL_runops = RUNOPS_DEFAULT; |
1068 | ENTER; |
1068 | ENTER; |
… | |
… | |
1087 | |
1087 | |
1088 | TAINT_NOT; |
1088 | TAINT_NOT; |
1089 | return 0; |
1089 | return 0; |
1090 | } |
1090 | } |
1091 | |
1091 | |
1092 | static struct coro_cctx *cctx_ssl_cctx; |
|
|
1093 | static struct CoroSLF cctx_ssl_frame; |
1092 | static struct CoroSLF cctx_ssl_frame; |
1094 | |
1093 | |
1095 | static void |
1094 | static void |
1096 | slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) |
1095 | slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) |
1097 | { |
1096 | { |
1098 | ta->prev = (struct coro *)cctx_ssl_cctx; |
|
|
1099 | ta->next = 0; |
1097 | ta->prev = 0; |
1100 | } |
1098 | } |
1101 | |
1099 | |
1102 | static int |
1100 | static int |
1103 | slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) |
1101 | slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) |
1104 | { |
1102 | { |
… | |
… | |
1107 | 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 */ |
1108 | } |
1106 | } |
1109 | |
1107 | |
1110 | /* 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 */ |
1111 | static void NOINLINE |
1109 | static void NOINLINE |
1112 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1110 | cctx_prepare (pTHX) |
1113 | { |
1111 | { |
1114 | PL_top_env = &PL_start_env; |
1112 | PL_top_env = &PL_start_env; |
1115 | |
1113 | |
1116 | if (cctx->flags & CC_TRACE) |
1114 | if (cctx_current->flags & CC_TRACE) |
1117 | PL_runops = runops_trace; |
1115 | PL_runops = runops_trace; |
1118 | |
1116 | |
1119 | /* 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 |
1120 | * that can lead to creation of a new cctx */ |
1118 | * that can lead to creation of a new cctx */ |
1121 | 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)", |
1122 | slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); |
1120 | slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); |
1123 | |
1121 | |
1124 | /* 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 */ |
1125 | cctx_ssl_cctx = cctx; |
|
|
1126 | cctx_ssl_frame = slf_frame; |
1123 | cctx_ssl_frame = slf_frame; |
1127 | |
1124 | |
1128 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1125 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1129 | slf_frame.check = slf_check_set_stacklevel; |
1126 | slf_frame.check = slf_check_set_stacklevel; |
1130 | } |
1127 | } |
… | |
… | |
1153 | /* normally we would need to skip the entersub here */ |
1150 | /* normally we would need to skip the entersub here */ |
1154 | /* 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 */ |
1155 | /* PL_nop = PL_nop->op_next */ |
1152 | /* PL_nop = PL_nop->op_next */ |
1156 | |
1153 | |
1157 | /* inject a fake subroutine call to cctx_init */ |
1154 | /* inject a fake subroutine call to cctx_init */ |
1158 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1155 | cctx_prepare (aTHX); |
1159 | |
1156 | |
1160 | /* cctx_run is the alternative tail of transfer() */ |
1157 | /* cctx_run is the alternative tail of transfer() */ |
1161 | transfer_tail (aTHX); |
1158 | transfer_tail (aTHX); |
1162 | |
1159 | |
1163 | /* 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 */ |
… | |
… | |
1258 | cctx_destroy (coro_cctx *cctx) |
1255 | cctx_destroy (coro_cctx *cctx) |
1259 | { |
1256 | { |
1260 | if (!cctx) |
1257 | if (!cctx) |
1261 | return; |
1258 | return; |
1262 | |
1259 | |
|
|
1260 | assert (cctx != cctx_current);//D temporary |
|
|
1261 | |
1263 | --cctx_count; |
1262 | --cctx_count; |
1264 | coro_destroy (&cctx->cctx); |
1263 | coro_destroy (&cctx->cctx); |
1265 | |
1264 | |
1266 | /* coro_transfer creates new, empty cctx's */ |
1265 | /* coro_transfer creates new, empty cctx's */ |
1267 | if (cctx->sptr) |
1266 | if (cctx->sptr) |
… | |
… | |
1352 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1351 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1353 | { |
1352 | { |
1354 | dSTACKLEVEL; |
1353 | dSTACKLEVEL; |
1355 | |
1354 | |
1356 | /* sometimes transfer is only called to set idle_sp */ |
1355 | /* sometimes transfer is only called to set idle_sp */ |
1357 | if (expect_false (!next)) |
1356 | if (expect_false (!prev)) |
1358 | { |
1357 | { |
1359 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1358 | cctx_current->idle_sp = STACKLEVEL; |
1360 | 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 */ |
1361 | } |
1360 | } |
1362 | else if (expect_true (prev != next)) |
1361 | else if (expect_true (prev != next)) |
1363 | { |
1362 | { |
1364 | coro_cctx *prev__cctx; |
1363 | coro_cctx *cctx_prev; |
1365 | |
1364 | |
1366 | if (expect_false (prev->flags & CF_NEW)) |
1365 | if (expect_false (prev->flags & CF_NEW)) |
1367 | { |
1366 | { |
1368 | /* create a new empty/source context */ |
1367 | /* create a new empty/source context */ |
1369 | prev->cctx = cctx_new_empty (); |
|
|
1370 | prev->flags &= ~CF_NEW; |
1368 | prev->flags &= ~CF_NEW; |
1371 | prev->flags |= CF_RUNNING; |
1369 | prev->flags |= CF_RUNNING; |
1372 | } |
1370 | } |
1373 | |
1371 | |
1374 | prev->flags &= ~CF_RUNNING; |
1372 | prev->flags &= ~CF_RUNNING; |
… | |
… | |
1385 | coro_setup (aTHX_ next); |
1383 | coro_setup (aTHX_ next); |
1386 | } |
1384 | } |
1387 | else |
1385 | else |
1388 | load_perl (aTHX_ next); |
1386 | load_perl (aTHX_ next); |
1389 | |
1387 | |
1390 | prev__cctx = prev->cctx; |
|
|
1391 | |
|
|
1392 | /* possibly untie and reuse the cctx */ |
1388 | /* possibly untie and reuse the cctx */ |
1393 | if (expect_true ( |
1389 | if (expect_true ( |
1394 | prev__cctx->idle_sp == STACKLEVEL |
1390 | cctx_current->idle_sp == STACKLEVEL |
1395 | && !(prev__cctx->flags & CC_TRACE) |
1391 | && !(cctx_current->flags & CC_TRACE) |
1396 | && !force_cctx |
1392 | && !force_cctx |
1397 | )) |
1393 | )) |
1398 | { |
1394 | { |
1399 | /* 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 */ |
1400 | 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)); |
1401 | |
|
|
1402 | prev->cctx = 0; |
|
|
1403 | |
1397 | |
1404 | /* 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 */ |
1405 | /* 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 */ |
1406 | if (expect_false (CCTX_EXPIRED (prev__cctx))) |
1400 | if (expect_false (CCTX_EXPIRED (cctx_current))) |
1407 | if (!next->cctx) |
1401 | if (!next->cctx) |
1408 | next->cctx = cctx_get (aTHX); |
1402 | next->cctx = cctx_get (aTHX); |
1409 | |
1403 | |
1410 | cctx_put (prev__cctx); |
1404 | cctx_put (cctx_current); |
|
|
1405 | assert (!prev->cctx);//D temporary |
1411 | } |
1406 | } |
|
|
1407 | else |
|
|
1408 | prev->cctx = cctx_current; |
1412 | |
1409 | |
1413 | ++next->usecount; |
1410 | ++next->usecount; |
1414 | |
1411 | |
1415 | if (expect_true (!next->cctx)) |
1412 | cctx_prev = cctx_current; |
1416 | next->cctx = cctx_get (aTHX); |
1413 | cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX); |
1417 | |
1414 | |
1418 | if (expect_false (prev__cctx != next->cctx)) |
1415 | next->cctx = 0; |
|
|
1416 | |
|
|
1417 | if (expect_false (cctx_prev != cctx_current)) |
1419 | { |
1418 | { |
1420 | prev__cctx->top_env = PL_top_env; |
1419 | cctx_prev->top_env = PL_top_env; |
1421 | PL_top_env = next->cctx->top_env; |
1420 | PL_top_env = cctx_current->top_env; |
1422 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1421 | coro_transfer (&cctx_prev->cctx, &cctx_current->cctx); |
1423 | } |
1422 | } |
1424 | |
1423 | |
1425 | transfer_tail (aTHX); |
1424 | transfer_tail (aTHX); |
1426 | } |
1425 | } |
1427 | } |
1426 | } |
… | |
… | |
2746 | coro_thx = PERL_GET_CONTEXT; |
2745 | coro_thx = PERL_GET_CONTEXT; |
2747 | # endif |
2746 | # endif |
2748 | #endif |
2747 | #endif |
2749 | BOOT_PAGESIZE; |
2748 | BOOT_PAGESIZE; |
2750 | |
2749 | |
|
|
2750 | cctx_current = cctx_new_empty (); |
|
|
2751 | |
2751 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
2752 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
2752 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
2753 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
2753 | |
2754 | |
2754 | 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; |
2755 | 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; |
… | |
… | |
2881 | _exit (int code) |
2882 | _exit (int code) |
2882 | PROTOTYPE: $ |
2883 | PROTOTYPE: $ |
2883 | CODE: |
2884 | CODE: |
2884 | _exit (code); |
2885 | _exit (code); |
2885 | |
2886 | |
2886 | #if CORO_CLONE |
|
|
2887 | |
|
|
2888 | SV * |
2887 | SV * |
2889 | clone (Coro::State coro) |
2888 | clone (Coro::State coro) |
2890 | CODE: |
2889 | CODE: |
2891 | { |
2890 | { |
|
|
2891 | #if CORO_CLONE |
2892 | struct coro *ncoro = coro_clone (coro); |
2892 | struct coro *ncoro = coro_clone (coro); |
2893 | MAGIC *mg; |
2893 | MAGIC *mg; |
2894 | /* TODO: too much duplication */ |
2894 | /* TODO: too much duplication */ |
2895 | ncoro->hv = newHV (); |
2895 | ncoro->hv = newHV (); |
2896 | mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); |
2896 | mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); |
2897 | mg->mg_flags |= MGf_DUP; |
2897 | mg->mg_flags |= MGf_DUP; |
2898 | RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv)); |
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 |
2899 | } |
2902 | } |
2900 | OUTPUT: |
2903 | OUTPUT: |
2901 | RETVAL |
2904 | RETVAL |
2902 | |
|
|
2903 | #endif |
|
|
2904 | |
2905 | |
2905 | int |
2906 | int |
2906 | cctx_stacksize (int new_stacksize = 0) |
2907 | cctx_stacksize (int new_stacksize = 0) |
2907 | PROTOTYPE: ;$ |
2908 | PROTOTYPE: ;$ |
2908 | CODE: |
2909 | CODE: |
… | |
… | |
3029 | |
3030 | |
3030 | SV * |
3031 | SV * |
3031 | has_cctx (Coro::State coro) |
3032 | has_cctx (Coro::State coro) |
3032 | PROTOTYPE: $ |
3033 | PROTOTYPE: $ |
3033 | CODE: |
3034 | CODE: |
3034 | RETVAL = boolSV (!!coro->cctx); |
3035 | /* maybe manage the running flag differently */ |
|
|
3036 | RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING)); |
3035 | OUTPUT: |
3037 | OUTPUT: |
3036 | RETVAL |
3038 | RETVAL |
3037 | |
3039 | |
3038 | int |
3040 | int |
3039 | is_traced (Coro::State coro) |
3041 | is_traced (Coro::State coro) |
… | |
… | |
3059 | |
3061 | |
3060 | void |
3062 | void |
3061 | force_cctx () |
3063 | force_cctx () |
3062 | PROTOTYPE: |
3064 | PROTOTYPE: |
3063 | CODE: |
3065 | CODE: |
3064 | SvSTATE_current->cctx->idle_sp = 0; |
3066 | cctx_current->idle_sp = 0; |
3065 | |
3067 | |
3066 | void |
3068 | void |
3067 | swap_defsv (Coro::State self) |
3069 | swap_defsv (Coro::State self) |
3068 | PROTOTYPE: $ |
3070 | PROTOTYPE: $ |
3069 | ALIAS: |
3071 | ALIAS: |