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 Dec 7, 2017
1 parent 0791fac commit f190dd6
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 24 deletions.
10 changes: 5 additions & 5 deletions src/httpserver.cpp
Original file line number Diff line number Diff line change
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 Down Expand Up @@ -108,7 +108,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 @@ -123,7 +123,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 @@ -137,14 +137,14 @@ class WorkQueue
/** Interrupt and exit loops */
void Interrupt()
{
std::unique_lock<std::mutex> lock(cs);
LOCK(cs);
running = false;
cond.notify_all();
}
/** Wait for worker threads to exit */
void WaitExit()
{
std::unique_lock<std::mutex> lock(cs);
WAIT_LOCK(cs, lock);
while (numThreads > 0)
cond.wait(lock);
}
Expand Down
4 changes: 2 additions & 2 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,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 @@ -1630,7 +1630,7 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)

// Wait for genesis block to be processed
{
WaitableLock lock(cs_GenesisWait);
WAIT_LOCK(cs_GenesisWait, lock);
while (!fHaveGenesis) {
condvar_GenesisWait.wait(lock);
}
Expand Down
4 changes: 2 additions & 2 deletions src/net.cpp
Original file line number Diff line number Diff line change
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 @@ -2347,7 +2347,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
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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 @@ -218,7 +218,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 @@ -260,7 +260,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 @@ -303,7 +303,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
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,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
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 f190dd6

Please sign in to comment.