Commit 12355e1e authored by Xavier Thompson's avatar Xavier Thompson

mutex.hpp: Optimise some atomics memory ordering

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