summaryrefslogtreecommitdiffstats
path: root/src/OSSupport/CriticalSection.cpp
blob: 3e8e7498d90ec9b37e5bc349a999b9cce2c33b91 (plain) (blame)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188

#include "Globals.h"  // NOTE: MSVC stupidness requires this to be the same across all modules
#include "IsThread.h"





////////////////////////////////////////////////////////////////////////////////
// cCriticalSection:

cCriticalSection::cCriticalSection()
{
	#ifdef _WIN32
		InitializeCriticalSection(&m_CriticalSection);
	#else
		pthread_mutexattr_init(&m_Attributes);
		pthread_mutexattr_settype(&m_Attributes, PTHREAD_MUTEX_RECURSIVE);

		if (pthread_mutex_init(&m_CriticalSection, &m_Attributes) != 0)
		{
			LOGERROR("Could not initialize Critical Section!");
		}
	#endif
	
	#ifdef _DEBUG
		m_IsLocked = 0;
	#endif  // _DEBUG
}





cCriticalSection::~cCriticalSection()
{
	#ifdef _WIN32
		DeleteCriticalSection(&m_CriticalSection);
	#else
		if (pthread_mutex_destroy(&m_CriticalSection) != 0)
		{
			LOGWARNING("Could not destroy Critical Section!");
		}
		pthread_mutexattr_destroy(&m_Attributes);
	#endif
}





void cCriticalSection::Lock()
{
	#ifdef _WIN32
		EnterCriticalSection(&m_CriticalSection);
	#else
		pthread_mutex_lock(&m_CriticalSection);
	#endif
	
	#ifdef _DEBUG
		m_IsLocked += 1;
		m_OwningThreadID = std::this_thread::get_id();
	#endif  // _DEBUG
}





void cCriticalSection::Unlock()
{
	#ifdef _DEBUG
		ASSERT(m_IsLocked > 0);
		m_IsLocked -= 1;
	#endif  // _DEBUG
	
	#ifdef _WIN32
		LeaveCriticalSection(&m_CriticalSection);
	#else
		pthread_mutex_unlock(&m_CriticalSection);
	#endif
}





#ifdef _DEBUG
bool cCriticalSection::IsLocked(void)
{
	return (m_IsLocked > 0);
}





bool cCriticalSection::IsLockedByCurrentThread(void)
{
	return ((m_IsLocked > 0) && (m_OwningThreadID == std::this_thread::get_id()));
}
#endif  // _DEBUG





////////////////////////////////////////////////////////////////////////////////
// cCSLock

cCSLock::cCSLock(cCriticalSection * a_CS)
	: m_CS(a_CS)
	, m_IsLocked(false)
{
	Lock();
}





cCSLock::cCSLock(cCriticalSection & a_CS)
	: m_CS(&a_CS)
	, m_IsLocked(false)
{
	Lock();
}





cCSLock::~cCSLock()
{
	if (!m_IsLocked)
	{
		return;
	}
	Unlock();
}





void cCSLock::Lock(void)
{
	ASSERT(!m_IsLocked);
	m_IsLocked = true;
	m_CS->Lock();
}





void cCSLock::Unlock(void)
{
	ASSERT(m_IsLocked);
	m_IsLocked = false;
	m_CS->Unlock();
}





////////////////////////////////////////////////////////////////////////////////
// cCSUnlock:

cCSUnlock::cCSUnlock(cCSLock & a_Lock) :
	m_Lock(a_Lock)
{
	m_Lock.Unlock();
}





cCSUnlock::~cCSUnlock()
{
	m_Lock.Lock();
}