• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Marl Authors.
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 //     https://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 // Stubs Thread-Safty-Analysis annotation macros for platforms that do not
16 // support them.
17 // See https://clang.llvm.org/docs/ThreadSafetyAnalysis.html
18 
19 #ifndef marl_tsa_h
20 #define marl_tsa_h
21 
22 // Enable thread safety attributes only with clang.
23 // The attributes can be safely erased when compiling with other compilers.
24 #if defined(__clang__) && (!defined(SWIG))
25 #define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
26 #else
27 #define THREAD_ANNOTATION_ATTRIBUTE__(x)  // no-op
28 #endif
29 
30 #define CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(capability(x))
31 
32 #define SCOPED_CAPABILITY THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
33 
34 #define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
35 
36 #define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x))
37 
38 #define ACQUIRED_BEFORE(...) \
39   THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__))
40 
41 #define ACQUIRED_AFTER(...) \
42   THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__))
43 
44 #define REQUIRES(...) \
45   THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__))
46 
47 #define REQUIRES_SHARED(...) \
48   THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__))
49 
50 #define ACQUIRE(...) \
51   THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__))
52 
53 #define ACQUIRE_SHARED(...) \
54   THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__))
55 
56 #define RELEASE(...) \
57   THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__))
58 
59 #define RELEASE_SHARED(...) \
60   THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__))
61 
62 #define TRY_ACQUIRE(...) \
63   THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__))
64 
65 #define TRY_ACQUIRE_SHARED(...) \
66   THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__))
67 
68 #define EXCLUDES(...) THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__))
69 
70 #define ASSERT_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x))
71 
72 #define ASSERT_SHARED_CAPABILITY(x) \
73   THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x))
74 
75 #define RETURN_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
76 
77 #define NO_THREAD_SAFETY_ANALYSIS \
78   THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
79 
80 #endif  // marl_tsa_h
81