Files
bitcoin/src/util/stdmutex.h
Hennadii Stepanov fba713a28c scripted-diff: Rename UNIQUE_NAME to BITCOIN_UNIQUE_NAME
The `nb30.h` Windows header defines `UNIQUE_NAME` as a macro.

This introduces a fragile dependency on header inclusion order: if
Windows headers happen to be included before `UNIQUE_NAME` is used, the
preprocessor expands it into a numeric literal, causing syntax errors.

Rename the macro to `BITCOIN_UNIQUE_NAME` to remove this fragility and
avoid the collision entirely.

-BEGIN VERIFY SCRIPT-
sed -i 's/\<UNIQUE_NAME\>/BITCOIN_UNIQUE_NAME/g' $(git grep -l 'UNIQUE_NAME' ./src/)
-END VERIFY SCRIPT-
2026-06-08 19:06:33 +01:00

44 lines
1.7 KiB
C++

// 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
// This header declares threading primitives compatible with Clang
// Thread Safety Analysis and provides appropriate annotation macros.
#include <threadsafety.h> // IWYU pragma: export
#include <util/macros.h>
#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__
// StdMutex::Guard provides an annotated version of std::lock_guard for us.
class SCOPED_LOCKABLE Guard : public std::lock_guard<StdMutex>
{
public:
explicit Guard(StdMutex& cs) EXCLUSIVE_LOCK_FUNCTION(cs) : std::lock_guard<StdMutex>(cs) {}
~Guard() UNLOCK_FUNCTION() = default;
};
static inline StdMutex& CheckNotHeld(StdMutex& cs) EXCLUSIVE_LOCKS_REQUIRED(!cs) LOCK_RETURNED(cs) { return cs; }
};
// Provide STDLOCK(..) wrapper around StdMutex::Guard that checks the lock is not already held
#define STDLOCK(cs) StdMutex::Guard BITCOIN_UNIQUE_NAME(criticalblock){StdMutex::CheckNotHeld(cs)}
#endif // BITCOIN_UTIL_STDMUTEX_H