Skip to content

Commit 9a482d3

Browse files
author
MarcoFalke
committed
Merge #19249: Add means to handle negative capabilities in the Clang Thread Safety annotations
f8213c0 Add means to handle negative capabilities in thread safety annotations (Hennadii Stepanov) Pull request description: This commit is separated from #19238, and it adds support of [Negative Capabilities](https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#negative) in the Clang Thread Safety Analysis attributes. > Negative requirements are an alternative `EXCLUDES` [`LOCKS_EXCLUDED`] that provide a stronger safety guarantee. A negative requirement uses the `REQUIRES` [`EXCLUSIVE_LOCKS_REQUIRED`] attribute, in conjunction with the ! operator, to indicate that a capability should not be held. Examples of usage: - #19238 (for a class) - https://github.com/hebasto/bitcoin/tree/200610-addrman-tsn (for the whole code base) ACKs for top commit: MarcoFalke: Approach ACK f8213c0 vasild: ACK f8213c0 Tree-SHA512: 86d992826b87579661bd228712ae5ee6acca6f70b885ef7e96458974eac184e4874a525c669607ba6b6c861aa4806409a8792d100e6914c858bcab43d31cfb1b
2 parents 62d863f + f8213c0 commit 9a482d3

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/sync.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ class LOCKABLE AnnotatedMixin : public PARENT
103103
}
104104

105105
using UniqueLock = std::unique_lock<PARENT>;
106+
#ifdef __clang__
107+
//! For negative capabilities in the Clang Thread Safety Analysis.
108+
//! A negative requirement uses the EXCLUSIVE_LOCKS_REQUIRED attribute, in conjunction
109+
//! with the ! operator, to indicate that a mutex should not be held.
110+
const AnnotatedMixin& operator!() const { return *this; }
111+
#endif // __clang__
106112
};
107113

108114
/**

src/threadsafety.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@
6060
// and should only be used when sync.h Mutex/LOCK/etc are not usable.
6161
class LOCKABLE StdMutex : public std::mutex
6262
{
63+
public:
64+
#ifdef __clang__
65+
//! For negative capabilities in the Clang Thread Safety Analysis.
66+
//! A negative requirement uses the EXCLUSIVE_LOCKS_REQUIRED attribute, in conjunction
67+
//! with the ! operator, to indicate that a mutex should not be held.
68+
const StdMutex& operator!() const { return *this; }
69+
#endif // __clang__
6370
};
6471

6572
// StdLockGuard provides an annotated version of std::lock_guard for us,

0 commit comments

Comments
 (0)