Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
251// it is not possible to use the lock's copy of the mutex for that purpose.252// Instead, the original mutex needs to be passed back to the reverse_lock for253// the sake of thread-safety analysis, but it is not actually used otherwise.254#define REVERSE_LOCK(g, cs) typename std::decay<decltype(g)>::type::reverse_lock BITCOIN_UNIQUE_NAME(revlock)(g, cs, #cs, __FILE__, __LINE__)256// When locking a Mutex, require negative capability to ensure the lock257// is not already held258inline Mutex& MaybeCheckNotHeld(Mutex& cs) EXCLUSIVE_LOCKS_REQUIRED(!cs) LOCK_RETURNED(cs) { return cs; }259inline Mutex* MaybeCheckNotHeld(Mutex* cs) EXCLUSIVE_LOCKS_REQUIRED(!cs) LOCK_RETURNED(cs) { return cs; }261// When locking a GlobalMutex or RecursiveMutex, just check it is not262// locked in the surrounding scope.263template <typename MutexType>264inline MutexType& MaybeCheckNotHeld(MutexType& m) LOCKS_EXCLUDED(m) LOCK_RETURNED(m) { return m; }265template <typename MutexType>266inline MutexType* MaybeCheckNotHeld(MutexType* m) LOCKS_EXCLUDED(m) LOCK_RETURNED(m) { return m; }268#define LOCK(cs) UniqueLock BITCOIN_UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)269#define LOCK2(cs1, cs2) \270UniqueLock criticalblock1(MaybeCheckNotHeld(cs1), #cs1, __FILE__, __LINE__); \271UniqueLock criticalblock2(MaybeCheckNotHeld(cs2), #cs2, __FILE__, __LINE__)272#define LOCK_ARGS(cs) MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__273#define TRY_LOCK(cs, name) UniqueLock name(LOCK_ARGS(cs), true)274#define WAIT_LOCK(cs, name) UniqueLock name(LOCK_ARGS(cs))276//! Run code while locking a mutex.277//!278//! Examples:279//!280//! WITH_LOCK(cs, shared_val = shared_val + 1);281//!282//! int val = WITH_LOCK(cs, return shared_val);283//!284//! Note:285//!286//! Since the return type deduction follows that of decltype(auto), while the287//! deduced type of:288//!289//! WITH_LOCK(cs, int i = 1; return i);290//!291//! is int, the deduced type of:292//!293//! WITH_LOCK(cs, int j = 1; return (j));294//!295//! is &int, a reference to a local variable296//!297//! The above is detectable at compile-time with the -Wreturn-local-addr flag in298//! gcc and the -Wreturn-stack-address flag in clang, both enabled by default.299#define WITH_LOCK(cs, code) (MaybeCheckNotHeld(cs), [&]() -> decltype(auto) { LOCK(cs); code; }())301#endif // BITCOIN_SYNC_H
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.