1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 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 16 #ifndef PANDA_LIBPANDABASE_CLANG_H_ 17 #define PANDA_LIBPANDABASE_CLANG_H_ 18 19 // Based on Thread safety analysis annotations taken from https://clang.llvm.org/docs/ThreadSafetyAnalysis.html 20 21 // Enable thread safety attributes only with clang. 22 // The attributes can be safely erased when compiling with other compilers. 23 #if defined(__clang__) && (!defined(SWIG)) 24 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 25 #define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x)) 26 #else 27 #define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op 28 #endif 29 30 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 31 #define CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(capability(x)) 32 33 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 34 #define SHARED_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(shared_capability(x)) 35 36 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 37 #define LOCKABLE THREAD_ANNOTATION_ATTRIBUTE__(lockable) 38 39 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 40 #define SCOPED_CAPABILITY THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable) 41 42 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 43 #define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x)) 44 45 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 46 #define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x)) 47 48 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 49 #define ACQUIRED_BEFORE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__)) 50 51 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 52 #define ACQUIRED_AFTER(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__)) 53 54 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 55 #define REQUIRES(...) THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__)) 56 57 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 58 #define REQUIRES_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__)) 59 60 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 61 #define ACQUIRE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__)) 62 63 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 64 #define ACQUIRE_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__)) 65 66 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 67 #define RELEASE(...) THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__)) 68 69 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 70 #define RELEASE_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__)) 71 72 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 73 #define RELEASE_GENERIC(...) THREAD_ANNOTATION_ATTRIBUTE__(release_generic_capability(__VA_ARGS__)) 74 75 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 76 #define TRY_ACQUIRE(...) THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__)) 77 78 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 79 #define TRY_ACQUIRE_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__)) 80 81 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 82 #define EXCLUDES(...) THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__)) 83 84 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 85 #define ASSERT_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x)) 86 87 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 88 #define ASSERT_SHARED_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x)) 89 90 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 91 #define RETURN_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x)) 92 93 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 94 #define NO_THREAD_SAFETY_ANALYSIS THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis) 95 96 #endif // PANDA_LIBPANDABASE_CLANG_H_ 97