summaryrefslogtreecommitdiffstats
path: root/contrib/tools/python3/Python/pytime.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/tools/python3/Python/pytime.c')
-rw-r--r--contrib/tools/python3/Python/pytime.c705
1 files changed, 346 insertions, 359 deletions
diff --git a/contrib/tools/python3/Python/pytime.c b/contrib/tools/python3/Python/pytime.c
index acd1842056a..560aea33f20 100644
--- a/contrib/tools/python3/Python/pytime.c
+++ b/contrib/tools/python3/Python/pytime.c
@@ -1,4 +1,10 @@
#include "Python.h"
+#include "pycore_time.h" // PyTime_t
+
+#include <time.h> // gmtime_r()
+#ifdef HAVE_SYS_TIME_H
+# include <sys/time.h> // gettimeofday()
+#endif
#ifdef MS_WINDOWS
# include <winsock2.h> // struct timeval
#endif
@@ -44,53 +50,82 @@
# error "time_t is not a two's complement integer type"
#endif
-#if _PyTime_MIN + _PyTime_MAX != -1
-# error "_PyTime_t is not a two's complement integer type"
+#if PyTime_MIN + PyTime_MAX != -1
+# error "PyTime_t is not a two's complement integer type"
#endif
-static void
-pytime_time_t_overflow(void)
+#ifdef MS_WINDOWS
+static _PyTimeFraction py_qpc_base = {0, 0};
+
+// Forward declaration
+static int py_win_perf_counter_frequency(_PyTimeFraction *base, int raise_exc);
+#endif
+
+
+static PyTime_t
+_PyTime_GCD(PyTime_t x, PyTime_t y)
{
- PyErr_SetString(PyExc_OverflowError,
- "timestamp out of range for platform time_t");
+ // Euclidean algorithm
+ assert(x >= 1);
+ assert(y >= 1);
+ while (y != 0) {
+ PyTime_t tmp = y;
+ y = x % y;
+ x = tmp;
+ }
+ assert(x >= 1);
+ return x;
}
-static void
-pytime_overflow(void)
+int
+_PyTimeFraction_Set(_PyTimeFraction *frac, PyTime_t numer, PyTime_t denom)
{
- PyErr_SetString(PyExc_OverflowError,
- "timestamp too large to convert to C _PyTime_t");
+ if (numer < 1 || denom < 1) {
+ return -1;
+ }
+
+ PyTime_t gcd = _PyTime_GCD(numer, denom);
+ frac->numer = numer / gcd;
+ frac->denom = denom / gcd;
+ return 0;
}
-static inline _PyTime_t
-pytime_from_nanoseconds(_PyTime_t t)
+double
+_PyTimeFraction_Resolution(const _PyTimeFraction *frac)
{
- // _PyTime_t is a number of nanoseconds
- return t;
+ return (double)frac->numer / (double)frac->denom / 1e9;
}
-static inline _PyTime_t
-pytime_as_nanoseconds(_PyTime_t t)
+static void
+pytime_time_t_overflow(void)
{
- // _PyTime_t is a number of nanoseconds: see pytime_from_nanoseconds()
- return t;
+ PyErr_SetString(PyExc_OverflowError,
+ "timestamp out of range for platform time_t");
+}
+
+
+static void
+pytime_overflow(void)
+{
+ PyErr_SetString(PyExc_OverflowError,
+ "timestamp too large to convert to C PyTime_t");
}
-// Compute t1 + t2. Clamp to [_PyTime_MIN; _PyTime_MAX] on overflow.
+// Compute t1 + t2. Clamp to [PyTime_MIN; PyTime_MAX] on overflow.
static inline int
-pytime_add(_PyTime_t *t1, _PyTime_t t2)
+pytime_add(PyTime_t *t1, PyTime_t t2)
{
- if (t2 > 0 && *t1 > _PyTime_MAX - t2) {
- *t1 = _PyTime_MAX;
+ if (t2 > 0 && *t1 > PyTime_MAX - t2) {
+ *t1 = PyTime_MAX;
return -1;
}
- else if (t2 < 0 && *t1 < _PyTime_MIN - t2) {
- *t1 = _PyTime_MIN;
+ else if (t2 < 0 && *t1 < PyTime_MIN - t2) {
+ *t1 = PyTime_MIN;
return -1;
}
else {
@@ -100,8 +135,8 @@ pytime_add(_PyTime_t *t1, _PyTime_t t2)
}
-_PyTime_t
-_PyTime_Add(_PyTime_t t1, _PyTime_t t2)
+PyTime_t
+_PyTime_Add(PyTime_t t1, PyTime_t t2)
{
(void)pytime_add(&t1, t2);
return t1;
@@ -109,11 +144,11 @@ _PyTime_Add(_PyTime_t t1, _PyTime_t t2)
static inline int
-pytime_mul_check_overflow(_PyTime_t a, _PyTime_t b)
+pytime_mul_check_overflow(PyTime_t a, PyTime_t b)
{
if (b != 0) {
assert(b > 0);
- return ((a < _PyTime_MIN / b) || (_PyTime_MAX / b < a));
+ return ((a < PyTime_MIN / b) || (PyTime_MAX / b < a));
}
else {
return 0;
@@ -121,13 +156,13 @@ pytime_mul_check_overflow(_PyTime_t a, _PyTime_t b)
}
-// Compute t * k. Clamp to [_PyTime_MIN; _PyTime_MAX] on overflow.
+// Compute t * k. Clamp to [PyTime_MIN; PyTime_MAX] on overflow.
static inline int
-pytime_mul(_PyTime_t *t, _PyTime_t k)
+pytime_mul(PyTime_t *t, PyTime_t k)
{
assert(k >= 0);
if (pytime_mul_check_overflow(*t, k)) {
- *t = (*t >= 0) ? _PyTime_MAX : _PyTime_MIN;
+ *t = (*t >= 0) ? PyTime_MAX : PyTime_MIN;
return -1;
}
else {
@@ -137,26 +172,32 @@ pytime_mul(_PyTime_t *t, _PyTime_t k)
}
-// Compute t * k. Clamp to [_PyTime_MIN; _PyTime_MAX] on overflow.
-static inline _PyTime_t
-_PyTime_Mul(_PyTime_t t, _PyTime_t k)
+// Compute t * k. Clamp to [PyTime_MIN; PyTime_MAX] on overflow.
+static inline PyTime_t
+_PyTime_Mul(PyTime_t t, PyTime_t k)
{
(void)pytime_mul(&t, k);
return t;
}
+PyTime_t
+_PyTimeFraction_Mul(PyTime_t ticks, const _PyTimeFraction *frac)
+{
+ const PyTime_t mul = frac->numer;
+ const PyTime_t div = frac->denom;
+ if (div == 1) {
+ // Fast-path taken by mach_absolute_time() with 1/1 time base.
+ return _PyTime_Mul(ticks, mul);
+ }
-_PyTime_t
-_PyTime_MulDiv(_PyTime_t ticks, _PyTime_t mul, _PyTime_t div)
-{
/* Compute (ticks * mul / div) in two parts to reduce the risk of integer
overflow: compute the integer part, and then the remaining part.
(ticks * mul) / div == (ticks / div) * mul + (ticks % div) * mul / div
*/
- _PyTime_t intpart, remaining;
+ PyTime_t intpart, remaining;
intpart = ticks / div;
ticks %= div;
remaining = _PyTime_Mul(ticks, mul) / div;
@@ -198,17 +239,17 @@ _PyLong_FromTime_t(time_t t)
}
-// Convert _PyTime_t to time_t.
+// Convert PyTime_t to time_t.
// Return 0 on success. Return -1 and clamp the value on overflow.
static int
-_PyTime_AsTime_t(_PyTime_t t, time_t *t2)
+_PyTime_AsTime_t(PyTime_t t, time_t *t2)
{
#if SIZEOF_TIME_T < _SIZEOF_PYTIME_T
- if ((_PyTime_t)PY_TIME_T_MAX < t) {
+ if ((PyTime_t)PY_TIME_T_MAX < t) {
*t2 = PY_TIME_T_MAX;
return -1;
}
- if (t < (_PyTime_t)PY_TIME_T_MIN) {
+ if (t < (PyTime_t)PY_TIME_T_MIN) {
*t2 = PY_TIME_T_MIN;
return -1;
}
@@ -219,17 +260,17 @@ _PyTime_AsTime_t(_PyTime_t t, time_t *t2)
#ifdef MS_WINDOWS
-// Convert _PyTime_t to long.
+// Convert PyTime_t to long.
// Return 0 on success. Return -1 and clamp the value on overflow.
static int
-_PyTime_AsLong(_PyTime_t t, long *t2)
+_PyTime_AsCLong(PyTime_t t, long *t2)
{
#if SIZEOF_LONG < _SIZEOF_PYTIME_T
- if ((_PyTime_t)LONG_MAX < t) {
+ if ((PyTime_t)LONG_MAX < t) {
*t2 = LONG_MAX;
return -1;
}
- if (t < (_PyTime_t)LONG_MIN) {
+ if (t < (PyTime_t)LONG_MIN) {
*t2 = LONG_MIN;
return -1;
}
@@ -404,50 +445,42 @@ _PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec,
}
-_PyTime_t
+PyTime_t
_PyTime_FromSeconds(int seconds)
{
/* ensure that integer overflow cannot happen, int type should have 32
- bits, whereas _PyTime_t type has at least 64 bits (SEC_TO_NS takes 30
+ bits, whereas PyTime_t type has at least 64 bits (SEC_TO_NS takes 30
bits). */
- static_assert(INT_MAX <= _PyTime_MAX / SEC_TO_NS, "_PyTime_t overflow");
- static_assert(INT_MIN >= _PyTime_MIN / SEC_TO_NS, "_PyTime_t underflow");
+ static_assert(INT_MAX <= PyTime_MAX / SEC_TO_NS, "PyTime_t overflow");
+ static_assert(INT_MIN >= PyTime_MIN / SEC_TO_NS, "PyTime_t underflow");
- _PyTime_t t = (_PyTime_t)seconds;
- assert((t >= 0 && t <= _PyTime_MAX / SEC_TO_NS)
- || (t < 0 && t >= _PyTime_MIN / SEC_TO_NS));
+ PyTime_t t = (PyTime_t)seconds;
+ assert((t >= 0 && t <= PyTime_MAX / SEC_TO_NS)
+ || (t < 0 && t >= PyTime_MIN / SEC_TO_NS));
t *= SEC_TO_NS;
- return pytime_from_nanoseconds(t);
-}
-
-
-_PyTime_t
-_PyTime_FromNanoseconds(_PyTime_t ns)
-{
- return pytime_from_nanoseconds(ns);
+ return t;
}
-_PyTime_t
-_PyTime_FromMicrosecondsClamp(_PyTime_t us)
+PyTime_t
+_PyTime_FromMicrosecondsClamp(PyTime_t us)
{
- _PyTime_t ns = _PyTime_Mul(us, US_TO_NS);
- return pytime_from_nanoseconds(ns);
+ PyTime_t ns = _PyTime_Mul(us, US_TO_NS);
+ return ns;
}
int
-_PyTime_FromNanosecondsObject(_PyTime_t *tp, PyObject *obj)
+_PyTime_FromLong(PyTime_t *tp, PyObject *obj)
{
-
if (!PyLong_Check(obj)) {
PyErr_Format(PyExc_TypeError, "expect int, got %s",
Py_TYPE(obj)->tp_name);
return -1;
}
- static_assert(sizeof(long long) == sizeof(_PyTime_t),
- "_PyTime_t is not long long");
+ static_assert(sizeof(long long) == sizeof(PyTime_t),
+ "PyTime_t is not long long");
long long nsec = PyLong_AsLongLong(obj);
if (nsec == -1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
@@ -456,28 +489,28 @@ _PyTime_FromNanosecondsObject(_PyTime_t *tp, PyObject *obj)
return -1;
}
- _PyTime_t t = (_PyTime_t)nsec;
- *tp = pytime_from_nanoseconds(t);
+ PyTime_t t = (PyTime_t)nsec;
+ *tp = t;
return 0;
}
#ifdef HAVE_CLOCK_GETTIME
static int
-pytime_fromtimespec(_PyTime_t *tp, struct timespec *ts, int raise_exc)
+pytime_fromtimespec(PyTime_t *tp, const struct timespec *ts, int raise_exc)
{
- _PyTime_t t, tv_nsec;
+ PyTime_t t, tv_nsec;
- static_assert(sizeof(ts->tv_sec) <= sizeof(_PyTime_t),
- "timespec.tv_sec is larger than _PyTime_t");
- t = (_PyTime_t)ts->tv_sec;
+ static_assert(sizeof(ts->tv_sec) <= sizeof(PyTime_t),
+ "timespec.tv_sec is larger than PyTime_t");
+ t = (PyTime_t)ts->tv_sec;
int res1 = pytime_mul(&t, SEC_TO_NS);
tv_nsec = ts->tv_nsec;
int res2 = pytime_add(&t, tv_nsec);
- *tp = pytime_from_nanoseconds(t);
+ *tp = t;
if (raise_exc && (res1 < 0 || res2 < 0)) {
pytime_overflow();
@@ -487,7 +520,7 @@ pytime_fromtimespec(_PyTime_t *tp, struct timespec *ts, int raise_exc)
}
int
-_PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts)
+_PyTime_FromTimespec(PyTime_t *tp, const struct timespec *ts)
{
return pytime_fromtimespec(tp, ts, 1);
}
@@ -496,18 +529,18 @@ _PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts)
#ifndef MS_WINDOWS
static int
-pytime_fromtimeval(_PyTime_t *tp, struct timeval *tv, int raise_exc)
+pytime_fromtimeval(PyTime_t *tp, struct timeval *tv, int raise_exc)
{
- static_assert(sizeof(tv->tv_sec) <= sizeof(_PyTime_t),
- "timeval.tv_sec is larger than _PyTime_t");
- _PyTime_t t = (_PyTime_t)tv->tv_sec;
+ static_assert(sizeof(tv->tv_sec) <= sizeof(PyTime_t),
+ "timeval.tv_sec is larger than PyTime_t");
+ PyTime_t t = (PyTime_t)tv->tv_sec;
int res1 = pytime_mul(&t, SEC_TO_NS);
- _PyTime_t usec = (_PyTime_t)tv->tv_usec * US_TO_NS;
+ PyTime_t usec = (PyTime_t)tv->tv_usec * US_TO_NS;
int res2 = pytime_add(&t, usec);
- *tp = pytime_from_nanoseconds(t);
+ *tp = t;
if (raise_exc && (res1 < 0 || res2 < 0)) {
pytime_overflow();
@@ -518,7 +551,7 @@ pytime_fromtimeval(_PyTime_t *tp, struct timeval *tv, int raise_exc)
int
-_PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv)
+_PyTime_FromTimeval(PyTime_t *tp, struct timeval *tv)
{
return pytime_fromtimeval(tp, tv, 1);
}
@@ -526,7 +559,7 @@ _PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv)
static int
-pytime_from_double(_PyTime_t *tp, double value, _PyTime_round_t round,
+pytime_from_double(PyTime_t *tp, double value, _PyTime_round_t round,
long unit_to_ns)
{
/* volatile avoids optimization changing how numbers are rounded */
@@ -538,19 +571,20 @@ pytime_from_double(_PyTime_t *tp, double value, _PyTime_round_t round,
d = pytime_round(d, round);
/* See comments in pytime_double_to_denominator */
- if (!((double)_PyTime_MIN <= d && d < -(double)_PyTime_MIN)) {
+ if (!((double)PyTime_MIN <= d && d < -(double)PyTime_MIN)) {
pytime_time_t_overflow();
+ *tp = 0;
return -1;
}
- _PyTime_t ns = (_PyTime_t)d;
+ PyTime_t ns = (PyTime_t)d;
- *tp = pytime_from_nanoseconds(ns);
+ *tp = ns;
return 0;
}
static int
-pytime_from_object(_PyTime_t *tp, PyObject *obj, _PyTime_round_t round,
+pytime_from_object(PyTime_t *tp, PyObject *obj, _PyTime_round_t round,
long unit_to_ns)
{
if (PyFloat_Check(obj)) {
@@ -571,45 +605,44 @@ pytime_from_object(_PyTime_t *tp, PyObject *obj, _PyTime_round_t round,
return -1;
}
- static_assert(sizeof(long long) <= sizeof(_PyTime_t),
- "_PyTime_t is smaller than long long");
- _PyTime_t ns = (_PyTime_t)sec;
+ static_assert(sizeof(long long) <= sizeof(PyTime_t),
+ "PyTime_t is smaller than long long");
+ PyTime_t ns = (PyTime_t)sec;
if (pytime_mul(&ns, unit_to_ns) < 0) {
pytime_overflow();
return -1;
}
- *tp = pytime_from_nanoseconds(ns);
+ *tp = ns;
return 0;
}
}
int
-_PyTime_FromSecondsObject(_PyTime_t *tp, PyObject *obj, _PyTime_round_t round)
+_PyTime_FromSecondsObject(PyTime_t *tp, PyObject *obj, _PyTime_round_t round)
{
return pytime_from_object(tp, obj, round, SEC_TO_NS);
}
int
-_PyTime_FromMillisecondsObject(_PyTime_t *tp, PyObject *obj, _PyTime_round_t round)
+_PyTime_FromMillisecondsObject(PyTime_t *tp, PyObject *obj, _PyTime_round_t round)
{
return pytime_from_object(tp, obj, round, MS_TO_NS);
}
double
-_PyTime_AsSecondsDouble(_PyTime_t t)
+PyTime_AsSecondsDouble(PyTime_t ns)
{
/* volatile avoids optimization changing how numbers are rounded */
volatile double d;
- _PyTime_t ns = pytime_as_nanoseconds(t);
if (ns % SEC_TO_NS == 0) {
/* Divide using integers to avoid rounding issues on the integer part.
1e-9 cannot be stored exactly in IEEE 64-bit. */
- _PyTime_t secs = ns / SEC_TO_NS;
+ PyTime_t secs = ns / SEC_TO_NS;
d = (double)secs;
}
else {
@@ -621,23 +654,28 @@ _PyTime_AsSecondsDouble(_PyTime_t t)
PyObject *
-_PyTime_AsNanosecondsObject(_PyTime_t t)
+_PyTime_AsLong(PyTime_t ns)
{
- _PyTime_t ns = pytime_as_nanoseconds(t);
- static_assert(sizeof(long long) >= sizeof(_PyTime_t),
- "_PyTime_t is larger than long long");
+ static_assert(sizeof(long long) >= sizeof(PyTime_t),
+ "PyTime_t is larger than long long");
return PyLong_FromLongLong((long long)ns);
}
+int
+_PyTime_FromSecondsDouble(double seconds, _PyTime_round_t round, PyTime_t *result)
+{
+ return pytime_from_double(result, seconds, round, SEC_TO_NS);
+}
-static _PyTime_t
-pytime_divide_round_up(const _PyTime_t t, const _PyTime_t k)
+
+static PyTime_t
+pytime_divide_round_up(const PyTime_t t, const PyTime_t k)
{
assert(k > 1);
if (t >= 0) {
// Don't use (t + k - 1) / k to avoid integer overflow
- // if t is equal to _PyTime_MAX
- _PyTime_t q = t / k;
+ // if t is equal to PyTime_MAX
+ PyTime_t q = t / k;
if (t % k) {
q += 1;
}
@@ -645,8 +683,8 @@ pytime_divide_round_up(const _PyTime_t t, const _PyTime_t k)
}
else {
// Don't use (t - (k - 1)) / k to avoid integer overflow
- // if t is equals to _PyTime_MIN.
- _PyTime_t q = t / k;
+ // if t is equals to PyTime_MIN.
+ PyTime_t q = t / k;
if (t % k) {
q -= 1;
}
@@ -655,15 +693,15 @@ pytime_divide_round_up(const _PyTime_t t, const _PyTime_t k)
}
-static _PyTime_t
-pytime_divide(const _PyTime_t t, const _PyTime_t k,
+static PyTime_t
+pytime_divide(const PyTime_t t, const PyTime_t k,
const _PyTime_round_t round)
{
assert(k > 1);
if (round == _PyTime_ROUND_HALF_EVEN) {
- _PyTime_t x = t / k;
- _PyTime_t r = t % k;
- _PyTime_t abs_r = Py_ABS(r);
+ PyTime_t x = t / k;
+ PyTime_t r = t % k;
+ PyTime_t abs_r = Py_ABS(r);
if (abs_r > k / 2 || (abs_r == k / 2 && (Py_ABS(x) & 1))) {
if (t >= 0) {
x++;
@@ -700,17 +738,17 @@ pytime_divide(const _PyTime_t t, const _PyTime_t k,
// Compute (t / k, t % k) in (pq, pr).
// Make sure that 0 <= pr < k.
// Return 0 on success.
-// Return -1 on underflow and store (_PyTime_MIN, 0) in (pq, pr).
+// Return -1 on underflow and store (PyTime_MIN, 0) in (pq, pr).
static int
-pytime_divmod(const _PyTime_t t, const _PyTime_t k,
- _PyTime_t *pq, _PyTime_t *pr)
+pytime_divmod(const PyTime_t t, const PyTime_t k,
+ PyTime_t *pq, PyTime_t *pr)
{
assert(k > 1);
- _PyTime_t q = t / k;
- _PyTime_t r = t % k;
+ PyTime_t q = t / k;
+ PyTime_t r = t % k;
if (r < 0) {
- if (q == _PyTime_MIN) {
- *pq = _PyTime_MIN;
+ if (q == PyTime_MIN) {
+ *pq = PyTime_MIN;
*pr = 0;
return -1;
}
@@ -725,47 +763,36 @@ pytime_divmod(const _PyTime_t t, const _PyTime_t k,
}
-_PyTime_t
-_PyTime_AsNanoseconds(_PyTime_t t)
-{
- return pytime_as_nanoseconds(t);
-}
-
-
#ifdef MS_WINDOWS
-_PyTime_t
-_PyTime_As100Nanoseconds(_PyTime_t t, _PyTime_round_t round)
+PyTime_t
+_PyTime_As100Nanoseconds(PyTime_t ns, _PyTime_round_t round)
{
- _PyTime_t ns = pytime_as_nanoseconds(t);
return pytime_divide(ns, NS_TO_100NS, round);
}
#endif
-_PyTime_t
-_PyTime_AsMicroseconds(_PyTime_t t, _PyTime_round_t round)
+PyTime_t
+_PyTime_AsMicroseconds(PyTime_t ns, _PyTime_round_t round)
{
- _PyTime_t ns = pytime_as_nanoseconds(t);
return pytime_divide(ns, NS_TO_US, round);
}
-_PyTime_t
-_PyTime_AsMilliseconds(_PyTime_t t, _PyTime_round_t round)
+PyTime_t
+_PyTime_AsMilliseconds(PyTime_t ns, _PyTime_round_t round)
{
- _PyTime_t ns = pytime_as_nanoseconds(t);
return pytime_divide(ns, NS_TO_MS, round);
}
static int
-pytime_as_timeval(_PyTime_t t, _PyTime_t *ptv_sec, int *ptv_usec,
+pytime_as_timeval(PyTime_t ns, PyTime_t *ptv_sec, int *ptv_usec,
_PyTime_round_t round)
{
- _PyTime_t ns = pytime_as_nanoseconds(t);
- _PyTime_t us = pytime_divide(ns, US_TO_NS, round);
+ PyTime_t us = pytime_divide(ns, US_TO_NS, round);
- _PyTime_t tv_sec, tv_usec;
+ PyTime_t tv_sec, tv_usec;
int res = pytime_divmod(us, SEC_TO_US, &tv_sec, &tv_usec);
*ptv_sec = tv_sec;
*ptv_usec = (int)tv_usec;
@@ -774,16 +801,16 @@ pytime_as_timeval(_PyTime_t t, _PyTime_t *ptv_sec, int *ptv_usec,
static int
-pytime_as_timeval_struct(_PyTime_t t, struct timeval *tv,
+pytime_as_timeval_struct(PyTime_t t, struct timeval *tv,
_PyTime_round_t round, int raise_exc)
{
- _PyTime_t tv_sec;
+ PyTime_t tv_sec;
int tv_usec;
int res = pytime_as_timeval(t, &tv_sec, &tv_usec, round);
int res2;
#ifdef MS_WINDOWS
// On Windows, timeval.tv_sec type is long
- res2 = _PyTime_AsLong(tv_sec, &tv->tv_sec);
+ res2 = _PyTime_AsCLong(tv_sec, &tv->tv_sec);
#else
res2 = _PyTime_AsTime_t(tv_sec, &tv->tv_sec);
#endif
@@ -801,24 +828,24 @@ pytime_as_timeval_struct(_PyTime_t t, struct timeval *tv,
int
-_PyTime_AsTimeval(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
+_PyTime_AsTimeval(PyTime_t t, struct timeval *tv, _PyTime_round_t round)
{
return pytime_as_timeval_struct(t, tv, round, 1);
}
void
-_PyTime_AsTimeval_clamp(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
+_PyTime_AsTimeval_clamp(PyTime_t t, struct timeval *tv, _PyTime_round_t round)
{
(void)pytime_as_timeval_struct(t, tv, round, 0);
}
int
-_PyTime_AsTimevalTime_t(_PyTime_t t, time_t *p_secs, int *us,
+_PyTime_AsTimevalTime_t(PyTime_t t, time_t *p_secs, int *us,
_PyTime_round_t round)
{
- _PyTime_t secs;
+ PyTime_t secs;
if (pytime_as_timeval(t, &secs, us, round) < 0) {
pytime_time_t_overflow();
return -1;
@@ -834,10 +861,9 @@ _PyTime_AsTimevalTime_t(_PyTime_t t, time_t *p_secs, int *us,
#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE)
static int
-pytime_as_timespec(_PyTime_t t, struct timespec *ts, int raise_exc)
+pytime_as_timespec(PyTime_t ns, struct timespec *ts, int raise_exc)
{
- _PyTime_t ns = pytime_as_nanoseconds(t);
- _PyTime_t tv_sec, tv_nsec;
+ PyTime_t tv_sec, tv_nsec;
int res = pytime_divmod(ns, SEC_TO_NS, &tv_sec, &tv_nsec);
int res2 = _PyTime_AsTime_t(tv_sec, &ts->tv_sec);
@@ -854,49 +880,53 @@ pytime_as_timespec(_PyTime_t t, struct timespec *ts, int raise_exc)
}
void
-_PyTime_AsTimespec_clamp(_PyTime_t t, struct timespec *ts)
+_PyTime_AsTimespec_clamp(PyTime_t t, struct timespec *ts)
{
(void)pytime_as_timespec(t, ts, 0);
}
int
-_PyTime_AsTimespec(_PyTime_t t, struct timespec *ts)
+_PyTime_AsTimespec(PyTime_t t, struct timespec *ts)
{
return pytime_as_timespec(t, ts, 1);
}
#endif
+// N.B. If raise_exc=0, this may be called without the GIL.
static int
-py_get_system_clock(_PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
+py_get_system_clock(PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
{
assert(info == NULL || raise_exc);
+ if (raise_exc) {
+ // raise_exc requires to hold the GIL
+ assert(PyGILState_Check());
+ }
#ifdef MS_WINDOWS
FILETIME system_time;
ULARGE_INTEGER large;
- GetSystemTimeAsFileTime(&system_time);
+ GetSystemTimePreciseAsFileTime(&system_time);
large.u.LowPart = system_time.dwLowDateTime;
large.u.HighPart = system_time.dwHighDateTime;
/* 11,644,473,600,000,000,000: number of nanoseconds between
the 1st january 1601 and the 1st january 1970 (369 years + 89 leap
days). */
- _PyTime_t ns = large.QuadPart * 100 - 11644473600000000000;
- *tp = pytime_from_nanoseconds(ns);
+ PyTime_t ns = large.QuadPart * 100 - 11644473600000000000;
+ *tp = ns;
if (info) {
- DWORD timeAdjustment, timeIncrement;
- BOOL isTimeAdjustmentDisabled, ok;
+ // GetSystemTimePreciseAsFileTime() is implemented using
+ // QueryPerformanceCounter() internally.
+ if (py_qpc_base.denom == 0) {
+ if (py_win_perf_counter_frequency(&py_qpc_base, raise_exc) < 0) {
+ return -1;
+ }
+ }
- info->implementation = "GetSystemTimeAsFileTime()";
+ info->implementation = "GetSystemTimePreciseAsFileTime()";
info->monotonic = 0;
- ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
- &isTimeAdjustmentDisabled);
- if (!ok) {
- PyErr_SetFromWindowsErr(0);
- return -1;
- }
- info->resolution = timeIncrement * 1e-7;
+ info->resolution = _PyTimeFraction_Resolution(&py_qpc_base);
info->adjustable = 1;
}
@@ -969,7 +999,7 @@ py_get_system_clock(_PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
}
#if defined(HAVE_CLOCK_GETTIME_RUNTIME) && defined(HAVE_CLOCK_GETTIME)
- } /* end of availibity block */
+ } /* end of availability block */
#endif
#endif /* !HAVE_CLOCK_GETTIME */
@@ -978,148 +1008,175 @@ py_get_system_clock(_PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
}
-_PyTime_t
-_PyTime_GetSystemClock(void)
+int
+PyTime_Time(PyTime_t *result)
+{
+ if (py_get_system_clock(result, NULL, 1) < 0) {
+ *result = 0;
+ return -1;
+ }
+ return 0;
+}
+
+
+int
+PyTime_TimeRaw(PyTime_t *result)
{
- _PyTime_t t;
- if (py_get_system_clock(&t, NULL, 0) < 0) {
- // If clock_gettime(CLOCK_REALTIME) or gettimeofday() fails:
- // silently ignore the failure and return 0.
- t = 0;
+ if (py_get_system_clock(result, NULL, 0) < 0) {
+ *result = 0;
+ return -1;
}
- return t;
+ return 0;
}
int
-_PyTime_GetSystemClockWithInfo(_PyTime_t *t, _Py_clock_info_t *info)
+_PyTime_TimeWithInfo(PyTime_t *t, _Py_clock_info_t *info)
{
return py_get_system_clock(t, info, 1);
}
-#ifdef __APPLE__
+#ifdef MS_WINDOWS
static int
-py_mach_timebase_info(_PyTime_t *pnumer, _PyTime_t *pdenom, int raise)
+py_win_perf_counter_frequency(_PyTimeFraction *base, int raise_exc)
{
- static mach_timebase_info_data_t timebase;
- /* According to the Technical Q&A QA1398, mach_timebase_info() cannot
- fail: https://developer.apple.com/library/mac/#qa/qa1398/ */
- (void)mach_timebase_info(&timebase);
+ LARGE_INTEGER freq;
+ // Since Windows XP, the function cannot fail.
+ (void)QueryPerformanceFrequency(&freq);
+ LONGLONG frequency = freq.QuadPart;
+
+ // Since Windows XP, frequency cannot be zero.
+ assert(frequency >= 1);
+
+ Py_BUILD_ASSERT(sizeof(PyTime_t) == sizeof(frequency));
+ PyTime_t denom = (PyTime_t)frequency;
- /* Sanity check: should never occur in practice */
- if (timebase.numer < 1 || timebase.denom < 1) {
- if (raise) {
+ // Known QueryPerformanceFrequency() values:
+ //
+ // * 10,000,000 (10 MHz): 100 ns resolution
+ // * 3,579,545 Hz (3.6 MHz): 279 ns resolution
+ if (_PyTimeFraction_Set(base, SEC_TO_NS, denom) < 0) {
+ if (raise_exc) {
PyErr_SetString(PyExc_RuntimeError,
- "invalid mach_timebase_info");
+ "invalid QueryPerformanceFrequency");
}
return -1;
}
+ return 0;
+}
- /* Check that timebase.numer and timebase.denom can be casted to
- _PyTime_t. In practice, timebase uses uint32_t, so casting cannot
- overflow. At the end, only make sure that the type is uint32_t
- (_PyTime_t is 64-bit long). */
- static_assert(sizeof(timebase.numer) <= sizeof(_PyTime_t),
- "timebase.numer is larger than _PyTime_t");
- static_assert(sizeof(timebase.denom) <= sizeof(_PyTime_t),
- "timebase.denom is larger than _PyTime_t");
- /* Make sure that _PyTime_MulDiv(ticks, timebase_numer, timebase_denom)
- cannot overflow.
+// N.B. If raise_exc=0, this may be called without the GIL.
+static int
+py_get_win_perf_counter(PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
+{
+ assert(info == NULL || raise_exc);
- Known time bases:
+ if (py_qpc_base.denom == 0) {
+ if (py_win_perf_counter_frequency(&py_qpc_base, raise_exc) < 0) {
+ return -1;
+ }
+ }
- * (1, 1) on Intel
- * (1000000000, 33333335) or (1000000000, 25000000) on PowerPC
+ if (info) {
+ info->implementation = "QueryPerformanceCounter()";
+ info->resolution = _PyTimeFraction_Resolution(&py_qpc_base);
+ info->monotonic = 1;
+ info->adjustable = 0;
+ }
- None of these time bases can overflow with 64-bit _PyTime_t, but
- check for overflow, just in case. */
- if ((_PyTime_t)timebase.numer > _PyTime_MAX / (_PyTime_t)timebase.denom) {
- if (raise) {
- PyErr_SetString(PyExc_OverflowError,
- "mach_timebase_info is too large");
+ LARGE_INTEGER now;
+ QueryPerformanceCounter(&now);
+ LONGLONG ticksll = now.QuadPart;
+
+ /* Make sure that casting LONGLONG to PyTime_t cannot overflow,
+ both types are signed */
+ PyTime_t ticks;
+ static_assert(sizeof(ticksll) <= sizeof(ticks),
+ "LONGLONG is larger than PyTime_t");
+ ticks = (PyTime_t)ticksll;
+
+ *tp = _PyTimeFraction_Mul(ticks, &py_qpc_base);
+ return 0;
+}
+#endif // MS_WINDOWS
+
+
+#ifdef __APPLE__
+static int
+py_mach_timebase_info(_PyTimeFraction *base, int raise_exc)
+{
+ mach_timebase_info_data_t timebase;
+ // According to the Technical Q&A QA1398, mach_timebase_info() cannot
+ // fail: https://developer.apple.com/library/mac/#qa/qa1398/
+ (void)mach_timebase_info(&timebase);
+
+ // Check that timebase.numer and timebase.denom can be casted to
+ // PyTime_t. In practice, timebase uses uint32_t, so casting cannot
+ // overflow. At the end, only make sure that the type is uint32_t
+ // (PyTime_t is 64-bit long).
+ Py_BUILD_ASSERT(sizeof(timebase.numer) <= sizeof(PyTime_t));
+ Py_BUILD_ASSERT(sizeof(timebase.denom) <= sizeof(PyTime_t));
+ PyTime_t numer = (PyTime_t)timebase.numer;
+ PyTime_t denom = (PyTime_t)timebase.denom;
+
+ // Known time bases:
+ //
+ // * (1, 1) on Intel: 1 ns
+ // * (1000000000, 33333335) on PowerPC: ~30 ns
+ // * (1000000000, 25000000) on PowerPC: 40 ns
+ if (_PyTimeFraction_Set(base, numer, denom) < 0) {
+ if (raise_exc) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "invalid mach_timebase_info");
}
return -1;
}
-
- *pnumer = (_PyTime_t)timebase.numer;
- *pdenom = (_PyTime_t)timebase.denom;
return 0;
}
#endif
+// N.B. If raise_exc=0, this may be called without the GIL.
static int
-py_get_monotonic_clock(_PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
+py_get_monotonic_clock(PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
{
assert(info == NULL || raise_exc);
-
-#if defined(MS_WINDOWS)
- ULONGLONG ticks = GetTickCount64();
- static_assert(sizeof(ticks) <= sizeof(_PyTime_t),
- "ULONGLONG is larger than _PyTime_t");
- _PyTime_t t;
- if (ticks <= (ULONGLONG)_PyTime_MAX) {
- t = (_PyTime_t)ticks;
- }
- else {
- // GetTickCount64() maximum is larger than _PyTime_t maximum:
- // ULONGLONG is unsigned, whereas _PyTime_t is signed.
- t = _PyTime_MAX;
+ if (raise_exc) {
+ // raise_exc requires to hold the GIL
+ assert(PyGILState_Check());
}
- int res = pytime_mul(&t, MS_TO_NS);
- *tp = t;
-
- if (raise_exc && res < 0) {
- pytime_overflow();
+#if defined(MS_WINDOWS)
+ if (py_get_win_perf_counter(tp, info, raise_exc) < 0) {
return -1;
}
-
- if (info) {
- DWORD timeAdjustment, timeIncrement;
- BOOL isTimeAdjustmentDisabled, ok;
- info->implementation = "GetTickCount64()";
- info->monotonic = 1;
- ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
- &isTimeAdjustmentDisabled);
- if (!ok) {
- PyErr_SetFromWindowsErr(0);
- return -1;
- }
- info->resolution = timeIncrement * 1e-7;
- info->adjustable = 0;
- }
-
#elif defined(__APPLE__)
- static _PyTime_t timebase_numer = 0;
- static _PyTime_t timebase_denom = 0;
- if (timebase_denom == 0) {
- if (py_mach_timebase_info(&timebase_numer, &timebase_denom, raise_exc) < 0) {
+ static _PyTimeFraction base = {0, 0};
+ if (base.denom == 0) {
+ if (py_mach_timebase_info(&base, raise_exc) < 0) {
return -1;
}
}
if (info) {
info->implementation = "mach_absolute_time()";
- info->resolution = (double)timebase_numer / (double)timebase_denom * 1e-9;
+ info->resolution = _PyTimeFraction_Resolution(&base);
info->monotonic = 1;
info->adjustable = 0;
}
uint64_t uticks = mach_absolute_time();
// unsigned => signed
- assert(uticks <= (uint64_t)_PyTime_MAX);
- _PyTime_t ticks = (_PyTime_t)uticks;
+ assert(uticks <= (uint64_t)PyTime_MAX);
+ PyTime_t ticks = (PyTime_t)uticks;
- _PyTime_t ns = _PyTime_MulDiv(ticks, timebase_numer, timebase_denom);
- *tp = pytime_from_nanoseconds(ns);
+ PyTime_t ns = _PyTimeFraction_Mul(ticks, &base);
+ *tp = ns;
#elif defined(__hpux)
- hrtime_t time;
-
- time = gethrtime();
+ hrtime_t time = gethrtime();
if (time == -1) {
if (raise_exc) {
PyErr_SetFromErrno(PyExc_OSError);
@@ -1127,7 +1184,7 @@ py_get_monotonic_clock(_PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
return -1;
}
- *tp = pytime_from_nanoseconds(time);
+ *tp = time;
if (info) {
info->implementation = "gethrtime()";
@@ -1175,127 +1232,53 @@ py_get_monotonic_clock(_PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
}
-_PyTime_t
-_PyTime_GetMonotonicClock(void)
+int
+PyTime_Monotonic(PyTime_t *result)
{
- _PyTime_t t;
- if (py_get_monotonic_clock(&t, NULL, 0) < 0) {
- // If mach_timebase_info(), clock_gettime() or gethrtime() fails:
- // silently ignore the failure and return 0.
- t = 0;
+ if (py_get_monotonic_clock(result, NULL, 1) < 0) {
+ *result = 0;
+ return -1;
}
- return t;
+ return 0;
}
int
-_PyTime_GetMonotonicClockWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
-{
- return py_get_monotonic_clock(tp, info, 1);
-}
-
-
-#ifdef MS_WINDOWS
-static int
-py_win_perf_counter_frequency(LONGLONG *pfrequency, int raise)
+PyTime_MonotonicRaw(PyTime_t *result)
{
- LONGLONG frequency;
-
- LARGE_INTEGER freq;
- // Since Windows XP, the function cannot fail.
- (void)QueryPerformanceFrequency(&freq);
- frequency = freq.QuadPart;
-
- // Since Windows XP, frequency cannot be zero.
- assert(frequency >= 1);
-
- /* Make also sure that (ticks * SEC_TO_NS) cannot overflow in
- _PyTime_MulDiv(), with ticks < frequency.
-
- Known QueryPerformanceFrequency() values:
-
- * 10,000,000 (10 MHz): 100 ns resolution
- * 3,579,545 Hz (3.6 MHz): 279 ns resolution
-
- None of these frequencies can overflow with 64-bit _PyTime_t, but
- check for integer overflow just in case. */
- if (frequency > _PyTime_MAX / SEC_TO_NS) {
- if (raise) {
- PyErr_SetString(PyExc_OverflowError,
- "QueryPerformanceFrequency is too large");
- }
+ if (py_get_monotonic_clock(result, NULL, 0) < 0) {
+ *result = 0;
return -1;
}
-
- *pfrequency = frequency;
return 0;
}
-static int
-py_get_win_perf_counter(_PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
+int
+_PyTime_MonotonicWithInfo(PyTime_t *tp, _Py_clock_info_t *info)
{
- assert(info == NULL || raise_exc);
-
- static LONGLONG frequency = 0;
- if (frequency == 0) {
- if (py_win_perf_counter_frequency(&frequency, raise_exc) < 0) {
- return -1;
- }
- }
-
- if (info) {
- info->implementation = "QueryPerformanceCounter()";
- info->resolution = 1.0 / (double)frequency;
- info->monotonic = 1;
- info->adjustable = 0;
- }
-
- LARGE_INTEGER now;
- QueryPerformanceCounter(&now);
- LONGLONG ticksll = now.QuadPart;
+ return py_get_monotonic_clock(tp, info, 1);
+}
- /* Make sure that casting LONGLONG to _PyTime_t cannot overflow,
- both types are signed */
- _PyTime_t ticks;
- static_assert(sizeof(ticksll) <= sizeof(ticks),
- "LONGLONG is larger than _PyTime_t");
- ticks = (_PyTime_t)ticksll;
- _PyTime_t ns = _PyTime_MulDiv(ticks, SEC_TO_NS, (_PyTime_t)frequency);
- *tp = pytime_from_nanoseconds(ns);
- return 0;
+int
+_PyTime_PerfCounterWithInfo(PyTime_t *t, _Py_clock_info_t *info)
+{
+ return _PyTime_MonotonicWithInfo(t, info);
}
-#endif // MS_WINDOWS
int
-_PyTime_GetPerfCounterWithInfo(_PyTime_t *t, _Py_clock_info_t *info)
+PyTime_PerfCounter(PyTime_t *result)
{
-#ifdef MS_WINDOWS
- return py_get_win_perf_counter(t, info, 1);
-#else
- return _PyTime_GetMonotonicClockWithInfo(t, info);
-#endif
+ return PyTime_Monotonic(result);
}
-_PyTime_t
-_PyTime_GetPerfCounter(void)
+int
+PyTime_PerfCounterRaw(PyTime_t *result)
{
- _PyTime_t t;
- int res;
-#ifdef MS_WINDOWS
- res = py_get_win_perf_counter(&t, NULL, 0);
-#else
- res = py_get_monotonic_clock(&t, NULL, 0);
-#endif
- if (res < 0) {
- // If py_win_perf_counter_frequency() or py_get_monotonic_clock()
- // fails: silently ignore the failure and return 0.
- t = 0;
- }
- return t;
+ return PyTime_MonotonicRaw(result);
}
@@ -1366,17 +1349,21 @@ _PyTime_gmtime(time_t t, struct tm *tm)
}
-_PyTime_t
-_PyDeadline_Init(_PyTime_t timeout)
+PyTime_t
+_PyDeadline_Init(PyTime_t timeout)
{
- _PyTime_t now = _PyTime_GetMonotonicClock();
+ PyTime_t now;
+ // silently ignore error: cannot report error to the caller
+ (void)PyTime_MonotonicRaw(&now);
return _PyTime_Add(now, timeout);
}
-_PyTime_t
-_PyDeadline_Get(_PyTime_t deadline)
+PyTime_t
+_PyDeadline_Get(PyTime_t deadline)
{
- _PyTime_t now = _PyTime_GetMonotonicClock();
+ PyTime_t now;
+ // silently ignore error: cannot report error to the caller
+ (void)PyTime_MonotonicRaw(&now);
return deadline - now;
}