-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfbCriticalSection.cpp
More file actions
95 lines (86 loc) · 1.67 KB
/
fbCriticalSection.cpp
File metadata and controls
95 lines (86 loc) · 1.67 KB
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
/* $Id: fbCriticalSection.cpp,v 1.3 2008/03/09 01:58:39 wyverex Exp $ */
/**
* fbCriticalSection
* Creates a CS lock to prevent threads
* from reading and writing at the same time
* @author Byron Heads
* @date March 07, 2008
*/
#include "fbCriticalSection.h"
/**
* fbCriticalSection
* Default Critical Section constructor
* @note Initilize all member vars
*/
fbCriticalSection::fbCriticalSection():_locked(false)
{
#ifdef Win32
InitializeCriticalSection(&hCriticalSection); /// < Create CS object
#else
pthread_mutex_init(&hMutex, NULL);
#endif
}
/**
* ~fbCriticalSection
* Destructor, Delete CS object
*/
fbCriticalSection::~fbCriticalSection()
{
#ifdef Win32
DeleteCriticalSection(&hCriticalSection);
#else
pthread_mutex_destroy(&hMutex);
#endif
}
/**
* lock
* Locks the CS, or blocks thread till CS is unlocked
*/
void fbCriticalSection::lock()
{
#ifdef Win32
EnterCriticalSection(&hCriticalSection);
#else
pthread_mutex_lock(&hMutex);
#endif
_locked = true; /// < remember CS is locked
}
/**
* unlock
* unlocks a locked CS and runs first block thread
*/
void fbCriticalSection::unlock()
{
#ifdef Win32
LeaveCriticalSection(&hCriticalSection);
#else
pthread_mutex_unlock(&hMutex);
#endif
_locked = false; /// < mark unlocked
}
/**
* isLocked
* Test if thread is currently locked
* @return True is CS is locked
*/
bool fbCriticalSection::isLocked()
{
return _locked;
}
/**
* tryLock
* Tries to lock CS, but doesn't block if locked
* @return True if was able to get lock
*/
bool fbCriticalSection::tryLock()
{
#ifdef Win32
if(!TryEnterCriticalSection(&hCriticalSection))
return false;
#else
if(!pthread_mutex_trylock(&hMutex))
return false;
#endif
_locked = true;
return true;
}