Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
6053ee3b IM |
2 | /* |
3 | * Mutexes: blocking mutual exclusion locks | |
4 | * | |
5 | * started by Ingo Molnar: | |
6 | * | |
7 | * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> | |
8 | * | |
9 | * This file contains the main data structure and API definitions. | |
10 | */ | |
11 | #ifndef __LINUX_MUTEX_H | |
12 | #define __LINUX_MUTEX_H | |
13 | ||
040a0a37 | 14 | #include <asm/current.h> |
6053ee3b IM |
15 | #include <linux/list.h> |
16 | #include <linux/spinlock_types.h> | |
ef5d4707 | 17 | #include <linux/lockdep.h> |
60063497 | 18 | #include <linux/atomic.h> |
083986e8 | 19 | #include <asm/processor.h> |
90631822 | 20 | #include <linux/osq_lock.h> |
3ca0ff57 | 21 | #include <linux/debug_locks.h> |
54da6a09 | 22 | #include <linux/cleanup.h> |
d84f3179 | 23 | #include <linux/mutex_types.h> |
6053ee3b | 24 | |
bb630f9f TG |
25 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
26 | # define __DEP_MAP_MUTEX_INITIALIZER(lockname) \ | |
27 | , .dep_map = { \ | |
28 | .name = #lockname, \ | |
29 | .wait_type_inner = LD_WAIT_SLEEP, \ | |
30 | } | |
31 | #else | |
32 | # define __DEP_MAP_MUTEX_INITIALIZER(lockname) | |
33 | #endif | |
34 | ||
6053ee3b | 35 | #ifdef CONFIG_DEBUG_MUTEXES |
3ca0ff57 | 36 | |
d6cac0b6 | 37 | # define __DEBUG_MUTEX_INITIALIZER(lockname) \ |
3ca0ff57 PZ |
38 | , .magic = &lockname |
39 | ||
40 | extern void mutex_destroy(struct mutex *lock); | |
41 | ||
6053ee3b | 42 | #else |
3ca0ff57 | 43 | |
6053ee3b | 44 | # define __DEBUG_MUTEX_INITIALIZER(lockname) |
3ca0ff57 PZ |
45 | |
46 | static inline void mutex_destroy(struct mutex *lock) {} | |
47 | ||
48 | #endif | |
49 | ||
d6cac0b6 | 50 | #ifndef CONFIG_PREEMPT_RT |
ef5dc121 RD |
51 | /** |
52 | * mutex_init - initialize the mutex | |
53 | * @mutex: the mutex to be initialized | |
54 | * | |
55 | * Initialize the mutex to unlocked state. | |
56 | * | |
57 | * It is not allowed to initialize an already locked mutex. | |
58 | */ | |
3ca0ff57 PZ |
59 | #define mutex_init(mutex) \ |
60 | do { \ | |
61 | static struct lock_class_key __key; \ | |
62 | \ | |
63 | __mutex_init((mutex), #mutex, &__key); \ | |
ef5d4707 | 64 | } while (0) |
6053ee3b IM |
65 | |
66 | #define __MUTEX_INITIALIZER(lockname) \ | |
3ca0ff57 | 67 | { .owner = ATOMIC_LONG_INIT(0) \ |
ebf4c55c | 68 | , .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(lockname.wait_lock) \ |
6053ee3b | 69 | , .wait_list = LIST_HEAD_INIT(lockname.wait_list) \ |
ef5d4707 IM |
70 | __DEBUG_MUTEX_INITIALIZER(lockname) \ |
71 | __DEP_MAP_MUTEX_INITIALIZER(lockname) } | |
6053ee3b IM |
72 | |
73 | #define DEFINE_MUTEX(mutexname) \ | |
74 | struct mutex mutexname = __MUTEX_INITIALIZER(mutexname) | |
75 | ||
ef5d4707 IM |
76 | extern void __mutex_init(struct mutex *lock, const char *name, |
77 | struct lock_class_key *key); | |
6053ee3b | 78 | |
45f8bde0 | 79 | /** |
6053ee3b IM |
80 | * mutex_is_locked - is the mutex locked |
81 | * @lock: the mutex to be queried | |
82 | * | |
db076bef | 83 | * Returns true if the mutex is locked, false if unlocked. |
6053ee3b | 84 | */ |
5f35d5a6 | 85 | extern bool mutex_is_locked(struct mutex *lock); |
6053ee3b | 86 | |
bb630f9f TG |
87 | #else /* !CONFIG_PREEMPT_RT */ |
88 | /* | |
89 | * Preempt-RT variant based on rtmutexes. | |
90 | */ | |
bb630f9f TG |
91 | |
92 | #define __MUTEX_INITIALIZER(mutexname) \ | |
93 | { \ | |
94 | .rtmutex = __RT_MUTEX_BASE_INITIALIZER(mutexname.rtmutex) \ | |
95 | __DEP_MAP_MUTEX_INITIALIZER(mutexname) \ | |
96 | } | |
97 | ||
98 | #define DEFINE_MUTEX(mutexname) \ | |
99 | struct mutex mutexname = __MUTEX_INITIALIZER(mutexname) | |
100 | ||
101 | extern void __mutex_rt_init(struct mutex *lock, const char *name, | |
102 | struct lock_class_key *key); | |
bb630f9f TG |
103 | |
104 | #define mutex_is_locked(l) rt_mutex_base_is_locked(&(l)->rtmutex) | |
105 | ||
106 | #define __mutex_init(mutex, name, key) \ | |
107 | do { \ | |
108 | rt_mutex_base_init(&(mutex)->rtmutex); \ | |
109 | __mutex_rt_init((mutex), name, key); \ | |
110 | } while (0) | |
111 | ||
112 | #define mutex_init(mutex) \ | |
113 | do { \ | |
114 | static struct lock_class_key __key; \ | |
115 | \ | |
116 | __mutex_init((mutex), #mutex, &__key); \ | |
117 | } while (0) | |
118 | #endif /* CONFIG_PREEMPT_RT */ | |
119 | ||
6053ee3b | 120 | /* |
67a6de49 | 121 | * See kernel/locking/mutex.c for detailed documentation of these APIs. |
387b1468 | 122 | * Also see Documentation/locking/mutex-design.rst. |
6053ee3b | 123 | */ |
ef5d4707 IM |
124 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
125 | extern void mutex_lock_nested(struct mutex *lock, unsigned int subclass); | |
e4c70a66 | 126 | extern void _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest_lock); |
040a0a37 | 127 | |
18d8362d AM |
128 | extern int __must_check mutex_lock_interruptible_nested(struct mutex *lock, |
129 | unsigned int subclass); | |
ad776537 LH |
130 | extern int __must_check mutex_lock_killable_nested(struct mutex *lock, |
131 | unsigned int subclass); | |
1460cb65 | 132 | extern void mutex_lock_io_nested(struct mutex *lock, unsigned int subclass); |
e4564f79 PZ |
133 | |
134 | #define mutex_lock(lock) mutex_lock_nested(lock, 0) | |
135 | #define mutex_lock_interruptible(lock) mutex_lock_interruptible_nested(lock, 0) | |
ad776537 | 136 | #define mutex_lock_killable(lock) mutex_lock_killable_nested(lock, 0) |
1460cb65 | 137 | #define mutex_lock_io(lock) mutex_lock_io_nested(lock, 0) |
e4c70a66 PZ |
138 | |
139 | #define mutex_lock_nest_lock(lock, nest_lock) \ | |
140 | do { \ | |
040a0a37 | 141 | typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \ |
e4c70a66 PZ |
142 | _mutex_lock_nest_lock(lock, &(nest_lock)->dep_map); \ |
143 | } while (0) | |
144 | ||
ef5d4707 | 145 | #else |
ec701584 HH |
146 | extern void mutex_lock(struct mutex *lock); |
147 | extern int __must_check mutex_lock_interruptible(struct mutex *lock); | |
148 | extern int __must_check mutex_lock_killable(struct mutex *lock); | |
1460cb65 | 149 | extern void mutex_lock_io(struct mutex *lock); |
e4564f79 | 150 | |
ef5d4707 | 151 | # define mutex_lock_nested(lock, subclass) mutex_lock(lock) |
d63a5a74 | 152 | # define mutex_lock_interruptible_nested(lock, subclass) mutex_lock_interruptible(lock) |
ad776537 | 153 | # define mutex_lock_killable_nested(lock, subclass) mutex_lock_killable(lock) |
e4c70a66 | 154 | # define mutex_lock_nest_lock(lock, nest_lock) mutex_lock(lock) |
291da9d4 | 155 | # define mutex_lock_io_nested(lock, subclass) mutex_lock_io(lock) |
ef5d4707 IM |
156 | #endif |
157 | ||
6053ee3b IM |
158 | /* |
159 | * NOTE: mutex_trylock() follows the spin_trylock() convention, | |
160 | * not the down_trylock() convention! | |
d98d38f2 AV |
161 | * |
162 | * Returns 1 if the mutex has been acquired successfully, and 0 on contention. | |
6053ee3b | 163 | */ |
ec701584 HH |
164 | extern int mutex_trylock(struct mutex *lock); |
165 | extern void mutex_unlock(struct mutex *lock); | |
040a0a37 | 166 | |
a511e3f9 | 167 | extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock); |
b1fca266 | 168 | |
54da6a09 | 169 | DEFINE_GUARD(mutex, struct mutex *, mutex_lock(_T), mutex_unlock(_T)) |
e4ab322f PZ |
170 | DEFINE_GUARD_COND(mutex, _try, mutex_trylock(_T)) |
171 | DEFINE_GUARD_COND(mutex, _intr, mutex_lock_interruptible(_T) == 0) | |
54da6a09 | 172 | |
e7224674 | 173 | #endif /* __LINUX_MUTEX_H */ |