1 // Copyright 2021 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Copyright 2018 The Fuchsia Authors. All rights reserved. 16 // Use of this source code is governed by a BSD-style license that can be 17 // found in the LICENSE file. 18 19 #pragma once 20 21 // Other libraries (e.g. libbase) may have already defined these symbols. 22 // Only define them if they are not defined elsewhere. 23 24 // Thread-safety annotations. 25 // Currently these are only supported on Clang. 26 #ifndef THREAD_ANNOTATION_ATTRIBUTE__ 27 #if defined(__clang__) && defined(_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS) && \ 28 __has_attribute(acquire_capability) 29 #define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x)) 30 #else 31 #define THREAD_ANNOTATION_ATTRIBUTE__(x) 32 #endif 33 #endif // THREAD_ANNOTATION_ATTRIBUTE__ 34 35 #ifndef CAPABILITY 36 #define CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(__capability__(x)) 37 #endif // CAPABILITY 38 39 #ifndef GUARDED_BY 40 #define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(__guarded_by__(x)) 41 #endif // GUARDED_BY 42 43 #ifndef ACQUIRE 44 #define ACQUIRE(...) THREAD_ANNOTATION_ATTRIBUTE__(__acquire_capability__(__VA_ARGS__)) 45 #endif // ACQUIRE 46 47 #ifndef TRY_ACQUIRE 48 #define TRY_ACQUIRE(...) THREAD_ANNOTATION_ATTRIBUTE__(__try_acquire_capability__(__VA_ARGS__)) 49 #endif // TRY_ACQUIRE 50 51 #ifndef ACQUIRED_BEFORE 52 #define ACQUIRED_BEFORE(...) THREAD_ANNOTATION_ATTRIBUTE__(__acquired_before__(__VA_ARGS__)) 53 #endif // ACQUIRED_BEFORE 54 55 #ifndef ACQUIRED_AFTER 56 #define ACQUIRED_AFTER(...) THREAD_ANNOTATION_ATTRIBUTE__(__acquired_after__(__VA_ARGS__)) 57 #endif // ACQUIRED_AFTER 58 59 #ifndef RELEASE 60 #define RELEASE(...) THREAD_ANNOTATION_ATTRIBUTE__(__release_capability__(__VA_ARGS__)) 61 #endif // RELEASE 62 63 #ifndef REQUIRES 64 #define REQUIRES(...) THREAD_ANNOTATION_ATTRIBUTE__(__requires_capability__(__VA_ARGS__)) 65 #endif // REQUIRES 66 67 #ifndef EXCLUDES 68 #define EXCLUDES(...) THREAD_ANNOTATION_ATTRIBUTE__(__locks_excluded__(__VA_ARGS__)) 69 #endif // EXCLUDES 70 71 #ifndef RETURN_CAPABILITY 72 #define RETURN_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(__lock_returned__(x)) 73 #endif // RETURN_CAPABILITY 74 75 #ifndef SCOPED_CAPABILITY 76 #define SCOPED_CAPABILITY THREAD_ANNOTATION_ATTRIBUTE__(__scoped_lockable__) 77 #endif // SCOPED_CAPABILITY 78 79 #ifndef NO_THREAD_SAFETY_ANALYSIS 80 #define NO_THREAD_SAFETY_ANALYSIS THREAD_ANNOTATION_ATTRIBUTE__(__no_thread_safety_analysis__) 81 #endif // NO_THREAD_SAFETY_ANALYSIS 82