|
1 |
| -//===---------- Linux implementation of the POSIX clock_gettime function --===// |
| 1 | +//===- Linux implementation of the POSIX clock_gettime function -*- C++ -*-===// |
2 | 2 | //
|
3 | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
4 | 4 | // See https://llvm.org/LICENSE.txt for license information.
|
5 | 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
6 | 6 | //
|
7 | 7 | //===----------------------------------------------------------------------===//
|
8 | 8 |
|
9 |
| -#include "src/time/clock_gettime.h" |
| 9 | +#ifndef LLVM_LIBC_SRC_TIME_LINUX_CLOCKGETTIMEIMPL_H |
| 10 | +#define LLVM_LIBC_SRC_TIME_LINUX_CLOCKGETTIMEIMPL_H |
10 | 11 |
|
11 | 12 | #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
|
12 | 13 | #include "src/__support/common.h"
|
| 14 | +#include "src/__support/error_or.h" |
13 | 15 | #include "src/errno/libc_errno.h"
|
14 | 16 |
|
15 | 17 | #include <sys/syscall.h> // For syscall numbers.
|
16 | 18 | #include <time.h>
|
17 | 19 |
|
18 | 20 | namespace __llvm_libc {
|
| 21 | +namespace internal { |
19 | 22 |
|
20 |
| -// TODO(michaelrj): Move this into time/linux with the other syscalls. |
21 |
| -LLVM_LIBC_FUNCTION(int, clock_gettime, |
22 |
| - (clockid_t clockid, struct timespec *tp)) { |
| 23 | +LIBC_INLINE ErrorOr<int> clock_gettimeimpl(clockid_t clockid, |
| 24 | + struct timespec *ts) { |
23 | 25 | #if SYS_clock_gettime
|
24 | 26 | int ret = __llvm_libc::syscall_impl<int>(SYS_clock_gettime,
|
25 | 27 | static_cast<long>(clockid),
|
26 |
| - reinterpret_cast<long>(tp)); |
| 28 | + reinterpret_cast<long>(ts)); |
27 | 29 | #elif defined(SYS_clock_gettime64)
|
| 30 | + struct timespec64 ts64; |
28 | 31 | int ret = __llvm_libc::syscall_impl<int>(SYS_clock_gettime64,
|
29 | 32 | static_cast<long>(clockid),
|
30 |
| - reinterpret_cast<long>(tp)); |
| 33 | + reinterpret_cast<long>(&ts64)); |
| 34 | + ts->tv_sec = static_cast<time_t>(ts64.tv_sec); |
| 35 | + ts->tv_nsec = static_cast<long>(ts64.tv_nsec); |
31 | 36 | #else
|
32 | 37 | #error "SYS_clock_gettime and SYS_clock_gettime64 syscalls not available."
|
33 | 38 | #endif
|
34 |
| - |
35 |
| - // A negative return value indicates an error with the magnitude of the |
36 |
| - // value being the error code. |
37 |
| - if (ret < 0) { |
38 |
| - libc_errno = -ret; |
39 |
| - return -1; |
40 |
| - } |
41 |
| - |
42 |
| - return 0; |
| 39 | + if (ret < 0) |
| 40 | + return Error(-ret); |
| 41 | + return ret; |
43 | 42 | }
|
44 | 43 |
|
| 44 | +} // namespace internal |
45 | 45 | } // namespace __llvm_libc
|
| 46 | + |
| 47 | +#endif // LLVM_LIBC_SRC_TIME_LINUX_CLOCKGETTIMEIMPL_H |
0 commit comments