Inside Bitcoin
Opening the selected file…
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.
1// Copyright (c) 2009-2010 Satoshi Nakamoto2// Copyright (c) 2009-present The Bitcoin Core developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or http://www.opensource.org/licenses/mit-license.php.6#ifndef BITCOIN_UTIL_STDMUTEX_H7#define BITCOIN_UTIL_STDMUTEX_H9// This header declares threading primitives compatible with Clang10// Thread Safety Analysis and provides appropriate annotation macros.11#include <threadsafety.h> // IWYU pragma: export13#include <util/macros.h>15#include <mutex>17// StdMutex provides an annotated version of std::mutex for us,18// and should only be used when sync.h Mutex/LOCK/etc are not usable.19class LOCKABLE StdMutex : public std::mutex20{21public:22#ifdef __clang__23//! For negative capabilities in the Clang Thread Safety Analysis.24//! A negative requirement uses the EXCLUSIVE_LOCKS_REQUIRED attribute, in conjunction25//! with the ! operator, to indicate that a mutex should not be held.26const StdMutex& operator!() const { return *this; }27#endif // __clang__29// StdMutex::Guard provides an annotated version of std::lock_guard for us.30class SCOPED_LOCKABLE Guard : public std::lock_guard<StdMutex>31{32public:33explicit Guard(StdMutex& cs) EXCLUSIVE_LOCK_FUNCTION(cs) : std::lock_guard<StdMutex>(cs) {}34~Guard() UNLOCK_FUNCTION() = default;35};37static inline StdMutex& CheckNotHeld(StdMutex& cs) EXCLUSIVE_LOCKS_REQUIRED(!cs) LOCK_RETURNED(cs) { return cs; }38};40// Provide STDLOCK(..) wrapper around StdMutex::Guard that checks the lock is not already held41#define STDLOCK(cs) StdMutex::Guard BITCOIN_UNIQUE_NAME(criticalblock){StdMutex::CheckNotHeld(cs)}43#endif // BITCOIN_UTIL_STDMUTEX_H