Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions async_simple/coro/ConditionVariable.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef ASYNC_SIMPLE_USE_MODULES
#include <mutex>
#include "async_simple/coro/Lazy.h"
#include "async_simple/coro/SpinLock.h"

#endif // ASYNC_SIMPLE_USE_MODULES

Expand Down Expand Up @@ -50,6 +51,7 @@ class ConditionVariable {
private:
friend class ConditionVariableAwaiter<Lock>;
std::atomic<ConditionVariableAwaiter<Lock>*> _awaiters = nullptr;
SpinLock _notifyLock;
};

template <class Lock>
Expand Down Expand Up @@ -94,21 +96,30 @@ inline Lazy<> ConditionVariable<Lock>::wait(Lock& lock, Pred&& pred) noexcept {

template <class Lock>
inline void ConditionVariable<Lock>::notifyAll() noexcept {
auto awaitings = _awaiters.load(std::memory_order_acquire);
while (!_awaiters.compare_exchange_weak(awaitings, nullptr,
ConditionVariableAwaiter<Lock>* awaitings = nullptr;
{
ScopedSpinLock notifyLock(_notifyLock);
awaitings = _awaiters.load(std::memory_order_acquire);
while (!_awaiters.compare_exchange_weak(awaitings, nullptr,
std::memory_order_acq_rel,
std::memory_order_acquire))
;
;
}
resumeWaiters(awaitings);
}

template <class Lock>
inline void ConditionVariable<Lock>::notifyOne() noexcept {
auto awaitings = _awaiters.load(std::memory_order_acquire);
while (awaitings && !_awaiters.compare_exchange_weak(awaitings, awaitings->_next,
std::memory_order_acq_rel,
std::memory_order_acquire))
;
ConditionVariableAwaiter<Lock>* awaitings = nullptr;
{
ScopedSpinLock notifyLock(_notifyLock);
awaitings = _awaiters.load(std::memory_order_acquire);
while (awaitings && !_awaiters.compare_exchange_weak(awaitings, awaitings->_next,
std::memory_order_acq_rel,
std::memory_order_acquire))
;
}

if (!awaitings) {
return;
}
Expand Down
Loading