From b2a9f0a58c173eb6a5dab8c622e39542787f20f8 Mon Sep 17 00:00:00 2001 From: Russell Yanofsky Date: Fri, 3 Nov 2017 07:49:16 -0400 Subject: [PATCH] scripted-diff: Small locking rename Call sync.h primitives "locks" and "mutexes" instead of "blocks" and "waitable critical sections" to match current coding conventions and c++11 standard names. This PR does not rename the "CCriticalSection" class (though this could be done as a followup) because it is used everywhere and would swamp the other changes in this PR. Plain mutexes should mostly be preferred instead of recursive mutexes in new code anyway. -BEGIN VERIFY SCRIPT- set -x set -e ren() { git grep -l $1 | xargs sed -i s/$1/$2/; } ren CCriticalBlock UniqueLock ren CWaitableCriticalSection Mutex ren CConditionVariable std::condition_variable ren cs_GenesisWait g_genesis_wait_mutex ren condvar_GenesisWait g_genesis_wait_cv perl -0777 -pi -e 's/.*typedef.*condition_variable.*\n\n?//g' src/sync.h -END VERIFY SCRIPT- Cherry-picked from: 190bf62be1214b072513c7fd7e01cc191723967c --- src/httpserver.cpp | 2 +- src/init.cpp | 12 ++++++------ src/net.h | 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 | 5 +++-- 9 files changed, 22 insertions(+), 24 deletions(-) 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 7a112045895..881d377b57d 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -553,17 +553,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(); } } @@ -1677,12 +1677,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 5decd27a98b..99292657882 100644 --- a/src/net.h +++ b/src/net.h @@ -395,7 +395,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 fa3f59f88b3..aa19c7a1369 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/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 3d073c92870..00043d6d80d 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 csBestBlock; +std::condition_variable cvBlockChange; int nScriptCheckThreads = 0; std::atomic_bool fImporting(false); bool fReindex = false; diff --git a/src/validation.h b/src/validation.h index 84920d786c5..af8ab9d2360 100644 --- a/src/validation.h +++ b/src/validation.h @@ -169,8 +169,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 csBestBlock; +extern std::condition_variable cvBlockChange; +extern uint256 g_best_block; extern std::atomic_bool fImporting; extern bool fReindex; extern int nScriptCheckThreads;