xref: /aosp_15_r20/external/abseil-cpp/absl/base/thread_annotations.h (revision 9356374a3709195abf420251b3e825997ff56c0f)
1*9356374aSAndroid Build Coastguard Worker // Copyright 2017 The Abseil Authors.
2*9356374aSAndroid Build Coastguard Worker //
3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*9356374aSAndroid Build Coastguard Worker //
7*9356374aSAndroid Build Coastguard Worker //      https://www.apache.org/licenses/LICENSE-2.0
8*9356374aSAndroid Build Coastguard Worker //
9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*9356374aSAndroid Build Coastguard Worker // limitations under the License.
14*9356374aSAndroid Build Coastguard Worker //
15*9356374aSAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
16*9356374aSAndroid Build Coastguard Worker // File: thread_annotations.h
17*9356374aSAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
18*9356374aSAndroid Build Coastguard Worker //
19*9356374aSAndroid Build Coastguard Worker // This header file contains macro definitions for thread safety annotations
20*9356374aSAndroid Build Coastguard Worker // that allow developers to document the locking policies of multi-threaded
21*9356374aSAndroid Build Coastguard Worker // code. The annotations can also help program analysis tools to identify
22*9356374aSAndroid Build Coastguard Worker // potential thread safety issues.
23*9356374aSAndroid Build Coastguard Worker //
24*9356374aSAndroid Build Coastguard Worker // These annotations are implemented using compiler attributes. Using the macros
25*9356374aSAndroid Build Coastguard Worker // defined here instead of raw attributes allow for portability and future
26*9356374aSAndroid Build Coastguard Worker // compatibility.
27*9356374aSAndroid Build Coastguard Worker //
28*9356374aSAndroid Build Coastguard Worker // When referring to mutexes in the arguments of the attributes, you should
29*9356374aSAndroid Build Coastguard Worker // use variable names or more complex expressions (e.g. my_object->mutex_)
30*9356374aSAndroid Build Coastguard Worker // that evaluate to a concrete mutex object whenever possible. If the mutex
31*9356374aSAndroid Build Coastguard Worker // you want to refer to is not in scope, you may use a member pointer
32*9356374aSAndroid Build Coastguard Worker // (e.g. &MyClass::mutex_) to refer to a mutex in some (unknown) object.
33*9356374aSAndroid Build Coastguard Worker 
34*9356374aSAndroid Build Coastguard Worker #ifndef ABSL_BASE_THREAD_ANNOTATIONS_H_
35*9356374aSAndroid Build Coastguard Worker #define ABSL_BASE_THREAD_ANNOTATIONS_H_
36*9356374aSAndroid Build Coastguard Worker 
37*9356374aSAndroid Build Coastguard Worker #include "absl/base/attributes.h"
38*9356374aSAndroid Build Coastguard Worker #include "absl/base/config.h"
39*9356374aSAndroid Build Coastguard Worker 
40*9356374aSAndroid Build Coastguard Worker // ABSL_GUARDED_BY()
41*9356374aSAndroid Build Coastguard Worker //
42*9356374aSAndroid Build Coastguard Worker // Documents if a shared field or global variable needs to be protected by a
43*9356374aSAndroid Build Coastguard Worker // mutex. ABSL_GUARDED_BY() allows the user to specify a particular mutex that
44*9356374aSAndroid Build Coastguard Worker // should be held when accessing the annotated variable.
45*9356374aSAndroid Build Coastguard Worker //
46*9356374aSAndroid Build Coastguard Worker // Although this annotation (and ABSL_PT_GUARDED_BY, below) cannot be applied to
47*9356374aSAndroid Build Coastguard Worker // local variables, a local variable and its associated mutex can often be
48*9356374aSAndroid Build Coastguard Worker // combined into a small class or struct, thereby allowing the annotation.
49*9356374aSAndroid Build Coastguard Worker //
50*9356374aSAndroid Build Coastguard Worker // Example:
51*9356374aSAndroid Build Coastguard Worker //
52*9356374aSAndroid Build Coastguard Worker //   class Foo {
53*9356374aSAndroid Build Coastguard Worker //     Mutex mu_;
54*9356374aSAndroid Build Coastguard Worker //     int p1_ ABSL_GUARDED_BY(mu_);
55*9356374aSAndroid Build Coastguard Worker //     ...
56*9356374aSAndroid Build Coastguard Worker //   };
57*9356374aSAndroid Build Coastguard Worker #if ABSL_HAVE_ATTRIBUTE(guarded_by)
58*9356374aSAndroid Build Coastguard Worker #define ABSL_GUARDED_BY(x) __attribute__((guarded_by(x)))
59*9356374aSAndroid Build Coastguard Worker #else
60*9356374aSAndroid Build Coastguard Worker #define ABSL_GUARDED_BY(x)
61*9356374aSAndroid Build Coastguard Worker #endif
62*9356374aSAndroid Build Coastguard Worker 
63*9356374aSAndroid Build Coastguard Worker // ABSL_PT_GUARDED_BY()
64*9356374aSAndroid Build Coastguard Worker //
65*9356374aSAndroid Build Coastguard Worker // Documents if the memory location pointed to by a pointer should be guarded
66*9356374aSAndroid Build Coastguard Worker // by a mutex when dereferencing the pointer.
67*9356374aSAndroid Build Coastguard Worker //
68*9356374aSAndroid Build Coastguard Worker // Example:
69*9356374aSAndroid Build Coastguard Worker //   class Foo {
70*9356374aSAndroid Build Coastguard Worker //     Mutex mu_;
71*9356374aSAndroid Build Coastguard Worker //     int *p1_ ABSL_PT_GUARDED_BY(mu_);
72*9356374aSAndroid Build Coastguard Worker //     ...
73*9356374aSAndroid Build Coastguard Worker //   };
74*9356374aSAndroid Build Coastguard Worker //
75*9356374aSAndroid Build Coastguard Worker // Note that a pointer variable to a shared memory location could itself be a
76*9356374aSAndroid Build Coastguard Worker // shared variable.
77*9356374aSAndroid Build Coastguard Worker //
78*9356374aSAndroid Build Coastguard Worker // Example:
79*9356374aSAndroid Build Coastguard Worker //
80*9356374aSAndroid Build Coastguard Worker //   // `q_`, guarded by `mu1_`, points to a shared memory location that is
81*9356374aSAndroid Build Coastguard Worker //   // guarded by `mu2_`:
82*9356374aSAndroid Build Coastguard Worker //   int *q_ ABSL_GUARDED_BY(mu1_) ABSL_PT_GUARDED_BY(mu2_);
83*9356374aSAndroid Build Coastguard Worker #if ABSL_HAVE_ATTRIBUTE(pt_guarded_by)
84*9356374aSAndroid Build Coastguard Worker #define ABSL_PT_GUARDED_BY(x) __attribute__((pt_guarded_by(x)))
85*9356374aSAndroid Build Coastguard Worker #else
86*9356374aSAndroid Build Coastguard Worker #define ABSL_PT_GUARDED_BY(x)
87*9356374aSAndroid Build Coastguard Worker #endif
88*9356374aSAndroid Build Coastguard Worker 
89*9356374aSAndroid Build Coastguard Worker // ABSL_ACQUIRED_AFTER() / ABSL_ACQUIRED_BEFORE()
90*9356374aSAndroid Build Coastguard Worker //
91*9356374aSAndroid Build Coastguard Worker // Documents the acquisition order between locks that can be held
92*9356374aSAndroid Build Coastguard Worker // simultaneously by a thread. For any two locks that need to be annotated
93*9356374aSAndroid Build Coastguard Worker // to establish an acquisition order, only one of them needs the annotation.
94*9356374aSAndroid Build Coastguard Worker // (i.e. You don't have to annotate both locks with both ABSL_ACQUIRED_AFTER
95*9356374aSAndroid Build Coastguard Worker // and ABSL_ACQUIRED_BEFORE.)
96*9356374aSAndroid Build Coastguard Worker //
97*9356374aSAndroid Build Coastguard Worker // As with ABSL_GUARDED_BY, this is only applicable to mutexes that are shared
98*9356374aSAndroid Build Coastguard Worker // fields or global variables.
99*9356374aSAndroid Build Coastguard Worker //
100*9356374aSAndroid Build Coastguard Worker // Example:
101*9356374aSAndroid Build Coastguard Worker //
102*9356374aSAndroid Build Coastguard Worker //   Mutex m1_;
103*9356374aSAndroid Build Coastguard Worker //   Mutex m2_ ABSL_ACQUIRED_AFTER(m1_);
104*9356374aSAndroid Build Coastguard Worker #if ABSL_HAVE_ATTRIBUTE(acquired_after)
105*9356374aSAndroid Build Coastguard Worker #define ABSL_ACQUIRED_AFTER(...) __attribute__((acquired_after(__VA_ARGS__)))
106*9356374aSAndroid Build Coastguard Worker #else
107*9356374aSAndroid Build Coastguard Worker #define ABSL_ACQUIRED_AFTER(...)
108*9356374aSAndroid Build Coastguard Worker #endif
109*9356374aSAndroid Build Coastguard Worker 
110*9356374aSAndroid Build Coastguard Worker #if ABSL_HAVE_ATTRIBUTE(acquired_before)
111*9356374aSAndroid Build Coastguard Worker #define ABSL_ACQUIRED_BEFORE(...) __attribute__((acquired_before(__VA_ARGS__)))
112*9356374aSAndroid Build Coastguard Worker #else
113*9356374aSAndroid Build Coastguard Worker #define ABSL_ACQUIRED_BEFORE(...)
114*9356374aSAndroid Build Coastguard Worker #endif
115*9356374aSAndroid Build Coastguard Worker 
116*9356374aSAndroid Build Coastguard Worker // ABSL_EXCLUSIVE_LOCKS_REQUIRED() / ABSL_SHARED_LOCKS_REQUIRED()
117*9356374aSAndroid Build Coastguard Worker //
118*9356374aSAndroid Build Coastguard Worker // Documents a function that expects a mutex to be held prior to entry.
119*9356374aSAndroid Build Coastguard Worker // The mutex is expected to be held both on entry to, and exit from, the
120*9356374aSAndroid Build Coastguard Worker // function.
121*9356374aSAndroid Build Coastguard Worker //
122*9356374aSAndroid Build Coastguard Worker // An exclusive lock allows read-write access to the guarded data member(s), and
123*9356374aSAndroid Build Coastguard Worker // only one thread can acquire a lock exclusively at any one time. A shared lock
124*9356374aSAndroid Build Coastguard Worker // allows read-only access, and any number of threads can acquire a shared lock
125*9356374aSAndroid Build Coastguard Worker // concurrently.
126*9356374aSAndroid Build Coastguard Worker //
127*9356374aSAndroid Build Coastguard Worker // Generally, non-const methods should be annotated with
128*9356374aSAndroid Build Coastguard Worker // ABSL_EXCLUSIVE_LOCKS_REQUIRED, while const methods should be annotated with
129*9356374aSAndroid Build Coastguard Worker // ABSL_SHARED_LOCKS_REQUIRED.
130*9356374aSAndroid Build Coastguard Worker //
131*9356374aSAndroid Build Coastguard Worker // Example:
132*9356374aSAndroid Build Coastguard Worker //
133*9356374aSAndroid Build Coastguard Worker //   Mutex mu1, mu2;
134*9356374aSAndroid Build Coastguard Worker //   int a ABSL_GUARDED_BY(mu1);
135*9356374aSAndroid Build Coastguard Worker //   int b ABSL_GUARDED_BY(mu2);
136*9356374aSAndroid Build Coastguard Worker //
137*9356374aSAndroid Build Coastguard Worker //   void foo() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu1, mu2) { ... }
138*9356374aSAndroid Build Coastguard Worker //   void bar() const ABSL_SHARED_LOCKS_REQUIRED(mu1, mu2) { ... }
139*9356374aSAndroid Build Coastguard Worker #if ABSL_HAVE_ATTRIBUTE(exclusive_locks_required)
140*9356374aSAndroid Build Coastguard Worker #define ABSL_EXCLUSIVE_LOCKS_REQUIRED(...) \
141*9356374aSAndroid Build Coastguard Worker   __attribute__((exclusive_locks_required(__VA_ARGS__)))
142*9356374aSAndroid Build Coastguard Worker #else
143*9356374aSAndroid Build Coastguard Worker #define ABSL_EXCLUSIVE_LOCKS_REQUIRED(...)
144*9356374aSAndroid Build Coastguard Worker #endif
145*9356374aSAndroid Build Coastguard Worker 
146*9356374aSAndroid Build Coastguard Worker #if ABSL_HAVE_ATTRIBUTE(shared_locks_required)
147*9356374aSAndroid Build Coastguard Worker #define ABSL_SHARED_LOCKS_REQUIRED(...) \
148*9356374aSAndroid Build Coastguard Worker   __attribute__((shared_locks_required(__VA_ARGS__)))
149*9356374aSAndroid Build Coastguard Worker #else
150*9356374aSAndroid Build Coastguard Worker #define ABSL_SHARED_LOCKS_REQUIRED(...)
151*9356374aSAndroid Build Coastguard Worker #endif
152*9356374aSAndroid Build Coastguard Worker 
153*9356374aSAndroid Build Coastguard Worker // ABSL_LOCKS_EXCLUDED()
154*9356374aSAndroid Build Coastguard Worker //
155*9356374aSAndroid Build Coastguard Worker // Documents the locks that cannot be held by callers of this function, as they
156*9356374aSAndroid Build Coastguard Worker // might be acquired by this function (Abseil's `Mutex` locks are
157*9356374aSAndroid Build Coastguard Worker // non-reentrant).
158*9356374aSAndroid Build Coastguard Worker #if ABSL_HAVE_ATTRIBUTE(locks_excluded)
159*9356374aSAndroid Build Coastguard Worker #define ABSL_LOCKS_EXCLUDED(...) __attribute__((locks_excluded(__VA_ARGS__)))
160*9356374aSAndroid Build Coastguard Worker #else
161*9356374aSAndroid Build Coastguard Worker #define ABSL_LOCKS_EXCLUDED(...)
162*9356374aSAndroid Build Coastguard Worker #endif
163*9356374aSAndroid Build Coastguard Worker 
164*9356374aSAndroid Build Coastguard Worker // ABSL_LOCK_RETURNED()
165*9356374aSAndroid Build Coastguard Worker //
166*9356374aSAndroid Build Coastguard Worker // Documents a function that returns a mutex without acquiring it.  For example,
167*9356374aSAndroid Build Coastguard Worker // a public getter method that returns a pointer to a private mutex should
168*9356374aSAndroid Build Coastguard Worker // be annotated with ABSL_LOCK_RETURNED.
169*9356374aSAndroid Build Coastguard Worker #if ABSL_HAVE_ATTRIBUTE(lock_returned)
170*9356374aSAndroid Build Coastguard Worker #define ABSL_LOCK_RETURNED(x) __attribute__((lock_returned(x)))
171*9356374aSAndroid Build Coastguard Worker #else
172*9356374aSAndroid Build Coastguard Worker #define ABSL_LOCK_RETURNED(x)
173*9356374aSAndroid Build Coastguard Worker #endif
174*9356374aSAndroid Build Coastguard Worker 
175*9356374aSAndroid Build Coastguard Worker // ABSL_LOCKABLE
176*9356374aSAndroid Build Coastguard Worker //
177*9356374aSAndroid Build Coastguard Worker // Documents if a class/type is a lockable type (such as the `Mutex` class).
178*9356374aSAndroid Build Coastguard Worker #if ABSL_HAVE_ATTRIBUTE(lockable)
179*9356374aSAndroid Build Coastguard Worker #define ABSL_LOCKABLE __attribute__((lockable))
180*9356374aSAndroid Build Coastguard Worker #else
181*9356374aSAndroid Build Coastguard Worker #define ABSL_LOCKABLE
182*9356374aSAndroid Build Coastguard Worker #endif
183*9356374aSAndroid Build Coastguard Worker 
184*9356374aSAndroid Build Coastguard Worker // ABSL_SCOPED_LOCKABLE
185*9356374aSAndroid Build Coastguard Worker //
186*9356374aSAndroid Build Coastguard Worker // Documents if a class does RAII locking (such as the `MutexLock` class).
187*9356374aSAndroid Build Coastguard Worker // The constructor should use `LOCK_FUNCTION()` to specify the mutex that is
188*9356374aSAndroid Build Coastguard Worker // acquired, and the destructor should use `UNLOCK_FUNCTION()` with no
189*9356374aSAndroid Build Coastguard Worker // arguments; the analysis will assume that the destructor unlocks whatever the
190*9356374aSAndroid Build Coastguard Worker // constructor locked.
191*9356374aSAndroid Build Coastguard Worker #if ABSL_HAVE_ATTRIBUTE(scoped_lockable)
192*9356374aSAndroid Build Coastguard Worker #define ABSL_SCOPED_LOCKABLE __attribute__((scoped_lockable))
193*9356374aSAndroid Build Coastguard Worker #else
194*9356374aSAndroid Build Coastguard Worker #define ABSL_SCOPED_LOCKABLE
195*9356374aSAndroid Build Coastguard Worker #endif
196*9356374aSAndroid Build Coastguard Worker 
197*9356374aSAndroid Build Coastguard Worker // ABSL_EXCLUSIVE_LOCK_FUNCTION()
198*9356374aSAndroid Build Coastguard Worker //
199*9356374aSAndroid Build Coastguard Worker // Documents functions that acquire a lock in the body of a function, and do
200*9356374aSAndroid Build Coastguard Worker // not release it.
201*9356374aSAndroid Build Coastguard Worker #if ABSL_HAVE_ATTRIBUTE(exclusive_lock_function)
202*9356374aSAndroid Build Coastguard Worker #define ABSL_EXCLUSIVE_LOCK_FUNCTION(...) \
203*9356374aSAndroid Build Coastguard Worker   __attribute__((exclusive_lock_function(__VA_ARGS__)))
204*9356374aSAndroid Build Coastguard Worker #else
205*9356374aSAndroid Build Coastguard Worker #define ABSL_EXCLUSIVE_LOCK_FUNCTION(...)
206*9356374aSAndroid Build Coastguard Worker #endif
207*9356374aSAndroid Build Coastguard Worker 
208*9356374aSAndroid Build Coastguard Worker // ABSL_SHARED_LOCK_FUNCTION()
209*9356374aSAndroid Build Coastguard Worker //
210*9356374aSAndroid Build Coastguard Worker // Documents functions that acquire a shared (reader) lock in the body of a
211*9356374aSAndroid Build Coastguard Worker // function, and do not release it.
212*9356374aSAndroid Build Coastguard Worker #if ABSL_HAVE_ATTRIBUTE(shared_lock_function)
213*9356374aSAndroid Build Coastguard Worker #define ABSL_SHARED_LOCK_FUNCTION(...) \
214*9356374aSAndroid Build Coastguard Worker   __attribute__((shared_lock_function(__VA_ARGS__)))
215*9356374aSAndroid Build Coastguard Worker #else
216*9356374aSAndroid Build Coastguard Worker #define ABSL_SHARED_LOCK_FUNCTION(...)
217*9356374aSAndroid Build Coastguard Worker #endif
218*9356374aSAndroid Build Coastguard Worker 
219*9356374aSAndroid Build Coastguard Worker // ABSL_UNLOCK_FUNCTION()
220*9356374aSAndroid Build Coastguard Worker //
221*9356374aSAndroid Build Coastguard Worker // Documents functions that expect a lock to be held on entry to the function,
222*9356374aSAndroid Build Coastguard Worker // and release it in the body of the function.
223*9356374aSAndroid Build Coastguard Worker #if ABSL_HAVE_ATTRIBUTE(unlock_function)
224*9356374aSAndroid Build Coastguard Worker #define ABSL_UNLOCK_FUNCTION(...) __attribute__((unlock_function(__VA_ARGS__)))
225*9356374aSAndroid Build Coastguard Worker #else
226*9356374aSAndroid Build Coastguard Worker #define ABSL_UNLOCK_FUNCTION(...)
227*9356374aSAndroid Build Coastguard Worker #endif
228*9356374aSAndroid Build Coastguard Worker 
229*9356374aSAndroid Build Coastguard Worker // ABSL_EXCLUSIVE_TRYLOCK_FUNCTION() / ABSL_SHARED_TRYLOCK_FUNCTION()
230*9356374aSAndroid Build Coastguard Worker //
231*9356374aSAndroid Build Coastguard Worker // Documents functions that try to acquire a lock, and return success or failure
232*9356374aSAndroid Build Coastguard Worker // (or a non-boolean value that can be interpreted as a boolean).
233*9356374aSAndroid Build Coastguard Worker // The first argument should be `true` for functions that return `true` on
234*9356374aSAndroid Build Coastguard Worker // success, or `false` for functions that return `false` on success. The second
235*9356374aSAndroid Build Coastguard Worker // argument specifies the mutex that is locked on success. If unspecified, this
236*9356374aSAndroid Build Coastguard Worker // mutex is assumed to be `this`.
237*9356374aSAndroid Build Coastguard Worker #if ABSL_HAVE_ATTRIBUTE(exclusive_trylock_function)
238*9356374aSAndroid Build Coastguard Worker #define ABSL_EXCLUSIVE_TRYLOCK_FUNCTION(...) \
239*9356374aSAndroid Build Coastguard Worker   __attribute__((exclusive_trylock_function(__VA_ARGS__)))
240*9356374aSAndroid Build Coastguard Worker #else
241*9356374aSAndroid Build Coastguard Worker #define ABSL_EXCLUSIVE_TRYLOCK_FUNCTION(...)
242*9356374aSAndroid Build Coastguard Worker #endif
243*9356374aSAndroid Build Coastguard Worker 
244*9356374aSAndroid Build Coastguard Worker #if ABSL_HAVE_ATTRIBUTE(shared_trylock_function)
245*9356374aSAndroid Build Coastguard Worker #define ABSL_SHARED_TRYLOCK_FUNCTION(...) \
246*9356374aSAndroid Build Coastguard Worker   __attribute__((shared_trylock_function(__VA_ARGS__)))
247*9356374aSAndroid Build Coastguard Worker #else
248*9356374aSAndroid Build Coastguard Worker #define ABSL_SHARED_TRYLOCK_FUNCTION(...)
249*9356374aSAndroid Build Coastguard Worker #endif
250*9356374aSAndroid Build Coastguard Worker 
251*9356374aSAndroid Build Coastguard Worker // ABSL_ASSERT_EXCLUSIVE_LOCK() / ABSL_ASSERT_SHARED_LOCK()
252*9356374aSAndroid Build Coastguard Worker //
253*9356374aSAndroid Build Coastguard Worker // Documents functions that dynamically check to see if a lock is held, and fail
254*9356374aSAndroid Build Coastguard Worker // if it is not held.
255*9356374aSAndroid Build Coastguard Worker #if ABSL_HAVE_ATTRIBUTE(assert_exclusive_lock)
256*9356374aSAndroid Build Coastguard Worker #define ABSL_ASSERT_EXCLUSIVE_LOCK(...) \
257*9356374aSAndroid Build Coastguard Worker   __attribute__((assert_exclusive_lock(__VA_ARGS__)))
258*9356374aSAndroid Build Coastguard Worker #else
259*9356374aSAndroid Build Coastguard Worker #define ABSL_ASSERT_EXCLUSIVE_LOCK(...)
260*9356374aSAndroid Build Coastguard Worker #endif
261*9356374aSAndroid Build Coastguard Worker 
262*9356374aSAndroid Build Coastguard Worker #if ABSL_HAVE_ATTRIBUTE(assert_shared_lock)
263*9356374aSAndroid Build Coastguard Worker #define ABSL_ASSERT_SHARED_LOCK(...) \
264*9356374aSAndroid Build Coastguard Worker   __attribute__((assert_shared_lock(__VA_ARGS__)))
265*9356374aSAndroid Build Coastguard Worker #else
266*9356374aSAndroid Build Coastguard Worker #define ABSL_ASSERT_SHARED_LOCK(...)
267*9356374aSAndroid Build Coastguard Worker #endif
268*9356374aSAndroid Build Coastguard Worker 
269*9356374aSAndroid Build Coastguard Worker // ABSL_NO_THREAD_SAFETY_ANALYSIS
270*9356374aSAndroid Build Coastguard Worker //
271*9356374aSAndroid Build Coastguard Worker // Turns off thread safety checking within the body of a particular function.
272*9356374aSAndroid Build Coastguard Worker // This annotation is used to mark functions that are known to be correct, but
273*9356374aSAndroid Build Coastguard Worker // the locking behavior is more complicated than the analyzer can handle.
274*9356374aSAndroid Build Coastguard Worker #if ABSL_HAVE_ATTRIBUTE(no_thread_safety_analysis)
275*9356374aSAndroid Build Coastguard Worker #define ABSL_NO_THREAD_SAFETY_ANALYSIS \
276*9356374aSAndroid Build Coastguard Worker   __attribute__((no_thread_safety_analysis))
277*9356374aSAndroid Build Coastguard Worker #else
278*9356374aSAndroid Build Coastguard Worker #define ABSL_NO_THREAD_SAFETY_ANALYSIS
279*9356374aSAndroid Build Coastguard Worker #endif
280*9356374aSAndroid Build Coastguard Worker 
281*9356374aSAndroid Build Coastguard Worker //------------------------------------------------------------------------------
282*9356374aSAndroid Build Coastguard Worker // Tool-Supplied Annotations
283*9356374aSAndroid Build Coastguard Worker //------------------------------------------------------------------------------
284*9356374aSAndroid Build Coastguard Worker 
285*9356374aSAndroid Build Coastguard Worker // ABSL_TS_UNCHECKED should be placed around lock expressions that are not valid
286*9356374aSAndroid Build Coastguard Worker // C++ syntax, but which are present for documentation purposes.  These
287*9356374aSAndroid Build Coastguard Worker // annotations will be ignored by the analysis.
288*9356374aSAndroid Build Coastguard Worker #define ABSL_TS_UNCHECKED(x) ""
289*9356374aSAndroid Build Coastguard Worker 
290*9356374aSAndroid Build Coastguard Worker // ABSL_TS_FIXME is used to mark lock expressions that are not valid C++ syntax.
291*9356374aSAndroid Build Coastguard Worker // It is used by automated tools to mark and disable invalid expressions.
292*9356374aSAndroid Build Coastguard Worker // The annotation should either be fixed, or changed to ABSL_TS_UNCHECKED.
293*9356374aSAndroid Build Coastguard Worker #define ABSL_TS_FIXME(x) ""
294*9356374aSAndroid Build Coastguard Worker 
295*9356374aSAndroid Build Coastguard Worker // Like ABSL_NO_THREAD_SAFETY_ANALYSIS, this turns off checking within the body
296*9356374aSAndroid Build Coastguard Worker // of a particular function.  However, this attribute is used to mark functions
297*9356374aSAndroid Build Coastguard Worker // that are incorrect and need to be fixed.  It is used by automated tools to
298*9356374aSAndroid Build Coastguard Worker // avoid breaking the build when the analysis is updated.
299*9356374aSAndroid Build Coastguard Worker // Code owners are expected to eventually fix the routine.
300*9356374aSAndroid Build Coastguard Worker #define ABSL_NO_THREAD_SAFETY_ANALYSIS_FIXME ABSL_NO_THREAD_SAFETY_ANALYSIS
301*9356374aSAndroid Build Coastguard Worker 
302*9356374aSAndroid Build Coastguard Worker // Similar to ABSL_NO_THREAD_SAFETY_ANALYSIS_FIXME, this macro marks a
303*9356374aSAndroid Build Coastguard Worker // ABSL_GUARDED_BY annotation that needs to be fixed, because it is producing
304*9356374aSAndroid Build Coastguard Worker // thread safety warning. It disables the ABSL_GUARDED_BY.
305*9356374aSAndroid Build Coastguard Worker #define ABSL_GUARDED_BY_FIXME(x)
306*9356374aSAndroid Build Coastguard Worker 
307*9356374aSAndroid Build Coastguard Worker // Disables warnings for a single read operation.  This can be used to avoid
308*9356374aSAndroid Build Coastguard Worker // warnings when it is known that the read is not actually involved in a race,
309*9356374aSAndroid Build Coastguard Worker // but the compiler cannot confirm that.
310*9356374aSAndroid Build Coastguard Worker #define ABSL_TS_UNCHECKED_READ(x) absl::base_internal::ts_unchecked_read(x)
311*9356374aSAndroid Build Coastguard Worker 
312*9356374aSAndroid Build Coastguard Worker namespace absl {
313*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
314*9356374aSAndroid Build Coastguard Worker namespace base_internal {
315*9356374aSAndroid Build Coastguard Worker 
316*9356374aSAndroid Build Coastguard Worker // Takes a reference to a guarded data member, and returns an unguarded
317*9356374aSAndroid Build Coastguard Worker // reference.
318*9356374aSAndroid Build Coastguard Worker // Do not use this function directly, use ABSL_TS_UNCHECKED_READ instead.
319*9356374aSAndroid Build Coastguard Worker template <typename T>
ts_unchecked_read(const T & v)320*9356374aSAndroid Build Coastguard Worker inline const T& ts_unchecked_read(const T& v) ABSL_NO_THREAD_SAFETY_ANALYSIS {
321*9356374aSAndroid Build Coastguard Worker   return v;
322*9356374aSAndroid Build Coastguard Worker }
323*9356374aSAndroid Build Coastguard Worker 
324*9356374aSAndroid Build Coastguard Worker template <typename T>
ts_unchecked_read(T & v)325*9356374aSAndroid Build Coastguard Worker inline T& ts_unchecked_read(T& v) ABSL_NO_THREAD_SAFETY_ANALYSIS {
326*9356374aSAndroid Build Coastguard Worker   return v;
327*9356374aSAndroid Build Coastguard Worker }
328*9356374aSAndroid Build Coastguard Worker 
329*9356374aSAndroid Build Coastguard Worker }  // namespace base_internal
330*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
331*9356374aSAndroid Build Coastguard Worker }  // namespace absl
332*9356374aSAndroid Build Coastguard Worker 
333*9356374aSAndroid Build Coastguard Worker #endif  // ABSL_BASE_THREAD_ANNOTATIONS_H_
334