Coverage Report

Created: 2026-10-28 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/base/thread_annotations.h
Line
Count
Source
1
// Copyright 2017 The Abseil Authors.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
//
15
// -----------------------------------------------------------------------------
16
// File: thread_annotations.h
17
// -----------------------------------------------------------------------------
18
//
19
// This header file contains macro definitions for thread safety annotations
20
// that allow developers to document the locking policies of multi-threaded
21
// code. The annotations can also help program analysis tools to identify
22
// potential thread safety issues.
23
//
24
// These annotations are implemented using compiler attributes. Using the macros
25
// defined here instead of raw attributes allow for portability and future
26
// compatibility.
27
//
28
// When referring to mutexes in the arguments of the attributes, you should
29
// use variable names or more complex expressions (e.g. my_object->mutex_)
30
// that evaluate to a concrete mutex object whenever possible. If the mutex
31
// you want to refer to is not in scope, you may use a member pointer
32
// (e.g. &MyClass::mutex_) to refer to a mutex in some (unknown) object.
33
34
#ifndef ABSL_BASE_THREAD_ANNOTATIONS_H_
35
#define ABSL_BASE_THREAD_ANNOTATIONS_H_
36
37
#include "absl/base/attributes.h"
38
#include "absl/base/config.h"
39
40
// ABSL_GUARDED_BY()
41
//
42
// Documents if a shared field or global variable needs to be protected by a
43
// mutex. ABSL_GUARDED_BY() allows the user to specify a particular mutex that
44
// should be held when accessing the annotated variable.
45
//
46
// Although this annotation (and ABSL_PT_GUARDED_BY, below) cannot be applied to
47
// local variables, a local variable and its associated mutex can often be
48
// combined into a small class or struct, thereby allowing the annotation.
49
//
50
// Example:
51
//
52
//   class Foo {
53
//     Mutex mu_;
54
//     int p1_ ABSL_GUARDED_BY(mu_);
55
//     ...
56
//   };
57
#if ABSL_HAVE_ATTRIBUTE(guarded_by)
58
#define ABSL_GUARDED_BY(x) __attribute__((guarded_by(x)))
59
#else
60
#define ABSL_GUARDED_BY(x)
61
#endif
62
63
// ABSL_PT_GUARDED_BY()
64
//
65
// Documents if the memory location pointed to by a pointer should be guarded
66
// by a mutex when dereferencing the pointer.
67
//
68
// Example:
69
//   class Foo {
70
//     Mutex mu_;
71
//     int *p1_ ABSL_PT_GUARDED_BY(mu_);
72
//     ...
73
//   };
74
//
75
// Note that a pointer variable to a shared memory location could itself be a
76
// shared variable.
77
//
78
// Example:
79
//
80
//   // `q_`, guarded by `mu1_`, points to a shared memory location that is
81
//   // guarded by `mu2_`:
82
//   int *q_ ABSL_GUARDED_BY(mu1_) ABSL_PT_GUARDED_BY(mu2_);
83
#if ABSL_HAVE_ATTRIBUTE(pt_guarded_by)
84
#define ABSL_PT_GUARDED_BY(x) __attribute__((pt_guarded_by(x)))
85
#else
86
#define ABSL_PT_GUARDED_BY(x)
87
#endif
88
89
// ABSL_ACQUIRED_AFTER() / ABSL_ACQUIRED_BEFORE()
90
//
91
// Documents the acquisition order between locks that can be held
92
// simultaneously by a thread. For any two locks that need to be annotated
93
// to establish an acquisition order, only one of them needs the annotation.
94
// (i.e. You don't have to annotate both locks with both ABSL_ACQUIRED_AFTER
95
// and ABSL_ACQUIRED_BEFORE.)
96
//
97
// As with ABSL_GUARDED_BY, this is only applicable to mutexes that are shared
98
// fields or global variables.
99
//
100
// Example:
101
//
102
//   Mutex m1_;
103
//   Mutex m2_ ABSL_ACQUIRED_AFTER(m1_);
104
#if ABSL_HAVE_ATTRIBUTE(acquired_after)
105
#define ABSL_ACQUIRED_AFTER(...) __attribute__((acquired_after(__VA_ARGS__)))
106
#else
107
#define ABSL_ACQUIRED_AFTER(...)
108
#endif
109
110
#if ABSL_HAVE_ATTRIBUTE(acquired_before)
111
#define ABSL_ACQUIRED_BEFORE(...) __attribute__((acquired_before(__VA_ARGS__)))
112
#else
113
#define ABSL_ACQUIRED_BEFORE(...)
114
#endif
115
116
// ABSL_EXCLUSIVE_LOCKS_REQUIRED() / ABSL_SHARED_LOCKS_REQUIRED()
117
//
118
// Documents a function that expects a mutex to be held prior to entry.
119
// The mutex is expected to be held both on entry to, and exit from, the
120
// function.
121
//
122
// An exclusive lock allows read-write access to the guarded data member(s), and
123
// only one thread can acquire a lock exclusively at any one time. A shared lock
124
// allows read-only access, and any number of threads can acquire a shared lock
125
// concurrently.
126
//
127
// Generally, non-const methods should be annotated with
128
// ABSL_EXCLUSIVE_LOCKS_REQUIRED, while const methods should be annotated with
129
// ABSL_SHARED_LOCKS_REQUIRED.
130
//
131
// Example:
132
//
133
//   Mutex mu1, mu2;
134
//   int a ABSL_GUARDED_BY(mu1);
135
//   int b ABSL_GUARDED_BY(mu2);
136
//
137
//   void foo() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu1, mu2) { ... }
138
//   void bar() const ABSL_SHARED_LOCKS_REQUIRED(mu1, mu2) { ... }
139
#if ABSL_HAVE_ATTRIBUTE(exclusive_locks_required)
140
#define ABSL_EXCLUSIVE_LOCKS_REQUIRED(...) \
141
  __attribute__((exclusive_locks_required(__VA_ARGS__)))
142
#else
143
#define ABSL_EXCLUSIVE_LOCKS_REQUIRED(...)
144
#endif
145
146
#if ABSL_HAVE_ATTRIBUTE(shared_locks_required)
147
#define ABSL_SHARED_LOCKS_REQUIRED(...) \
148
  __attribute__((shared_locks_required(__VA_ARGS__)))
149
#else
150
#define ABSL_SHARED_LOCKS_REQUIRED(...)
151
#endif
152
153
// ABSL_LOCKS_EXCLUDED()
154
//
155
// Documents the locks that cannot be held by callers of this function, as they
156
// might be acquired by this function (Abseil's `Mutex` locks are
157
// non-reentrant).
158
#if ABSL_HAVE_ATTRIBUTE(locks_excluded)
159
#define ABSL_LOCKS_EXCLUDED(...) __attribute__((locks_excluded(__VA_ARGS__)))
160
#else
161
#define ABSL_LOCKS_EXCLUDED(...)
162
#endif
163
164
// ABSL_LOCK_RETURNED()
165
//
166
// Documents a function that returns a mutex without acquiring it.  For example,
167
// a public getter method that returns a pointer to a private mutex should
168
// be annotated with ABSL_LOCK_RETURNED.
169
#if ABSL_HAVE_ATTRIBUTE(lock_returned)
170
#define ABSL_LOCK_RETURNED(x) __attribute__((lock_returned(x)))
171
#else
172
#define ABSL_LOCK_RETURNED(x)
173
#endif
174
175
// ABSL_LOCKABLE
176
//
177
// Documents if a class/type is a lockable type (such as the `Mutex` class).
178
#if ABSL_HAVE_ATTRIBUTE(lockable)
179
#define ABSL_LOCKABLE __attribute__((lockable))
180
#else
181
#define ABSL_LOCKABLE
182
#endif
183
184
// ABSL_SCOPED_LOCKABLE
185
//
186
// Documents if a class does RAII locking (such as the `MutexLock` class).
187
// The constructor should use `LOCK_FUNCTION()` to specify the mutex that is
188
// acquired, and the destructor should use `UNLOCK_FUNCTION()` with no
189
// arguments; the analysis will assume that the destructor unlocks whatever the
190
// constructor locked.
191
#if ABSL_HAVE_CPP_ATTRIBUTE(clang::scoped_lockable)
192
#define ABSL_SCOPED_LOCKABLE [[clang::scoped_lockable]]
193
#elif ABSL_HAVE_ATTRIBUTE(scoped_lockable)
194
#define ABSL_SCOPED_LOCKABLE __attribute__((scoped_lockable))
195
#else
196
#define ABSL_SCOPED_LOCKABLE
197
#endif
198
199
// ABSL_EXCLUSIVE_LOCK_FUNCTION()
200
//
201
// Documents functions that acquire a lock in the body of a function, and do
202
// not release it.
203
#if ABSL_HAVE_ATTRIBUTE(exclusive_lock_function)
204
#define ABSL_EXCLUSIVE_LOCK_FUNCTION(...) \
205
  __attribute__((exclusive_lock_function(__VA_ARGS__)))
206
#else
207
#define ABSL_EXCLUSIVE_LOCK_FUNCTION(...)
208
#endif
209
210
// ABSL_SHARED_LOCK_FUNCTION()
211
//
212
// Documents functions that acquire a shared (reader) lock in the body of a
213
// function, and do not release it.
214
#if ABSL_HAVE_ATTRIBUTE(shared_lock_function)
215
#define ABSL_SHARED_LOCK_FUNCTION(...) \
216
  __attribute__((shared_lock_function(__VA_ARGS__)))
217
#else
218
#define ABSL_SHARED_LOCK_FUNCTION(...)
219
#endif
220
221
// ABSL_UNLOCK_FUNCTION()
222
//
223
// Documents functions that expect a lock to be held on entry to the function,
224
// and release it in the body of the function.
225
#if ABSL_HAVE_ATTRIBUTE(unlock_function)
226
#define ABSL_UNLOCK_FUNCTION(...) __attribute__((unlock_function(__VA_ARGS__)))
227
#else
228
#define ABSL_UNLOCK_FUNCTION(...)
229
#endif
230
231
// ABSL_EXCLUSIVE_TRYLOCK_FUNCTION() / ABSL_SHARED_TRYLOCK_FUNCTION()
232
//
233
// Documents functions that try to acquire a lock, and return success or failure
234
// (or a non-boolean value that can be interpreted as a boolean).
235
// The first argument should be `true` for functions that return `true` on
236
// success, or `false` for functions that return `false` on success. The second
237
// argument specifies the mutex that is locked on success. If unspecified, this
238
// mutex is assumed to be `this`.
239
#if ABSL_HAVE_ATTRIBUTE(exclusive_trylock_function)
240
#define ABSL_EXCLUSIVE_TRYLOCK_FUNCTION(...) \
241
  __attribute__((exclusive_trylock_function(__VA_ARGS__)))
242
#else
243
#define ABSL_EXCLUSIVE_TRYLOCK_FUNCTION(...)
244
#endif
245
246
#if ABSL_HAVE_ATTRIBUTE(shared_trylock_function)
247
#define ABSL_SHARED_TRYLOCK_FUNCTION(...) \
248
  __attribute__((shared_trylock_function(__VA_ARGS__)))
249
#else
250
#define ABSL_SHARED_TRYLOCK_FUNCTION(...)
251
#endif
252
253
// ABSL_ASSERT_EXCLUSIVE_LOCK() / ABSL_ASSERT_SHARED_LOCK()
254
//
255
// Documents functions that dynamically check to see if a lock is held, and fail
256
// if it is not held.
257
#if ABSL_HAVE_ATTRIBUTE(assert_exclusive_lock)
258
#define ABSL_ASSERT_EXCLUSIVE_LOCK(...) \
259
  __attribute__((assert_exclusive_lock(__VA_ARGS__)))
260
#else
261
#define ABSL_ASSERT_EXCLUSIVE_LOCK(...)
262
#endif
263
264
#if ABSL_HAVE_ATTRIBUTE(assert_shared_lock)
265
#define ABSL_ASSERT_SHARED_LOCK(...) \
266
  __attribute__((assert_shared_lock(__VA_ARGS__)))
267
#else
268
#define ABSL_ASSERT_SHARED_LOCK(...)
269
#endif
270
271
// ABSL_NO_THREAD_SAFETY_ANALYSIS
272
//
273
// Turns off thread safety checking within the body of a particular function.
274
// This annotation is used to mark functions that are known to be correct, but
275
// the locking behavior is more complicated than the analyzer can handle.
276
#if ABSL_HAVE_ATTRIBUTE(no_thread_safety_analysis)
277
#define ABSL_NO_THREAD_SAFETY_ANALYSIS \
278
  __attribute__((no_thread_safety_analysis))
279
#else
280
#define ABSL_NO_THREAD_SAFETY_ANALYSIS
281
#endif
282
283
//------------------------------------------------------------------------------
284
// Tool-Supplied Annotations
285
//------------------------------------------------------------------------------
286
287
// ABSL_TS_UNCHECKED should be placed around lock expressions that are not valid
288
// C++ syntax, but which are present for documentation purposes.  These
289
// annotations will be ignored by the analysis.
290
#define ABSL_TS_UNCHECKED(x) ""
291
292
// ABSL_TS_FIXME is used to mark lock expressions that are not valid C++ syntax.
293
// It is used by automated tools to mark and disable invalid expressions.
294
// The annotation should either be fixed, or changed to ABSL_TS_UNCHECKED.
295
#define ABSL_TS_FIXME(x) ""
296
297
// Like ABSL_NO_THREAD_SAFETY_ANALYSIS, this turns off checking within the body
298
// of a particular function.  However, this attribute is used to mark functions
299
// that are incorrect and need to be fixed.  It is used by automated tools to
300
// avoid breaking the build when the analysis is updated.
301
// Code owners are expected to eventually fix the routine.
302
#define ABSL_NO_THREAD_SAFETY_ANALYSIS_FIXME ABSL_NO_THREAD_SAFETY_ANALYSIS
303
304
// Similar to ABSL_NO_THREAD_SAFETY_ANALYSIS_FIXME, this macro marks a
305
// ABSL_GUARDED_BY annotation that needs to be fixed, because it is producing
306
// thread safety warning. It disables the ABSL_GUARDED_BY.
307
#define ABSL_GUARDED_BY_FIXME(x)
308
309
// Disables warnings for a single read operation.  This can be used to avoid
310
// warnings when it is known that the read is not actually involved in a race,
311
// but the compiler cannot confirm that.
312
0
#define ABSL_TS_UNCHECKED_READ(x) absl::base_internal::ts_unchecked_read(x)
313
314
namespace absl {
315
ABSL_NAMESPACE_BEGIN
316
namespace base_internal {
317
318
// Takes a reference to a guarded data member, and returns an unguarded
319
// reference.
320
// Do not use this function directly, use ABSL_TS_UNCHECKED_READ instead.
321
template <typename T>
322
inline const T& ts_unchecked_read(const T& v) ABSL_NO_THREAD_SAFETY_ANALYSIS {
323
  return v;
324
}
325
326
template <typename T>
327
0
inline T& ts_unchecked_read(T& v) ABSL_NO_THREAD_SAFETY_ANALYSIS {
328
0
  return v;
329
0
}
330
331
}  // namespace base_internal
332
ABSL_NAMESPACE_END
333
}  // namespace absl
334
335
#endif  // ABSL_BASE_THREAD_ANNOTATIONS_H_