Skip to content

Commit

Permalink
Adjust locking
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescowens committed Jan 18, 2025
1 parent 64cc435 commit 7acd87d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
27 changes: 14 additions & 13 deletions src/gridcoin/project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,26 +326,33 @@ AutoGreylist::AutoGreylist()
: m_greylist_ptr(std::make_shared<Greylist>())
, m_superblock_hash(uint256 {})
{
//Refresh();
}

AutoGreylist::Greylist::const_iterator AutoGreylist::begin() const
{
LOCK(autogreylist_lock);

return m_greylist_ptr->begin();
}

AutoGreylist::Greylist::const_iterator AutoGreylist::end() const
{
LOCK(autogreylist_lock);

return m_greylist_ptr->end();
}

AutoGreylist::Greylist::size_type AutoGreylist::size() const
{
LOCK(autogreylist_lock);

return m_greylist_ptr->size();
}

bool AutoGreylist::Contains(const std::string& name, const bool& only_auto_greylisted) const
{
LOCK(autogreylist_lock);

if (m_greylist_ptr == nullptr) {
return false;
}
Expand All @@ -367,10 +374,8 @@ bool AutoGreylist::Contains(const std::string& name, const bool& only_auto_greyl
}
}

void AutoGreylist::Refresh()
void AutoGreylist::Refresh() EXCLUSIVE_LOCKS_REQUIRED (cs_main)
{
LOCK(cs_main);

// If the current superblock has not changed, then no need to do anything.
if (!m_superblock_hash.IsNull() && Quorum::CurrentSuperblock()->GetHash() == m_superblock_hash) {
return;
Expand All @@ -383,10 +388,8 @@ void AutoGreylist::Refresh()
}
}

void AutoGreylist::RefreshWithSuperblock(SuperblockPtr superblock_ptr_in)
void AutoGreylist::RefreshWithSuperblock(SuperblockPtr superblock_ptr_in) EXCLUSIVE_LOCKS_REQUIRED (cs_main)
{
LOCK(lock);

if (superblock_ptr_in.IsEmpty()) {
return;
}
Expand All @@ -397,6 +400,8 @@ void AutoGreylist::RefreshWithSuperblock(SuperblockPtr superblock_ptr_in)
// with the underlying whitelist state.
const WhitelistSnapshot whitelist = GetWhitelist().Snapshot(GRC::ProjectEntry::ProjectFilterFlag::ALL_BUT_DELETED, false, false);

LOCK(autogreylist_lock);

m_greylist_ptr->clear();

// No need to go further if the whitelist is empty (ignoring deleted records).
Expand Down Expand Up @@ -516,7 +521,7 @@ void AutoGreylist::RefreshWithSuperblock(SuperblockPtr superblock_ptr_in)
}
}

void AutoGreylist::RefreshWithSuperblock(Superblock& superblock)
void AutoGreylist::RefreshWithSuperblock(Superblock& superblock) EXCLUSIVE_LOCKS_REQUIRED (cs_main)
{
SuperblockPtr superblock_ptr;

Expand All @@ -526,11 +531,7 @@ void AutoGreylist::RefreshWithSuperblock(Superblock& superblock)
// overloaded version which takes the superblock_ptr and follows the chain backwards to do the greylist calculations.
superblock_ptr.Replace(superblock);

{
LOCK(cs_main);

superblock_ptr.Rebind(pindexBest);
}
superblock_ptr.Rebind(pindexBest);

RefreshWithSuperblock(superblock_ptr);

Expand Down
2 changes: 1 addition & 1 deletion src/gridcoin/project.h
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ class AutoGreylist
static std::shared_ptr<AutoGreylist> GetAutoGreylistCache();

private:
CCriticalSection lock;
mutable CCriticalSection autogreylist_lock;

GreylistPtr m_greylist_ptr;
uint256 m_superblock_hash;
Expand Down

0 comments on commit 7acd87d

Please sign in to comment.