Skip to content

Commit

Permalink
merge bitcoin#11599: Small locking rename
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg committed Jun 6, 2021
1 parent c5c3dee commit 944aea8
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/httpserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class WorkQueue
{
private:
/** Mutex protects entire object */
CWaitableCriticalSection cs;
Mutex cs;
std::condition_variable cond;
std::deque<std::unique_ptr<WorkItem>> queue;
bool running;
Expand Down
12 changes: 6 additions & 6 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -702,17 +702,17 @@ static void BlockNotifyCallback(bool initialSync, const CBlockIndex *pBlockIndex
}

static bool fHaveGenesis = false;
static CWaitableCriticalSection cs_GenesisWait;
static CConditionVariable condvar_GenesisWait;
static Mutex g_genesis_wait_mutex;
static std::condition_variable g_genesis_wait_cv;

static void BlockNotifyGenesisWait(bool, const CBlockIndex *pBlockIndex)
{
if (pBlockIndex != nullptr) {
{
LOCK(cs_GenesisWait);
LOCK(g_genesis_wait_mutex);
fHaveGenesis = true;
}
condvar_GenesisWait.notify_all();
g_genesis_wait_cv.notify_all();
}
}

Expand Down Expand Up @@ -2377,12 +2377,12 @@ bool AppInitMain()

// Wait for genesis block to be processed
{
WAIT_LOCK(cs_GenesisWait, lock);
WAIT_LOCK(g_genesis_wait_mutex, 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.
while (!fHaveGenesis && !ShutdownRequested()) {
condvar_GenesisWait.wait_for(lock, std::chrono::milliseconds(500));
g_genesis_wait_cv.wait_for(lock, std::chrono::milliseconds(500));
}
uiInterface.NotifyBlockTip.disconnect(BlockNotifyGenesisWait);
}
Expand Down
2 changes: 1 addition & 1 deletion src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ friend class CNode;
bool fMsgProcWake;

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

CThreadInterrupt interruptNet;
Expand Down
2 changes: 1 addition & 1 deletion src/random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ void RandAddSeedSleep()
}


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

Expand Down
2 changes: 1 addition & 1 deletion src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct CUpdatedBlock
int height;
};

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

Expand Down
15 changes: 6 additions & 9 deletions src/sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,15 @@ class LOCKABLE AnnotatedMixin : public PARENT
typedef AnnotatedMixin<std::recursive_mutex> CCriticalSection;

/** Wrapped mutex: supports waiting but not recursive locking */
typedef AnnotatedMixin<std::mutex> CWaitableCriticalSection;

/** Just a typedef for std::condition_variable, can be wrapped later if desired */
typedef std::condition_variable CConditionVariable;
typedef AnnotatedMixin<std::mutex> Mutex;

#ifdef DEBUG_LOCKCONTENTION
void PrintLockContention(const char* pszName, const char* pszFile, int nLine);
#endif

/** Wrapper around std::unique_lock style lock for Mutex. */
template <typename Mutex, typename Base = typename Mutex::UniqueLock>
class SCOPED_LOCKABLE CCriticalBlock : public Base
class SCOPED_LOCKABLE UniqueLock : public Base
{
private:
void Enter(const char* pszName, const char* pszFile, int nLine)
Expand All @@ -144,15 +141,15 @@ class SCOPED_LOCKABLE CCriticalBlock : public Base
}

public:
CCriticalBlock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(mutexIn) : Base(mutexIn, std::defer_lock)
UniqueLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(mutexIn) : Base(mutexIn, std::defer_lock)
{
if (fTry)
TryEnter(pszName, pszFile, nLine);
else
Enter(pszName, pszFile, nLine);
}

CCriticalBlock(Mutex* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
UniqueLock(Mutex* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
{
if (!pmutexIn) return;

Expand All @@ -163,7 +160,7 @@ class SCOPED_LOCKABLE CCriticalBlock : public Base
Enter(pszName, pszFile, nLine);
}

~CCriticalBlock() UNLOCK_FUNCTION()
~UniqueLock() UNLOCK_FUNCTION()
{
if (Base::owns_lock())
LeaveCritical();
Expand All @@ -176,7 +173,7 @@ class SCOPED_LOCKABLE CCriticalBlock : public Base
};

template<typename MutexArg>
using DebugLock = CCriticalBlock<typename std::remove_reference<typename std::remove_pointer<MutexArg>::type>::type>;
using DebugLock = UniqueLock<typename std::remove_reference<typename std::remove_pointer<MutexArg>::type>::type>;

#define PASTE(x, y) x ## y
#define PASTE2(x, y) PASTE(x, y)
Expand Down
2 changes: 1 addition & 1 deletion src/test/sync_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ BOOST_AUTO_TEST_CASE(potential_deadlock_detected)
CCriticalSection rmutex1, rmutex2;
TestPotentialDeadLockDetected(rmutex1, rmutex2);

CWaitableCriticalSection mutex1, mutex2;
Mutex mutex1, mutex2;
TestPotentialDeadLockDetected(mutex1, mutex2);

#ifdef DEBUG_LOCKORDER
Expand Down
2 changes: 1 addition & 1 deletion src/threadinterrupt.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class CThreadInterrupt

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

Expand Down
4 changes: 2 additions & 2 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ BlockMap& mapBlockIndex = g_chainstate.mapBlockIndex;
PrevBlockMap& mapPrevBlockIndex = g_chainstate.mapPrevBlockIndex;
CChain& chainActive = g_chainstate.chainActive;
CBlockIndex *pindexBestHeader = nullptr;
CWaitableCriticalSection g_best_block_mutex;
CConditionVariable g_best_block_cv;
Mutex g_best_block_mutex;
std::condition_variable g_best_block_cv;
uint256 g_best_block;
int nScriptCheckThreads = 0;
std::atomic_bool fImporting(false);
Expand Down
4 changes: 2 additions & 2 deletions src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ extern PrevBlockMap& mapPrevBlockIndex;
extern uint64_t nLastBlockTx;
extern uint64_t nLastBlockSize;
extern const std::string strMessageMagic;
extern CWaitableCriticalSection g_best_block_mutex;
extern CConditionVariable g_best_block_cv;
extern Mutex g_best_block_mutex;
extern std::condition_variable g_best_block_cv;
extern uint256 g_best_block;
extern std::atomic_bool fImporting;
extern std::atomic_bool fReindex;
Expand Down

0 comments on commit 944aea8

Please sign in to comment.