| 1 |
|
| 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 |
|
| 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 |
|
| 34 | #define LOCKABLE __attribute__((lockable))
|
| 35 |
|
| 36 |
|
| 37 | #define LOCKS_EXCLUDED(...) __attribute__((locks_excluded(__VA_ARGS__)))
|
| 38 |
|
| 39 |
|
| 40 | #define LOCK_RETURNED(x) __attribute__((lock_returned(x)))
|
| 41 |
|
| 42 |
|
| 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 |
|
| 58 | #define EXCLUSIVE_TRYLOCK_FUNCTION(...) \
|
| 59 | __attribute__((exclusive_trylock_function(__VA_ARGS__)))
|
| 60 |
|
| 61 |
|
| 62 | #define EXCLUSIVE_LOCKS_REQUIRED(...) __attribute__((exclusive_locks_required(__VA_ARGS__)))
|
| 63 |
|
| 64 |
|
| 65 | #define SHARED_LOCKS_REQUIRED(...) __attribute__((shared_locks_required(__VA_ARGS__)))
|
| 66 |
|
| 67 |
|
| 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 |
|
| 96 | struct cond {
|
| 97 | pthread_cond_t cond;
|
| 98 | };
|
| 99 |
|
| 100 |
|
| 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 |
|
| 108 | void mutex_init_recursive(struct mutex *mtx);
|
| 109 | void mutex_destroy(struct mutex *mtx);
|
| 110 |
|
| 111 | void mutex_lock(struct mutex *mtx) EXCLUSIVE_LOCK_FUNCTION(*mtx);
|
| 112 | void mutex_unlock(struct mutex *mtx) UNLOCK_FUNCTION(*mtx);
|
| 113 |
|
| 114 | bool mutex_trylock(struct mutex *mtx) EXCLUSIVE_TRYLOCK_FUNCTION(true, *mtx);
|
| 115 |
|
| 116 |
|
| 117 | void cond_init(struct cond *cnd);
|
| 118 | |
| 119 | * Initialize the cond struct and specify the process-shared rather than default
|
| 120 | * process-private attribute.
|
| 121 |
|
| 122 | void cond_init_pshared(struct cond *cnd);
|
| 123 | void cond_destroy(struct cond *cnd);
|
| 124 |
|
| 125 | void cond_wait(struct cond *cnd, struct mutex *mtx) EXCLUSIVE_LOCKS_REQUIRED(mtx);
|
| 126 | void cond_signal(struct cond *cnd);
|
| 127 | void cond_broadcast(struct cond *cnd);
|
| 128 |
|
| 129 | #endif
|