blob: e23fff1d20132737a9802035fcfb7ef5704c1ca0 [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -07001// 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
Austin Schuhb4691e92020-12-31 12:37:18 -080037#include "absl/base/attributes.h"
38#include "absl/base/config.h"
Austin Schuh36244a12019-09-21 17:52:38 -070039// TODO(mbonadei): Remove after the backward compatibility period.
40#include "absl/base/internal/thread_annotations.h" // IWYU pragma: export
41
Austin Schuh36244a12019-09-21 17:52:38 -070042// ABSL_GUARDED_BY()
43//
44// Documents if a shared field or global variable needs to be protected by a
45// mutex. ABSL_GUARDED_BY() allows the user to specify a particular mutex that
46// should be held when accessing the annotated variable.
47//
48// Although this annotation (and ABSL_PT_GUARDED_BY, below) cannot be applied to
49// local variables, a local variable and its associated mutex can often be
50// combined into a small class or struct, thereby allowing the annotation.
51//
52// Example:
53//
54// class Foo {
55// Mutex mu_;
56// int p1_ ABSL_GUARDED_BY(mu_);
57// ...
58// };
Austin Schuhb4691e92020-12-31 12:37:18 -080059#if ABSL_HAVE_ATTRIBUTE(guarded_by)
60#define ABSL_GUARDED_BY(x) __attribute__((guarded_by(x)))
61#else
62#define ABSL_GUARDED_BY(x)
63#endif
Austin Schuh36244a12019-09-21 17:52:38 -070064
65// ABSL_PT_GUARDED_BY()
66//
67// Documents if the memory location pointed to by a pointer should be guarded
68// by a mutex when dereferencing the pointer.
69//
70// Example:
71// class Foo {
72// Mutex mu_;
73// int *p1_ ABSL_PT_GUARDED_BY(mu_);
74// ...
75// };
76//
77// Note that a pointer variable to a shared memory location could itself be a
78// shared variable.
79//
80// Example:
81//
82// // `q_`, guarded by `mu1_`, points to a shared memory location that is
83// // guarded by `mu2_`:
84// int *q_ ABSL_GUARDED_BY(mu1_) ABSL_PT_GUARDED_BY(mu2_);
Austin Schuhb4691e92020-12-31 12:37:18 -080085#if ABSL_HAVE_ATTRIBUTE(pt_guarded_by)
86#define ABSL_PT_GUARDED_BY(x) __attribute__((pt_guarded_by(x)))
87#else
88#define ABSL_PT_GUARDED_BY(x)
89#endif
Austin Schuh36244a12019-09-21 17:52:38 -070090
91// ABSL_ACQUIRED_AFTER() / ABSL_ACQUIRED_BEFORE()
92//
93// Documents the acquisition order between locks that can be held
94// simultaneously by a thread. For any two locks that need to be annotated
95// to establish an acquisition order, only one of them needs the annotation.
96// (i.e. You don't have to annotate both locks with both ABSL_ACQUIRED_AFTER
97// and ABSL_ACQUIRED_BEFORE.)
98//
99// As with ABSL_GUARDED_BY, this is only applicable to mutexes that are shared
100// fields or global variables.
101//
102// Example:
103//
104// Mutex m1_;
105// Mutex m2_ ABSL_ACQUIRED_AFTER(m1_);
Austin Schuhb4691e92020-12-31 12:37:18 -0800106#if ABSL_HAVE_ATTRIBUTE(acquired_after)
107#define ABSL_ACQUIRED_AFTER(...) __attribute__((acquired_after(__VA_ARGS__)))
108#else
109#define ABSL_ACQUIRED_AFTER(...)
110#endif
Austin Schuh36244a12019-09-21 17:52:38 -0700111
Austin Schuhb4691e92020-12-31 12:37:18 -0800112#if ABSL_HAVE_ATTRIBUTE(acquired_before)
113#define ABSL_ACQUIRED_BEFORE(...) __attribute__((acquired_before(__VA_ARGS__)))
114#else
115#define ABSL_ACQUIRED_BEFORE(...)
116#endif
Austin Schuh36244a12019-09-21 17:52:38 -0700117
118// ABSL_EXCLUSIVE_LOCKS_REQUIRED() / ABSL_SHARED_LOCKS_REQUIRED()
119//
120// Documents a function that expects a mutex to be held prior to entry.
121// The mutex is expected to be held both on entry to, and exit from, the
122// function.
123//
124// An exclusive lock allows read-write access to the guarded data member(s), and
125// only one thread can acquire a lock exclusively at any one time. A shared lock
126// allows read-only access, and any number of threads can acquire a shared lock
127// concurrently.
128//
129// Generally, non-const methods should be annotated with
130// ABSL_EXCLUSIVE_LOCKS_REQUIRED, while const methods should be annotated with
131// ABSL_SHARED_LOCKS_REQUIRED.
132//
133// Example:
134//
135// Mutex mu1, mu2;
136// int a ABSL_GUARDED_BY(mu1);
137// int b ABSL_GUARDED_BY(mu2);
138//
139// void foo() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu1, mu2) { ... }
140// void bar() const ABSL_SHARED_LOCKS_REQUIRED(mu1, mu2) { ... }
Austin Schuhb4691e92020-12-31 12:37:18 -0800141#if ABSL_HAVE_ATTRIBUTE(exclusive_locks_required)
142#define ABSL_EXCLUSIVE_LOCKS_REQUIRED(...) \
143 __attribute__((exclusive_locks_required(__VA_ARGS__)))
144#else
145#define ABSL_EXCLUSIVE_LOCKS_REQUIRED(...)
146#endif
Austin Schuh36244a12019-09-21 17:52:38 -0700147
Austin Schuhb4691e92020-12-31 12:37:18 -0800148#if ABSL_HAVE_ATTRIBUTE(shared_locks_required)
Austin Schuh36244a12019-09-21 17:52:38 -0700149#define ABSL_SHARED_LOCKS_REQUIRED(...) \
Austin Schuhb4691e92020-12-31 12:37:18 -0800150 __attribute__((shared_locks_required(__VA_ARGS__)))
151#else
152#define ABSL_SHARED_LOCKS_REQUIRED(...)
153#endif
Austin Schuh36244a12019-09-21 17:52:38 -0700154
155// ABSL_LOCKS_EXCLUDED()
156//
157// Documents the locks acquired in the body of the function. These locks
158// cannot be held when calling this function (as Abseil's `Mutex` locks are
159// non-reentrant).
Austin Schuhb4691e92020-12-31 12:37:18 -0800160#if ABSL_HAVE_ATTRIBUTE(locks_excluded)
161#define ABSL_LOCKS_EXCLUDED(...) __attribute__((locks_excluded(__VA_ARGS__)))
162#else
163#define ABSL_LOCKS_EXCLUDED(...)
164#endif
Austin Schuh36244a12019-09-21 17:52:38 -0700165
166// ABSL_LOCK_RETURNED()
167//
168// Documents a function that returns a mutex without acquiring it. For example,
169// a public getter method that returns a pointer to a private mutex should
170// be annotated with ABSL_LOCK_RETURNED.
Austin Schuhb4691e92020-12-31 12:37:18 -0800171#if ABSL_HAVE_ATTRIBUTE(lock_returned)
172#define ABSL_LOCK_RETURNED(x) __attribute__((lock_returned(x)))
173#else
174#define ABSL_LOCK_RETURNED(x)
175#endif
Austin Schuh36244a12019-09-21 17:52:38 -0700176
177// ABSL_LOCKABLE
178//
179// Documents if a class/type is a lockable type (such as the `Mutex` class).
Austin Schuhb4691e92020-12-31 12:37:18 -0800180#if ABSL_HAVE_ATTRIBUTE(lockable)
181#define ABSL_LOCKABLE __attribute__((lockable))
182#else
183#define ABSL_LOCKABLE
184#endif
Austin Schuh36244a12019-09-21 17:52:38 -0700185
186// ABSL_SCOPED_LOCKABLE
187//
188// Documents if a class does RAII locking (such as the `MutexLock` class).
189// The constructor should use `LOCK_FUNCTION()` to specify the mutex that is
190// acquired, and the destructor should use `UNLOCK_FUNCTION()` with no
191// arguments; the analysis will assume that the destructor unlocks whatever the
192// constructor locked.
Austin Schuhb4691e92020-12-31 12:37:18 -0800193#if ABSL_HAVE_ATTRIBUTE(scoped_lockable)
194#define ABSL_SCOPED_LOCKABLE __attribute__((scoped_lockable))
195#else
196#define ABSL_SCOPED_LOCKABLE
197#endif
Austin Schuh36244a12019-09-21 17:52:38 -0700198
199// ABSL_EXCLUSIVE_LOCK_FUNCTION()
200//
201// Documents functions that acquire a lock in the body of a function, and do
202// not release it.
Austin Schuhb4691e92020-12-31 12:37:18 -0800203#if ABSL_HAVE_ATTRIBUTE(exclusive_lock_function)
204#define ABSL_EXCLUSIVE_LOCK_FUNCTION(...) \
205 __attribute__((exclusive_lock_function(__VA_ARGS__)))
206#else
207#define ABSL_EXCLUSIVE_LOCK_FUNCTION(...)
208#endif
Austin Schuh36244a12019-09-21 17:52:38 -0700209
210// ABSL_SHARED_LOCK_FUNCTION()
211//
212// Documents functions that acquire a shared (reader) lock in the body of a
213// function, and do not release it.
Austin Schuhb4691e92020-12-31 12:37:18 -0800214#if ABSL_HAVE_ATTRIBUTE(shared_lock_function)
Austin Schuh36244a12019-09-21 17:52:38 -0700215#define ABSL_SHARED_LOCK_FUNCTION(...) \
Austin Schuhb4691e92020-12-31 12:37:18 -0800216 __attribute__((shared_lock_function(__VA_ARGS__)))
217#else
218#define ABSL_SHARED_LOCK_FUNCTION(...)
219#endif
Austin Schuh36244a12019-09-21 17:52:38 -0700220
221// ABSL_UNLOCK_FUNCTION()
222//
223// Documents functions that expect a lock to be held on entry to the function,
224// and release it in the body of the function.
Austin Schuhb4691e92020-12-31 12:37:18 -0800225#if ABSL_HAVE_ATTRIBUTE(unlock_function)
226#define ABSL_UNLOCK_FUNCTION(...) __attribute__((unlock_function(__VA_ARGS__)))
227#else
228#define ABSL_UNLOCK_FUNCTION(...)
229#endif
Austin Schuh36244a12019-09-21 17:52:38 -0700230
231// ABSL_EXCLUSIVE_TRYLOCK_FUNCTION() / ABSL_SHARED_TRYLOCK_FUNCTION()
232//
233// Documents functions that try to acquire a lock, and return success or failure
234// (or a non-boolean value that can be interpreted as a boolean).
235// The first argument should be `true` for functions that return `true` on
236// success, or `false` for functions that return `false` on success. The second
237// argument specifies the mutex that is locked on success. If unspecified, this
238// mutex is assumed to be `this`.
Austin Schuhb4691e92020-12-31 12:37:18 -0800239#if ABSL_HAVE_ATTRIBUTE(exclusive_trylock_function)
Austin Schuh36244a12019-09-21 17:52:38 -0700240#define ABSL_EXCLUSIVE_TRYLOCK_FUNCTION(...) \
Austin Schuhb4691e92020-12-31 12:37:18 -0800241 __attribute__((exclusive_trylock_function(__VA_ARGS__)))
242#else
243#define ABSL_EXCLUSIVE_TRYLOCK_FUNCTION(...)
244#endif
Austin Schuh36244a12019-09-21 17:52:38 -0700245
Austin Schuhb4691e92020-12-31 12:37:18 -0800246#if ABSL_HAVE_ATTRIBUTE(shared_trylock_function)
247#define ABSL_SHARED_TRYLOCK_FUNCTION(...) \
248 __attribute__((shared_trylock_function(__VA_ARGS__)))
249#else
250#define ABSL_SHARED_TRYLOCK_FUNCTION(...)
251#endif
Austin Schuh36244a12019-09-21 17:52:38 -0700252
253// ABSL_ASSERT_EXCLUSIVE_LOCK() / ABSL_ASSERT_SHARED_LOCK()
254//
255// Documents functions that dynamically check to see if a lock is held, and fail
256// if it is not held.
Austin Schuhb4691e92020-12-31 12:37:18 -0800257#if ABSL_HAVE_ATTRIBUTE(assert_exclusive_lock)
Austin Schuh36244a12019-09-21 17:52:38 -0700258#define ABSL_ASSERT_EXCLUSIVE_LOCK(...) \
Austin Schuhb4691e92020-12-31 12:37:18 -0800259 __attribute__((assert_exclusive_lock(__VA_ARGS__)))
260#else
261#define ABSL_ASSERT_EXCLUSIVE_LOCK(...)
262#endif
Austin Schuh36244a12019-09-21 17:52:38 -0700263
Austin Schuhb4691e92020-12-31 12:37:18 -0800264#if ABSL_HAVE_ATTRIBUTE(assert_shared_lock)
Austin Schuh36244a12019-09-21 17:52:38 -0700265#define ABSL_ASSERT_SHARED_LOCK(...) \
Austin Schuhb4691e92020-12-31 12:37:18 -0800266 __attribute__((assert_shared_lock(__VA_ARGS__)))
267#else
268#define ABSL_ASSERT_SHARED_LOCK(...)
269#endif
Austin Schuh36244a12019-09-21 17:52:38 -0700270
271// ABSL_NO_THREAD_SAFETY_ANALYSIS
272//
273// Turns off thread safety checking within the body of a particular function.
274// This annotation is used to mark functions that are known to be correct, but
275// the locking behavior is more complicated than the analyzer can handle.
Austin Schuhb4691e92020-12-31 12:37:18 -0800276#if ABSL_HAVE_ATTRIBUTE(no_thread_safety_analysis)
Austin Schuh36244a12019-09-21 17:52:38 -0700277#define ABSL_NO_THREAD_SAFETY_ANALYSIS \
Austin Schuhb4691e92020-12-31 12:37:18 -0800278 __attribute__((no_thread_safety_analysis))
279#else
280#define ABSL_NO_THREAD_SAFETY_ANALYSIS
281#endif
Austin Schuh36244a12019-09-21 17:52:38 -0700282
283//------------------------------------------------------------------------------
284// Tool-Supplied Annotations
285//------------------------------------------------------------------------------
286
287// ABSL_TS_UNCHECKED should be placed around lock expressions that are not valid
288// C++ syntax, but which are present for documentation purposes. These
289// annotations will be ignored by the analysis.
290#define ABSL_TS_UNCHECKED(x) ""
291
292// ABSL_TS_FIXME is used to mark lock expressions that are not valid C++ syntax.
293// It is used by automated tools to mark and disable invalid expressions.
294// The annotation should either be fixed, or changed to ABSL_TS_UNCHECKED.
295#define ABSL_TS_FIXME(x) ""
296
297// Like ABSL_NO_THREAD_SAFETY_ANALYSIS, this turns off checking within the body
298// of a particular function. However, this attribute is used to mark functions
299// that are incorrect and need to be fixed. It is used by automated tools to
300// avoid breaking the build when the analysis is updated.
301// Code owners are expected to eventually fix the routine.
302#define ABSL_NO_THREAD_SAFETY_ANALYSIS_FIXME ABSL_NO_THREAD_SAFETY_ANALYSIS
303
304// Similar to ABSL_NO_THREAD_SAFETY_ANALYSIS_FIXME, this macro marks a
305// ABSL_GUARDED_BY annotation that needs to be fixed, because it is producing
306// thread safety warning. It disables the ABSL_GUARDED_BY.
307#define ABSL_GUARDED_BY_FIXME(x)
308
309// Disables warnings for a single read operation. This can be used to avoid
310// warnings when it is known that the read is not actually involved in a race,
311// but the compiler cannot confirm that.
312#define ABSL_TS_UNCHECKED_READ(x) absl::base_internal::ts_unchecked_read(x)
313
314namespace absl {
Austin Schuhb4691e92020-12-31 12:37:18 -0800315ABSL_NAMESPACE_BEGIN
Austin Schuh36244a12019-09-21 17:52:38 -0700316namespace base_internal {
317
318// Takes a reference to a guarded data member, and returns an unguarded
319// reference.
320// Do not used this function directly, use ABSL_TS_UNCHECKED_READ instead.
321template <typename T>
322inline const T& ts_unchecked_read(const T& v) ABSL_NO_THREAD_SAFETY_ANALYSIS {
323 return v;
324}
325
326template <typename T>
327inline T& ts_unchecked_read(T& v) ABSL_NO_THREAD_SAFETY_ANALYSIS {
328 return v;
329}
330
331} // namespace base_internal
Austin Schuhb4691e92020-12-31 12:37:18 -0800332ABSL_NAMESPACE_END
Austin Schuh36244a12019-09-21 17:52:38 -0700333} // namespace absl
334
335#endif // ABSL_BASE_THREAD_ANNOTATIONS_H_