10#include "dead_lock_detector.hpp"
32template<
bool UseDeadLockDetector>
50 if constexpr (UseDeadLockDetector) {
52 tt_axiom(other !=
this,
"Mutex already locked.");
53 tt_axiom(other ==
nullptr,
"Potential dead-lock.");
56 tt_axiom(semaphore.
load() <= 2);
59 uint32_t expected = 0;
61 [[unlikely]] lock_contended(expected);
64 tt_axiom(semaphore.
load() <= 2);
75 if constexpr (UseDeadLockDetector) {
77 tt_axiom(other !=
this,
"Mutex already locked.");
78 tt_axiom(other ==
nullptr,
"Potential dead-lock.");
81 tt_axiom(semaphore.
load() <= 2);
84 uint32_t expected = 0;
86 tt_axiom(semaphore.
load() <= 2);
88 if constexpr (UseDeadLockDetector) {
92 [[unlikely]]
return false;
95 tt_axiom(semaphore.
load() <= 2);
99 void unlock() noexcept {
100 if constexpr (UseDeadLockDetector) {
104 tt_axiom(semaphore.
load() <= 2);
106 if (semaphore.
fetch_sub(1, std::memory_order::relaxed) != 1) {
107 [[unlikely]] semaphore.
store(0, std::memory_order::release);
109 semaphore.notify_one();
114 tt_axiom(semaphore.
load() <= 2);
126 tt_no_inline
void lock_contended(uint32_t expected)
noexcept
128 tt_axiom(semaphore.
load() <= 2);
131 ttlet should_wait = expected == 2;
136 tt_axiom(semaphore.
load() <= 2);
140 tt_axiom(semaphore.
load() <= 2);
147#if TT_BUILD_TYPE == TT_BT_DEBUG
148using unfair_mutex = unfair_mutex_impl<true>;
150using unfair_mutex = unfair_mutex_impl<false>;
static void * lock(void *object) noexcept
Lock an object on this thread.
static void remove_object(void *object) noexcept
Remove the object from the detection.
static bool unlock(void *object) noexcept
Unlock 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:33
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:74
T atomic_thread_fence(T... args)
T compare_exchange_strong(T... args)