mirror of
https://github.com/dashpay/dash.git
synced 2024-12-26 04:22:55 +01:00
merge #11640: Make LOCK, LOCK2, TRY_LOCK work with CWaitableCriticalSection
This commit is contained in:
parent
ead46a6236
commit
c5c3dee308
@ -101,6 +101,7 @@ BITCOIN_TESTS =\
|
|||||||
test/skiplist_tests.cpp \
|
test/skiplist_tests.cpp \
|
||||||
test/streams_tests.cpp \
|
test/streams_tests.cpp \
|
||||||
test/subsidy_tests.cpp \
|
test/subsidy_tests.cpp \
|
||||||
|
test/sync_tests.cpp \
|
||||||
test/timedata_tests.cpp \
|
test/timedata_tests.cpp \
|
||||||
test/torcontrol_tests.cpp \
|
test/torcontrol_tests.cpp \
|
||||||
test/transaction_tests.cpp \
|
test/transaction_tests.cpp \
|
||||||
|
@ -74,7 +74,7 @@ class WorkQueue
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
/** Mutex protects entire object */
|
/** Mutex protects entire object */
|
||||||
std::mutex cs;
|
CWaitableCriticalSection cs;
|
||||||
std::condition_variable cond;
|
std::condition_variable cond;
|
||||||
std::deque<std::unique_ptr<WorkItem>> queue;
|
std::deque<std::unique_ptr<WorkItem>> queue;
|
||||||
bool running;
|
bool running;
|
||||||
@ -93,7 +93,7 @@ public:
|
|||||||
/** Enqueue a work item */
|
/** Enqueue a work item */
|
||||||
bool Enqueue(WorkItem* item)
|
bool Enqueue(WorkItem* item)
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(cs);
|
LOCK(cs);
|
||||||
if (queue.size() >= maxDepth) {
|
if (queue.size() >= maxDepth) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -107,7 +107,7 @@ public:
|
|||||||
while (true) {
|
while (true) {
|
||||||
std::unique_ptr<WorkItem> i;
|
std::unique_ptr<WorkItem> i;
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(cs);
|
WAIT_LOCK(cs, lock);
|
||||||
while (running && queue.empty())
|
while (running && queue.empty())
|
||||||
cond.wait(lock);
|
cond.wait(lock);
|
||||||
if (!running)
|
if (!running)
|
||||||
@ -121,7 +121,7 @@ public:
|
|||||||
/** Interrupt and exit loops */
|
/** Interrupt and exit loops */
|
||||||
void Interrupt()
|
void Interrupt()
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(cs);
|
LOCK(cs);
|
||||||
running = false;
|
running = false;
|
||||||
cond.notify_all();
|
cond.notify_all();
|
||||||
}
|
}
|
||||||
|
@ -709,7 +709,7 @@ static void BlockNotifyGenesisWait(bool, const CBlockIndex *pBlockIndex)
|
|||||||
{
|
{
|
||||||
if (pBlockIndex != nullptr) {
|
if (pBlockIndex != nullptr) {
|
||||||
{
|
{
|
||||||
WaitableLock lock_GenesisWait(cs_GenesisWait);
|
LOCK(cs_GenesisWait);
|
||||||
fHaveGenesis = true;
|
fHaveGenesis = true;
|
||||||
}
|
}
|
||||||
condvar_GenesisWait.notify_all();
|
condvar_GenesisWait.notify_all();
|
||||||
@ -2377,7 +2377,7 @@ bool AppInitMain()
|
|||||||
|
|
||||||
// Wait for genesis block to be processed
|
// Wait for genesis block to be processed
|
||||||
{
|
{
|
||||||
WaitableLock lock(cs_GenesisWait);
|
WAIT_LOCK(cs_GenesisWait, lock);
|
||||||
// We previously could hang here if StartShutdown() is called prior to
|
// We previously could hang here if StartShutdown() is called prior to
|
||||||
// ThreadImport getting started, so instead we just wait on a timer to
|
// ThreadImport getting started, so instead we just wait on a timer to
|
||||||
// check ShutdownRequested() regularly.
|
// check ShutdownRequested() regularly.
|
||||||
|
@ -2862,7 +2862,7 @@ void CConnman::ThreadMessageHandler()
|
|||||||
|
|
||||||
ReleaseNodeVector(vNodesCopy);
|
ReleaseNodeVector(vNodesCopy);
|
||||||
|
|
||||||
std::unique_lock<std::mutex> lock(mutexMsgProc);
|
WAIT_LOCK(mutexMsgProc, lock);
|
||||||
if (!fMoreWork) {
|
if (!fMoreWork) {
|
||||||
condMsgProc.wait_until(lock, std::chrono::steady_clock::now() + std::chrono::milliseconds(100), [this] { return fMsgProcWake; });
|
condMsgProc.wait_until(lock, std::chrono::steady_clock::now() + std::chrono::milliseconds(100), [this] { return fMsgProcWake; });
|
||||||
}
|
}
|
||||||
@ -3198,7 +3198,7 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
|
|||||||
flagInterruptMsgProc = false;
|
flagInterruptMsgProc = false;
|
||||||
|
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(mutexMsgProc);
|
LOCK(mutexMsgProc);
|
||||||
fMsgProcWake = false;
|
fMsgProcWake = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -620,7 +620,7 @@ private:
|
|||||||
bool fMsgProcWake;
|
bool fMsgProcWake;
|
||||||
|
|
||||||
std::condition_variable condMsgProc;
|
std::condition_variable condMsgProc;
|
||||||
std::mutex mutexMsgProc;
|
CWaitableCriticalSection mutexMsgProc;
|
||||||
std::atomic<bool> flagInterruptMsgProc;
|
std::atomic<bool> flagInterruptMsgProc;
|
||||||
|
|
||||||
CThreadInterrupt interruptNet;
|
CThreadInterrupt interruptNet;
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
#include <wincrypt.h>
|
#include <wincrypt.h>
|
||||||
#endif
|
#endif
|
||||||
#include <logging.h> // for LogPrint()
|
#include <logging.h> // for LogPrint()
|
||||||
|
#include <sync.h> // for WAIT_LOCK
|
||||||
#include <utiltime.h> // for GetTime()
|
#include <utiltime.h> // for GetTime()
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -295,7 +296,7 @@ void RandAddSeedSleep()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static std::mutex cs_rng_state;
|
static CWaitableCriticalSection cs_rng_state;
|
||||||
static unsigned char rng_state[32] = {0};
|
static unsigned char rng_state[32] = {0};
|
||||||
static uint64_t rng_counter = 0;
|
static uint64_t rng_counter = 0;
|
||||||
|
|
||||||
@ -305,7 +306,7 @@ static void AddDataToRng(void* data, size_t len) {
|
|||||||
hasher.Write((const unsigned char*)data, len);
|
hasher.Write((const unsigned char*)data, len);
|
||||||
unsigned char buf[64];
|
unsigned char buf[64];
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(cs_rng_state);
|
WAIT_LOCK(cs_rng_state, lock);
|
||||||
hasher.Write(rng_state, sizeof(rng_state));
|
hasher.Write(rng_state, sizeof(rng_state));
|
||||||
hasher.Write((const unsigned char*)&rng_counter, sizeof(rng_counter));
|
hasher.Write((const unsigned char*)&rng_counter, sizeof(rng_counter));
|
||||||
++rng_counter;
|
++rng_counter;
|
||||||
@ -337,7 +338,7 @@ void GetStrongRandBytes(unsigned char* out, int num)
|
|||||||
|
|
||||||
// Combine with and update state
|
// Combine with and update state
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(cs_rng_state);
|
WAIT_LOCK(cs_rng_state, lock);
|
||||||
hasher.Write(rng_state, sizeof(rng_state));
|
hasher.Write(rng_state, sizeof(rng_state));
|
||||||
hasher.Write((const unsigned char*)&rng_counter, sizeof(rng_counter));
|
hasher.Write((const unsigned char*)&rng_counter, sizeof(rng_counter));
|
||||||
++rng_counter;
|
++rng_counter;
|
||||||
|
@ -53,7 +53,7 @@ struct CUpdatedBlock
|
|||||||
int height;
|
int height;
|
||||||
};
|
};
|
||||||
|
|
||||||
static std::mutex cs_blockchange;
|
static CWaitableCriticalSection cs_blockchange;
|
||||||
static std::condition_variable cond_blockchange;
|
static std::condition_variable cond_blockchange;
|
||||||
static CUpdatedBlock latestblock;
|
static CUpdatedBlock latestblock;
|
||||||
|
|
||||||
@ -278,7 +278,7 @@ static UniValue waitfornewblock(const JSONRPCRequest& request)
|
|||||||
|
|
||||||
CUpdatedBlock block;
|
CUpdatedBlock block;
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(cs_blockchange);
|
WAIT_LOCK(cs_blockchange, lock);
|
||||||
block = latestblock;
|
block = latestblock;
|
||||||
if(timeout)
|
if(timeout)
|
||||||
cond_blockchange.wait_for(lock, std::chrono::milliseconds(timeout), [&block]{return latestblock.height != block.height || latestblock.hash != block.hash || !IsRPCRunning(); });
|
cond_blockchange.wait_for(lock, std::chrono::milliseconds(timeout), [&block]{return latestblock.height != block.height || latestblock.hash != block.hash || !IsRPCRunning(); });
|
||||||
@ -320,7 +320,7 @@ static UniValue waitforblock(const JSONRPCRequest& request)
|
|||||||
|
|
||||||
CUpdatedBlock block;
|
CUpdatedBlock block;
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(cs_blockchange);
|
WAIT_LOCK(cs_blockchange, lock);
|
||||||
if(timeout)
|
if(timeout)
|
||||||
cond_blockchange.wait_for(lock, std::chrono::milliseconds(timeout), [&hash]{return latestblock.hash == hash || !IsRPCRunning();});
|
cond_blockchange.wait_for(lock, std::chrono::milliseconds(timeout), [&hash]{return latestblock.hash == hash || !IsRPCRunning();});
|
||||||
else
|
else
|
||||||
@ -363,7 +363,7 @@ static UniValue waitforblockheight(const JSONRPCRequest& request)
|
|||||||
|
|
||||||
CUpdatedBlock block;
|
CUpdatedBlock block;
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(cs_blockchange);
|
WAIT_LOCK(cs_blockchange, lock);
|
||||||
if(timeout)
|
if(timeout)
|
||||||
cond_blockchange.wait_for(lock, std::chrono::milliseconds(timeout), [&height]{return latestblock.height >= height || !IsRPCRunning();});
|
cond_blockchange.wait_for(lock, std::chrono::milliseconds(timeout), [&height]{return latestblock.height >= height || !IsRPCRunning();});
|
||||||
else
|
else
|
||||||
|
@ -503,7 +503,7 @@ static UniValue getblocktemplate(const JSONRPCRequest& request)
|
|||||||
{
|
{
|
||||||
checktxtime = std::chrono::steady_clock::now() + std::chrono::minutes(1);
|
checktxtime = std::chrono::steady_clock::now() + std::chrono::minutes(1);
|
||||||
|
|
||||||
WaitableLock lock(g_best_block_mutex);
|
WAIT_LOCK(g_best_block_mutex, lock);
|
||||||
while (g_best_block == hashWatchedChain && IsRPCRunning())
|
while (g_best_block == hashWatchedChain && IsRPCRunning())
|
||||||
{
|
{
|
||||||
if (g_best_block_cv.wait_until(lock, checktxtime) == std::cv_status::timeout)
|
if (g_best_block_cv.wait_until(lock, checktxtime) == std::cv_status::timeout)
|
||||||
|
@ -104,7 +104,11 @@ static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch,
|
|||||||
printf("%s\n", strOutput.c_str());
|
printf("%s\n", strOutput.c_str());
|
||||||
LogPrintf("%s\n", strOutput.c_str());
|
LogPrintf("%s\n", strOutput.c_str());
|
||||||
|
|
||||||
assert(false);
|
if (g_debug_lockorder_abort) {
|
||||||
|
fprintf(stderr, "Assertion failed: detected inconsistent lock order at %s:%i, details in debug log.\n", __FILE__, __LINE__);
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
throw std::logic_error("potential deadlock detected");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void push_lock(void* c, const CLockLocation& locklocation)
|
static void push_lock(void* c, const CLockLocation& locklocation)
|
||||||
@ -193,4 +197,6 @@ void DeleteLock(void* cs)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool g_debug_lockorder_abort = true;
|
||||||
|
|
||||||
#endif /* DEBUG_LOCKORDER */
|
#endif /* DEBUG_LOCKORDER */
|
||||||
|
105
src/sync.h
105
src/sync.h
@ -46,14 +46,42 @@ LEAVE_CRITICAL_SECTION(mutex); // no RAII
|
|||||||
// //
|
// //
|
||||||
///////////////////////////////
|
///////////////////////////////
|
||||||
|
|
||||||
|
#ifdef DEBUG_LOCKORDER
|
||||||
|
void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false);
|
||||||
|
void LeaveCritical();
|
||||||
|
std::string LocksHeld();
|
||||||
|
void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs) ASSERT_EXCLUSIVE_LOCK(cs);
|
||||||
|
void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs);
|
||||||
|
void DeleteLock(void* cs);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Template mixin that adds -Wthread-safety locking
|
* Call abort() if a potential lock order deadlock bug is detected, instead of
|
||||||
* annotations to a subset of the mutex API.
|
* just logging information and throwing a logic_error. Defaults to true, and
|
||||||
|
* set to false in DEBUG_LOCKORDER unit tests.
|
||||||
|
*/
|
||||||
|
extern bool g_debug_lockorder_abort;
|
||||||
|
#else
|
||||||
|
void static inline EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false) {}
|
||||||
|
void static inline LeaveCritical() {}
|
||||||
|
void static inline AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs) ASSERT_EXCLUSIVE_LOCK(cs) {}
|
||||||
|
void static inline AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs) {}
|
||||||
|
void static inline DeleteLock(void* cs) {}
|
||||||
|
#endif
|
||||||
|
#define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
|
||||||
|
#define AssertLockNotHeld(cs) AssertLockNotHeldInternal(#cs, __FILE__, __LINE__, &cs)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Template mixin that adds -Wthread-safety locking annotations and lock order
|
||||||
|
* checking to a subset of the mutex API.
|
||||||
*/
|
*/
|
||||||
template <typename PARENT>
|
template <typename PARENT>
|
||||||
class LOCKABLE AnnotatedMixin : public PARENT
|
class LOCKABLE AnnotatedMixin : public PARENT
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
~AnnotatedMixin() {
|
||||||
|
DeleteLock((void*)this);
|
||||||
|
}
|
||||||
|
|
||||||
void lock() EXCLUSIVE_LOCK_FUNCTION()
|
void lock() EXCLUSIVE_LOCK_FUNCTION()
|
||||||
{
|
{
|
||||||
PARENT::lock();
|
PARENT::lock();
|
||||||
@ -68,36 +96,15 @@ public:
|
|||||||
{
|
{
|
||||||
return PARENT::try_lock();
|
return PARENT::try_lock();
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
#ifdef DEBUG_LOCKORDER
|
using UniqueLock = std::unique_lock<PARENT>;
|
||||||
void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false);
|
};
|
||||||
void LeaveCritical();
|
|
||||||
std::string LocksHeld();
|
|
||||||
void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs) ASSERT_EXCLUSIVE_LOCK(cs);
|
|
||||||
void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs);
|
|
||||||
void DeleteLock(void* cs);
|
|
||||||
#else
|
|
||||||
void static inline EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false) {}
|
|
||||||
void static inline LeaveCritical() {}
|
|
||||||
void static inline AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs) ASSERT_EXCLUSIVE_LOCK(cs) {}
|
|
||||||
void static inline AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs) {}
|
|
||||||
void static inline DeleteLock(void* cs) {}
|
|
||||||
#endif
|
|
||||||
#define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
|
|
||||||
#define AssertLockNotHeld(cs) AssertLockNotHeldInternal(#cs, __FILE__, __LINE__, &cs)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapped 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.
|
* TODO: We should move away from using the recursive lock by default.
|
||||||
*/
|
*/
|
||||||
class CCriticalSection : public AnnotatedMixin<std::recursive_mutex>
|
typedef AnnotatedMixin<std::recursive_mutex> CCriticalSection;
|
||||||
{
|
|
||||||
public:
|
|
||||||
~CCriticalSection() {
|
|
||||||
DeleteLock((void*)this);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/** Wrapped mutex: supports waiting but not recursive locking */
|
/** Wrapped mutex: supports waiting but not recursive locking */
|
||||||
typedef AnnotatedMixin<std::mutex> CWaitableCriticalSection;
|
typedef AnnotatedMixin<std::mutex> CWaitableCriticalSection;
|
||||||
@ -105,27 +112,23 @@ typedef AnnotatedMixin<std::mutex> CWaitableCriticalSection;
|
|||||||
/** Just a typedef for std::condition_variable, can be wrapped later if desired */
|
/** Just a typedef for std::condition_variable, can be wrapped later if desired */
|
||||||
typedef std::condition_variable CConditionVariable;
|
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
|
#ifdef DEBUG_LOCKCONTENTION
|
||||||
void PrintLockContention(const char* pszName, const char* pszFile, int nLine);
|
void PrintLockContention(const char* pszName, const char* pszFile, int nLine);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/** Wrapper around std::unique_lock<CCriticalSection> */
|
/** Wrapper around std::unique_lock style lock for Mutex. */
|
||||||
class SCOPED_LOCKABLE CCriticalBlock
|
template <typename Mutex, typename Base = typename Mutex::UniqueLock>
|
||||||
|
class SCOPED_LOCKABLE CCriticalBlock : public Base
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
std::unique_lock<CCriticalSection> lock;
|
|
||||||
|
|
||||||
void Enter(const char* pszName, const char* pszFile, int nLine)
|
void Enter(const char* pszName, const char* pszFile, int nLine)
|
||||||
{
|
{
|
||||||
EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()));
|
EnterCritical(pszName, pszFile, nLine, (void*)(Base::mutex()));
|
||||||
#ifdef DEBUG_LOCKCONTENTION
|
#ifdef DEBUG_LOCKCONTENTION
|
||||||
if (!lock.try_lock()) {
|
if (!Base::try_lock()) {
|
||||||
PrintLockContention(pszName, pszFile, nLine);
|
PrintLockContention(pszName, pszFile, nLine);
|
||||||
#endif
|
#endif
|
||||||
lock.lock();
|
Base::lock();
|
||||||
#ifdef DEBUG_LOCKCONTENTION
|
#ifdef DEBUG_LOCKCONTENTION
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@ -133,15 +136,15 @@ private:
|
|||||||
|
|
||||||
bool TryEnter(const char* pszName, const char* pszFile, int nLine)
|
bool TryEnter(const char* pszName, const char* pszFile, int nLine)
|
||||||
{
|
{
|
||||||
EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()), true);
|
EnterCritical(pszName, pszFile, nLine, (void*)(Base::mutex()), true);
|
||||||
lock.try_lock();
|
Base::try_lock();
|
||||||
if (!lock.owns_lock())
|
if (!Base::owns_lock())
|
||||||
LeaveCritical();
|
LeaveCritical();
|
||||||
return lock.owns_lock();
|
return Base::owns_lock();
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CCriticalBlock(CCriticalSection& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(mutexIn) : lock(mutexIn, std::defer_lock)
|
CCriticalBlock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(mutexIn) : Base(mutexIn, std::defer_lock)
|
||||||
{
|
{
|
||||||
if (fTry)
|
if (fTry)
|
||||||
TryEnter(pszName, pszFile, nLine);
|
TryEnter(pszName, pszFile, nLine);
|
||||||
@ -149,11 +152,11 @@ public:
|
|||||||
Enter(pszName, pszFile, nLine);
|
Enter(pszName, pszFile, nLine);
|
||||||
}
|
}
|
||||||
|
|
||||||
CCriticalBlock(CCriticalSection* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
|
CCriticalBlock(Mutex* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
|
||||||
{
|
{
|
||||||
if (!pmutexIn) return;
|
if (!pmutexIn) return;
|
||||||
|
|
||||||
lock = std::unique_lock<CCriticalSection>(*pmutexIn, std::defer_lock);
|
*static_cast<Base*>(this) = Base(*pmutexIn, std::defer_lock);
|
||||||
if (fTry)
|
if (fTry)
|
||||||
TryEnter(pszName, pszFile, nLine);
|
TryEnter(pszName, pszFile, nLine);
|
||||||
else
|
else
|
||||||
@ -162,22 +165,28 @@ public:
|
|||||||
|
|
||||||
~CCriticalBlock() UNLOCK_FUNCTION()
|
~CCriticalBlock() UNLOCK_FUNCTION()
|
||||||
{
|
{
|
||||||
if (lock.owns_lock())
|
if (Base::owns_lock())
|
||||||
LeaveCritical();
|
LeaveCritical();
|
||||||
}
|
}
|
||||||
|
|
||||||
operator bool()
|
operator bool()
|
||||||
{
|
{
|
||||||
return lock.owns_lock();
|
return Base::owns_lock();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename MutexArg>
|
||||||
|
using DebugLock = CCriticalBlock<typename std::remove_reference<typename std::remove_pointer<MutexArg>::type>::type>;
|
||||||
|
|
||||||
#define PASTE(x, y) x ## y
|
#define PASTE(x, y) x ## y
|
||||||
#define PASTE2(x, y) PASTE(x, y)
|
#define PASTE2(x, y) PASTE(x, y)
|
||||||
|
|
||||||
#define LOCK(cs) CCriticalBlock PASTE2(criticalblock, __COUNTER__)(cs, #cs, __FILE__, __LINE__)
|
#define LOCK(cs) DebugLock<decltype(cs)> PASTE2(criticalblock, __COUNTER__)(cs, #cs, __FILE__, __LINE__)
|
||||||
#define LOCK2(cs1, cs2) CCriticalBlock criticalblock1(cs1, #cs1, __FILE__, __LINE__), criticalblock2(cs2, #cs2, __FILE__, __LINE__)
|
#define LOCK2(cs1, cs2) \
|
||||||
#define TRY_LOCK(cs, name) CCriticalBlock name(cs, #cs, __FILE__, __LINE__, true)
|
DebugLock<decltype(cs1)> criticalblock1(cs1, #cs1, __FILE__, __LINE__); \
|
||||||
|
DebugLock<decltype(cs2)> criticalblock2(cs2, #cs2, __FILE__, __LINE__);
|
||||||
|
#define TRY_LOCK(cs, name) DebugLock<decltype(cs)> name(cs, #cs, __FILE__, __LINE__, true)
|
||||||
|
#define WAIT_LOCK(cs, name) DebugLock<decltype(cs)> name(cs, #cs, __FILE__, __LINE__)
|
||||||
|
|
||||||
#define ENTER_CRITICAL_SECTION(cs) \
|
#define ENTER_CRITICAL_SECTION(cs) \
|
||||||
{ \
|
{ \
|
||||||
|
52
src/test/sync_tests.cpp
Normal file
52
src/test/sync_tests.cpp
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
// Copyright (c) 2012-2017 The Bitcoin Core developers
|
||||||
|
// Distributed under the MIT software license, see the accompanying
|
||||||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
|
#include <sync.h>
|
||||||
|
#include <test/test_dash.h>
|
||||||
|
|
||||||
|
#include <boost/test/unit_test.hpp>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
template <typename MutexType>
|
||||||
|
void TestPotentialDeadLockDetected(MutexType& mutex1, MutexType& mutex2)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
LOCK2(mutex1, mutex2);
|
||||||
|
}
|
||||||
|
bool error_thrown = false;
|
||||||
|
try {
|
||||||
|
LOCK2(mutex2, mutex1);
|
||||||
|
} catch (const std::logic_error& e) {
|
||||||
|
BOOST_CHECK_EQUAL(e.what(), "potential deadlock detected");
|
||||||
|
error_thrown = true;
|
||||||
|
}
|
||||||
|
#ifdef DEBUG_LOCKORDER
|
||||||
|
BOOST_CHECK(error_thrown);
|
||||||
|
#else
|
||||||
|
BOOST_CHECK(!error_thrown);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
BOOST_FIXTURE_TEST_SUITE(sync_tests, BasicTestingSetup)
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(potential_deadlock_detected)
|
||||||
|
{
|
||||||
|
#ifdef DEBUG_LOCKORDER
|
||||||
|
bool prev = g_debug_lockorder_abort;
|
||||||
|
g_debug_lockorder_abort = false;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
CCriticalSection rmutex1, rmutex2;
|
||||||
|
TestPotentialDeadLockDetected(rmutex1, rmutex2);
|
||||||
|
|
||||||
|
CWaitableCriticalSection mutex1, mutex2;
|
||||||
|
TestPotentialDeadLockDetected(mutex1, mutex2);
|
||||||
|
|
||||||
|
#ifdef DEBUG_LOCKORDER
|
||||||
|
g_debug_lockorder_abort = prev;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_SUITE_END()
|
@ -4,6 +4,7 @@
|
|||||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
#include <threadinterrupt.h>
|
#include <threadinterrupt.h>
|
||||||
|
#include <sync.h>
|
||||||
|
|
||||||
CThreadInterrupt::CThreadInterrupt() : flag(false) {}
|
CThreadInterrupt::CThreadInterrupt() : flag(false) {}
|
||||||
|
|
||||||
@ -20,7 +21,7 @@ void CThreadInterrupt::reset()
|
|||||||
void CThreadInterrupt::operator()()
|
void CThreadInterrupt::operator()()
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(mut);
|
LOCK(mut);
|
||||||
flag.store(true, std::memory_order_release);
|
flag.store(true, std::memory_order_release);
|
||||||
}
|
}
|
||||||
cond.notify_all();
|
cond.notify_all();
|
||||||
@ -28,7 +29,7 @@ void CThreadInterrupt::operator()()
|
|||||||
|
|
||||||
bool CThreadInterrupt::sleep_for(std::chrono::milliseconds rel_time)
|
bool CThreadInterrupt::sleep_for(std::chrono::milliseconds rel_time)
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(mut);
|
WAIT_LOCK(mut, lock);
|
||||||
return !cond.wait_for(lock, rel_time, [this]() { return flag.load(std::memory_order_acquire); });
|
return !cond.wait_for(lock, rel_time, [this]() { return flag.load(std::memory_order_acquire); });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,8 @@
|
|||||||
#ifndef BITCOIN_THREADINTERRUPT_H
|
#ifndef BITCOIN_THREADINTERRUPT_H
|
||||||
#define BITCOIN_THREADINTERRUPT_H
|
#define BITCOIN_THREADINTERRUPT_H
|
||||||
|
|
||||||
|
#include <sync.h>
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
@ -28,7 +30,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
std::condition_variable cond;
|
std::condition_variable cond;
|
||||||
std::mutex mut;
|
CWaitableCriticalSection mut;
|
||||||
std::atomic<bool> flag;
|
std::atomic<bool> flag;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -2597,7 +2597,7 @@ void static UpdateTip(const CBlockIndex *pindexNew, const CChainParams& chainPar
|
|||||||
mempool.AddTransactionsUpdated(1);
|
mempool.AddTransactionsUpdated(1);
|
||||||
|
|
||||||
{
|
{
|
||||||
WaitableLock lock(g_best_block_mutex);
|
LOCK(g_best_block_mutex);
|
||||||
g_best_block = pindexNew->GetBlockHash();
|
g_best_block = pindexNew->GetBlockHash();
|
||||||
g_best_block_cv.notify_all();
|
g_best_block_cv.notify_all();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user