Skip to content

Commit

Permalink
[libc++] Synchronize clock selection between chrono.cpp and filesyste…
Browse files Browse the repository at this point in the history
…m_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
  • Loading branch information
ldionne committed Jul 5, 2023
1 parent 6d64faf commit 5f1ba3a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
6 changes: 3 additions & 3 deletions libcxx/src/chrono.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 10 additions & 2 deletions libcxx/src/filesystem/filesystem_clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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))
Expand Down

0 comments on commit 5f1ba3a

Please sign in to comment.