@@ -8,38 +8,46 @@ class readWriteLock {
88 int m_readCount = 0 ;
99 int m_writeCount = 0 ;
1010 bool m_writeWaiting = false ;
11- bool m_notify = true ;
11+ bool m_multi = true ;
1212public:
1313 readWriteLock (const char *name) : m_readLock(name), m_writeLock(name) {}
1414
1515 void acquireRead () {
16- std::unique_lock<fastlock> rm (m_readLock);
17- while (m_writeCount > 0 || m_writeWaiting)
18- m_cv.wait (rm);
16+ std::unique_lock<fastlock> rm (m_readLock, std::defer_lock);
17+ if (m_multi) {
18+ rm.lock ();
19+ while (m_writeCount > 0 || m_writeWaiting)
20+ m_cv.wait (rm);
21+ }
1922 m_readCount++;
2023 }
2124
2225 bool tryAcquireRead () {
2326 std::unique_lock<fastlock> rm (m_readLock, std::defer_lock);
24- if (!rm.try_lock ())
25- return false ;
26- if (m_writeCount > 0 || m_writeWaiting)
27- return false ;
27+ if (m_multi) {
28+ if (!rm.try_lock ())
29+ return false ;
30+ if (m_writeCount > 0 || m_writeWaiting)
31+ return false ;
32+ }
2833 m_readCount++;
2934 return true ;
3035 }
3136
3237 void acquireWrite (bool exclusive = true ) {
33- std::unique_lock<fastlock> rm (m_readLock);
34- m_writeWaiting = true ;
35- while (m_readCount > 0 )
36- m_cv.wait (rm);
37- if (exclusive) {
38- /* Another thread might have the write lock while we have the read lock
39- but won't be able to release it until they can acquire the read lock
40- so release the read lock and try again instead of waiting to avoid deadlock */
41- while (!m_writeLock.try_lock ())
38+ std::unique_lock<fastlock> rm (m_readLock, std::defer_lock);
39+ if (m_multi) {
40+ rm.lock ();
41+ m_writeWaiting = true ;
42+ while (m_readCount > 0 )
4243 m_cv.wait (rm);
44+ if (exclusive) {
45+ /* Another thread might have the write lock while we have the read lock
46+ but won't be able to release it until they can acquire the read lock
47+ so release the read lock and try again instead of waiting to avoid deadlock */
48+ while (!m_writeLock.try_lock ())
49+ m_cv.wait (rm);
50+ }
4351 }
4452 m_writeCount++;
4553 m_writeWaiting = false ;
@@ -52,41 +60,47 @@ class readWriteLock {
5260
5361 bool tryAcquireWrite (bool exclusive = true ) {
5462 std::unique_lock<fastlock> rm (m_readLock, std::defer_lock);
55- if (!rm.try_lock ())
56- return false ;
57- if (m_readCount > 0 )
58- return false ;
59- if (exclusive)
60- if (!m_writeLock.try_lock ())
63+ if (m_multi) {
64+ if (!rm.try_lock ())
6165 return false ;
66+ if (m_readCount > 0 )
67+ return false ;
68+ if (exclusive)
69+ if (!m_writeLock.try_lock ())
70+ return false ;
71+ }
6272 m_writeCount++;
6373 return true ;
6474 }
6575
6676 void releaseRead () {
67- std::unique_lock<fastlock> rm (m_readLock);
68- m_readCount--;
69- if (m_notify)
77+ std::unique_lock<fastlock> rm (m_readLock, std::defer_lock );
78+ if (m_multi) {
79+ rm. lock ();
7080 m_cv.notify_all ();
81+ }
82+ m_readCount--;
7183 }
7284
7385 void releaseWrite (bool exclusive = true ) {
74- std::unique_lock<fastlock> rm (m_readLock);
86+ std::unique_lock<fastlock> rm (m_readLock, std::defer_lock );
7587 serverAssert (m_writeCount > 0 );
76- if (exclusive)
77- m_writeLock. unlock ();
78- m_writeCount--;
79- if (m_notify)
88+ if (m_multi) {
89+ rm. lock ();
90+ if (exclusive)
91+ m_writeLock. unlock ();
8092 m_cv.notify_all ();
93+ }
94+ m_writeCount--;
8195 }
8296
8397 void downgradeWrite (bool exclusive = true ) {
8498 releaseWrite (exclusive);
8599 acquireRead ();
86100 }
87101
88- void setNotify (bool notify ) {
89- m_notify = notify ;
102+ void setMulti (bool multi ) {
103+ m_multi = multi ;
90104 }
91105
92106 bool hasReader () {
0 commit comments