[libcxx-commits] [libcxx] 5f1ba3a - [libc++] Synchronize clock selection between chrono.cpp and filesystem_clock.cpp

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Wed Jul 5 05:38:24 PDT 2023


Author: Louis Dionne
Date: 2023-07-05T08:38:20-04:00
New Revision: 5f1ba3a5020acdac4a7368f19b074577b5a6da8c

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

LOG: [libc++] Synchronize clock selection between chrono.cpp and filesystem_clock.cpp

Note that _FilesystemClock will now be implemented by calling gettimeofday()
on Apple platforms instead of clock_gettime(). However, since both are
equivalent, this should not change the behavior on Apple platforms.
There should be no behavior change on other platforms.

In addition to being a consistency clean up, this fixes some issues seen
by folks as reported in https://reviews.llvm.org/D154390#4471924.

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

Added: 
    

Modified: 
    libcxx/src/chrono.cpp
    libcxx/src/filesystem/filesystem_clock.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/src/chrono.cpp b/libcxx/src/chrono.cpp
index 213f888a5c9b54..415988b2314d7f 100644
--- a/libcxx/src/chrono.cpp
+++ b/libcxx/src/chrono.cpp
@@ -24,7 +24,7 @@
 #include "include/apple_availability.h"
 
 #if __has_include(<unistd.h>)
-# include <unistd.h>
+# include <unistd.h> // _POSIX_TIMERS
 #endif
 
 #if __has_include(<sys/time.h>)
@@ -116,7 +116,7 @@ static system_clock::time_point __libcpp_system_clock_now() {
   return system_clock::time_point(duration_cast<system_clock::duration>(d - nt_to_unix_epoch));
 }
 
-#elif defined(CLOCK_REALTIME) && defined(_LIBCPP_USE_CLOCK_GETTIME)
+#elif defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_REALTIME)
 
 static system_clock::time_point __libcpp_system_clock_now() {
   struct timespec tp;
@@ -226,7 +226,7 @@ static steady_clock::time_point __libcpp_steady_clock_now() noexcept {
   return steady_clock::time_point(nanoseconds(_zx_clock_get_monotonic()));
 }
 
-#  elif defined(CLOCK_MONOTONIC)
+#  elif defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
 
 static steady_clock::time_point __libcpp_steady_clock_now() {
     struct timespec tp;

diff  --git a/libcxx/src/filesystem/filesystem_clock.cpp b/libcxx/src/filesystem/filesystem_clock.cpp
index 86045e69370cd6..beecd5d4a5ea35 100644
--- a/libcxx/src/filesystem/filesystem_clock.cpp
+++ b/libcxx/src/filesystem/filesystem_clock.cpp
@@ -19,10 +19,18 @@
 # include <windows.h>
 #endif
 
-#if !defined(CLOCK_REALTIME) && !defined(_LIBCPP_WIN32API)
+#if __has_include(<unistd.h>)
+# include <unistd.h> // _POSIX_TIMERS
+#endif
+
+#if __has_include(<sys/time.h>)
 # include <sys/time.h> // for gettimeofday and timeval
 #endif
 
+#if !defined(__APPLE__) && defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0
+# define _LIBCPP_USE_CLOCK_GETTIME
+#endif
+
 _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
 
 const bool _FilesystemClock::is_steady;
@@ -36,7 +44,7 @@ _FilesystemClock::time_point _FilesystemClock::now() noexcept {
   detail::TimeSpec tp = detail::filetime_to_timespec(time);
   return time_point(__secs(tp.tv_sec) +
                     chrono::duration_cast<duration>(__nsecs(tp.tv_nsec)));
-#elif defined(CLOCK_REALTIME)
+#elif defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_REALTIME)
   typedef chrono::duration<rep, nano> __nsecs;
   struct timespec tp;
   if (0 != clock_gettime(CLOCK_REALTIME, &tp))


        


More information about the libcxx-commits mailing list