Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion async_simple/async_simple.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ export extern "C++" {
#include "uthread/internal/thread.h"
#include "uthread/Uthread.h"
#include "uthread/Async.h"
#include "coro/ConditionVariable.h"
// clang-format off
#include "coro/SpinLock.h"
#include "coro/ConditionVariable.h"
// clang-format on
#include "coro/Latch.h"
#include "coro/CountEvent.h"
#include "coro/Collect.h"
Expand Down
32 changes: 22 additions & 10 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,31 @@ 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,
std::memory_order_acq_rel,
std::memory_order_acquire))
;
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