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/config.h"
38 // TODO(mbonadei): Remove after the backward compatibility period.
39 #include "absl/base/internal/thread_annotations.h"  // IWYU pragma: export
40 
41 #if defined(__clang__)
42 #define ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(x) __attribute__((x))
43 #else
44 #define ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(x)  // no-op
45 #endif
46 
47 // ABSL_GUARDED_BY()
48 //
49 // Documents if a shared field or global variable needs to be protected by a
50 // mutex. ABSL_GUARDED_BY() allows the user to specify a particular mutex that
51 // should be held when accessing the annotated variable.
52 //
53 // Although this annotation (and ABSL_PT_GUARDED_BY, below) cannot be applied to
54 // local variables, a local variable and its associated mutex can often be
55 // combined into a small class or struct, thereby allowing the annotation.
56 //
57 // Example:
58 //
59 //   class Foo {
60 //     Mutex mu_;
61 //     int p1_ ABSL_GUARDED_BY(mu_);
62 //     ...
63 //   };
64 #define ABSL_GUARDED_BY(x) \
65   ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(guarded_by(x))
66 
67 // ABSL_PT_GUARDED_BY()
68 //
69 // Documents if the memory location pointed to by a pointer should be guarded
70 // by a mutex when dereferencing the pointer.
71 //
72 // Example:
73 //   class Foo {
74 //     Mutex mu_;
75 //     int *p1_ ABSL_PT_GUARDED_BY(mu_);
76 //     ...
77 //   };
78 //
79 // Note that a pointer variable to a shared memory location could itself be a
80 // shared variable.
81 //
82 // Example:
83 //
84 //   // `q_`, guarded by `mu1_`, points to a shared memory location that is
85 //   // guarded by `mu2_`:
86 //   int *q_ ABSL_GUARDED_BY(mu1_) ABSL_PT_GUARDED_BY(mu2_);
87 #define ABSL_PT_GUARDED_BY(x) \
88   ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(pt_guarded_by(x))
89 
90 // ABSL_ACQUIRED_AFTER() / ABSL_ACQUIRED_BEFORE()
91 //
92 // Documents the acquisition order between locks that can be held
93 // simultaneously by a thread. For any two locks that need to be annotated
94 // to establish an acquisition order, only one of them needs the annotation.
95 // (i.e. You don't have to annotate both locks with both ABSL_ACQUIRED_AFTER
96 // and ABSL_ACQUIRED_BEFORE.)
97 //
98 // As with ABSL_GUARDED_BY, this is only applicable to mutexes that are shared
99 // fields or global variables.
100 //
101 // Example:
102 //
103 //   Mutex m1_;
104 //   Mutex m2_ ABSL_ACQUIRED_AFTER(m1_);
105 #define ABSL_ACQUIRED_AFTER(...) \
106   ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(acquired_after(__VA_ARGS__))
107 
108 #define ABSL_ACQUIRED_BEFORE(...) \
109   ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(acquired_before(__VA_ARGS__))
110 
111 // ABSL_EXCLUSIVE_LOCKS_REQUIRED() / ABSL_SHARED_LOCKS_REQUIRED()
112 //
113 // Documents a function that expects a mutex to be held prior to entry.
114 // The mutex is expected to be held both on entry to, and exit from, the
115 // function.
116 //
117 // An exclusive lock allows read-write access to the guarded data member(s), and
118 // only one thread can acquire a lock exclusively at any one time. A shared lock
119 // allows read-only access, and any number of threads can acquire a shared lock
120 // concurrently.
121 //
122 // Generally, non-const methods should be annotated with
123 // ABSL_EXCLUSIVE_LOCKS_REQUIRED, while const methods should be annotated with
124 // ABSL_SHARED_LOCKS_REQUIRED.
125 //
126 // Example:
127 //
128 //   Mutex mu1, mu2;
129 //   int a ABSL_GUARDED_BY(mu1);
130 //   int b ABSL_GUARDED_BY(mu2);
131 //
132 //   void foo() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu1, mu2) { ... }
133 //   void bar() const ABSL_SHARED_LOCKS_REQUIRED(mu1, mu2) { ... }
134 #define ABSL_EXCLUSIVE_LOCKS_REQUIRED(...)   \
135   ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE( \
136       exclusive_locks_required(__VA_ARGS__))
137 
138 #define ABSL_SHARED_LOCKS_REQUIRED(...) \
139   ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(shared_locks_required(__VA_ARGS__))
140 
141 // ABSL_LOCKS_EXCLUDED()
142 //
143 // Documents the locks acquired in the body of the function. These locks
144 // cannot be held when calling this function (as Abseil's `Mutex` locks are
145 // non-reentrant).
146 #define ABSL_LOCKS_EXCLUDED(...) \
147   ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(locks_excluded(__VA_ARGS__))
148 
149 // ABSL_LOCK_RETURNED()
150 //
151 // Documents a function that returns a mutex without acquiring it.  For example,
152 // a public getter method that returns a pointer to a private mutex should
153 // be annotated with ABSL_LOCK_RETURNED.
154 #define ABSL_LOCK_RETURNED(x) \
155   ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(lock_returned(x))
156 
157 // ABSL_LOCKABLE
158 //
159 // Documents if a class/type is a lockable type (such as the `Mutex` class).
160 #define ABSL_LOCKABLE ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(lockable)
161 
162 // ABSL_SCOPED_LOCKABLE
163 //
164 // Documents if a class does RAII locking (such as the `MutexLock` class).
165 // The constructor should use `LOCK_FUNCTION()` to specify the mutex that is
166 // acquired, and the destructor should use `UNLOCK_FUNCTION()` with no
167 // arguments; the analysis will assume that the destructor unlocks whatever the
168 // constructor locked.
169 #define ABSL_SCOPED_LOCKABLE \
170   ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(scoped_lockable)
171 
172 // ABSL_EXCLUSIVE_LOCK_FUNCTION()
173 //
174 // Documents functions that acquire a lock in the body of a function, and do
175 // not release it.
176 #define ABSL_EXCLUSIVE_LOCK_FUNCTION(...)    \
177   ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE( \
178       exclusive_lock_function(__VA_ARGS__))
179 
180 // ABSL_SHARED_LOCK_FUNCTION()
181 //
182 // Documents functions that acquire a shared (reader) lock in the body of a
183 // function, and do not release it.
184 #define ABSL_SHARED_LOCK_FUNCTION(...) \
185   ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(shared_lock_function(__VA_ARGS__))
186 
187 // ABSL_UNLOCK_FUNCTION()
188 //
189 // Documents functions that expect a lock to be held on entry to the function,
190 // and release it in the body of the function.
191 #define ABSL_UNLOCK_FUNCTION(...) \
192   ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(unlock_function(__VA_ARGS__))
193 
194 // ABSL_EXCLUSIVE_TRYLOCK_FUNCTION() / ABSL_SHARED_TRYLOCK_FUNCTION()
195 //
196 // Documents functions that try to acquire a lock, and return success or failure
197 // (or a non-boolean value that can be interpreted as a boolean).
198 // The first argument should be `true` for functions that return `true` on
199 // success, or `false` for functions that return `false` on success. The second
200 // argument specifies the mutex that is locked on success. If unspecified, this
201 // mutex is assumed to be `this`.
202 #define ABSL_EXCLUSIVE_TRYLOCK_FUNCTION(...) \
203   ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE( \
204       exclusive_trylock_function(__VA_ARGS__))
205 
206 #define ABSL_SHARED_TRYLOCK_FUNCTION(...)    \
207   ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE( \
208       shared_trylock_function(__VA_ARGS__))
209 
210 // ABSL_ASSERT_EXCLUSIVE_LOCK() / ABSL_ASSERT_SHARED_LOCK()
211 //
212 // Documents functions that dynamically check to see if a lock is held, and fail
213 // if it is not held.
214 #define ABSL_ASSERT_EXCLUSIVE_LOCK(...) \
215   ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(assert_exclusive_lock(__VA_ARGS__))
216 
217 #define ABSL_ASSERT_SHARED_LOCK(...) \
218   ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(assert_shared_lock(__VA_ARGS__))
219 
220 // ABSL_NO_THREAD_SAFETY_ANALYSIS
221 //
222 // Turns off thread safety checking within the body of a particular function.
223 // This annotation is used to mark functions that are known to be correct, but
224 // the locking behavior is more complicated than the analyzer can handle.
225 #define ABSL_NO_THREAD_SAFETY_ANALYSIS \
226   ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(no_thread_safety_analysis)
227 
228 //------------------------------------------------------------------------------
229 // Tool-Supplied Annotations
230 //------------------------------------------------------------------------------
231 
232 // ABSL_TS_UNCHECKED should be placed around lock expressions that are not valid
233 // C++ syntax, but which are present for documentation purposes.  These
234 // annotations will be ignored by the analysis.
235 #define ABSL_TS_UNCHECKED(x) ""
236 
237 // ABSL_TS_FIXME is used to mark lock expressions that are not valid C++ syntax.
238 // It is used by automated tools to mark and disable invalid expressions.
239 // The annotation should either be fixed, or changed to ABSL_TS_UNCHECKED.
240 #define ABSL_TS_FIXME(x) ""
241 
242 // Like ABSL_NO_THREAD_SAFETY_ANALYSIS, this turns off checking within the body
243 // of a particular function.  However, this attribute is used to mark functions
244 // that are incorrect and need to be fixed.  It is used by automated tools to
245 // avoid breaking the build when the analysis is updated.
246 // Code owners are expected to eventually fix the routine.
247 #define ABSL_NO_THREAD_SAFETY_ANALYSIS_FIXME ABSL_NO_THREAD_SAFETY_ANALYSIS
248 
249 // Similar to ABSL_NO_THREAD_SAFETY_ANALYSIS_FIXME, this macro marks a
250 // ABSL_GUARDED_BY annotation that needs to be fixed, because it is producing
251 // thread safety warning. It disables the ABSL_GUARDED_BY.
252 #define ABSL_GUARDED_BY_FIXME(x)
253 
254 // Disables warnings for a single read operation.  This can be used to avoid
255 // warnings when it is known that the read is not actually involved in a race,
256 // but the compiler cannot confirm that.
257 #define ABSL_TS_UNCHECKED_READ(x) absl::base_internal::ts_unchecked_read(x)
258 
259 namespace absl {
260 ABSL_NAMESPACE_BEGIN
261 namespace base_internal {
262 
263 // Takes a reference to a guarded data member, and returns an unguarded
264 // reference.
265 // Do not used this function directly, use ABSL_TS_UNCHECKED_READ instead.
266 template <typename T>
ts_unchecked_read(const T & v)267 inline const T& ts_unchecked_read(const T& v) ABSL_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) ABSL_NO_THREAD_SAFETY_ANALYSIS {
273   return v;
274 }
275 
276 }  // namespace base_internal
277 ABSL_NAMESPACE_END
278 }  // namespace absl
279 
280 #endif  // ABSL_BASE_THREAD_ANNOTATIONS_H_
281