-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadWriteLock.cpp
325 lines (287 loc) · 7.46 KB
/
ReadWriteLock.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
#include "BaseLib/ReadWriteLock.h"
#include "BaseLib/Exception.h"
#include "BaseLib/Debug.h"
#include "BaseLib/Thread.h"
namespace BaseLib
{
#ifdef USE_GCC
/**
* The pthread_rwlock_rdlock() and pthread_rwlock_tryrdlock() functions may fail if:
* [EINVAL] - The value specified by rwlock does not refer to an initialised read-write lock object.
* [EDEADLK] - The current thread already owns the read-write lock for writing.
* [EAGAIN] - The read lock could not be acquired because the maximum number of read locks for rwlock has been exceeded.
*/
std::string interpretPthreadReturn(int status)
{
std::stringstream err;
switch(status)
{
case EINVAL:
err << "[EINVAL] - The value specified by rwlock does not refer to an initialised read-write lock object.";
break;
case EDEADLK:
err << "[EDEADLK] - The current thread already owns the read-write lock for writing.";
break;
case EAGAIN:
err << "[EAGAIN] - The read lock could not be acquired because the maximum number of read locks for rwlock has been exceeded.";
break;
default:
err <<"Unknown error occurred!";
break;
}
return err.str();
}
#endif
/**
* @brief ReadWriteVariableHolder::ReadWriteVariableHolder
*/
ReadWriteVariableHolder::ReadWriteVariableHolder()
#ifdef USE_GCC
: mutex_()
#endif
{
#ifdef USE_GCC
if(pthread_rwlock_init(&mutex_, NULL))
throw std::runtime_error("ReadWriteVariableHolder::ReadWriteVariableHolder(): Could not create mutex");
#else
InitializeSRWLock(&criticalSection_);
#endif
}
/**
* @brief ReadWriteVariableHolder::~ReadWriteVariableHolder
*/
ReadWriteVariableHolder::~ReadWriteVariableHolder()
{
#ifdef USE_GCC
if(pthread_rwlock_destroy(&mutex_))
throw std::runtime_error("ReadWriteVariableHolder::~ReadWriteVariableHolder(): Could not destroy mutex");
#else
//Because SRWLs don't use any dynamically allocated events or memory internally, there is no need to delete them la ter on, and initialization ensures the right bit pattern is contained in memory.
#endif
}
/**
* @brief ReadWriteLock::ReadWriteLock
*/
ReadWriteLock::ReadWriteLock(MutexPolicy policy)
: policy_(policy)
, mutexOwnerThreadId_(0)
, accessCount_(0)
, mutexHolder_(new ReadWriteVariableHolder())
{
}
/**
* @brief ReadWriteLock::~ReadWriteLock
*/
ReadWriteLock::~ReadWriteLock()
{
}
/**
* @brief ReadWriteLock::lockForRead
*/
void ReadWriteLock::lockForRead()
{
#ifdef USE_GCC
int status = pthread_rwlock_rdlock(&mutexHolder_->GetMutex());
if (status != 0)
{
std::stringstream err;
err << "ReadWriteLock::lockForRead(): Could not lock mutex: " << interpretPthreadReturn(status);
throw std::runtime_error(err.str());
}
#else
AcquireSRWLockShared(&mutexHolder_->GetMutex());
#endif
ASSERT(accessCount_ != -1);
++accessCount_;
if(Thread::isInterrupted())
{
throw InterruptedException("Thread was interrupted");
}
}
/**
* @brief ReadWriteLock::lockForWrite
*/
void ReadWriteLock::lockForWrite()
{
#ifdef USE_GCC
int status = pthread_rwlock_wrlock(&mutexHolder_->GetMutex());
if (status != 0)
{
std::stringstream err;
err << "ReadWriteLock::lockForWrite(): Could not lock mutex: " << interpretPthreadReturn(status);
throw std::runtime_error(err.str());
}
#else
AcquireSRWLockExclusive(&mutexHolder_->GetMutex());
#endif
ASSERT(accessCount_ == 0);
accessCount_ = -1;
if(Thread::isInterrupted())
{
throw InterruptedException("Thread was interrupted");
}
}
void ReadWriteLock::lock()
{
return lockForWrite();
}
/**
* @brief ReadWriteLock::tryLockForRead
* @return
*/
bool ReadWriteLock::tryLockForRead()
{
if(Thread::isInterrupted())
{
throw InterruptedException("Thread was interrupted");
}
#ifdef USE_GCC
int status = pthread_rwlock_tryrdlock(&mutexHolder_->GetMutex());
if(status == 0)
{
ASSERT(accessCount_ != -1);
++accessCount_;
return true;
}
else if(status == EBUSY)
{
return false;
}
else
{
ICRITICAL() << "returned unknown error code: " << status;
throw std::runtime_error("ReadWriteLock::tryLockForRead(): ERROR! returned unknown error code!");
}
#else
int status = TryAcquireSRWLockShared(&mutexHolder_->GetMutex());
if(status != 0)
{
ASSERT(accessCount_ != -1);
++accessCount_;
return true;
}
else
{
return false;
}
#endif
return false;
}
/**
* @brief ReadWriteLock::tryLockForRead
* @param timeout
* @return
*/
/*bool ReadWriteLock::tryLockForRead(int timeout)
{
return false;
}*/
/**
* @brief ReadWriteLock::tryLockForWrite
* @return
*/
bool ReadWriteLock::tryLockForWrite()
{
if(Thread::isInterrupted())
{
throw InterruptedException("Thread was interrupted");
}
#ifdef USE_GCC
int status = pthread_rwlock_trywrlock(&mutexHolder_->GetMutex());
if(status == 0)
{
ASSERT(accessCount_ == 0);
accessCount_ = -1;
return true;
}
else if(status == EBUSY)
{
ASSERT(accessCount_ > 0);
return false;
}
else
{
ICRITICAL() << "returned unknown error code: " << status;
throw std::runtime_error("ReadWriteLock::tryLockForWrite(): ERROR! returned unknown error code!");
}
#else
BOOLEAN status = TryAcquireSRWLockExclusive(&mutexHolder_->GetMutex());
if(status != 0)
{
ASSERT(accessCount_ == 0);
accessCount_ = -1;
return true;
}
else
{
ASSERT(accessCount_ > 0);
return false;
}
#endif
return false;
}
/**
* @brief ReadWriteLock::tryLockForWrite
* @param timeout
* @return
*/
/*bool ReadWriteLock::tryLockForWrite(int timeout)
{
return false;
}*/
/**
* @brief ReadWriteLock::unlock
*/
void ReadWriteLock::unlock()
{
#ifdef WIN32
int prevAccessCount = accessCount_;
#endif
if(accessCount_ == -1) accessCount_ = 0;
else --accessCount_;
ASSERT(accessCount_ >= 0);
#ifdef USE_GCC
int status = pthread_rwlock_unlock(&mutexHolder_->GetMutex());
if (status != 0)
throw std::runtime_error("ReadWriteLock::unlock(): Could not unlock mutex");
#else
if(prevAccessCount > 0)
ReleaseSRWLockShared(&mutexHolder_->GetMutex());
else if(prevAccessCount == -1)
ReleaseSRWLockExclusive(&mutexHolder_->GetMutex());
else // accessCount_ == 0 || accessCount_ < -1
throw std::runtime_error("ReadWriteLock::unlock(): Could not unlock mutex. Access count was 0!");
#endif
}
/**
* @brief ReadWriteLock::unlockRead
*/
/*void ReadWriteLock::unlockRead()
{
ASSERT(accessCount_ > 0);
#ifdef USE_GCC
int status = pthread_rwlock_unlock(&mutexHolder_->GetMutex());
if (status != 0)
throw std::runtime_error("ReadWriteLock::unlock(): Could not unlock mutex");
#else
ReleaseSRWLockShared(&mutexHolder_->GetMutex());
#endif
accessCount_--;
ASSERT(accessCount_ > -1);
}*/
/**
* @brief ReadWriteLock::unlockWrite
*/
/*void ReadWriteLock::unlockWrite()
{
ASSERT(accessCount_ == -1);
#ifdef USE_GCC
int status = pthread_rwlock_unlock(&mutexHolder_->GetMutex());
if (status != 0)
throw std::runtime_error("ReadWriteLock::unlock(): Could not unlock mutex");
#else
ReleaseSRWLockExclusive(&mutexHolder_->GetMutex());
#endif
accessCount_ = 0;
}*/
}