From 5f1ba3a5020acdac4a7368f19b074577b5a6da8c Mon Sep 17 00:00:00 2001 From: Louis Dionne Date: Tue, 4 Jul 2023 14:40:27 -0400 Subject: [PATCH] [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 --- libcxx/src/chrono.cpp | 6 +++--- libcxx/src/filesystem/filesystem_clock.cpp | 12 ++++++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/libcxx/src/chrono.cpp b/libcxx/src/chrono.cpp index 213f888..415988b 100644 --- a/libcxx/src/chrono.cpp +++ b/libcxx/src/chrono.cpp @@ -24,7 +24,7 @@ #include "include/apple_availability.h" #if __has_include() -# include +# include // _POSIX_TIMERS #endif #if __has_include() @@ -116,7 +116,7 @@ static system_clock::time_point __libcpp_system_clock_now() { return system_clock::time_point(duration_cast(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 86045e69..beecd5d 100644 --- a/libcxx/src/filesystem/filesystem_clock.cpp +++ b/libcxx/src/filesystem/filesystem_clock.cpp @@ -19,10 +19,18 @@ # include #endif -#if !defined(CLOCK_REALTIME) && !defined(_LIBCPP_WIN32API) +#if __has_include() +# include // _POSIX_TIMERS +#endif + +#if __has_include() # include // 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(__nsecs(tp.tv_nsec))); -#elif defined(CLOCK_REALTIME) +#elif defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_REALTIME) typedef chrono::duration __nsecs; struct timespec tp; if (0 != clock_gettime(CLOCK_REALTIME, &tp)) -- 2.7.4