Skip to content

Commit 92b973c

Browse files
committed
Move DEBUG_LOCKCONTENTION out of sync.h and into implementation
1 parent d36d086 commit 92b973c

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

src/kernel/sync.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,8 @@ using RecursiveMutex = AnnotatedMixin<std::recursive_mutex>;
118118
/** Wrapped mutex: supports waiting but not recursive locking */
119119
typedef AnnotatedMixin<std::mutex> Mutex;
120120

121-
#ifdef DEBUG_LOCKCONTENTION
122-
void PrintLockContention(const char* pszName, const char* pszFile, int nLine);
123-
#endif
121+
template <typename Base>
122+
void CheckContentionAndLock(Base* base, const char* pszName, const char* pszFile, int nLine);
124123

125124
/** Wrapper around std::unique_lock style lock for Mutex. */
126125
template <typename Mutex, typename Base = typename Mutex::UniqueLock>
@@ -130,14 +129,7 @@ class SCOPED_LOCKABLE UniqueLock : public Base
130129
void Enter(const char* pszName, const char* pszFile, int nLine)
131130
{
132131
EnterCritical(pszName, pszFile, nLine, Base::mutex());
133-
#ifdef DEBUG_LOCKCONTENTION
134-
if (!Base::try_lock()) {
135-
PrintLockContention(pszName, pszFile, nLine);
136-
#endif
137-
Base::lock();
138-
#ifdef DEBUG_LOCKCONTENTION
139-
}
140-
#endif
132+
CheckContentionAndLock(static_cast<Base*>(this), pszName, pszFile, nLine);
141133
}
142134

143135
bool TryEnter(const char* pszName, const char* pszFile, int nLine)

src/sync.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@
2323
#if !defined(HAVE_THREAD_LOCAL)
2424
static_assert(false, "thread_local is not supported");
2525
#endif
26-
void PrintLockContention(const char* pszName, const char* pszFile, int nLine)
27-
{
28-
LogPrintf("LOCKCONTENTION: %s\n", pszName);
29-
LogPrintf("Locker: %s:%d\n", pszFile, nLine);
30-
}
3126
#endif /* DEBUG_LOCKCONTENTION */
3227

3328
#ifdef DEBUG_LOCKORDER
@@ -344,3 +339,26 @@ bool LockStackEmpty()
344339
return true;
345340
#endif
346341
}
342+
343+
#ifdef DEBUG_LOCKCONTENTION
344+
static void PrintLockContention(const char* pszName, const char* pszFile, int nLine)
345+
{
346+
LogPrintf("LOCKCONTENTION: %s\n", pszName);
347+
LogPrintf("Locker: %s:%d\n", pszFile, nLine);
348+
}
349+
#endif
350+
351+
template <typename Base>
352+
void CheckContentionAndLock(Base* base, const char* pszName, const char* pszFile, int nLine)
353+
{
354+
#ifdef DEBUG_LOCKCONTENTION
355+
if (!base->try_lock()) {
356+
PrintLockContention(pszName, pszFile, nLine);
357+
#endif
358+
base->lock();
359+
#ifdef DEBUG_LOCKCONTENTION
360+
}
361+
#endif
362+
}
363+
template void CheckContentionAndLock(Mutex::UniqueLock*, const char*, const char*, int);
364+
template void CheckContentionAndLock(RecursiveMutex::UniqueLock*, const char*, const char*, int);

0 commit comments

Comments
 (0)