Skip to content

Commit 4df6567

Browse files
committed
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()`.
1 parent 0f16212 commit 4df6567

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/sync.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
#include <util/strencodings.h>
1414
#include <util/threadnames.h>
1515

16+
#include <boost/thread/mutex.hpp>
17+
1618
#include <map>
19+
#include <mutex>
1720
#include <set>
1821
#include <system_error>
1922
#include <thread>
@@ -135,7 +138,8 @@ static void potential_deadlock_detected(const LockPair& mismatch, const LockStac
135138
throw std::logic_error(strprintf("potential deadlock detected: %s -> %s -> %s", mutex_b, mutex_a, mutex_b));
136139
}
137140

138-
static void push_lock(void* c, const CLockLocation& locklocation)
141+
template <typename MutexType>
142+
static void push_lock(MutexType* c, const CLockLocation& locklocation)
139143
{
140144
LockData& lockdata = GetLockData();
141145
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
@@ -175,10 +179,16 @@ static void pop_lock()
175179
}
176180
}
177181

178-
void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry)
182+
template <typename MutexType>
183+
void EnterCritical(const char* pszName, const char* pszFile, int nLine, MutexType* cs, bool fTry)
179184
{
180185
push_lock(cs, CLockLocation(pszName, pszFile, nLine, fTry, util::ThreadGetInternalName()));
181186
}
187+
template void EnterCritical(const char*, const char*, int, Mutex*, bool);
188+
template void EnterCritical(const char*, const char*, int, RecursiveMutex*, bool);
189+
template void EnterCritical(const char*, const char*, int, std::mutex*, bool);
190+
template void EnterCritical(const char*, const char*, int, std::recursive_mutex*, bool);
191+
template void EnterCritical(const char*, const char*, int, boost::mutex*, bool);
182192

183193
void CheckLastCritical(void* cs, std::string& lockname, const char* guardname, const char* file, int line)
184194
{

src/sync.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ LEAVE_CRITICAL_SECTION(mutex); // no RAII
4848
///////////////////////////////
4949

5050
#ifdef DEBUG_LOCKORDER
51-
void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false);
51+
template <typename MutexType>
52+
void EnterCritical(const char* pszName, const char* pszFile, int nLine, MutexType* cs, bool fTry = false);
5253
void LeaveCritical();
5354
void CheckLastCritical(void* cs, std::string& lockname, const char* guardname, const char* file, int line);
5455
std::string LocksHeld();
@@ -65,7 +66,8 @@ bool LockStackEmpty();
6566
*/
6667
extern bool g_debug_lockorder_abort;
6768
#else
68-
inline void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false) {}
69+
template <typename MutexType>
70+
inline void EnterCritical(const char* pszName, const char* pszFile, int nLine, MutexType* cs, bool fTry = false) {}
6971
inline void LeaveCritical() {}
7072
inline void CheckLastCritical(void* cs, std::string& lockname, const char* guardname, const char* file, int line) {}
7173
template <typename MutexType>
@@ -133,7 +135,7 @@ class SCOPED_LOCKABLE UniqueLock : public Base
133135
private:
134136
void Enter(const char* pszName, const char* pszFile, int nLine)
135137
{
136-
EnterCritical(pszName, pszFile, nLine, (void*)(Base::mutex()));
138+
EnterCritical(pszName, pszFile, nLine, Base::mutex());
137139
#ifdef DEBUG_LOCKCONTENTION
138140
if (!Base::try_lock()) {
139141
PrintLockContention(pszName, pszFile, nLine);
@@ -146,7 +148,7 @@ class SCOPED_LOCKABLE UniqueLock : public Base
146148

147149
bool TryEnter(const char* pszName, const char* pszFile, int nLine)
148150
{
149-
EnterCritical(pszName, pszFile, nLine, (void*)(Base::mutex()), true);
151+
EnterCritical(pszName, pszFile, nLine, Base::mutex(), true);
150152
Base::try_lock();
151153
if (!Base::owns_lock())
152154
LeaveCritical();
@@ -203,7 +205,7 @@ class SCOPED_LOCKABLE UniqueLock : public Base
203205

204206
~reverse_lock() {
205207
templock.swap(lock);
206-
EnterCritical(lockname.c_str(), file.c_str(), line, (void*)lock.mutex());
208+
EnterCritical(lockname.c_str(), file.c_str(), line, lock.mutex());
207209
lock.lock();
208210
}
209211

@@ -234,7 +236,7 @@ using DebugLock = UniqueLock<typename std::remove_reference<typename std::remove
234236

235237
#define ENTER_CRITICAL_SECTION(cs) \
236238
{ \
237-
EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \
239+
EnterCritical(#cs, __FILE__, __LINE__, &cs); \
238240
(cs).lock(); \
239241
}
240242

0 commit comments

Comments
 (0)