Skip to content

Commit d36d086

Browse files
committed
Move DEBUG_LOCKORDER out of sync.h and into implementation
1 parent c7ffdb1 commit d36d086

File tree

2 files changed

+46
-42
lines changed

2 files changed

+46
-42
lines changed

src/kernel/sync.h

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ LEAVE_CRITICAL_SECTION(mutex); // no RAII
5151
// //
5252
///////////////////////////////
5353

54-
#ifdef DEBUG_LOCKORDER
5554
template <typename MutexType>
5655
void EnterCritical(const char* pszName, const char* pszFile, int nLine, MutexType* cs, bool fTry = false);
5756
void LeaveCritical();
@@ -70,18 +69,7 @@ bool LockStackEmpty();
7069
* set to false in DEBUG_LOCKORDER unit tests.
7170
*/
7271
extern bool g_debug_lockorder_abort;
73-
#else
74-
template <typename MutexType>
75-
inline void EnterCritical(const char* pszName, const char* pszFile, int nLine, MutexType* cs, bool fTry = false) {}
76-
inline void LeaveCritical() {}
77-
inline void CheckLastCritical(void* cs, std::string& lockname, const char* guardname, const char* file, int line) {}
78-
template <typename MutexType>
79-
inline void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, MutexType* cs) EXCLUSIVE_LOCKS_REQUIRED(cs) {}
80-
template <typename MutexType>
81-
void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, MutexType* cs) LOCKS_EXCLUDED(cs) {}
82-
inline void DeleteLock(void* cs) {}
83-
inline bool LockStackEmpty() { return true; }
84-
#endif
72+
8573
#define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
8674
#define AssertLockNotHeld(cs) AssertLockNotHeldInternal(#cs, __FILE__, __LINE__, &cs)
8775

src/sync.cpp

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,41 @@ static void pop_lock()
209209
}
210210
}
211211

212+
static bool LockHeld(void* mutex)
213+
{
214+
LockData& lockdata = GetLockData();
215+
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
216+
217+
const LockStack& lock_stack = lockdata.m_lock_stacks[std::this_thread::get_id()];
218+
for (const LockStackItem& i : lock_stack) {
219+
if (i.first == mutex) return true;
220+
}
221+
222+
return false;
223+
}
224+
225+
std::string LocksHeld()
226+
{
227+
LockData& lockdata = GetLockData();
228+
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
229+
230+
const LockStack& lock_stack = lockdata.m_lock_stacks[std::this_thread::get_id()];
231+
std::string result;
232+
for (const LockStackItem& i : lock_stack)
233+
result += i.second.ToString() + std::string("\n");
234+
return result;
235+
}
236+
237+
bool g_debug_lockorder_abort = true;
238+
239+
#endif // DEBUG_LOCKORDER
240+
212241
template <typename MutexType>
213242
void EnterCritical(const char* pszName, const char* pszFile, int nLine, MutexType* cs, bool fTry)
214243
{
244+
#ifdef DEBUG_LOCKORDER
215245
push_lock(cs, CLockLocation(pszName, pszFile, nLine, fTry, util::ThreadGetInternalName()));
246+
#endif
216247
}
217248
template void EnterCritical(const char*, const char*, int, Mutex*, bool);
218249
template void EnterCritical(const char*, const char*, int, RecursiveMutex*, bool);
@@ -221,6 +252,7 @@ template void EnterCritical(const char*, const char*, int, std::recursive_mutex*
221252

222253
void CheckLastCritical(void* cs, std::string& lockname, const char* guardname, const char* file, int line)
223254
{
255+
#ifdef DEBUG_LOCKORDER
224256
LockData& lockdata = GetLockData();
225257
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
226258

@@ -243,60 +275,43 @@ void CheckLastCritical(void* cs, std::string& lockname, const char* guardname, c
243275
abort();
244276
}
245277
throw std::logic_error(strprintf("%s was not most recent critical section locked", guardname));
278+
#endif
246279
}
247280

248281
void LeaveCritical()
249282
{
283+
#ifdef DEBUG_LOCKORDER
250284
pop_lock();
251-
}
252-
253-
std::string LocksHeld()
254-
{
255-
LockData& lockdata = GetLockData();
256-
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
257-
258-
const LockStack& lock_stack = lockdata.m_lock_stacks[std::this_thread::get_id()];
259-
std::string result;
260-
for (const LockStackItem& i : lock_stack)
261-
result += i.second.ToString() + std::string("\n");
262-
return result;
263-
}
264-
265-
static bool LockHeld(void* mutex)
266-
{
267-
LockData& lockdata = GetLockData();
268-
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
269-
270-
const LockStack& lock_stack = lockdata.m_lock_stacks[std::this_thread::get_id()];
271-
for (const LockStackItem& i : lock_stack) {
272-
if (i.first == mutex) return true;
273-
}
274-
275-
return false;
285+
#endif
276286
}
277287

278288
template <typename MutexType>
279289
void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, MutexType* cs)
280290
{
291+
#ifdef DEBUG_LOCKORDER
281292
if (LockHeld(cs)) return;
282293
tfm::format(std::cerr, "Assertion failed: lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld());
283294
abort();
295+
#endif
284296
}
285297
template void AssertLockHeldInternal(const char*, const char*, int, Mutex*);
286298
template void AssertLockHeldInternal(const char*, const char*, int, RecursiveMutex*);
287299

288300
template <typename MutexType>
289301
void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, MutexType* cs)
290302
{
303+
#ifdef DEBUG_LOCKORDER
291304
if (!LockHeld(cs)) return;
292305
tfm::format(std::cerr, "Assertion failed: lock %s held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld());
293306
abort();
307+
#endif
294308
}
295309
template void AssertLockNotHeldInternal(const char*, const char*, int, Mutex*);
296310
template void AssertLockNotHeldInternal(const char*, const char*, int, RecursiveMutex*);
297311

298312
void DeleteLock(void* cs)
299313
{
314+
#ifdef DEBUG_LOCKORDER
300315
LockData& lockdata = GetLockData();
301316
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
302317
const LockPair item = std::make_pair(cs, nullptr);
@@ -312,19 +327,20 @@ void DeleteLock(void* cs)
312327
lockdata.lockorders.erase(invinvitem);
313328
lockdata.invlockorders.erase(invit++);
314329
}
330+
#endif
315331
}
316332

317333
bool LockStackEmpty()
318334
{
335+
#ifdef DEBUG_LOCKORDER
319336
LockData& lockdata = GetLockData();
320337
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
321338
const auto it = lockdata.m_lock_stacks.find(std::this_thread::get_id());
322339
if (it == lockdata.m_lock_stacks.end()) {
323340
return true;
324341
}
325342
return it->second.empty();
343+
#else
344+
return true;
345+
#endif
326346
}
327-
328-
bool g_debug_lockorder_abort = true;
329-
330-
#endif /* DEBUG_LOCKORDER */

0 commit comments

Comments
 (0)