[libcxx-commits] [PATCH] D155022: [libc++] Fix clock selection in chrono.cpp and filesystem_clock.cpp

Louis Dionne via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jul 11 15:09:14 PDT 2023


ldionne created this revision.
Herald added a project: All.
ldionne requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

This patch partly reverts the change that was made in 5f1ba3a502 <https://reviews.llvm.org/rG5f1ba3a5020acdac4a7368f19b074577b5a6da8c>
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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155022

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


Index: libcxx/src/filesystem/filesystem_clock.cpp
===================================================================
--- libcxx/src/filesystem/filesystem_clock.cpp
+++ libcxx/src/filesystem/filesystem_clock.cpp
@@ -29,8 +29,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
@@ -46,7 +46,7 @@
   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))
@@ -58,7 +58,7 @@
   timeval tv;
   gettimeofday(&tv, 0);
   return time_point(__secs(tv.tv_sec) + __microsecs(tv.tv_usec));
-#endif // CLOCK_REALTIME
+#endif
 }
 
 _LIBCPP_END_NAMESPACE_FILESYSTEM
Index: libcxx/src/chrono.cpp
===================================================================
--- libcxx/src/chrono.cpp
+++ libcxx/src/chrono.cpp
@@ -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 @@
   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 @@
   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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D155022.539307.patch
Type: text/x-patch
Size: 2302 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20230711/190982ec/attachment-0001.bin>


More information about the libcxx-commits mailing list