xref: /linux/tools/perf/util/mutex.h (revision e78f70bad29c5ae1e1076698b690b15794e9b81e)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_MUTEX_H
3 #define __PERF_MUTEX_H
4 
5 #include <pthread.h>
6 #include <stdbool.h>
7 
8 /*
9  * A function-like feature checking macro that is a wrapper around
10  * `__has_attribute`, which is defined by GCC 5+ and Clang and evaluates to a
11  * nonzero constant integer if the attribute is supported or 0 if not.
12  */
13 #ifdef __has_attribute
14 #define HAVE_ATTRIBUTE(x) __has_attribute(x)
15 #else
16 #define HAVE_ATTRIBUTE(x) 0
17 #endif
18 
19 #if HAVE_ATTRIBUTE(guarded_by) && HAVE_ATTRIBUTE(pt_guarded_by) && \
20 	HAVE_ATTRIBUTE(lockable) && HAVE_ATTRIBUTE(exclusive_lock_function) && \
21 	HAVE_ATTRIBUTE(exclusive_trylock_function) && HAVE_ATTRIBUTE(exclusive_locks_required) && \
22 	HAVE_ATTRIBUTE(no_thread_safety_analysis)
23 
24 /* Documents if a shared field or global variable needs to be protected by a mutex. */
25 #define GUARDED_BY(x) __attribute__((guarded_by(x)))
26 
27 /*
28  * Documents if the memory location pointed to by a pointer should be guarded by
29  * a mutex when dereferencing the pointer.
30  */
31 #define PT_GUARDED_BY(x) __attribute__((pt_guarded_by(x)))
32 
33 /* Documents if a type is a lockable type. */
34 #define LOCKABLE __attribute__((lockable))
35 
36 /* Documents a function that expects a lock not to be held prior to entry. */
37 #define LOCKS_EXCLUDED(...) __attribute__((locks_excluded(__VA_ARGS__)))
38 
39 /* Documents a function that returns a lock. */
40 #define LOCK_RETURNED(x) __attribute__((lock_returned(x)))
41 
42 /* Documents functions that acquire a lock in the body of a function, and do not release it. */
43 #define EXCLUSIVE_LOCK_FUNCTION(...)  __attribute__((exclusive_lock_function(__VA_ARGS__)))
44 
45 /*
46  * Documents functions that acquire a shared (reader) lock in the body of a
47  * function, and do not release it.
48  */
49 #define SHARED_LOCK_FUNCTION(...)  __attribute__((shared_lock_function(__VA_ARGS__)))
50 
51 /*
52  * Documents functions that expect a lock to be held on entry to the function,
53  * and release it in the body of the function.
54  */
55 #define UNLOCK_FUNCTION(...) __attribute__((unlock_function(__VA_ARGS__)))
56 
57 /* Documents functions that try to acquire a lock, and return success or failure. */
58 #define EXCLUSIVE_TRYLOCK_FUNCTION(...) \
59 	__attribute__((exclusive_trylock_function(__VA_ARGS__)))
60 
61 /* Documents a function that expects a mutex to be held prior to entry. */
62 #define EXCLUSIVE_LOCKS_REQUIRED(...) __attribute__((exclusive_locks_required(__VA_ARGS__)))
63 
64 /* Documents a function that expects a shared (reader) lock to be held prior to entry. */
65 #define SHARED_LOCKS_REQUIRED(...) __attribute__((shared_locks_required(__VA_ARGS__)))
66 
67 /* Turns off thread safety checking within the body of a particular function. */
68 #define NO_THREAD_SAFETY_ANALYSIS __attribute__((no_thread_safety_analysis))
69 
70 #else
71 
72 #define GUARDED_BY(x)
73 #define PT_GUARDED_BY(x)
74 #define LOCKABLE
75 #define LOCKS_EXCLUDED(...)
76 #define LOCK_RETURNED(x)
77 #define EXCLUSIVE_LOCK_FUNCTION(...)
78 #define SHARED_LOCK_FUNCTION(...)
79 #define UNLOCK_FUNCTION(...)
80 #define EXCLUSIVE_TRYLOCK_FUNCTION(...)
81 #define EXCLUSIVE_LOCKS_REQUIRED(...)
82 #define SHARED_LOCKS_REQUIRED(...)
83 #define NO_THREAD_SAFETY_ANALYSIS
84 
85 #endif
86 
87 /*
88  * A wrapper around the mutex implementation that allows perf to error check
89  * usage, etc.
90  */
91 struct LOCKABLE mutex {
92 	pthread_mutex_t lock;
93 };
94 
95 /* A wrapper around the condition variable implementation. */
96 struct cond {
97 	pthread_cond_t cond;
98 };
99 
100 /* Default initialize the mtx struct. */
101 void mutex_init(struct mutex *mtx);
102 /*
103  * Initialize the mtx struct and set the process-shared rather than default
104  * process-private attribute.
105  */
106 void mutex_init_pshared(struct mutex *mtx);
107 void mutex_destroy(struct mutex *mtx);
108 
109 void mutex_lock(struct mutex *mtx) EXCLUSIVE_LOCK_FUNCTION(*mtx);
110 void mutex_unlock(struct mutex *mtx) UNLOCK_FUNCTION(*mtx);
111 /* Tries to acquire the lock and returns true on success. */
112 bool mutex_trylock(struct mutex *mtx) EXCLUSIVE_TRYLOCK_FUNCTION(true, *mtx);
113 
114 /* Default initialize the cond struct. */
115 void cond_init(struct cond *cnd);
116 /*
117  * Initialize the cond struct and specify the process-shared rather than default
118  * process-private attribute.
119  */
120 void cond_init_pshared(struct cond *cnd);
121 void cond_destroy(struct cond *cnd);
122 
123 void cond_wait(struct cond *cnd, struct mutex *mtx) EXCLUSIVE_LOCKS_REQUIRED(mtx);
124 void cond_signal(struct cond *cnd);
125 void cond_broadcast(struct cond *cnd);
126 
127 #endif /* __PERF_MUTEX_H */
128