aboutsummaryrefslogtreecommitdiff
path: root/absl/synchronization/internal/kernel_timeout.h
diff options
context:
space:
mode:
authorBenjamin Barenblat <bbaren@google.com>2023-05-08 12:51:08 -0400
committerBenjamin Barenblat <bbaren@google.com>2023-05-08 12:51:08 -0400
commitc15cec707b1c7d847e59d2db4d0b82b711a7ee6d (patch)
tree79f448d5bbc8cf52917b0b091f0e1ab60a419c85 /absl/synchronization/internal/kernel_timeout.h
parentf4f2c1da90c4e6a0683c4e66c0268baa1b79cdf3 (diff)
parentc2435f8342c2d0ed8101cb43adfd605fdc52dca2 (diff)
downloadabseil-c15cec707b1c7d847e59d2db4d0b82b711a7ee6d.tar.gz
abseil-c15cec707b1c7d847e59d2db4d0b82b711a7ee6d.tar.bz2
abseil-c15cec707b1c7d847e59d2db4d0b82b711a7ee6d.zip
Merge new upstream LTS 20230125.3
Diffstat (limited to 'absl/synchronization/internal/kernel_timeout.h')
-rw-r--r--absl/synchronization/internal/kernel_timeout.h30
1 files changed, 22 insertions, 8 deletions
diff --git a/absl/synchronization/internal/kernel_timeout.h b/absl/synchronization/internal/kernel_timeout.h
index bbd4d2d7..f5c2c0ef 100644
--- a/absl/synchronization/internal/kernel_timeout.h
+++ b/absl/synchronization/internal/kernel_timeout.h
@@ -19,8 +19,8 @@
// Constructible from a absl::Time (for a timeout to be respected) or {}
// (for "no timeout".)
// This is a private low-level API for use by a handful of low-level
-// components that are friends of this class. Higher-level components
-// should build APIs based on absl::Time and absl::Duration.
+// components. Higher-level components should build APIs based on
+// absl::Time and absl::Duration.
#ifndef ABSL_SYNCHRONIZATION_INTERNAL_KERNEL_TIMEOUT_H_
#define ABSL_SYNCHRONIZATION_INTERNAL_KERNEL_TIMEOUT_H_
@@ -28,6 +28,7 @@
#include <time.h>
#include <algorithm>
+#include <cstdint>
#include <limits>
#include "absl/base/internal/raw_logging.h"
@@ -38,7 +39,6 @@ namespace absl {
ABSL_NAMESPACE_BEGIN
namespace synchronization_internal {
-class Futex;
class Waiter;
class KernelTimeout {
@@ -60,7 +60,10 @@ class KernelTimeout {
// Convert to parameter for sem_timedwait/futex/similar. Only for approved
// users. Do not call if !has_timeout.
- struct timespec MakeAbsTimespec();
+ struct timespec MakeAbsTimespec() const;
+
+ // Convert to unix epoch nanos. Do not call if !has_timeout.
+ int64_t MakeAbsNanos() const;
private:
// internal rep, not user visible: ns after unix epoch.
@@ -111,7 +114,8 @@ class KernelTimeout {
constexpr uint64_t max_nanos =
(std::numeric_limits<int64_t>::max)() - 999999u;
uint64_t ms_from_now =
- (std::min<uint64_t>(max_nanos, ns_ - now) + 999999u) / 1000000u;
+ ((std::min)(max_nanos, static_cast<uint64_t>(ns_ - now)) + 999999u) /
+ 1000000u;
if (ms_from_now > kInfinite) {
return kInfinite;
}
@@ -119,13 +123,12 @@ class KernelTimeout {
}
return 0;
}
-#endif
- friend class Futex;
friend class Waiter;
+#endif
};
-inline struct timespec KernelTimeout::MakeAbsTimespec() {
+inline struct timespec KernelTimeout::MakeAbsTimespec() const {
int64_t n = ns_;
static const int64_t kNanosPerSecond = 1000 * 1000 * 1000;
if (n == 0) {
@@ -149,6 +152,17 @@ inline struct timespec KernelTimeout::MakeAbsTimespec() {
return abstime;
}
+inline int64_t KernelTimeout::MakeAbsNanos() const {
+ if (ns_ == 0) {
+ ABSL_RAW_LOG(
+ ERROR, "Tried to create a timeout from a non-timeout; never do this.");
+ // But we'll try to continue sanely. no-timeout ~= saturated timeout.
+ return (std::numeric_limits<int64_t>::max)();
+ }
+
+ return ns_;
+}
+
} // namespace synchronization_internal
ABSL_NAMESPACE_END
} // namespace absl