|
8 | 8 |
|
9 | 9 | #include "src/__support/time/linux/clock_gettime.h"
|
10 | 10 | #include "src/__support/OSUtil/syscall.h"
|
| 11 | +#include "src/__support/OSUtil/vdso.h" |
11 | 12 | #include <sys/syscall.h>
|
12 | 13 | namespace LIBC_NAMESPACE {
|
13 | 14 | namespace internal {
|
14 | 15 | ErrorOr<int> clock_gettime(clockid_t clockid, timespec *ts) {
|
| 16 | + int ret; |
| 17 | + do { |
| 18 | +#ifdef LIBC_VDSO_HAS_CLOCK_GETTIME |
| 19 | + if (void *symbol = vdso::get_symbol(vdso::VDSOSym::ClockGetTime)) { |
| 20 | + using FuncTy = int (*)(clockid_t, timespec *); |
| 21 | + auto func = reinterpret_cast<FuncTy>(symbol); |
| 22 | + ret = func(clockid, ts); |
| 23 | + break; |
| 24 | + } |
| 25 | +#endif |
15 | 26 | #if SYS_clock_gettime
|
16 |
| - int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_gettime, |
17 |
| - static_cast<long>(clockid), |
18 |
| - reinterpret_cast<long>(ts)); |
| 27 | + ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_gettime, |
| 28 | + static_cast<long>(clockid), |
| 29 | + reinterpret_cast<long>(ts)); |
19 | 30 | #elif defined(SYS_clock_gettime64)
|
20 |
| - static_assert( |
21 |
| - sizeof(time_t) == sizeof(int64_t), |
22 |
| - "SYS_clock_gettime64 requires struct timespec with 64-bit members."); |
23 |
| - int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_gettime64, |
24 |
| - static_cast<long>(clockid), |
25 |
| - reinterpret_cast<long>(ts)); |
| 31 | + static_assert( |
| 32 | + sizeof(time_t) == sizeof(int64_t), |
| 33 | + "SYS_clock_gettime64 requires struct timespec with 64-bit members."); |
| 34 | + ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_gettime64, |
| 35 | + static_cast<long>(clockid), |
| 36 | + reinterpret_cast<long>(ts)); |
26 | 37 | #else
|
27 | 38 | #error "SYS_clock_gettime and SYS_clock_gettime64 syscalls not available."
|
28 | 39 | #endif
|
| 40 | + } while (0); |
| 41 | + |
29 | 42 | if (ret < 0)
|
30 | 43 | return Error(-ret);
|
31 | 44 | return ret;
|
|
0 commit comments