2017-09-19 22:54:40 +02:00
|
|
|
// Copyright 2017 The Abseil Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
2019-03-08 16:27:53 +01:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
|
|
|
// An optional absolute timeout, with nanosecond granularity,
|
|
|
|
// compatible with absl::Time. Suitable for in-register
|
|
|
|
// parameter-passing (e.g. syscalls.)
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
#ifndef ABSL_SYNCHRONIZATION_INTERNAL_KERNEL_TIMEOUT_H_
|
|
|
|
#define ABSL_SYNCHRONIZATION_INTERNAL_KERNEL_TIMEOUT_H_
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
#include "absl/base/internal/raw_logging.h"
|
|
|
|
#include "absl/time/clock.h"
|
|
|
|
#include "absl/time/time.h"
|
|
|
|
|
|
|
|
namespace absl {
|
|
|
|
namespace synchronization_internal {
|
|
|
|
|
2017-11-10 15:33:50 +01:00
|
|
|
class Futex;
|
2017-09-19 22:54:40 +02:00
|
|
|
class Waiter;
|
|
|
|
|
|
|
|
class KernelTimeout {
|
|
|
|
public:
|
|
|
|
// A timeout that should expire at <t>. Any value, in the full
|
|
|
|
// InfinitePast() to InfiniteFuture() range, is valid here and will be
|
|
|
|
// respected.
|
|
|
|
explicit KernelTimeout(absl::Time t) : ns_(MakeNs(t)) {}
|
|
|
|
// No timeout.
|
|
|
|
KernelTimeout() : ns_(0) {}
|
|
|
|
|
|
|
|
// A more explicit factory for those who prefer it. Equivalent to {}.
|
|
|
|
static KernelTimeout Never() { return {}; }
|
|
|
|
|
|
|
|
// We explicitly do not support other custom formats: timespec, int64_t nanos.
|
|
|
|
// Unify on this and absl::Time, please.
|
2019-03-19 19:14:01 +01:00
|
|
|
|
2017-09-19 22:54:40 +02:00
|
|
|
bool has_timeout() const { return ns_ != 0; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
// internal rep, not user visible: ns after unix epoch.
|
|
|
|
// zero = no timeout.
|
|
|
|
// Negative we treat as an unlikely (and certainly expired!) but valid
|
|
|
|
// timeout.
|
|
|
|
int64_t ns_;
|
|
|
|
|
|
|
|
static int64_t MakeNs(absl::Time t) {
|
|
|
|
// optimization--InfiniteFuture is common "no timeout" value
|
|
|
|
// and cheaper to compare than convert.
|
|
|
|
if (t == absl::InfiniteFuture()) return 0;
|
|
|
|
int64_t x = ToUnixNanos(t);
|
|
|
|
|
|
|
|
// A timeout that lands exactly on the epoch (x=0) needs to be respected,
|
|
|
|
// so we alter it unnoticably to 1. Negative timeouts are in
|
|
|
|
// theory supported, but handled poorly by the kernel (long
|
|
|
|
// delays) so push them forward too; since all such times have
|
|
|
|
// already passed, it's indistinguishable.
|
|
|
|
if (x <= 0) x = 1;
|
|
|
|
// A time larger than what can be represented to the kernel is treated
|
|
|
|
// as no timeout.
|
2018-10-29 23:53:34 +01:00
|
|
|
if (x == (std::numeric_limits<int64_t>::max)()) x = 0;
|
2017-09-19 22:54:40 +02:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert to parameter for sem_timedwait/futex/similar. Only for approved
|
|
|
|
// users. Do not call if !has_timeout.
|
|
|
|
struct timespec MakeAbsTimespec() {
|
|
|
|
int64_t n = ns_;
|
|
|
|
static const int64_t kNanosPerSecond = 1000 * 1000 * 1000;
|
|
|
|
if (n == 0) {
|
|
|
|
ABSL_RAW_LOG(
|
|
|
|
ERROR,
|
|
|
|
"Tried to create a timespec from a non-timeout; never do this.");
|
|
|
|
// But we'll try to continue sanely. no-timeout ~= saturated timeout.
|
2018-10-29 23:53:34 +01:00
|
|
|
n = (std::numeric_limits<int64_t>::max)();
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Kernel APIs validate timespecs as being at or after the epoch,
|
|
|
|
// despite the kernel time type being signed. However, no one can
|
|
|
|
// tell the difference between a timeout at or before the epoch (since
|
|
|
|
// all such timeouts have expired!)
|
|
|
|
if (n < 0) n = 0;
|
|
|
|
|
|
|
|
struct timespec abstime;
|
Export of internal Abseil changes.
--
22fa219d17b2281c0695642830c4300711bd65ea by CJ Johnson <johnsoncj@google.com>:
Rearrange the private method declarations in InlinedVector
PiperOrigin-RevId: 224202447
--
eed3c9f488f23b521bee41d3683eb6cc22517ded by Derek Mauro <dmauro@google.com>:
Fix leak_check target (it was always a no-op when LSAN isn't available).
Fixes https://github.com/abseil/abseil-cpp/issues/232
PiperOrigin-RevId: 224201634
--
fc08039e175204b14a9561f618fcfc0234586801 by Greg Falcon <gfalcon@google.com>:
Add parens around more invocations of min() and max() missed in my prior CL.
PiperOrigin-RevId: 224162430
--
0ec5476a8293c7796cd84928a1a558b14f14f222 by Abseil Team <absl-team@google.com>:
Update absl/numeric/CMakeLists.txt to use new functions
i.e. absl_cc_(library|test)
PiperOrigin-RevId: 224139165
--
2b46aa6fabb20c589661f8bbc84030ecf39ce394 by Abseil Team <absl-team@google.com>:
Update absl/meta/CMakeLists.txt to use new functions
i.e. absl_cc_(library|test)
PiperOrigin-RevId: 224117258
--
6c951c798f8c6903bd8793a8a4b5f69244be8aa9 by Abseil Team <absl-team@google.com>:
Fix 2 Unused C++ BUILD Dependencies
PiperOrigin-RevId: 224070093
--
0ee7bd191708708f91fc5209c197fd93f6e4a8b3 by Greg Falcon <gfalcon@google.com>:
Inside Abseil headers, wrap most invocations of methods and functions named `min` and `max` in parentheses, for better interoperability with Windows toolchains.
CCTZ fixes will appear in a follow-up CL.
PiperOrigin-RevId: 224051960
--
f562f56577b84a8bc07e5873775c01d068531bca by Jon Cohen <cohenjon@google.com>:
Generate Abseil compile options. The single source of truth is now absl/copts/copts.py
The way this works goes something like this:
copts.py acts as the configuration file. We use python because unlike JSON it allows comments. It has two maps in it: one from names to external flags, and one from names to internal flags.
generate_copts.py imports the maps and loops through them to write GENERATED_copts.bzl and GENERATED_AbseilCopts.cmake
AbseilConfigureCopts.cmake and configure_copts.bzl import their respective copts args and set the platform-appropriate copts into ABSL_DEFAULT_COPTS, ABSL_TEST_COPTS, ABSL_EXCEPTIONS_FLAG, and ABSL_EXCEPTIONS_LINKOPTS
For Bazel, each BUILD file load()s configure_copts.bzl
For CMake, AbseilHelpers.cmake include()s AbseilConfigureCopts.cmake to get the final copts and both inserts them as needed into legacy target rules and also makes them available to the rest of our CMakeLists.txt file. We may instead want to include() AbseilConfigureCopts.cmake directly into each CMakeLists.txt file for consistency, but I'm not sure what the deal is with cmake and include guards, or if they are even needed. That's also not as idiomatic -- CMake tends to use directory scope where globals set at a higher level CMakeLists.txt file are used in the subdirectory CMakeLists.txt files.
PiperOrigin-RevId: 224039419
--
f7402f6bb65037e668a7355f0a003f5c05a3b6a7 by Abseil Team <absl-team@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 224036622
GitOrigin-RevId: 22fa219d17b2281c0695642830c4300711bd65ea
Change-Id: I6b505360539ff2aef8aa30c51a5f7d55db1c75cf
2018-12-05 21:37:41 +01:00
|
|
|
int64_t seconds = (std::min)(n / kNanosPerSecond,
|
|
|
|
int64_t{(std::numeric_limits<time_t>::max)()});
|
2017-09-19 22:54:40 +02:00
|
|
|
abstime.tv_sec = static_cast<time_t>(seconds);
|
|
|
|
abstime.tv_nsec =
|
|
|
|
static_cast<decltype(abstime.tv_nsec)>(n % kNanosPerSecond);
|
|
|
|
return abstime;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
// Converts to milliseconds from now, or INFINITE when
|
|
|
|
// !has_timeout(). For use by SleepConditionVariableSRW on
|
|
|
|
// Windows. Callers should recognize that the return value is a
|
|
|
|
// relative duration (it should be recomputed by calling this method
|
|
|
|
// in the case of a spurious wakeup).
|
2018-07-13 01:48:27 +02:00
|
|
|
// This header file may be included transitively by public header files,
|
|
|
|
// so we define our own DWORD and INFINITE instead of getting them from
|
2018-07-18 03:53:05 +02:00
|
|
|
// <intsafe.h> and <WinBase.h>.
|
2018-07-18 20:29:01 +02:00
|
|
|
typedef unsigned long DWord; // NOLINT
|
2018-07-13 01:48:27 +02:00
|
|
|
DWord InMillisecondsFromNow() const {
|
2018-10-29 23:53:34 +01:00
|
|
|
constexpr DWord kInfinite = (std::numeric_limits<DWord>::max)();
|
2017-09-19 22:54:40 +02:00
|
|
|
if (!has_timeout()) {
|
2018-07-12 07:27:31 +02:00
|
|
|
return kInfinite;
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
// The use of absl::Now() to convert from absolute time to
|
|
|
|
// relative time means that absl::Now() cannot use anything that
|
|
|
|
// depends on KernelTimeout (for example, Mutex) on Windows.
|
|
|
|
int64_t now = ToUnixNanos(absl::Now());
|
|
|
|
if (ns_ >= now) {
|
|
|
|
// Round up so that Now() + ms_from_now >= ns_.
|
|
|
|
constexpr uint64_t max_nanos =
|
2018-10-29 23:53:34 +01:00
|
|
|
(std::numeric_limits<int64_t>::max)() - 999999u;
|
2017-09-19 22:54:40 +02:00
|
|
|
uint64_t ms_from_now =
|
|
|
|
(std::min<uint64_t>(max_nanos, ns_ - now) + 999999u) / 1000000u;
|
2018-07-18 03:55:24 +02:00
|
|
|
if (ms_from_now > kInfinite) {
|
2018-07-12 07:27:31 +02:00
|
|
|
return kInfinite;
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
2018-07-13 01:48:27 +02:00
|
|
|
return static_cast<DWord>(ms_from_now);
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-11-10 15:33:50 +01:00
|
|
|
friend class Futex;
|
2017-09-19 22:54:40 +02:00
|
|
|
friend class Waiter;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace synchronization_internal
|
|
|
|
} // namespace absl
|
2019-03-19 19:14:01 +01:00
|
|
|
|
2017-09-19 22:54:40 +02:00
|
|
|
#endif // ABSL_SYNCHRONIZATION_INTERNAL_KERNEL_TIMEOUT_H_
|