1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <mutex> 20 21 #define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x)) 22 23 #define CAPABILITY(x) \ 24 THREAD_ANNOTATION_ATTRIBUTE__(capability(x)) 25 26 #define SCOPED_CAPABILITY \ 27 THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable) 28 29 #define SHARED_CAPABILITY(...) \ 30 THREAD_ANNOTATION_ATTRIBUTE__(shared_capability(__VA_ARGS__)) 31 32 #define GUARDED_BY(x) \ 33 THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x)) 34 35 #define PT_GUARDED_BY(x) \ 36 THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x)) 37 38 #define EXCLUSIVE_LOCKS_REQUIRED(...) \ 39 THREAD_ANNOTATION_ATTRIBUTE__(exclusive_locks_required(__VA_ARGS__)) 40 41 #define SHARED_LOCKS_REQUIRED(...) \ 42 THREAD_ANNOTATION_ATTRIBUTE__(shared_locks_required(__VA_ARGS__)) 43 44 #define ACQUIRED_BEFORE(...) \ 45 THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__)) 46 47 #define ACQUIRED_AFTER(...) \ 48 THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__)) 49 50 #define REQUIRES(...) \ 51 THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__)) 52 53 #define REQUIRES_SHARED(...) \ 54 THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__)) 55 56 #define ACQUIRE(...) \ 57 THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__)) 58 59 #define ACQUIRE_SHARED(...) \ 60 THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__)) 61 62 #define RELEASE(...) \ 63 THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__)) 64 65 #define RELEASE_SHARED(...) \ 66 THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__)) 67 68 #define TRY_ACQUIRE(...) \ 69 THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__)) 70 71 #define TRY_ACQUIRE_SHARED(...) \ 72 THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__)) 73 74 #define EXCLUDES(...) \ 75 THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__)) 76 77 #define ASSERT_CAPABILITY(x) \ 78 THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x)) 79 80 #define ASSERT_SHARED_CAPABILITY(x) \ 81 THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x)) 82 83 #define RETURN_CAPABILITY(x) \ 84 THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x)) 85 86 #define EXCLUSIVE_LOCK_FUNCTION(...) \ 87 THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock_function(__VA_ARGS__)) 88 89 #define EXCLUSIVE_TRYLOCK_FUNCTION(...) \ 90 THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock_function(__VA_ARGS__)) 91 92 #define SHARED_LOCK_FUNCTION(...) \ 93 THREAD_ANNOTATION_ATTRIBUTE__(shared_lock_function(__VA_ARGS__)) 94 95 #define SHARED_TRYLOCK_FUNCTION(...) \ 96 THREAD_ANNOTATION_ATTRIBUTE__(shared_trylock_function(__VA_ARGS__)) 97 98 #define UNLOCK_FUNCTION(...) \ 99 THREAD_ANNOTATION_ATTRIBUTE__(unlock_function(__VA_ARGS__)) 100 101 #define SCOPED_LOCKABLE \ 102 THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable) 103 104 #define LOCK_RETURNED(x) \ 105 THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x)) 106 107 #define NO_THREAD_SAFETY_ANALYSIS \ 108 THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis) 109 110 namespace android { 111 namespace base { 112 113 // A class to help thread safety analysis deal with std::unique_lock and condition_variable. 114 // 115 // Clang's thread safety analysis currently doesn't perform alias analysis, so movable types 116 // like std::unique_lock can't be marked with thread safety annotations. This helper allows 117 // for manual assertion of lock state in a scope. 118 // 119 // For example: 120 // 121 // std::mutex mutex; 122 // std::condition_variable cv; 123 // std::vector<int> vec GUARDED_BY(mutex); 124 // 125 // int pop() { 126 // std::unique_lock lock(mutex); 127 // ScopedLockAssertion lock_assertion(mutex); 128 // cv.wait(lock, []() { 129 // ScopedLockAssertion lock_assertion(mutex); 130 // return !vec.empty(); 131 // }); 132 // 133 // int result = vec.back(); 134 // vec.pop_back(); 135 // return result; 136 // } 137 class SCOPED_CAPABILITY ScopedLockAssertion { 138 public: ScopedLockAssertion(std::mutex & mutex)139 ScopedLockAssertion(std::mutex& mutex) ACQUIRE(mutex) {} RELEASE()140 ~ScopedLockAssertion() RELEASE() {} 141 }; 142 143 } // namespace base 144 } // namespace android 145