From 944aea8753f862a1f72e351622fd49c8566f4035 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kittywhiskers@users.noreply.github.com> Date: Sat, 5 Jun 2021 00:56:33 +0530 Subject: [PATCH] merge #11599: Small locking rename --- src/httpserver.cpp | 2 +- src/init.cpp | 12 ++++++------ src/net.h | 2 +- src/random.cpp | 2 +- src/rpc/blockchain.cpp | 2 +- src/sync.h | 15 ++++++--------- src/test/sync_tests.cpp | 2 +- src/threadinterrupt.h | 2 +- src/validation.cpp | 4 ++-- src/validation.h | 4 ++-- 10 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/httpserver.cpp b/src/httpserver.cpp index d4b1643118..b7bd24333b 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -74,7 +74,7 @@ class WorkQueue { private: /** Mutex protects entire object */ - CWaitableCriticalSection cs; + Mutex cs; std::condition_variable cond; std::deque> queue; bool running; diff --git a/src/init.cpp b/src/init.cpp index f0ba6f03c8..f726bc46c8 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -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(); } } @@ -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); } diff --git a/src/net.h b/src/net.h index 3ea61e9fef..31391d9ba7 100644 --- a/src/net.h +++ b/src/net.h @@ -620,7 +620,7 @@ private: bool fMsgProcWake; std::condition_variable condMsgProc; - CWaitableCriticalSection mutexMsgProc; + Mutex mutexMsgProc; std::atomic flagInterruptMsgProc; CThreadInterrupt interruptNet; diff --git a/src/random.cpp b/src/random.cpp index bb6fd116e9..c2c1a863bd 100644 --- a/src/random.cpp +++ b/src/random.cpp @@ -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; diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 19026ee337..341829348d 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -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; diff --git a/src/sync.h b/src/sync.h index 324c22d4b6..94152b9080 100644 --- a/src/sync.h +++ b/src/sync.h @@ -107,10 +107,7 @@ public: typedef AnnotatedMixin CCriticalSection; /** Wrapped mutex: supports waiting but not recursive locking */ -typedef AnnotatedMixin CWaitableCriticalSection; - -/** Just a typedef for std::condition_variable, can be wrapped later if desired */ -typedef std::condition_variable CConditionVariable; +typedef AnnotatedMixin Mutex; #ifdef DEBUG_LOCKCONTENTION void PrintLockContention(const char* pszName, const char* pszFile, int nLine); @@ -118,7 +115,7 @@ void PrintLockContention(const char* pszName, const char* pszFile, int nLine); /** Wrapper around std::unique_lock style lock for Mutex. */ template -class SCOPED_LOCKABLE CCriticalBlock : public Base +class SCOPED_LOCKABLE UniqueLock : public Base { private: void Enter(const char* pszName, const char* pszFile, int nLine) @@ -144,7 +141,7 @@ private: } 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); @@ -152,7 +149,7 @@ public: 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; @@ -163,7 +160,7 @@ public: Enter(pszName, pszFile, nLine); } - ~CCriticalBlock() UNLOCK_FUNCTION() + ~UniqueLock() UNLOCK_FUNCTION() { if (Base::owns_lock()) LeaveCritical(); @@ -176,7 +173,7 @@ public: }; template -using DebugLock = CCriticalBlock::type>::type>; +using DebugLock = UniqueLock::type>::type>; #define PASTE(x, y) x ## y #define PASTE2(x, y) PASTE(x, y) diff --git a/src/test/sync_tests.cpp b/src/test/sync_tests.cpp index 60c7b80061..8bf1b96230 100644 --- a/src/test/sync_tests.cpp +++ b/src/test/sync_tests.cpp @@ -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 diff --git a/src/threadinterrupt.h b/src/threadinterrupt.h index 8ba6b12367..1f2a1d599b 100644 --- a/src/threadinterrupt.h +++ b/src/threadinterrupt.h @@ -30,7 +30,7 @@ public: private: std::condition_variable cond; - CWaitableCriticalSection mut; + Mutex mut; std::atomic flag; }; diff --git a/src/validation.cpp b/src/validation.cpp index 07ba069c7a..5df4593e2c 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -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); diff --git a/src/validation.h b/src/validation.h index 2c521a0ba7..08367ab2a1 100644 --- a/src/validation.h +++ b/src/validation.h @@ -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;