Restrict use of CLOCK_MONOTONIC clock sources to Linux
And document the reason behind this to avoid future bugs.
diff --git a/src/shims/time.h b/src/shims/time.h
index 70af03d..6cc5a16 100644
--- a/src/shims/time.h
+++ b/src/shims/time.h
@@ -121,6 +121,19 @@
#endif
}
+/* On the use of clock sources in the CLOCK_MONOTONIC family
+ *
+ * The code below requires monotonic clock sources that only tick
+ * while the machine is running.
+ *
+ * Per POSIX, the CLOCK_MONOTONIC family is supposed to tick during
+ * machine sleep; this is not the case on Linux, and that behavior
+ * became part of the Linux ABI.
+ *
+ * Using the CLOCK_MONOTONIC family on POSIX-compliant platforms
+ * will lead to bugs, hence its use is restricted to Linux.
+ */
+
static inline uint64_t
_dispatch_absolute_time(void)
{
@@ -130,7 +143,7 @@
struct timespec ts;
dispatch_assume_zero(clock_gettime(CLOCK_UPTIME, &ts));
return _dispatch_timespec_to_nano(ts);
-#elif HAVE_DECL_CLOCK_MONOTONIC
+#elif HAVE_DECL_CLOCK_MONOTONIC && defined(__linux__)
struct timespec ts;
dispatch_assume_zero(clock_gettime(CLOCK_MONOTONIC, &ts));
return _dispatch_timespec_to_nano(ts);
@@ -152,7 +165,7 @@
struct timespec ts;
dispatch_assume_zero(clock_gettime(CLOCK_UPTIME_FAST, &ts));
return _dispatch_timespec_to_nano(ts);
-#elif HAVE_DECL_CLOCK_MONOTONIC_COARSE
+#elif HAVE_DECL_CLOCK_MONOTONIC_COARSE && defined(__linux__)
struct timespec ts;
dispatch_assume_zero(clock_gettime(CLOCK_MONOTONIC_COARSE, &ts));
return _dispatch_timespec_to_nano(ts);