1 #ifndef __LINUX_SPINLOCK_H
2 #define __LINUX_SPINLOCK_H
5 * include/linux/spinlock.h - generic locking declarations
8 #include <linux/config.h>
9 #include <linux/preempt.h>
10 #include <linux/linkage.h>
11 #include <linux/compiler.h>
12 #include <linux/thread_info.h>
13 #include <linux/kernel.h>
14 #include <linux/stringify.h>
16 #include <asm/processor.h> /* for cpu relax */
17 #include <asm/system.h>
20 * Must define these before including other files, inline functions need them
22 #define LOCK_SECTION_NAME \
23 ".text.lock." __stringify(KBUILD_BASENAME)
25 #define LOCK_SECTION_START(extra) \
28 ".ifndef " LOCK_SECTION_NAME "\n\t" \
29 LOCK_SECTION_NAME ":\n\t" \
32 #define LOCK_SECTION_END \
35 #define __lockfunc fastcall __attribute__((section(".spinlock.text")))
38 * If CONFIG_SMP is set, pull in the _raw_* definitions
42 #define assert_spin_locked(x) BUG_ON(!spin_is_locked(x))
43 #include <asm/spinlock.h>
45 int __lockfunc
_spin_trylock(spinlock_t
*lock
);
46 int __lockfunc
_read_trylock(rwlock_t
*lock
);
47 int __lockfunc
_write_trylock(rwlock_t
*lock
);
49 void __lockfunc
_spin_lock(spinlock_t
*lock
) __acquires(spinlock_t
);
50 void __lockfunc
_read_lock(rwlock_t
*lock
) __acquires(rwlock_t
);
51 void __lockfunc
_write_lock(rwlock_t
*lock
) __acquires(rwlock_t
);
53 void __lockfunc
_spin_unlock(spinlock_t
*lock
) __releases(spinlock_t
);
54 void __lockfunc
_read_unlock(rwlock_t
*lock
) __releases(rwlock_t
);
55 void __lockfunc
_write_unlock(rwlock_t
*lock
) __releases(rwlock_t
);
57 unsigned long __lockfunc
_spin_lock_irqsave(spinlock_t
*lock
) __acquires(spinlock_t
);
58 unsigned long __lockfunc
_read_lock_irqsave(rwlock_t
*lock
) __acquires(rwlock_t
);
59 unsigned long __lockfunc
_write_lock_irqsave(rwlock_t
*lock
) __acquires(rwlock_t
);
61 void __lockfunc
_spin_lock_irq(spinlock_t
*lock
) __acquires(spinlock_t
);
62 void __lockfunc
_spin_lock_bh(spinlock_t
*lock
) __acquires(spinlock_t
);
63 void __lockfunc
_read_lock_irq(rwlock_t
*lock
) __acquires(rwlock_t
);
64 void __lockfunc
_read_lock_bh(rwlock_t
*lock
) __acquires(rwlock_t
);
65 void __lockfunc
_write_lock_irq(rwlock_t
*lock
) __acquires(rwlock_t
);
66 void __lockfunc
_write_lock_bh(rwlock_t
*lock
) __acquires(rwlock_t
);
68 void __lockfunc
_spin_unlock_irqrestore(spinlock_t
*lock
, unsigned long flags
) __releases(spinlock_t
);
69 void __lockfunc
_spin_unlock_irq(spinlock_t
*lock
) __releases(spinlock_t
);
70 void __lockfunc
_spin_unlock_bh(spinlock_t
*lock
) __releases(spinlock_t
);
71 void __lockfunc
_read_unlock_irqrestore(rwlock_t
*lock
, unsigned long flags
) __releases(rwlock_t
);
72 void __lockfunc
_read_unlock_irq(rwlock_t
*lock
) __releases(rwlock_t
);
73 void __lockfunc
_read_unlock_bh(rwlock_t
*lock
) __releases(rwlock_t
);
74 void __lockfunc
_write_unlock_irqrestore(rwlock_t
*lock
, unsigned long flags
) __releases(rwlock_t
);
75 void __lockfunc
_write_unlock_irq(rwlock_t
*lock
) __releases(rwlock_t
);
76 void __lockfunc
_write_unlock_bh(rwlock_t
*lock
) __releases(rwlock_t
);
78 int __lockfunc
_spin_trylock_bh(spinlock_t
*lock
);
79 int __lockfunc
generic_raw_read_trylock(rwlock_t
*lock
);
80 int in_lock_functions(unsigned long addr
);
84 #define in_lock_functions(ADDR) 0
86 #if !defined(CONFIG_PREEMPT) && !defined(CONFIG_DEBUG_SPINLOCK)
87 # define _atomic_dec_and_lock(atomic,lock) atomic_dec_and_test(atomic)
88 # define ATOMIC_DEC_AND_LOCK
91 #ifdef CONFIG_DEBUG_SPINLOCK
93 #define SPINLOCK_MAGIC 0x1D244B3C
96 volatile unsigned long lock
;
97 volatile unsigned int babble
;
102 #define SPIN_LOCK_UNLOCKED (spinlock_t) { SPINLOCK_MAGIC, 0, 10, __FILE__ , NULL, 0}
104 #define spin_lock_init(x) \
106 (x)->magic = SPINLOCK_MAGIC; \
109 (x)->module = __FILE__; \
114 #define CHECK_LOCK(x) \
116 if ((x)->magic != SPINLOCK_MAGIC) { \
117 printk(KERN_ERR "%s:%d: spin_is_locked on uninitialized spinlock %p.\n", \
118 __FILE__, __LINE__, (x)); \
122 #define _raw_spin_lock(x) \
125 if ((x)->lock&&(x)->babble) { \
127 printk("%s:%d: spin_lock(%s:%p) already locked by %s/%d\n", \
128 __FILE__,__LINE__, (x)->module, \
129 (x), (x)->owner, (x)->oline); \
132 (x)->owner = __FILE__; \
133 (x)->oline = __LINE__; \
136 /* without debugging, spin_is_locked on UP always says
137 * FALSE. --> printk if already locked. */
138 #define spin_is_locked(x) \
141 if ((x)->lock&&(x)->babble) { \
143 printk("%s:%d: spin_is_locked(%s:%p) already locked by %s/%d\n", \
144 __FILE__,__LINE__, (x)->module, \
145 (x), (x)->owner, (x)->oline); \
150 /* with debugging, assert_spin_locked() on UP does check
151 * the lock value properly */
152 #define assert_spin_locked(x) \
155 BUG_ON(!(x)->lock); \
158 /* without debugging, spin_trylock on UP always says
159 * TRUE. --> printk if already locked. */
160 #define _raw_spin_trylock(x) \
163 if ((x)->lock&&(x)->babble) { \
165 printk("%s:%d: spin_trylock(%s:%p) already locked by %s/%d\n", \
166 __FILE__,__LINE__, (x)->module, \
167 (x), (x)->owner, (x)->oline); \
170 (x)->owner = __FILE__; \
171 (x)->oline = __LINE__; \
175 #define spin_unlock_wait(x) \
178 if ((x)->lock&&(x)->babble) { \
180 printk("%s:%d: spin_unlock_wait(%s:%p) owned by %s/%d\n", \
181 __FILE__,__LINE__, (x)->module, (x), \
182 (x)->owner, (x)->oline); \
186 #define _raw_spin_unlock(x) \
189 if (!(x)->lock&&(x)->babble) { \
191 printk("%s:%d: spin_unlock(%s:%p) not locked\n", \
192 __FILE__,__LINE__, (x)->module, (x));\
198 * gcc versions before ~2.95 have a nasty bug with empty initializers.
201 typedef struct { } spinlock_t
;
202 #define SPIN_LOCK_UNLOCKED (spinlock_t) { }
204 typedef struct { int gcc_is_buggy
; } spinlock_t
;
205 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 0 }
209 * If CONFIG_SMP is unset, declare the _raw_* definitions as nops
211 #define spin_lock_init(lock) do { (void)(lock); } while(0)
212 #define _raw_spin_lock(lock) do { (void)(lock); } while(0)
213 #define spin_is_locked(lock) ((void)(lock), 0)
214 #define assert_spin_locked(lock) do { (void)(lock); } while(0)
215 #define _raw_spin_trylock(lock) (((void)(lock), 1))
216 #define spin_unlock_wait(lock) (void)(lock)
217 #define _raw_spin_unlock(lock) do { (void)(lock); } while(0)
218 #endif /* CONFIG_DEBUG_SPINLOCK */
220 /* RW spinlocks: No debug version */
223 typedef struct { } rwlock_t
;
224 #define RW_LOCK_UNLOCKED (rwlock_t) { }
226 typedef struct { int gcc_is_buggy
; } rwlock_t
;
227 #define RW_LOCK_UNLOCKED (rwlock_t) { 0 }
230 #define rwlock_init(lock) do { (void)(lock); } while(0)
231 #define _raw_read_lock(lock) do { (void)(lock); } while(0)
232 #define _raw_read_unlock(lock) do { (void)(lock); } while(0)
233 #define _raw_write_lock(lock) do { (void)(lock); } while(0)
234 #define _raw_write_unlock(lock) do { (void)(lock); } while(0)
235 #define read_can_lock(lock) (((void)(lock), 1))
236 #define write_can_lock(lock) (((void)(lock), 1))
237 #define _raw_read_trylock(lock) ({ (void)(lock); (1); })
238 #define _raw_write_trylock(lock) ({ (void)(lock); (1); })
240 #define _spin_trylock(lock) ({preempt_disable(); _raw_spin_trylock(lock) ? \
241 1 : ({preempt_enable(); 0;});})
243 #define _read_trylock(lock) ({preempt_disable();_raw_read_trylock(lock) ? \
244 1 : ({preempt_enable(); 0;});})
246 #define _write_trylock(lock) ({preempt_disable(); _raw_write_trylock(lock) ? \
247 1 : ({preempt_enable(); 0;});})
249 #define _spin_trylock_bh(lock) ({preempt_disable(); local_bh_disable(); \
250 _raw_spin_trylock(lock) ? \
251 1 : ({preempt_enable_no_resched(); local_bh_enable(); 0;});})
253 #define _spin_lock(lock) \
256 _raw_spin_lock(lock); \
260 #define _write_lock(lock) \
263 _raw_write_lock(lock); \
267 #define _read_lock(lock) \
270 _raw_read_lock(lock); \
274 #define _spin_unlock(lock) \
276 _raw_spin_unlock(lock); \
281 #define _write_unlock(lock) \
283 _raw_write_unlock(lock); \
288 #define _read_unlock(lock) \
290 _raw_read_unlock(lock); \
295 #define _spin_lock_irqsave(lock, flags) \
297 local_irq_save(flags); \
299 _raw_spin_lock(lock); \
303 #define _spin_lock_irq(lock) \
305 local_irq_disable(); \
307 _raw_spin_lock(lock); \
311 #define _spin_lock_bh(lock) \
313 local_bh_disable(); \
315 _raw_spin_lock(lock); \
319 #define _read_lock_irqsave(lock, flags) \
321 local_irq_save(flags); \
323 _raw_read_lock(lock); \
327 #define _read_lock_irq(lock) \
329 local_irq_disable(); \
331 _raw_read_lock(lock); \
335 #define _read_lock_bh(lock) \
337 local_bh_disable(); \
339 _raw_read_lock(lock); \
343 #define _write_lock_irqsave(lock, flags) \
345 local_irq_save(flags); \
347 _raw_write_lock(lock); \
351 #define _write_lock_irq(lock) \
353 local_irq_disable(); \
355 _raw_write_lock(lock); \
359 #define _write_lock_bh(lock) \
361 local_bh_disable(); \
363 _raw_write_lock(lock); \
367 #define _spin_unlock_irqrestore(lock, flags) \
369 _raw_spin_unlock(lock); \
370 local_irq_restore(flags); \
375 #define _spin_unlock_irq(lock) \
377 _raw_spin_unlock(lock); \
378 local_irq_enable(); \
383 #define _spin_unlock_bh(lock) \
385 _raw_spin_unlock(lock); \
386 preempt_enable_no_resched(); \
391 #define _write_unlock_bh(lock) \
393 _raw_write_unlock(lock); \
394 preempt_enable_no_resched(); \
399 #define _read_unlock_irqrestore(lock, flags) \
401 _raw_read_unlock(lock); \
402 local_irq_restore(flags); \
407 #define _write_unlock_irqrestore(lock, flags) \
409 _raw_write_unlock(lock); \
410 local_irq_restore(flags); \
415 #define _read_unlock_irq(lock) \
417 _raw_read_unlock(lock); \
418 local_irq_enable(); \
423 #define _read_unlock_bh(lock) \
425 _raw_read_unlock(lock); \
426 preempt_enable_no_resched(); \
431 #define _write_unlock_irq(lock) \
433 _raw_write_unlock(lock); \
434 local_irq_enable(); \
442 * Define the various spin_lock and rw_lock methods. Note we define these
443 * regardless of whether CONFIG_SMP or CONFIG_PREEMPT are set. The various
444 * methods are defined as nops in the case they are not required.
446 #define spin_trylock(lock) __cond_lock(_spin_trylock(lock))
447 #define read_trylock(lock) __cond_lock(_read_trylock(lock))
448 #define write_trylock(lock) __cond_lock(_write_trylock(lock))
450 #define spin_lock(lock) _spin_lock(lock)
451 #define write_lock(lock) _write_lock(lock)
452 #define read_lock(lock) _read_lock(lock)
455 #define spin_lock_irqsave(lock, flags) flags = _spin_lock_irqsave(lock)
456 #define read_lock_irqsave(lock, flags) flags = _read_lock_irqsave(lock)
457 #define write_lock_irqsave(lock, flags) flags = _write_lock_irqsave(lock)
459 #define spin_lock_irqsave(lock, flags) _spin_lock_irqsave(lock, flags)
460 #define read_lock_irqsave(lock, flags) _read_lock_irqsave(lock, flags)
461 #define write_lock_irqsave(lock, flags) _write_lock_irqsave(lock, flags)
464 #define spin_lock_irq(lock) _spin_lock_irq(lock)
465 #define spin_lock_bh(lock) _spin_lock_bh(lock)
467 #define read_lock_irq(lock) _read_lock_irq(lock)
468 #define read_lock_bh(lock) _read_lock_bh(lock)
470 #define write_lock_irq(lock) _write_lock_irq(lock)
471 #define write_lock_bh(lock) _write_lock_bh(lock)
473 #define spin_unlock(lock) _spin_unlock(lock)
474 #define write_unlock(lock) _write_unlock(lock)
475 #define read_unlock(lock) _read_unlock(lock)
477 #define spin_unlock_irqrestore(lock, flags) _spin_unlock_irqrestore(lock, flags)
478 #define spin_unlock_irq(lock) _spin_unlock_irq(lock)
479 #define spin_unlock_bh(lock) _spin_unlock_bh(lock)
481 #define read_unlock_irqrestore(lock, flags) _read_unlock_irqrestore(lock, flags)
482 #define read_unlock_irq(lock) _read_unlock_irq(lock)
483 #define read_unlock_bh(lock) _read_unlock_bh(lock)
485 #define write_unlock_irqrestore(lock, flags) _write_unlock_irqrestore(lock, flags)
486 #define write_unlock_irq(lock) _write_unlock_irq(lock)
487 #define write_unlock_bh(lock) _write_unlock_bh(lock)
489 #define spin_trylock_bh(lock) __cond_lock(_spin_trylock_bh(lock))
491 #define spin_trylock_irq(lock) \
493 local_irq_disable(); \
494 _spin_trylock(lock) ? \
495 1 : ({local_irq_enable(); 0; }); \
498 #define spin_trylock_irqsave(lock, flags) \
500 local_irq_save(flags); \
501 _spin_trylock(lock) ? \
502 1 : ({local_irq_restore(flags); 0;}); \
505 #ifdef CONFIG_LOCKMETER
506 extern void _metered_spin_lock (spinlock_t
*lock
);
507 extern void _metered_spin_unlock (spinlock_t
*lock
);
508 extern int _metered_spin_trylock(spinlock_t
*lock
);
509 extern void _metered_read_lock (rwlock_t
*lock
);
510 extern void _metered_read_unlock (rwlock_t
*lock
);
511 extern void _metered_write_lock (rwlock_t
*lock
);
512 extern void _metered_write_unlock (rwlock_t
*lock
);
513 extern int _metered_read_trylock (rwlock_t
*lock
);
514 extern int _metered_write_trylock(rwlock_t
*lock
);
517 /* "lock on reference count zero" */
518 #ifndef ATOMIC_DEC_AND_LOCK
519 #include <asm/atomic.h>
520 extern int _atomic_dec_and_lock(atomic_t
*atomic
, spinlock_t
*lock
);
523 #define atomic_dec_and_lock(atomic,lock) __cond_lock(_atomic_dec_and_lock(atomic,lock))
526 * bit-based spin_lock()
528 * Don't use this unless you really need to: spin_lock() and spin_unlock()
529 * are significantly faster.
531 static inline void bit_spin_lock(int bitnum
, unsigned long *addr
)
534 * Assuming the lock is uncontended, this never enters
535 * the body of the outer loop. If it is contended, then
536 * within the inner loop a non-atomic test is used to
537 * busywait with less bus contention for a good time to
538 * attempt to acquire the lock bit.
541 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
542 while (test_and_set_bit(bitnum
, addr
)) {
543 while (test_bit(bitnum
, addr
)) {
554 * Return true if it was acquired
556 static inline int bit_spin_trylock(int bitnum
, unsigned long *addr
)
559 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
560 if (test_and_set_bit(bitnum
, addr
)) {
570 * bit-based spin_unlock()
572 static inline void bit_spin_unlock(int bitnum
, unsigned long *addr
)
574 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
575 BUG_ON(!test_bit(bitnum
, addr
));
576 smp_mb__before_clear_bit();
577 clear_bit(bitnum
, addr
);
584 * Return true if the lock is held.
586 static inline int bit_spin_is_locked(int bitnum
, unsigned long *addr
)
588 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
589 return test_bit(bitnum
, addr
);
590 #elif defined CONFIG_PREEMPT
591 return preempt_count();
597 #define DEFINE_SPINLOCK(x) spinlock_t x = SPIN_LOCK_UNLOCKED
598 #define DEFINE_RWLOCK(x) rwlock_t x = RW_LOCK_UNLOCKED
601 * spin_can_lock - would spin_trylock() succeed?
602 * @lock: the spinlock in question.
604 #define spin_can_lock(lock) (!spin_is_locked(lock))
606 #endif /* __LINUX_SPINLOCK_H */