Pigweed
 
Loading...
Searching...
No Matches
lock_annotations.h
1// Copyright 2021 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// 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, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14//
15// This header file contains macro definitions for thread safety annotations
16// that allow developers to document the locking policies of multi-threaded
17// code. The annotations can also help program analysis tools to identify
18// potential thread safety issues.
19//
20// These annotations are implemented using compiler attributes. Using the macros
21// defined here instead of raw attributes allow for portability and future
22// compatibility.
23//
24// The thread safety analysis system is documented at
25// http://clang.llvm.org/docs/ThreadSafetyAnalysis.html
26//
27// When referring to locks in the arguments of the attributes, you should
28// use variable names or more complex expressions (e.g. my_object->lock_)
29// that evaluate to a concrete lock object whenever possible. If the lock
30// you want to refer to is not in scope, you may use a member pointer
31// (e.g. &MyClass::lock_) to refer to a lock in some (unknown) object.
32
33#pragma once
34
35#include "pw_preprocessor/compiler.h"
36
57#if PW_HAVE_ATTRIBUTE(guarded_by)
58#define PW_GUARDED_BY(x) __attribute__((guarded_by(x)))
59#else
60#define PW_GUARDED_BY(x)
61#endif
62
88#if PW_HAVE_ATTRIBUTE(pt_guarded_by)
89#define PW_PT_GUARDED_BY(x) __attribute__((pt_guarded_by(x)))
90#else
91#define PW_PT_GUARDED_BY(x)
92#endif
93
112#if PW_HAVE_ATTRIBUTE(acquired_after)
113#define PW_ACQUIRED_AFTER(...) __attribute__((acquired_after(__VA_ARGS__)))
114#else
115#define PW_ACQUIRED_AFTER(...)
116#endif
117
118#if PW_HAVE_ATTRIBUTE(acquired_before)
119#define PW_ACQUIRED_BEFORE(...) __attribute__((acquired_before(__VA_ARGS__)))
120#else
121#define PW_ACQUIRED_BEFORE(...)
122#endif
123
150#if PW_HAVE_ATTRIBUTE(exclusive_locks_required)
151#define PW_EXCLUSIVE_LOCKS_REQUIRED(...) \
152 __attribute__((exclusive_locks_required(__VA_ARGS__)))
153#else
154#define PW_EXCLUSIVE_LOCKS_REQUIRED(...)
155#endif
156
157#if PW_HAVE_ATTRIBUTE(shared_locks_required)
158#define PW_SHARED_LOCKS_REQUIRED(...) \
159 __attribute__((shared_locks_required(__VA_ARGS__)))
160#else
161#define PW_SHARED_LOCKS_REQUIRED(...)
162#endif
163
183#if PW_HAVE_ATTRIBUTE(locks_excluded)
184#define PW_LOCKS_EXCLUDED(...) __attribute__((locks_excluded(__VA_ARGS__)))
185#else
186#define PW_LOCKS_EXCLUDED(...)
187#endif
188
206#if PW_HAVE_ATTRIBUTE(lock_returned)
207#define PW_LOCK_RETURNED(x) __attribute__((lock_returned(x)))
208#else
209#define PW_LOCK_RETURNED(x)
210#endif
211
217#if PW_HAVE_ATTRIBUTE(capability)
218#define PW_LOCKABLE(name) __attribute__((capability(name)))
219#elif PW_HAVE_ATTRIBUTE(lockable)
220#define PW_LOCKABLE(name) __attribute__((lockable))
221#else
222#define PW_LOCKABLE(name)
223#endif
224
234#if PW_HAVE_ATTRIBUTE(scoped_lockable)
235#define PW_SCOPED_LOCKABLE __attribute__((scoped_lockable))
236#else
237#define PW_SCOPED_LOCKABLE
238#endif
239
244#if PW_HAVE_ATTRIBUTE(exclusive_lock_function)
245#define PW_EXCLUSIVE_LOCK_FUNCTION(...) \
246 __attribute__((exclusive_lock_function(__VA_ARGS__)))
247#else
248#define PW_EXCLUSIVE_LOCK_FUNCTION(...)
249#endif
250
255#if PW_HAVE_ATTRIBUTE(shared_lock_function)
256#define PW_SHARED_LOCK_FUNCTION(...) \
257 __attribute__((shared_lock_function(__VA_ARGS__)))
258#else
259#define PW_SHARED_LOCK_FUNCTION(...)
260#endif
261
266#if PW_HAVE_ATTRIBUTE(unlock_function)
267#define PW_UNLOCK_FUNCTION(...) __attribute__((unlock_function(__VA_ARGS__)))
268#else
269#define PW_UNLOCK_FUNCTION(...)
270#endif
271
281#if PW_HAVE_ATTRIBUTE(exclusive_trylock_function)
282#define PW_EXCLUSIVE_TRYLOCK_FUNCTION(...) \
283 __attribute__((exclusive_trylock_function(__VA_ARGS__)))
284#else
285#define PW_EXCLUSIVE_TRYLOCK_FUNCTION(...)
286#endif
287
288#if PW_HAVE_ATTRIBUTE(shared_trylock_function)
289#define PW_SHARED_TRYLOCK_FUNCTION(...) \
290 __attribute__((shared_trylock_function(__VA_ARGS__)))
291#else
292#define PW_SHARED_TRYLOCK_FUNCTION(...)
293#endif
294
300#if PW_HAVE_ATTRIBUTE(assert_exclusive_lock)
301#define PW_ASSERT_EXCLUSIVE_LOCK(...) \
302 __attribute__((assert_exclusive_lock(__VA_ARGS__)))
303#else
304#define PW_ASSERT_EXCLUSIVE_LOCK(...)
305#endif
306
307#if PW_HAVE_ATTRIBUTE(assert_shared_lock)
308#define PW_ASSERT_SHARED_LOCK(...) \
309 __attribute__((assert_shared_lock(__VA_ARGS__)))
310#else
311#define PW_ASSERT_SHARED_LOCK(...)
312#endif
313
319#if PW_HAVE_ATTRIBUTE(no_thread_safety_analysis)
320#define PW_NO_LOCK_SAFETY_ANALYSIS __attribute__((no_thread_safety_analysis))
321#else
322#define PW_NO_LOCK_SAFETY_ANALYSIS
323#endif