-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMutexBare.cpp
164 lines (144 loc) · 3.51 KB
/
MutexBare.cpp
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
#include "BaseLib/MutexBare.h"
#include "BaseLib/Debug.h"
#include "BaseLib/ElapsedTimer.h"
#include "BaseLib/Thread.h"
namespace BaseLib
{
// --------------------------------------------------------------
// MutexVariableHolderBare
// --------------------------------------------------------------
MutexVariableHolderBare::MutexVariableHolderBare()
{
#ifdef USE_GCC
if (pthread_mutex_init(&mutex_, NULL))
throw std::runtime_error("MutexBare::MutexBare(): Could not create mutex");
#else
InitializeCriticalSection(&criticalSection_);
#endif
}
MutexVariableHolderBare::~MutexVariableHolderBare()
{
#ifdef USE_GCC
if(pthread_mutex_destroy(&mutex_))
{
//ASSERT(0);
ICRITICAL() << "MutexBare::~MutexBare(): Could not destroy mutex";
throw std::runtime_error("MutexBare::~MutexBare(): Could not destroy mutex");
}
#else
DeleteCriticalSection(&criticalSection_);
#endif
}
// --------------------------------------------------------------
// MutexBare
// --------------------------------------------------------------
MutexBare::MutexBare(MutexPolicy policy)
: policy_(policy)
, mutexHolder_(new MutexVariableHolderBare())
{ }
MutexBare::MutexBare(const MutexBare &other)
: policy_(other.policy_)
, mutexHolder_(new MutexVariableHolderBare())
{
if(other.policy_.IsShared()) {
mutexHolder_ = other.mutexHolder_->shared_from_this();
}
}
MutexBare::~MutexBare()
{ }
MutexBare &MutexBare::operator=(const MutexBare &)
{
return *this;
}
void MutexBare::lock()
{
#ifdef USE_GCC
int status = pthread_mutex_lock(&mutexHolder_->GetMutex());
if (status != 0)
{
ASSERT(status == 0);
throw std::runtime_error("MutexBare::lock(): Could not lock mutex");
}
#else
EnterCriticalSection(&mutexHolder_->GetMutex());
#endif
}
void MutexBare::unlock()
{
#ifdef USE_GCC
int status = pthread_mutex_unlock(&mutexHolder_->GetMutex());
if (status != 0)
{
ASSERT(status == 0);
throw std::runtime_error("MutexBare::unlock(): Could not unlock mutex");
}
#else
LeaveCriticalSection(&mutexHolder_->GetMutex());
#endif
}
/**
* TODO: Waiting is not yet implemented!
*/
bool MutexBare::tryLock(int64 milliseconds)
{
if(milliseconds != 0)
{
ElapsedTimer timer(milliseconds);
bool isLocked = false;
do
{
isLocked = tryLockPrivate();
if(!isLocked)
{
int waitTimeMs = (int) std::min<int64>(300, timer.Remaining().InMillis());
Thread::msleep(waitTimeMs);
}
}
while(!isLocked && !timer.HasExpired());
if(isLocked)
{
return isLocked;
}
else
{
ICRITICAL() << "Timed out waiting for lock!";
return false;
}
}
return tryLockPrivate();
}
bool MutexBare::isDebug() const
{
return false;
}
bool MutexBare::tryLockPrivate()
{
#ifdef USE_GCC
int status = pthread_mutex_trylock(&mutexHolder_->GetMutex());
if(status == 0)
{
return true;
}
else if(status == EBUSY)
{
return false;
}
else
{
std::cerr << "returned unknown error code: " << status;
throw std::runtime_error("MutexBare::tryLock(): ERROR! returned unknown error code: "); // + string(status));
}
#else
int isAcquired = TryEnterCriticalSection(&mutexHolder_->GetMutex());
if(isAcquired != 0)
{
return true;
}
else
{
return false;
}
#endif
return false;
}
}