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.

(cherry picked from commit bitcoin/bitcoin@7e319d6393)
This commit is contained in:
Matt Corallo 2017-08-16 13:56:02 -04:00 committed by Jack Grigg
parent e5d2a3a5a2
commit 8164dc35d7
5 changed files with 33 additions and 32 deletions

View File

@ -555,14 +555,14 @@ static void TxExpiryNotifyCallback(const uint256& txid)
}
static bool fHaveGenesis = false;
static boost::mutex cs_GenesisWait;
static CWaitableCriticalSection cs_GenesisWait;
static CConditionVariable condvar_GenesisWait;
static void BlockNotifyGenesisWait(bool, const CBlockIndex *pBlockIndex)
{
if (pBlockIndex != NULL) {
{
boost::unique_lock<boost::mutex> lock_GenesisWait(cs_GenesisWait);
WaitableLock lock_GenesisWait(cs_GenesisWait);
fHaveGenesis = true;
}
condvar_GenesisWait.notify_all();
@ -1905,7 +1905,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
// Wait for genesis block to be processed
{
boost::unique_lock<boost::mutex> lock(cs_GenesisWait);
WaitableLock lock(cs_GenesisWait);
while (!fHaveGenesis) {
condvar_GenesisWait.wait(lock);
}

View File

@ -3863,7 +3863,7 @@ void static UpdateTip(CBlockIndex *pindexNew, const CChainParams& chainParams) {
RenderPoolMetrics("transparent", transparentPool);
{
boost::unique_lock<boost::mutex> lock(g_best_block_mutex);
WaitableLock lock(g_best_block_mutex);
g_best_block = pindexNew->GetBlockHash();
g_best_block_height = pindexNew->nHeight;
g_best_block_cv.notify_all();

View File

@ -601,7 +601,7 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp)
{
// Wait to respond until either the best block changes, OR some time passes and there are more transactions
uint256 hashWatchedChain;
boost::system_time checktxtime;
std::chrono::steady_clock::time_point checktxtime;
unsigned int nTransactionsUpdatedLastLP;
if (lpval.isStr())
@ -623,9 +623,9 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp)
// Don't call chainActive->Tip() without holding cs_main
LEAVE_CRITICAL_SECTION(cs_main);
{
checktxtime = boost::get_system_time() + boost::posix_time::seconds(10);
checktxtime = std::chrono::steady_clock::now() + std::chrono::seconds(10);
boost::unique_lock<boost::mutex> lock(g_best_block_mutex);
WaitableLock lock(g_best_block_mutex);
while (g_best_block == hashWatchedChain && IsRPCRunning())
{
// Before waiting, generate the coinbase for the block following the next
@ -640,7 +640,7 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp)
Params(), CAmount{0}, minerAddress, cached_next_cb_height);
next_cb_mtx = cached_next_cb_mtx;
}
bool timedout = !g_best_block_cv.timed_wait(lock, checktxtime);
bool timedout = g_best_block_cv.wait_until(lock, checktxtime) == std::cv_status::timeout;
// Optimization: even if timed out, a new block may have arrived
// while waiting for cs_main; if so, don't discard next_cb_mtx.
@ -652,7 +652,7 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp)
next_cb_mtx = nullopt;
break;
}
checktxtime += boost::posix_time::seconds(10);
checktxtime += std::chrono::seconds(10);
}
if (g_best_block_height != nHeight + 1) {
// Unexpected height (reorg or >1 blocks arrived while waiting) invalidates coinbase tx.

View File

@ -12,7 +12,9 @@
#include <boost/thread/condition_variable.hpp>
#include <boost/thread/locks.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/recursive_mutex.hpp>
#include <condition_variable>
#include <thread>
#include <mutex>
////////////////////////////////////////////////
@ -23,17 +25,17 @@
/*
CCriticalSection mutex;
boost::recursive_mutex mutex;
std::recursive_mutex mutex;
LOCK(mutex);
boost::unique_lock<boost::recursive_mutex> criticalblock(mutex);
std::unique_lock<std::recursive_mutex> criticalblock(mutex);
LOCK2(mutex1, mutex2);
boost::unique_lock<boost::recursive_mutex> criticalblock1(mutex1);
boost::unique_lock<boost::recursive_mutex> criticalblock2(mutex2);
std::unique_lock<std::recursive_mutex> criticalblock1(mutex1);
std::unique_lock<std::recursive_mutex> criticalblock2(mutex2);
TRY_LOCK(mutex, name);
boost::unique_lock<boost::recursive_mutex> name(mutex, boost::try_to_lock_t);
std::unique_lock<std::recursive_mutex> name(mutex, std::try_to_lock_t);
ENTER_CRITICAL_SECTION(mutex); // no RAII
mutex.lock();
@ -90,10 +92,10 @@ void static inline DeleteLock(void* cs) {}
#define AssertLockNotHeld(cs) AssertLockNotHeldInternal(#cs, __FILE__, __LINE__, &cs)
/**
* Wrapped boost mutex: supports recursive locking, but no waiting
* Wrapped mutex: supports recursive locking, but no waiting
* TODO: We should move away from using the recursive lock by default.
*/
class CCriticalSection : public AnnotatedMixin<boost::recursive_mutex>
class CCriticalSection : public AnnotatedMixin<std::recursive_mutex>
{
public:
~CCriticalSection() {
@ -101,22 +103,24 @@ public:
}
};
/** Wrapped boost mutex: supports waiting but not recursive locking */
typedef AnnotatedMixin<boost::mutex> CWaitableCriticalSection;
/** Wrapped mutex: supports waiting but not recursive locking */
typedef AnnotatedMixin<std::mutex> CWaitableCriticalSection;
/** Just a typedef for boost::condition_variable, can be wrapped later if desired */
typedef boost::condition_variable CConditionVariable;
/** Just a typedef for std::condition_variable, can be wrapped later if desired */
typedef std::condition_variable CConditionVariable;
/** Just a typedef for std::unique_lock, can be wrapped later if desired */
typedef std::unique_lock<std::mutex> WaitableLock;
#ifdef DEBUG_LOCKCONTENTION
void PrintLockContention(const char* pszName, const char* pszFile, int nLine);
#endif
/** Wrapper around boost::unique_lock<Mutex> */
template <typename Mutex>
class SCOPED_LOCKABLE CMutexLock
/** Wrapper around std::unique_lock<CCriticalSection> */
class SCOPED_LOCKABLE CCriticalBlock
{
private:
boost::unique_lock<Mutex> lock;
std::unique_lock<CCriticalSection> lock;
void Enter(const char* pszName, const char* pszFile, int nLine)
{
@ -141,7 +145,7 @@ private:
}
public:
CMutexLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(mutexIn) : lock(mutexIn, boost::defer_lock)
CCriticalBlock(CCriticalSection& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(mutexIn) : lock(mutexIn, std::defer_lock)
{
if (fTry)
TryEnter(pszName, pszFile, nLine);
@ -149,18 +153,18 @@ public:
Enter(pszName, pszFile, nLine);
}
CMutexLock(Mutex* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
CCriticalBlock(CCriticalSection* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
{
if (!pmutexIn) return;
lock = boost::unique_lock<Mutex>(*pmutexIn, boost::defer_lock);
lock = std::unique_lock<CCriticalSection>(*pmutexIn, std::defer_lock);
if (fTry)
TryEnter(pszName, pszFile, nLine);
else
Enter(pszName, pszFile, nLine);
}
~CMutexLock() UNLOCK_FUNCTION()
~CCriticalBlock() UNLOCK_FUNCTION()
{
if (lock.owns_lock())
LeaveCritical();
@ -172,8 +176,6 @@ public:
}
};
typedef CMutexLock<CCriticalSection> CCriticalBlock;
#define PASTE(x, y) x ## y
#define PASTE2(x, y) PASTE(x, y)

View File

@ -91,7 +91,6 @@ EXPECTED_BOOST_INCLUDES=(
boost/thread/locks.hpp
boost/thread/mutex.hpp
boost/thread/once.hpp
boost/thread/recursive_mutex.hpp
boost/thread/synchronized_value.hpp
boost/thread/thread.hpp
boost/thread/tss.hpp