[PATCH] D27429: [Chrono][Darwin] On Darwin use CLOCK_UPTIME_RAW instead of CLOCK_MONOTONIC

Bruno Cardoso Lopes via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 5 14:36:46 PST 2016


bruno created this revision.
bruno added reviewers: mclow.lists, dexonsmith, EricWF.
bruno added a subscriber: cfe-commits.

CLOCK_MONOTONIC is only defined on Darwin on libc versions >= 1133 and its behaviour differs from Linux. CLOCK_UPTIME on Darwin actually matches
CLOCK_MONOTONIC on Linux, due to historical coincidence (Linux doesn't match POSIX here but Darwin does). Use CLOCK_UPTIME_RAW on Darwin since the _RAW version gives nanosecond precision and is lower overhead.


https://reviews.llvm.org/D27429

Files:
  src/chrono.cpp


Index: src/chrono.cpp
===================================================================
--- src/chrono.cpp
+++ src/chrono.cpp
@@ -75,8 +75,19 @@
 steady_clock::now() _NOEXCEPT
 {
     struct timespec tp;
+#if defined(__APPLE__) && defined(CLOCK_UPTIME_RAW)
+    // CLOCK_MONOTONIC is only defined on Darwin on libc versions >= 1133 and
+    // its behaviour differs from Linux.
+    // CLOCK_UPTIME on Darwin actually matches CLOCK_MONOTONIC on Linux, due to
+    // historical coincidence (Linux doesn't match POSIX here but Darwin does).
+    // Use CLOCK_UPTIME_RAW on Darwin since the _RAW version gives nanosecond
+    // precision and is lower overhead.
+    if (0 != clock_gettime(CLOCK_UPTIME_RAW, &tp))
+        __throw_system_error(errno, "clock_gettime(CLOCK_UPTIME_RAW) failed");
+#else
     if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
         __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
+#endif
     return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D27429.80328.patch
Type: text/x-patch
Size: 1014 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20161205/bd920c76/attachment.bin>


More information about the cfe-commits mailing list