Skip to content

Commit

Permalink
Merge bitcoin#24099: Replace RecursiveMutex cs_mapLocalHost with Mu…
Browse files Browse the repository at this point in the history
…tex, and rename it

5e7e4c9 refactor: replace RecursiveMutex g_maplocalhost_mutex with Mutex (w0xlt)
a7da140 scripted-diff: rename cs_mapLocalHost -> g_maplocalhost_mutex (w0xlt)

Pull request description:

  This PR is related to bitcoin#19303 and gets rid of the `RecursiveMutex cs_mapLocalHost`.

ACKs for top commit:
  shaavan:
    ACK 5e7e4c9
  theStack:
    ACK 5e7e4c9
  hebasto:
    ACK 5e7e4c9, I have reviewed the code and it looks OK, I agree it can be merged.

Tree-SHA512: 961171e346fe385e16db9830115a8096f4ca2499bbea11a08c02ca808638dfb63c434ab9d66392c71e85be6352c8a2b6a0054b5a61aaabd28d71581fed5beae7
  • Loading branch information
MarcoFalke committed Jan 20, 2022
2 parents 63fc2f5 + 5e7e4c9 commit 1824644
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
22 changes: 11 additions & 11 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ static const uint64_t RANDOMIZER_ID_ADDRCACHE = 0x1cf2e4ddd306dda9ULL; // SHA256
//
bool fDiscover = true;
bool fListen = true;
RecursiveMutex cs_mapLocalHost;
std::map<CNetAddr, LocalServiceInfo> mapLocalHost GUARDED_BY(cs_mapLocalHost);
static bool vfLimited[NET_MAX] GUARDED_BY(cs_mapLocalHost) = {};
Mutex g_maplocalhost_mutex;
std::map<CNetAddr, LocalServiceInfo> mapLocalHost GUARDED_BY(g_maplocalhost_mutex);
static bool vfLimited[NET_MAX] GUARDED_BY(g_maplocalhost_mutex) = {};
std::string strSubVersion;

void CConnman::AddAddrFetch(const std::string& strDest)
Expand All @@ -137,7 +137,7 @@ bool GetLocal(CService& addr, const CNetAddr *paddrPeer)
int nBestScore = -1;
int nBestReachability = -1;
{
LOCK(cs_mapLocalHost);
LOCK(g_maplocalhost_mutex);
for (const auto& entry : mapLocalHost)
{
int nScore = entry.second.nScore;
Expand Down Expand Up @@ -193,7 +193,7 @@ CAddress GetLocalAddress(const CNetAddr *paddrPeer, ServiceFlags nLocalServices)

static int GetnScore(const CService& addr)
{
LOCK(cs_mapLocalHost);
LOCK(g_maplocalhost_mutex);
const auto it = mapLocalHost.find(addr);
return (it != mapLocalHost.end()) ? it->second.nScore : 0;
}
Expand Down Expand Up @@ -264,7 +264,7 @@ bool AddLocal(const CService& addr_, int nScore)
LogPrintf("AddLocal(%s,%i)\n", addr.ToString(), nScore);

{
LOCK(cs_mapLocalHost);
LOCK(g_maplocalhost_mutex);
const auto [it, is_newly_added] = mapLocalHost.emplace(addr, LocalServiceInfo());
LocalServiceInfo &info = it->second;
if (is_newly_added || nScore >= info.nScore) {
Expand All @@ -283,7 +283,7 @@ bool AddLocal(const CNetAddr &addr, int nScore)

void RemoveLocal(const CService& addr)
{
LOCK(cs_mapLocalHost);
LOCK(g_maplocalhost_mutex);
LogPrintf("RemoveLocal(%s)\n", addr.ToString());
mapLocalHost.erase(addr);
}
Expand All @@ -292,13 +292,13 @@ void SetReachable(enum Network net, bool reachable)
{
if (net == NET_UNROUTABLE || net == NET_INTERNAL)
return;
LOCK(cs_mapLocalHost);
LOCK(g_maplocalhost_mutex);
vfLimited[net] = !reachable;
}

bool IsReachable(enum Network net)
{
LOCK(cs_mapLocalHost);
LOCK(g_maplocalhost_mutex);
return !vfLimited[net];
}

Expand All @@ -310,7 +310,7 @@ bool IsReachable(const CNetAddr &addr)
/** vote for a local address */
bool SeenLocal(const CService& addr)
{
LOCK(cs_mapLocalHost);
LOCK(g_maplocalhost_mutex);
const auto it = mapLocalHost.find(addr);
if (it == mapLocalHost.end()) return false;
++it->second.nScore;
Expand All @@ -321,7 +321,7 @@ bool SeenLocal(const CService& addr)
/** check whether a given address is potentially local */
bool IsLocal(const CService& addr)
{
LOCK(cs_mapLocalHost);
LOCK(g_maplocalhost_mutex);
return mapLocalHost.count(addr) > 0;
}

Expand Down
4 changes: 2 additions & 2 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ struct LocalServiceInfo {
uint16_t nPort;
};

extern RecursiveMutex cs_mapLocalHost;
extern std::map<CNetAddr, LocalServiceInfo> mapLocalHost GUARDED_BY(cs_mapLocalHost);
extern Mutex g_maplocalhost_mutex;
extern std::map<CNetAddr, LocalServiceInfo> mapLocalHost GUARDED_BY(g_maplocalhost_mutex);

extern const std::string NET_MESSAGE_COMMAND_OTHER;
typedef std::map<std::string, uint64_t> mapMsgCmdSize; //command, total bytes
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ static RPCHelpMan getnetworkinfo()
obj.pushKV("incrementalfee", ValueFromAmount(::incrementalRelayFee.GetFeePerK()));
UniValue localAddresses(UniValue::VARR);
{
LOCK(cs_mapLocalHost);
LOCK(g_maplocalhost_mutex);
for (const std::pair<const CNetAddr, LocalServiceInfo> &item : mapLocalHost)
{
UniValue rec(UniValue::VOBJ);
Expand Down
2 changes: 1 addition & 1 deletion src/test/net_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ BOOST_AUTO_TEST_CASE(ipv4_peer_with_ipv6_addrMe_test)
// that a normal IPv4 address is among the entries, but if this address is
// !IsRoutable the undefined behavior is easier to trigger deterministically
{
LOCK(cs_mapLocalHost);
LOCK(g_maplocalhost_mutex);
in_addr ipv4AddrLocal;
ipv4AddrLocal.s_addr = 0x0100007f;
CNetAddr addr = CNetAddr(ipv4AddrLocal);
Expand Down

0 comments on commit 1824644

Please sign in to comment.