Skip to content

[libcxx] Support for using timespec_get #117362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions clang/cmake/caches/Fuchsia-stage2.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ foreach(target armv6m-none-eabi;armv7m-none-eabi;armv8m.main-none-eabi;armv8.1m.
foreach(lang C;CXX;ASM)
# TODO: The preprocessor defines workaround various issues in libc and libc++ integration.
# These should be addressed and removed over time.
set(RUNTIMES_${target}_CMAKE_${lang}_local_flags "--target=${target} -Wno-atomic-alignment \"-Dvfprintf(stream, format, vlist)=vprintf(format, vlist)\" \"-Dfprintf(stream, format, ...)=printf(format)\" \"-Dgettimeofday(tv, tz)\" -D_LIBCPP_PRINT=1")
set(RUNTIMES_${target}_CMAKE_${lang}_local_flags "--target=${target} -Wno-atomic-alignment \"-Dvfprintf(stream, format, vlist)=vprintf(format, vlist)\" \"-Dfprintf(stream, format, ...)=printf(format)\" -D_LIBCPP_PRINT=1")
if(NOT ${target} STREQUAL "aarch64-none-elf")
set(RUNTIMES_${target}_CMAKE_${lang}_local_flags "${RUNTIMES_${target}_CMAKE_${lang}_local_flags} -mthumb")
endif()
Expand Down Expand Up @@ -394,7 +394,7 @@ foreach(target riscv32-unknown-elf)
foreach(lang C;CXX;ASM)
# TODO: The preprocessor defines workaround various issues in libc and libc++ integration.
# These should be addressed and removed over time.
set(RUNTIMES_${target}_CMAKE_${lang}_FLAGS "--target=${target} -march=rv32imafc -mabi=ilp32f -Wno-atomic-alignment \"-Dvfprintf(stream, format, vlist)=vprintf(format, vlist)\" \"-Dfprintf(stream, format, ...)=printf(format)\" \"-Dgettimeofday(tv, tz)\" -D_LIBCPP_PRINT=1" CACHE STRING "")
set(RUNTIMES_${target}_CMAKE_${lang}_FLAGS "--target=${target} -march=rv32imafc -mabi=ilp32f -Wno-atomic-alignment \"-Dvfprintf(stream, format, vlist)=vprintf(format, vlist)\" \"-Dfprintf(stream, format, ...)=printf(format)\" -D_LIBCPP_PRINT=1" CACHE STRING "")
endforeach()
foreach(type SHARED;MODULE;EXE)
set(RUNTIMES_${target}_CMAKE_${type}_LINKER_FLAGS "-fuse-ld=lld" CACHE STRING "")
Expand Down
22 changes: 22 additions & 0 deletions libcxx/src/chrono.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
# include <sys/time.h> // for gettimeofday and timeval
#endif

#if defined(__LLVM_LIBC__)
# define _LIBCPP_HAS_TIMESPEC_GET
#endif

// OpenBSD and GPU do not have a fully conformant suite of POSIX timers, but
// it does have clock_gettime and CLOCK_MONOTONIC which is all we need.
#if defined(__APPLE__) || defined(__gnu_hurd__) || defined(__OpenBSD__) || defined(__AMDGPU__) || \
Expand Down Expand Up @@ -115,6 +119,15 @@ 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_HAS_TIMESPEC_GET)

static system_clock::time_point __libcpp_system_clock_now() {
struct timespec ts;
if (timespec_get(&ts, TIME_UTC) != TIME_UTC)
__throw_system_error(errno, "timespec_get(TIME_UTC) failed");
return system_clock::time_point(seconds(ts.tv_sec) + microseconds(ts.tv_nsec / 1000));
}

#elif defined(_LIBCPP_HAS_CLOCK_GETTIME)

static system_clock::time_point __libcpp_system_clock_now() {
Expand Down Expand Up @@ -216,6 +229,15 @@ static steady_clock::time_point __libcpp_steady_clock_now() noexcept {
return steady_clock::time_point(nanoseconds(_zx_clock_get_monotonic()));
}

# elif defined(_LIBCPP_HAS_TIMESPEC_GET)

static steady_clock::time_point __libcpp_steady_clock_now() {
struct timespec ts;
if (timespec_get(&ts, TIME_MONOTONIC) != TIME_MONOTONIC)
__throw_system_error(errno, "timespec_get(TIME_MONOTONIC) failed");
return steady_clock::time_point(seconds(ts.tv_sec) + microseconds(ts.tv_nsec / 1000));
}

# elif defined(_LIBCPP_HAS_CLOCK_GETTIME)

static steady_clock::time_point __libcpp_steady_clock_now() {
Expand Down
10 changes: 10 additions & 0 deletions libcxx/src/filesystem/filesystem_clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
# include <sys/time.h> // for gettimeofday and timeval
#endif

#if defined(__LLVM_LIBC__)
# define _LIBCPP_HAS_TIMESPEC_GET
#endif

#if defined(__APPLE__) || defined(__gnu_hurd__) || defined(__AMDGPU__) || defined(__NVPTX__) || \
(defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0)
# define _LIBCPP_HAS_CLOCK_GETTIME
Expand All @@ -50,6 +54,12 @@ _FilesystemClock::time_point _FilesystemClock::now() noexcept {
GetSystemTimeAsFileTime(&time);
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_HAS_TIMESPEC_GET)
typedef chrono::duration<rep, nano> __nsecs;
struct timespec ts;
if (timespec_get(&ts, TIME_UTC) != TIME_UTC)
__throw_system_error(errno, "timespec_get(TIME_UTC) failed");
return time_point(__secs(ts.tv_sec) + chrono::duration_cast<duration>(__nsecs(ts.tv_nsec)));
#elif defined(_LIBCPP_HAS_CLOCK_GETTIME)
typedef chrono::duration<rep, nano> __nsecs;
struct timespec tp;
Expand Down
Loading