[libc++] Fix clock selection in chrono.cpp and filesystem_clock.cpp
authorLouis Dionne <ldionne.2@gmail.com>
Tue, 11 Jul 2023 21:54:45 +0000 (17:54 -0400)
committerLouis Dionne <ldionne.2@gmail.com>
Wed, 12 Jul 2023 22:07:21 +0000 (18:07 -0400)
This patch partly reverts the change that was made in 5f1ba3a502
regarding the clock selection on Apple platforms. It turns out that
gettimeofday() is marked as obsolete by POSIX and clock_gettime() is
recommended instead. Since both are equivalent for our purposes,
prefer using clock_gettime() even on Apple platforms.

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

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

index 415988b..0990d8d 100644 (file)
@@ -31,8 +31,8 @@
 # include <sys/time.h> // for gettimeofday and timeval
 #endif
 
-#if !defined(__APPLE__) && defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0
-# define _LIBCPP_USE_CLOCK_GETTIME
+#if defined(__APPLE__) || (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0)
+# define _LIBCPP_HAS_CLOCK_GETTIME
 #endif
 
 #if defined(_LIBCPP_WIN32API)
@@ -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(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_REALTIME)
+#elif defined(_LIBCPP_HAS_CLOCK_GETTIME)
 
 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(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
+#  elif defined(_LIBCPP_HAS_CLOCK_GETTIME)
 
 static steady_clock::time_point __libcpp_steady_clock_now() {
     struct timespec tp;
index beecd5d..9ea070b 100644 (file)
@@ -27,8 +27,8 @@
 # include <sys/time.h> // for gettimeofday and timeval
 #endif
 
-#if !defined(__APPLE__) && defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0
-# define _LIBCPP_USE_CLOCK_GETTIME
+#if defined(__APPLE__) || (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0)
+# define _LIBCPP_HAS_CLOCK_GETTIME
 #endif
 
 _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
@@ -44,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(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_REALTIME)
+#elif defined(_LIBCPP_HAS_CLOCK_GETTIME)
   typedef chrono::duration<rep, nano> __nsecs;
   struct timespec tp;
   if (0 != clock_gettime(CLOCK_REALTIME, &tp))
@@ -56,7 +56,7 @@ _FilesystemClock::time_point _FilesystemClock::now() noexcept {
   timeval tv;
   gettimeofday(&tv, 0);
   return time_point(__secs(tv.tv_sec) + __microsecs(tv.tv_usec));
-#endif // CLOCK_REALTIME
+#endif
 }
 
 _LIBCPP_END_NAMESPACE_FILESYSTEM