rcpputils  master
C++ API providing common utilities and data structures.
thread_safety_annotations.hpp
Go to the documentation of this file.
1 // Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
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 #ifndef RCPPUTILS__THREAD_SAFETY_ANNOTATIONS_HPP_
16 #define RCPPUTILS__THREAD_SAFETY_ANNOTATIONS_HPP_
17 
18 #include <mutex>
19 
35 // Prefixing all macros to avoid potential conflict with other projects.
36 #if defined(__clang__) && defined(_LIBCPP_HAS_THREAD_SAFETY_ANNOTATIONS) && (!defined(SWIG))
37 #define RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
38 #else
39 #define RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op
40 #endif
41 
48 inline const std::mutex & operator!(const std::mutex & a)
49 {
50  return a;
51 }
52 
62 #define RCPPUTILS_TSA_CAPABILITY(x) \
63  RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(capability(x))
64 
74 #define RCPPUTILS_TSA_SCOPED_CAPABILITY \
75  RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
76 
85 #define RCPPUTILS_TSA_GUARDED_BY(x) \
86  RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
87 
97 #define RCPPUTILS_TSA_PT_GUARDED_BY(x) \
98  RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x))
99 
109 #define RCPPUTILS_TSA_ACQUIRED_BEFORE(...) \
110  RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__))
111 
121 #define RCPPUTILS_TSA_ACQUIRED_AFTER(...) \
122  RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__))
123 
133 #define RCPPUTILS_TSA_REQUIRES(...) \
134  RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__))
135 
145 #define RCPPUTILS_TSA_REQUIRES_SHARED(...) \
146  RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__))
147 
156 #define RCPPUTILS_TSA_ACQUIRE(...) \
157  RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__))
158 
167 #define RCPPUTILS_TSA_ACQUIRE_SHARED(...) \
168  RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__))
169 
178 #define RCPPUTILS_TSA_RELEASE(...) \
179  RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__))
180 
189 #define RCPPUTILS_TSA_RELEASE_SHARED(...) \
190  RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__))
191 
199 #define RCPPUTILS_TSA_TRY_ACQUIRE(...) \
200  RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__))
201 
209 #define RCPPUTILS_TSA_TRY_ACQUIRE_SHARED(...) \
210  RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__))
211 
223 #define RCPPUTILS_TSA_EXCLUDES(...) \
224  RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__))
225 
233 #define RCPPUTILS_TSA_ASSERT_CAPABILITY(...) \
234  RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(__VA_ARGS__))
235 
244 #define RCPPUTILS_TSA_ASSERT_SHARED_CAPABILITY(...) \
245  RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(__VA_ARGS__))
246 
255 #define RCPPUTILS_TSA_RETURN_CAPABILITY(x) \
256  RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
257 
268 #define RCPPUTILS_TSA_NO_THREAD_SAFETY_ANALYSIS \
269  RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
270 
271 #endif // RCPPUTILS__THREAD_SAFETY_ANNOTATIONS_HPP_
std::mutex
operator!
const std::mutex & operator!(const std::mutex &a)
Defined for negation functionality.
Definition: thread_safety_annotations.hpp:48