mirror of
https://github.com/dashpay/dash.git
synced 2024-12-25 12:02:48 +01:00
39bca402a0
1633f5ec8846408182cceb60dc88f022635f4002 util, refactor: Add UNIQUE_NAME helper macro (Hennadii Stepanov) Pull request description: This PR replaces repetitive code with a helper macro. ACKs for top commit: laanwj: Tested ACK 1633f5ec8846408182cceb60dc88f022635f4002 Tree-SHA512: 5f04e472c5f3184c0a9df75395377c6744bfb2cd8f95f8427c1c5e20daa7d6a9b29e45424b88391fc6326d365907a750ab50fda534b49d1df80dccf0e18467a4 Signed-off-by: Vijay <vijaydas.mp@gmail.com>
468 lines
14 KiB
C++
468 lines
14 KiB
C++
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#ifndef BITCOIN_SYNC_H
|
|
#define BITCOIN_SYNC_H
|
|
|
|
#include <logging.h>
|
|
#include <logging/timer.h>
|
|
#include <threadsafety.h>
|
|
#include <util/macros.h>
|
|
|
|
#include <condition_variable>
|
|
#include <mutex>
|
|
#include <shared_mutex>
|
|
#include <string>
|
|
#include <thread>
|
|
|
|
/////////////////////////////////////////////////
|
|
// //
|
|
// THE SIMPLE DEFINITION, EXCLUDING DEBUG CODE //
|
|
// //
|
|
/////////////////////////////////////////////////
|
|
|
|
/*
|
|
RecursiveMutex mutex;
|
|
std::recursive_mutex mutex;
|
|
|
|
LOCK(mutex);
|
|
std::unique_lock<std::recursive_mutex> criticalblock(mutex);
|
|
|
|
LOCK2(mutex1, mutex2);
|
|
std::unique_lock<std::recursive_mutex> criticalblock1(mutex1);
|
|
std::unique_lock<std::recursive_mutex> criticalblock2(mutex2);
|
|
|
|
TRY_LOCK(mutex, name);
|
|
std::unique_lock<std::recursive_mutex> name(mutex, std::try_to_lock_t);
|
|
|
|
ENTER_CRITICAL_SECTION(mutex); // no RAII
|
|
mutex.lock();
|
|
|
|
LEAVE_CRITICAL_SECTION(mutex); // no RAII
|
|
mutex.unlock();
|
|
*/
|
|
|
|
///////////////////////////////
|
|
// //
|
|
// THE ACTUAL IMPLEMENTATION //
|
|
// //
|
|
///////////////////////////////
|
|
|
|
#ifdef DEBUG_LOCKORDER
|
|
template <typename MutexType>
|
|
void EnterCritical(const char* pszName, const char* pszFile, int nLine, MutexType* cs, bool fTry = false);
|
|
void LeaveCritical();
|
|
void CheckLastCritical(void* cs, std::string& lockname, const char* guardname, const char* file, int line);
|
|
std::string LocksHeld();
|
|
template <typename MutexType>
|
|
void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, MutexType* cs) ASSERT_EXCLUSIVE_LOCK(cs);
|
|
void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs);
|
|
void DeleteLock(void* cs);
|
|
bool LockStackEmpty();
|
|
|
|
/**
|
|
* Call abort() if a potential lock order deadlock bug is detected, instead of
|
|
* 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
|
|
template <typename MutexType>
|
|
inline void EnterCritical(const char* pszName, const char* pszFile, int nLine, MutexType* cs, bool fTry = false) {}
|
|
inline void LeaveCritical() {}
|
|
inline void CheckLastCritical(void* cs, std::string& lockname, const char* guardname, const char* file, int line) {}
|
|
template <typename MutexType>
|
|
inline void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, MutexType* cs) ASSERT_EXCLUSIVE_LOCK(cs) {}
|
|
inline void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs) {}
|
|
inline void DeleteLock(void* cs) {}
|
|
inline bool LockStackEmpty() { return true; }
|
|
#endif
|
|
|
|
/**
|
|
* Template mixin that adds -Wthread-safety locking annotations and lock order
|
|
* checking to a subset of the mutex API.
|
|
*/
|
|
template <typename PARENT>
|
|
class LOCKABLE AnnotatedMixin : public PARENT
|
|
{
|
|
public:
|
|
~AnnotatedMixin() {
|
|
DeleteLock((void*)this);
|
|
}
|
|
|
|
void lock() EXCLUSIVE_LOCK_FUNCTION()
|
|
{
|
|
PARENT::lock();
|
|
}
|
|
|
|
void unlock() UNLOCK_FUNCTION()
|
|
{
|
|
PARENT::unlock();
|
|
}
|
|
|
|
bool try_lock() EXCLUSIVE_TRYLOCK_FUNCTION(true)
|
|
{
|
|
return PARENT::try_lock();
|
|
}
|
|
|
|
using UniqueLock = std::unique_lock<PARENT>;
|
|
#ifdef __clang__
|
|
//! For negative capabilities in the Clang Thread Safety Analysis.
|
|
//! A negative requirement uses the EXCLUSIVE_LOCKS_REQUIRED attribute, in conjunction
|
|
//! with the ! operator, to indicate that a mutex should not be held.
|
|
const AnnotatedMixin& operator!() const { return *this; }
|
|
#endif // __clang__
|
|
};
|
|
|
|
template <typename PARENT>
|
|
class LOCKABLE SharedAnnotatedMixin : public AnnotatedMixin<PARENT>
|
|
{
|
|
public:
|
|
bool try_shared_lock() SHARED_TRYLOCK_FUNCTION(true)
|
|
{
|
|
return PARENT::try_shared_lock();
|
|
}
|
|
void shared_lock() SHARED_LOCK_FUNCTION()
|
|
{
|
|
PARENT::shared_lock();
|
|
}
|
|
using SharedLock = std::shared_lock<PARENT>;
|
|
};
|
|
|
|
/**
|
|
* Wrapped mutex: supports recursive locking, but no waiting
|
|
* TODO: We should move away from using the recursive lock by default.
|
|
*/
|
|
using RecursiveMutex = AnnotatedMixin<std::recursive_mutex>;
|
|
|
|
/** Wrapped mutex: supports waiting but not recursive locking */
|
|
using Mutex = AnnotatedMixin<std::mutex>;
|
|
|
|
/** Wrapped shared mutex: supports read locking via .shared_lock, exlusive locking via .lock;
|
|
* does not support recursive locking */
|
|
using SharedMutex = SharedAnnotatedMixin<std::shared_mutex>;
|
|
|
|
#define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
|
|
|
|
inline void AssertLockNotHeldInline(const char* name, const char* file, int line, Mutex* cs) EXCLUSIVE_LOCKS_REQUIRED(!cs) { AssertLockNotHeldInternal(name, file, line, cs); }
|
|
inline void AssertLockNotHeldInline(const char* name, const char* file, int line, RecursiveMutex* cs) LOCKS_EXCLUDED(cs) { AssertLockNotHeldInternal(name, file, line, cs); }
|
|
inline void AssertLockNotHeldInline(const char* name, const char* file, int line, SharedMutex* cs) LOCKS_EXCLUDED(cs) { AssertLockNotHeldInternal(name, file, line, cs); }
|
|
#define AssertLockNotHeld(cs) AssertLockNotHeldInline(#cs, __FILE__, __LINE__, &cs)
|
|
|
|
/** Wrapper around std::unique_lock style lock for Mutex. */
|
|
template <typename Mutex, typename Base = typename Mutex::UniqueLock>
|
|
class SCOPED_LOCKABLE UniqueLock : public Base
|
|
{
|
|
private:
|
|
void Enter(const char* pszName, const char* pszFile, int nLine)
|
|
{
|
|
EnterCritical(pszName, pszFile, nLine, Base::mutex());
|
|
if (Base::try_lock()) return;
|
|
LOG_TIME_MICROS_WITH_CATEGORY(strprintf("lock contention %s, %s:%d", pszName, pszFile, nLine), BCLog::LOCK);
|
|
Base::lock();
|
|
}
|
|
|
|
bool TryEnter(const char* pszName, const char* pszFile, int nLine)
|
|
{
|
|
EnterCritical(pszName, pszFile, nLine, Base::mutex(), true);
|
|
if (Base::try_lock()) {
|
|
return true;
|
|
}
|
|
LeaveCritical();
|
|
return false;
|
|
}
|
|
|
|
public:
|
|
UniqueLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(mutexIn) : Base(mutexIn, std::defer_lock)
|
|
{
|
|
if (fTry)
|
|
TryEnter(pszName, pszFile, nLine);
|
|
else
|
|
Enter(pszName, pszFile, nLine);
|
|
}
|
|
|
|
UniqueLock(Mutex* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
|
|
{
|
|
if (!pmutexIn) return;
|
|
|
|
*static_cast<Base*>(this) = Base(*pmutexIn, std::defer_lock);
|
|
if (fTry)
|
|
TryEnter(pszName, pszFile, nLine);
|
|
else
|
|
Enter(pszName, pszFile, nLine);
|
|
}
|
|
|
|
~UniqueLock() UNLOCK_FUNCTION()
|
|
{
|
|
if (Base::owns_lock())
|
|
LeaveCritical();
|
|
}
|
|
|
|
operator bool()
|
|
{
|
|
return Base::owns_lock();
|
|
}
|
|
|
|
protected:
|
|
// needed for reverse_lock
|
|
UniqueLock() { }
|
|
|
|
public:
|
|
/**
|
|
* An RAII-style reverse lock. Unlocks on construction and locks on destruction.
|
|
*/
|
|
class reverse_lock {
|
|
public:
|
|
explicit reverse_lock(UniqueLock& _lock, const char* _guardname, const char* _file, int _line) : lock(_lock), file(_file), line(_line) {
|
|
CheckLastCritical((void*)lock.mutex(), lockname, _guardname, _file, _line);
|
|
lock.unlock();
|
|
LeaveCritical();
|
|
lock.swap(templock);
|
|
}
|
|
|
|
~reverse_lock() {
|
|
templock.swap(lock);
|
|
EnterCritical(lockname.c_str(), file.c_str(), line, lock.mutex());
|
|
lock.lock();
|
|
}
|
|
|
|
private:
|
|
reverse_lock(reverse_lock const&);
|
|
reverse_lock& operator=(reverse_lock const&);
|
|
|
|
UniqueLock& lock;
|
|
UniqueLock templock;
|
|
std::string lockname;
|
|
const std::string file;
|
|
const int line;
|
|
};
|
|
friend class reverse_lock;
|
|
};
|
|
|
|
template <typename Mutex, typename Base = typename Mutex::SharedLock>
|
|
class SCOPED_LOCKABLE SharedLock : public Base
|
|
{
|
|
private:
|
|
void SharedEnter(const char* pszName, const char* pszFile, int nLine)
|
|
{
|
|
EnterCritical(pszName, pszFile, nLine, Base::mutex());
|
|
#ifdef DEBUG_LOCKCONTENTION
|
|
if (!Base::try_lock()) {
|
|
PrintLockContention(pszName, pszFile, nLine);
|
|
#endif
|
|
Base::lock();
|
|
#ifdef DEBUG_LOCKCONTENTION
|
|
}
|
|
#endif
|
|
}
|
|
|
|
bool TrySharedEnter(const char* pszName, const char* pszFile, int nLine)
|
|
{
|
|
EnterCritical(pszName, pszFile, nLine, Base::mutex(), true);
|
|
if (Base::try_lock()) {
|
|
return true;
|
|
}
|
|
LeaveCritical();
|
|
return false;
|
|
}
|
|
|
|
public:
|
|
SharedLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) SHARED_LOCK_FUNCTION(mutexIn) : Base(mutexIn, std::defer_lock)
|
|
{
|
|
if (fTry) {
|
|
TrySharedEnter(pszName, pszFile, nLine);
|
|
} else {
|
|
SharedEnter(pszName, pszFile, nLine);
|
|
}
|
|
}
|
|
|
|
SharedLock(Mutex* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) SHARED_LOCK_FUNCTION(pmutexIn)
|
|
{
|
|
if (!pmutexIn) return;
|
|
|
|
*static_cast<Base*>(this) = Base(*pmutexIn, std::defer_lock);
|
|
if (fTry) {
|
|
TrySharedEnter(pszName, pszFile, nLine);
|
|
} else {
|
|
SharedEnter(pszName, pszFile, nLine);
|
|
}
|
|
}
|
|
|
|
~SharedLock() UNLOCK_FUNCTION()
|
|
{
|
|
if (Base::owns_lock()) {
|
|
LeaveCritical();
|
|
}
|
|
}
|
|
};
|
|
|
|
#define REVERSE_LOCK(g) typename std::decay<decltype(g)>::type::reverse_lock UNIQUE_NAME(revlock)(g, #g, __FILE__, __LINE__)
|
|
|
|
template<typename MutexArg>
|
|
using DebugLock = UniqueLock<typename std::remove_reference<typename std::remove_pointer<MutexArg>::type>::type>;
|
|
template<typename MutexArg>
|
|
using ReadLock = SharedLock<typename std::remove_reference<typename std::remove_pointer<MutexArg>::type>::type>;
|
|
|
|
#define LOCK(cs) DebugLock<decltype(cs)> UNIQUE_NAME(criticalblock)(cs, #cs, __FILE__, __LINE__)
|
|
#define READ_LOCK(cs) ReadLock<decltype(cs)> UNIQUE_NAME(criticalblock)(cs, #cs, __FILE__, __LINE__)
|
|
#define LOCK2(cs1, cs2) \
|
|
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 TRY_READ_LOCK(cs, name) ReadLock<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) \
|
|
{ \
|
|
EnterCritical(#cs, __FILE__, __LINE__, &cs); \
|
|
(cs).lock(); \
|
|
}
|
|
|
|
#define LEAVE_CRITICAL_SECTION(cs) \
|
|
{ \
|
|
std::string lockname; \
|
|
CheckLastCritical(reinterpret_cast<void*>(&cs), lockname, #cs, __FILE__, __LINE__); \
|
|
(cs).unlock(); \
|
|
LeaveCritical(); \
|
|
}
|
|
|
|
//! Run code while locking a mutex.
|
|
//!
|
|
//! Examples:
|
|
//!
|
|
//! WITH_LOCK(cs, shared_val = shared_val + 1);
|
|
//!
|
|
//! int val = WITH_LOCK(cs, return shared_val);
|
|
//!
|
|
//! Note:
|
|
//!
|
|
//! Since the return type deduction follows that of decltype(auto), while the
|
|
//! deduced type of:
|
|
//!
|
|
//! WITH_LOCK(cs, return {int i = 1; return i;});
|
|
//!
|
|
//! is int, the deduced type of:
|
|
//!
|
|
//! WITH_LOCK(cs, return {int j = 1; return (j);});
|
|
//!
|
|
//! is &int, a reference to a local variable
|
|
//!
|
|
//! The above is detectable at compile-time with the -Wreturn-local-addr flag in
|
|
//! gcc and the -Wreturn-stack-address flag in clang, both enabled by default.
|
|
#define WITH_LOCK(cs, code) [&]() -> decltype(auto) { LOCK(cs); code; }()
|
|
#define WITH_READ_LOCK(cs, code) [&]() -> decltype(auto) { READ_LOCK(cs); code; }()
|
|
|
|
class CSemaphore
|
|
{
|
|
private:
|
|
std::condition_variable condition;
|
|
std::mutex mutex;
|
|
int value;
|
|
|
|
public:
|
|
explicit CSemaphore(int init) : value(init) {}
|
|
|
|
void wait()
|
|
{
|
|
std::unique_lock<std::mutex> lock(mutex);
|
|
condition.wait(lock, [&]() { return value >= 1; });
|
|
value--;
|
|
}
|
|
|
|
bool try_wait()
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
if (value < 1)
|
|
return false;
|
|
value--;
|
|
return true;
|
|
}
|
|
|
|
void post()
|
|
{
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
value++;
|
|
}
|
|
condition.notify_one();
|
|
}
|
|
};
|
|
|
|
/** RAII-style semaphore lock */
|
|
class CSemaphoreGrant
|
|
{
|
|
private:
|
|
CSemaphore* sem;
|
|
bool fHaveGrant;
|
|
|
|
public:
|
|
void Acquire()
|
|
{
|
|
if (fHaveGrant)
|
|
return;
|
|
sem->wait();
|
|
fHaveGrant = true;
|
|
}
|
|
|
|
void Release()
|
|
{
|
|
if (!fHaveGrant)
|
|
return;
|
|
sem->post();
|
|
fHaveGrant = false;
|
|
}
|
|
|
|
bool TryAcquire()
|
|
{
|
|
if (!fHaveGrant && sem->try_wait())
|
|
fHaveGrant = true;
|
|
return fHaveGrant;
|
|
}
|
|
|
|
void MoveTo(CSemaphoreGrant& grant)
|
|
{
|
|
grant.Release();
|
|
grant.sem = sem;
|
|
grant.fHaveGrant = fHaveGrant;
|
|
fHaveGrant = false;
|
|
}
|
|
|
|
CSemaphoreGrant() : sem(nullptr), fHaveGrant(false) {}
|
|
|
|
explicit CSemaphoreGrant(CSemaphore& sema, bool fTry = false) : sem(&sema), fHaveGrant(false)
|
|
{
|
|
if (fTry)
|
|
TryAcquire();
|
|
else
|
|
Acquire();
|
|
}
|
|
|
|
~CSemaphoreGrant()
|
|
{
|
|
Release();
|
|
}
|
|
|
|
operator bool() const
|
|
{
|
|
return fHaveGrant;
|
|
}
|
|
};
|
|
|
|
// Utility class for indicating to compiler thread analysis that a mutex is
|
|
// locked (when it couldn't be determined otherwise).
|
|
struct SCOPED_LOCKABLE LockAssertion
|
|
{
|
|
template <typename Mutex>
|
|
explicit LockAssertion(Mutex& mutex) EXCLUSIVE_LOCK_FUNCTION(mutex)
|
|
{
|
|
#ifdef DEBUG_LOCKORDER
|
|
AssertLockHeld(mutex);
|
|
#endif
|
|
}
|
|
~LockAssertion() UNLOCK_FUNCTION() {}
|
|
};
|
|
|
|
#endif // BITCOIN_SYNC_H
|