Commit 349a8d21 authored by Xavier Thompson's avatar Xavier Thompson

mutex.hpp: Optimise some atomics memory ordering

parent afb4b7d4
......@@ -25,6 +25,8 @@ namespace typon
std::atomic<std::uintptr_t> _stack { 0 };
};
using enum std::memory_order;
std::atomic<Node *> _state { nullptr };
Node * _waiters { nullptr };
......@@ -36,12 +38,13 @@ namespace typon
bool await_ready() noexcept
{
std::atomic<Node *> & state = _mutex->_state;
Node * next = nullptr;
for(;;)
{
// _next must be properly set before updating _state
_next = next;
if(_mutex->_state.compare_exchange_weak(next, this))
if(state.compare_exchange_weak(next, this, acq_rel, relaxed))
{
return !next;
}
......@@ -64,12 +67,11 @@ namespace typon
Node * waiter = _mutex->_waiters;
if (!waiter)
{
Node * state = this;
if (_mutex->_state.compare_exchange_strong(state, nullptr))
Node * next = this;
if (_mutex->_state.compare_exchange_strong(next, nullptr, acq_rel))
{
return;
}
auto next = state;
while (next != this)
{
auto tmp = next->_next;
......
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