@@ -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+
212241template <typename MutexType>
213242void 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}
217248template void EnterCritical (const char *, const char *, int , Mutex*, bool );
218249template 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
222253void 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
248281void 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
278288template <typename MutexType>
279289void 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}
285297template void AssertLockHeldInternal (const char *, const char *, int , Mutex*);
286298template void AssertLockHeldInternal (const char *, const char *, int , RecursiveMutex*);
287299
288300template <typename MutexType>
289301void 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}
295309template void AssertLockNotHeldInternal (const char *, const char *, int , Mutex*);
296310template void AssertLockNotHeldInternal (const char *, const char *, int , RecursiveMutex*);
297311
298312void 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
317333bool 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