|
11 | 11 |
|
12 | 12 | #include "hdr/types/clockid_t.h"
|
13 | 13 | #include "hdr/types/struct_timespec.h"
|
| 14 | +#include "src/__support/OSUtil/linux/vdso.h" |
14 | 15 | #include "src/__support/OSUtil/syscall.h"
|
15 | 16 | #include "src/__support/common.h"
|
16 | 17 | #include "src/__support/error_or.h"
|
17 | 18 | #include "src/__support/macros/config.h"
|
18 | 19 | #include <sys/syscall.h>
|
19 | 20 |
|
| 21 | +#if defined(SYS_clock_gettime64) |
| 22 | +#include <linux/time_types.h> |
| 23 | +#endif |
| 24 | + |
20 | 25 | namespace LIBC_NAMESPACE_DECL {
|
21 | 26 | namespace internal {
|
22 | 27 | LIBC_INLINE ErrorOr<int> clock_gettime(clockid_t clockid, timespec *ts) {
|
23 |
| -#if SYS_clock_gettime |
24 |
| - int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_gettime, |
25 |
| - static_cast<long>(clockid), |
26 |
| - reinterpret_cast<long>(ts)); |
| 28 | + using namespace vdso; |
| 29 | + int ret; |
| 30 | +#if defined(SYS_clock_gettime) |
| 31 | + TypedSymbol<VDSOSym::ClockGetTime> clock_gettime; |
| 32 | + if (LIBC_LIKELY(clock_gettime != nullptr)) |
| 33 | + ret = clock_gettime(clockid, ts); |
| 34 | + else |
| 35 | + ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_gettime, |
| 36 | + static_cast<long>(clockid), |
| 37 | + reinterpret_cast<long>(ts)); |
27 | 38 | #elif defined(SYS_clock_gettime64)
|
28 | 39 | static_assert(
|
29 | 40 | sizeof(time_t) == sizeof(int64_t),
|
30 | 41 | "SYS_clock_gettime64 requires struct timespec with 64-bit members.");
|
31 |
| - int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_gettime64, |
32 |
| - static_cast<long>(clockid), |
33 |
| - reinterpret_cast<long>(ts)); |
| 42 | + |
| 43 | + TypedSymbol<VDSOSym::ClockGetTime64> clock_gettime64; |
| 44 | + __kernel_timespec ts64{}; |
| 45 | + if (LIBC_LIKELY(clock_gettime64 != nullptr)) |
| 46 | + ret = clock_gettime64(clockid, &ts64); |
| 47 | + else |
| 48 | + ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_gettime64, |
| 49 | + static_cast<long>(clockid), |
| 50 | + reinterpret_cast<long>(&ts64)); |
| 51 | + if (ret == 0) { |
| 52 | + ts->tv_sec = static_cast<decltype(ts->tv_sec)>(ts64.tv_sec); |
| 53 | + ts->tv_nsec = static_cast<decltype(ts->tv_nsec)>(ts64.tv_nsec); |
| 54 | + } |
34 | 55 | #else
|
35 | 56 | #error "SYS_clock_gettime and SYS_clock_gettime64 syscalls not available."
|
36 | 57 | #endif
|
|
0 commit comments