diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index 36adf154f1..b710c605bc 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -704,7 +704,7 @@ static RPCHelpMan getblocktemplate() NodeContext& node = EnsureAnyNodeContext(request.context); ChainstateManager& chainman = EnsureChainman(node); Mining& miner = EnsureMining(node); - LOCK(cs_main); + WAIT_LOCK(cs_main, csmain_lock); uint256 tip{CHECK_NONFATAL(miner.getTip()).value().hash}; std::string strMode = "template"; @@ -810,8 +810,8 @@ static RPCHelpMan getblocktemplate() } // Release lock while waiting - LEAVE_CRITICAL_SECTION(cs_main); { + REVERSE_LOCK(csmain_lock, cs_main); MillisecondsDouble checktxtime{std::chrono::minutes(1)}; while (IsRPCRunning()) { // If hashWatchedChain is not a real block hash, this will @@ -830,8 +830,6 @@ static RPCHelpMan getblocktemplate() checktxtime = std::chrono::seconds(10); } } - ENTER_CRITICAL_SECTION(cs_main); - tip = CHECK_NONFATAL(miner.getTip()).value().hash; if (!IsRPCRunning()) diff --git a/src/sync.cpp b/src/sync.cpp index e5be6fd115..fb60e3cf14 100644 --- a/src/sync.cpp +++ b/src/sync.cpp @@ -206,8 +206,6 @@ void EnterCritical(const char* pszName, const char* pszFile, int nLine, MutexTyp { push_lock(cs, CLockLocation(pszName, pszFile, nLine, fTry, util::ThreadGetInternalName())); } -template void EnterCritical(const char*, const char*, int, Mutex*, bool); -template void EnterCritical(const char*, const char*, int, RecursiveMutex*, bool); template void EnterCritical(const char*, const char*, int, std::mutex*, bool); template void EnterCritical(const char*, const char*, int, std::recursive_mutex*, bool); diff --git a/src/sync.h b/src/sync.h index ee189bee10..f863e5f582 100644 --- a/src/sync.h +++ b/src/sync.h @@ -39,12 +39,6 @@ LOCK2(mutex1, mutex2); TRY_LOCK(mutex, name); std::unique_lock name(mutex, std::try_to_lock_t); - -ENTER_CRITICAL_SECTION(mutex); // no RAII - mutex.lock(); - -LEAVE_CRITICAL_SECTION(mutex); // no RAII - mutex.unlock(); */ /////////////////////////////// @@ -270,20 +264,6 @@ inline MutexType* MaybeCheckNotHeld(MutexType* m) LOCKS_EXCLUDED(m) LOCK_RETURNE #define TRY_LOCK(cs, name) UniqueLock name(LOCK_ARGS(cs), true) #define WAIT_LOCK(cs, name) UniqueLock name(LOCK_ARGS(cs)) -#define ENTER_CRITICAL_SECTION(cs) \ - { \ - EnterCritical(#cs, __FILE__, __LINE__, &cs); \ - (cs).lock(); \ - } - -#define LEAVE_CRITICAL_SECTION(cs) \ - { \ - std::string lockname; \ - CheckLastCritical((void*)(&cs), lockname, #cs, __FILE__, __LINE__); \ - (cs).unlock(); \ - LeaveCritical(); \ - } - //! Run code while locking a mutex. //! //! Examples: diff --git a/src/test/reverselock_tests.cpp b/src/test/reverselock_tests.cpp index 64b3ddf2cd..6abf55c8f7 100644 --- a/src/test/reverselock_tests.cpp +++ b/src/test/reverselock_tests.cpp @@ -17,8 +17,10 @@ BOOST_AUTO_TEST_CASE(reverselock_basics) WAIT_LOCK(mutex, lock); BOOST_CHECK(lock.owns_lock()); + AssertLockHeld(mutex); { REVERSE_LOCK(lock, mutex); + AssertLockNotHeld(mutex); BOOST_CHECK(!lock.owns_lock()); } BOOST_CHECK(lock.owns_lock()); diff --git a/src/test/sync_tests.cpp b/src/test/sync_tests.cpp index 0576bf1633..dcd1b1ce36 100644 --- a/src/test/sync_tests.cpp +++ b/src/test/sync_tests.cpp @@ -37,8 +37,7 @@ void TestPotentialDeadLockDetected(MutexType& mutex1, MutexType& mutex2) template void TestDoubleLock2(MutexType& m) { - ENTER_CRITICAL_SECTION(m); - LEAVE_CRITICAL_SECTION(m); + LOCK(m); } template @@ -48,15 +47,15 @@ void TestDoubleLock(bool should_throw) g_debug_lockorder_abort = false; MutexType m; - ENTER_CRITICAL_SECTION(m); - if (should_throw) { - BOOST_CHECK_EXCEPTION(TestDoubleLock2(m), std::logic_error, + { + LOCK(m); + if (should_throw) { + BOOST_CHECK_EXCEPTION(TestDoubleLock2(m), std::logic_error, HasReason("double lock detected")); - } else { - BOOST_CHECK_NO_THROW(TestDoubleLock2(m)); + } else { + BOOST_CHECK_NO_THROW(TestDoubleLock2(m)); + } } - LEAVE_CRITICAL_SECTION(m); - BOOST_CHECK(LockStackEmpty()); g_debug_lockorder_abort = prev; @@ -64,15 +63,15 @@ void TestDoubleLock(bool should_throw) #endif /* DEBUG_LOCKORDER */ template -void TestInconsistentLockOrderDetected(MutexType& mutex1, MutexType& mutex2) NO_THREAD_SAFETY_ANALYSIS +void TestInconsistentLockOrderDetected(MutexType& mutex1, MutexType& mutex2) { - ENTER_CRITICAL_SECTION(mutex1); - ENTER_CRITICAL_SECTION(mutex2); + { + WAIT_LOCK(mutex1, lock1); + LOCK(mutex2); #ifdef DEBUG_LOCKORDER - BOOST_CHECK_EXCEPTION(LEAVE_CRITICAL_SECTION(mutex1), std::logic_error, HasReason("mutex1 was not most recent critical section locked")); + BOOST_CHECK_EXCEPTION(REVERSE_LOCK(lock1, mutex1), std::logic_error, HasReason("mutex1 was not most recent critical section locked")); #endif // DEBUG_LOCKORDER - LEAVE_CRITICAL_SECTION(mutex2); - LEAVE_CRITICAL_SECTION(mutex1); + } BOOST_CHECK(LockStackEmpty()); } } // namespace