Skip to content

Commit 58b23b8

Browse files
committed
Restrict use of CLOCK_MONOTONIC clock sources to Linux
And document the reason behind this to avoid future bugs.
1 parent 2eaa7d3 commit 58b23b8

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/shims/time.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,19 @@ _dispatch_get_nanoseconds(void)
121121
#endif
122122
}
123123

124+
/* On the use of clock sources in the CLOCK_MONOTONIC family
125+
*
126+
* The code below requires monotonic clock sources that only tick
127+
* while the machine is running.
128+
*
129+
* Per POSIX, the CLOCK_MONOTONIC family is supposed to tick during
130+
* machine sleep; this is not the case on Linux, and that behavior
131+
* became part of the Linux ABI.
132+
*
133+
* Using the CLOCK_MONOTONIC family on POSIX-compliant platforms
134+
* will lead to bugs, hence its use is restricted to Linux.
135+
*/
136+
124137
static inline uint64_t
125138
_dispatch_absolute_time(void)
126139
{
@@ -130,7 +143,7 @@ _dispatch_absolute_time(void)
130143
struct timespec ts;
131144
dispatch_assume_zero(clock_gettime(CLOCK_UPTIME, &ts));
132145
return _dispatch_timespec_to_nano(ts);
133-
#elif HAVE_DECL_CLOCK_MONOTONIC
146+
#elif HAVE_DECL_CLOCK_MONOTONIC && defined(__linux__)
134147
struct timespec ts;
135148
dispatch_assume_zero(clock_gettime(CLOCK_MONOTONIC, &ts));
136149
return _dispatch_timespec_to_nano(ts);
@@ -152,7 +165,7 @@ _dispatch_approximate_time(void)
152165
struct timespec ts;
153166
dispatch_assume_zero(clock_gettime(CLOCK_UPTIME_FAST, &ts));
154167
return _dispatch_timespec_to_nano(ts);
155-
#elif HAVE_DECL_CLOCK_MONOTONIC_COARSE
168+
#elif HAVE_DECL_CLOCK_MONOTONIC_COARSE && defined(__linux__)
156169
struct timespec ts;
157170
dispatch_assume_zero(clock_gettime(CLOCK_MONOTONIC_COARSE, &ts));
158171
return _dispatch_timespec_to_nano(ts);

0 commit comments

Comments
 (0)