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//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_LIBC_SRC_TIME_LINUX_CLOCKGETTIMEIMPL_H
10#define LLVM_LIBC_SRC_TIME_LINUX_CLOCKGETTIMEIMPL_H
11
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"
16
17#include <stdint.h> // For int64_t.
18#include <sys/syscall.h> // For syscall numbers.
19#include <time.h>
20
21namespace LIBC_NAMESPACE {
22namespace internal {
23
24LIBC_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 ts: static_cast<long>(clockid),
29 ts: 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;
43}
44
45} // namespace internal
46} // namespace LIBC_NAMESPACE
47
48#endif // LLVM_LIBC_SRC_TIME_LINUX_CLOCKGETTIMEIMPL_H
49

source code of libc/src/time/linux/clockGetTimeImpl.h