aboutsummaryrefslogtreecommitdiff
path: root/src/sync.h
diff options
context:
space:
mode:
authorVasil Dimov <vd@FreeBSD.org>2020-06-19 20:51:16 +0200
committerVasil Dimov <vd@FreeBSD.org>2020-08-05 09:42:42 +0200
commit4df6567e4cbb4677e8048de2f8008612e1b860b9 (patch)
treeb33c0dbe0e968d4157b3fb565408a290d12864f6 /src/sync.h
parent0f16212c5931b30f430014caa485de53f9a14920 (diff)
downloadbitcoin-4df6567e4cbb4677e8048de2f8008612e1b860b9.tar.xz
sync: make EnterCritical() & push_lock() type safe
The functions `EnterCritical()` and `push_lock()` take a pointer to a mutex, but that pointer used to be of type `void*` because we use a few different types for mutexes. This `void*` argument was not type safe because somebody could have send a pointer to anything that is not a mutex. Furthermore it wouldn't allow to check whether the passed mutex is recursive or not. Thus, change the functions to templated ones so that we can implement stricter checks for non-recursive mutexes. This also simplifies the callers of `EnterCritical()`.
Diffstat (limited to 'src/sync.h')
-rw-r--r--src/sync.h14
1 files changed, 8 insertions, 6 deletions
diff --git a/src/sync.h b/src/sync.h
index 05ff2ee8a9..ef9ad4c545 100644
--- a/src/sync.h
+++ b/src/sync.h
@@ -48,7 +48,8 @@ LEAVE_CRITICAL_SECTION(mutex); // no RAII
///////////////////////////////
#ifdef DEBUG_LOCKORDER
-void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false);
+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();
@@ -65,7 +66,8 @@ bool LockStackEmpty();
*/
extern bool g_debug_lockorder_abort;
#else
-inline void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false) {}
+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>
@@ -133,7 +135,7 @@ class SCOPED_LOCKABLE UniqueLock : public Base
private:
void Enter(const char* pszName, const char* pszFile, int nLine)
{
- EnterCritical(pszName, pszFile, nLine, (void*)(Base::mutex()));
+ EnterCritical(pszName, pszFile, nLine, Base::mutex());
#ifdef DEBUG_LOCKCONTENTION
if (!Base::try_lock()) {
PrintLockContention(pszName, pszFile, nLine);
@@ -146,7 +148,7 @@ private:
bool TryEnter(const char* pszName, const char* pszFile, int nLine)
{
- EnterCritical(pszName, pszFile, nLine, (void*)(Base::mutex()), true);
+ EnterCritical(pszName, pszFile, nLine, Base::mutex(), true);
Base::try_lock();
if (!Base::owns_lock())
LeaveCritical();
@@ -203,7 +205,7 @@ public:
~reverse_lock() {
templock.swap(lock);
- EnterCritical(lockname.c_str(), file.c_str(), line, (void*)lock.mutex());
+ EnterCritical(lockname.c_str(), file.c_str(), line, lock.mutex());
lock.lock();
}
@@ -234,7 +236,7 @@ using DebugLock = UniqueLock<typename std::remove_reference<typename std::remove
#define ENTER_CRITICAL_SECTION(cs) \
{ \
- EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \
+ EnterCritical(#cs, __FILE__, __LINE__, &cs); \
(cs).lock(); \
}