1 // Copyright 2019 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 // WARNING: This is a backwards compatible header and it will be removed after
20 // the migration to prefixed thread annotations is finished; please include
21 // "absl/base/thread_annotations.h".
22 //
23 // This header file contains macro definitions for thread safety annotations
24 // that allow developers to document the locking policies of multi-threaded
25 // code. The annotations can also help program analysis tools to identify
26 // potential thread safety issues.
27 //
28 // These annotations are implemented using compiler attributes. Using the macros
29 // defined here instead of raw attributes allow for portability and future
30 // compatibility.
31 //
32 // When referring to mutexes in the arguments of the attributes, you should
33 // use variable names or more complex expressions (e.g. my_object->mutex_)
34 // that evaluate to a concrete mutex object whenever possible. If the mutex
35 // you want to refer to is not in scope, you may use a member pointer
36 // (e.g. &MyClass::mutex_) to refer to a mutex in some (unknown) object.
37
38 #ifndef ABSL_BASE_INTERNAL_THREAD_ANNOTATIONS_H_
39 #define ABSL_BASE_INTERNAL_THREAD_ANNOTATIONS_H_
40
41 // ABSL_LEGACY_THREAD_ANNOTATIONS is a *temporary* compatibility macro that can
42 // be defined on the compile command-line to restore the legacy spellings of the
43 // thread annotations macros/functions. The macros in this file are available
44 // under ABSL_ prefixed spellings in absl/base/thread_annotations.h. This macro
45 // and the legacy spellings will be removed in the future.
46 #ifdef ABSL_LEGACY_THREAD_ANNOTATIONS
47
48 #if defined(__clang__)
49 #define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
50 #else
51 #define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op
52 #endif
53
54 // GUARDED_BY()
55 //
56 // Documents if a shared field or global variable needs to be protected by a
57 // mutex. GUARDED_BY() allows the user to specify a particular mutex that
58 // should be held when accessing the annotated variable.
59 //
60 // Although this annotation (and PT_GUARDED_BY, below) cannot be applied to
61 // local variables, a local variable and its associated mutex can often be
62 // combined into a small class or struct, thereby allowing the annotation.
63 //
64 // Example:
65 //
66 // class Foo {
67 // Mutex mu_;
68 // int p1_ GUARDED_BY(mu_);
69 // ...
70 // };
71 #define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
72
73 // PT_GUARDED_BY()
74 //
75 // Documents if the memory location pointed to by a pointer should be guarded
76 // by a mutex when dereferencing the pointer.
77 //
78 // Example:
79 // class Foo {
80 // Mutex mu_;
81 // int *p1_ PT_GUARDED_BY(mu_);
82 // ...
83 // };
84 //
85 // Note that a pointer variable to a shared memory location could itself be a
86 // shared variable.
87 //
88 // Example:
89 //
90 // // `q_`, guarded by `mu1_`, points to a shared memory location that is
91 // // guarded by `mu2_`:
92 // int *q_ GUARDED_BY(mu1_) PT_GUARDED_BY(mu2_);
93 #define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x))
94
95 // ACQUIRED_AFTER() / ACQUIRED_BEFORE()
96 //
97 // Documents the acquisition order between locks that can be held
98 // simultaneously by a thread. For any two locks that need to be annotated
99 // to establish an acquisition order, only one of them needs the annotation.
100 // (i.e. You don't have to annotate both locks with both ACQUIRED_AFTER
101 // and ACQUIRED_BEFORE.)
102 //
103 // As with GUARDED_BY, this is only applicable to mutexes that are shared
104 // fields or global variables.
105 //
106 // Example:
107 //
108 // Mutex m1_;
109 // Mutex m2_ ACQUIRED_AFTER(m1_);
110 #define ACQUIRED_AFTER(...) \
111 THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__))
112
113 #define ACQUIRED_BEFORE(...) \
114 THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__))
115
116 // EXCLUSIVE_LOCKS_REQUIRED() / 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 // EXCLUSIVE_LOCKS_REQUIRED, while const methods should be annotated with
129 // SHARED_LOCKS_REQUIRED.
130 //
131 // Example:
132 //
133 // Mutex mu1, mu2;
134 // int a GUARDED_BY(mu1);
135 // int b GUARDED_BY(mu2);
136 //
137 // void foo() EXCLUSIVE_LOCKS_REQUIRED(mu1, mu2) { ... }
138 // void bar() const SHARED_LOCKS_REQUIRED(mu1, mu2) { ... }
139 #define EXCLUSIVE_LOCKS_REQUIRED(...) \
140 THREAD_ANNOTATION_ATTRIBUTE__(exclusive_locks_required(__VA_ARGS__))
141
142 #define SHARED_LOCKS_REQUIRED(...) \
143 THREAD_ANNOTATION_ATTRIBUTE__(shared_locks_required(__VA_ARGS__))
144
145 // LOCKS_EXCLUDED()
146 //
147 // Documents the locks acquired in the body of the function. These locks
148 // cannot be held when calling this function (as Abseil's `Mutex` locks are
149 // non-reentrant).
150 #define LOCKS_EXCLUDED(...) \
151 THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__))
152
153 // LOCK_RETURNED()
154 //
155 // Documents a function that returns a mutex without acquiring it. For example,
156 // a public getter method that returns a pointer to a private mutex should
157 // be annotated with LOCK_RETURNED.
158 #define LOCK_RETURNED(x) \
159 THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
160
161 // LOCKABLE
162 //
163 // Documents if a class/type is a lockable type (such as the `Mutex` class).
164 #define LOCKABLE \
165 THREAD_ANNOTATION_ATTRIBUTE__(lockable)
166
167 // SCOPED_LOCKABLE
168 //
169 // Documents if a class does RAII locking (such as the `MutexLock` class).
170 // The constructor should use `LOCK_FUNCTION()` to specify the mutex that is
171 // acquired, and the destructor should use `UNLOCK_FUNCTION()` with no
172 // arguments; the analysis will assume that the destructor unlocks whatever the
173 // constructor locked.
174 #define SCOPED_LOCKABLE \
175 THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
176
177 // EXCLUSIVE_LOCK_FUNCTION()
178 //
179 // Documents functions that acquire a lock in the body of a function, and do
180 // not release it.
181 #define EXCLUSIVE_LOCK_FUNCTION(...) \
182 THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock_function(__VA_ARGS__))
183
184 // SHARED_LOCK_FUNCTION()
185 //
186 // Documents functions that acquire a shared (reader) lock in the body of a
187 // function, and do not release it.
188 #define SHARED_LOCK_FUNCTION(...) \
189 THREAD_ANNOTATION_ATTRIBUTE__(shared_lock_function(__VA_ARGS__))
190
191 // UNLOCK_FUNCTION()
192 //
193 // Documents functions that expect a lock to be held on entry to the function,
194 // and release it in the body of the function.
195 #define UNLOCK_FUNCTION(...) \
196 THREAD_ANNOTATION_ATTRIBUTE__(unlock_function(__VA_ARGS__))
197
198 // EXCLUSIVE_TRYLOCK_FUNCTION() / SHARED_TRYLOCK_FUNCTION()
199 //
200 // Documents functions that try to acquire a lock, and return success or failure
201 // (or a non-boolean value that can be interpreted as a boolean).
202 // The first argument should be `true` for functions that return `true` on
203 // success, or `false` for functions that return `false` on success. The second
204 // argument specifies the mutex that is locked on success. If unspecified, this
205 // mutex is assumed to be `this`.
206 #define EXCLUSIVE_TRYLOCK_FUNCTION(...) \
207 THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock_function(__VA_ARGS__))
208
209 #define SHARED_TRYLOCK_FUNCTION(...) \
210 THREAD_ANNOTATION_ATTRIBUTE__(shared_trylock_function(__VA_ARGS__))
211
212 // ASSERT_EXCLUSIVE_LOCK() / ASSERT_SHARED_LOCK()
213 //
214 // Documents functions that dynamically check to see if a lock is held, and fail
215 // if it is not held.
216 #define ASSERT_EXCLUSIVE_LOCK(...) \
217 THREAD_ANNOTATION_ATTRIBUTE__(assert_exclusive_lock(__VA_ARGS__))
218
219 #define ASSERT_SHARED_LOCK(...) \
220 THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_lock(__VA_ARGS__))
221
222 // NO_THREAD_SAFETY_ANALYSIS
223 //
224 // Turns off thread safety checking within the body of a particular function.
225 // This annotation is used to mark functions that are known to be correct, but
226 // the locking behavior is more complicated than the analyzer can handle.
227 #define NO_THREAD_SAFETY_ANALYSIS \
228 THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
229
230 //------------------------------------------------------------------------------
231 // Tool-Supplied Annotations
232 //------------------------------------------------------------------------------
233
234 // TS_UNCHECKED should be placed around lock expressions that are not valid
235 // C++ syntax, but which are present for documentation purposes. These
236 // annotations will be ignored by the analysis.
237 #define TS_UNCHECKED(x) ""
238
239 // TS_FIXME is used to mark lock expressions that are not valid C++ syntax.
240 // It is used by automated tools to mark and disable invalid expressions.
241 // The annotation should either be fixed, or changed to TS_UNCHECKED.
242 #define TS_FIXME(x) ""
243
244 // Like NO_THREAD_SAFETY_ANALYSIS, this turns off checking within the body of
245 // a particular function. However, this attribute is used to mark functions
246 // that are incorrect and need to be fixed. It is used by automated tools to
247 // avoid breaking the build when the analysis is updated.
248 // Code owners are expected to eventually fix the routine.
249 #define NO_THREAD_SAFETY_ANALYSIS_FIXME NO_THREAD_SAFETY_ANALYSIS
250
251 // Similar to NO_THREAD_SAFETY_ANALYSIS_FIXME, this macro marks a GUARDED_BY
252 // annotation that needs to be fixed, because it is producing thread safety
253 // warning. It disables the GUARDED_BY.
254 #define GUARDED_BY_FIXME(x)
255
256 // Disables warnings for a single read operation. This can be used to avoid
257 // warnings when it is known that the read is not actually involved in a race,
258 // but the compiler cannot confirm that.
259 #define TS_UNCHECKED_READ(x) thread_safety_analysis::ts_unchecked_read(x)
260
261
262 namespace thread_safety_analysis {
263
264 // Takes a reference to a guarded data member, and returns an unguarded
265 // reference.
266 template <typename T>
ts_unchecked_read(const T & v)267 inline const T& ts_unchecked_read(const T& v) NO_THREAD_SAFETY_ANALYSIS {
268 return v;
269 }
270
271 template <typename T>
ts_unchecked_read(T & v)272 inline T& ts_unchecked_read(T& v) NO_THREAD_SAFETY_ANALYSIS {
273 return v;
274 }
275
276 } // namespace thread_safety_analysis
277
278 #endif // defined(ABSL_LEGACY_THREAD_ANNOTATIONS)
279
280 #endif // ABSL_BASE_INTERNAL_THREAD_ANNOTATIONS_H_
281