summaryrefslogtreecommitdiffstats
path: root/source/cCriticalSection.h
blob: 60602e82161b749b1bbf4602e0c450c4fd8955c5 (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
#pragma once

class cCriticalSection
{
public:
	cCriticalSection();
	~cCriticalSection();

	void Lock();
	void Unlock();
private:
	void* m_CriticalSectionPtr; // Pointer to a CRITICAL_SECTION object
#ifndef _WIN32
	void* m_Attributes;
#endif
};




/// RAII for cCriticalSection - locks the CS on creation, unlocks on destruction
class cCSLock
{
	cCriticalSection * m_CS;

	#ifdef _DEBUG
	// Unlike a cCriticalSection, this object should be used from a single thread, therefore access to m_IsLocked is not threadsafe
	bool m_IsLocked;
	#endif  // _DEBUG
	
public:
	cCSLock(cCriticalSection * a_CS);
	~cCSLock();
	
	// Temporarily unlock or re-lock:
	void Lock(void);
	void Unlock(void);
} ;





/// Temporary RAII unlock for a cCSLock. Useful for unlock-wait-relock scenarios
class cCSUnlock
{
	cCSLock & m_Lock;
public:
	cCSUnlock(cCSLock & a_Lock);
	~cCSUnlock();
} ;