Skip to content

Commit b6c88a1

Browse files
committed
Use LOCK macros for non-recursive locks
Instead of std::unique_lock.
1 parent 0840766 commit b6c88a1

File tree

10 files changed

+24
-23
lines changed

10 files changed

+24
-23
lines changed

src/httpserver.cpp

Lines changed: 4 additions & 4 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;
@@ -87,7 +87,7 @@ class WorkQueue
8787
/** Enqueue a work item */
8888
bool Enqueue(WorkItem* item)
8989
{
90-
std::unique_lock<std::mutex> lock(cs);
90+
LOCK(cs);
9191
if (queue.size() >= maxDepth) {
9292
return false;
9393
}
@@ -101,7 +101,7 @@ class WorkQueue
101101
while (true) {
102102
std::unique_ptr<WorkItem> i;
103103
{
104-
std::unique_lock<std::mutex> lock(cs);
104+
WAIT_LOCK(cs, lock);
105105
while (running && queue.empty())
106106
cond.wait(lock);
107107
if (!running)
@@ -115,7 +115,7 @@ class WorkQueue
115115
/** Interrupt and exit loops */
116116
void Interrupt()
117117
{
118-
std::unique_lock<std::mutex> lock(cs);
118+
LOCK(cs);
119119
running = false;
120120
cond.notify_all();
121121
}

src/init.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ static void BlockNotifyGenesisWait(bool, const CBlockIndex *pBlockIndex)
557557
{
558558
if (pBlockIndex != nullptr) {
559559
{
560-
WaitableLock lock_GenesisWait(cs_GenesisWait);
560+
LOCK(cs_GenesisWait);
561561
fHaveGenesis = true;
562562
}
563563
condvar_GenesisWait.notify_all();
@@ -1644,7 +1644,7 @@ bool AppInitMain()
16441644

16451645
// Wait for genesis block to be processed
16461646
{
1647-
WaitableLock lock(cs_GenesisWait);
1647+
WAIT_LOCK(cs_GenesisWait, lock);
16481648
// We previously could hang here if StartShutdown() is called prior to
16491649
// ThreadImport getting started, so instead we just wait on a timer to
16501650
// check ShutdownRequested() regularly.

src/net.cpp

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

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

23302330
{
2331-
std::unique_lock<std::mutex> lock(mutexMsgProc);
2331+
LOCK(mutexMsgProc);
23322332
fMsgProcWake = false;
23332333
}
23342334

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
@@ -42,7 +42,7 @@ struct CUpdatedBlock
4242
int height;
4343
};
4444

45-
static std::mutex cs_blockchange;
45+
static CWaitableCriticalSection cs_blockchange;
4646
static std::condition_variable cond_blockchange;
4747
static CUpdatedBlock latestblock;
4848

@@ -226,7 +226,7 @@ UniValue waitfornewblock(const JSONRPCRequest& request)
226226

227227
CUpdatedBlock block;
228228
{
229-
std::unique_lock<std::mutex> lock(cs_blockchange);
229+
WAIT_LOCK(cs_blockchange, lock);
230230
block = latestblock;
231231
if(timeout)
232232
cond_blockchange.wait_for(lock, std::chrono::milliseconds(timeout), [&block]{return latestblock.height != block.height || latestblock.hash != block.hash || !IsRPCRunning(); });
@@ -268,7 +268,7 @@ UniValue waitforblock(const JSONRPCRequest& request)
268268

269269
CUpdatedBlock block;
270270
{
271-
std::unique_lock<std::mutex> lock(cs_blockchange);
271+
WAIT_LOCK(cs_blockchange, lock);
272272
if(timeout)
273273
cond_blockchange.wait_for(lock, std::chrono::milliseconds(timeout), [&hash]{return latestblock.hash == hash || !IsRPCRunning();});
274274
else
@@ -311,7 +311,7 @@ UniValue waitforblockheight(const JSONRPCRequest& request)
311311

312312
CUpdatedBlock block;
313313
{
314-
std::unique_lock<std::mutex> lock(cs_blockchange);
314+
WAIT_LOCK(cs_blockchange, lock);
315315
if(timeout)
316316
cond_blockchange.wait_for(lock, std::chrono::milliseconds(timeout), [&height]{return latestblock.height >= height || !IsRPCRunning();});
317317
else

src/rpc/mining.cpp

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

479-
WaitableLock lock(csBestBlock);
479+
WAIT_LOCK(csBestBlock, lock);
480480
while (chainActive.Tip()->GetBlockHash() == hashWatchedChain && IsRPCRunning())
481481
{
482482
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)