OLD | NEW |
1 // | 1 // |
2 // Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 // Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
3 // | 3 // |
4 // Use of this source code is governed by a BSD-style license | 4 // Use of this source code is governed by a BSD-style license |
5 // that can be found in the LICENSE file in the root of the source | 5 // that can be found in the LICENSE file in the root of the source |
6 // tree. An additional intellectual property rights grant can be found | 6 // tree. An additional intellectual property rights grant can be found |
7 // in the file PATENTS. All contributing project authors may | 7 // in the file PATENTS. All contributing project authors may |
8 // be found in the AUTHORS file in the root of the source tree. | 8 // be found in the AUTHORS file in the root of the source tree. |
9 // | 9 // |
10 // Borrowed from | 10 // Borrowed from |
11 // https://code.google.com/p/gperftools/source/browse/src/base/thread_annotation
s.h | 11 // https://code.google.com/p/gperftools/source/browse/src/base/thread_annotation
s.h |
12 // but adapted for clang attributes instead of the gcc. | 12 // but adapted for clang attributes instead of the gcc. |
13 // | 13 // |
14 // This header file contains the macro definitions for thread safety | 14 // This header file contains the macro definitions for thread safety |
15 // annotations that allow the developers to document the locking policies | 15 // annotations that allow the developers to document the locking policies |
16 // of their multi-threaded code. The annotations can also help program | 16 // of their multi-threaded code. The annotations can also help program |
17 // analysis tools to identify potential thread safety issues. | 17 // analysis tools to identify potential thread safety issues. |
18 | 18 |
19 #ifndef WEBRTC_RTC_BASE_THREAD_ANNOTATIONS_H_ | 19 #ifndef WEBRTC_RTC_BASE_THREAD_ANNOTATIONS_H_ |
20 #define WEBRTC_RTC_BASE_THREAD_ANNOTATIONS_H_ | 20 #define WEBRTC_RTC_BASE_THREAD_ANNOTATIONS_H_ |
21 | 21 |
22 #if defined(__clang__) && (!defined(SWIG)) | 22 #if defined(__clang__) && (!defined(SWIG)) |
23 #define RTC_THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x)) | 23 #define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x)) |
24 #else | 24 #else |
25 #define RTC_THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op | 25 #define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op |
26 #endif | 26 #endif |
27 | 27 |
28 // Document if a shared variable/field needs to be protected by a lock. | 28 // Document if a shared variable/field needs to be protected by a lock. |
29 // GUARDED_BY allows the user to specify a particular lock that should be | 29 // GUARDED_BY allows the user to specify a particular lock that should be |
30 // held when accessing the annotated variable, while GUARDED_VAR only | 30 // held when accessing the annotated variable, while GUARDED_VAR only |
31 // indicates a shared variable should be guarded (by any lock). GUARDED_VAR | 31 // indicates a shared variable should be guarded (by any lock). GUARDED_VAR |
32 // is primarily used when the client cannot express the name of the lock. | 32 // is primarily used when the client cannot express the name of the lock. |
33 #if !defined(GUARDED_BY) | 33 #if !defined(GUARDED_BY) |
34 #define GUARDED_BY(x) RTC_THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x)) | 34 #define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x)) |
35 #endif | 35 #endif |
36 #define RTC_GUARDED_VAR RTC_THREAD_ANNOTATION_ATTRIBUTE__(guarded_var) | 36 #if !defined(GUARDED_VAR) |
| 37 #define GUARDED_VAR THREAD_ANNOTATION_ATTRIBUTE__(guarded_var) |
| 38 #endif |
37 | 39 |
38 // Document if the memory location pointed to by a pointer should be guarded | 40 // Document if the memory location pointed to by a pointer should be guarded |
39 // by a lock when dereferencing the pointer. Similar to GUARDED_VAR, | 41 // by a lock when dereferencing the pointer. Similar to GUARDED_VAR, |
40 // PT_GUARDED_VAR is primarily used when the client cannot express the name | 42 // PT_GUARDED_VAR is primarily used when the client cannot express the name |
41 // of the lock. Note that a pointer variable to a shared memory location | 43 // of the lock. Note that a pointer variable to a shared memory location |
42 // could itself be a shared variable. For example, if a shared global pointer | 44 // could itself be a shared variable. For example, if a shared global pointer |
43 // q, which is guarded by mu1, points to a shared memory location that is | 45 // q, which is guarded by mu1, points to a shared memory location that is |
44 // guarded by mu2, q should be annotated as follows: | 46 // guarded by mu2, q should be annotated as follows: |
45 // int *q GUARDED_BY(mu1) PT_GUARDED_BY(mu2); | 47 // int *q GUARDED_BY(mu1) PT_GUARDED_BY(mu2); |
46 #define RTC_PT_GUARDED_BY(x) RTC_THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x)) | 48 #if !defined(PT_GUARDED_BY) |
47 #define RTC_PT_GUARDED_VAR RTC_THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_var) | 49 #define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x)) |
| 50 #endif |
| 51 #if !defined(PT_GUARDED_VAR) |
| 52 #define PT_GUARDED_VAR THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_var) |
| 53 #endif |
48 | 54 |
49 // Document the acquisition order between locks that can be held | 55 // Document the acquisition order between locks that can be held |
50 // simultaneously by a thread. For any two locks that need to be annotated | 56 // simultaneously by a thread. For any two locks that need to be annotated |
51 // to establish an acquisition order, only one of them needs the annotation. | 57 // to establish an acquisition order, only one of them needs the annotation. |
52 // (i.e. You don't have to annotate both locks with both ACQUIRED_AFTER | 58 // (i.e. You don't have to annotate both locks with both ACQUIRED_AFTER |
53 // and ACQUIRED_BEFORE.) | 59 // and ACQUIRED_BEFORE.) |
54 #if !defined(ACQUIRED_AFTER) | 60 #if !defined(ACQUIRED_AFTER) |
55 #define ACQUIRED_AFTER(x) RTC_THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(x)) | 61 #define ACQUIRED_AFTER(x) THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(x)) |
56 #endif | 62 #endif |
57 #if !defined(ACQUIRED_BEFORE) | 63 #if !defined(ACQUIRED_BEFORE) |
58 #define ACQUIRED_BEFORE(x) RTC_THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(x)) | 64 #define ACQUIRED_BEFORE(x) THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(x)) |
59 #endif | 65 #endif |
60 | 66 |
61 // The following three annotations document the lock requirements for | 67 // The following three annotations document the lock requirements for |
62 // functions/methods. | 68 // functions/methods. |
63 | 69 |
64 // Document if a function expects certain locks to be held before it is called | 70 // Document if a function expects certain locks to be held before it is called |
65 #if !defined(EXCLUSIVE_LOCKS_REQUIRED) | 71 #if !defined(EXCLUSIVE_LOCKS_REQUIRED) |
66 #define EXCLUSIVE_LOCKS_REQUIRED(...) \ | 72 #define EXCLUSIVE_LOCKS_REQUIRED(...) \ |
67 RTC_THREAD_ANNOTATION_ATTRIBUTE__(exclusive_locks_required(__VA_ARGS__)) | 73 THREAD_ANNOTATION_ATTRIBUTE__(exclusive_locks_required(__VA_ARGS__)) |
68 #endif | 74 #endif |
69 | 75 |
70 #if !defined(SHARED_LOCKS_REQUIRED) | 76 #if !defined(SHARED_LOCKS_REQUIRED) |
71 #define SHARED_LOCKS_REQUIRED(...) \ | 77 #define SHARED_LOCKS_REQUIRED(...) \ |
72 RTC_THREAD_ANNOTATION_ATTRIBUTE__(shared_locks_required(__VA_ARGS__)) | 78 THREAD_ANNOTATION_ATTRIBUTE__(shared_locks_required(__VA_ARGS__)) |
73 #endif | 79 #endif |
74 | 80 |
75 // Document the locks acquired in the body of the function. These locks | 81 // Document the locks acquired in the body of the function. These locks |
76 // cannot be held when calling this function (as google3's Mutex locks are | 82 // cannot be held when calling this function (as google3's Mutex locks are |
77 // non-reentrant). | 83 // non-reentrant). |
78 #if !defined(LOCKS_EXCLUDED) | 84 #if !defined(LOCKS_EXCLUDED) |
79 #define LOCKS_EXCLUDED(...) \ | 85 #define LOCKS_EXCLUDED(...) \ |
80 RTC_THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__)) | 86 THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__)) |
81 #endif | 87 #endif |
82 | 88 |
83 // Document the lock the annotated function returns without acquiring it. | 89 // Document the lock the annotated function returns without acquiring it. |
84 #define RTC_LOCK_RETURNED(x) RTC_THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x)) | 90 #if !defined(LOCK_RETURNED) |
| 91 #define LOCK_RETURNED(x) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x)) |
| 92 #endif |
85 | 93 |
86 // Document if a class/type is a lockable type (such as the Mutex class). | 94 // Document if a class/type is a lockable type (such as the Mutex class). |
87 #define RTC_LOCKABLE RTC_THREAD_ANNOTATION_ATTRIBUTE__(lockable) | 95 #if !defined(LOCKABLE) |
| 96 #define LOCKABLE THREAD_ANNOTATION_ATTRIBUTE__(lockable) |
| 97 #endif |
88 | 98 |
89 // Document if a class is a scoped lockable type (such as the MutexLock class). | 99 // Document if a class is a scoped lockable type (such as the MutexLock class). |
90 #define RTC_SCOPED_LOCKABLE RTC_THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable) | 100 #if !defined(SCOPED_LOCKABLE) |
| 101 #define SCOPED_LOCKABLE THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable) |
| 102 #endif |
91 | 103 |
92 // The following annotations specify lock and unlock primitives. | 104 // The following annotations specify lock and unlock primitives. |
93 #define RTC_EXCLUSIVE_LOCK_FUNCTION(...) \ | 105 #if !defined(EXCLUSIVE_LOCK_FUNCTION) |
94 RTC_THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock_function(__VA_ARGS__)) | 106 #define EXCLUSIVE_LOCK_FUNCTION(...) \ |
| 107 THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock_function(__VA_ARGS__)) |
| 108 #endif |
95 | 109 |
96 #define RTC_SHARED_LOCK_FUNCTION(...) \ | 110 #if !defined(SHARED_LOCK_FUNCTION) |
97 RTC_THREAD_ANNOTATION_ATTRIBUTE__(shared_lock_function(__VA_ARGS__)) | 111 #define SHARED_LOCK_FUNCTION(...) \ |
| 112 THREAD_ANNOTATION_ATTRIBUTE__(shared_lock_function(__VA_ARGS__)) |
| 113 #endif |
98 | 114 |
99 #define RTC_EXCLUSIVE_TRYLOCK_FUNCTION(...) \ | 115 #if !defined(EXCLUSIVE_TRYLOCK_FUNCTION) |
100 RTC_THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock_function(__VA_ARGS__)) | 116 #define EXCLUSIVE_TRYLOCK_FUNCTION(...) \ |
| 117 THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock_function(__VA_ARGS__)) |
| 118 #endif |
101 | 119 |
102 #define RTC_SHARED_TRYLOCK_FUNCTION(...) \ | 120 #if !defined(SHARED_TRYLOCK_FUNCTION) |
103 RTC_THREAD_ANNOTATION_ATTRIBUTE__(shared_trylock_function(__VA_ARGS__)) | 121 #define SHARED_TRYLOCK_FUNCTION(...) \ |
| 122 THREAD_ANNOTATION_ATTRIBUTE__(shared_trylock_function(__VA_ARGS__)) |
| 123 #endif |
104 | 124 |
105 #define RTC_UNLOCK_FUNCTION(...) \ | 125 #if !defined(UNLOCK_FUNCTION) |
106 RTC_THREAD_ANNOTATION_ATTRIBUTE__(unlock_function(__VA_ARGS__)) | 126 #define UNLOCK_FUNCTION(...) \ |
| 127 THREAD_ANNOTATION_ATTRIBUTE__(unlock_function(__VA_ARGS__)) |
| 128 #endif |
107 | 129 |
108 // An escape hatch for thread safety analysis to ignore the annotated function. | 130 // An escape hatch for thread safety analysis to ignore the annotated function. |
109 #if !defined(NO_THREAD_SAFETY_ANALYSIS) | 131 #if !defined(NO_THREAD_SAFETY_ANALYSIS) |
110 #define NO_THREAD_SAFETY_ANALYSIS \ | 132 #define NO_THREAD_SAFETY_ANALYSIS \ |
111 RTC_THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis) | 133 THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis) |
112 #endif | 134 #endif |
113 | 135 |
114 #endif // WEBRTC_RTC_BASE_THREAD_ANNOTATIONS_H_ | 136 #endif // WEBRTC_RTC_BASE_THREAD_ANNOTATIONS_H_ |
OLD | NEW |