aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/spinlock.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/linux/spinlock.h')
-rw-r--r--include/linux/spinlock.h377
1 files changed, 213 insertions, 164 deletions
diff --git a/include/linux/spinlock.h b/include/linux/spinlock.h
index 71dccfeb0d88..86088213334a 100644
--- a/include/linux/spinlock.h
+++ b/include/linux/spinlock.h
@@ -8,13 +8,13 @@
8 * 8 *
9 * on SMP builds: 9 * on SMP builds:
10 * 10 *
11 * asm/spinlock_types.h: contains the raw_spinlock_t/raw_rwlock_t and the 11 * asm/spinlock_types.h: contains the arch_spinlock_t/arch_rwlock_t and the
12 * initializers 12 * initializers
13 * 13 *
14 * linux/spinlock_types.h: 14 * linux/spinlock_types.h:
15 * defines the generic type and initializers 15 * defines the generic type and initializers
16 * 16 *
17 * asm/spinlock.h: contains the __raw_spin_*()/etc. lowlevel 17 * asm/spinlock.h: contains the arch_spin_*()/etc. lowlevel
18 * implementations, mostly inline assembly code 18 * implementations, mostly inline assembly code
19 * 19 *
20 * (also included on UP-debug builds:) 20 * (also included on UP-debug builds:)
@@ -34,7 +34,7 @@
34 * defines the generic type and initializers 34 * defines the generic type and initializers
35 * 35 *
36 * linux/spinlock_up.h: 36 * linux/spinlock_up.h:
37 * contains the __raw_spin_*()/etc. version of UP 37 * contains the arch_spin_*()/etc. version of UP
38 * builds. (which are NOPs on non-debug, non-preempt 38 * builds. (which are NOPs on non-debug, non-preempt
39 * builds) 39 * builds)
40 * 40 *
@@ -75,12 +75,12 @@
75#define __lockfunc __attribute__((section(".spinlock.text"))) 75#define __lockfunc __attribute__((section(".spinlock.text")))
76 76
77/* 77/*
78 * Pull the raw_spinlock_t and raw_rwlock_t definitions: 78 * Pull the arch_spinlock_t and arch_rwlock_t definitions:
79 */ 79 */
80#include <linux/spinlock_types.h> 80#include <linux/spinlock_types.h>
81 81
82/* 82/*
83 * Pull the __raw*() functions/declarations (UP-nondebug doesnt need them): 83 * Pull the arch_spin*() functions/declarations (UP-nondebug doesnt need them):
84 */ 84 */
85#ifdef CONFIG_SMP 85#ifdef CONFIG_SMP
86# include <asm/spinlock.h> 86# include <asm/spinlock.h>
@@ -89,45 +89,31 @@
89#endif 89#endif
90 90
91#ifdef CONFIG_DEBUG_SPINLOCK 91#ifdef CONFIG_DEBUG_SPINLOCK
92 extern void __spin_lock_init(spinlock_t *lock, const char *name, 92 extern void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name,
93 struct lock_class_key *key); 93 struct lock_class_key *key);
94# define spin_lock_init(lock) \ 94# define raw_spin_lock_init(lock) \
95do { \ 95do { \
96 static struct lock_class_key __key; \ 96 static struct lock_class_key __key; \
97 \ 97 \
98 __spin_lock_init((lock), #lock, &__key); \ 98 __raw_spin_lock_init((lock), #lock, &__key); \
99} while (0) 99} while (0)
100 100
101#else 101#else
102# define spin_lock_init(lock) \ 102# define raw_spin_lock_init(lock) \
103 do { *(lock) = __SPIN_LOCK_UNLOCKED(lock); } while (0) 103 do { *(lock) = __RAW_SPIN_LOCK_UNLOCKED(lock); } while (0)
104#endif 104#endif
105 105
106#ifdef CONFIG_DEBUG_SPINLOCK 106#define raw_spin_is_locked(lock) arch_spin_is_locked(&(lock)->raw_lock)
107 extern void __rwlock_init(rwlock_t *lock, const char *name,
108 struct lock_class_key *key);
109# define rwlock_init(lock) \
110do { \
111 static struct lock_class_key __key; \
112 \
113 __rwlock_init((lock), #lock, &__key); \
114} while (0)
115#else
116# define rwlock_init(lock) \
117 do { *(lock) = __RW_LOCK_UNLOCKED(lock); } while (0)
118#endif
119
120#define spin_is_locked(lock) __raw_spin_is_locked(&(lock)->raw_lock)
121 107
122#ifdef CONFIG_GENERIC_LOCKBREAK 108#ifdef CONFIG_GENERIC_LOCKBREAK
123#define spin_is_contended(lock) ((lock)->break_lock) 109#define raw_spin_is_contended(lock) ((lock)->break_lock)
124#else 110#else
125 111
126#ifdef __raw_spin_is_contended 112#ifdef arch_spin_is_contended
127#define spin_is_contended(lock) __raw_spin_is_contended(&(lock)->raw_lock) 113#define raw_spin_is_contended(lock) arch_spin_is_contended(&(lock)->raw_lock)
128#else 114#else
129#define spin_is_contended(lock) (((void)(lock), 0)) 115#define raw_spin_is_contended(lock) (((void)(lock), 0))
130#endif /*__raw_spin_is_contended*/ 116#endif /*arch_spin_is_contended*/
131#endif 117#endif
132 118
133/* The lock does not imply full memory barrier. */ 119/* The lock does not imply full memory barrier. */
@@ -136,182 +122,260 @@ static inline void smp_mb__after_lock(void) { smp_mb(); }
136#endif 122#endif
137 123
138/** 124/**
139 * spin_unlock_wait - wait until the spinlock gets unlocked 125 * raw_spin_unlock_wait - wait until the spinlock gets unlocked
140 * @lock: the spinlock in question. 126 * @lock: the spinlock in question.
141 */ 127 */
142#define spin_unlock_wait(lock) __raw_spin_unlock_wait(&(lock)->raw_lock) 128#define raw_spin_unlock_wait(lock) arch_spin_unlock_wait(&(lock)->raw_lock)
143 129
144#ifdef CONFIG_DEBUG_SPINLOCK 130#ifdef CONFIG_DEBUG_SPINLOCK
145 extern void _raw_spin_lock(spinlock_t *lock); 131 extern void do_raw_spin_lock(raw_spinlock_t *lock);
146#define _raw_spin_lock_flags(lock, flags) _raw_spin_lock(lock) 132#define do_raw_spin_lock_flags(lock, flags) do_raw_spin_lock(lock)
147 extern int _raw_spin_trylock(spinlock_t *lock); 133 extern int do_raw_spin_trylock(raw_spinlock_t *lock);
148 extern void _raw_spin_unlock(spinlock_t *lock); 134 extern void do_raw_spin_unlock(raw_spinlock_t *lock);
149 extern void _raw_read_lock(rwlock_t *lock);
150#define _raw_read_lock_flags(lock, flags) _raw_read_lock(lock)
151 extern int _raw_read_trylock(rwlock_t *lock);
152 extern void _raw_read_unlock(rwlock_t *lock);
153 extern void _raw_write_lock(rwlock_t *lock);
154#define _raw_write_lock_flags(lock, flags) _raw_write_lock(lock)
155 extern int _raw_write_trylock(rwlock_t *lock);
156 extern void _raw_write_unlock(rwlock_t *lock);
157#else 135#else
158# define _raw_spin_lock(lock) __raw_spin_lock(&(lock)->raw_lock) 136static inline void do_raw_spin_lock(raw_spinlock_t *lock)
159# define _raw_spin_lock_flags(lock, flags) \ 137{
160 __raw_spin_lock_flags(&(lock)->raw_lock, *(flags)) 138 arch_spin_lock(&lock->raw_lock);
161# define _raw_spin_trylock(lock) __raw_spin_trylock(&(lock)->raw_lock) 139}
162# define _raw_spin_unlock(lock) __raw_spin_unlock(&(lock)->raw_lock) 140
163# define _raw_read_lock(rwlock) __raw_read_lock(&(rwlock)->raw_lock) 141static inline void
164# define _raw_read_lock_flags(lock, flags) \ 142do_raw_spin_lock_flags(raw_spinlock_t *lock, unsigned long *flags)
165 __raw_read_lock_flags(&(lock)->raw_lock, *(flags)) 143{
166# define _raw_read_trylock(rwlock) __raw_read_trylock(&(rwlock)->raw_lock) 144 arch_spin_lock_flags(&lock->raw_lock, *flags);
167# define _raw_read_unlock(rwlock) __raw_read_unlock(&(rwlock)->raw_lock) 145}
168# define _raw_write_lock(rwlock) __raw_write_lock(&(rwlock)->raw_lock) 146
169# define _raw_write_lock_flags(lock, flags) \ 147static inline int do_raw_spin_trylock(raw_spinlock_t *lock)
170 __raw_write_lock_flags(&(lock)->raw_lock, *(flags)) 148{
171# define _raw_write_trylock(rwlock) __raw_write_trylock(&(rwlock)->raw_lock) 149 return arch_spin_trylock(&(lock)->raw_lock);
172# define _raw_write_unlock(rwlock) __raw_write_unlock(&(rwlock)->raw_lock) 150}
151
152static inline void do_raw_spin_unlock(raw_spinlock_t *lock)
153{
154 arch_spin_unlock(&lock->raw_lock);
155}
173#endif 156#endif
174 157
175#define read_can_lock(rwlock) __raw_read_can_lock(&(rwlock)->raw_lock)
176#define write_can_lock(rwlock) __raw_write_can_lock(&(rwlock)->raw_lock)
177
178/* 158/*
179 * Define the various spin_lock and rw_lock methods. Note we define these 159 * Define the various spin_lock methods. Note we define these
180 * regardless of whether CONFIG_SMP or CONFIG_PREEMPT are set. The various 160 * regardless of whether CONFIG_SMP or CONFIG_PREEMPT are set. The
181 * methods are defined as nops in the case they are not required. 161 * various methods are defined as nops in the case they are not
162 * required.
182 */ 163 */
183#define spin_trylock(lock) __cond_lock(lock, _spin_trylock(lock)) 164#define raw_spin_trylock(lock) __cond_lock(lock, _raw_spin_trylock(lock))
184#define read_trylock(lock) __cond_lock(lock, _read_trylock(lock))
185#define write_trylock(lock) __cond_lock(lock, _write_trylock(lock))
186 165
187#define spin_lock(lock) _spin_lock(lock) 166#define raw_spin_lock(lock) _raw_spin_lock(lock)
188 167
189#ifdef CONFIG_DEBUG_LOCK_ALLOC 168#ifdef CONFIG_DEBUG_LOCK_ALLOC
190# define spin_lock_nested(lock, subclass) _spin_lock_nested(lock, subclass) 169# define raw_spin_lock_nested(lock, subclass) \
191# define spin_lock_nest_lock(lock, nest_lock) \ 170 _raw_spin_lock_nested(lock, subclass)
171
172# define raw_spin_lock_nest_lock(lock, nest_lock) \
192 do { \ 173 do { \
193 typecheck(struct lockdep_map *, &(nest_lock)->dep_map);\ 174 typecheck(struct lockdep_map *, &(nest_lock)->dep_map);\
194 _spin_lock_nest_lock(lock, &(nest_lock)->dep_map); \ 175 _raw_spin_lock_nest_lock(lock, &(nest_lock)->dep_map); \
195 } while (0) 176 } while (0)
196#else 177#else
197# define spin_lock_nested(lock, subclass) _spin_lock(lock) 178# define raw_spin_lock_nested(lock, subclass) _raw_spin_lock(lock)
198# define spin_lock_nest_lock(lock, nest_lock) _spin_lock(lock) 179# define raw_spin_lock_nest_lock(lock, nest_lock) _raw_spin_lock(lock)
199#endif 180#endif
200 181
201#define write_lock(lock) _write_lock(lock)
202#define read_lock(lock) _read_lock(lock)
203
204#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) 182#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
205 183
206#define spin_lock_irqsave(lock, flags) \ 184#define raw_spin_lock_irqsave(lock, flags) \
207 do { \ 185 do { \
208 typecheck(unsigned long, flags); \ 186 typecheck(unsigned long, flags); \
209 flags = _spin_lock_irqsave(lock); \ 187 flags = _raw_spin_lock_irqsave(lock); \
210 } while (0)
211#define read_lock_irqsave(lock, flags) \
212 do { \
213 typecheck(unsigned long, flags); \
214 flags = _read_lock_irqsave(lock); \
215 } while (0)
216#define write_lock_irqsave(lock, flags) \
217 do { \
218 typecheck(unsigned long, flags); \
219 flags = _write_lock_irqsave(lock); \
220 } while (0) 188 } while (0)
221 189
222#ifdef CONFIG_DEBUG_LOCK_ALLOC 190#ifdef CONFIG_DEBUG_LOCK_ALLOC
223#define spin_lock_irqsave_nested(lock, flags, subclass) \ 191#define raw_spin_lock_irqsave_nested(lock, flags, subclass) \
224 do { \ 192 do { \
225 typecheck(unsigned long, flags); \ 193 typecheck(unsigned long, flags); \
226 flags = _spin_lock_irqsave_nested(lock, subclass); \ 194 flags = _raw_spin_lock_irqsave_nested(lock, subclass); \
227 } while (0) 195 } while (0)
228#else 196#else
229#define spin_lock_irqsave_nested(lock, flags, subclass) \ 197#define raw_spin_lock_irqsave_nested(lock, flags, subclass) \
230 do { \ 198 do { \
231 typecheck(unsigned long, flags); \ 199 typecheck(unsigned long, flags); \
232 flags = _spin_lock_irqsave(lock); \ 200 flags = _raw_spin_lock_irqsave(lock); \
233 } while (0) 201 } while (0)
234#endif 202#endif
235 203
236#else 204#else
237 205
238#define spin_lock_irqsave(lock, flags) \ 206#define raw_spin_lock_irqsave(lock, flags) \
239 do { \
240 typecheck(unsigned long, flags); \
241 _spin_lock_irqsave(lock, flags); \
242 } while (0)
243#define read_lock_irqsave(lock, flags) \
244 do { \
245 typecheck(unsigned long, flags); \
246 _read_lock_irqsave(lock, flags); \
247 } while (0)
248#define write_lock_irqsave(lock, flags) \
249 do { \ 207 do { \
250 typecheck(unsigned long, flags); \ 208 typecheck(unsigned long, flags); \
251 _write_lock_irqsave(lock, flags); \ 209 _raw_spin_lock_irqsave(lock, flags); \
252 } while (0) 210 } while (0)
253#define spin_lock_irqsave_nested(lock, flags, subclass) \
254 spin_lock_irqsave(lock, flags)
255 211
256#endif 212#define raw_spin_lock_irqsave_nested(lock, flags, subclass) \
213 raw_spin_lock_irqsave(lock, flags)
257 214
258#define spin_lock_irq(lock) _spin_lock_irq(lock) 215#endif
259#define spin_lock_bh(lock) _spin_lock_bh(lock)
260#define read_lock_irq(lock) _read_lock_irq(lock)
261#define read_lock_bh(lock) _read_lock_bh(lock)
262#define write_lock_irq(lock) _write_lock_irq(lock)
263#define write_lock_bh(lock) _write_lock_bh(lock)
264#define spin_unlock(lock) _spin_unlock(lock)
265#define read_unlock(lock) _read_unlock(lock)
266#define write_unlock(lock) _write_unlock(lock)
267#define spin_unlock_irq(lock) _spin_unlock_irq(lock)
268#define read_unlock_irq(lock) _read_unlock_irq(lock)
269#define write_unlock_irq(lock) _write_unlock_irq(lock)
270
271#define spin_unlock_irqrestore(lock, flags) \
272 do { \
273 typecheck(unsigned long, flags); \
274 _spin_unlock_irqrestore(lock, flags); \
275 } while (0)
276#define spin_unlock_bh(lock) _spin_unlock_bh(lock)
277 216
278#define read_unlock_irqrestore(lock, flags) \ 217#define raw_spin_lock_irq(lock) _raw_spin_lock_irq(lock)
279 do { \ 218#define raw_spin_lock_bh(lock) _raw_spin_lock_bh(lock)
280 typecheck(unsigned long, flags); \ 219#define raw_spin_unlock(lock) _raw_spin_unlock(lock)
281 _read_unlock_irqrestore(lock, flags); \ 220#define raw_spin_unlock_irq(lock) _raw_spin_unlock_irq(lock)
282 } while (0)
283#define read_unlock_bh(lock) _read_unlock_bh(lock)
284 221
285#define write_unlock_irqrestore(lock, flags) \ 222#define raw_spin_unlock_irqrestore(lock, flags) \
286 do { \ 223 do { \
287 typecheck(unsigned long, flags); \ 224 typecheck(unsigned long, flags); \
288 _write_unlock_irqrestore(lock, flags); \ 225 _raw_spin_unlock_irqrestore(lock, flags); \
289 } while (0) 226 } while (0)
290#define write_unlock_bh(lock) _write_unlock_bh(lock) 227#define raw_spin_unlock_bh(lock) _raw_spin_unlock_bh(lock)
291 228
292#define spin_trylock_bh(lock) __cond_lock(lock, _spin_trylock_bh(lock)) 229#define raw_spin_trylock_bh(lock) \
230 __cond_lock(lock, _raw_spin_trylock_bh(lock))
293 231
294#define spin_trylock_irq(lock) \ 232#define raw_spin_trylock_irq(lock) \
295({ \ 233({ \
296 local_irq_disable(); \ 234 local_irq_disable(); \
297 spin_trylock(lock) ? \ 235 raw_spin_trylock(lock) ? \
298 1 : ({ local_irq_enable(); 0; }); \ 236 1 : ({ local_irq_enable(); 0; }); \
299}) 237})
300 238
301#define spin_trylock_irqsave(lock, flags) \ 239#define raw_spin_trylock_irqsave(lock, flags) \
302({ \ 240({ \
303 local_irq_save(flags); \ 241 local_irq_save(flags); \
304 spin_trylock(lock) ? \ 242 raw_spin_trylock(lock) ? \
305 1 : ({ local_irq_restore(flags); 0; }); \ 243 1 : ({ local_irq_restore(flags); 0; }); \
306}) 244})
307 245
308#define write_trylock_irqsave(lock, flags) \ 246/**
309({ \ 247 * raw_spin_can_lock - would raw_spin_trylock() succeed?
310 local_irq_save(flags); \ 248 * @lock: the spinlock in question.
311 write_trylock(lock) ? \ 249 */
312 1 : ({ local_irq_restore(flags); 0; }); \ 250#define raw_spin_can_lock(lock) (!raw_spin_is_locked(lock))
251
252/* Include rwlock functions */
253#include <linux/rwlock.h>
254
255/*
256 * Pull the _spin_*()/_read_*()/_write_*() functions/declarations:
257 */
258#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
259# include <linux/spinlock_api_smp.h>
260#else
261# include <linux/spinlock_api_up.h>
262#endif
263
264/*
265 * Map the spin_lock functions to the raw variants for PREEMPT_RT=n
266 */
267
268static inline raw_spinlock_t *spinlock_check(spinlock_t *lock)
269{
270 return &lock->rlock;
271}
272
273#define spin_lock_init(_lock) \
274do { \
275 spinlock_check(_lock); \
276 raw_spin_lock_init(&(_lock)->rlock); \
277} while (0)
278
279static inline void spin_lock(spinlock_t *lock)
280{
281 raw_spin_lock(&lock->rlock);
282}
283
284static inline void spin_lock_bh(spinlock_t *lock)
285{
286 raw_spin_lock_bh(&lock->rlock);
287}
288
289static inline int spin_trylock(spinlock_t *lock)
290{
291 return raw_spin_trylock(&lock->rlock);
292}
293
294#define spin_lock_nested(lock, subclass) \
295do { \
296 raw_spin_lock_nested(spinlock_check(lock), subclass); \
297} while (0)
298
299#define spin_lock_nest_lock(lock, nest_lock) \
300do { \
301 raw_spin_lock_nest_lock(spinlock_check(lock), nest_lock); \
302} while (0)
303
304static inline void spin_lock_irq(spinlock_t *lock)
305{
306 raw_spin_lock_irq(&lock->rlock);
307}
308
309#define spin_lock_irqsave(lock, flags) \
310do { \
311 raw_spin_lock_irqsave(spinlock_check(lock), flags); \
312} while (0)
313
314#define spin_lock_irqsave_nested(lock, flags, subclass) \
315do { \
316 raw_spin_lock_irqsave_nested(spinlock_check(lock), flags, subclass); \
317} while (0)
318
319static inline void spin_unlock(spinlock_t *lock)
320{
321 raw_spin_unlock(&lock->rlock);
322}
323
324static inline void spin_unlock_bh(spinlock_t *lock)
325{
326 raw_spin_unlock_bh(&lock->rlock);
327}
328
329static inline void spin_unlock_irq(spinlock_t *lock)
330{
331 raw_spin_unlock_irq(&lock->rlock);
332}
333
334static inline void spin_unlock_irqrestore(spinlock_t *lock, unsigned long flags)
335{
336 raw_spin_unlock_irqrestore(&lock->rlock, flags);
337}
338
339static inline int spin_trylock_bh(spinlock_t *lock)
340{
341 return raw_spin_trylock_bh(&lock->rlock);
342}
343
344static inline int spin_trylock_irq(spinlock_t *lock)
345{
346 return raw_spin_trylock_irq(&lock->rlock);
347}
348
349#define spin_trylock_irqsave(lock, flags) \
350({ \
351 raw_spin_trylock_irqsave(spinlock_check(lock), flags); \
313}) 352})
314 353
354static inline void spin_unlock_wait(spinlock_t *lock)
355{
356 raw_spin_unlock_wait(&lock->rlock);
357}
358
359static inline int spin_is_locked(spinlock_t *lock)
360{
361 return raw_spin_is_locked(&lock->rlock);
362}
363
364static inline int spin_is_contended(spinlock_t *lock)
365{
366 return raw_spin_is_contended(&lock->rlock);
367}
368
369static inline int spin_can_lock(spinlock_t *lock)
370{
371 return raw_spin_can_lock(&lock->rlock);
372}
373
374static inline void assert_spin_locked(spinlock_t *lock)
375{
376 assert_raw_spin_locked(&lock->rlock);
377}
378
315/* 379/*
316 * Pull the atomic_t declaration: 380 * Pull the atomic_t declaration:
317 * (asm-mips/atomic.h needs above definitions) 381 * (asm-mips/atomic.h needs above definitions)
@@ -329,19 +393,4 @@ extern int _atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock);
329#define atomic_dec_and_lock(atomic, lock) \ 393#define atomic_dec_and_lock(atomic, lock) \
330 __cond_lock(lock, _atomic_dec_and_lock(atomic, lock)) 394 __cond_lock(lock, _atomic_dec_and_lock(atomic, lock))
331 395
332/**
333 * spin_can_lock - would spin_trylock() succeed?
334 * @lock: the spinlock in question.
335 */
336#define spin_can_lock(lock) (!spin_is_locked(lock))
337
338/*
339 * Pull the _spin_*()/_read_*()/_write_*() functions/declarations:
340 */
341#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
342# include <linux/spinlock_api_smp.h>
343#else
344# include <linux/spinlock_api_up.h>
345#endif
346
347#endif /* __LINUX_SPINLOCK_H */ 396#endif /* __LINUX_SPINLOCK_H */