Skip to content

Commit 7e319d6

Browse files
TheBlueMattpracticalswift
authored andcommitted
Fix -Wthread-safety-analysis warnings. Change the sync.h primitives to std from boost.
Commit 1. This code was written by @TheBlueMatt in the following branch: * https://github.com/TheBlueMatt/bitcoin/commits/2017-08-test-10923 This commit message was written by me (@practicalswift) who also squashed @TheBlueMatt's commits into one and tried to summarize the changes made. Commit 2. Remove boost include. Remove boost mentions in comments.
1 parent e022463 commit 7e319d6

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

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();
@@ -1634,7 +1634,7 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
16341634

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

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)