From d5240fc53307c536ef6b469cf6dbedeb304eba06 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Thu, 15 Dec 2022 16:09:01 -0800 Subject: Add MakeAbsNs to KernelTimeout. Rather than add new friends every time a new (internal) use arises, just expose the timestamp. PiperOrigin-RevId: 495722262 Change-Id: I25d2ce64769dc58cbe634259f07c600ce6c1e714 --- absl/synchronization/internal/kernel_timeout.h | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'absl/synchronization') diff --git a/absl/synchronization/internal/kernel_timeout.h b/absl/synchronization/internal/kernel_timeout.h index 44a3a2e8..020472a0 100644 --- a/absl/synchronization/internal/kernel_timeout.h +++ b/absl/synchronization/internal/kernel_timeout.h @@ -28,6 +28,7 @@ #include #include +#include #include #include "absl/base/internal/raw_logging.h" @@ -60,7 +61,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. @@ -126,7 +130,7 @@ class KernelTimeout { friend class Waiter; }; -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) { @@ -150,6 +154,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::max)(); + } + + return ns_; +} + } // namespace synchronization_internal ABSL_NAMESPACE_END } // namespace absl -- cgit v1.2.3