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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470 | // Copyright (C) 2020-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
#include <util/readwrite_mutex.h>
#include <gtest/gtest.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/shared_ptr.hpp><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/make_shared.hpp><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <chrono><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <iostream><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <thread><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <unistd.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
using namespace isc::util;
using namespace std;
namespace {
/// @brief Test Fixture for testing read-write mutexes.
///
/// Each not basic test follows the same schema:
/// @code
/// main thread work thread
/// <- started
/// work ->
/// enter guard
/// <- done
/// terminate ->
/// return
/// join
/// @endcode
class ReadWriteMutexTest : public ::testing::Test {
public:
/// @brief Read-write mutex.
ReadWriteMutex rw_mutex_;
/// @brief Synchronization objects for work threads.
struct Sync {
bool started = false;
mutex started_mtx;
condition_variable started_cv;
bool work = false;
mutex work_mtx;
condition_variable work_cv;
bool done = false;
mutex done_mtx;
condition_variable done_cv;
bool terminate = false;
mutex terminate_mtx;
condition_variable terminate_cv;
} syncr_, syncw_;
/// @brief Body of the reader.
///
/// @param rw_mutex The read-write mutex.
/// @param syncr The reader synchronization object.
void reader(ReadWriteMutex& rw_mutex, Sync& syncr) {
// Take mutex to wait for main thread signals.
unique_lock<mutex> terminate_lock(syncr.terminate_mtx);
// Signal the thread started.
{
lock_guard<mutex> lock(syncr.started_mtx);
syncr.started = true;
}
// Wait for work.
{
unique_lock<mutex> work_lock(syncr.work_mtx);
// When this thread starts waiting, the main thread is resumed.
syncr.started_cv.notify_one();
syncr.work_cv.wait(work_lock, [&](){ return syncr.work; });
}
{
// Enter a read lock guard.
ReadLockGuard rwlock(rw_mutex);
// Signal the thread holds the guard.
{
lock_guard<mutex> done_lock(syncr.done_mtx);
syncr.done = true;
}
syncr.done_cv.notify_one();
}
// Wait to terminate.
syncr.terminate_cv.wait(terminate_lock, [&](){ return syncr.terminate; });
}
/// @brief Body of the writer.
///
/// @param rw_mutex The read-write mutex.
/// @param syncw The writer synchronization object.
void writer(ReadWriteMutex& rw_mutex, Sync& syncw) {
// Take mutex to wait for main thread signals.
unique_lock<mutex> terminate_lock(syncw.terminate_mtx);
// Signal the thread started.
{
lock_guard<mutex> lock(syncw.started_mtx);
syncw.started = true;
}
// Wait for work.
{
unique_lock<mutex> work_lock(syncw.work_mtx);
// When this thread starts waiting, the main thread is resumed.
syncw.started_cv.notify_one();
syncw.work_cv.wait(work_lock, [&](){ return syncw.work; });
}
{
// Enter a write lock guard.
WriteLockGuard rwlock(rw_mutex);
// Signal the thread holds the guard.
{
lock_guard<mutex> done_lock(syncw.done_mtx);
syncw.done = true;
}
syncw.done_cv.notify_one();
}
// Wait to terminate.
syncw.terminate_cv.wait(terminate_lock, [&](){ return syncw.terminate; });
}
};
// Verify basic read lock guard.
TEST_F(ReadWriteMutexTest, basicRead) {<--- syntax error
ReadLockGuard lock(rw_mutex_);
}
// Verify basic write lock guard.
TEST_F(ReadWriteMutexTest, basicWrite) {
WriteLockGuard lock(rw_mutex_);
}
// Verify read lock guard using a thread.
TEST_F(ReadWriteMutexTest, read) {
// Take mutex to wait for work thread signals.
boost::shared_ptr<std::thread> thread;
{
unique_lock<mutex> started_lock(syncr_.started_mtx);
// Create a work thread.
thread = boost::make_shared<std::thread>([this](){ reader(rw_mutex_, syncr_); });
// Wait work thread to start.
syncr_.started_cv.wait(started_lock, [this](){ return syncr_.started; });
unique_lock<mutex> done_lock(syncr_.done_mtx);
// Signal the thread to work.
{
lock_guard<mutex> work_lock(syncr_.work_mtx);
syncr_.work = true;
}
syncr_.work_cv.notify_one();
// Wait thread to hold the read lock.
syncr_.done_cv.wait(done_lock, [this](){ return syncr_.done; });
}
// Signal the thread to terminate.
{
lock_guard<mutex> terminate_lock(syncr_.terminate_mtx);
syncr_.terminate = true;
}
syncr_.terminate_cv.notify_one();
// Join the thread.
thread->join();
}
// Verify write lock guard using a thread.
TEST_F(ReadWriteMutexTest, write) {
// Take mutex to wait for work thread signals.
boost::shared_ptr<std::thread> thread;
{
unique_lock<mutex> started_lock(syncw_.started_mtx);
// Create a work thread.
thread = boost::make_shared<std::thread>([this](){ writer(rw_mutex_, syncw_); });
// Wait work thread to start.
syncw_.started_cv.wait(started_lock, [this](){ return syncw_.started; });
unique_lock<mutex> done_lock(syncw_.done_mtx);
// Signal the thread to work.
{
lock_guard<mutex> work_lock(syncw_.work_mtx);
syncw_.work = true;
}
syncw_.work_cv.notify_one();
// Wait thread to hold the write lock.
syncw_.done_cv.wait(done_lock, [this](){ return syncw_.done; });
}
// Signal the thread to terminate.
{
lock_guard<mutex> terminate_lock(syncw_.terminate_mtx);
syncw_.terminate = true;
}
syncw_.terminate_cv.notify_one();
// Join the thread.
thread->join();
}
// Verify read lock guard can be acquired by multiple threads.
TEST_F(ReadWriteMutexTest, readRead) {
// Take mutex to wait for work thread signals.
boost::shared_ptr<std::thread> thread;
{
unique_lock<mutex> started_lock(syncr_.started_mtx);
// Create a work thread.
thread = boost::make_shared<std::thread>([this](){ reader(rw_mutex_, syncr_); });
// Enter a read lock guard.
ReadLockGuard rwlock(rw_mutex_);
// Wait work thread to start.
syncr_.started_cv.wait(started_lock, [this](){ return syncr_.started; });
unique_lock<mutex> done_lock(syncr_.done_mtx);
// Signal the thread to work.
{
lock_guard<mutex> work_lock(syncr_.work_mtx);
syncr_.work = true;
}
syncr_.work_cv.notify_one();
// Wait thread to hold the read lock.
syncr_.done_cv.wait(done_lock, [this](){ return syncr_.done; });
}
// Signal the thread to terminate.
{
lock_guard<mutex> terminate_lock(syncr_.terminate_mtx);
syncr_.terminate = true;
}
syncr_.terminate_cv.notify_one();
// Join the thread.
thread->join();
}
// Verify write lock guard is exclusive of a reader.
TEST_F(ReadWriteMutexTest, readWrite) {
// Take mutex to wait for work thread signals.
boost::shared_ptr<std::thread> thread;
{
unique_lock<mutex> started_lock(syncw_.started_mtx);
// Create a work thread.
thread = boost::make_shared<std::thread>([this](){ writer(rw_mutex_, syncw_); });
// Wait work thread to start.
syncw_.started_cv.wait(started_lock, [this](){ return syncw_.started; });
unique_lock<mutex> done_lock(syncw_.done_mtx);
{
// Enter a read lock guard.
ReadLockGuard rwlock(rw_mutex_);
// Signal the thread to work.
{
lock_guard<mutex> work_lock(syncw_.work_mtx);
syncw_.work = true;
}
syncw_.work_cv.notify_one();
// Verify the work thread is waiting for the write lock.
cout << "pausing for one second" << std::endl;
bool ret = syncw_.done_cv.wait_for(done_lock, chrono::seconds(1), [this](){ return syncw_.done; });
EXPECT_FALSE(syncw_.done);
EXPECT_FALSE(ret);
// Exiting the read lock guard.
}
// Wait thread to hold the write lock.
syncw_.done_cv.wait(done_lock, [this](){ return syncw_.done; });
}
// Signal the thread to terminate.
{
lock_guard<mutex> terminate_lock(syncw_.terminate_mtx);
syncw_.terminate = true;
}
syncw_.terminate_cv.notify_one();
// Join the thread.
thread->join();
}
// Verify write lock guard is exclusive of a writer.
TEST_F(ReadWriteMutexTest, writeWrite) {
// Take mutex to wait for work thread signals.
boost::shared_ptr<std::thread> thread;
{
unique_lock<mutex> started_lock(syncw_.started_mtx);
// Create a work thread.
thread = boost::make_shared<std::thread>([this](){ writer(rw_mutex_, syncw_); });
// Wait work thread to start.
syncw_.started_cv.wait(started_lock, [this](){ return syncw_.started; });
unique_lock<mutex> done_lock(syncw_.done_mtx);
{
// Enter a write lock guard.
WriteLockGuard rwlock(rw_mutex_);
// Signal the thread to work.
{
lock_guard<mutex> work_lock(syncw_.work_mtx);
syncw_.work = true;
}
syncw_.work_cv.notify_one();
// Verify the work thread is waiting for the write lock.
cout << "pausing for one second" << std::endl;
bool ret = syncw_.done_cv.wait_for(done_lock, chrono::seconds(1), [this](){ return syncw_.done; });
EXPECT_FALSE(syncw_.done);
EXPECT_FALSE(ret);
// Exiting the write lock guard.
}
// Wait thread to hold the write lock.
syncw_.done_cv.wait(done_lock, [this](){ return syncw_.done; });
}
// Signal the thread to terminate.
{
lock_guard<mutex> terminate_lock(syncw_.terminate_mtx);
syncw_.terminate = true;
}
syncw_.terminate_cv.notify_one();
// Join the thread.
thread->join();
}
// Verify that a writer has the preference.
TEST_F(ReadWriteMutexTest, readWriteRead) {
// Take mutex to wait for work thread signals.
boost::shared_ptr<std::thread> threadw;
{
unique_lock<mutex> startedw_lock(syncw_.started_mtx);
// First thread is a writer.
threadw = boost::make_shared<std::thread>([this](){ writer(rw_mutex_, syncw_); });
// Wait work thread to start.
syncw_.started_cv.wait(startedw_lock, [this](){ return syncw_.started; });
}
boost::shared_ptr<std::thread> threadr;
{
unique_lock<mutex> startedr_lock(syncr_.started_mtx);
// Second thread is a reader.
threadr = boost::make_shared<std::thread>([this](){ reader(rw_mutex_, syncr_); });
// Wait work thread to start.
syncr_.started_cv.wait(startedr_lock, [this](){ return syncr_.started; });
}
{
unique_lock<mutex> donew_lock(syncw_.done_mtx);
{
// Enter a read lock guard.
ReadLockGuard rwlock(rw_mutex_);
// Signal the writer thread to work.
{
lock_guard<mutex> work_lock(syncw_.work_mtx);
syncw_.work = true;
}
syncw_.work_cv.notify_one();
// Verify the writer thread is waiting for the write lock.
cout << "pausing for one second" << std::endl;
bool ret = syncw_.done_cv.wait_for(donew_lock, chrono::seconds(1), [this](){ return syncw_.done; });
EXPECT_FALSE(syncw_.done);
EXPECT_FALSE(ret);
{
unique_lock<mutex> doner_lock(syncr_.done_mtx);
// Signal the reader thread to work.
{
lock_guard<mutex> work_lock(syncr_.work_mtx);
syncr_.work = true;
}
syncr_.work_cv.notify_one();
// Verify the reader thread is waiting for the read lock.
cout << "pausing for one second" << std::endl;
ret = syncr_.done_cv.wait_for(doner_lock, chrono::seconds(1), [this](){ return syncr_.done; });
EXPECT_FALSE(syncr_.done);
EXPECT_FALSE(ret);
}
// Exiting the read lock guard.
}
{
unique_lock<mutex> doner_lock(syncr_.done_mtx);
// Verify the reader thread is still waiting for the read lock.
cout << "pausing for one second" << std::endl;
bool ret = syncr_.done_cv.wait_for(doner_lock, chrono::seconds(1), [this](){ return syncr_.done; });
EXPECT_FALSE(syncr_.done);
EXPECT_FALSE(ret);
}
// Wait writer thread to hold the write lock.
syncw_.done_cv.wait(donew_lock, [this](){ return syncw_.done; });
}
{
unique_lock<mutex> doner_lock(syncr_.done_mtx);
// Wait reader thread to hold the read lock.
syncr_.done_cv.wait(doner_lock, [this](){ return syncr_.done; });
}
// Signal the writer thread to terminate.
{
lock_guard<mutex> terminate_lock(syncw_.terminate_mtx);
syncw_.terminate = true;
}
syncw_.terminate_cv.notify_one();
// Join the writer thread.
threadw->join();
// Signal the reader thread to terminate.
{
lock_guard<mutex> terminate_lock(syncr_.terminate_mtx);
syncr_.terminate = true;
}
syncr_.terminate_cv.notify_one();
// Join the reader thread.
threadr->join();
}
}
|