Skip to content

Commit

Permalink
Add thread safety annotations to CAddrMan public functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hebasto committed Jun 14, 2021
1 parent b138973 commit 5ef1d0b
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/addrman.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ class CAddrMan
*/
template <typename Stream>
void Serialize(Stream& s_) const
EXCLUSIVE_LOCKS_REQUIRED(!cs)
{
LOCK(cs);

Expand Down Expand Up @@ -296,6 +297,7 @@ class CAddrMan

template <typename Stream>
void Unserialize(Stream& s_)
EXCLUSIVE_LOCKS_REQUIRED(!cs)
{
LOCK(cs);

Expand Down Expand Up @@ -452,6 +454,7 @@ class CAddrMan
}

void Clear()
EXCLUSIVE_LOCKS_REQUIRED(!cs)
{
LOCK(cs);
std::vector<int>().swap(vRandom);
Expand Down Expand Up @@ -487,13 +490,15 @@ class CAddrMan

//! Return the number of (unique) addresses in all tables.
size_t size() const
EXCLUSIVE_LOCKS_REQUIRED(!cs)
{
LOCK(cs); // TODO: Cache this in an atomic to avoid this overhead
return vRandom.size();
}

//! Add a single address.
bool Add(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty = 0)
EXCLUSIVE_LOCKS_REQUIRED(!cs)
{
LOCK(cs);
bool fRet = false;
Expand All @@ -508,6 +513,7 @@ class CAddrMan

//! Add multiple addresses.
bool Add(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty = 0)
EXCLUSIVE_LOCKS_REQUIRED(!cs)
{
LOCK(cs);
int nAdd = 0;
Expand All @@ -523,6 +529,7 @@ class CAddrMan

//! Mark an entry as accessible.
void Good(const CService &addr, bool test_before_evict = true, int64_t nTime = GetAdjustedTime())
EXCLUSIVE_LOCKS_REQUIRED(!cs)
{
LOCK(cs);
Check();
Expand All @@ -532,6 +539,7 @@ class CAddrMan

//! Mark an entry as connection attempted to.
void Attempt(const CService &addr, bool fCountFailure, int64_t nTime = GetAdjustedTime())
EXCLUSIVE_LOCKS_REQUIRED(!cs)
{
LOCK(cs);
Check();
Expand All @@ -541,6 +549,7 @@ class CAddrMan

//! See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
void ResolveCollisions()
EXCLUSIVE_LOCKS_REQUIRED(!cs)
{
LOCK(cs);
Check();
Expand All @@ -550,6 +559,7 @@ class CAddrMan

//! Randomly select an address in tried that another address is attempting to evict.
CAddrInfo SelectTriedCollision()
EXCLUSIVE_LOCKS_REQUIRED(!cs)
{
LOCK(cs);
Check();
Expand All @@ -562,6 +572,7 @@ class CAddrMan
* Choose an address to connect to.
*/
CAddrInfo Select(bool newOnly = false)
EXCLUSIVE_LOCKS_REQUIRED(!cs)
{
LOCK(cs);
Check();
Expand All @@ -578,6 +589,7 @@ class CAddrMan
* @param[in] network Select only addresses of this network (nullopt = all).
*/
std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network)
EXCLUSIVE_LOCKS_REQUIRED(!cs)
{
LOCK(cs);
Check();
Expand All @@ -589,6 +601,7 @@ class CAddrMan

//! Outer function for Connected_()
void Connected(const CService &addr, int64_t nTime = GetAdjustedTime())
EXCLUSIVE_LOCKS_REQUIRED(!cs)
{
LOCK(cs);
Check();
Expand All @@ -597,6 +610,7 @@ class CAddrMan
}

void SetServices(const CService &addr, ServiceFlags nServices)
EXCLUSIVE_LOCKS_REQUIRED(!cs)
{
LOCK(cs);
Check();
Expand Down

0 comments on commit 5ef1d0b

Please sign in to comment.