12#include "../utility/module.hpp"
16namespace hi {
inline namespace v1 {
35template<
bool UseDeadLockDetector>
46 if constexpr (UseDeadLockDetector) {
51 bool is_locked()
const noexcept
53 return semaphore.load(std::memory_order::relaxed) != 0;
58 if constexpr (UseDeadLockDetector) {
69 semaphore_value_type expected = 0;
70 if (!semaphore.compare_exchange_strong(expected, 1, std::memory_order::acquire)) {
71 [[unlikely]] lock_contended(expected);
86 if constexpr (UseDeadLockDetector) {
97 semaphore_value_type expected = 0;
98 if (!semaphore.compare_exchange_strong(expected, 1, std::memory_order::acquire)) {
101 if constexpr (UseDeadLockDetector) {
106 [[unlikely]]
return false;
113 void unlock() noexcept
115 if constexpr (UseDeadLockDetector) {
122 if (semaphore.fetch_sub(1, std::memory_order::relaxed) != 1) {
123 [[unlikely]] semaphore.store(0, std::memory_order::release);
125 semaphore.notify_one();
127 atomic_thread_fence(std::memory_order::release);
140 std::atomic_unsigned_lock_free semaphore = 0;
141 using semaphore_value_type =
typename decltype(semaphore)::value_type;
143 bool holds_invariant() const noexcept
145 return semaphore.load(std::memory_order::relaxed) <= 2;
148 hi_no_inline
void lock_contended(semaphore_value_type expected)
noexcept
153 hilet should_wait = expected == 2;
157 if (should_wait || semaphore.compare_exchange_strong(expected, 2)) {
165 }
while (!semaphore.compare_exchange_strong(expected, 2));
170using unfair_mutex = unfair_mutex_impl<true>;
172using unfair_mutex = unfair_mutex_impl<false>;
A deadlock detector service for unfair_mutex.
#define hi_assert(expression,...)
Assert if expression is true.
Definition assert.hpp:184
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:238
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
@ other
The gui_event does not have associated data.
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
static bool unlock(void *object) noexcept
Unlock an object on this thread.
static void remove_object(void *object) noexcept
Remove the object from the detection.
static void * lock(void *object) noexcept
Lock an object on this thread.
An unfair mutex This is a fast implementation of a mutex which does not fairly arbitrate between mult...
Definition unfair_mutex.hpp:36
bool try_lock() noexcept
When try_lock() is called from a thread that already owns the lock it will return false.
Definition unfair_mutex.hpp:84