summaryrefslogtreecommitdiff
path: root/Runtime/Threads/Mutex.cpp
blob: 9ca99911021048c826d08ab039a32e989e9c1fb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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