diff --git a/src/Lib/Threads.cpp b/src/Lib/Threads.cpp index ebb08f735..1da287d30 100644 --- a/src/Lib/Threads.cpp +++ b/src/Lib/Threads.cpp @@ -56,11 +56,31 @@ Lib::Mutex::Mutex() { m_mutex = new MutexStructInternal(); } Lib::Mutex::~Mutex() { delete m_mutex; } -void Lib::Mutex::LockMutex() { m_mutex->m_cs.lock(); } +void Lib::Mutex::LockMutex() { + if (m_mutex->m_owner == std::this_thread::get_id()) { + return; + } -void Lib::Mutex::UnlockMutex() { m_mutex->m_cs.unlock(); } + m_mutex->m_cs.lock(); + m_mutex->m_owner = std::this_thread::get_id(); +} -bool Lib::Mutex::TryLockMutex() { return m_mutex->m_cs.try_lock(); } +void Lib::Mutex::UnlockMutex() { + m_mutex->m_owner = std::thread::id(); + m_mutex->m_cs.unlock(); +} + +bool Lib::Mutex::TryLockMutex() { + if (m_mutex->m_owner == std::this_thread::get_id()) { + return false; + } + + if (m_mutex->m_cs.try_lock()) { + m_mutex->m_owner = std::this_thread::get_id(); + return true; + } + return false; +} Lib::ConditionVariable::ConditionVariable() { m_cond_var = new ConditionVariableStructInternal(); } diff --git a/src/Lib/Threads.h b/src/Lib/Threads.h index c02721753..14c61370b 100644 --- a/src/Lib/Threads.h +++ b/src/Lib/Threads.h @@ -91,6 +91,7 @@ struct MutexStructInternal { MutexStructInternal() = default; ~MutexStructInternal() = default; std::mutex m_cs{}; + std::atomic m_owner{}; }; class ConditionVariable { public: