Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / src / time / linux / clockGetTimeImpl.h
blob8c8c9fcf845cc7eed09c259f55fd7ec158baf0df
1 //===- Linux implementation of the POSIX clock_gettime function -*- 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 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC_TIME_LINUX_CLOCKGETTIMEIMPL_H
10 #define LLVM_LIBC_SRC_TIME_LINUX_CLOCKGETTIMEIMPL_H
12 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
13 #include "src/__support/common.h"
14 #include "src/__support/error_or.h"
15 #include "src/errno/libc_errno.h"
17 #include <stdint.h> // For int64_t.
18 #include <sys/syscall.h> // For syscall numbers.
19 #include <time.h>
21 namespace LIBC_NAMESPACE {
22 namespace internal {
24 LIBC_INLINE ErrorOr<int> clock_gettimeimpl(clockid_t clockid,
25 struct timespec *ts) {
26 #if SYS_clock_gettime
27 int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_gettime,
28 static_cast<long>(clockid),
29 reinterpret_cast<long>(ts));
30 #elif defined(SYS_clock_gettime64)
31 static_assert(
32 sizeof(time_t) == sizeof(int64_t),
33 "SYS_clock_gettime64 requires struct timespec with 64-bit members.");
34 int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_gettime64,
35 static_cast<long>(clockid),
36 reinterpret_cast<long>(ts));
37 #else
38 #error "SYS_clock_gettime and SYS_clock_gettime64 syscalls not available."
39 #endif
40 if (ret < 0)
41 return Error(-ret);
42 return ret;
45 } // namespace internal
46 } // namespace LIBC_NAMESPACE
48 #endif // LLVM_LIBC_SRC_TIME_LINUX_CLOCKGETTIMEIMPL_H