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