[libcxx-commits] [PATCH] D154457: [libc++] Synchronize clock selection between chrono.cpp and filesystem_clock.cpp

Louis Dionne via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jul 4 11:55:50 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++.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154457

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
@@ -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 @@
   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))
Index: libcxx/src/chrono.cpp
===================================================================
--- libcxx/src/chrono.cpp
+++ 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 @@
   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 @@
   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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154457.537146.patch
Type: text/x-patch
Size: 2106 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20230704/e54519d7/attachment.bin>


More information about the libcxx-commits mailing list