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 | 42a70e3 | 2017-09-06 01:38:35 -0700 | [diff] [blame] | 23 | #define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x)) |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 24 | #else |
danilchap | 42a70e3 | 2017-09-06 01:38:35 -0700 | [diff] [blame] | 25 | #define 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 | 42a70e3 | 2017-09-06 01:38:35 -0700 | [diff] [blame] | 34 | #define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x)) |
Chris Mumford | 0ed6de4 | 2017-08-14 14:33:58 -0700 | [diff] [blame] | 35 | #endif |
danilchap | 42a70e3 | 2017-09-06 01:38:35 -0700 | [diff] [blame] | 36 | #if !defined(GUARDED_VAR) |
| 37 | #define GUARDED_VAR THREAD_ANNOTATION_ATTRIBUTE__(guarded_var) |
| 38 | #endif |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 39 | |
| 40 | // Document if the memory location pointed to by a pointer should be guarded |
| 41 | // by a lock when dereferencing the pointer. Similar to GUARDED_VAR, |
| 42 | // PT_GUARDED_VAR is primarily used when the client cannot express the name |
| 43 | // of the lock. Note that a pointer variable to a shared memory location |
| 44 | // could itself be a shared variable. For example, if a shared global pointer |
| 45 | // q, which is guarded by mu1, points to a shared memory location that is |
| 46 | // guarded by mu2, q should be annotated as follows: |
| 47 | // int *q GUARDED_BY(mu1) PT_GUARDED_BY(mu2); |
danilchap | 42a70e3 | 2017-09-06 01:38:35 -0700 | [diff] [blame] | 48 | #if !defined(PT_GUARDED_BY) |
| 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 |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 54 | |
| 55 | // Document the acquisition order between locks that can be held |
| 56 | // simultaneously by a thread. For any two locks that need to be annotated |
| 57 | // to establish an acquisition order, only one of them needs the annotation. |
| 58 | // (i.e. You don't have to annotate both locks with both ACQUIRED_AFTER |
| 59 | // and ACQUIRED_BEFORE.) |
Chris Mumford | 0ed6de4 | 2017-08-14 14:33:58 -0700 | [diff] [blame] | 60 | #if !defined(ACQUIRED_AFTER) |
danilchap | 42a70e3 | 2017-09-06 01:38:35 -0700 | [diff] [blame] | 61 | #define ACQUIRED_AFTER(x) THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(x)) |
Chris Mumford | 0ed6de4 | 2017-08-14 14:33:58 -0700 | [diff] [blame] | 62 | #endif |
| 63 | #if !defined(ACQUIRED_BEFORE) |
danilchap | 42a70e3 | 2017-09-06 01:38:35 -0700 | [diff] [blame] | 64 | #define ACQUIRED_BEFORE(x) THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(x)) |
Chris Mumford | 0ed6de4 | 2017-08-14 14:33:58 -0700 | [diff] [blame] | 65 | #endif |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 66 | |
| 67 | // The following three annotations document the lock requirements for |
| 68 | // functions/methods. |
| 69 | |
| 70 | // 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] | 71 | #if !defined(EXCLUSIVE_LOCKS_REQUIRED) |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 72 | #define EXCLUSIVE_LOCKS_REQUIRED(...) \ |
danilchap | 42a70e3 | 2017-09-06 01:38:35 -0700 | [diff] [blame] | 73 | THREAD_ANNOTATION_ATTRIBUTE__(exclusive_locks_required(__VA_ARGS__)) |
Chris Mumford | 0ed6de4 | 2017-08-14 14:33:58 -0700 | [diff] [blame] | 74 | #endif |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 75 | |
Chris Mumford | 0ed6de4 | 2017-08-14 14:33:58 -0700 | [diff] [blame] | 76 | #if !defined(SHARED_LOCKS_REQUIRED) |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 77 | #define SHARED_LOCKS_REQUIRED(...) \ |
danilchap | 42a70e3 | 2017-09-06 01:38:35 -0700 | [diff] [blame] | 78 | THREAD_ANNOTATION_ATTRIBUTE__(shared_locks_required(__VA_ARGS__)) |
Chris Mumford | 0ed6de4 | 2017-08-14 14:33:58 -0700 | [diff] [blame] | 79 | #endif |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 80 | |
| 81 | // Document the locks acquired in the body of the function. These locks |
| 82 | // cannot be held when calling this function (as google3's Mutex locks are |
| 83 | // non-reentrant). |
Chris Mumford | 0ed6de4 | 2017-08-14 14:33:58 -0700 | [diff] [blame] | 84 | #if !defined(LOCKS_EXCLUDED) |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 85 | #define LOCKS_EXCLUDED(...) \ |
danilchap | 42a70e3 | 2017-09-06 01:38:35 -0700 | [diff] [blame] | 86 | THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__)) |
Chris Mumford | 0ed6de4 | 2017-08-14 14:33:58 -0700 | [diff] [blame] | 87 | #endif |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 88 | |
| 89 | // Document the lock the annotated function returns without acquiring it. |
danilchap | 42a70e3 | 2017-09-06 01:38:35 -0700 | [diff] [blame] | 90 | #if !defined(LOCK_RETURNED) |
| 91 | #define LOCK_RETURNED(x) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x)) |
| 92 | #endif |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 93 | |
| 94 | // 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] | 95 | #if !defined(LOCKABLE) |
| 96 | #define LOCKABLE THREAD_ANNOTATION_ATTRIBUTE__(lockable) |
| 97 | #endif |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 98 | |
| 99 | // 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] | 100 | #if !defined(SCOPED_LOCKABLE) |
| 101 | #define SCOPED_LOCKABLE THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable) |
| 102 | #endif |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 103 | |
| 104 | // The following annotations specify lock and unlock primitives. |
danilchap | 42a70e3 | 2017-09-06 01:38:35 -0700 | [diff] [blame] | 105 | #if !defined(EXCLUSIVE_LOCK_FUNCTION) |
| 106 | #define EXCLUSIVE_LOCK_FUNCTION(...) \ |
| 107 | THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock_function(__VA_ARGS__)) |
| 108 | #endif |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 109 | |
danilchap | 42a70e3 | 2017-09-06 01:38:35 -0700 | [diff] [blame] | 110 | #if !defined(SHARED_LOCK_FUNCTION) |
| 111 | #define SHARED_LOCK_FUNCTION(...) \ |
| 112 | THREAD_ANNOTATION_ATTRIBUTE__(shared_lock_function(__VA_ARGS__)) |
| 113 | #endif |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 114 | |
danilchap | 42a70e3 | 2017-09-06 01:38:35 -0700 | [diff] [blame] | 115 | #if !defined(EXCLUSIVE_TRYLOCK_FUNCTION) |
| 116 | #define EXCLUSIVE_TRYLOCK_FUNCTION(...) \ |
| 117 | THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock_function(__VA_ARGS__)) |
| 118 | #endif |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 119 | |
danilchap | 42a70e3 | 2017-09-06 01:38:35 -0700 | [diff] [blame] | 120 | #if !defined(SHARED_TRYLOCK_FUNCTION) |
| 121 | #define SHARED_TRYLOCK_FUNCTION(...) \ |
| 122 | THREAD_ANNOTATION_ATTRIBUTE__(shared_trylock_function(__VA_ARGS__)) |
| 123 | #endif |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 124 | |
danilchap | 42a70e3 | 2017-09-06 01:38:35 -0700 | [diff] [blame] | 125 | #if !defined(UNLOCK_FUNCTION) |
| 126 | #define UNLOCK_FUNCTION(...) \ |
| 127 | THREAD_ANNOTATION_ATTRIBUTE__(unlock_function(__VA_ARGS__)) |
| 128 | #endif |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 129 | |
| 130 | // An escape hatch for thread safety analysis to ignore the annotated function. |
Chris Mumford | 0ed6de4 | 2017-08-14 14:33:58 -0700 | [diff] [blame] | 131 | #if !defined(NO_THREAD_SAFETY_ANALYSIS) |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 132 | #define NO_THREAD_SAFETY_ANALYSIS \ |
danilchap | 42a70e3 | 2017-09-06 01:38:35 -0700 | [diff] [blame] | 133 | THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis) |
Chris Mumford | 0ed6de4 | 2017-08-14 14:33:58 -0700 | [diff] [blame] | 134 | #endif |
andresp@webrtc.org | 7fb75ec | 2013-12-20 20:20:50 +0000 | [diff] [blame] | 135 | |
Henrik Kjellander | c036276 | 2017-06-29 08:03:04 +0200 | [diff] [blame] | 136 | #endif // WEBRTC_RTC_BASE_THREAD_ANNOTATIONS_H_ |