From adc7c6cb12f00b1633101b8e360f1eb2fe262838 Mon Sep 17 00:00:00 2001 From: Ilya Savinov Date: Fri, 15 Sep 2017 21:05:03 +0300 Subject: [PATCH] Remove some recursive locks (#1624) * unnecessary recursive lock removed in `VersionBitsTipState` method * interface changed for CRateChecksGuard; unnecessary recursive lock removed --- src/governance.cpp | 8 ++++---- src/governance.h | 19 +++++++++---------- src/validation.cpp | 2 +- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/governance.cpp b/src/governance.cpp index 36f896eb3..bd35053cd 100644 --- a/src/governance.cpp +++ b/src/governance.cpp @@ -277,7 +277,7 @@ void CGovernanceManager::CheckOrphanVotes(CGovernanceObject& govobj, CGovernance std::vector vecVotePairs; mapOrphanVotes.GetAll(nHash, vecVotePairs); - CRateChecksGuard guard(false, *this); + ScopedLockBool guard(cs, fRateChecksEnabled, false); int64_t nNow = GetAdjustedTime(); for(size_t i = 0; i < vecVotePairs.size(); ++i) { @@ -467,7 +467,7 @@ void CGovernanceManager::UpdateCachesAndClean() it->second.fDirtyCache = true; } - CRateChecksGuard guard(false, *this); + ScopedLockBool guard(cs, fRateChecksEnabled, false); // UPDATE CACHE FOR EACH OBJECT THAT IS FLAGGED DIRTYCACHE=TRUE @@ -986,7 +986,7 @@ void CGovernanceManager::CheckMasternodeOrphanVotes() { LOCK2(cs_main, cs); - CRateChecksGuard guard(false, *this); + ScopedLockBool guard(cs, fRateChecksEnabled, false); for(object_m_it it = mapObjects.begin(); it != mapObjects.end(); ++it) { it->second.CheckOrphanVotes(); @@ -997,7 +997,7 @@ void CGovernanceManager::CheckMasternodeOrphanObjects() { LOCK2(cs_main, cs); int64_t nNow = GetAdjustedTime(); - CRateChecksGuard guard(false, *this); + ScopedLockBool guard(cs, fRateChecksEnabled, false); object_info_m_it it = mapMasternodeOrphanObjects.begin(); while(it != mapMasternodeOrphanObjects.end()) { object_info_pair_t& pair = it->second; diff --git a/src/governance.h b/src/governance.h index 6028e452b..6fe8f8fc6 100644 --- a/src/governance.h +++ b/src/governance.h @@ -265,23 +265,22 @@ private: bool fRateChecksEnabled; - class CRateChecksGuard + class ScopedLockBool { - CGovernanceManager& govman; - bool fRateChecksPrev; + bool& ref; + bool fPrevValue; public: - CRateChecksGuard(bool value, CGovernanceManager& gm) : govman(gm) + ScopedLockBool(CCriticalSection& _cs, bool& _ref, bool _value) : ref(_ref) { - ENTER_CRITICAL_SECTION(govman.cs) - fRateChecksPrev = govman.fRateChecksEnabled; - govman.fRateChecksEnabled = value; + AssertLockHeld(_cs); + fPrevValue = ref; + ref = _value; } - ~CRateChecksGuard() + ~ScopedLockBool() { - govman.fRateChecksEnabled = fRateChecksPrev; - LEAVE_CRITICAL_SECTION(govman.cs) + ref = fPrevValue; } }; diff --git a/src/validation.cpp b/src/validation.cpp index 51b002c7b..a510e3da8 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -4367,7 +4367,7 @@ std::string GetWarnings(const std::string& strFor) ThresholdState VersionBitsTipState(const Consensus::Params& params, Consensus::DeploymentPos pos) { - LOCK(cs_main); + AssertLockHeld(cs_main); return VersionBitsState(chainActive.Tip(), params, pos, versionbitscache); }