1 //===--- clock_gettime linux implementation ---------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_CLOCK_GETTIME_H 10 #define LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_CLOCK_GETTIME_H 11 12 #include "hdr/types/clockid_t.h" 13 #include "hdr/types/struct_timespec.h" 14 #include "src/__support/OSUtil/syscall.h" 15 #include "src/__support/common.h" 16 #include "src/__support/error_or.h" 17 #include <sys/syscall.h> 18 19 namespace LIBC_NAMESPACE { 20 namespace internal { clock_gettime(clockid_t clockid,timespec * ts)21LIBC_INLINE ErrorOr<int> clock_gettime(clockid_t clockid, timespec *ts) { 22 #if SYS_clock_gettime 23 int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_gettime, 24 static_cast<long>(clockid), 25 reinterpret_cast<long>(ts)); 26 #elif defined(SYS_clock_gettime64) 27 static_assert( 28 sizeof(time_t) == sizeof(int64_t), 29 "SYS_clock_gettime64 requires struct timespec with 64-bit members."); 30 int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_gettime64, 31 static_cast<long>(clockid), 32 reinterpret_cast<long>(ts)); 33 #else 34 #error "SYS_clock_gettime and SYS_clock_gettime64 syscalls not available." 35 #endif 36 if (ret < 0) 37 return Error(-ret); 38 return ret; 39 } 40 41 } // namespace internal 42 } // namespace LIBC_NAMESPACE 43 44 #endif // LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_CLOCK_GETTIME_H 45