-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMutex.cpp
368 lines (316 loc) · 8.55 KB
/
Mutex.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/***************************************************************************
Mutex.cpp - description
-------------------
begin : Sat Mar 12 2011
copyright : (C) 2011 by Knut-Helge Vik
email : knuthelge@vik.gs
***************************************************************************/
#include "BaseLib/Mutex.h"
#include "BaseLib/CommonDefines.h"
#include "BaseLib/Thread.h"
#include "BaseLib/ThreadManager.h"
#include "BaseLib/Debug.h"
#include "BaseLib/Exception.h"
namespace BaseLib
{
// --------------------------------------------------------------
// MutexVariableHolder
// --------------------------------------------------------------
MutexVariableHolder::MutexVariableHolder()
{
#ifdef USE_GCC
if (pthread_mutex_init(&mutex_, NULL))
throw std::runtime_error("Mutex::Mutex(): Could not create mutex");
#else
InitializeCriticalSection(&criticalSection_);
#endif
}
MutexVariableHolder::~MutexVariableHolder()
{
#ifdef USE_GCC
if(pthread_mutex_destroy(&mutex_))
{
//ASSERT(0);
ICRITICAL() << "Mutex::~Mutex(): Could not destroy mutex";
throw std::runtime_error("Mutex::~Mutex(): Could not destroy mutex");
}
#else
DeleteCriticalSection(&criticalSection_);
#endif
}
// --------------------------------------------------------------
// Mutex
// --------------------------------------------------------------
Mutex::Mutex(MutexPolicy policy)
: policy_(policy)
, mutexOwnerThreadId_(0)
, mutexHolder_(new MutexVariableHolder())
{ }
Mutex::Mutex(const Mutex &other)
: policy_(other.policy_)
, mutexOwnerThreadId_(0)
, mutexHolder_(new MutexVariableHolder())
{
if(other.policy_.IsShared()) {
mutexHolder_ = other.mutexHolder_->shared_from_this();
}
}
Mutex::~Mutex()
{ }
Mutex &Mutex::operator=(const Mutex &)
{
return *this;
}
void Mutex::lock()
{
Thread *self = debugMutexLockEntry();
#ifdef USE_GCC
int status = pthread_mutex_lock(&mutexHolder_->GetMutex());
if (status != 0)
{
ASSERT(status == 0);
throw std::runtime_error("Mutex::lock(): Could not lock mutex");
}
#else
EnterCriticalSection(&mutexHolder_->GetMutex());
#endif
debugMutexLockExit(self);
if(Thread::isInterrupted())
{
throw InterruptedException("Thread was interrupted");
}
}
void Mutex::unlock()
{
Thread* self = debugMutexUnlockEntry();
#ifdef USE_GCC
int status = pthread_mutex_unlock(&mutexHolder_->GetMutex());
if (status != 0)
{
ASSERT(status == 0);
throw std::runtime_error("Mutex::unlock(): Could not unlock mutex");
}
#else
LeaveCriticalSection(&mutexHolder_->GetMutex());
#endif
debugMutexUnlockExit(self);
}
/**
* TODO: Waiting is not yet implemented!
*/
bool Mutex::tryLock(int64 milliseconds)
{
if(Thread::isInterrupted())
{
throw InterruptedException("Thread was interrupted");
}
if(milliseconds != 0)
{
ElapsedTimer timer(milliseconds);
bool isLocked = false;
do
{
isLocked = tryLockPrivate();
if(!isLocked)
{
if(Thread::isInterrupted())
{
throw InterruptedException("Thread was interrupted");
}
int waitTimeMs = (int) std::min<int64>(300, timer.Remaining().InMillis());
Thread::msleep(waitTimeMs);
}
}
while(!isLocked && !timer.HasExpired());
if(isLocked)
{
return isLocked;
}
else
{
Thread *owner = ThreadManager::GetInstance().GetThread(this->mutexOwnerThreadId_);
if(owner) ICRITICAL() << "Lock timeout, waiting for owner thread : " << owner;
ICRITICAL() << "Timed out waiting for lock!";
}
}
return tryLockPrivate();
}
bool Mutex::tryLockPrivate()
{
// -- debug --
Thread *self = debugMutexTrylockEntry();
// -- debug --
#ifdef USE_GCC
int status = pthread_mutex_trylock(&mutexHolder_->GetMutex());
if(status == 0)
{
debugMutexLockExit(self);
return true;
}
else if(status == EBUSY)
{
debugMutexTrylockFailedExit(self);
return false;
}
else
{
std::cerr << "returned unknown error code: " << status;
throw std::runtime_error("Mutex::tryLock(): ERROR! returned unknown error code: "); // + string(status));
}
#else
int isAcquired = TryEnterCriticalSection(&mutexHolder_->GetMutex());
if(isAcquired != 0)
{
debugMutexLockExit(self);
return true;
}
else
{
debugMutexTrylockFailedExit(self);
return false;
}
#endif
return false;
}
Thread* Mutex::debugMutexLockEntry()
{
ASSERT(mutexHolder_);
// -- deadlock detection --
Thread *self = NULL;
if(policy_.IsDebug())
{
self = Thread::currentThread();
if(self)
{
self->setThreadState(ThreadDetails::LockingState);
if(mutexOwnerThreadId_ == self->threadId())
{
std::stringstream str;
str << "Deadlock detected in thread id"
<< self->threadId() << " name " << self->threadName()
<< " owner is " << mutexOwnerThreadId_;
throw DeadlockException(str.str());
}
}
}
// -- end --
return self;
}
void Mutex::debugMutexLockExit(Thread *self)
{
ASSERT(mutexHolder_);
// -- deadlock detection --
if(policy_.IsDebug() && self)
{
ASSERT(self);
mutexOwnerThreadId_ = self->threadId();
self->setThreadState(ThreadDetails::RunningMutexedState);
}
// -- end --
// -- deadlock detection --
/*if(debug_ && self)
{
if(mutexOwnerThreadId_ == self->threadId())
{
std::stringstream str;
str << "Deadlock detected in thread id"
<< self->threadId() << " name " << self->threadName()
<< " owner is " << mutexOwnerThreadId_;
throw DeadlockException(str.str());
}
self->setThreadState(ThreadDetails::RunningMutexedState);
mutexOwnerThreadId_ = self->threadId();
}*/
// -- end --
}
Thread* Mutex::debugMutexUnlockEntry()
{
ASSERT(mutexHolder_);
// -- deadlock detection --
Thread *self = NULL;
if(policy_.IsDebug())
{
self = Thread::currentThread();
if(self)
{
self->setThreadState(ThreadDetails::UnlockingState);
if(mutexOwnerThreadId_ != self->threadId())
{
ICRITICAL() << "Unlocking mutex which is not owned! Current thread id "
<< self->threadId() << " name " << self->threadName()
<< " owner is " << mutexOwnerThreadId_;
}
}
mutexOwnerThreadId_ = 0;
}
// -- end --
return self;
}
void Mutex::debugMutexUnlockExit(Thread *self)
{
ASSERT(mutexHolder_);
// -- deadlock detection --
if(policy_.IsDebug() && self)
{
self->setThreadState(ThreadDetails::RunningState);
}
// -- end --
}
Thread* Mutex::debugMutexTrylockEntry()
{
// -- deadlock detection --
Thread *self = NULL;
if(policy_.IsDebug())
{
self = Thread::currentThread();
if(self)
{
self->setThreadState(ThreadDetails::LockingState);
if(mutexOwnerThreadId_ == self->threadId())
IWARNING() << "Attempting to lock owned mutex! Thread id " << self->threadId() << " name " << self->threadName();
}
}
// -- end --
return self;
}
void Mutex::debugMutexTrylockFailedExit(Thread *self)
{
// -- deadlock detection --
if(policy_.IsDebug() && self)
{
self->setThreadState(ThreadDetails::RunningState);
}
// -- end --
}
// --------------------------------------------------------------
// Null operation mutex
// --------------------------------------------------------------
NullMutex::NullMutex(MutexPolicy)
{
}
NullMutex::~NullMutex()
{
}
void NullMutex::lock() const
{
// if(Thread::isInterrupted())
// {
// throw InterruptedException("Thread was interrupted");
// }
}
void NullMutex::unlock() const
{
// if(Thread::isInterrupted())
// {
// throw InterruptedException("Thread was interrupted");
// }
}
bool NullMutex::tryLock(int64) const
{
// if(Thread::isInterrupted())
// {
// throw InterruptedException("Thread was interrupted");
// }
return true;
}
}