Skip to content

Commit ef8a634

Browse files
committed
Merge #10866: Fix -Wthread-safety-analysis warnings. Compile with -Wthread-safety-analysis if available.
76ea17c Add mutex requirement for AddToCompactExtraTransactions(…) (practicalswift) 4616c82 Use -Wthread-safety-analysis if available (+ -Werror=thread-safety-analysis if --enable-werror) (practicalswift) 7e319d6 Fix -Wthread-safety-analysis warnings. Change the sync.h primitives to std from boost. (Matt Corallo) Pull request description: * Add mutex requirement for `AddToCompactExtraTransactions(…)`. * Use `-Wthread-safety-analysis` if available. * Rebased on top of https://github.com/TheBlueMatt/bitcoin/commits/2017-08-test-10923 - now includes: Fix -Wthread-safety-analysis warnings. Change the sync.h primitives to std from boost. Tree-SHA512: fb7365f85daa2741c276a1c899228181a8d46af51db7fbbdffceeaff121a3eb2ab74d7c8bf5e7de879bcc5042d00d24cb4649c312d51caba45a3f6135fd8b38f
2 parents 70fec9e + 76ea17c commit ef8a634

File tree

5 files changed

+35
-31
lines changed

5 files changed

+35
-31
lines changed

configure.ac

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ if test "x$enable_werror" = "xyes"; then
241241
AC_MSG_ERROR("enable-werror set but -Werror is not usable")
242242
fi
243243
AX_CHECK_COMPILE_FLAG([-Werror=vla],[ERROR_CXXFLAGS="$ERROR_CXXFLAGS -Werror=vla"],,[[$CXXFLAG_WERROR]])
244+
AX_CHECK_COMPILE_FLAG([-Werror=thread-safety-analysis],[ERROR_CXXFLAGS="$ERROR_CXXFLAGS -Werror=thread-safety-analysis"],,[[$CXXFLAG_WERROR]])
244245
fi
245246

246247
if test "x$CXXFLAGS_overridden" = "xno"; then
@@ -249,6 +250,7 @@ if test "x$CXXFLAGS_overridden" = "xno"; then
249250
AX_CHECK_COMPILE_FLAG([-Wformat],[CXXFLAGS="$CXXFLAGS -Wformat"],,[[$CXXFLAG_WERROR]])
250251
AX_CHECK_COMPILE_FLAG([-Wvla],[CXXFLAGS="$CXXFLAGS -Wvla"],,[[$CXXFLAG_WERROR]])
251252
AX_CHECK_COMPILE_FLAG([-Wformat-security],[CXXFLAGS="$CXXFLAGS -Wformat-security"],,[[$CXXFLAG_WERROR]])
253+
AX_CHECK_COMPILE_FLAG([-Wthread-safety-analysis],[CXXFLAGS="$CXXFLAGS -Wthread-safety-analysis"],,[[$CXXFLAG_WERROR]])
252254

253255
## Some compilers (gcc) ignore unknown -Wno-* options, but warn about all
254256
## unknown options if any other warning is produced. Test the -Wfoo case, and

src/init.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -544,14 +544,14 @@ static void BlockNotifyCallback(bool initialSync, const CBlockIndex *pBlockIndex
544544
}
545545

546546
static bool fHaveGenesis = false;
547-
static boost::mutex cs_GenesisWait;
547+
static CWaitableCriticalSection cs_GenesisWait;
548548
static CConditionVariable condvar_GenesisWait;
549549

550550
static void BlockNotifyGenesisWait(bool, const CBlockIndex *pBlockIndex)
551551
{
552552
if (pBlockIndex != nullptr) {
553553
{
554-
boost::unique_lock<boost::mutex> lock_GenesisWait(cs_GenesisWait);
554+
WaitableLock lock_GenesisWait(cs_GenesisWait);
555555
fHaveGenesis = true;
556556
}
557557
condvar_GenesisWait.notify_all();
@@ -1630,7 +1630,7 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
16301630

16311631
// Wait for genesis block to be processed
16321632
{
1633-
boost::unique_lock<boost::mutex> lock(cs_GenesisWait);
1633+
WaitableLock lock(cs_GenesisWait);
16341634
while (!fHaveGenesis) {
16351635
condvar_GenesisWait.wait(lock);
16361636
}

src/net_processing.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
633633
// mapOrphanTransactions
634634
//
635635

636-
void AddToCompactExtraTransactions(const CTransactionRef& tx)
636+
void AddToCompactExtraTransactions(const CTransactionRef& tx) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
637637
{
638638
size_t max_extra_txn = gArgs.GetArg("-blockreconstructionextratxn", DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN);
639639
if (max_extra_txn <= 0)

src/rpc/mining.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
455455
{
456456
// Wait to respond until either the best block changes, OR a minute has passed and there are more transactions
457457
uint256 hashWatchedChain;
458-
boost::system_time checktxtime;
458+
std::chrono::steady_clock::time_point checktxtime;
459459
unsigned int nTransactionsUpdatedLastLP;
460460

461461
if (lpval.isStr())
@@ -476,17 +476,17 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
476476
// Release the wallet and main lock while waiting
477477
LEAVE_CRITICAL_SECTION(cs_main);
478478
{
479-
checktxtime = boost::get_system_time() + boost::posix_time::minutes(1);
479+
checktxtime = std::chrono::steady_clock::now() + std::chrono::minutes(1);
480480

481-
boost::unique_lock<boost::mutex> lock(csBestBlock);
481+
WaitableLock lock(csBestBlock);
482482
while (chainActive.Tip()->GetBlockHash() == hashWatchedChain && IsRPCRunning())
483483
{
484-
if (!cvBlockChange.timed_wait(lock, checktxtime))
484+
if (cvBlockChange.wait_until(lock, checktxtime) == std::cv_status::timeout)
485485
{
486486
// Timeout: Check transactions for update
487487
if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLastLP)
488488
break;
489-
checktxtime += boost::posix_time::seconds(10);
489+
checktxtime += std::chrono::seconds(10);
490490
}
491491
}
492492
}

src/sync.h

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
#include <boost/thread/condition_variable.hpp>
1212
#include <boost/thread/mutex.hpp>
13-
#include <boost/thread/recursive_mutex.hpp>
13+
#include <condition_variable>
14+
#include <thread>
15+
#include <mutex>
1416

1517

1618
////////////////////////////////////////////////
@@ -21,17 +23,17 @@
2123

2224
/*
2325
CCriticalSection mutex;
24-
boost::recursive_mutex mutex;
26+
std::recursive_mutex mutex;
2527
2628
LOCK(mutex);
27-
boost::unique_lock<boost::recursive_mutex> criticalblock(mutex);
29+
std::unique_lock<std::recursive_mutex> criticalblock(mutex);
2830
2931
LOCK2(mutex1, mutex2);
30-
boost::unique_lock<boost::recursive_mutex> criticalblock1(mutex1);
31-
boost::unique_lock<boost::recursive_mutex> criticalblock2(mutex2);
32+
std::unique_lock<std::recursive_mutex> criticalblock1(mutex1);
33+
std::unique_lock<std::recursive_mutex> criticalblock2(mutex2);
3234
3335
TRY_LOCK(mutex, name);
34-
boost::unique_lock<boost::recursive_mutex> name(mutex, boost::try_to_lock_t);
36+
std::unique_lock<std::recursive_mutex> name(mutex, std::try_to_lock_t);
3537
3638
ENTER_CRITICAL_SECTION(mutex); // no RAII
3739
mutex.lock();
@@ -85,33 +87,35 @@ void static inline DeleteLock(void* cs) {}
8587
#define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
8688

8789
/**
88-
* Wrapped boost mutex: supports recursive locking, but no waiting
90+
* Wrapped mutex: supports recursive locking, but no waiting
8991
* TODO: We should move away from using the recursive lock by default.
9092
*/
91-
class CCriticalSection : public AnnotatedMixin<boost::recursive_mutex>
93+
class CCriticalSection : public AnnotatedMixin<std::recursive_mutex>
9294
{
9395
public:
9496
~CCriticalSection() {
9597
DeleteLock((void*)this);
9698
}
9799
};
98100

99-
/** Wrapped boost mutex: supports waiting but not recursive locking */
100-
typedef AnnotatedMixin<boost::mutex> CWaitableCriticalSection;
101+
/** Wrapped mutex: supports waiting but not recursive locking */
102+
typedef AnnotatedMixin<std::mutex> CWaitableCriticalSection;
101103

102-
/** Just a typedef for boost::condition_variable, can be wrapped later if desired */
103-
typedef boost::condition_variable CConditionVariable;
104+
/** Just a typedef for std::condition_variable, can be wrapped later if desired */
105+
typedef std::condition_variable CConditionVariable;
106+
107+
/** Just a typedef for std::unique_lock, can be wrapped later if desired */
108+
typedef std::unique_lock<std::mutex> WaitableLock;
104109

105110
#ifdef DEBUG_LOCKCONTENTION
106111
void PrintLockContention(const char* pszName, const char* pszFile, int nLine);
107112
#endif
108113

109-
/** Wrapper around boost::unique_lock<Mutex> */
110-
template <typename Mutex>
111-
class SCOPED_LOCKABLE CMutexLock
114+
/** Wrapper around std::unique_lock<CCriticalSection> */
115+
class SCOPED_LOCKABLE CCriticalBlock
112116
{
113117
private:
114-
boost::unique_lock<Mutex> lock;
118+
std::unique_lock<CCriticalSection> lock;
115119

116120
void Enter(const char* pszName, const char* pszFile, int nLine)
117121
{
@@ -136,26 +140,26 @@ class SCOPED_LOCKABLE CMutexLock
136140
}
137141

138142
public:
139-
CMutexLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(mutexIn) : lock(mutexIn, boost::defer_lock)
143+
CCriticalBlock(CCriticalSection& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(mutexIn) : lock(mutexIn, std::defer_lock)
140144
{
141145
if (fTry)
142146
TryEnter(pszName, pszFile, nLine);
143147
else
144148
Enter(pszName, pszFile, nLine);
145149
}
146150

147-
CMutexLock(Mutex* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
151+
CCriticalBlock(CCriticalSection* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
148152
{
149153
if (!pmutexIn) return;
150154

151-
lock = boost::unique_lock<Mutex>(*pmutexIn, boost::defer_lock);
155+
lock = std::unique_lock<CCriticalSection>(*pmutexIn, std::defer_lock);
152156
if (fTry)
153157
TryEnter(pszName, pszFile, nLine);
154158
else
155159
Enter(pszName, pszFile, nLine);
156160
}
157161

158-
~CMutexLock() UNLOCK_FUNCTION()
162+
~CCriticalBlock() UNLOCK_FUNCTION()
159163
{
160164
if (lock.owns_lock())
161165
LeaveCritical();
@@ -167,8 +171,6 @@ class SCOPED_LOCKABLE CMutexLock
167171
}
168172
};
169173

170-
typedef CMutexLock<CCriticalSection> CCriticalBlock;
171-
172174
#define PASTE(x, y) x ## y
173175
#define PASTE2(x, y) PASTE(x, y)
174176

0 commit comments

Comments
 (0)