blob: b6db61b9891669b4a111596517d48adf60ecd99b [file] [log] [blame]
Brian Silverman70325d62015-09-20 17:00:43 -04001// Copyright (c) 2008, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29// ---
30//
31//
32// This header file contains the macro definitions for thread safety
33// annotations that allow the developers to document the locking policies
34// of their multi-threaded code. The annotations can also help program
35// analysis tools to identify potential thread safety issues.
36//
37//
38// The annotations are implemented using GCC's "attributes" extension.
39// Using the macros defined here instead of the raw GCC attributes allows
40// for portability and future compatibility.
41//
42
43#ifndef BASE_THREAD_ANNOTATIONS_H_
44#define BASE_THREAD_ANNOTATIONS_H_
45
46
47#include <config.h>
48#if defined(__GNUC__) && defined(__SUPPORT_TS_ANNOTATION__) && !defined(SWIG)
49#define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
50#else
51#define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op
52#endif
53
54
55// Document if a shared variable/field needs to be protected by a lock.
56// GUARDED_BY allows the user to specify a particular lock that should be
57// held when accessing the annotated variable, while GUARDED_VAR only
58// indicates a shared variable should be guarded (by any lock). GUARDED_VAR
59// is primarily used when the client cannot express the name of the lock.
60#define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
61#define GUARDED_VAR THREAD_ANNOTATION_ATTRIBUTE__(guarded)
62
63// Document if the memory location pointed to by a pointer should be guarded
64// by a lock when dereferencing the pointer. Similar to GUARDED_VAR,
65// PT_GUARDED_VAR is primarily used when the client cannot express the name
66// of the lock. Note that a pointer variable to a shared memory location
67// could itself be a shared variable. For example, if a shared global pointer
68// q, which is guarded by mu1, points to a shared memory location that is
69// guarded by mu2, q should be annotated as follows:
70// int *q GUARDED_BY(mu1) PT_GUARDED_BY(mu2);
71#define PT_GUARDED_BY(x) \
72 THREAD_ANNOTATION_ATTRIBUTE__(point_to_guarded_by(x))
73#define PT_GUARDED_VAR \
74 THREAD_ANNOTATION_ATTRIBUTE__(point_to_guarded)
75
76// Document the acquisition order between locks that can be held
77// simultaneously by a thread. For any two locks that need to be annotated
78// to establish an acquisition order, only one of them needs the annotation.
79// (i.e. You don't have to annotate both locks with both ACQUIRED_AFTER
80// and ACQUIRED_BEFORE.)
81#define ACQUIRED_AFTER(x) \
82 THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(x))
83#define ACQUIRED_BEFORE(x) \
84 THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(x))
85
86// The following three annotations document the lock requirements for
87// functions/methods.
88
89// Document if a function expects certain locks to be held before it is called
90#define EXCLUSIVE_LOCKS_REQUIRED(x) \
91 THREAD_ANNOTATION_ATTRIBUTE__(exclusive_locks_required(x))
92
93#define SHARED_LOCKS_REQUIRED(x) \
94 THREAD_ANNOTATION_ATTRIBUTE__(shared_locks_required(x))
95
96// Document the locks acquired in the body of the function. These locks
97// non-reentrant).
98#define LOCKS_EXCLUDED(x) \
99 THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(x))
100
101// Document the lock the annotated function returns without acquiring it.
102#define LOCK_RETURNED(x) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
103
104// Document if a class/type is a lockable type (such as the Mutex class).
105#define LOCKABLE THREAD_ANNOTATION_ATTRIBUTE__(lockable)
106
107// Document if a class is a scoped lockable type (such as the MutexLock class).
108#define SCOPED_LOCKABLE THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
109
110// The following annotations specify lock and unlock primitives.
111#define EXCLUSIVE_LOCK_FUNCTION(x) \
112 THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock(x))
113
114#define SHARED_LOCK_FUNCTION(x) \
115 THREAD_ANNOTATION_ATTRIBUTE__(shared_lock(x))
116
117#define EXCLUSIVE_TRYLOCK_FUNCTION(x) \
118 THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock(x))
119
120#define SHARED_TRYLOCK_FUNCTION(x) \
121 THREAD_ANNOTATION_ATTRIBUTE__(shared_trylock(x))
122
123#define UNLOCK_FUNCTION(x) \
124 THREAD_ANNOTATION_ATTRIBUTE__(unlock(x))
125
126// An escape hatch for thread safety analysis to ignore the annotated function.
127#define NO_THREAD_SAFETY_ANALYSIS \
128 THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
129
130#endif // BASE_THREAD_ANNOTATIONS_H_