Warning: This file is not a C or C++ file. It does not have highlighting.
1 | //===--- Linux absolute timeout ---------------------------------*- 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_ABS_TIMEOUT_H |
10 | #define LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_ABS_TIMEOUT_H |
11 | |
12 | #include "hdr/time_macros.h" |
13 | #include "hdr/types/struct_timespec.h" |
14 | #include "src/__support/CPP/expected.h" |
15 | #include "src/__support/macros/config.h" |
16 | #include "src/__support/time/units.h" |
17 | |
18 | namespace LIBC_NAMESPACE_DECL { |
19 | namespace internal { |
20 | // We use AbsTimeout to remind ourselves that the timeout is an absolute time. |
21 | // This is a simple wrapper around the timespec struct that also keeps track of |
22 | // whether the time is in realtime or monotonic time. |
23 | class AbsTimeout { |
24 | timespec timeout; |
25 | bool realtime_flag; |
26 | LIBC_INLINE constexpr explicit AbsTimeout(timespec ts, bool realtime) |
27 | : timeout(ts), realtime_flag(realtime) {} |
28 | |
29 | public: |
30 | enum class Error { Invalid, BeforeEpoch }; |
31 | LIBC_INLINE const timespec &get_timespec() const { return timeout; } |
32 | LIBC_INLINE bool is_realtime() const { return realtime_flag; } |
33 | LIBC_INLINE static constexpr cpp::expected<AbsTimeout, Error> |
34 | from_timespec(timespec ts, bool realtime) { |
35 | using namespace time_units; |
36 | if (ts.tv_nsec < 0 || ts.tv_nsec >= 1_s_ns) |
37 | return cpp::unexpected(Error::Invalid); |
38 | |
39 | // POSIX allows tv_sec to be negative. We interpret this as an expired |
40 | // timeout. |
41 | if (ts.tv_sec < 0) |
42 | return cpp::unexpected(Error::BeforeEpoch); |
43 | |
44 | return AbsTimeout{ts, realtime}; |
45 | } |
46 | }; |
47 | } // namespace internal |
48 | } // namespace LIBC_NAMESPACE_DECL |
49 | |
50 | #endif // LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_ABS_TIMEOUT_H |
51 |
Warning: This file is not a C or C++ file. It does not have highlighting.