2012-11-11 02:21:07 +01:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2016-01-27 12:05:25 +01:00
|
|
|
// Copyright (c) 2009-2014 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-11-11 02:21:07 +01:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2012-11-11 02:21:07 +01:00
|
|
|
#ifndef BITCOIN_THREADSAFETY_H
|
|
|
|
#define BITCOIN_THREADSAFETY_H
|
|
|
|
|
2021-06-09 14:06:31 +02:00
|
|
|
#include <mutex>
|
|
|
|
|
2012-11-11 02:21:07 +01:00
|
|
|
#ifdef __clang__
|
|
|
|
// TL;DR Add GUARDED_BY(mutex) to member variables. The others are
|
|
|
|
// rarely necessary. Ex: int nFoo GUARDED_BY(cs_foo);
|
|
|
|
//
|
2018-09-04 12:29:13 +02:00
|
|
|
// See https://clang.llvm.org/docs/ThreadSafetyAnalysis.html
|
2012-11-11 02:21:07 +01:00
|
|
|
// for documentation. The clang compiler can do advanced static analysis
|
|
|
|
// of locking when given the -Wthread-safety option.
|
2014-09-19 19:21:46 +02:00
|
|
|
#define LOCKABLE __attribute__((lockable))
|
|
|
|
#define SCOPED_LOCKABLE __attribute__((scoped_lockable))
|
|
|
|
#define GUARDED_BY(x) __attribute__((guarded_by(x)))
|
|
|
|
#define GUARDED_VAR __attribute__((guarded_var))
|
|
|
|
#define PT_GUARDED_BY(x) __attribute__((pt_guarded_by(x)))
|
|
|
|
#define PT_GUARDED_VAR __attribute__((pt_guarded_var))
|
|
|
|
#define ACQUIRED_AFTER(...) __attribute__((acquired_after(__VA_ARGS__)))
|
|
|
|
#define ACQUIRED_BEFORE(...) __attribute__((acquired_before(__VA_ARGS__)))
|
|
|
|
#define EXCLUSIVE_LOCK_FUNCTION(...) __attribute__((exclusive_lock_function(__VA_ARGS__)))
|
|
|
|
#define SHARED_LOCK_FUNCTION(...) __attribute__((shared_lock_function(__VA_ARGS__)))
|
|
|
|
#define EXCLUSIVE_TRYLOCK_FUNCTION(...) __attribute__((exclusive_trylock_function(__VA_ARGS__)))
|
|
|
|
#define SHARED_TRYLOCK_FUNCTION(...) __attribute__((shared_trylock_function(__VA_ARGS__)))
|
|
|
|
#define UNLOCK_FUNCTION(...) __attribute__((unlock_function(__VA_ARGS__)))
|
|
|
|
#define LOCK_RETURNED(x) __attribute__((lock_returned(x)))
|
|
|
|
#define LOCKS_EXCLUDED(...) __attribute__((locks_excluded(__VA_ARGS__)))
|
|
|
|
#define EXCLUSIVE_LOCKS_REQUIRED(...) __attribute__((exclusive_locks_required(__VA_ARGS__)))
|
|
|
|
#define SHARED_LOCKS_REQUIRED(...) __attribute__((shared_locks_required(__VA_ARGS__)))
|
|
|
|
#define NO_THREAD_SAFETY_ANALYSIS __attribute__((no_thread_safety_analysis))
|
2018-07-26 17:19:07 +02:00
|
|
|
#define ASSERT_EXCLUSIVE_LOCK(...) __attribute((assert_exclusive_lock(__VA_ARGS__)))
|
2012-11-11 02:21:07 +01:00
|
|
|
#else
|
|
|
|
#define LOCKABLE
|
|
|
|
#define SCOPED_LOCKABLE
|
|
|
|
#define GUARDED_BY(x)
|
|
|
|
#define GUARDED_VAR
|
|
|
|
#define PT_GUARDED_BY(x)
|
|
|
|
#define PT_GUARDED_VAR
|
|
|
|
#define ACQUIRED_AFTER(...)
|
|
|
|
#define ACQUIRED_BEFORE(...)
|
|
|
|
#define EXCLUSIVE_LOCK_FUNCTION(...)
|
|
|
|
#define SHARED_LOCK_FUNCTION(...)
|
|
|
|
#define EXCLUSIVE_TRYLOCK_FUNCTION(...)
|
|
|
|
#define SHARED_TRYLOCK_FUNCTION(...)
|
|
|
|
#define UNLOCK_FUNCTION(...)
|
|
|
|
#define LOCK_RETURNED(x)
|
|
|
|
#define LOCKS_EXCLUDED(...)
|
|
|
|
#define EXCLUSIVE_LOCKS_REQUIRED(...)
|
|
|
|
#define SHARED_LOCKS_REQUIRED(...)
|
|
|
|
#define NO_THREAD_SAFETY_ANALYSIS
|
2018-07-26 17:19:07 +02:00
|
|
|
#define ASSERT_EXCLUSIVE_LOCK(...)
|
2014-09-19 19:21:46 +02:00
|
|
|
#endif // __GNUC__
|
2014-08-28 22:21:03 +02:00
|
|
|
|
2021-06-09 14:02:42 +02:00
|
|
|
// 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
|
|
|
|
{
|
2020-06-09 15:50:45 +02:00
|
|
|
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__
|
2021-06-09 14:02:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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>
|
2021-06-09 14:06:31 +02:00
|
|
|
{
|
|
|
|
public:
|
2021-06-09 14:02:42 +02:00
|
|
|
explicit StdLockGuard(StdMutex& cs) EXCLUSIVE_LOCK_FUNCTION(cs) : std::lock_guard<StdMutex>(cs) {}
|
|
|
|
~StdLockGuard() UNLOCK_FUNCTION() {}
|
2021-06-09 14:06:31 +02:00
|
|
|
};
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
#endif // BITCOIN_THREADSAFETY_H
|