diff --git a/src/httpserver.cpp b/src/httpserver.cpp index b50a3cf7fe1..6548d817a33 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -68,7 +68,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 65dd9334596..4c0d2304284 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -305,7 +305,7 @@ void OnRPCStopped() { uiInterface.NotifyBlockTip.disconnect(&RPCNotifyBlockChange); RPCNotifyBlockChange(false, nullptr); - cvBlockChange.notify_all(); + g_best_block_cv.notify_all(); LogPrint("rpc", "RPC stopped.\n"); } @@ -547,17 +547,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 != NULL) { { - LOCK(cs_GenesisWait); + LOCK(g_genesis_wait_mutex); fHaveGenesis = true; } - condvar_GenesisWait.notify_all(); + g_genesis_wait_cv.notify_all(); } } @@ -1668,12 +1668,12 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler) // 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 ec6733d7c52..cd7c082b968 100644 --- a/src/net.h +++ b/src/net.h @@ -399,7 +399,7 @@ class CConnman bool fMsgProcWake; std::condition_variable condMsgProc; - CWaitableCriticalSection mutexMsgProc; + Mutex mutexMsgProc; std::atomic flagInterruptMsgProc; CThreadInterrupt interruptNet; diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 2d7093f424d..9ea249b0a97 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -43,7 +43,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/rpc/mining.cpp b/src/rpc/mining.cpp index 58795317e58..f98ae514b16 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -537,10 +537,10 @@ UniValue getblocktemplate(const JSONRPCRequest& request) { checktxtime = std::chrono::steady_clock::now() + std::chrono::minutes(1); - WAIT_LOCK(csBestBlock, lock); + WAIT_LOCK(g_best_block_mutex, lock); while (chainActive.Tip()->GetBlockHash() == hashWatchedChain && IsRPCRunning()) { - if (cvBlockChange.wait_until(lock, checktxtime) == std::cv_status::timeout) + if (g_best_block_cv.wait_until(lock, checktxtime) == std::cv_status::timeout) { // Timeout: Check transactions for update if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLastLP) diff --git a/src/sync.h b/src/sync.h index bc8c2c1451e..a9ea150f53a 100644 --- a/src/sync.h +++ b/src/sync.h @@ -105,10 +105,7 @@ typedef AnnotatedMixin CCriticalSection; typedef CCriticalSection CDynamicCriticalSection; /** 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); @@ -116,7 +113,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) @@ -142,7 +139,7 @@ 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); @@ -150,7 +147,7 @@ class SCOPED_LOCKABLE CCriticalBlock : public Base 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; @@ -161,7 +158,7 @@ class SCOPED_LOCKABLE CCriticalBlock : public Base Enter(pszName, pszFile, nLine); } - ~CCriticalBlock() UNLOCK_FUNCTION() + ~UniqueLock() UNLOCK_FUNCTION() { if (Base::owns_lock()) LeaveCritical(); @@ -174,7 +171,7 @@ class SCOPED_LOCKABLE CCriticalBlock : public Base }; 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 539e2ff3ab4..df0380546e3 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 7f0dc09f66f..b7d87a10738 100644 --- a/src/threadinterrupt.h +++ b/src/threadinterrupt.h @@ -29,7 +29,7 @@ class CThreadInterrupt private: std::condition_variable cond; - CWaitableCriticalSection mut; + Mutex mut; std::atomic flag; }; diff --git a/src/validation.cpp b/src/validation.cpp index f07dc12dd09..39a7509e723 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -63,8 +63,8 @@ CCriticalSection cs_main; BlockMap mapBlockIndex; CChain chainActive; CBlockIndex *pindexBestHeader = NULL; -CWaitableCriticalSection csBestBlock; -CConditionVariable cvBlockChange; +Mutex g_best_block_mutex; +std::condition_variable g_best_block_cv; int nScriptCheckThreads = 0; std::atomic_bool fImporting(false); bool fReindex = false; @@ -2177,7 +2177,7 @@ void static UpdateTip(CBlockIndex *pindexNew, const CChainParams& chainParams) { // New best block mempool.AddTransactionsUpdated(1); - cvBlockChange.notify_all(); + g_best_block_cv.notify_all(); static bool fWarned = false; std::vector warningMessages; diff --git a/src/validation.h b/src/validation.h index 3ea29a0b9d1..0eb80c71ac2 100644 --- a/src/validation.h +++ b/src/validation.h @@ -171,8 +171,9 @@ extern uint64_t nLastBlockTx; extern uint64_t nLastBlockSize; extern uint64_t nLastBlockWeight; extern const std::string strMessageMagic; -extern CWaitableCriticalSection csBestBlock; -extern CConditionVariable cvBlockChange; +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 bool fReindex; extern int nScriptCheckThreads;