Directory: | ./ |
---|---|
File: | include/hpp/corbaserver/read-write-lock.hh |
Date: | 2024-12-13 15:50:05 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 34 | 35 | 97.1% |
Branches: | 12 | 24 | 50.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | // Copyright (C) 2023 by Joseph Mirabel | ||
2 | // | ||
3 | // This code was taken from https://stackoverflow.com/a/28121513 | ||
4 | |||
5 | #include <condition_variable> | ||
6 | #include <mutex> | ||
7 | |||
8 | namespace hpp { | ||
9 | namespace corbaServer { | ||
10 | |||
11 | /// Synchronization class that ensures the following. | ||
12 | /// - A Write block Weads and Writes. | ||
13 | /// - Reads are only blocked by Writes, not by other Reads. | ||
14 | /// - Write is favored over Reads. | ||
15 | /// | ||
16 | /// This code was taken from https://stackoverflow.com/a/28121513 | ||
17 | class ReadWriteLock { | ||
18 | public: | ||
19 | 8 | ReadWriteLock() | |
20 | 8 | : shared_(), | |
21 | 8 | readerQ_(), | |
22 | 8 | writerQ_(), | |
23 | 8 | activeReaders_(0), | |
24 | 8 | waitingWriters_(0), | |
25 | 8 | activeWriters_(0) {} | |
26 | |||
27 | 12 | void readLock() { | |
28 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | std::unique_lock<std::mutex> lk(shared_); |
29 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
|
12 | while (waitingWriters_ != 0) readerQ_.wait(lk); |
30 | 12 | ++activeReaders_; | |
31 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | lk.unlock(); |
32 | 12 | } | |
33 | |||
34 | 12 | void readUnlock() { | |
35 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | std::unique_lock<std::mutex> lk(shared_); |
36 | 12 | --activeReaders_; | |
37 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | lk.unlock(); |
38 | 12 | writerQ_.notify_one(); | |
39 | 12 | } | |
40 | |||
41 | 9 | void writeLock() { | |
42 |
1/2✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
|
9 | std::unique_lock<std::mutex> lk(shared_); |
43 | 9 | ++waitingWriters_; | |
44 |
2/4✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
|
9 | while (activeReaders_ != 0 || activeWriters_ != 0) writerQ_.wait(lk); |
45 | 9 | ++activeWriters_; | |
46 |
1/2✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
|
9 | lk.unlock(); |
47 | 9 | } | |
48 | |||
49 | 9 | void writeUnlock() { | |
50 |
1/2✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
|
9 | std::unique_lock<std::mutex> lk(shared_); |
51 | 9 | --waitingWriters_; | |
52 | 9 | --activeWriters_; | |
53 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (waitingWriters_ > 0) |
54 | ✗ | writerQ_.notify_one(); | |
55 | else | ||
56 | 9 | readerQ_.notify_all(); | |
57 |
1/2✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
|
9 | lk.unlock(); |
58 | 9 | } | |
59 | |||
60 | private: | ||
61 | std::mutex shared_; | ||
62 | std::condition_variable readerQ_; | ||
63 | std::condition_variable writerQ_; | ||
64 | int activeReaders_; | ||
65 | int waitingWriters_; | ||
66 | int activeWriters_; | ||
67 | }; | ||
68 | |||
69 | } // end of namespace corbaServer. | ||
70 | } // end of namespace hpp. | ||
71 |