Skip to content

Commit 9c4dc59

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

File tree

11 files changed

+26
-24
lines changed

11 files changed

+26
-24
lines changed

src/httpserver.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class WorkQueue
6969
{
7070
private:
7171
/** Mutex protects entire object */
72-
std::mutex cs;
72+
CWaitableCriticalSection cs;
7373
std::condition_variable cond;
7474
std::deque<std::unique_ptr<WorkItem>> queue;
7575
bool running;
@@ -88,7 +88,7 @@ class WorkQueue
8888
/** Enqueue a work item */
8989
bool Enqueue(WorkItem* item)
9090
{
91-
std::unique_lock<std::mutex> lock(cs);
91+
LOCK(cs);
9292
if (queue.size() >= maxDepth) {
9393
return false;
9494
}
@@ -102,7 +102,7 @@ class WorkQueue
102102
while (true) {
103103
std::unique_ptr<WorkItem> i;
104104
{
105-
std::unique_lock<std::mutex> lock(cs);
105+
WAIT_LOCK(cs, lock);
106106
while (running && queue.empty())
107107
cond.wait(lock);
108108
if (!running)
@@ -116,7 +116,7 @@ class WorkQueue
116116
/** Interrupt and exit loops */
117117
void Interrupt()
118118
{
119-
std::unique_lock<std::mutex> lock(cs);
119+
LOCK(cs);
120120
running = false;
121121
cond.notify_all();
122122
}

src/init.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ static void BlockNotifyGenesisWait(bool, const CBlockIndex *pBlockIndex)
566566
{
567567
if (pBlockIndex != nullptr) {
568568
{
569-
WaitableLock lock_GenesisWait(cs_GenesisWait);
569+
LOCK(cs_GenesisWait);
570570
fHaveGenesis = true;
571571
}
572572
condvar_GenesisWait.notify_all();
@@ -1660,7 +1660,7 @@ bool AppInitMain()
16601660

16611661
// Wait for genesis block to be processed
16621662
{
1663-
WaitableLock lock(cs_GenesisWait);
1663+
WAIT_LOCK(cs_GenesisWait, lock);
16641664
// We previously could hang here if StartShutdown() is called prior to
16651665
// ThreadImport getting started, so instead we just wait on a timer to
16661666
// check ShutdownRequested() regularly.

src/net.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,7 +2064,7 @@ void CConnman::ThreadMessageHandler()
20642064
pnode->Release();
20652065
}
20662066

2067-
std::unique_lock<std::mutex> lock(mutexMsgProc);
2067+
WAIT_LOCK(mutexMsgProc, lock);
20682068
if (!fMoreWork) {
20692069
condMsgProc.wait_until(lock, std::chrono::steady_clock::now() + std::chrono::milliseconds(100), [this] { return fMsgProcWake; });
20702070
}
@@ -2346,7 +2346,7 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
23462346
flagInterruptMsgProc = false;
23472347

23482348
{
2349-
std::unique_lock<std::mutex> lock(mutexMsgProc);
2349+
LOCK(mutexMsgProc);
23502350
fMsgProcWake = false;
23512351
}
23522352

src/net.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ class CConnman
425425
bool fMsgProcWake;
426426

427427
std::condition_variable condMsgProc;
428-
std::mutex mutexMsgProc;
428+
CWaitableCriticalSection mutexMsgProc;
429429
std::atomic<bool> flagInterruptMsgProc;
430430

431431
CThreadInterrupt interruptNet;

src/random.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <wincrypt.h>
1313
#endif
1414
#include <logging.h> // for LogPrint()
15+
#include <sync.h> // for WAIT_LOCK
1516
#include <utiltime.h> // for GetTime()
1617

1718
#include <stdlib.h>
@@ -295,7 +296,7 @@ void RandAddSeedSleep()
295296
}
296297

297298

298-
static std::mutex cs_rng_state;
299+
static CWaitableCriticalSection cs_rng_state;
299300
static unsigned char rng_state[32] = {0};
300301
static uint64_t rng_counter = 0;
301302

@@ -305,7 +306,7 @@ static void AddDataToRng(void* data, size_t len) {
305306
hasher.Write((const unsigned char*)data, len);
306307
unsigned char buf[64];
307308
{
308-
std::unique_lock<std::mutex> lock(cs_rng_state);
309+
WAIT_LOCK(cs_rng_state, lock);
309310
hasher.Write(rng_state, sizeof(rng_state));
310311
hasher.Write((const unsigned char*)&rng_counter, sizeof(rng_counter));
311312
++rng_counter;
@@ -337,7 +338,7 @@ void GetStrongRandBytes(unsigned char* out, int num)
337338

338339
// Combine with and update state
339340
{
340-
std::unique_lock<std::mutex> lock(cs_rng_state);
341+
WAIT_LOCK(cs_rng_state, lock);
341342
hasher.Write(rng_state, sizeof(rng_state));
342343
hasher.Write((const unsigned char*)&rng_counter, sizeof(rng_counter));
343344
++rng_counter;

src/rpc/blockchain.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ struct CUpdatedBlock
4949
int height;
5050
};
5151

52-
static std::mutex cs_blockchange;
52+
static CWaitableCriticalSection cs_blockchange;
5353
static std::condition_variable cond_blockchange;
5454
static CUpdatedBlock latestblock;
5555

@@ -224,7 +224,7 @@ static UniValue waitfornewblock(const JSONRPCRequest& request)
224224

225225
CUpdatedBlock block;
226226
{
227-
std::unique_lock<std::mutex> lock(cs_blockchange);
227+
WAIT_LOCK(cs_blockchange, lock);
228228
block = latestblock;
229229
if(timeout)
230230
cond_blockchange.wait_for(lock, std::chrono::milliseconds(timeout), [&block]{return latestblock.height != block.height || latestblock.hash != block.hash || !IsRPCRunning(); });
@@ -266,7 +266,7 @@ static UniValue waitforblock(const JSONRPCRequest& request)
266266

267267
CUpdatedBlock block;
268268
{
269-
std::unique_lock<std::mutex> lock(cs_blockchange);
269+
WAIT_LOCK(cs_blockchange, lock);
270270
if(timeout)
271271
cond_blockchange.wait_for(lock, std::chrono::milliseconds(timeout), [&hash]{return latestblock.hash == hash || !IsRPCRunning();});
272272
else
@@ -309,7 +309,7 @@ static UniValue waitforblockheight(const JSONRPCRequest& request)
309309

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

src/rpc/mining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ static UniValue getblocktemplate(const JSONRPCRequest& request)
470470
{
471471
checktxtime = std::chrono::steady_clock::now() + std::chrono::minutes(1);
472472

473-
WaitableLock lock(g_best_block_mutex);
473+
WAIT_LOCK(g_best_block_mutex, lock);
474474
while (g_best_block == hashWatchedChain && IsRPCRunning())
475475
{
476476
if (g_best_block_cv.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::CThreadInterrupt() : flag(false) {}
911

1012
CThreadInterrupt::operator bool() const
@@ -20,15 +22,15 @@ void CThreadInterrupt::reset()
2022
void CThreadInterrupt::operator()()
2123
{
2224
{
23-
std::unique_lock<std::mutex> lock(mut);
25+
LOCK(mut);
2426
flag.store(true, std::memory_order_release);
2527
}
2628
cond.notify_all();
2729
}
2830

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

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>
@@ -28,7 +30,7 @@ class CThreadInterrupt
2830

2931
private:
3032
std::condition_variable cond;
31-
std::mutex mut;
33+
CWaitableCriticalSection mut;
3234
std::atomic<bool> flag;
3335
};
3436

0 commit comments

Comments
 (0)