aboutsummaryrefslogtreecommitdiff
path: root/src/sync.cpp
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.cpp
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.cpp')
-rw-r--r--src/sync.cpp14
1 files changed, 12 insertions, 2 deletions
diff --git a/src/sync.cpp b/src/sync.cpp
index 4be13a3c48..7de8439d6f 100644
--- a/src/sync.cpp
+++ b/src/sync.cpp
@@ -13,7 +13,10 @@
#include <util/strencodings.h>
#include <util/threadnames.h>
+#include <boost/thread/mutex.hpp>
+
#include <map>
+#include <mutex>
#include <set>
#include <system_error>
#include <thread>
@@ -135,7 +138,8 @@ static void potential_deadlock_detected(const LockPair& mismatch, const LockStac
throw std::logic_error(strprintf("potential deadlock detected: %s -> %s -> %s", mutex_b, mutex_a, mutex_b));
}
-static void push_lock(void* c, const CLockLocation& locklocation)
+template <typename MutexType>
+static void push_lock(MutexType* c, const CLockLocation& locklocation)
{
LockData& lockdata = GetLockData();
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
@@ -175,10 +179,16 @@ static void pop_lock()
}
}
-void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry)
+template <typename MutexType>
+void EnterCritical(const char* pszName, const char* pszFile, int nLine, MutexType* cs, bool fTry)
{
push_lock(cs, CLockLocation(pszName, pszFile, nLine, fTry, util::ThreadGetInternalName()));
}
+template void EnterCritical(const char*, const char*, int, Mutex*, bool);
+template void EnterCritical(const char*, const char*, int, RecursiveMutex*, bool);
+template void EnterCritical(const char*, const char*, int, std::mutex*, bool);
+template void EnterCritical(const char*, const char*, int, std::recursive_mutex*, bool);
+template void EnterCritical(const char*, const char*, int, boost::mutex*, bool);
void CheckLastCritical(void* cs, std::string& lockname, const char* guardname, const char* file, int line)
{