// License: Modified MIT (NON-AI) // See the LICENSE file in the root directory for license information. // Copyright 2025 Timothy Prepscius #pragma once #include #include "Lock.h" #include "../system/Debug.h" #include "../assert/debug_assert.h" namespace tjp::core { #ifdef USE_REAL_TEST_MUTEX inline TestMutex::Lock TestMutex::lock_of() { return Lock(*this); } inline void TestMutex::lock () { auto id = get_thread_id(); auto next = id; auto previous = v.exchange(next); debug_assert(previous == 0 || previous == id); c++; } inline void TestMutex::unlock () { auto id = get_thread_id(); auto next = (--c) ? get_thread_id() : 0; auto previous = v.exchange(next); debug_assert(previous == id); } inline u64 TestMutex::get_thread_id() const { return std::hash()(std::this_thread::get_id()); } inline bool TestMutex::locked_by_caller() const { return v == get_thread_id(); } inline TestMutex::Lock::Lock () { } inline TestMutex::Lock::Lock (TestMutex::Lock &&rhs) { std::swap(rhs.m, m); } inline TestMutex::Lock::Lock (TestMutex &m_) { m_.lock(); m = &m_; } inline TestMutex::Lock::~Lock () { if (m) m->unlock(); } #endif inline auto lock_of(TestMutex &m) { return m.lock_of(); } } // namespace