Export of internal Abseil changes
-- 90ecacd2a3db96ee64ef23af37a80fad404e2b32 by Gennadiy Rozental <rogeeff@google.com>: Fixes MSVC regression by making MSVC version of class Flag into an aggregate type. PiperOrigin-RevId: 277767054 -- 018f3b040df51d91a988fa146fee163721e605e9 by Abseil Team <absl-team@google.com>: Change libstdc++ lacking std::unique_ptr check from a gcc version check to based on feature macros. PiperOrigin-RevId: 277736042 -- 475844775ae343e2414318f08549ee3fa6676a8d by CJ Johnson <johnsoncj@google.com>: Pass allocator_type through allocator_traits before extracting the typedefs PiperOrigin-RevId: 277730393 -- d843bc4bc30bf5b11af76db8beda8634b6111a62 by Abseil Team <absl-team@google.com>: Convert the Waiter::Init() method to the default constructor and define a destructor for the Waiter class. Use placement new and delete with Waiter objects. PiperOrigin-RevId: 277728823 -- 1ba6edf421dd2dfe13c55970a03c99592cb6677d by Derek Mauro <dmauro@google.com>: Use lowercase spelling for include of dbghelp.h When cross-compiling under MinGW this is important PiperOrigin-RevId: 277629783 -- cfc662a6fa357a84ddda8037156c7f26cee40c36 by Abseil Team <absl-team@google.com>: Don't use atomic ops on waiter and wakeup counts in WIN32 waiter mode. Port the new CONDVAR waiter mode code in CL 277366017 to the WIN32 waiter mode. PiperOrigin-RevId: 277603611 -- 833106542e61fa0832900adf3c1b2afc6890b94b by Abseil Team <absl-team@google.com>: Add the PerThreadSem::Destroy() method. For ABSL_WAITER_MODE_CONDVAR or ABSL_WAITER_MODE_SEM, PerThreadSem::Destroy() is used to destroy the pthread mutex and condition variable or the POSIX semaphore. PiperOrigin-RevId: 277586675 -- 7814da4a59106cf1e0e4db1a31b9592ebbd2094b by Samuel Benzaquen <sbenza@google.com>: Enable the assertion in the iterator's operator* and operator-> PiperOrigin-RevId: 277563401 GitOrigin-RevId: 90ecacd2a3db96ee64ef23af37a80fad404e2b32 Change-Id: Ib19be3680da74f0b94055c9039115ec6bcaea7b0
This commit is contained in:
parent
83880e3d8c
commit
846e5dbeda
13 changed files with 238 additions and 143 deletions
|
|
@ -122,10 +122,12 @@ class Futex {
|
|||
}
|
||||
};
|
||||
|
||||
void Waiter::Init() {
|
||||
Waiter::Waiter() {
|
||||
futex_.store(0, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
Waiter::~Waiter() = default;
|
||||
|
||||
bool Waiter::Wait(KernelTimeout t) {
|
||||
// Loop until we can atomically decrement futex from a positive
|
||||
// value, waiting on a futex while we believe it is zero.
|
||||
|
|
@ -199,7 +201,7 @@ class PthreadMutexHolder {
|
|||
pthread_mutex_t *mu_;
|
||||
};
|
||||
|
||||
void Waiter::Init() {
|
||||
Waiter::Waiter() {
|
||||
const int err = pthread_mutex_init(&mu_, 0);
|
||||
if (err != 0) {
|
||||
ABSL_RAW_LOG(FATAL, "pthread_mutex_init failed: %d", err);
|
||||
|
|
@ -214,6 +216,18 @@ void Waiter::Init() {
|
|||
wakeup_count_ = 0;
|
||||
}
|
||||
|
||||
Waiter::~Waiter() {
|
||||
const int err = pthread_mutex_destroy(&mu_);
|
||||
if (err != 0) {
|
||||
ABSL_RAW_LOG(FATAL, "pthread_mutex_destroy failed: %d", err);
|
||||
}
|
||||
|
||||
const int err2 = pthread_cond_destroy(&cv_);
|
||||
if (err2 != 0) {
|
||||
ABSL_RAW_LOG(FATAL, "pthread_cond_destroy failed: %d", err2);
|
||||
}
|
||||
}
|
||||
|
||||
bool Waiter::Wait(KernelTimeout t) {
|
||||
struct timespec abs_timeout;
|
||||
if (t.has_timeout()) {
|
||||
|
|
@ -274,13 +288,19 @@ void Waiter::InternalCondVarPoke() {
|
|||
|
||||
#elif ABSL_WAITER_MODE == ABSL_WAITER_MODE_SEM
|
||||
|
||||
void Waiter::Init() {
|
||||
Waiter::Waiter() {
|
||||
if (sem_init(&sem_, 0, 0) != 0) {
|
||||
ABSL_RAW_LOG(FATAL, "sem_init failed with errno %d\n", errno);
|
||||
}
|
||||
wakeups_.store(0, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
Waiter::~Waiter() {
|
||||
if (sem_destroy(&sem_) != 0) {
|
||||
ABSL_RAW_LOG(FATAL, "sem_destroy failed with errno %d\n", errno);
|
||||
}
|
||||
}
|
||||
|
||||
bool Waiter::Wait(KernelTimeout t) {
|
||||
struct timespec abs_timeout;
|
||||
if (t.has_timeout()) {
|
||||
|
|
@ -388,39 +408,32 @@ class LockHolder {
|
|||
SRWLOCK* mu_;
|
||||
};
|
||||
|
||||
void Waiter::Init() {
|
||||
Waiter::Waiter() {
|
||||
auto *mu = ::new (static_cast<void *>(&mu_storage_)) SRWLOCK;
|
||||
auto *cv = ::new (static_cast<void *>(&cv_storage_)) CONDITION_VARIABLE;
|
||||
InitializeSRWLock(mu);
|
||||
InitializeConditionVariable(cv);
|
||||
waiter_count_.store(0, std::memory_order_relaxed);
|
||||
wakeup_count_.store(0, std::memory_order_relaxed);
|
||||
waiter_count_ = 0;
|
||||
wakeup_count_ = 0;
|
||||
}
|
||||
|
||||
// SRW locks and condition variables do not need to be explicitly destroyed.
|
||||
// https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-initializesrwlock
|
||||
// https://stackoverflow.com/questions/28975958/why-does-windows-have-no-deleteconditionvariable-function-to-go-together-with
|
||||
Waiter::~Waiter() = default;
|
||||
|
||||
bool Waiter::Wait(KernelTimeout t) {
|
||||
SRWLOCK *mu = WinHelper::GetLock(this);
|
||||
CONDITION_VARIABLE *cv = WinHelper::GetCond(this);
|
||||
|
||||
LockHolder h(mu);
|
||||
waiter_count_.fetch_add(1, std::memory_order_relaxed);
|
||||
++waiter_count_;
|
||||
|
||||
// Loop until we find a wakeup to consume or timeout.
|
||||
// Note that, since the thread ticker is just reset, we don't need to check
|
||||
// whether the thread is idle on the very first pass of the loop.
|
||||
bool first_pass = true;
|
||||
while (true) {
|
||||
int x = wakeup_count_.load(std::memory_order_relaxed);
|
||||
if (x != 0) {
|
||||
if (!wakeup_count_.compare_exchange_weak(x, x - 1,
|
||||
std::memory_order_acquire,
|
||||
std::memory_order_relaxed)) {
|
||||
continue; // Raced with someone, retry.
|
||||
}
|
||||
// Successfully consumed a wakeup, we're done.
|
||||
waiter_count_.fetch_sub(1, std::memory_order_relaxed);
|
||||
return true;
|
||||
}
|
||||
|
||||
while (wakeup_count_ == 0) {
|
||||
if (!first_pass) MaybeBecomeIdle();
|
||||
// No wakeups available, time to wait.
|
||||
if (!SleepConditionVariableSRW(cv, mu, t.InMillisecondsFromNow(), 0)) {
|
||||
|
|
@ -429,7 +442,7 @@ bool Waiter::Wait(KernelTimeout t) {
|
|||
// initialization guarantees this is not a narrowing conversion.
|
||||
const unsigned long err{GetLastError()}; // NOLINT(runtime/int)
|
||||
if (err == ERROR_TIMEOUT) {
|
||||
waiter_count_.fetch_sub(1, std::memory_order_relaxed);
|
||||
--waiter_count_;
|
||||
return false;
|
||||
} else {
|
||||
ABSL_RAW_LOG(FATAL, "SleepConditionVariableSRW failed: %lu", err);
|
||||
|
|
@ -437,23 +450,27 @@ bool Waiter::Wait(KernelTimeout t) {
|
|||
}
|
||||
first_pass = false;
|
||||
}
|
||||
// Consume a wakeup and we're done.
|
||||
--wakeup_count_;
|
||||
--waiter_count_;
|
||||
return true;
|
||||
}
|
||||
|
||||
void Waiter::Post() {
|
||||
wakeup_count_.fetch_add(1, std::memory_order_release);
|
||||
Poke();
|
||||
LockHolder h(WinHelper::GetLock(this));
|
||||
++wakeup_count_;
|
||||
InternalCondVarPoke();
|
||||
}
|
||||
|
||||
void Waiter::Poke() {
|
||||
if (waiter_count_.load(std::memory_order_relaxed) == 0) {
|
||||
return;
|
||||
}
|
||||
// Potentially a waiter. Take the lock and check again.
|
||||
LockHolder h(WinHelper::GetLock(this));
|
||||
if (waiter_count_.load(std::memory_order_relaxed) == 0) {
|
||||
return;
|
||||
InternalCondVarPoke();
|
||||
}
|
||||
|
||||
void Waiter::InternalCondVarPoke() {
|
||||
if (waiter_count_ != 0) {
|
||||
WakeConditionVariable(WinHelper::GetCond(this));
|
||||
}
|
||||
WakeConditionVariable(WinHelper::GetCond(this));
|
||||
}
|
||||
|
||||
#else
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue