… | |
… | |
175 | static HV *coro_state_stash, *coro_stash; |
175 | static HV *coro_state_stash, *coro_stash; |
176 | 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 */ |
177 | |
177 | |
178 | static AV *av_destroy; /* destruction queue */ |
178 | static AV *av_destroy; /* destruction queue */ |
179 | static SV *sv_manager; /* the manager coro */ |
179 | static SV *sv_manager; /* the manager coro */ |
|
|
180 | static SV *sv_idle; /* $Coro::idle */ |
180 | |
181 | |
181 | static GV *irsgv; /* $/ */ |
182 | static GV *irsgv; /* $/ */ |
182 | static GV *stdoutgv; /* *STDOUT */ |
183 | static GV *stdoutgv; /* *STDOUT */ |
183 | static SV *rv_diehook; |
184 | static SV *rv_diehook; |
184 | static SV *rv_warnhook; |
185 | static SV *rv_warnhook; |
… | |
… | |
246 | { |
247 | { |
247 | SV *defsv; |
248 | SV *defsv; |
248 | AV *defav; |
249 | AV *defav; |
249 | SV *errsv; |
250 | SV *errsv; |
250 | SV *irsgv; |
251 | SV *irsgv; |
|
|
252 | HV *hinthv; |
251 | #define VAR(name,type) type name; |
253 | #define VAR(name,type) type name; |
252 | # include "state.h" |
254 | # include "state.h" |
253 | #undef VAR |
255 | #undef VAR |
254 | } perl_slots; |
256 | } perl_slots; |
255 | |
257 | |
… | |
… | |
516 | perl_slots *slot = c->slot; |
518 | perl_slots *slot = c->slot; |
517 | c->slot = 0; |
519 | c->slot = 0; |
518 | |
520 | |
519 | PL_mainstack = c->mainstack; |
521 | PL_mainstack = c->mainstack; |
520 | |
522 | |
521 | GvSV (PL_defgv) = slot->defsv; |
523 | GvSV (PL_defgv) = slot->defsv; |
522 | GvAV (PL_defgv) = slot->defav; |
524 | GvAV (PL_defgv) = slot->defav; |
523 | GvSV (PL_errgv) = slot->errsv; |
525 | GvSV (PL_errgv) = slot->errsv; |
524 | GvSV (irsgv) = slot->irsgv; |
526 | GvSV (irsgv) = slot->irsgv; |
|
|
527 | GvHV (PL_hintgv) = slot->hinthv; |
525 | |
528 | |
526 | #define VAR(name,type) PL_ ## name = slot->name; |
529 | #define VAR(name,type) PL_ ## name = slot->name; |
527 | # include "state.h" |
530 | # include "state.h" |
528 | #undef VAR |
531 | #undef VAR |
529 | |
532 | |
… | |
… | |
615 | c->mainstack = PL_mainstack; |
618 | c->mainstack = PL_mainstack; |
616 | |
619 | |
617 | { |
620 | { |
618 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
621 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
619 | |
622 | |
620 | slot->defav = GvAV (PL_defgv); |
623 | slot->defav = GvAV (PL_defgv); |
621 | slot->defsv = DEFSV; |
624 | slot->defsv = DEFSV; |
622 | slot->errsv = ERRSV; |
625 | slot->errsv = ERRSV; |
623 | slot->irsgv = GvSV (irsgv); |
626 | slot->irsgv = GvSV (irsgv); |
|
|
627 | slot->hinthv = GvHV (PL_hintgv); |
624 | |
628 | |
625 | #define VAR(name,type) slot->name = PL_ ## name; |
629 | #define VAR(name,type) slot->name = PL_ ## name; |
626 | # include "state.h" |
630 | # include "state.h" |
627 | #undef VAR |
631 | #undef VAR |
628 | } |
632 | } |
… | |
… | |
864 | |
868 | |
865 | PL_runops = RUNOPS_DEFAULT; |
869 | PL_runops = RUNOPS_DEFAULT; |
866 | PL_curcop = &PL_compiling; |
870 | PL_curcop = &PL_compiling; |
867 | PL_in_eval = EVAL_NULL; |
871 | PL_in_eval = EVAL_NULL; |
868 | PL_comppad = 0; |
872 | PL_comppad = 0; |
|
|
873 | PL_comppad_name = 0; |
|
|
874 | PL_comppad_name_fill = 0; |
|
|
875 | PL_comppad_name_floor = 0; |
869 | PL_curpm = 0; |
876 | PL_curpm = 0; |
870 | PL_curpad = 0; |
877 | PL_curpad = 0; |
871 | PL_localizing = 0; |
878 | PL_localizing = 0; |
872 | PL_dirty = 0; |
879 | PL_dirty = 0; |
873 | PL_restartop = 0; |
880 | PL_restartop = 0; |
874 | #if PERL_VERSION_ATLEAST (5,10,0) |
881 | #if PERL_VERSION_ATLEAST (5,10,0) |
875 | PL_parser = 0; |
882 | PL_parser = 0; |
876 | #endif |
883 | #endif |
|
|
884 | PL_hints = 0; |
877 | |
885 | |
878 | /* recreate the die/warn hooks */ |
886 | /* recreate the die/warn hooks */ |
879 | PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); |
887 | PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); |
880 | PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); |
888 | PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); |
881 | |
889 | |
882 | GvSV (PL_defgv) = newSV (0); |
890 | GvSV (PL_defgv) = newSV (0); |
883 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
891 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
884 | GvSV (PL_errgv) = newSV (0); |
892 | GvSV (PL_errgv) = newSV (0); |
885 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
893 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
|
|
894 | GvHV (PL_hintgv) = 0; |
886 | PL_rs = newSVsv (GvSV (irsgv)); |
895 | PL_rs = newSVsv (GvSV (irsgv)); |
887 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
896 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
888 | |
897 | |
889 | { |
898 | { |
890 | dSP; |
899 | dSP; |
… | |
… | |
944 | SvREFCNT_dec (GvAV (PL_defgv)); |
953 | SvREFCNT_dec (GvAV (PL_defgv)); |
945 | SvREFCNT_dec (GvSV (PL_errgv)); |
954 | SvREFCNT_dec (GvSV (PL_errgv)); |
946 | SvREFCNT_dec (PL_defoutgv); |
955 | SvREFCNT_dec (PL_defoutgv); |
947 | SvREFCNT_dec (PL_rs); |
956 | SvREFCNT_dec (PL_rs); |
948 | SvREFCNT_dec (GvSV (irsgv)); |
957 | SvREFCNT_dec (GvSV (irsgv)); |
|
|
958 | SvREFCNT_dec (GvHV (PL_hintgv)); |
949 | |
959 | |
950 | SvREFCNT_dec (PL_diehook); |
960 | SvREFCNT_dec (PL_diehook); |
951 | SvREFCNT_dec (PL_warnhook); |
961 | SvREFCNT_dec (PL_warnhook); |
952 | |
962 | |
953 | SvREFCNT_dec (coro->saved_deffh); |
963 | SvREFCNT_dec (coro->saved_deffh); |
… | |
… | |
971 | static int |
981 | static int |
972 | runops_trace (pTHX) |
982 | runops_trace (pTHX) |
973 | { |
983 | { |
974 | COP *oldcop = 0; |
984 | COP *oldcop = 0; |
975 | int oldcxix = -2; |
985 | int oldcxix = -2; |
976 | struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */ |
|
|
977 | coro_cctx *cctx = coro->cctx; |
|
|
978 | |
986 | |
979 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
987 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
980 | { |
988 | { |
981 | PERL_ASYNC_CHECK (); |
989 | PERL_ASYNC_CHECK (); |
982 | |
990 | |
983 | if (cctx->flags & CC_TRACE_ALL) |
991 | if (cctx_current->flags & CC_TRACE_ALL) |
984 | { |
992 | { |
985 | if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) |
993 | if (PL_op->op_type == OP_LEAVESUB && cctx_current->flags & CC_TRACE_SUB) |
986 | { |
994 | { |
987 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
995 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
988 | SV **bot, **top; |
996 | SV **bot, **top; |
989 | AV *av = newAV (); /* return values */ |
997 | AV *av = newAV (); /* return values */ |
990 | SV **cb; |
998 | SV **cb; |
… | |
… | |
1027 | |
1035 | |
1028 | if (PL_curcop != &PL_compiling) |
1036 | if (PL_curcop != &PL_compiling) |
1029 | { |
1037 | { |
1030 | SV **cb; |
1038 | SV **cb; |
1031 | |
1039 | |
1032 | if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) |
1040 | if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB) |
1033 | { |
1041 | { |
1034 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1042 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1035 | |
1043 | |
1036 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1044 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1037 | { |
1045 | { |
… | |
… | |
1060 | } |
1068 | } |
1061 | |
1069 | |
1062 | oldcxix = cxstack_ix; |
1070 | oldcxix = cxstack_ix; |
1063 | } |
1071 | } |
1064 | |
1072 | |
1065 | if (cctx->flags & CC_TRACE_LINE) |
1073 | if (cctx_current->flags & CC_TRACE_LINE) |
1066 | { |
1074 | { |
1067 | dSP; |
1075 | dSP; |
1068 | |
1076 | |
1069 | PL_runops = RUNOPS_DEFAULT; |
1077 | PL_runops = RUNOPS_DEFAULT; |
1070 | ENTER; |
1078 | ENTER; |
… | |
… | |
1331 | /* TODO: throwing up here is considered harmful */ |
1339 | /* TODO: throwing up here is considered harmful */ |
1332 | |
1340 | |
1333 | if (expect_true (prev != next)) |
1341 | if (expect_true (prev != next)) |
1334 | { |
1342 | { |
1335 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1343 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1336 | croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,"); |
1344 | croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,"); |
1337 | |
1345 | |
1338 | if (expect_false (next->flags & CF_RUNNING)) |
1346 | if (expect_false (next->flags & CF_RUNNING)) |
1339 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); |
1347 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); |
1340 | |
1348 | |
1341 | if (expect_false (next->flags & CF_DESTROYED)) |
1349 | if (expect_false (next->flags & CF_DESTROYED)) |
… | |
… | |
1385 | coro_setup (aTHX_ next); |
1393 | coro_setup (aTHX_ next); |
1386 | } |
1394 | } |
1387 | else |
1395 | else |
1388 | load_perl (aTHX_ next); |
1396 | load_perl (aTHX_ next); |
1389 | |
1397 | |
|
|
1398 | assert (!prev->cctx);//D temporary |
|
|
1399 | |
1390 | /* possibly untie and reuse the cctx */ |
1400 | /* possibly untie and reuse the cctx */ |
1391 | if (expect_true ( |
1401 | if (expect_true ( |
1392 | cctx_current->idle_sp == STACKLEVEL |
1402 | cctx_current->idle_sp == STACKLEVEL |
1393 | && !(cctx_current->flags & CC_TRACE) |
1403 | && !(cctx_current->flags & CC_TRACE) |
1394 | && !force_cctx |
1404 | && !force_cctx |
1395 | )) |
1405 | )) |
1396 | { |
1406 | { |
1397 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1407 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1398 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te)); |
1408 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te)); |
1399 | |
1409 | |
1400 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1410 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */ |
1401 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
1411 | /* without this the next cctx_get might destroy the running cctx while still in use */ |
1402 | if (expect_false (CCTX_EXPIRED (cctx_current))) |
1412 | if (expect_false (CCTX_EXPIRED (cctx_current))) |
1403 | if (!next->cctx) |
1413 | if (expect_true (!next->cctx)) |
1404 | next->cctx = cctx_get (aTHX); |
1414 | next->cctx = cctx_get (aTHX); |
1405 | |
1415 | |
1406 | cctx_put (cctx_current); |
1416 | cctx_put (cctx_current); |
1407 | assert (!prev->cctx);//D temporary |
|
|
1408 | } |
1417 | } |
1409 | else |
1418 | else |
1410 | prev->cctx = cctx_current; |
1419 | prev->cctx = cctx_current; |
1411 | |
1420 | |
1412 | ++next->usecount; |
1421 | ++next->usecount; |
… | |
… | |
1583 | { |
1592 | { |
1584 | struct coro *coro; |
1593 | struct coro *coro; |
1585 | SV *sv_hook; |
1594 | SV *sv_hook; |
1586 | void (*xs_hook)(void); |
1595 | void (*xs_hook)(void); |
1587 | |
1596 | |
1588 | if (SvROK (coro_sv)) |
|
|
1589 | coro_sv = SvRV (coro_sv); |
|
|
1590 | |
|
|
1591 | coro = SvSTATE (coro_sv); |
1597 | coro = SvSTATE (coro_sv); |
1592 | |
1598 | |
1593 | if (coro->flags & CF_READY) |
1599 | if (coro->flags & CF_READY) |
1594 | return 0; |
1600 | return 0; |
1595 | |
1601 | |
… | |
… | |
1669 | } |
1675 | } |
1670 | } |
1676 | } |
1671 | else |
1677 | else |
1672 | { |
1678 | { |
1673 | /* nothing to schedule: call the idle handler */ |
1679 | /* nothing to schedule: call the idle handler */ |
|
|
1680 | if (SvROK (sv_idle) |
|
|
1681 | && SvOBJECT (SvRV (sv_idle))) |
|
|
1682 | { |
|
|
1683 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
|
|
1684 | api_ready (aTHX_ SvRV (sv_idle)); |
|
|
1685 | --coro_nready; |
|
|
1686 | } |
|
|
1687 | else |
|
|
1688 | { |
1674 | dSP; |
1689 | dSP; |
1675 | |
1690 | |
1676 | ENTER; |
1691 | ENTER; |
1677 | SAVETMPS; |
1692 | SAVETMPS; |
1678 | |
1693 | |
1679 | PUSHMARK (SP); |
1694 | PUSHMARK (SP); |
1680 | PUTBACK; |
1695 | PUTBACK; |
1681 | call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); |
1696 | call_sv (sv_idle, G_VOID | G_DISCARD); |
1682 | |
1697 | |
1683 | FREETMPS; |
1698 | FREETMPS; |
1684 | LEAVE; |
1699 | LEAVE; |
|
|
1700 | } |
1685 | } |
1701 | } |
1686 | } |
1702 | } |
1687 | } |
1703 | } |
1688 | |
1704 | |
1689 | INLINE void |
1705 | INLINE void |
… | |
… | |
1760 | static void |
1776 | static void |
1761 | api_trace (pTHX_ SV *coro_sv, int flags) |
1777 | api_trace (pTHX_ SV *coro_sv, int flags) |
1762 | { |
1778 | { |
1763 | struct coro *coro = SvSTATE (coro_sv); |
1779 | struct coro *coro = SvSTATE (coro_sv); |
1764 | |
1780 | |
|
|
1781 | if (coro->flags & CF_RUNNING) |
|
|
1782 | croak ("cannot enable tracing on a running coroutine, caught"); |
|
|
1783 | |
1765 | if (flags & CC_TRACE) |
1784 | if (flags & CC_TRACE) |
1766 | { |
1785 | { |
1767 | if (!coro->cctx) |
1786 | if (!coro->cctx) |
1768 | coro->cctx = cctx_new_run (); |
1787 | coro->cctx = cctx_new_run (); |
1769 | else if (!(coro->cctx->flags & CC_TRACE)) |
1788 | else if (!(coro->cctx->flags & CC_TRACE)) |
1770 | croak ("cannot enable tracing on coroutine with custom stack,"); |
1789 | croak ("cannot enable tracing on coroutine with custom stack, caught"); |
1771 | |
1790 | |
1772 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1791 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1773 | } |
1792 | } |
1774 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
1793 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
1775 | { |
1794 | { |
… | |
… | |
3032 | |
3051 | |
3033 | SV * |
3052 | SV * |
3034 | has_cctx (Coro::State coro) |
3053 | has_cctx (Coro::State coro) |
3035 | PROTOTYPE: $ |
3054 | PROTOTYPE: $ |
3036 | CODE: |
3055 | CODE: |
3037 | RETVAL = boolSV (!!coro->cctx); |
3056 | /* maybe manage the running flag differently */ |
|
|
3057 | RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING)); |
3038 | OUTPUT: |
3058 | OUTPUT: |
3039 | RETVAL |
3059 | RETVAL |
3040 | |
3060 | |
3041 | int |
3061 | int |
3042 | is_traced (Coro::State coro) |
3062 | is_traced (Coro::State coro) |
… | |
… | |
3062 | |
3082 | |
3063 | void |
3083 | void |
3064 | force_cctx () |
3084 | force_cctx () |
3065 | PROTOTYPE: |
3085 | PROTOTYPE: |
3066 | CODE: |
3086 | CODE: |
3067 | SvSTATE_current->cctx->idle_sp = 0; |
3087 | cctx_current->idle_sp = 0; |
3068 | |
3088 | |
3069 | void |
3089 | void |
3070 | swap_defsv (Coro::State self) |
3090 | swap_defsv (Coro::State self) |
3071 | PROTOTYPE: $ |
3091 | PROTOTYPE: $ |
3072 | ALIAS: |
3092 | ALIAS: |
… | |
… | |
3095 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
3115 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
3096 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
3116 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
3097 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
3117 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
3098 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
3118 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
3099 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
3119 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
|
|
3120 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
3100 | |
3121 | |
3101 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3122 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3102 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3123 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3103 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3124 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3104 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
3125 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |