Commit d5240fc5 by Abseil Team Committed by Copybara-Service

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
parent d241d919
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include <time.h> #include <time.h>
#include <algorithm> #include <algorithm>
#include <cstdint>
#include <limits> #include <limits>
#include "absl/base/internal/raw_logging.h" #include "absl/base/internal/raw_logging.h"
...@@ -60,7 +61,10 @@ class KernelTimeout { ...@@ -60,7 +61,10 @@ class KernelTimeout {
// Convert to parameter for sem_timedwait/futex/similar. Only for approved // Convert to parameter for sem_timedwait/futex/similar. Only for approved
// users. Do not call if !has_timeout. // 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: private:
// internal rep, not user visible: ns after unix epoch. // internal rep, not user visible: ns after unix epoch.
...@@ -126,7 +130,7 @@ class KernelTimeout { ...@@ -126,7 +130,7 @@ class KernelTimeout {
friend class Waiter; friend class Waiter;
}; };
inline struct timespec KernelTimeout::MakeAbsTimespec() { inline struct timespec KernelTimeout::MakeAbsTimespec() const {
int64_t n = ns_; int64_t n = ns_;
static const int64_t kNanosPerSecond = 1000 * 1000 * 1000; static const int64_t kNanosPerSecond = 1000 * 1000 * 1000;
if (n == 0) { if (n == 0) {
...@@ -150,6 +154,17 @@ inline struct timespec KernelTimeout::MakeAbsTimespec() { ...@@ -150,6 +154,17 @@ inline struct timespec KernelTimeout::MakeAbsTimespec() {
return abstime; 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 } // namespace synchronization_internal
ABSL_NAMESPACE_END ABSL_NAMESPACE_END
} // namespace absl } // namespace absl
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment