[libcxx-commits] [libcxx] ce167c6 - [libcxx] Use Fuchsia-native monotonic clock for std::chrono::steady_clock

Roland McGrath via libcxx-commits libcxx-commits at lists.llvm.org
Mon Jan 10 13:15:01 PST 2022


Author: Roland McGrath
Date: 2022-01-10T13:14:50-08:00
New Revision: ce167c6fb2ae67cffa6702b869762fb80b62d3bc

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

LOG: [libcxx] Use Fuchsia-native monotonic clock for std::chrono::steady_clock

Use the zx_clock_get_monotonic system call directly rather than
going through the POSIX clock_gettime function.  The libc function
is a trivial wrapper around the system call, and is not a standard C
function.  Avoiding it reduces the Fuchsia libc ABI surface that
libc++ depends on.

Reviewed By: phosek, ldionne, #libc

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

Added: 
    

Modified: 
    libcxx/src/chrono.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/src/chrono.cpp b/libcxx/src/chrono.cpp
index 5aa7af75894b8..4f2d51042ff10 100644
--- a/libcxx/src/chrono.cpp
+++ b/libcxx/src/chrono.cpp
@@ -44,6 +44,10 @@
 #  endif
 #endif // defined(_LIBCPP_WIN32API)
 
+#if defined(__Fuchsia__)
+#  include <zircon/syscalls.h>
+#endif
+
 #if __has_include(<mach/mach_time.h>)
 # include <mach/mach_time.h>
 #endif
@@ -266,7 +270,18 @@ static steady_clock::time_point __libcpp_steady_clock_now() {
   return steady_clock::time_point(seconds(ts.tv_sec) + nanoseconds(ts.tv_nsec));
 }
 
-#elif defined(CLOCK_MONOTONIC)
+#  elif defined(__Fuchsia__)
+
+static steady_clock::time_point __libcpp_steady_clock_now() noexcept {
+  // Implicitly link against the vDSO system call ABI without
+  // requiring the final link to specify -lzircon explicitly when
+  // statically linking libc++.
+#    pragma comment(lib, "zircon")
+
+  return steady_clock::time_point(nanoseconds(_zx_clock_get_monotonic()));
+}
+
+#  elif defined(CLOCK_MONOTONIC)
 
 static steady_clock::time_point __libcpp_steady_clock_now() {
     struct timespec tp;
@@ -275,9 +290,9 @@ static steady_clock::time_point __libcpp_steady_clock_now() {
     return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
 }
 
-#else
-#   error "Monotonic clock not implemented on this platform"
-#endif
+#  else
+#    error "Monotonic clock not implemented on this platform"
+#  endif
 
 const bool steady_clock::is_steady;
 


        


More information about the libcxx-commits mailing list