Use the proper attribute from Clang’s thread safety analysis for
AssertLockHeld()
:
-
if
DEBUG_LOCKORDER
is defined thenAssertLockHeld()
will check if the caller owns the given mutex and willabort()
if not. Clang has an attribute exactly for that -ASSERT_EXCLUSIVE_LOCK()
, documented as: “These are attributes on a function or method that does a run-time test to see whether the calling thread holds the given capability. The function is assumed to fail (no return) if the capability is not held.” [1] -
if
DEBUG_LOCKORDER
is not defined, thenAssertLockHeld()
does nothing, thus don’t tag it with any attributes (don’t fool the compiler that we do something which we don’t).
Replace LockAssertion
with AssertLockHeld
and remove the former.
On the places where Clang cannot deduce that a mutex is held, use
NO_THREAD_SAFETY_ANALYSIS
, intended to be used when a code is
“too complicated for the analysis to understand” [2].
[1] https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#assert-capability-and-assert-shared-capability [2] https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#no-thread-safety-analysis
PR is compared with alternatives in https://github.com/bitcoin-core/bitcoin-devwiki/wiki/AssertLockHeld-PRs