Skip to content

Commit

Permalink
adapter/darwin: atomic sync experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
Will committed Mar 9, 2024
1 parent 235691d commit 008c16e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 32 deletions.
23 changes: 7 additions & 16 deletions devel/include/detail/wtr/watcher/synchronized.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,24 @@ 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<bool>& owner_flag) -> bool
{
return exchange_when(owner_flag, false, true);
}

inline static auto try_leave(std::atomic<bool>& owner_flag) -> bool
inline static auto eventually_take(std::atomic<bool>& owner_flag)
{
return exchange_when(owner_flag, true, false);
while (! try_take(owner_flag)) { ; }
}

inline static auto eventually_take(std::atomic<bool>& owner_flag)
inline static auto leave(std::atomic<bool>& owner_flag) -> void
{
while (! try_take(owner_flag)) { std::this_thread::yield(); }
owner_flag.store(false);
}

#else
Expand All @@ -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(); }

Expand All @@ -92,7 +83,7 @@ class Synchronized {
return Synchronized{primitive};
}

inline ~Synchronized() { try_leave(this->primitive); }
inline ~Synchronized() { leave(this->primitive); }
};

#endif
23 changes: 7 additions & 16 deletions include/wtr/watcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,33 +502,24 @@ 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<bool>& owner_flag) -> bool
{
return exchange_when(owner_flag, false, true);
}

inline static auto try_leave(std::atomic<bool>& owner_flag) -> bool
inline static auto eventually_take(std::atomic<bool>& owner_flag)
{
return exchange_when(owner_flag, true, false);
while (! try_take(owner_flag)) { ; }
}

inline static auto eventually_take(std::atomic<bool>& owner_flag)
inline static auto leave(std::atomic<bool>& owner_flag) -> void
{
while (! try_take(owner_flag)) { std::this_thread::yield(); }
owner_flag.store(false);
}

#else
Expand All @@ -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(); }

Expand All @@ -560,7 +551,7 @@ class Synchronized {
return Synchronized{primitive};
}

inline ~Synchronized() { try_leave(this->primitive); }
inline ~Synchronized() { leave(this->primitive); }
};

#endif
Expand Down

0 comments on commit 008c16e

Please sign in to comment.