diff --git a/src/avalanche.h b/src/avalanche.h --- a/src/avalanche.h +++ b/src/avalanche.h @@ -315,7 +315,7 @@ std::atomic stopRequest; bool running GUARDED_BY(cs_running); - CWaitableCriticalSection cs_running; + Mutex cs_running; std::condition_variable cond_running; public: diff --git a/src/httpserver.cpp b/src/httpserver.cpp --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -72,7 +72,7 @@ template 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 --- a/src/init.cpp +++ b/src/init.cpp @@ -965,16 +965,16 @@ } 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(); } } @@ -2270,12 +2270,12 @@ // 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 --- a/src/net.h +++ b/src/net.h @@ -435,7 +435,7 @@ 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 --- a/src/random.cpp +++ b/src/random.cpp @@ -307,7 +307,7 @@ memory_cleanse(&nPerfCounter2, sizeof(nPerfCounter2)); } -static CWaitableCriticalSection cs_rng_state; +static Mutex cs_rng_state; static uint8_t rng_state[32] = {0}; static uint64_t rng_counter = 0; diff --git a/src/rcu.cpp b/src/rcu.cpp --- a/src/rcu.cpp +++ b/src/rcu.cpp @@ -183,7 +183,7 @@ // the thread causing synchronization delay so this thread can be waked up // at an apropriate time. static std::condition_variable cond; - static CWaitableCriticalSection cs; + static Mutex cs; WAIT_LOCK(cs, lock); do { diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -38,7 +38,7 @@ 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 --- a/src/sync.h +++ b/src/sync.h @@ -104,12 +104,7 @@ 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); @@ -117,7 +112,7 @@ /** 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) { EnterCritical(pszName, pszFile, nLine, (void *)(Base::mutex())); @@ -139,37 +134,41 @@ } public: - CCriticalBlock(Mutex &mutexIn, const char *pszName, const char *pszFile, - int nLine, bool fTry = false) - EXCLUSIVE_LOCK_FUNCTION(mutexIn) + 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) + if (fTry) { TryEnter(pszName, pszFile, nLine); - else + } else { Enter(pszName, pszFile, nLine); + } } - CCriticalBlock(Mutex *pmutexIn, const char *pszName, const char *pszFile, - int nLine, bool fTry = false) - EXCLUSIVE_LOCK_FUNCTION(pmutexIn) { - if (!pmutexIn) return; + UniqueLock(Mutex *pmutexIn, const char *pszName, const char *pszFile, + int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn) { + if (!pmutexIn) { + return; + } *static_cast(this) = Base(*pmutexIn, std::defer_lock); - if (fTry) + if (fTry) { TryEnter(pszName, pszFile, nLine); - else + } else { Enter(pszName, pszFile, nLine); + } } - ~CCriticalBlock() UNLOCK_FUNCTION() { - if (Base::owns_lock()) LeaveCritical(); + ~UniqueLock() UNLOCK_FUNCTION() { + if (Base::owns_lock()) { + LeaveCritical(); + } } operator bool() { return Base::owns_lock(); } }; template -using DebugLock = CCriticalBlock::type>::type>; #define PASTE(x, y) x##y diff --git a/src/test/rcu_tests.cpp b/src/test/rcu_tests.cpp --- a/src/test/rcu_tests.cpp +++ b/src/test/rcu_tests.cpp @@ -40,8 +40,8 @@ [&] { return otherstep == step; })) void synchronize(std::atomic &step, - const std::atomic &otherstep, - CWaitableCriticalSection &cs, std::condition_variable &cond, + const std::atomic &otherstep, Mutex &cs, + std::condition_variable &cond, std::atomic &syncRev) { assert(step == RCUTestStep::Init); @@ -76,8 +76,7 @@ void lockAndWaitForSynchronize(std::atomic &step, const std::atomic &otherstep, - CWaitableCriticalSection &cs, - std::condition_variable &cond, + Mutex &cs, std::condition_variable &cond, std::atomic &syncRev) { assert(step == RCUTestStep::Init); WAIT_LOCK(cs, lock); @@ -111,7 +110,7 @@ static const int COUNT = 128; BOOST_AUTO_TEST_CASE(synchronize_test) { - CWaitableCriticalSection cs; + Mutex cs; std::condition_variable cond; std::atomic parentstep; std::atomic childstep; diff --git a/src/test/sync_tests.cpp b/src/test/sync_tests.cpp --- a/src/test/sync_tests.cpp +++ b/src/test/sync_tests.cpp @@ -37,7 +37,7 @@ 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 --- a/src/threadinterrupt.h +++ b/src/threadinterrupt.h @@ -28,7 +28,7 @@ private: std::condition_variable cond; - CWaitableCriticalSection mut; + Mutex mut; std::atomic flag; }; diff --git a/src/validation.h b/src/validation.h --- a/src/validation.h +++ b/src/validation.h @@ -214,8 +214,8 @@ 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; diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -174,8 +174,8 @@ BlockMap &mapBlockIndex = g_chainstate.mapBlockIndex; 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);