ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.xs
(Generate patch)

Comparing Coro/Coro/State.xs (file contents):
Revision 1.458 by root, Fri Jun 17 04:59:27 2016 UTC vs.
Revision 1.466 by root, Sat Jun 25 19:22:33 2016 UTC

116# define CORO_CLOCK_MONOTONIC 1 116# define CORO_CLOCK_MONOTONIC 1
117# define CORO_CLOCK_THREAD_CPUTIME_ID 3 117# define CORO_CLOCK_THREAD_CPUTIME_ID 3
118# endif 118# endif
119#endif 119#endif
120 120
121/* perl usually suppressed asserts. for debugging, we sometimes force it to be on */
122#if 0
123# undef NDEBUG
124# include <assert.h>
125#endif
126
121static double (*nvtime)(); /* so why doesn't it take void? */ 127static double (*nvtime)(); /* so why doesn't it take void? */
122static void (*u2time)(pTHX_ UV ret[2]); 128static void (*u2time)(pTHX_ UV ret[2]);
123 129
124/* we hijack an hopefully unused CV flag for our purposes */ 130/* we hijack an hopefully unused CV flag for our purposes */
125#define CVf_SLF 0x4000 131#define CVf_SLF 0x4000
730static void 736static void
731swap_svs_enter (pTHX_ Coro__State c) 737swap_svs_enter (pTHX_ Coro__State c)
732{ 738{
733 int i; 739 int i;
734 740
741 for (i = 0; i <= AvFILLp (c->swap_sv); i += 2)
742 swap_sv (AvARRAY (c->swap_sv)[i], AvARRAY (c->swap_sv)[i + 1]);
743}
744
745static void
746swap_svs_leave (pTHX_ Coro__State c)
747{
748 int i;
749
735 for (i = AvFILLp (c->swap_sv) - 1; i >= 0; i -= 2) 750 for (i = AvFILLp (c->swap_sv) - 1; i >= 0; i -= 2)
736 swap_sv (AvARRAY (c->swap_sv)[i], AvARRAY (c->swap_sv)[i + 1]);
737}
738
739static void
740swap_svs_leave (pTHX_ Coro__State c)
741{
742 int i;
743
744 for (i = 0; i <= AvFILLp (c->swap_sv); i += 2)
745 swap_sv (AvARRAY (c->swap_sv)[i], AvARRAY (c->swap_sv)[i + 1]); 751 swap_sv (AvARRAY (c->swap_sv)[i], AvARRAY (c->swap_sv)[i + 1]);
746} 752}
747 753
748#define SWAP_SVS_ENTER(coro) \ 754#define SWAP_SVS_ENTER(coro) \
749 if (ecb_expect_false ((coro)->swap_sv)) \ 755 if (ecb_expect_false ((coro)->swap_sv)) \
963#endif 969#endif
964 970
965 New(54,PL_savestack,24,ANY); 971 New(54,PL_savestack,24,ANY);
966 PL_savestack_ix = 0; 972 PL_savestack_ix = 0;
967 PL_savestack_max = 24; 973 PL_savestack_max = 24;
974#if PERL_VERSION_ATLEAST (5,24,0)
975 /* perl 5.24 moves SS_MAXPUSH optimisation from */
976 /* the header macros to PL_savestack_max */
977 PL_savestack_max -= SS_MAXPUSH;
978#endif
968 979
969#if !PERL_VERSION_ATLEAST (5,10,0) 980#if !PERL_VERSION_ATLEAST (5,10,0)
970 New(54,PL_retstack,4,OP*); 981 New(54,PL_retstack,4,OP*);
971 PL_retstack_ix = 0; 982 PL_retstack_ix = 0;
972 PL_retstack_max = 4; 983 PL_retstack_max = 4;
1038 } 1049 }
1039 1050
1040 return rss; 1051 return rss;
1041} 1052}
1042 1053
1043/** coroutine stack handling ************************************************/ 1054/** provide custom get/set/clear methods for %SIG elements ******************/
1044
1045static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg);
1046static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg);
1047static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg);
1048 1055
1049/* apparently < 5.8.8 */ 1056/* apparently < 5.8.8 */
1050#ifndef MgPV_nolen_const 1057#ifndef MgPV_nolen_const
1051#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ 1058#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \
1052 SvPV_nolen((SV*)((mg)->mg_ptr)) : \ 1059 SvPV_nolen((SV*)((mg)->mg_ptr)) : \
1053 (const char*)(mg)->mg_ptr) 1060 (const char*)(mg)->mg_ptr)
1054#endif 1061#endif
1062
1063/* this will be a patched copy of PL_vtbl_sigelem */
1064static MGVTBL coro_sigelem_vtbl;
1065
1066static int ecb_cold
1067coro_sig_copy (pTHX_ SV *sv, MAGIC *mg, SV *nsv, const char *name, I32 namlen)
1068{
1069 char *key = SvPV_nolen ((SV *)name);
1070
1071 /* do what mg_copy normally does */
1072 sv_magic (nsv, mg->mg_obj, PERL_MAGIC_sigelem, name, namlen);
1073 assert (mg_find (nsv, PERL_MAGIC_sigelem)->mg_virtual == &PL_vtbl_sigelem);
1074
1075 /* patch sigelem vtbl, but only for __WARN__ and __DIE__ */
1076 if (*key == '_'
1077 && (strEQ (key, "__DIE__")
1078 || strEQ (key, "__WARN__")))
1079 mg_find (nsv, PERL_MAGIC_sigelem)->mg_virtual = &coro_sigelem_vtbl;
1080
1081 return 1;
1082}
1083
1084/* perl does not have a %SIG vtbl, we provide one so we can override */
1085/* the magic vtbl for the __DIE__ and __WARN__ members */
1086static const MGVTBL coro_sig_vtbl = {
1087 0, 0, 0, 0, 0,
1088 coro_sig_copy
1089};
1055 1090
1056/* 1091/*
1057 * This overrides the default magic get method of %SIG elements. 1092 * This overrides the default magic get method of %SIG elements.
1058 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook 1093 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
1059 * and instead of trying to save and restore the hash elements (extremely slow), 1094 * and instead of trying to save and restore the hash elements (extremely slow),
1061 */ 1096 */
1062static int ecb_cold 1097static int ecb_cold
1063coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) 1098coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
1064{ 1099{
1065 const char *s = MgPV_nolen_const (mg); 1100 const char *s = MgPV_nolen_const (mg);
1101 /* the key must be either __DIE__ or __WARN__ here */
1102 SV **svp = s[2] == 'D' ? &PL_diehook : &PL_warnhook;
1066 1103
1067 if (*s == '_') 1104 SV *ssv;
1068 {
1069 SV **svp = 0;
1070 1105
1071 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
1072 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
1073
1074 if (svp) 1106 if (!*svp)
1075 {
1076 SV *ssv;
1077
1078 if (!*svp)
1079 ssv = &PL_sv_undef; 1107 ssv = &PL_sv_undef;
1080 else if (SvTYPE (*svp) == SVt_PVCV) /* perlio directly stores a CV in warnhook. ugh. */ 1108 else if (SvTYPE (*svp) == SVt_PVCV) /* perlio directly stores a CV in warnhook. ugh. */
1081 ssv = sv_2mortal (newRV_inc (*svp)); 1109 ssv = sv_2mortal (newRV_inc (*svp));
1082 else 1110 else
1083 ssv = *svp; 1111 ssv = *svp;
1084 1112
1085 sv_setsv (sv, ssv); 1113 sv_setsv (sv, ssv);
1086 return 0; 1114 return 0;
1087 }
1088 }
1089
1090 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0;
1091} 1115}
1092 1116
1093static int ecb_cold 1117static int ecb_cold
1094coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) 1118coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg)
1095{ 1119{
1096 const char *s = MgPV_nolen_const (mg); 1120 const char *s = MgPV_nolen_const (mg);
1121 /* the key must be either __DIE__ or __WARN__ here */
1122 SV **svp = s[2] == 'D' ? &PL_diehook : &PL_warnhook;
1097 1123
1098 if (*s == '_')
1099 {
1100 SV **svp = 0;
1101
1102 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
1103 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
1104
1105 if (svp)
1106 {
1107 SV *old = *svp; 1124 SV *old = *svp;
1108 *svp = 0; 1125 *svp = 0;
1109 SvREFCNT_dec (old); 1126 SvREFCNT_dec (old);
1110 return 0; 1127 return 0;
1111 }
1112 }
1113
1114 return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0;
1115} 1128}
1116 1129
1117static int ecb_cold 1130static int ecb_cold
1118coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) 1131coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg)
1119{ 1132{
1120 const char *s = MgPV_nolen_const (mg); 1133 const char *s = MgPV_nolen_const (mg);
1134 /* the key must be either __DIE__ or __WARN__ here */
1135 SV **svp = s[2] == 'D' ? &PL_diehook : &PL_warnhook;
1121 1136
1122 if (*s == '_')
1123 {
1124 SV **svp = 0;
1125
1126 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
1127 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
1128
1129 if (svp)
1130 {
1131 SV *old = *svp; 1137 SV *old = *svp;
1132 *svp = SvOK (sv) ? newSVsv (sv) : 0; 1138 *svp = SvOK (sv) ? newSVsv (sv) : 0;
1133 SvREFCNT_dec (old); 1139 SvREFCNT_dec (old);
1134 return 0; 1140 return 0;
1135 }
1136 }
1137
1138 return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0;
1139} 1141}
1140 1142
1141static void 1143static void
1142prepare_nop (pTHX_ struct coro_transfer_args *ta) 1144prepare_nop (pTHX_ struct coro_transfer_args *ta)
1143{ 1145{
1154static int 1156static int
1155slf_check_repeat (pTHX_ struct CoroSLF *frame) 1157slf_check_repeat (pTHX_ struct CoroSLF *frame)
1156{ 1158{
1157 return 1; 1159 return 1;
1158} 1160}
1161
1162/** coroutine stack handling ************************************************/
1159 1163
1160static UNOP init_perl_op; 1164static UNOP init_perl_op;
1161 1165
1162ecb_noinline static void /* noinline to keep it out of the transfer fast path */ 1166ecb_noinline static void /* noinline to keep it out of the transfer fast path */
1163init_perl (pTHX_ struct coro *coro) 1167init_perl (pTHX_ struct coro *coro)
1538 * coro body here, as perl_run destroys these. Likewise, we cannot catch 1542 * coro body here, as perl_run destroys these. Likewise, we cannot catch
1539 * runtime errors here, as this is just a random interpreter, not a thread. 1543 * runtime errors here, as this is just a random interpreter, not a thread.
1540 */ 1544 */
1541 1545
1542 /* 1546 /*
1547 * pp_entersub in 5.24 no longer ENTERs, but perl_destruct
1548 * requires PL_scopestack_ix, so do it here if required.
1549 */
1550 if (!PL_scopestack_ix)
1551 ENTER;
1552
1553 /*
1543 * If perl-run returns we assume exit() was being called or the coro 1554 * If perl-run returns we assume exit() was being called or the coro
1544 * fell off the end, which seems to be the only valid (non-bug) 1555 * fell off the end, which seems to be the only valid (non-bug)
1545 * reason for perl_run to return. We try to mimic whatever perl is normally 1556 * reason for perl_run to return. We try to mimic whatever perl is normally
1546 * doing in that case. YMMV. 1557 * doing in that case. YMMV.
1547 */ 1558 */
2428 av_clear (GvAV (PL_defgv)); 2439 av_clear (GvAV (PL_defgv));
2429 hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0); 2440 hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0);
2430 2441
2431 if (ecb_expect_false (coro->swap_sv)) 2442 if (ecb_expect_false (coro->swap_sv))
2432 { 2443 {
2444 swap_svs_leave (coro);
2433 SvREFCNT_dec_NN (coro->swap_sv); 2445 SvREFCNT_dec_NN (coro->swap_sv);
2434 coro->swap_sv = 0; 2446 coro->swap_sv = 0;
2435 } 2447 }
2436 2448
2437 coro->prio = 0; 2449 coro->prio = 0;
2438 2450
2439 if (coro->cctx && ecb_expect_false (coro->cctx->flags & CC_TRACE)) 2451 if (ecb_expect_false (coro->cctx) && ecb_expect_false (coro->cctx->flags & CC_TRACE))
2440 api_trace (aTHX_ coro_current, 0); 2452 api_trace (aTHX_ coro_current, 0);
2441 2453
2442 frame->prepare = prepare_schedule; 2454 frame->prepare = prepare_schedule;
2443 av_push (av_async_pool, SvREFCNT_inc (hv)); 2455 av_push (av_async_pool, SvREFCNT_inc (hv));
2444 } 2456 }
3618 cctx_current = cctx_new_empty (); 3630 cctx_current = cctx_new_empty ();
3619 3631
3620 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 3632 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
3621 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 3633 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
3622 3634
3623 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; 3635 {
3624 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; 3636 /*
3625 orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr; 3637 * we provide a vtbvl for %SIG magic that replaces PL_vtbl_sig
3638 * by coro_sig_vtbl in hash values.
3639 */
3640 MAGIC *mg = mg_find ((SV *)GvHV (gv_fetchpv ("SIG", GV_ADD | GV_NOTQUAL, SVt_PVHV)), PERL_MAGIC_sig);
3641
3642 /* this only works if perl doesn't have a vtbl for %SIG */
3643 assert (!mg->mg_virtual);
3644
3645 /*
3646 * The irony is that the perl API itself asserts that mg_virtual
3647 * must be non-const, yet perl5porters insisted on marking their
3648 * vtbls as read-only, just to thwart perl modules from patching
3649 * them.
3650 */
3651 mg->mg_virtual = (MGVTBL *)&coro_sig_vtbl;
3652 mg->mg_flags |= MGf_COPY;
3653
3654 coro_sigelem_vtbl = PL_vtbl_sigelem;
3655 coro_sigelem_vtbl.svt_get = coro_sigelem_get;
3656 coro_sigelem_vtbl.svt_set = coro_sigelem_set;
3657 coro_sigelem_vtbl.svt_clear = coro_sigelem_clr;
3658 }
3626 3659
3627 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); 3660 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
3628 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); 3661 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
3629 3662
3630 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 3663 coro_state_stash = gv_stashpv ("Coro::State", TRUE);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines