Skip to content

Commit f9f2fb6

Browse files
committed
[libcxx] Support for using timespec_get
clock_gettime is a POSIX API that may not be available on platforms like baremetal; timespec_get is the C11 equivalent. This change adds support for using timespec_get instead of clock_gettime to improve compatibility with non-POSIX platforms. For now, this is only enabled with LLVM libc which implemented timespec_get in llvm#116102, but in the future this can be expanded to other platforms.
1 parent 39b2979 commit f9f2fb6

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

clang/cmake/caches/Fuchsia-stage2.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ foreach(target armv6m-none-eabi;armv7m-none-eabi;armv8m.main-none-eabi)
329329
foreach(lang C;CXX;ASM)
330330
# TODO: The preprocessor defines workaround various issues in libc and libc++ integration.
331331
# These should be addressed and removed over time.
332-
set(RUNTIMES_${target}_CMAKE_${lang}_local_flags "--target=${target} -mthumb -Wno-atomic-alignment \"-Dvfprintf(stream, format, vlist)=vprintf(format, vlist)\" \"-Dfprintf(stream, format, ...)=printf(format)\" \"-Dtimeval=struct timeval{int tv_sec; int tv_usec;}\" \"-Dgettimeofday(tv, tz)\" -D_LIBCPP_PRINT=1")
332+
set(RUNTIMES_${target}_CMAKE_${lang}_local_flags "--target=${target} -mthumb -Wno-atomic-alignment \"-Dvfprintf(stream, format, vlist)=vprintf(format, vlist)\" \"-Dfprintf(stream, format, ...)=printf(format)\" -D_LIBCPP_PRINT=1")
333333
if(${target} STREQUAL "armv8m.main-none-eabi")
334334
set(RUNTIMES_${target}_CMAKE_${lang}_local_flags "${RUNTIMES_${target}_CMAKE_${lang}_local_flags} -mfloat-abi=softfp -march=armv8m.main+fp+dsp -mcpu=cortex-m33" CACHE STRING "")
335335
endif()
@@ -385,7 +385,7 @@ foreach(target riscv32-unknown-elf)
385385
foreach(lang C;CXX;ASM)
386386
# TODO: The preprocessor defines workaround various issues in libc and libc++ integration.
387387
# These should be addressed and removed over time.
388-
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)\" \"-Dtimeval=struct timeval{int tv_sec; int tv_usec;}\" \"-Dgettimeofday(tv, tz)\" -D_LIBCPP_PRINT=1" CACHE STRING "")
388+
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 "")
389389
endforeach()
390390
foreach(type SHARED;MODULE;EXE)
391391
set(RUNTIMES_${target}_CMAKE_${type}_LINKER_FLAGS "-fuse-ld=lld" CACHE STRING "")

libcxx/src/chrono.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
# include <sys/time.h> // for gettimeofday and timeval
3232
#endif
3333

34+
#if defined(__LLVM_LIBC__)
35+
# define _LIBCPP_HAS_TIMESPEC_GET
36+
#endif
37+
3438
// OpenBSD and GPU do not have a fully conformant suite of POSIX timers, but
3539
// it does have clock_gettime and CLOCK_MONOTONIC which is all we need.
3640
#if defined(__APPLE__) || defined(__gnu_hurd__) || defined(__OpenBSD__) || defined(__AMDGPU__) || \
@@ -115,6 +119,15 @@ static system_clock::time_point __libcpp_system_clock_now() {
115119
return system_clock::time_point(duration_cast<system_clock::duration>(d - nt_to_unix_epoch));
116120
}
117121

122+
#elif defined(_LIBCPP_HAS_TIMESPEC_GET)
123+
124+
static system_clock::time_point __libcpp_system_clock_now() {
125+
struct timespec ts;
126+
if (timespec_get(&ts, TIME_UTC) != TIME_UTC)
127+
__throw_system_error(errno, "timespec_get(TIME_UTC) failed");
128+
return system_clock::time_point(seconds(ts.tv_sec) + microseconds(ts.tv_nsec / 1000));
129+
}
130+
118131
#elif defined(_LIBCPP_HAS_CLOCK_GETTIME)
119132

120133
static system_clock::time_point __libcpp_system_clock_now() {
@@ -213,6 +226,15 @@ static steady_clock::time_point __libcpp_steady_clock_now() noexcept {
213226
return steady_clock::time_point(nanoseconds(_zx_clock_get_monotonic()));
214227
}
215228

229+
# elif defined(_LIBCPP_HAS_TIMESPEC_GET)
230+
231+
static steady_clock::time_point __libcpp_steady_clock_now() {
232+
struct timespec ts;
233+
if (timespec_get(&ts, TIME_MONOTONIC) != TIME_MONOTONIC)
234+
__throw_system_error(errno, "timespec_get(TIME_MONOTONIC) failed");
235+
return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
236+
}
237+
216238
# elif defined(_LIBCPP_HAS_CLOCK_GETTIME)
217239

218240
static steady_clock::time_point __libcpp_steady_clock_now() {

libcxx/src/filesystem/filesystem_clock.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
# include <sys/time.h> // for gettimeofday and timeval
3030
#endif
3131

32+
#if defined(__LLVM_LIBC__)
33+
# define _LIBCPP_HAS_TIMESPEC_GET
34+
#endif
35+
3236
#if defined(__APPLE__) || defined(__gnu_hurd__) || defined(__AMDGPU__) || defined(__NVPTX__) || \
3337
(defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0)
3438
# define _LIBCPP_HAS_CLOCK_GETTIME
@@ -46,6 +50,12 @@ _FilesystemClock::time_point _FilesystemClock::now() noexcept {
4650
GetSystemTimeAsFileTime(&time);
4751
detail::TimeSpec tp = detail::filetime_to_timespec(time);
4852
return time_point(__secs(tp.tv_sec) + chrono::duration_cast<duration>(__nsecs(tp.tv_nsec)));
53+
#elif defined(_LIBCPP_HAS_TIMESPEC_GET)
54+
typedef chrono::duration<rep, nano> __nsecs;
55+
struct timespec ts;
56+
if (timespec_get(&ts, TIME_UTC) != TIME_UTC)
57+
__throw_system_error(errno, "timespec_get(TIME_UTC) failed");
58+
return time_point(__secs(ts.tv_sec) + chrono::duration_cast<duration>(__nsecs(ts.tv_nsec)));
4959
#elif defined(_LIBCPP_HAS_CLOCK_GETTIME)
5060
typedef chrono::duration<rep, nano> __nsecs;
5161
struct timespec tp;

0 commit comments

Comments
 (0)