From 008c16e0840b625a96103c0e14082007dcb4e0c2 Mon Sep 17 00:00:00 2001 From: Will Date: Fri, 8 Mar 2024 21:36:37 -0500 Subject: [PATCH] adapter/darwin: atomic sync experiments --- .../detail/wtr/watcher/synchronized.hpp | 23 ++++++------------- include/wtr/watcher.hpp | 23 ++++++------------- 2 files changed, 14 insertions(+), 32 deletions(-) diff --git a/devel/include/detail/wtr/watcher/synchronized.hpp b/devel/include/detail/wtr/watcher/synchronized.hpp index cfda9157..f3d8ea23 100644 --- a/devel/include/detail/wtr/watcher/synchronized.hpp +++ b/devel/include/detail/wtr/watcher/synchronized.hpp @@ -34,18 +34,9 @@ class Synchronized { bool exchange_when_current_is, bool want_value) -> bool { -#ifdef WTR_ALLOW_NON_SEQUENTIAL_CONSISTENCY - constexpr auto relord = std::memory_order_release; - constexpr auto acqord = std::memory_order_acquire; -#else - constexpr auto relord = std::memory_order_seq_cst; - constexpr auto acqord = std::memory_order_seq_cst; -#endif return current_value.compare_exchange_strong( exchange_when_current_is, - want_value, - acqord, - relord); + want_value); } inline static auto try_take(std::atomic& owner_flag) -> bool @@ -53,14 +44,14 @@ class Synchronized { return exchange_when(owner_flag, false, true); } - inline static auto try_leave(std::atomic& owner_flag) -> bool + inline static auto eventually_take(std::atomic& owner_flag) { - return exchange_when(owner_flag, true, false); + while (! try_take(owner_flag)) { ; } } - inline static auto eventually_take(std::atomic& owner_flag) + inline static auto leave(std::atomic& owner_flag) -> void { - while (! try_take(owner_flag)) { std::this_thread::yield(); } + owner_flag.store(false); } #else @@ -70,7 +61,7 @@ class Synchronized { return mtx.try_lock(); } - inline static auto try_leave(std::mutex& mtx) { mtx.unlock(); } + inline static auto leave(std::mutex& mtx) { mtx.unlock(); } inline static auto eventually_take(std::mutex& mtx) { mtx.lock(); } @@ -92,7 +83,7 @@ class Synchronized { return Synchronized{primitive}; } - inline ~Synchronized() { try_leave(this->primitive); } + inline ~Synchronized() { leave(this->primitive); } }; #endif diff --git a/include/wtr/watcher.hpp b/include/wtr/watcher.hpp index 591aa387..46f4c0e2 100644 --- a/include/wtr/watcher.hpp +++ b/include/wtr/watcher.hpp @@ -502,18 +502,9 @@ class Synchronized { bool exchange_when_current_is, bool want_value) -> bool { -#ifdef WTR_ALLOW_NON_SEQUENTIAL_CONSISTENCY - constexpr auto relord = std::memory_order_release; - constexpr auto acqord = std::memory_order_acquire; -#else - constexpr auto relord = std::memory_order_seq_cst; - constexpr auto acqord = std::memory_order_seq_cst; -#endif return current_value.compare_exchange_strong( exchange_when_current_is, - want_value, - acqord, - relord); + want_value); } inline static auto try_take(std::atomic& owner_flag) -> bool @@ -521,14 +512,14 @@ class Synchronized { return exchange_when(owner_flag, false, true); } - inline static auto try_leave(std::atomic& owner_flag) -> bool + inline static auto eventually_take(std::atomic& owner_flag) { - return exchange_when(owner_flag, true, false); + while (! try_take(owner_flag)) { ; } } - inline static auto eventually_take(std::atomic& owner_flag) + inline static auto leave(std::atomic& owner_flag) -> void { - while (! try_take(owner_flag)) { std::this_thread::yield(); } + owner_flag.store(false); } #else @@ -538,7 +529,7 @@ class Synchronized { return mtx.try_lock(); } - inline static auto try_leave(std::mutex& mtx) { mtx.unlock(); } + inline static auto leave(std::mutex& mtx) { mtx.unlock(); } inline static auto eventually_take(std::mutex& mtx) { mtx.lock(); } @@ -560,7 +551,7 @@ class Synchronized { return Synchronized{primitive}; } - inline ~Synchronized() { try_leave(this->primitive); } + inline ~Synchronized() { leave(this->primitive); } }; #endif