refactor: Move `StdMutex` to its own header
What changed, and why it matters
This commit is a simple code reorganization: it moves two helper classes (StdMutex and StdLockGuard) from one header file to a new header file and updates which files include them. There is no change to how the code behaves, no bug fix, and no security-related change.
No security action needed. Treat as normal code maintenance.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors the Bitcoin Core codebase by extracting StdMutex and StdLockGuard from src/threadsafety.h into a new header src/util/stdmutex.h. It updates src/logging.h to include the new header instead of threadsafety.h. The class definitions, annotations, and behavior are unchanged. threadsafety.h no longer includes
Changed components
src/threadsafety.hsrc/util/stdmutex.hsrc/logging.hInspect captured patch +36 / −25
diff --git a/src/logging.h b/src/logging.h
index e2149501..803bdac1 100644
--- a/src/logging.h
+++ b/src/logging.h
@@ -8,9 +8,9 @@
#include <crypto/siphash.h>
#include <logging/categories.h> // IWYU pragma: export
-#include <threadsafety.h>
#include <util/fs.h>
#include <util/log.h> // IWYU pragma: export
+#include <util/stdmutex.h>
#include <util/string.h>
#include <util/time.h>
diff --git a/src/threadsafety.h b/src/threadsafety.h
index b79d4c76..35f3b813 100644
--- a/src/threadsafety.h
+++ b/src/threadsafety.h
@@ -6,8 +6,6 @@
#ifndef BITCOIN_THREADSAFETY_H
#define BITCOIN_THREADSAFETY_H
-#include <mutex>
-
#ifdef __clang__
// TL;DR Add GUARDED_BY(mutex) to member variables. The others are
// rarely necessary. Ex: int nFoo GUARDED_BY(cs_foo);
@@ -54,26 +52,4 @@
#define ASSERT_EXCLUSIVE_LOCK(...)
#endif // __GNUC__
-// StdMutex provides an annotated version of std::mutex for us,
-// and should only be used when sync.h Mutex/LOCK/etc are not usable.
-class LOCKABLE StdMutex : public std::mutex
-{
-public:
-#ifdef __clang__
- //! For negative capabilities in the Clang Thread Safety Analysis.
- //! A negative requirement uses the EXCLUSIVE_LOCKS_REQUIRED attribute, in conjunction
- //! with the ! operator, to indicate that a mutex should not be held.
- const StdMutex& operator!() const { return *this; }
-#endif // __clang__
-};
-
-// StdLockGuard provides an annotated version of std::lock_guard for us,
-// and should only be used when sync.h Mutex/LOCK/etc are not usable.
-class SCOPED_LOCKABLE StdLockGuard : public std::lock_guard<StdMutex>
-{
-public:
- explicit StdLockGuard(StdMutex& cs) EXCLUSIVE_LOCK_FUNCTION(cs) : std::lock_guard<StdMutex>(cs) {}
- ~StdLockGuard() UNLOCK_FUNCTION() = default;
-};
-
#endif // BITCOIN_THREADSAFETY_H
diff --git a/src/util/stdmutex.h b/src/util/stdmutex.h
new file mode 100644
index 00000000..4c3c0cec
--- /dev/null
+++ b/src/util/stdmutex.h
@@ -0,0 +1,35 @@
+// Copyright (c) 2009-2010 Satoshi Nakamoto
+// Copyright (c) 2009-present The Bitcoin Core developers
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#ifndef BITCOIN_UTIL_STDMUTEX_H
+#define BITCOIN_UTIL_STDMUTEX_H
+
+#include <threadsafety.h> // IWYU pragma: export
+
+#include <mutex>
+
+// StdMutex provides an annotated version of std::mutex for us,
+// and should only be used when sync.h Mutex/LOCK/etc are not usable.
+class LOCKABLE StdMutex : public std::mutex
+{
+public:
+#ifdef __clang__
+ //! For negative capabilities in the Clang Thread Safety Analysis.
+ //! A negative requirement uses the EXCLUSIVE_LOCKS_REQUIRED attribute, in conjunction
+ //! with the ! operator, to indicate that a mutex should not be held.
+ const StdMutex& operator!() const { return *this; }
+#endif // __clang__
+};
+
+// StdLockGuard provides an annotated version of std::lock_guard for us,
+// and should only be used when sync.h Mutex/LOCK/etc are not usable.
+class SCOPED_LOCKABLE StdLockGuard : public std::lock_guard<StdMutex>
+{
+public:
+ explicit StdLockGuard(StdMutex& cs) EXCLUSIVE_LOCK_FUNCTION(cs) : std::lock_guard<StdMutex>(cs) {}
+ ~StdLockGuard() UNLOCK_FUNCTION() = default;
+};
+
+#endif // BITCOIN_UTIL_STDMUTEX_H
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.