Use LOCK macros for non-recursive locks

Instead of std::unique_lock.
This commit is contained in:
Russell Yanofsky 2017-11-08 17:07:40 -05:00
parent 1382913e61
commit 9c4dc597dd
11 changed files with 26 additions and 24 deletions

View file

@ -69,7 +69,7 @@ class WorkQueue
{ {
private: private:
/** Mutex protects entire object */ /** Mutex protects entire object */
std::mutex cs; CWaitableCriticalSection cs;
std::condition_variable cond; std::condition_variable cond;
std::deque<std::unique_ptr<WorkItem>> queue; std::deque<std::unique_ptr<WorkItem>> queue;
bool running; bool running;
@ -88,7 +88,7 @@ public:
/** Enqueue a work item */ /** Enqueue a work item */
bool Enqueue(WorkItem* item) bool Enqueue(WorkItem* item)
{ {
std::unique_lock<std::mutex> lock(cs); LOCK(cs);
if (queue.size() >= maxDepth) { if (queue.size() >= maxDepth) {
return false; return false;
} }
@ -102,7 +102,7 @@ public:
while (true) { while (true) {
std::unique_ptr<WorkItem> i; std::unique_ptr<WorkItem> i;
{ {
std::unique_lock<std::mutex> lock(cs); WAIT_LOCK(cs, lock);
while (running && queue.empty()) while (running && queue.empty())
cond.wait(lock); cond.wait(lock);
if (!running) if (!running)
@ -116,7 +116,7 @@ public:
/** Interrupt and exit loops */ /** Interrupt and exit loops */
void Interrupt() void Interrupt()
{ {
std::unique_lock<std::mutex> lock(cs); LOCK(cs);
running = false; running = false;
cond.notify_all(); cond.notify_all();
} }

View file

@ -566,7 +566,7 @@ static void BlockNotifyGenesisWait(bool, const CBlockIndex *pBlockIndex)
{ {
if (pBlockIndex != nullptr) { if (pBlockIndex != nullptr) {
{ {
WaitableLock lock_GenesisWait(cs_GenesisWait); LOCK(cs_GenesisWait);
fHaveGenesis = true; fHaveGenesis = true;
} }
condvar_GenesisWait.notify_all(); condvar_GenesisWait.notify_all();
@ -1660,7 +1660,7 @@ bool AppInitMain()
// Wait for genesis block to be processed // Wait for genesis block to be processed
{ {
WaitableLock lock(cs_GenesisWait); WAIT_LOCK(cs_GenesisWait, lock);
// We previously could hang here if StartShutdown() is called prior to // We previously could hang here if StartShutdown() is called prior to
// ThreadImport getting started, so instead we just wait on a timer to // ThreadImport getting started, so instead we just wait on a timer to
// check ShutdownRequested() regularly. // check ShutdownRequested() regularly.

View file

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

View file

@ -425,7 +425,7 @@ private:
bool fMsgProcWake; bool fMsgProcWake;
std::condition_variable condMsgProc; std::condition_variable condMsgProc;
std::mutex mutexMsgProc; CWaitableCriticalSection mutexMsgProc;
std::atomic<bool> flagInterruptMsgProc; std::atomic<bool> flagInterruptMsgProc;
CThreadInterrupt interruptNet; CThreadInterrupt interruptNet;

View file

@ -12,6 +12,7 @@
#include <wincrypt.h> #include <wincrypt.h>
#endif #endif
#include <logging.h> // for LogPrint() #include <logging.h> // for LogPrint()
#include <sync.h> // for WAIT_LOCK
#include <utiltime.h> // for GetTime() #include <utiltime.h> // for GetTime()
#include <stdlib.h> #include <stdlib.h>
@ -295,7 +296,7 @@ void RandAddSeedSleep()
} }
static std::mutex cs_rng_state; static CWaitableCriticalSection cs_rng_state;
static unsigned char rng_state[32] = {0}; static unsigned char rng_state[32] = {0};
static uint64_t rng_counter = 0; static uint64_t rng_counter = 0;
@ -305,7 +306,7 @@ static void AddDataToRng(void* data, size_t len) {
hasher.Write((const unsigned char*)data, len); hasher.Write((const unsigned char*)data, len);
unsigned char buf[64]; unsigned char buf[64];
{ {
std::unique_lock<std::mutex> lock(cs_rng_state); WAIT_LOCK(cs_rng_state, lock);
hasher.Write(rng_state, sizeof(rng_state)); hasher.Write(rng_state, sizeof(rng_state));
hasher.Write((const unsigned char*)&rng_counter, sizeof(rng_counter)); hasher.Write((const unsigned char*)&rng_counter, sizeof(rng_counter));
++rng_counter; ++rng_counter;
@ -337,7 +338,7 @@ void GetStrongRandBytes(unsigned char* out, int num)
// Combine with and update state // Combine with and update state
{ {
std::unique_lock<std::mutex> lock(cs_rng_state); WAIT_LOCK(cs_rng_state, lock);
hasher.Write(rng_state, sizeof(rng_state)); hasher.Write(rng_state, sizeof(rng_state));
hasher.Write((const unsigned char*)&rng_counter, sizeof(rng_counter)); hasher.Write((const unsigned char*)&rng_counter, sizeof(rng_counter));
++rng_counter; ++rng_counter;

View file

@ -49,7 +49,7 @@ struct CUpdatedBlock
int height; int height;
}; };
static std::mutex cs_blockchange; static CWaitableCriticalSection cs_blockchange;
static std::condition_variable cond_blockchange; static std::condition_variable cond_blockchange;
static CUpdatedBlock latestblock; static CUpdatedBlock latestblock;
@ -224,7 +224,7 @@ static UniValue waitfornewblock(const JSONRPCRequest& request)
CUpdatedBlock block; CUpdatedBlock block;
{ {
std::unique_lock<std::mutex> lock(cs_blockchange); WAIT_LOCK(cs_blockchange, lock);
block = latestblock; block = latestblock;
if(timeout) if(timeout)
cond_blockchange.wait_for(lock, std::chrono::milliseconds(timeout), [&block]{return latestblock.height != block.height || latestblock.hash != block.hash || !IsRPCRunning(); }); 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)
CUpdatedBlock block; CUpdatedBlock block;
{ {
std::unique_lock<std::mutex> lock(cs_blockchange); WAIT_LOCK(cs_blockchange, lock);
if(timeout) if(timeout)
cond_blockchange.wait_for(lock, std::chrono::milliseconds(timeout), [&hash]{return latestblock.hash == hash || !IsRPCRunning();}); cond_blockchange.wait_for(lock, std::chrono::milliseconds(timeout), [&hash]{return latestblock.hash == hash || !IsRPCRunning();});
else else
@ -309,7 +309,7 @@ static UniValue waitforblockheight(const JSONRPCRequest& request)
CUpdatedBlock block; CUpdatedBlock block;
{ {
std::unique_lock<std::mutex> lock(cs_blockchange); WAIT_LOCK(cs_blockchange, lock);
if(timeout) if(timeout)
cond_blockchange.wait_for(lock, std::chrono::milliseconds(timeout), [&height]{return latestblock.height >= height || !IsRPCRunning();}); cond_blockchange.wait_for(lock, std::chrono::milliseconds(timeout), [&height]{return latestblock.height >= height || !IsRPCRunning();});
else else

View file

@ -470,7 +470,7 @@ static UniValue getblocktemplate(const JSONRPCRequest& request)
{ {
checktxtime = std::chrono::steady_clock::now() + std::chrono::minutes(1); checktxtime = std::chrono::steady_clock::now() + std::chrono::minutes(1);
WaitableLock lock(g_best_block_mutex); WAIT_LOCK(g_best_block_mutex, lock);
while (g_best_block == hashWatchedChain && IsRPCRunning()) while (g_best_block == hashWatchedChain && IsRPCRunning())
{ {
if (g_best_block_cv.wait_until(lock, checktxtime) == std::cv_status::timeout) if (g_best_block_cv.wait_until(lock, checktxtime) == std::cv_status::timeout)

View file

@ -112,9 +112,6 @@ typedef AnnotatedMixin<std::mutex> CWaitableCriticalSection;
/** Just a typedef for std::condition_variable, can be wrapped later if desired */ /** Just a typedef for std::condition_variable, can be wrapped later if desired */
typedef std::condition_variable CConditionVariable; typedef std::condition_variable CConditionVariable;
/** Just a typedef for std::unique_lock, can be wrapped later if desired */
typedef std::unique_lock<std::mutex> WaitableLock;
#ifdef DEBUG_LOCKCONTENTION #ifdef DEBUG_LOCKCONTENTION
void PrintLockContention(const char* pszName, const char* pszFile, int nLine); void PrintLockContention(const char* pszName, const char* pszFile, int nLine);
#endif #endif

View file

@ -5,6 +5,8 @@
#include <threadinterrupt.h> #include <threadinterrupt.h>
#include <sync.h>
CThreadInterrupt::CThreadInterrupt() : flag(false) {} CThreadInterrupt::CThreadInterrupt() : flag(false) {}
CThreadInterrupt::operator bool() const CThreadInterrupt::operator bool() const
@ -20,7 +22,7 @@ void CThreadInterrupt::reset()
void CThreadInterrupt::operator()() void CThreadInterrupt::operator()()
{ {
{ {
std::unique_lock<std::mutex> lock(mut); LOCK(mut);
flag.store(true, std::memory_order_release); flag.store(true, std::memory_order_release);
} }
cond.notify_all(); cond.notify_all();
@ -28,7 +30,7 @@ void CThreadInterrupt::operator()()
bool CThreadInterrupt::sleep_for(std::chrono::milliseconds rel_time) bool CThreadInterrupt::sleep_for(std::chrono::milliseconds rel_time)
{ {
std::unique_lock<std::mutex> lock(mut); WAIT_LOCK(mut, lock);
return !cond.wait_for(lock, rel_time, [this]() { return flag.load(std::memory_order_acquire); }); return !cond.wait_for(lock, rel_time, [this]() { return flag.load(std::memory_order_acquire); });
} }

View file

@ -5,6 +5,8 @@
#ifndef BITCOIN_THREADINTERRUPT_H #ifndef BITCOIN_THREADINTERRUPT_H
#define BITCOIN_THREADINTERRUPT_H #define BITCOIN_THREADINTERRUPT_H
#include <sync.h>
#include <atomic> #include <atomic>
#include <chrono> #include <chrono>
#include <condition_variable> #include <condition_variable>
@ -28,7 +30,7 @@ public:
private: private:
std::condition_variable cond; std::condition_variable cond;
std::mutex mut; CWaitableCriticalSection mut;
std::atomic<bool> flag; std::atomic<bool> flag;
}; };

View file

@ -2254,7 +2254,7 @@ void static UpdateTip(const CBlockIndex *pindexNew, const CChainParams& chainPar
mempool.AddTransactionsUpdated(1); mempool.AddTransactionsUpdated(1);
{ {
WaitableLock lock(g_best_block_mutex); LOCK(g_best_block_mutex);
g_best_block = pindexNew->GetBlockHash(); g_best_block = pindexNew->GetBlockHash();
g_best_block_cv.notify_all(); g_best_block_cv.notify_all();
} }