andresp@webrtc.org | c9faf10 | 2013-12-20 20:20:50 | [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 | |
noahric | 890b96c | 2016-12-12 21:36:46 | [diff] [blame] | 19 | #ifndef WEBRTC_BASE_THREAD_ANNOTATIONS_H_ |
| 20 | #define WEBRTC_BASE_THREAD_ANNOTATIONS_H_ |
andresp@webrtc.org | c9faf10 | 2013-12-20 20:20:50 | [diff] [blame] | 21 | |
| 22 | #if defined(__clang__) && (!defined(SWIG)) |
| 23 | #define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x)) |
| 24 | #else |
| 25 | #define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op |
| 26 | #endif |
| 27 | |
| 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. |
| 33 | #define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x)) |
danilchap | 507d895 | 2016-10-24 13:07:25 | [diff] [blame] | 34 | #define GUARDED_VAR THREAD_ANNOTATION_ATTRIBUTE__(guarded_var) |
andresp@webrtc.org | c9faf10 | 2013-12-20 20:20:50 | [diff] [blame] | 35 | |
| 36 | // Document if the memory location pointed to by a pointer should be guarded |
| 37 | // by a lock when dereferencing the pointer. Similar to GUARDED_VAR, |
| 38 | // PT_GUARDED_VAR is primarily used when the client cannot express the name |
| 39 | // of the lock. Note that a pointer variable to a shared memory location |
| 40 | // could itself be a shared variable. For example, if a shared global pointer |
| 41 | // q, which is guarded by mu1, points to a shared memory location that is |
| 42 | // guarded by mu2, q should be annotated as follows: |
| 43 | // int *q GUARDED_BY(mu1) PT_GUARDED_BY(mu2); |
danilchap | 507d895 | 2016-10-24 13:07:25 | [diff] [blame] | 44 | #define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x)) |
| 45 | #define PT_GUARDED_VAR THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_var) |
andresp@webrtc.org | c9faf10 | 2013-12-20 20:20:50 | [diff] [blame] | 46 | |
| 47 | // Document the acquisition order between locks that can be held |
| 48 | // simultaneously by a thread. For any two locks that need to be annotated |
| 49 | // to establish an acquisition order, only one of them needs the annotation. |
| 50 | // (i.e. You don't have to annotate both locks with both ACQUIRED_AFTER |
| 51 | // and ACQUIRED_BEFORE.) |
| 52 | #define ACQUIRED_AFTER(x) THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(x)) |
| 53 | #define ACQUIRED_BEFORE(x) THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(x)) |
| 54 | |
| 55 | // The following three annotations document the lock requirements for |
| 56 | // functions/methods. |
| 57 | |
| 58 | // Document if a function expects certain locks to be held before it is called |
| 59 | #define EXCLUSIVE_LOCKS_REQUIRED(...) \ |
| 60 | THREAD_ANNOTATION_ATTRIBUTE__(exclusive_locks_required(__VA_ARGS__)) |
andresp@webrtc.org | c9faf10 | 2013-12-20 20:20:50 | [diff] [blame] | 61 | |
| 62 | #define SHARED_LOCKS_REQUIRED(...) \ |
| 63 | THREAD_ANNOTATION_ATTRIBUTE__(shared_locks_required(__VA_ARGS__)) |
| 64 | |
| 65 | // Document the locks acquired in the body of the function. These locks |
| 66 | // cannot be held when calling this function (as google3's Mutex locks are |
| 67 | // non-reentrant). |
danilchap | 507d895 | 2016-10-24 13:07:25 | [diff] [blame] | 68 | #define LOCKS_EXCLUDED(...) \ |
| 69 | THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__)) |
andresp@webrtc.org | c9faf10 | 2013-12-20 20:20:50 | [diff] [blame] | 70 | |
| 71 | // Document the lock the annotated function returns without acquiring it. |
| 72 | #define LOCK_RETURNED(x) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x)) |
| 73 | |
| 74 | // Document if a class/type is a lockable type (such as the Mutex class). |
| 75 | #define LOCKABLE THREAD_ANNOTATION_ATTRIBUTE__(lockable) |
| 76 | |
| 77 | // Document if a class is a scoped lockable type (such as the MutexLock class). |
| 78 | #define SCOPED_LOCKABLE THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable) |
| 79 | |
| 80 | // The following annotations specify lock and unlock primitives. |
| 81 | #define EXCLUSIVE_LOCK_FUNCTION(...) \ |
| 82 | THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock_function(__VA_ARGS__)) |
| 83 | |
| 84 | #define SHARED_LOCK_FUNCTION(...) \ |
| 85 | THREAD_ANNOTATION_ATTRIBUTE__(shared_lock_function(__VA_ARGS__)) |
| 86 | |
| 87 | #define EXCLUSIVE_TRYLOCK_FUNCTION(...) \ |
| 88 | THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock_function(__VA_ARGS__)) |
| 89 | |
| 90 | #define SHARED_TRYLOCK_FUNCTION(...) \ |
| 91 | THREAD_ANNOTATION_ATTRIBUTE__(shared_trylock_function(__VA_ARGS__)) |
| 92 | |
| 93 | #define UNLOCK_FUNCTION(...) \ |
| 94 | THREAD_ANNOTATION_ATTRIBUTE__(unlock_function(__VA_ARGS__)) |
| 95 | |
| 96 | // An escape hatch for thread safety analysis to ignore the annotated function. |
| 97 | #define NO_THREAD_SAFETY_ANALYSIS \ |
| 98 | THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis) |
| 99 | |
noahric | 890b96c | 2016-12-12 21:36:46 | [diff] [blame] | 100 | #endif // WEBRTC_BASE_THREAD_ANNOTATIONS_H_ |