| 1 |
|
| 2 | |
| 3 | * RT Mutexes: blocking mutual exclusion locks with PI support
|
| 4 | *
|
| 5 | * started by Ingo Molnar and Thomas Gleixner:
|
| 6 | *
|
| 7 | * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <[email protected]>
|
| 8 | * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <[email protected]>
|
| 9 | *
|
| 10 | * This file contains the public data structure and API definitions.
|
| 11 |
|
| 12 |
|
| 13 | #ifndef __LINUX_RT_MUTEX_H
|
| 14 | #define __LINUX_RT_MUTEX_H
|
| 15 |
|
| 16 | #include <linux/compiler.h>
|
| 17 | #include <linux/linkage.h>
|
| 18 | #include <linux/rbtree_types.h>
|
| 19 | #include <linux/spinlock_types_raw.h>
|
| 20 |
|
| 21 | extern int max_lock_depth;
|
| 22 |
|
| 23 | struct rt_mutex_base {
|
| 24 | raw_spinlock_t wait_lock;
|
| 25 | struct rb_root_cached waiters __guarded_by(&wait_lock);
|
| 26 | struct task_struct *owner __guarded_by(&wait_lock);
|
| 27 | };
|
| 28 |
|
| 29 | #define __RT_MUTEX_BASE_INITIALIZER(rtbasename) \
|
| 30 | { \
|
| 31 | .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(rtbasename.wait_lock), \
|
| 32 | .waiters = RB_ROOT_CACHED, \
|
| 33 | .owner = NULL \
|
| 34 | }
|
| 35 |
|
| 36 | |
| 37 | * rt_mutex_base_is_locked - is the rtmutex locked
|
| 38 | * @lock: the mutex to be queried
|
| 39 | *
|
| 40 | * Returns true if the mutex is locked, false if unlocked.
|
| 41 |
|
| 42 | static inline bool rt_mutex_base_is_locked(struct rt_mutex_base *lock)
|
| 43 | {
|
| 44 | return data_race(READ_ONCE(lock->owner) != NULL);
|
| 45 | }
|
| 46 |
|
| 47 | #ifdef CONFIG_RT_MUTEXES
|
| 48 | #define RT_MUTEX_HAS_WAITERS 1UL
|
| 49 |
|
| 50 | static inline struct task_struct *rt_mutex_owner(struct rt_mutex_base *lock)
|
| 51 | {
|
| 52 | unsigned long owner = (unsigned long) data_race(READ_ONCE(lock->owner));
|
| 53 |
|
| 54 | return (struct task_struct *) (owner & ~RT_MUTEX_HAS_WAITERS);
|
| 55 | }
|
| 56 | #endif
|
| 57 | extern void rt_mutex_base_init(struct rt_mutex_base *rtb);
|
| 58 |
|
| 59 | context_lock_struct(rt_mutex);
|
| 60 |
|
| 61 | |
| 62 | * The rt_mutex structure
|
| 63 | *
|
| 64 | * @wait_lock: spinlock to protect the structure
|
| 65 | * @waiters: rbtree root to enqueue waiters in priority order;
|
| 66 | * caches top-waiter (leftmost node).
|
| 67 | * @owner: the mutex owner
|
| 68 |
|
| 69 | struct rt_mutex {
|
| 70 | struct rt_mutex_base rtmutex;
|
| 71 | #ifdef CONFIG_DEBUG_LOCK_ALLOC
|
| 72 | struct lockdep_map dep_map;
|
| 73 | #endif
|
| 74 | };
|
| 75 |
|
| 76 | struct rt_mutex_waiter;
|
| 77 | struct hrtimer_sleeper;
|
| 78 |
|
| 79 | #ifdef CONFIG_DEBUG_RT_MUTEXES
|
| 80 | extern void rt_mutex_debug_task_free(struct task_struct *tsk);
|
| 81 | #else
|
| 82 | static inline void rt_mutex_debug_task_free(struct task_struct *tsk) { }
|
| 83 | #endif
|
| 84 |
|
| 85 | #define rt_mutex_init(mutex) \
|
| 86 | do { \
|
| 87 | static struct lock_class_key __key; \
|
| 88 | __rt_mutex_init(mutex, __func__, &__key); \
|
| 89 | } while (0)
|
| 90 |
|
| 91 | #ifdef CONFIG_DEBUG_LOCK_ALLOC
|
| 92 | #define __DEP_MAP_RT_MUTEX_INITIALIZER(mutexname) \
|
| 93 | .dep_map = { \
|
| 94 | .name = #mutexname, \
|
| 95 | .wait_type_inner = LD_WAIT_SLEEP, \
|
| 96 | }
|
| 97 | #else
|
| 98 | #define __DEP_MAP_RT_MUTEX_INITIALIZER(mutexname)
|
| 99 | #endif
|
| 100 |
|
| 101 | #define __RT_MUTEX_INITIALIZER(mutexname) \
|
| 102 | { \
|
| 103 | .rtmutex = __RT_MUTEX_BASE_INITIALIZER(mutexname.rtmutex), \
|
| 104 | __DEP_MAP_RT_MUTEX_INITIALIZER(mutexname) \
|
| 105 | }
|
| 106 |
|
| 107 | #define DEFINE_RT_MUTEX(mutexname) \
|
| 108 | struct rt_mutex mutexname = __RT_MUTEX_INITIALIZER(mutexname)
|
| 109 |
|
| 110 | extern void __rt_mutex_init(struct rt_mutex *lock, const char *name, struct lock_class_key *key);
|
| 111 |
|
| 112 | #ifdef CONFIG_DEBUG_LOCK_ALLOC
|
| 113 | extern void rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass)
|
| 114 | __acquires(lock);
|
| 115 | extern void _rt_mutex_lock_nest_lock(struct rt_mutex *lock, struct lockdep_map *nest_lock)
|
| 116 | __acquires(lock);
|
| 117 | #define rt_mutex_lock(lock) rt_mutex_lock_nested(lock, 0)
|
| 118 | #define rt_mutex_lock_nest_lock(lock, nest_lock) \
|
| 119 | do { \
|
| 120 | typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
|
| 121 | _rt_mutex_lock_nest_lock(lock, &(nest_lock)->dep_map); \
|
| 122 | } while (0)
|
| 123 |
|
| 124 | #else
|
| 125 | extern void rt_mutex_lock(struct rt_mutex *lock) __acquires(lock);
|
| 126 | #define rt_mutex_lock_nested(lock, subclass) rt_mutex_lock(lock)
|
| 127 | #define rt_mutex_lock_nest_lock(lock, nest_lock) rt_mutex_lock(lock)
|
| 128 | #endif
|
| 129 |
|
| 130 | extern int rt_mutex_lock_interruptible(struct rt_mutex *lock)
|
| 131 | __cond_acquires(0, lock);
|
| 132 | extern int rt_mutex_lock_killable(struct rt_mutex *lock)
|
| 133 | __cond_acquires(0, lock);
|
| 134 | extern int rt_mutex_trylock(struct rt_mutex *lock)
|
| 135 | __cond_acquires(true, lock);
|
| 136 |
|
| 137 | extern void rt_mutex_unlock(struct rt_mutex *lock)
|
| 138 | __releases(lock);
|
| 139 |
|
| 140 | #endif
|