Bitcoin Core 31.99.0
P2P Digital Currency
stdmutex.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-present The Bitcoin Core developers
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#ifndef BITCOIN_UTIL_STDMUTEX_H
7#define BITCOIN_UTIL_STDMUTEX_H
8
9// This header declares threading primitives compatible with Clang
10// Thread Safety Analysis and provides appropriate annotation macros.
11#include <threadsafety.h> // IWYU pragma: export
12
13#include <util/macros.h>
14
15#include <mutex>
16
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::mutex
20{
21public:
22#ifdef __clang__
26 const StdMutex& operator!() const { return *this; }
27#endif // __clang__
28
29 // StdMutex::Guard provides an annotated version of std::lock_guard for us.
30 class SCOPED_LOCKABLE Guard : public std::lock_guard<StdMutex>
31 {
32 public:
33 explicit Guard(StdMutex& cs) EXCLUSIVE_LOCK_FUNCTION(cs) : std::lock_guard<StdMutex>(cs) {}
34 ~Guard() UNLOCK_FUNCTION() = default;
35 };
36
37 static inline StdMutex& CheckNotHeld(StdMutex& cs) EXCLUSIVE_LOCKS_REQUIRED(!cs) LOCK_RETURNED(cs) { return cs; }
38};
39
40// Provide STDLOCK(..) wrapper around StdMutex::Guard that checks the lock is not already held
41#define STDLOCK(cs) StdMutex::Guard UNIQUE_NAME(criticalblock){StdMutex::CheckNotHeld(cs)}
42
43using StdLockGuard = StdMutex::Guard; // TODO: remove, provided for backwards compat only
44
45#endif // BITCOIN_UTIL_STDMUTEX_H
static void pool cs
Guard(StdMutex &cs) EXCLUSIVE_LOCK_FUNCTION(cs)
Definition: stdmutex.h:33
~Guard() UNLOCK_FUNCTION()=default
Definition: common.h:29
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
#define EXCLUSIVE_LOCK_FUNCTION(...)
Definition: threadsafety.h:41
#define SCOPED_LOCKABLE
Definition: threadsafety.h:36
#define LOCKABLE
Definition: threadsafety.h:35
#define LOCK_RETURNED(x)
Definition: threadsafety.h:47
#define UNLOCK_FUNCTION(...)
Definition: threadsafety.h:45