[libcxx-commits] [libcxx] 8bec892 - [libc++][Apple] Use CLOCK_MONOTONIC_RAW instead of CLOCK_UPTIME_RAW for steady_clock

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Wed Feb 12 07:43:57 PST 2020


Author: Louis Dionne
Date: 2020-02-12T16:43:36+01:00
New Revision: 8bec8927134f116eb11ddea9db78b1a6241884c9

URL: https://github.com/llvm/llvm-project/commit/8bec8927134f116eb11ddea9db78b1a6241884c9
DIFF: https://github.com/llvm/llvm-project/commit/8bec8927134f116eb11ddea9db78b1a6241884c9.diff

LOG: [libc++][Apple] Use CLOCK_MONOTONIC_RAW instead of CLOCK_UPTIME_RAW for steady_clock

Summary:
In D27429, we switched the Apple implementation of steady_clock::now()
from clock_gettime(CLOCK_MONOTONIC) to clock_gettime(CLOCK_UPTIME_RAW).
The purpose was to get nanosecond precision, and also to improve the
performance of the implementation.

However, it appears that CLOCK_UPTIME_RAW does not satisfy the requirements
of the Standard, since it is not strictly speaking monotonic. Indeed, the
clock does not increment while the system is asleep, which had been
mentioned in D27429 but somehow not addressed.

This patch switches to CLOCK_MONOTONIC_RAW, which is monotonic, increased
during sleep, and also has nanosecond precision.

https://llvm.org/PR44773

Reviewers: bruno, howard.hinnant, EricWF

Subscribers: christof, jkorous, dexonsmith, libcxx-commits, mclow.lists, EricWF

Tags: #libc

Differential Revision: https://reviews.llvm.org/D74341

Added: 
    

Modified: 
    libcxx/src/chrono.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/src/chrono.cpp b/libcxx/src/chrono.cpp
index 9d448b6a985b..2d5f172aff7f 100644
--- a/libcxx/src/chrono.cpp
+++ b/libcxx/src/chrono.cpp
@@ -114,14 +114,14 @@ const bool steady_clock::is_steady;
 
 #if defined(__APPLE__)
 
-// Darwin libc versions >= 1133 provide ns precision via CLOCK_UPTIME_RAW
-#if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_UPTIME_RAW)
+// Darwin libc versions >= 1133 provide ns precision via CLOCK_MONOTONIC_RAW
+#if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC_RAW)
 steady_clock::time_point
 steady_clock::now() _NOEXCEPT
 {
     struct timespec tp;
-    if (0 != clock_gettime(CLOCK_UPTIME_RAW, &tp))
-        __throw_system_error(errno, "clock_gettime(CLOCK_UPTIME_RAW) failed");
+    if (0 != clock_gettime(CLOCK_MONOTONIC_RAW, &tp))
+        __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC_RAW) failed");
     return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
 }
 
@@ -177,7 +177,7 @@ steady_clock::now() _NOEXCEPT
     static FP fp = init_steady_clock();
     return time_point(duration(fp()));
 }
-#endif // defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_UPTIME_RAW)
+#endif // defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC_RAW)
 
 #elif defined(_LIBCPP_WIN32API)
 
@@ -206,9 +206,9 @@ steady_clock::now() _NOEXCEPT
 
 #elif defined(CLOCK_MONOTONIC)
 
-// On Apple platforms only CLOCK_UPTIME_RAW or mach_absolute_time are able to
-// time functions in the nanosecond range. Thus, they are the only acceptable
-// implementations of steady_clock.
+// On Apple platforms only CLOCK_UPTIME_RAW, CLOCK_MONOTONIC_RAW or
+// mach_absolute_time are able to time functions in the nanosecond range.
+// Thus, they are the only acceptable implementations of steady_clock.
 #ifdef __APPLE__
 #error "Never use CLOCK_MONOTONIC for steady_clock::now on Apple platforms"
 #endif


        


More information about the libcxx-commits mailing list