Merge #19979: Replace LockAssertion with AssertLockHeld, remove LockAssertion

0bd1184adf6610c0bd14f4e9a25c0a200e65ae25 Remove unused LockAssertion struct (Hennadii Stepanov)
ab2a44297fd0796bf5797ae2a477e8e56d9c3c12 Replace LockAssertion with a proper thread safety annotations (Hennadii Stepanov)
73f71e19965e07534eb47701f2b23c9ed59ef475 refactor: Use explicit function type instead of template (Hennadii Stepanov)

Pull request description:

  This PR replaces `LockAssertion` with `AssertLockHeld`, and removes `LockAssertion`.

  This PR is compared with alternatives in https://github.com/bitcoin-core/bitcoin-devwiki/wiki/AssertLockHeld-PRs

ACKs for top commit:
  MarcoFalke:
    ACK 0bd1184adf6610c0bd14f4e9a25c0a200e65ae25
  ajtowns:
    ACK 0bd1184adf6610c0bd14f4e9a25c0a200e65ae25
  vasild:
    ACK 0bd1184ad

Tree-SHA512: ef7780dd689faf0bb479fdb97c49bc652e2dd10c148234bb95502dfbb676442d8565ee37864d923ca21a25f9dc2a335bf46ee82c095e387b59a664ab05c0ae41
This commit is contained in:
MarcoFalke 2020-09-23 16:36:52 +02:00 committed by Konstantin Akimov
parent d0a4198166
commit 1db9e6ac76
No known key found for this signature in database
GPG Key ID: 2176C4A5D01EA524
5 changed files with 16 additions and 49 deletions

View File

@ -946,25 +946,6 @@ bool ChainstateManager::ProcessNewBlock(...)
} }
``` ```
- When Clang Thread Safety Analysis is unable to determine if a mutex is locked, use `LockAssertion` class instances:
```C++
// net_processing.h
void RelayTransaction(...) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
// net_processing.cpp
void RelayTransaction(...)
{
AssertLockHeld(::cs_main);
connman.ForEachNode([&txid, &wtxid](CNode* pnode) {
LockAssertion lock(::cs_main);
...
});
}
```
- Build and run tests with `-DDEBUG_LOCKORDER` to verify that no potential - Build and run tests with `-DDEBUG_LOCKORDER` to verify that no potential
deadlocks are introduced. deadlocks are introduced.

View File

@ -3510,8 +3510,8 @@ void CConnman::ThreadOpenMasternodeConnections(CDeterministicMNManager& dmnman,
MasternodeProbeConn isProbe = MasternodeProbeConn::IsNotConnection; MasternodeProbeConn isProbe = MasternodeProbeConn::IsNotConnection;
const auto getPendingQuorumNodes = [&]() { const auto getPendingQuorumNodes = [&]() EXCLUSIVE_LOCKS_REQUIRED(cs_vPendingMasternodes) {
LockAssertion lock(cs_vPendingMasternodes); AssertLockHeld(cs_vPendingMasternodes);
std::vector<CDeterministicMNCPtr> ret; std::vector<CDeterministicMNCPtr> ret;
for (const auto& group : masternodeQuorumNodes) { for (const auto& group : masternodeQuorumNodes) {
for (const auto& proRegTxHash : group.second) { for (const auto& proRegTxHash : group.second) {
@ -3549,8 +3549,8 @@ void CConnman::ThreadOpenMasternodeConnections(CDeterministicMNManager& dmnman,
return ret; return ret;
}; };
const auto getPendingProbes = [&]() { const auto getPendingProbes = [&]() EXCLUSIVE_LOCKS_REQUIRED(cs_vPendingMasternodes) {
LockAssertion lock(cs_vPendingMasternodes); AssertLockHeld(cs_vPendingMasternodes);
std::vector<CDeterministicMNCPtr> ret; std::vector<CDeterministicMNCPtr> ret;
for (auto it = masternodePendingProbes.begin(); it != masternodePendingProbes.end(); ) { for (auto it = masternodePendingProbes.begin(); it != masternodePendingProbes.end(); ) {
auto dmn = mnList.GetMN(*it); auto dmn = mnList.GetMN(*it);

View File

@ -1275,6 +1275,8 @@ public:
return ForNode(id, FullyConnectedOnly, func); return ForNode(id, FullyConnectedOnly, func);
} }
using NodeFn = std::function<void(CNode*)>;
bool IsConnected(const CService& addr, std::function<bool(const CNode* pnode)> cond) bool IsConnected(const CService& addr, std::function<bool(const CNode* pnode)> cond)
{ {
return ForNode(addr, cond, [](CNode* pnode){ return ForNode(addr, cond, [](CNode* pnode){
@ -1331,10 +1333,9 @@ public:
} }
}; };
template<typename Callable> void ForEachNode(const NodeFn& fn)
void ForEachNode(Callable&& func)
{ {
ForEachNode(FullyConnectedOnly, func); ForEachNode(FullyConnectedOnly, fn);
} }
template<typename Condition, typename Callable> template<typename Condition, typename Callable>
@ -1347,10 +1348,9 @@ public:
} }
}; };
template<typename Callable> void ForEachNode(const NodeFn& fn) const
void ForEachNode(Callable&& func) const
{ {
ForEachNode(FullyConnectedOnly, func); ForEachNode(FullyConnectedOnly, fn);
} }
template<typename Condition, typename Callable, typename CallableAfter> template<typename Condition, typename Callable, typename CallableAfter>

View File

@ -2000,8 +2000,8 @@ void PeerManagerImpl::NewPoWValidBlock(const CBlockIndex *pindex, const std::sha
m_most_recent_compact_block = pcmpctblock; m_most_recent_compact_block = pcmpctblock;
} }
m_connman.ForEachNode([this, pindex, &lazy_ser, &hashBlock](CNode* pnode) { m_connman.ForEachNode([this, pindex, &lazy_ser, &hashBlock](CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
LockAssertion lock(::cs_main); AssertLockHeld(::cs_main);
// TODO: Avoid the repeated-serialization here // TODO: Avoid the repeated-serialization here
if (pnode->fDisconnect) if (pnode->fDisconnect)
return; return;
@ -5275,8 +5275,8 @@ void PeerManagerImpl::EvictExtraOutboundPeers(std::chrono::seconds now)
// We want to prevent recently connected to Onion peers from being disconnected here, protect them as long as // We want to prevent recently connected to Onion peers from being disconnected here, protect them as long as
// there are more non_onion nodes than onion nodes so far // there are more non_onion nodes than onion nodes so far
size_t onion_count = 0; size_t onion_count = 0;
m_connman.ForEachNode([&](CNode* pnode) { m_connman.ForEachNode([&](CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
LockAssertion lock(::cs_main); AssertLockHeld(::cs_main);
if (pnode->addr.IsTor() && ++onion_count <= m_connman.GetMaxOutboundOnionNodeCount()) return; if (pnode->addr.IsTor() && ++onion_count <= m_connman.GetMaxOutboundOnionNodeCount()) return;
// Don't disconnect masternodes just because they were slow in block announcement // Don't disconnect masternodes just because they were slow in block announcement
if (pnode->m_masternode_connection) return; if (pnode->m_masternode_connection) return;
@ -5293,8 +5293,8 @@ void PeerManagerImpl::EvictExtraOutboundPeers(std::chrono::seconds now)
} }
}); });
if (worst_peer != -1) { if (worst_peer != -1) {
bool disconnected = m_connman.ForNode(worst_peer, [&](CNode *pnode) { bool disconnected = m_connman.ForNode(worst_peer, [&](CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
LockAssertion lock(::cs_main); AssertLockHeld(::cs_main);
// Only disconnect a peer that has been connected to us for // Only disconnect a peer that has been connected to us for
// some reasonable fraction of our check-frequency, to give // some reasonable fraction of our check-frequency, to give

View File

@ -452,18 +452,4 @@ public:
} }
}; };
// Utility class for indicating to compiler thread analysis that a mutex is
// locked (when it couldn't be determined otherwise).
struct SCOPED_LOCKABLE LockAssertion
{
template <typename Mutex>
explicit LockAssertion(Mutex& mutex) EXCLUSIVE_LOCK_FUNCTION(mutex)
{
#ifdef DEBUG_LOCKORDER
AssertLockHeld(mutex);
#endif
}
~LockAssertion() UNLOCK_FUNCTION() {}
};
#endif // BITCOIN_SYNC_H #endif // BITCOIN_SYNC_H