blob: e7506371c3564d365f65cd61bc9a72eec591115c (
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
|
#include "precomp_wasabi_bfc.h"
#include "critsec.h"
// uncomment this if needed
//#define CS_DEBUG
CriticalSection::CriticalSection() {
#ifdef WIN32
InitializeCriticalSection(&cs);
#elif defined(__APPLE__)
MPCreateCriticalRegion(&cr);
#elif defined(LINUX)
pthread_mutex_t recursive = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
cs.mutex = recursive;
#endif
#ifdef ASSERTS_ENABLED
#ifdef CS_DEBUG
within = 0;
#endif
#endif
}
CriticalSection::~CriticalSection() {
#ifdef CS_DEBUG
#ifdef ASSERTS_ENABLED
ASSERT(!within);
#endif
#endif
#ifdef WIN32
DeleteCriticalSection(&cs);
#elif defined(__APPLE__)
MPDeleteCriticalRegion(cr);
#elif defined(LINUX)
pthread_mutex_destroy(&cs.mutex);
#endif
}
void CriticalSection::enter() {
#ifdef WIN32
EnterCriticalSection(&cs);
#elif defined(__APPLE__)
MPEnterCriticalRegion(cr, kDurationForever);
#elif defined(LINUX)
pthread_mutex_lock(&cs.mutex);
#endif
#ifdef CS_DEBUG
#ifdef ASSERTS_ENABLED
ASSERT(!within);
within = 1;
#endif
#endif
}
void CriticalSection::leave() {
#if defined(CS_DEBUG) && defined(ASSERTS_ENABLED)
ASSERT(within);
within = 0;
#endif
#ifdef WIN32
LeaveCriticalSection(&cs);
#elif defined(__APPLE__)
MPExitCriticalRegion(cr);
#elif defined(LINUX)
pthread_mutex_unlock(&cs.mutex);
#endif
}
void CriticalSection::inout() {
enter();
leave();
}
|