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