Skip to content

Commit 58e6881

Browse files
committed
refactor: Refactor duplicated code into LockHeld()
1 parent f511f61 commit 58e6881

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/sync.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -185,23 +185,27 @@ std::string LocksHeld()
185185
return result;
186186
}
187187

188+
static bool LockHeld(void* mutex)
189+
{
190+
for (const LockStackItem& i : g_lockstack) {
191+
if (i.first == mutex) return true;
192+
}
193+
194+
return false;
195+
}
196+
188197
void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs)
189198
{
190-
for (const LockStackItem& i : g_lockstack)
191-
if (i.first == cs)
192-
return;
199+
if (LockHeld(cs)) return;
193200
tfm::format(std::cerr, "Assertion failed: lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld());
194201
abort();
195202
}
196203

197204
void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs)
198205
{
199-
for (const LockStackItem& i : g_lockstack) {
200-
if (i.first == cs) {
201-
tfm::format(std::cerr, "Assertion failed: lock %s held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld());
202-
abort();
203-
}
204-
}
206+
if (!LockHeld(cs)) return;
207+
tfm::format(std::cerr, "Assertion failed: lock %s held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld());
208+
abort();
205209
}
206210

207211
void DeleteLock(void* cs)

0 commit comments

Comments
 (0)