diff options
author | chai <chaifix@163.com> | 2019-08-14 22:50:43 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2019-08-14 22:50:43 +0800 |
commit | 15740faf9fe9fe4be08965098bbf2947e096aeeb (patch) | |
tree | a730ec236656cc8cab5b13f088adfaed6bb218fb /Runtime/Threads/Mutex.cpp |
Diffstat (limited to 'Runtime/Threads/Mutex.cpp')
-rw-r--r-- | Runtime/Threads/Mutex.cpp | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/Runtime/Threads/Mutex.cpp b/Runtime/Threads/Mutex.cpp new file mode 100644 index 0000000..9ca9991 --- /dev/null +++ b/Runtime/Threads/Mutex.cpp @@ -0,0 +1,70 @@ +#include "UnityPrefix.h" + +#if SUPPORT_THREADS + +#include "Mutex.h" + +#ifndef THREAD_LOCK_DEBUG +#define THREAD_LOCK_DEBUG 0 +#endif + +Mutex::Mutex() +{ +} + +Mutex::~Mutex () +{ +} + +bool Mutex::IsLocked() +{ + if(m_Mutex.TryLock()) + { + Unlock(); + return false; + } + else + { + return true; + } +} + +void Mutex::BlockUntilUnlocked() +{ + Lock(); + Unlock(); +} + +void Mutex::Lock() +{ +#if THREAD_LOCK_DEBUG + m_PerThreadLockDepth.SetIntValue(m_PerThreadLockDepth.GetIntValue() + 1); +#endif + m_Mutex.Lock(); +} + +void Mutex::Unlock() +{ +#if THREAD_LOCK_DEBUG + AssertIf(m_PerThreadLockDepth.GetIntValue() == 0); + m_PerThreadLockDepth.SetIntValue(m_PerThreadLockDepth.GetIntValue() - 1); +#endif + m_Mutex.Unlock(); +} + +bool Mutex::TryLock() +{ +#if THREAD_LOCK_DEBUG + if (m_Mutex.TryLock()) + { + m_PerThreadLockDepth.SetIntValue(m_PerThreadLockDepth.GetIntValue() + 1); + return true; + } + else + return false; +#else + return m_Mutex.TryLock(); +#endif +} + +#endif // SUPPORT_THREADS ; has dummy mutex implemented in headerfile |