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