Skip to content

Commit 674dcfe

Browse files
committed
Use LOCK macros for non-recursive locks
Instead of std::unique_lock.
1 parent 313609a commit 674dcfe

File tree

10 files changed

+25
-24
lines changed

10 files changed

+25
-24
lines changed

src/httpserver.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class WorkQueue
6868
{
6969
private:
7070
/** Mutex protects entire object */
71-
std::mutex cs;
71+
CWaitableCriticalSection cs;
7272
std::condition_variable cond;
7373
std::deque<std::unique_ptr<WorkItem>> queue;
7474
bool running;
@@ -108,7 +108,7 @@ class WorkQueue
108108
/** Enqueue a work item */
109109
bool Enqueue(WorkItem* item)
110110
{
111-
std::unique_lock<std::mutex> lock(cs);
111+
LOCK(cs);
112112
if (queue.size() >= maxDepth) {
113113
return false;
114114
}
@@ -123,7 +123,7 @@ class WorkQueue
123123
while (true) {
124124
std::unique_ptr<WorkItem> i;
125125
{
126-
std::unique_lock<std::mutex> lock(cs);
126+
WAIT_LOCK(cs, lock);
127127
while (running && queue.empty())
128128
cond.wait(lock);
129129
if (!running)
@@ -137,14 +137,14 @@ class WorkQueue
137137
/** Interrupt and exit loops */
138138
void Interrupt()
139139
{
140-
std::unique_lock<std::mutex> lock(cs);
140+
LOCK(cs);
141141
running = false;
142142
cond.notify_all();
143143
}
144144
/** Wait for worker threads to exit */
145145
void WaitExit()
146146
{
147-
std::unique_lock<std::mutex> lock(cs);
147+
WAIT_LOCK(cs, lock);
148148
while (numThreads > 0)
149149
cond.wait(lock);
150150
}

src/init.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ static void BlockNotifyGenesisWait(bool, const CBlockIndex *pBlockIndex)
549549
{
550550
if (pBlockIndex != nullptr) {
551551
{
552-
WaitableLock lock_GenesisWait(cs_GenesisWait);
552+
LOCK(cs_GenesisWait);
553553
fHaveGenesis = true;
554554
}
555555
condvar_GenesisWait.notify_all();
@@ -1634,7 +1634,7 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
16341634

16351635
// Wait for genesis block to be processed
16361636
{
1637-
WaitableLock lock(cs_GenesisWait);
1637+
WAIT_LOCK(cs_GenesisWait, lock);
16381638
while (!fHaveGenesis) {
16391639
condvar_GenesisWait.wait(lock);
16401640
}

src/net.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2030,7 +2030,7 @@ void CConnman::ThreadMessageHandler()
20302030
pnode->Release();
20312031
}
20322032

2033-
std::unique_lock<std::mutex> lock(mutexMsgProc);
2033+
WAIT_LOCK(mutexMsgProc, lock);
20342034
if (!fMoreWork) {
20352035
condMsgProc.wait_until(lock, std::chrono::steady_clock::now() + std::chrono::milliseconds(100), [this] { return fMsgProcWake; });
20362036
}
@@ -2349,7 +2349,7 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
23492349
flagInterruptMsgProc = false;
23502350

23512351
{
2352-
std::unique_lock<std::mutex> lock(mutexMsgProc);
2352+
LOCK(mutexMsgProc);
23532353
fMsgProcWake = false;
23542354
}
23552355

src/net.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ class CConnman
422422
bool fMsgProcWake;
423423

424424
std::condition_variable condMsgProc;
425-
std::mutex mutexMsgProc;
425+
CWaitableCriticalSection mutexMsgProc;
426426
std::atomic<bool> flagInterruptMsgProc;
427427

428428
CThreadInterrupt interruptNet;

src/random.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ void RandAddSeedSleep()
294294
}
295295

296296

297-
static std::mutex cs_rng_state;
297+
static CWaitableCriticalSection cs_rng_state;
298298
static unsigned char rng_state[32] = {0};
299299
static uint64_t rng_counter = 0;
300300

@@ -304,7 +304,7 @@ static void AddDataToRng(void* data, size_t len) {
304304
hasher.Write((const unsigned char*)data, len);
305305
unsigned char buf[64];
306306
{
307-
std::unique_lock<std::mutex> lock(cs_rng_state);
307+
WAIT_LOCK(cs_rng_state, lock);
308308
hasher.Write(rng_state, sizeof(rng_state));
309309
hasher.Write((const unsigned char*)&rng_counter, sizeof(rng_counter));
310310
++rng_counter;
@@ -336,7 +336,7 @@ void GetStrongRandBytes(unsigned char* out, int num)
336336

337337
// Combine with and update state
338338
{
339-
std::unique_lock<std::mutex> lock(cs_rng_state);
339+
WAIT_LOCK(cs_rng_state, lock);
340340
hasher.Write(rng_state, sizeof(rng_state));
341341
hasher.Write((const unsigned char*)&rng_counter, sizeof(rng_counter));
342342
++rng_counter;

src/rpc/blockchain.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ struct CUpdatedBlock
4141
int height;
4242
};
4343

44-
static std::mutex cs_blockchange;
44+
static CWaitableCriticalSection cs_blockchange;
4545
static std::condition_variable cond_blockchange;
4646
static CUpdatedBlock latestblock;
4747

@@ -218,7 +218,7 @@ UniValue waitfornewblock(const JSONRPCRequest& request)
218218

219219
CUpdatedBlock block;
220220
{
221-
std::unique_lock<std::mutex> lock(cs_blockchange);
221+
WAIT_LOCK(cs_blockchange, lock);
222222
block = latestblock;
223223
if(timeout)
224224
cond_blockchange.wait_for(lock, std::chrono::milliseconds(timeout), [&block]{return latestblock.height != block.height || latestblock.hash != block.hash || !IsRPCRunning(); });
@@ -260,7 +260,7 @@ UniValue waitforblock(const JSONRPCRequest& request)
260260

261261
CUpdatedBlock block;
262262
{
263-
std::unique_lock<std::mutex> lock(cs_blockchange);
263+
WAIT_LOCK(cs_blockchange, lock);
264264
if(timeout)
265265
cond_blockchange.wait_for(lock, std::chrono::milliseconds(timeout), [&hash]{return latestblock.hash == hash || !IsRPCRunning();});
266266
else
@@ -303,7 +303,7 @@ UniValue waitforblockheight(const JSONRPCRequest& request)
303303

304304
CUpdatedBlock block;
305305
{
306-
std::unique_lock<std::mutex> lock(cs_blockchange);
306+
WAIT_LOCK(cs_blockchange, lock);
307307
if(timeout)
308308
cond_blockchange.wait_for(lock, std::chrono::milliseconds(timeout), [&height]{return latestblock.height >= height || !IsRPCRunning();});
309309
else

src/rpc/mining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
478478
{
479479
checktxtime = std::chrono::steady_clock::now() + std::chrono::minutes(1);
480480

481-
WaitableLock lock(csBestBlock);
481+
WAIT_LOCK(csBestBlock, lock);
482482
while (chainActive.Tip()->GetBlockHash() == hashWatchedChain && IsRPCRunning())
483483
{
484484
if (cvBlockChange.wait_until(lock, checktxtime) == std::cv_status::timeout)

src/sync.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,6 @@ typedef AnnotatedMixin<std::mutex> CWaitableCriticalSection;
112112
/** Just a typedef for std::condition_variable, can be wrapped later if desired */
113113
typedef std::condition_variable CConditionVariable;
114114

115-
/** Just a typedef for std::unique_lock, can be wrapped later if desired */
116-
typedef std::unique_lock<std::mutex> WaitableLock;
117-
118115
#ifdef DEBUG_LOCKCONTENTION
119116
void PrintLockContention(const char* pszName, const char* pszFile, int nLine);
120117
#endif

src/threadinterrupt.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#include <threadinterrupt.h>
77

8+
#include "sync.h"
9+
810
CThreadInterrupt::operator bool() const
911
{
1012
return flag.load(std::memory_order_acquire);
@@ -18,15 +20,15 @@ void CThreadInterrupt::reset()
1820
void CThreadInterrupt::operator()()
1921
{
2022
{
21-
std::unique_lock<std::mutex> lock(mut);
23+
LOCK(mut);
2224
flag.store(true, std::memory_order_release);
2325
}
2426
cond.notify_all();
2527
}
2628

2729
bool CThreadInterrupt::sleep_for(std::chrono::milliseconds rel_time)
2830
{
29-
std::unique_lock<std::mutex> lock(mut);
31+
WAIT_LOCK(mut, lock);
3032
return !cond.wait_for(lock, rel_time, [this]() { return flag.load(std::memory_order_acquire); });
3133
}
3234

src/threadinterrupt.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#ifndef BITCOIN_THREADINTERRUPT_H
66
#define BITCOIN_THREADINTERRUPT_H
77

8+
#include "sync.h"
9+
810
#include <atomic>
911
#include <chrono>
1012
#include <condition_variable>
@@ -27,7 +29,7 @@ class CThreadInterrupt
2729

2830
private:
2931
std::condition_variable cond;
30-
std::mutex mut;
32+
CWaitableCriticalSection mut;
3133
std::atomic<bool> flag;
3234
};
3335

0 commit comments

Comments
 (0)