Fix time.steady(strict=True): don't use CLOCK_REALTIME

This commit is contained in:
Victor Stinner 2012-03-26 22:53:14 +02:00
parent 3c1e48176e
commit 8486076dd5
1 changed files with 28 additions and 7 deletions

View File

@ -779,26 +779,47 @@ steady_clock(int strict)
return PyFloat_FromDouble(secs); return PyFloat_FromDouble(secs);
#elif defined(HAVE_CLOCK_GETTIME) #elif defined(HAVE_CLOCK_GETTIME)
static int clk_index = 0; static int steady_clk_index = 0;
clockid_t clk_ids[] = { static int monotonic_clk_index = 0;
int *clk_index;
clockid_t steady_clk_ids[] = {
#ifdef CLOCK_MONOTONIC_RAW #ifdef CLOCK_MONOTONIC_RAW
CLOCK_MONOTONIC_RAW, CLOCK_MONOTONIC_RAW,
#endif #endif
CLOCK_MONOTONIC, CLOCK_MONOTONIC,
CLOCK_REALTIME CLOCK_REALTIME
}; };
clockid_t monotonic_clk_ids[] = {
#ifdef CLOCK_MONOTONIC_RAW
CLOCK_MONOTONIC_RAW,
#endif
CLOCK_MONOTONIC
};
clockid_t *clk_ids;
int clk_ids_len;
int ret; int ret;
struct timespec tp; struct timespec tp;
while (0 <= clk_index) { if (strict) {
clockid_t clk_id = clk_ids[clk_index]; clk_index = &monotonic_clk_index;
clk_ids = monotonic_clk_ids;
clk_ids_len = Py_ARRAY_LENGTH(monotonic_clk_ids);
}
else {
clk_index = &steady_clk_index;
clk_ids = steady_clk_ids;
clk_ids_len = Py_ARRAY_LENGTH(steady_clk_ids);
}
while (0 <= *clk_index) {
clockid_t clk_id = clk_ids[*clk_index];
ret = clock_gettime(clk_id, &tp); ret = clock_gettime(clk_id, &tp);
if (ret == 0) if (ret == 0)
return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9); return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9);
clk_index++; (*clk_index)++;
if (Py_ARRAY_LENGTH(clk_ids) <= clk_index) if (clk_ids_len <= *clk_index)
clk_index = -1; (*clk_index) = -1;
} }
if (strict) { if (strict) {
PyErr_SetFromErrno(PyExc_OSError); PyErr_SetFromErrno(PyExc_OSError);