• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- timeout 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_MONOTONICITY_H
10 #define LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_MONOTONICITY_H
11 
12 #include "hdr/time_macros.h"
13 #include "src/__support/libc_assert.h"
14 #include "src/__support/time/linux/abs_timeout.h"
15 #include "src/__support/time/linux/clock_conversion.h"
16 namespace LIBC_NAMESPACE {
17 namespace internal {
18 // This function is separated from abs_timeout.
19 // This function pulls in the dependency to clock_conversion.h,
20 // which may transitively depend on vDSO hence futex. However, this structure
21 // would be passed to futex, so we need to avoid cyclic dependencies.
22 // This function is going to be used in timed locks. Pthread generally uses
23 // realtime clocks for timeouts. However, due to non-monotoncity, realtime
24 // clocks reportedly lead to undesired behaviors. Therefore, we also provide a
25 // method to convert the timespec to a monotonic clock relative to the time of
26 // function call.
ensure_monotonicity(AbsTimeout & timeout)27 LIBC_INLINE void ensure_monotonicity(AbsTimeout &timeout) {
28   if (timeout.is_realtime()) {
29     auto res = AbsTimeout::from_timespec(
30         convert_clock(timeout.get_timespec(), CLOCK_REALTIME, CLOCK_MONOTONIC),
31         false);
32 
33     LIBC_ASSERT(res.has_value());
34     if (!res.has_value())
35       __builtin_unreachable();
36 
37     timeout = *res;
38   }
39 }
40 } // namespace internal
41 } // namespace LIBC_NAMESPACE
42 
43 #endif // LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_MONOTONICITY_H
44