Skip to content

Commit

Permalink
Use LOCK macros for non-recursive locks
Browse files Browse the repository at this point in the history
Instead of std::unique_lock.
  • Loading branch information
ryanofsky committed Feb 8, 2018
1 parent 0840766 commit b6c88a1
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 23 deletions.
8 changes: 4 additions & 4 deletions src/httpserver.cpp
Expand Up @@ -68,7 +68,7 @@ class WorkQueue
{
private:
/** Mutex protects entire object */
std::mutex cs;
CWaitableCriticalSection cs;
std::condition_variable cond;
std::deque<std::unique_ptr<WorkItem>> queue;
bool running;
Expand All @@ -87,7 +87,7 @@ class WorkQueue
/** Enqueue a work item */
bool Enqueue(WorkItem* item)
{
std::unique_lock<std::mutex> lock(cs);
LOCK(cs);
if (queue.size() >= maxDepth) {
return false;
}
Expand All @@ -101,7 +101,7 @@ class WorkQueue
while (true) {
std::unique_ptr<WorkItem> i;
{
std::unique_lock<std::mutex> lock(cs);
WAIT_LOCK(cs, lock);
while (running && queue.empty())
cond.wait(lock);
if (!running)
Expand All @@ -115,7 +115,7 @@ class WorkQueue
/** Interrupt and exit loops */
void Interrupt()
{
std::unique_lock<std::mutex> lock(cs);
LOCK(cs);
running = false;
cond.notify_all();
}
Expand Down
4 changes: 2 additions & 2 deletions src/init.cpp
Expand Up @@ -557,7 +557,7 @@ static void BlockNotifyGenesisWait(bool, const CBlockIndex *pBlockIndex)
{
if (pBlockIndex != nullptr) {
{
WaitableLock lock_GenesisWait(cs_GenesisWait);
LOCK(cs_GenesisWait);
fHaveGenesis = true;
}
condvar_GenesisWait.notify_all();
Expand Down Expand Up @@ -1644,7 +1644,7 @@ bool AppInitMain()

// 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
// ThreadImport getting started, so instead we just wait on a timer to
// check ShutdownRequested() regularly.
Expand Down
4 changes: 2 additions & 2 deletions src/net.cpp
Expand Up @@ -2032,7 +2032,7 @@ void CConnman::ThreadMessageHandler()
pnode->Release();
}

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

{
std::unique_lock<std::mutex> lock(mutexMsgProc);
LOCK(mutexMsgProc);
fMsgProcWake = false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/net.h
Expand Up @@ -422,7 +422,7 @@ class CConnman
bool fMsgProcWake;

std::condition_variable condMsgProc;
std::mutex mutexMsgProc;
CWaitableCriticalSection mutexMsgProc;
std::atomic<bool> flagInterruptMsgProc;

CThreadInterrupt interruptNet;
Expand Down
6 changes: 3 additions & 3 deletions src/random.cpp
Expand Up @@ -294,7 +294,7 @@ void RandAddSeedSleep()
}


static std::mutex cs_rng_state;
static CWaitableCriticalSection cs_rng_state;
static unsigned char rng_state[32] = {0};
static uint64_t rng_counter = 0;

Expand All @@ -304,7 +304,7 @@ static void AddDataToRng(void* data, size_t len) {
hasher.Write((const unsigned char*)data, len);
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((const unsigned char*)&rng_counter, sizeof(rng_counter));
++rng_counter;
Expand Down Expand Up @@ -336,7 +336,7 @@ void GetStrongRandBytes(unsigned char* out, int num)

// 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((const unsigned char*)&rng_counter, sizeof(rng_counter));
++rng_counter;
Expand Down
8 changes: 4 additions & 4 deletions src/rpc/blockchain.cpp
Expand Up @@ -42,7 +42,7 @@ struct CUpdatedBlock
int height;
};

static std::mutex cs_blockchange;
static CWaitableCriticalSection cs_blockchange;
static std::condition_variable cond_blockchange;
static CUpdatedBlock latestblock;

Expand Down Expand Up @@ -226,7 +226,7 @@ UniValue waitfornewblock(const JSONRPCRequest& request)

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

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

CUpdatedBlock block;
{
std::unique_lock<std::mutex> lock(cs_blockchange);
WAIT_LOCK(cs_blockchange, lock);
if(timeout)
cond_blockchange.wait_for(lock, std::chrono::milliseconds(timeout), [&height]{return latestblock.height >= height || !IsRPCRunning();});
else
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/mining.cpp
Expand Up @@ -476,7 +476,7 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
{
checktxtime = std::chrono::steady_clock::now() + std::chrono::minutes(1);

WaitableLock lock(csBestBlock);
WAIT_LOCK(csBestBlock, lock);
while (chainActive.Tip()->GetBlockHash() == hashWatchedChain && IsRPCRunning())
{
if (cvBlockChange.wait_until(lock, checktxtime) == std::cv_status::timeout)
Expand Down
3 changes: 0 additions & 3 deletions src/sync.h
Expand Up @@ -112,9 +112,6 @@ typedef AnnotatedMixin<std::mutex> CWaitableCriticalSection;
/** Just a typedef for std::condition_variable, can be wrapped later if desired */
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
void PrintLockContention(const char* pszName, const char* pszFile, int nLine);
#endif
Expand Down
6 changes: 4 additions & 2 deletions src/threadinterrupt.cpp
Expand Up @@ -5,6 +5,8 @@

#include <threadinterrupt.h>

#include <sync.h>

CThreadInterrupt::operator bool() const
{
return flag.load(std::memory_order_acquire);
Expand All @@ -18,15 +20,15 @@ void CThreadInterrupt::reset()
void CThreadInterrupt::operator()()
{
{
std::unique_lock<std::mutex> lock(mut);
LOCK(mut);
flag.store(true, std::memory_order_release);
}
cond.notify_all();
}

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); });
}

Expand Down
4 changes: 3 additions & 1 deletion src/threadinterrupt.h
Expand Up @@ -5,6 +5,8 @@
#ifndef BITCOIN_THREADINTERRUPT_H
#define BITCOIN_THREADINTERRUPT_H

#include <sync.h>

#include <atomic>
#include <chrono>
#include <condition_variable>
Expand All @@ -27,7 +29,7 @@ class CThreadInterrupt

private:
std::condition_variable cond;
std::mutex mut;
CWaitableCriticalSection mut;
std::atomic<bool> flag;
};

Expand Down

0 comments on commit b6c88a1

Please sign in to comment.