summaryrefslogtreecommitdiffstats
path: root/Modules/timemodule.c
diff options
context:
space:
mode:
Diffstat (limited to 'Modules/timemodule.c')
-rw-r--r--Modules/timemodule.c80
1 files changed, 23 insertions, 57 deletions
diff --git a/Modules/timemodule.c b/Modules/timemodule.c
index d0917a4..c90d8f9 100644
--- a/Modules/timemodule.c
+++ b/Modules/timemodule.c
@@ -96,8 +96,8 @@ floatclock(_Py_clock_info_t *info)
#define WIN32_PERF_COUNTER
/* Win32 has better clock replacement; we have our own version, due to Mark
Hammond and Tim Peters */
-static int
-win_perf_counter(_Py_clock_info_t *info, PyObject **result)
+static PyObject*
+win_perf_counter(_Py_clock_info_t *info)
{
static LONGLONG cpu_frequency = 0;
static LONGLONG ctrStart;
@@ -109,10 +109,8 @@ win_perf_counter(_Py_clock_info_t *info, PyObject **result)
QueryPerformanceCounter(&now);
ctrStart = now.QuadPart;
if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) {
- /* Unlikely to happen - this works on all intel
- machines at least! Revert to clock() */
- *result = NULL;
- return -1;
+ PyErr_SetFromWindowsErr(0);
+ return NULL;
}
cpu_frequency = freq.QuadPart;
}
@@ -124,8 +122,7 @@ win_perf_counter(_Py_clock_info_t *info, PyObject **result)
info->monotonic = 1;
info->adjustable = 0;
}
- *result = PyFloat_FromDouble(diff / (double)cpu_frequency);
- return 0;
+ return PyFloat_FromDouble(diff / (double)cpu_frequency);
}
#endif
@@ -135,11 +132,10 @@ static PyObject*
pyclock(_Py_clock_info_t *info)
{
#ifdef WIN32_PERF_COUNTER
- PyObject *res;
- if (win_perf_counter(info, &res) == 0)
- return res;
-#endif
+ return win_perf_counter(info);
+#else
return floatclock(info);
+#endif
}
static PyObject *
@@ -169,7 +165,7 @@ time_clock_gettime(PyObject *self, PyObject *args)
ret = clock_gettime((clockid_t)clk_id, &tp);
if (ret != 0) {
- PyErr_SetFromErrno(PyExc_IOError);
+ PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9);
@@ -200,7 +196,7 @@ time_clock_settime(PyObject *self, PyObject *args)
ret = clock_settime((clockid_t)clk_id, &tp);
if (ret != 0) {
- PyErr_SetFromErrno(PyExc_IOError);
+ PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
Py_RETURN_NONE;
@@ -223,7 +219,7 @@ time_clock_getres(PyObject *self, PyObject *args)
ret = clock_getres((clockid_t)clk_id, &tp);
if (ret != 0) {
- PyErr_SetFromErrno(PyExc_IOError);
+ PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
@@ -1036,35 +1032,24 @@ Monotonic clock, cannot go backward.");
static PyObject*
perf_counter(_Py_clock_info_t *info)
{
-#if defined(WIN32_PERF_COUNTER) || defined(PYMONOTONIC)
- PyObject *res;
-#endif
-#if defined(WIN32_PERF_COUNTER)
- static int use_perf_counter = 1;
-#endif
-#ifdef PYMONOTONIC
- static int use_monotonic = 1;
-#endif
-
#ifdef WIN32_PERF_COUNTER
- if (use_perf_counter) {
- if (win_perf_counter(info, &res) == 0)
- return res;
- use_perf_counter = 0;
- }
-#endif
+ return win_perf_counter(info);
+#else
#ifdef PYMONOTONIC
+ static int use_monotonic = 1;
+
if (use_monotonic) {
- res = pymonotonic(info);
+ PyObject *res = pymonotonic(info);
if (res != NULL)
return res;
use_monotonic = 0;
PyErr_Clear();
}
#endif
-
return floattime(info);
+
+#endif
}
static PyObject *
@@ -1535,29 +1520,10 @@ static PyObject*
floattime(_Py_clock_info_t *info)
{
_PyTime_timeval t;
-#ifdef HAVE_CLOCK_GETTIME
- struct timespec tp;
- int ret;
-
- /* _PyTime_gettimeofday() does not use clock_gettime()
- because it would require to link Python to the rt (real-time)
- library, at least on Linux */
- ret = clock_gettime(CLOCK_REALTIME, &tp);
- if (ret == 0) {
- if (info) {
- struct timespec res;
- info->implementation = "clock_gettime(CLOCK_REALTIME)";
- info->monotonic = 0;
- info->adjustable = 1;
- if (clock_getres(CLOCK_REALTIME, &res) == 0)
- info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
- else
- info->resolution = 1e-9;
- }
- return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9);
+ if (_PyTime_gettimeofday_info(&t, info) < 0) {
+ assert(info != NULL);
+ return NULL;
}
-#endif
- _PyTime_gettimeofday_info(&t, info);
return PyFloat_FromDouble((double)t.tv_sec + t.tv_usec * 1e-6);
}
@@ -1591,7 +1557,7 @@ floatsleep(double secs)
else
#endif
{
- PyErr_SetFromErrno(PyExc_IOError);
+ PyErr_SetFromErrno(PyExc_OSError);
return -1;
}
}
@@ -1625,7 +1591,7 @@ floatsleep(double secs)
if (rc == WAIT_OBJECT_0) {
Py_BLOCK_THREADS
errno = EINTR;
- PyErr_SetFromErrno(PyExc_IOError);
+ PyErr_SetFromErrno(PyExc_OSError);
return -1;
}
}