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 expect a lock to be held on entry to the function, 47 * and release it in the body of the function. 48 */ 49 #define UNLOCK_FUNCTION(...) __attribute__((unlock_function(__VA_ARGS__))) 50 51 /* Documents functions that try to acquire a lock, and return success or failure. */ 52 #define EXCLUSIVE_TRYLOCK_FUNCTION(...) \ 53 __attribute__((exclusive_trylock_function(__VA_ARGS__))) 54 55 /* Documents a function that expects a mutex to be held prior to entry. */ 56 #define EXCLUSIVE_LOCKS_REQUIRED(...) __attribute__((exclusive_locks_required(__VA_ARGS__))) 57 58 /* Turns off thread safety checking within the body of a particular function. */ 59 #define NO_THREAD_SAFETY_ANALYSIS __attribute__((no_thread_safety_analysis)) 60 61 #else 62 63 #define GUARDED_BY(x) 64 #define PT_GUARDED_BY(x) 65 #define LOCKABLE 66 #define LOCKS_EXCLUDED(...) 67 #define LOCK_RETURNED(x) 68 #define EXCLUSIVE_LOCK_FUNCTION(...) 69 #define UNLOCK_FUNCTION(...) 70 #define EXCLUSIVE_TRYLOCK_FUNCTION(...) 71 #define EXCLUSIVE_LOCKS_REQUIRED(...) 72 #define NO_THREAD_SAFETY_ANALYSIS 73 74 #endif 75 76 /* 77 * A wrapper around the mutex implementation that allows perf to error check 78 * usage, etc. 79 */ 80 struct LOCKABLE mutex { 81 pthread_mutex_t lock; 82 }; 83 84 /* A wrapper around the condition variable implementation. */ 85 struct cond { 86 pthread_cond_t cond; 87 }; 88 89 /* Default initialize the mtx struct. */ 90 void mutex_init(struct mutex *mtx); 91 /* 92 * Initialize the mtx struct and set the process-shared rather than default 93 * process-private attribute. 94 */ 95 void mutex_init_pshared(struct mutex *mtx); 96 void mutex_destroy(struct mutex *mtx); 97 98 void mutex_lock(struct mutex *mtx) EXCLUSIVE_LOCK_FUNCTION(*mtx); 99 void mutex_unlock(struct mutex *mtx) UNLOCK_FUNCTION(*mtx); 100 /* Tries to acquire the lock and returns true on success. */ 101 bool mutex_trylock(struct mutex *mtx) EXCLUSIVE_TRYLOCK_FUNCTION(true, *mtx); 102 103 /* Default initialize the cond struct. */ 104 void cond_init(struct cond *cnd); 105 /* 106 * Initialize the cond struct and specify the process-shared rather than default 107 * process-private attribute. 108 */ 109 void cond_init_pshared(struct cond *cnd); 110 void cond_destroy(struct cond *cnd); 111 112 void cond_wait(struct cond *cnd, struct mutex *mtx) EXCLUSIVE_LOCKS_REQUIRED(mtx); 113 void cond_signal(struct cond *cnd); 114 void cond_broadcast(struct cond *cnd); 115 116 #endif /* __PERF_MUTEX_H */ 117