Skip to content

Commit ce6f264

Browse files
committed
On macOS >= 10.12, use clock_gettime(CLOCK_MONOTONIC_RAW) for time.monotonic
1 parent ec736e7 commit ce6f264

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

Doc/library/time.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ Functions
295295

296296
* On Windows, call ``QueryPerformanceCounter()`` and
297297
``QueryPerformanceFrequency()``.
298-
* On macOS, call ``mach_absolute_time()`` and ``mach_timebase_info()``.
298+
* On macOS, call ``clock_gettime(CLOCK_MONOTONIC_RAW)`` if available, otherwise call ``mach_absolute_time()`` and ``mach_timebase_info()``.
299299
* On HP-UX, call ``gethrtime()``.
300300
* Call ``clock_gettime(CLOCK_HIGHRES)`` if available.
301301
* Otherwise, call ``clock_gettime(CLOCK_MONOTONIC)``.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
On macOS >= 10.12, use :func:`clock_gettime` with
2+
:data:`CLOCK_MONOTONIC_RAW` for :func:`time.monotonic`. Patch by John Keith
3+
Hohm.

Python/pytime.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# include <winsock2.h> // struct timeval
1313
#endif
1414

15-
#if defined(__APPLE__)
15+
#if defined(__APPLE__) && !defined(CLOCK_MONOTONIC_RAW)
1616
# include <mach/mach_time.h> // mach_absolute_time(), mach_timebase_info()
1717

1818
#if defined(__APPLE__) && defined(__has_builtin)
@@ -933,7 +933,7 @@ py_get_system_clock(PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
933933
struct timespec ts;
934934
#endif
935935

936-
#if !defined(HAVE_CLOCK_GETTIME) || defined(__APPLE__)
936+
#if !defined(HAVE_CLOCK_GETTIME) || (defined(__APPLE__) && !defined(CLOCK_MONOTONIC_RAW))
937937
struct timeval tv;
938938
#endif
939939

@@ -1090,7 +1090,7 @@ py_get_win_perf_counter(PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
10901090
#endif // MS_WINDOWS
10911091

10921092

1093-
#ifdef __APPLE__
1093+
#if defined(__APPLE__) && !defined(CLOCK_MONOTONIC_RAW)
10941094
static PyStatus
10951095
py_mach_timebase_info(_PyTimeFraction *base)
10961096
{
@@ -1125,7 +1125,7 @@ _PyTime_Init(struct _Py_time_runtime_state *state)
11251125
{
11261126
#ifdef MS_WINDOWS
11271127
return py_win_perf_counter_frequency(&state->base);
1128-
#elif defined(__APPLE__)
1128+
#elif defined(__APPLE__) && !defined(CLOCK_MONOTONIC_RAW)
11291129
return py_mach_timebase_info(&state->base);
11301130
#else
11311131
return PyStatus_Ok();
@@ -1146,7 +1146,7 @@ py_get_monotonic_clock(PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
11461146
if (py_get_win_perf_counter(tp, info, raise_exc) < 0) {
11471147
return -1;
11481148
}
1149-
#elif defined(__APPLE__)
1149+
#elif defined(__APPLE__) && !defined(CLOCK_MONOTONIC_RAW)
11501150
if (info) {
11511151
info->implementation = "mach_absolute_time()";
11521152
info->resolution = _PyTimeFraction_Resolution(&_PyRuntime.time.base);
@@ -1185,6 +1185,9 @@ py_get_monotonic_clock(PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
11851185
#ifdef CLOCK_HIGHRES
11861186
const clockid_t clk_id = CLOCK_HIGHRES;
11871187
const char *implementation = "clock_gettime(CLOCK_HIGHRES)";
1188+
#elif defined(__APPLE__) && defined(CLOCK_MONOTONIC_RAW)
1189+
const clockid_t clk_id = CLOCK_MONOTONIC_RAW;
1190+
const char *implementation = "clock_gettime(CLOCK_MONOTONIC_RAW)";
11881191
#else
11891192
const clockid_t clk_id = CLOCK_MONOTONIC;
11901193
const char *implementation = "clock_gettime(CLOCK_MONOTONIC)";

0 commit comments

Comments
 (0)