From 58e6881bc5be002e8ddbc9b75422c0deae66a2df Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Mon, 18 May 2020 17:45:46 +0300 Subject: [PATCH] refactor: Refactor duplicated code into LockHeld() --- src/sync.cpp | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/sync.cpp b/src/sync.cpp index cfbbb78c6b2..5817e22c86e 100644 --- a/src/sync.cpp +++ b/src/sync.cpp @@ -185,23 +185,27 @@ std::string LocksHeld() return result; } +static bool LockHeld(void* mutex) +{ + for (const LockStackItem& i : g_lockstack) { + if (i.first == mutex) return true; + } + + return false; +} + void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs) { - for (const LockStackItem& i : g_lockstack) - if (i.first == cs) - return; + if (LockHeld(cs)) return; tfm::format(std::cerr, "Assertion failed: lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld()); abort(); } void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs) { - for (const LockStackItem& i : g_lockstack) { - if (i.first == cs) { - tfm::format(std::cerr, "Assertion failed: lock %s held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld()); - abort(); - } - } + if (!LockHeld(cs)) return; + tfm::format(std::cerr, "Assertion failed: lock %s held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld()); + abort(); } void DeleteLock(void* cs)