Skip to content

Commit 8235dca

Browse files
author
MarcoFalke
committed
Merge #19979: Replace LockAssertion with AssertLockHeld, remove LockAssertion
0bd1184 Remove unused LockAssertion struct (Hennadii Stepanov) ab2a442 Replace LockAssertion with a proper thread safety annotations (Hennadii Stepanov) 73f71e1 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 0bd1184 ajtowns: ACK 0bd1184 vasild: ACK 0bd1184 Tree-SHA512: ef7780dd689faf0bb479fdb97c49bc652e2dd10c148234bb95502dfbb676442d8565ee37864d923ca21a25f9dc2a335bf46ee82c095e387b59a664ab05c0ae41
2 parents 9e217f5 + 0bd1184 commit 8235dca

File tree

4 files changed

+13
-48
lines changed

4 files changed

+13
-48
lines changed

doc/developer-notes.md

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -793,25 +793,6 @@ bool ChainstateManager::ProcessNewBlock(...)
793793
}
794794
```
795795
796-
- When Clang Thread Safety Analysis is unable to determine if a mutex is locked, use `LockAssertion` class instances:
797-
798-
```C++
799-
// net_processing.h
800-
void RelayTransaction(...) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
801-
802-
// net_processing.cpp
803-
void RelayTransaction(...)
804-
{
805-
AssertLockHeld(::cs_main);
806-
807-
connman.ForEachNode([&txid, &wtxid](CNode* pnode) {
808-
LockAssertion lock(::cs_main);
809-
...
810-
});
811-
}
812-
813-
```
814-
815796
- Build and run tests with `-DDEBUG_LOCKORDER` to verify that no potential
816797
deadlocks are introduced. As of 0.12, this is defined by default when
817798
configuring with `--enable-debug`.

src/net.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,8 @@ class CConnman
258258

259259
void PushMessage(CNode* pnode, CSerializedNetMsg&& msg);
260260

261-
template<typename Callable>
262-
void ForEachNode(Callable&& func)
261+
using NodeFn = std::function<void(CNode*)>;
262+
void ForEachNode(const NodeFn& func)
263263
{
264264
LOCK(cs_vNodes);
265265
for (auto&& node : vNodes) {
@@ -268,8 +268,7 @@ class CConnman
268268
}
269269
};
270270

271-
template<typename Callable>
272-
void ForEachNode(Callable&& func) const
271+
void ForEachNode(const NodeFn& func) const
273272
{
274273
LOCK(cs_vNodes);
275274
for (auto&& node : vNodes) {

src/net_processing.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,8 @@ static void MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid, CConnman& connma
662662
return;
663663
}
664664
}
665-
connman.ForNode(nodeid, [&connman](CNode* pfrom){
666-
LockAssertion lock(::cs_main);
665+
connman.ForNode(nodeid, [&connman](CNode* pfrom) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
666+
AssertLockHeld(::cs_main);
667667
uint64_t nCMPCTBLOCKVersion = (pfrom->GetLocalServices() & NODE_WITNESS) ? 2 : 1;
668668
if (lNodesAnnouncingHeaderAndIDs.size() >= 3) {
669669
// As per BIP152, we only get 3 of our peers to announce
@@ -1355,8 +1355,8 @@ void PeerManager::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_
13551355
fWitnessesPresentInMostRecentCompactBlock = fWitnessEnabled;
13561356
}
13571357

1358-
m_connman.ForEachNode([this, &pcmpctblock, pindex, &msgMaker, fWitnessEnabled, &hashBlock](CNode* pnode) {
1359-
LockAssertion lock(::cs_main);
1358+
m_connman.ForEachNode([this, &pcmpctblock, pindex, &msgMaker, fWitnessEnabled, &hashBlock](CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
1359+
AssertLockHeld(::cs_main);
13601360

13611361
// TODO: Avoid the repeated-serialization here
13621362
if (pnode->GetCommonVersion() < INVALID_CB_NO_BAN_VERSION || pnode->fDisconnect)
@@ -1489,9 +1489,8 @@ bool static AlreadyHaveBlock(const uint256& block_hash) EXCLUSIVE_LOCKS_REQUIRED
14891489

14901490
void RelayTransaction(const uint256& txid, const uint256& wtxid, const CConnman& connman)
14911491
{
1492-
connman.ForEachNode([&txid, &wtxid](CNode* pnode)
1493-
{
1494-
LockAssertion lock(::cs_main);
1492+
connman.ForEachNode([&txid, &wtxid](CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
1493+
AssertLockHeld(::cs_main);
14951494

14961495
CNodeState* state = State(pnode->GetId());
14971496
if (state == nullptr) return;
@@ -3975,8 +3974,8 @@ void PeerManager::EvictExtraOutboundPeers(int64_t time_in_seconds)
39753974
NodeId worst_peer = -1;
39763975
int64_t oldest_block_announcement = std::numeric_limits<int64_t>::max();
39773976

3978-
m_connman.ForEachNode([&](CNode* pnode) {
3979-
LockAssertion lock(::cs_main);
3977+
m_connman.ForEachNode([&](CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
3978+
AssertLockHeld(::cs_main);
39803979

39813980
// Ignore non-outbound peers, or nodes marked for disconnect already
39823981
if (!pnode->IsOutboundOrBlockRelayConn() || pnode->fDisconnect) return;
@@ -3992,8 +3991,8 @@ void PeerManager::EvictExtraOutboundPeers(int64_t time_in_seconds)
39923991
}
39933992
});
39943993
if (worst_peer != -1) {
3995-
bool disconnected = m_connman.ForNode(worst_peer, [&](CNode *pnode) {
3996-
LockAssertion lock(::cs_main);
3994+
bool disconnected = m_connman.ForNode(worst_peer, [&](CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
3995+
AssertLockHeld(::cs_main);
39973996

39983997
// Only disconnect a peer that has been connected to us for
39993998
// some reasonable fraction of our check-frequency, to give

src/sync.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -352,18 +352,4 @@ class CSemaphoreGrant
352352
}
353353
};
354354

355-
// Utility class for indicating to compiler thread analysis that a mutex is
356-
// locked (when it couldn't be determined otherwise).
357-
struct SCOPED_LOCKABLE LockAssertion
358-
{
359-
template <typename Mutex>
360-
explicit LockAssertion(Mutex& mutex) EXCLUSIVE_LOCK_FUNCTION(mutex)
361-
{
362-
#ifdef DEBUG_LOCKORDER
363-
AssertLockHeld(mutex);
364-
#endif
365-
}
366-
~LockAssertion() UNLOCK_FUNCTION() {}
367-
};
368-
369355
#endif // BITCOIN_SYNC_H

0 commit comments

Comments
 (0)