Skip to content

Commit d2464df

Browse files
committed
Merge pull request #6287
a794284 locking: add a quick example of GUARDED_BY (Cory Fields) 2b890dd locking: fix a few small issues uncovered by -Wthread-safety (Cory Fields) cd27bba locking: teach Clang's -Wthread-safety to cope with our scoped lock macros (Cory Fields)
2 parents d946e9a + a794284 commit d2464df

File tree

5 files changed

+15
-14
lines changed

5 files changed

+15
-14
lines changed

src/main.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ struct COrphanTx {
7575
CTransaction tx;
7676
NodeId fromPeer;
7777
};
78-
map<uint256, COrphanTx> mapOrphanTransactions;
79-
map<uint256, set<uint256> > mapOrphanTransactionsByPrev;
80-
void EraseOrphansFor(NodeId peer);
78+
map<uint256, COrphanTx> mapOrphanTransactions GUARDED_BY(cs_main);;
79+
map<uint256, set<uint256> > mapOrphanTransactionsByPrev GUARDED_BY(cs_main);;
80+
void EraseOrphansFor(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
8181

8282
/**
8383
* Returns true if there are nRequired or more blocks of minVersion or above
@@ -527,7 +527,7 @@ CBlockTreeDB *pblocktree = NULL;
527527
// mapOrphanTransactions
528528
//
529529

530-
bool AddOrphanTx(const CTransaction& tx, NodeId peer)
530+
bool AddOrphanTx(const CTransaction& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
531531
{
532532
uint256 hash = tx.GetHash();
533533
if (mapOrphanTransactions.count(hash))
@@ -557,7 +557,7 @@ bool AddOrphanTx(const CTransaction& tx, NodeId peer)
557557
return true;
558558
}
559559

560-
void static EraseOrphanTx(uint256 hash)
560+
void static EraseOrphanTx(uint256 hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
561561
{
562562
map<uint256, COrphanTx>::iterator it = mapOrphanTransactions.find(hash);
563563
if (it == mapOrphanTransactions.end())
@@ -591,7 +591,7 @@ void EraseOrphansFor(NodeId peer)
591591
}
592592

593593

594-
unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans)
594+
unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
595595
{
596596
unsigned int nEvicted = 0;
597597
while (mapOrphanTransactions.size() > nMaxOrphans)
@@ -3649,7 +3649,7 @@ std::string GetWarnings(const std::string& strFor)
36493649
//
36503650

36513651

3652-
bool static AlreadyHave(const CInv& inv)
3652+
bool static AlreadyHave(const CInv& inv) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
36533653
{
36543654
switch (inv.type)
36553655
{

src/net.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2183,8 +2183,10 @@ void CNode::EndMessage() UNLOCK_FUNCTION(cs_vSend)
21832183
Fuzz(GetArg("-fuzzmessagestest", 10));
21842184

21852185
if (ssSend.size() == 0)
2186+
{
2187+
LEAVE_CRITICAL_SECTION(cs_vSend);
21862188
return;
2187-
2189+
}
21882190
// Set the size
21892191
unsigned int nSize = ssSend.size() - CMessageHeader::HEADER_SIZE;
21902192
WriteLE32((uint8_t*)&ssSend[CMessageHeader::MESSAGE_SIZE_OFFSET], nSize);

src/sync.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void PrintLockContention(const char* pszName, const char* pszFile, int nLine);
101101

102102
/** Wrapper around boost::unique_lock<Mutex> */
103103
template <typename Mutex>
104-
class CMutexLock
104+
class SCOPED_LOCKABLE CMutexLock
105105
{
106106
private:
107107
boost::unique_lock<Mutex> lock;
@@ -129,15 +129,15 @@ class CMutexLock
129129
}
130130

131131
public:
132-
CMutexLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) : lock(mutexIn, boost::defer_lock)
132+
CMutexLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(mutexIn) : lock(mutexIn, boost::defer_lock)
133133
{
134134
if (fTry)
135135
TryEnter(pszName, pszFile, nLine);
136136
else
137137
Enter(pszName, pszFile, nLine);
138138
}
139139

140-
CMutexLock(Mutex* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false)
140+
CMutexLock(Mutex* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
141141
{
142142
if (!pmutexIn) return;
143143

@@ -148,7 +148,7 @@ class CMutexLock
148148
Enter(pszName, pszFile, nLine);
149149
}
150150

151-
~CMutexLock()
151+
~CMutexLock() UNLOCK_FUNCTION()
152152
{
153153
if (lock.owns_lock())
154154
LeaveCritical();

src/util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ CTranslationInterface translationInterface;
114114

115115
/** Init OpenSSL library multithreading support */
116116
static CCriticalSection** ppmutexOpenSSL;
117-
void locking_callback(int mode, int i, const char* file, int line)
117+
void locking_callback(int mode, int i, const char* file, int line) NO_THREAD_SAFETY_ANALYSIS
118118
{
119119
if (mode & CRYPTO_LOCK) {
120120
ENTER_CRITICAL_SECTION(*ppmutexOpenSSL[i]);

src/wallet/rpcwallet.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,6 @@ UniValue listaddressgroupings(const UniValue& params, bool fHelp)
476476
addressInfo.push_back(CBitcoinAddress(address).ToString());
477477
addressInfo.push_back(ValueFromAmount(balances[address]));
478478
{
479-
LOCK(pwalletMain->cs_wallet);
480479
if (pwalletMain->mapAddressBook.find(CBitcoinAddress(address).Get()) != pwalletMain->mapAddressBook.end())
481480
addressInfo.push_back(pwalletMain->mapAddressBook.find(CBitcoinAddress(address).Get())->second.name);
482481
}

0 commit comments

Comments
 (0)