1
1
#include "Python.h"
2
+ #include "pycore_initconfig.h" // _PyStatus_ERR
2
3
#include "pycore_time.h" // PyTime_t
3
4
#include "pycore_pystate.h" // _Py_AssertHoldsTstate()
4
5
56
57
#endif
57
58
58
59
59
- #ifdef MS_WINDOWS
60
- static _PyTimeFraction py_qpc_base = {0 , 0 };
61
-
62
- // Forward declaration
63
- static int py_win_perf_counter_frequency (_PyTimeFraction * base , int raise_exc );
64
- #endif
65
-
66
-
67
60
static PyTime_t
68
61
_PyTime_GCD (PyTime_t x , PyTime_t y )
69
62
{
@@ -923,15 +916,9 @@ py_get_system_clock(PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
923
916
if (info ) {
924
917
// GetSystemTimePreciseAsFileTime() is implemented using
925
918
// QueryPerformanceCounter() internally.
926
- if (py_qpc_base .denom == 0 ) {
927
- if (py_win_perf_counter_frequency (& py_qpc_base , raise_exc ) < 0 ) {
928
- return -1 ;
929
- }
930
- }
931
-
932
919
info -> implementation = "GetSystemTimePreciseAsFileTime()" ;
933
920
info -> monotonic = 0 ;
934
- info -> resolution = _PyTimeFraction_Resolution (& py_qpc_base );
921
+ info -> resolution = _PyTimeFraction_Resolution (& _PyRuntime . time . base );
935
922
info -> adjustable = 1 ;
936
923
}
937
924
@@ -1043,8 +1030,8 @@ _PyTime_TimeWithInfo(PyTime_t *t, _Py_clock_info_t *info)
1043
1030
1044
1031
1045
1032
#ifdef MS_WINDOWS
1046
- static int
1047
- py_win_perf_counter_frequency (_PyTimeFraction * base , int raise_exc )
1033
+ static PyStatus
1034
+ py_win_perf_counter_frequency (_PyTimeFraction * base )
1048
1035
{
1049
1036
LARGE_INTEGER freq ;
1050
1037
// Since Windows XP, the function cannot fail.
@@ -1062,13 +1049,9 @@ py_win_perf_counter_frequency(_PyTimeFraction *base, int raise_exc)
1062
1049
// * 10,000,000 (10 MHz): 100 ns resolution
1063
1050
// * 3,579,545 Hz (3.6 MHz): 279 ns resolution
1064
1051
if (_PyTimeFraction_Set (base , SEC_TO_NS , denom ) < 0 ) {
1065
- if (raise_exc ) {
1066
- PyErr_SetString (PyExc_RuntimeError ,
1067
- "invalid QueryPerformanceFrequency" );
1068
- }
1069
- return -1 ;
1052
+ return _PyStatus_ERR ("invalid QueryPerformanceFrequency" );
1070
1053
}
1071
- return 0 ;
1054
+ return PyStatus_Ok () ;
1072
1055
}
1073
1056
1074
1057
@@ -1078,15 +1061,9 @@ py_get_win_perf_counter(PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
1078
1061
{
1079
1062
assert (info == NULL || raise_exc );
1080
1063
1081
- if (py_qpc_base .denom == 0 ) {
1082
- if (py_win_perf_counter_frequency (& py_qpc_base , raise_exc ) < 0 ) {
1083
- return -1 ;
1084
- }
1085
- }
1086
-
1087
1064
if (info ) {
1088
1065
info -> implementation = "QueryPerformanceCounter()" ;
1089
- info -> resolution = _PyTimeFraction_Resolution (& py_qpc_base );
1066
+ info -> resolution = _PyTimeFraction_Resolution (& _PyRuntime . time . base );
1090
1067
info -> monotonic = 1 ;
1091
1068
info -> adjustable = 0 ;
1092
1069
}
@@ -1102,15 +1079,15 @@ py_get_win_perf_counter(PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
1102
1079
"LONGLONG is larger than PyTime_t" );
1103
1080
ticks = (PyTime_t )ticksll ;
1104
1081
1105
- * tp = _PyTimeFraction_Mul (ticks , & py_qpc_base );
1082
+ * tp = _PyTimeFraction_Mul (ticks , & _PyRuntime . time . base );
1106
1083
return 0 ;
1107
1084
}
1108
1085
#endif // MS_WINDOWS
1109
1086
1110
1087
1111
1088
#ifdef __APPLE__
1112
- static int
1113
- py_mach_timebase_info (_PyTimeFraction * base , int raise_exc )
1089
+ static PyStatus
1090
+ py_mach_timebase_info (_PyTimeFraction * base )
1114
1091
{
1115
1092
mach_timebase_info_data_t timebase ;
1116
1093
// According to the Technical Q&A QA1398, mach_timebase_info() cannot
@@ -1132,16 +1109,23 @@ py_mach_timebase_info(_PyTimeFraction *base, int raise_exc)
1132
1109
// * (1000000000, 33333335) on PowerPC: ~30 ns
1133
1110
// * (1000000000, 25000000) on PowerPC: 40 ns
1134
1111
if (_PyTimeFraction_Set (base , numer , denom ) < 0 ) {
1135
- if (raise_exc ) {
1136
- PyErr_SetString (PyExc_RuntimeError ,
1137
- "invalid mach_timebase_info" );
1138
- }
1139
- return -1 ;
1112
+ return _PyStatus_ERR ("invalid mach_timebase_info" );
1140
1113
}
1141
- return 0 ;
1114
+ return PyStatus_Ok () ;
1142
1115
}
1143
1116
#endif
1144
1117
1118
+ PyStatus
1119
+ _PyTime_Init (struct _Py_time_runtime_state * state )
1120
+ {
1121
+ #ifdef MS_WINDOWS
1122
+ return py_win_perf_counter_frequency (& state -> base );
1123
+ #elif defined(__APPLE__ )
1124
+ return py_mach_timebase_info (& state -> base );
1125
+ #else
1126
+ return PyStatus_Ok ();
1127
+ #endif
1128
+ }
1145
1129
1146
1130
// N.B. If raise_exc=0, this may be called without a thread state.
1147
1131
static int
@@ -1158,16 +1142,9 @@ py_get_monotonic_clock(PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
1158
1142
return -1 ;
1159
1143
}
1160
1144
#elif defined(__APPLE__ )
1161
- static _PyTimeFraction base = {0 , 0 };
1162
- if (base .denom == 0 ) {
1163
- if (py_mach_timebase_info (& base , raise_exc ) < 0 ) {
1164
- return -1 ;
1165
- }
1166
- }
1167
-
1168
1145
if (info ) {
1169
1146
info -> implementation = "mach_absolute_time()" ;
1170
- info -> resolution = _PyTimeFraction_Resolution (& base );
1147
+ info -> resolution = _PyTimeFraction_Resolution (& _PyRuntime . time . base );
1171
1148
info -> monotonic = 1 ;
1172
1149
info -> adjustable = 0 ;
1173
1150
}
@@ -1177,7 +1154,7 @@ py_get_monotonic_clock(PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
1177
1154
assert (uticks <= (uint64_t )PyTime_MAX );
1178
1155
PyTime_t ticks = (PyTime_t )uticks ;
1179
1156
1180
- PyTime_t ns = _PyTimeFraction_Mul (ticks , & base );
1157
+ PyTime_t ns = _PyTimeFraction_Mul (ticks , & _PyRuntime . time . base );
1181
1158
* tp = ns ;
1182
1159
1183
1160
#elif defined(__hpux )
0 commit comments