1b1994897Sopenharmony_ci/**
2b1994897Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3b1994897Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4b1994897Sopenharmony_ci * you may not use this file except in compliance with the License.
5b1994897Sopenharmony_ci * You may obtain a copy of the License at
6b1994897Sopenharmony_ci *
7b1994897Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8b1994897Sopenharmony_ci *
9b1994897Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10b1994897Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11b1994897Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b1994897Sopenharmony_ci * See the License for the specific language governing permissions and
13b1994897Sopenharmony_ci * limitations under the License.
14b1994897Sopenharmony_ci */
15b1994897Sopenharmony_ci
16b1994897Sopenharmony_ci#ifndef LIBPANDABASE_CLANG_H
17b1994897Sopenharmony_ci#define LIBPANDABASE_CLANG_H
18b1994897Sopenharmony_ci
19b1994897Sopenharmony_ci// Based on Thread safety analysis annotations taken from https://clang.llvm.org/docs/ThreadSafetyAnalysis.html
20b1994897Sopenharmony_ci
21b1994897Sopenharmony_ci// Enable thread safety attributes only with clang.
22b1994897Sopenharmony_ci// The attributes can be safely erased when compiling with other compilers.
23b1994897Sopenharmony_ci#if defined(__clang__) && (!defined(SWIG))
24b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
25b1994897Sopenharmony_ci#define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
26b1994897Sopenharmony_ci#else
27b1994897Sopenharmony_ci#define THREAD_ANNOTATION_ATTRIBUTE__(x)  // no-op
28b1994897Sopenharmony_ci#endif
29b1994897Sopenharmony_ci
30b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
31b1994897Sopenharmony_ci#define CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(capability(x))
32b1994897Sopenharmony_ci
33b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
34b1994897Sopenharmony_ci#define SHARED_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(shared_capability(x))
35b1994897Sopenharmony_ci
36b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
37b1994897Sopenharmony_ci#define LOCKABLE  THREAD_ANNOTATION_ATTRIBUTE__(lockable)
38b1994897Sopenharmony_ci
39b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
40b1994897Sopenharmony_ci#define SCOPED_CAPABILITY THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
41b1994897Sopenharmony_ci
42b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
43b1994897Sopenharmony_ci#define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
44b1994897Sopenharmony_ci
45b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
46b1994897Sopenharmony_ci#define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x))
47b1994897Sopenharmony_ci
48b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
49b1994897Sopenharmony_ci#define ACQUIRED_BEFORE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__))
50b1994897Sopenharmony_ci
51b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
52b1994897Sopenharmony_ci#define ACQUIRED_AFTER(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__))
53b1994897Sopenharmony_ci
54b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
55b1994897Sopenharmony_ci#define REQUIRES(...) THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__))
56b1994897Sopenharmony_ci
57b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
58b1994897Sopenharmony_ci#define REQUIRES_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__))
59b1994897Sopenharmony_ci
60b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
61b1994897Sopenharmony_ci#define ACQUIRE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__))
62b1994897Sopenharmony_ci
63b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
64b1994897Sopenharmony_ci#define ACQUIRE_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__))
65b1994897Sopenharmony_ci
66b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
67b1994897Sopenharmony_ci#define RELEASE(...) THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__))
68b1994897Sopenharmony_ci
69b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
70b1994897Sopenharmony_ci#define RELEASE_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__))
71b1994897Sopenharmony_ci
72b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
73b1994897Sopenharmony_ci#define RELEASE_GENERIC(...) THREAD_ANNOTATION_ATTRIBUTE__(release_generic_capability(__VA_ARGS__))
74b1994897Sopenharmony_ci
75b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
76b1994897Sopenharmony_ci#define TRY_ACQUIRE(...) THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__))
77b1994897Sopenharmony_ci
78b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
79b1994897Sopenharmony_ci#define TRY_ACQUIRE_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__))
80b1994897Sopenharmony_ci
81b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
82b1994897Sopenharmony_ci#define EXCLUDES(...) THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__))
83b1994897Sopenharmony_ci
84b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
85b1994897Sopenharmony_ci#define ASSERT_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x))
86b1994897Sopenharmony_ci
87b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
88b1994897Sopenharmony_ci#define ASSERT_SHARED_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x))
89b1994897Sopenharmony_ci
90b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
91b1994897Sopenharmony_ci#define RETURN_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
92b1994897Sopenharmony_ci
93b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
94b1994897Sopenharmony_ci#define NO_THREAD_SAFETY_ANALYSIS THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
95b1994897Sopenharmony_ci
96b1994897Sopenharmony_ci#endif  // LIBPANDABASE_CLANG_H
97