1
2
3
4
5
6
7
8
9
10
11#ifndef __LINUX_MUTEX_H
12#define __LINUX_MUTEX_H
13
14#include <asm/current.h>
15#include <linux/list.h>
16#include <linux/spinlock_types.h>
17#include <linux/linkage.h>
18#include <linux/lockdep.h>
19#include <linux/atomic.h>
20#include <asm/processor.h>
21#include <linux/osq_lock.h>
22#include <linux/debug_locks.h>
23
24struct ww_acquire_ctx;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54struct mutex {
55 atomic_long_t owner;
56 spinlock_t wait_lock;
57#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
58 struct optimistic_spin_queue osq;
59#endif
60 struct list_head wait_list;
61#ifdef CONFIG_DEBUG_MUTEXES
62 void *magic;
63#endif
64#ifdef CONFIG_DEBUG_LOCK_ALLOC
65 struct lockdep_map dep_map;
66#endif
67};
68
69
70
71
72
73
74static inline struct task_struct *__mutex_owner(struct mutex *lock)
75{
76 return (struct task_struct *)(atomic_long_read(&lock->owner) & ~0x07);
77}
78
79
80
81
82
83struct mutex_waiter {
84 struct list_head list;
85 struct task_struct *task;
86 struct ww_acquire_ctx *ww_ctx;
87#ifdef CONFIG_DEBUG_MUTEXES
88 void *magic;
89#endif
90};
91
92#ifdef CONFIG_DEBUG_MUTEXES
93
94#define __DEBUG_MUTEX_INITIALIZER(lockname) \
95 , .magic = &lockname
96
97extern void mutex_destroy(struct mutex *lock);
98
99#else
100
101# define __DEBUG_MUTEX_INITIALIZER(lockname)
102
103static inline void mutex_destroy(struct mutex *lock) {}
104
105#endif
106
107
108
109
110
111
112
113
114
115#define mutex_init(mutex) \
116do { \
117 static struct lock_class_key __key; \
118 \
119 __mutex_init((mutex), #mutex, &__key); \
120} while (0)
121
122#ifdef CONFIG_DEBUG_LOCK_ALLOC
123# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
124 , .dep_map = { .name = #lockname }
125#else
126# define __DEP_MAP_MUTEX_INITIALIZER(lockname)
127#endif
128
129#define __MUTEX_INITIALIZER(lockname) \
130 { .owner = ATOMIC_LONG_INIT(0) \
131 , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
132 , .wait_list = LIST_HEAD_INIT(lockname.wait_list) \
133 __DEBUG_MUTEX_INITIALIZER(lockname) \
134 __DEP_MAP_MUTEX_INITIALIZER(lockname) }
135
136#define DEFINE_MUTEX(mutexname) \
137 struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
138
139extern void __mutex_init(struct mutex *lock, const char *name,
140 struct lock_class_key *key);
141
142
143
144
145
146
147
148static inline bool mutex_is_locked(struct mutex *lock)
149{
150
151
152
153 return __mutex_owner(lock) != NULL;
154}
155
156
157
158
159
160#ifdef CONFIG_DEBUG_LOCK_ALLOC
161extern void mutex_lock_nested(struct mutex *lock, unsigned int subclass);
162extern void _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest_lock);
163
164extern int __must_check mutex_lock_interruptible_nested(struct mutex *lock,
165 unsigned int subclass);
166extern int __must_check mutex_lock_killable_nested(struct mutex *lock,
167 unsigned int subclass);
168extern void mutex_lock_io_nested(struct mutex *lock, unsigned int subclass);
169
170#define mutex_lock(lock) mutex_lock_nested(lock, 0)
171#define mutex_lock_interruptible(lock) mutex_lock_interruptible_nested(lock, 0)
172#define mutex_lock_killable(lock) mutex_lock_killable_nested(lock, 0)
173#define mutex_lock_io(lock) mutex_lock_io_nested(lock, 0)
174
175#define mutex_lock_nest_lock(lock, nest_lock) \
176do { \
177 typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
178 _mutex_lock_nest_lock(lock, &(nest_lock)->dep_map); \
179} while (0)
180
181#else
182extern void mutex_lock(struct mutex *lock);
183extern int __must_check mutex_lock_interruptible(struct mutex *lock);
184extern int __must_check mutex_lock_killable(struct mutex *lock);
185extern void mutex_lock_io(struct mutex *lock);
186
187# define mutex_lock_nested(lock, subclass) mutex_lock(lock)
188# define mutex_lock_interruptible_nested(lock, subclass) mutex_lock_interruptible(lock)
189# define mutex_lock_killable_nested(lock, subclass) mutex_lock_killable(lock)
190# define mutex_lock_nest_lock(lock, nest_lock) mutex_lock(lock)
191# define mutex_lock_io_nested(lock, subclass) mutex_lock(lock)
192#endif
193
194
195
196
197
198
199
200extern int mutex_trylock(struct mutex *lock);
201extern void mutex_unlock(struct mutex *lock);
202
203extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
204
205
206
207
208
209enum mutex_trylock_recursive_enum {
210 MUTEX_TRYLOCK_FAILED = 0,
211 MUTEX_TRYLOCK_SUCCESS = 1,
212 MUTEX_TRYLOCK_RECURSIVE,
213};
214
215
216
217
218
219
220
221
222
223
224
225
226
227static inline __must_check enum mutex_trylock_recursive_enum
228mutex_trylock_recursive(struct mutex *lock)
229{
230 if (unlikely(__mutex_owner(lock) == current))
231 return MUTEX_TRYLOCK_RECURSIVE;
232
233 return mutex_trylock(lock);
234}
235
236#endif
237