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.461 by root, Fri Jun 17 16:29:50 2016 UTC vs.
Revision 1.464 by root, Wed Jun 22 22:53:49 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
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 sv_magic (nsv, mg->mg_obj, PERL_MAGIC_sigelem, name, namlen);
1070 assert (mg_find (nsv, PERL_MAGIC_sigelem)->mg_virtual == &PL_vtbl_sigelem);
1071 mg_find (nsv, PERL_MAGIC_sigelem)->mg_virtual = &coro_sigelem_vtbl;
1072 return 1;
1073}
1074
1075/* perl does not have a %SIG vtbl, we provide one so we can override */
1076/* the cwvtblagic for %SIG members */
1077static const MGVTBL coro_sig_vtbl = {
1078 0, 0, 0, 0, 0,
1079 coro_sig_copy
1080};
1055 1081
1056/* 1082/*
1057 * This overrides the default magic get method of %SIG elements. 1083 * 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 1084 * 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), 1085 * and instead of trying to save and restore the hash elements (extremely slow),
1085 sv_setsv (sv, ssv); 1111 sv_setsv (sv, ssv);
1086 return 0; 1112 return 0;
1087 } 1113 }
1088 } 1114 }
1089 1115
1090 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; 1116 return PL_vtbl_sigelem.svt_get ? PL_vtbl_sigelem.svt_get (aTHX_ sv, mg) : 0;
1091} 1117}
1092 1118
1093static int ecb_cold 1119static int ecb_cold
1094coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) 1120coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg)
1095{ 1121{
1109 SvREFCNT_dec (old); 1135 SvREFCNT_dec (old);
1110 return 0; 1136 return 0;
1111 } 1137 }
1112 } 1138 }
1113 1139
1114 return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0; 1140 return PL_vtbl_sigelem.svt_clear ? PL_vtbl_sigelem.svt_clear (aTHX_ sv, mg) : 0;
1115} 1141}
1116 1142
1117static int ecb_cold 1143static int ecb_cold
1118coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) 1144coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg)
1119{ 1145{
1133 SvREFCNT_dec (old); 1159 SvREFCNT_dec (old);
1134 return 0; 1160 return 0;
1135 } 1161 }
1136 } 1162 }
1137 1163
1138 return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; 1164 return PL_vtbl_sigelem.svt_set ? PL_vtbl_sigelem.svt_set (aTHX_ sv, mg) : 0;
1139} 1165}
1140 1166
1141static void 1167static void
1142prepare_nop (pTHX_ struct coro_transfer_args *ta) 1168prepare_nop (pTHX_ struct coro_transfer_args *ta)
1143{ 1169{
1154static int 1180static int
1155slf_check_repeat (pTHX_ struct CoroSLF *frame) 1181slf_check_repeat (pTHX_ struct CoroSLF *frame)
1156{ 1182{
1157 return 1; 1183 return 1;
1158} 1184}
1185
1186/** coroutine stack handling ************************************************/
1159 1187
1160static UNOP init_perl_op; 1188static UNOP init_perl_op;
1161 1189
1162ecb_noinline static void /* noinline to keep it out of the transfer fast path */ 1190ecb_noinline static void /* noinline to keep it out of the transfer fast path */
1163init_perl (pTHX_ struct coro *coro) 1191init_perl (pTHX_ struct coro *coro)
1536 /* 1564 /*
1537 * Unfortunately, there is no way to get at the return values of the 1565 * Unfortunately, there is no way to get at the return values of the
1538 * coro body here, as perl_run destroys these. Likewise, we cannot catch 1566 * 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. 1567 * runtime errors here, as this is just a random interpreter, not a thread.
1540 */ 1568 */
1569
1570 /*
1571 * pp_entersub in 5.24 no longer ENTERs, but perl_destruct
1572 * requires PL_scopestack_ix, so do it here if required.
1573 */
1574 if (!PL_scopestack_ix)
1575 ENTER;
1541 1576
1542 /* 1577 /*
1543 * If perl-run returns we assume exit() was being called or the coro 1578 * 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) 1579 * 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 1580 * reason for perl_run to return. We try to mimic whatever perl is normally
3619 cctx_current = cctx_new_empty (); 3654 cctx_current = cctx_new_empty ();
3620 3655
3621 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 3656 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
3622 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 3657 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
3623 3658
3624 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; 3659 {
3625 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; 3660 /*
3626 orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr; 3661 * we provide a vtbvl for %SIG magic that replaces PL_vtbl_sig
3662 * by coro_sig_vtbl in hash values.
3663 */
3664 MAGIC *mg = mg_find ((SV *)GvHV (gv_fetchpv ("SIG", GV_ADD | GV_NOTQUAL, SVt_PVHV)), PERL_MAGIC_sig);
3665
3666 /* this only works if perl doesn't have a vtbl for %SIG */
3667 assert (!mg->mg_virtual);
3668
3669 /*
3670 * The irony is that the perl API itself asserts that mg_virtual
3671 * must be non-const, yet perl5porters insisted on marking their
3672 * vtbls as read-only, just to thwart perl modules from patching
3673 * them.
3674 */
3675 mg->mg_virtual = (MGVTBL *)&coro_sig_vtbl;
3676 mg->mg_flags |= MGf_COPY;
3677
3678 coro_sigelem_vtbl = PL_vtbl_sigelem;
3679 coro_sigelem_vtbl.svt_get = coro_sigelem_get;
3680 coro_sigelem_vtbl.svt_set = coro_sigelem_set;
3681 coro_sigelem_vtbl.svt_clear = coro_sigelem_clr;
3682 }
3627 3683
3628 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); 3684 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
3629 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); 3685 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
3630 3686
3631 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 3687 coro_state_stash = gv_stashpv ("Coro::State", TRUE);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines