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 // TODO(mbonadei): Remove after the backward compatibility period.
40 #include "absl/base/internal/thread_annotations.h" // IWYU pragma: export
41
42 // ABSL_GUARDED_BY()
43 //
44 // Documents if a shared field or global variable needs to be protected by a
45 // mutex. ABSL_GUARDED_BY() allows the user to specify a particular mutex that
46 // should be held when accessing the annotated variable.
47 //
48 // Although this annotation (and ABSL_PT_GUARDED_BY, below) cannot be applied to
49 // local variables, a local variable and its associated mutex can often be
50 // combined into a small class or struct, thereby allowing the annotation.
51 //
52 // Example:
53 //
54 // class Foo {
55 // Mutex mu_;
56 // int p1_ ABSL_GUARDED_BY(mu_);
57 // ...
58 // };
59 #if ABSL_HAVE_ATTRIBUTE(guarded_by)
60 #define ABSL_GUARDED_BY(x) __attribute__((guarded_by(x)))
61 #else
62 #define ABSL_GUARDED_BY(x)
63 #endif
64
65 // ABSL_PT_GUARDED_BY()
66 //
67 // Documents if the memory location pointed to by a pointer should be guarded
68 // by a mutex when dereferencing the pointer.
69 //
70 // Example:
71 // class Foo {
72 // Mutex mu_;
73 // int *p1_ ABSL_PT_GUARDED_BY(mu_);
74 // ...
75 // };
76 //
77 // Note that a pointer variable to a shared memory location could itself be a
78 // shared variable.
79 //
80 // Example:
81 //
82 // // `q_`, guarded by `mu1_`, points to a shared memory location that is
83 // // guarded by `mu2_`:
84 // int *q_ ABSL_GUARDED_BY(mu1_) ABSL_PT_GUARDED_BY(mu2_);
85 #if ABSL_HAVE_ATTRIBUTE(pt_guarded_by)
86 #define ABSL_PT_GUARDED_BY(x) __attribute__((pt_guarded_by(x)))
87 #else
88 #define ABSL_PT_GUARDED_BY(x)
89 #endif
90
91 // ABSL_ACQUIRED_AFTER() / ABSL_ACQUIRED_BEFORE()
92 //
93 // Documents the acquisition order between locks that can be held
94 // simultaneously by a thread. For any two locks that need to be annotated
95 // to establish an acquisition order, only one of them needs the annotation.
96 // (i.e. You don't have to annotate both locks with both ABSL_ACQUIRED_AFTER
97 // and ABSL_ACQUIRED_BEFORE.)
98 //
99 // As with ABSL_GUARDED_BY, this is only applicable to mutexes that are shared
100 // fields or global variables.
101 //
102 // Example:
103 //
104 // Mutex m1_;
105 // Mutex m2_ ABSL_ACQUIRED_AFTER(m1_);
106 #if ABSL_HAVE_ATTRIBUTE(acquired_after)
107 #define ABSL_ACQUIRED_AFTER(...) __attribute__((acquired_after(__VA_ARGS__)))
108 #else
109 #define ABSL_ACQUIRED_AFTER(...)
110 #endif
111
112 #if ABSL_HAVE_ATTRIBUTE(acquired_before)
113 #define ABSL_ACQUIRED_BEFORE(...) __attribute__((acquired_before(__VA_ARGS__)))
114 #else
115 #define ABSL_ACQUIRED_BEFORE(...)
116 #endif
117
118 // ABSL_EXCLUSIVE_LOCKS_REQUIRED() / ABSL_SHARED_LOCKS_REQUIRED()
119 //
120 // Documents a function that expects a mutex to be held prior to entry.
121 // The mutex is expected to be held both on entry to, and exit from, the
122 // function.
123 //
124 // An exclusive lock allows read-write access to the guarded data member(s), and
125 // only one thread can acquire a lock exclusively at any one time. A shared lock
126 // allows read-only access, and any number of threads can acquire a shared lock
127 // concurrently.
128 //
129 // Generally, non-const methods should be annotated with
130 // ABSL_EXCLUSIVE_LOCKS_REQUIRED, while const methods should be annotated with
131 // ABSL_SHARED_LOCKS_REQUIRED.
132 //
133 // Example:
134 //
135 // Mutex mu1, mu2;
136 // int a ABSL_GUARDED_BY(mu1);
137 // int b ABSL_GUARDED_BY(mu2);
138 //
139 // void foo() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu1, mu2) { ... }
140 // void bar() const ABSL_SHARED_LOCKS_REQUIRED(mu1, mu2) { ... }
141 #if ABSL_HAVE_ATTRIBUTE(exclusive_locks_required)
142 #define ABSL_EXCLUSIVE_LOCKS_REQUIRED(...) \
143 __attribute__((exclusive_locks_required(__VA_ARGS__)))
144 #else
145 #define ABSL_EXCLUSIVE_LOCKS_REQUIRED(...)
146 #endif
147
148 #if ABSL_HAVE_ATTRIBUTE(shared_locks_required)
149 #define ABSL_SHARED_LOCKS_REQUIRED(...) \
150 __attribute__((shared_locks_required(__VA_ARGS__)))
151 #else
152 #define ABSL_SHARED_LOCKS_REQUIRED(...)
153 #endif
154
155 // ABSL_LOCKS_EXCLUDED()
156 //
157 // Documents the locks acquired in the body of the function. These locks
158 // cannot be held when calling this function (as Abseil's `Mutex` locks are
159 // non-reentrant).
160 #if ABSL_HAVE_ATTRIBUTE(locks_excluded)
161 #define ABSL_LOCKS_EXCLUDED(...) __attribute__((locks_excluded(__VA_ARGS__)))
162 #else
163 #define ABSL_LOCKS_EXCLUDED(...)
164 #endif
165
166 // ABSL_LOCK_RETURNED()
167 //
168 // Documents a function that returns a mutex without acquiring it. For example,
169 // a public getter method that returns a pointer to a private mutex should
170 // be annotated with ABSL_LOCK_RETURNED.
171 #if ABSL_HAVE_ATTRIBUTE(lock_returned)
172 #define ABSL_LOCK_RETURNED(x) __attribute__((lock_returned(x)))
173 #else
174 #define ABSL_LOCK_RETURNED(x)
175 #endif
176
177 // ABSL_LOCKABLE
178 //
179 // Documents if a class/type is a lockable type (such as the `Mutex` class).
180 #if ABSL_HAVE_ATTRIBUTE(lockable)
181 #define ABSL_LOCKABLE __attribute__((lockable))
182 #else
183 #define ABSL_LOCKABLE
184 #endif
185
186 // ABSL_SCOPED_LOCKABLE
187 //
188 // Documents if a class does RAII locking (such as the `MutexLock` class).
189 // The constructor should use `LOCK_FUNCTION()` to specify the mutex that is
190 // acquired, and the destructor should use `UNLOCK_FUNCTION()` with no
191 // arguments; the analysis will assume that the destructor unlocks whatever the
192 // constructor locked.
193 #if 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 #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 used this function directly, use ABSL_TS_UNCHECKED_READ instead.
321 template <typename T>
ts_unchecked_read(const T & v)322 inline const T& ts_unchecked_read(const T& v) ABSL_NO_THREAD_SAFETY_ANALYSIS {
323 return v;
324 }
325
326 template <typename T>
ts_unchecked_read(T & v)327 inline T& ts_unchecked_read(T& v) ABSL_NO_THREAD_SAFETY_ANALYSIS {
328 return v;
329 }
330
331 } // namespace base_internal
332 ABSL_NAMESPACE_END
333 } // namespace absl
334
335 #endif // ABSL_BASE_THREAD_ANNOTATIONS_H_
336