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 \
36 * If CONFIG_SMP is set, pull in the _raw_* definitions
39 #include <asm/spinlock.h>
41 #define __lockfunc fastcall __attribute__((section(".spinlock.text")))
43 int __lockfunc
_spin_trylock(spinlock_t
*lock
);
44 int __lockfunc
_write_trylock(rwlock_t
*lock
);
45 void __lockfunc
_spin_lock(spinlock_t
*lock
);
46 void __lockfunc
_write_lock(rwlock_t
*lock
);
47 void __lockfunc
_spin_lock(spinlock_t
*lock
);
48 void __lockfunc
_read_lock(rwlock_t
*lock
);
49 void __lockfunc
_spin_unlock(spinlock_t
*lock
);
50 void __lockfunc
_write_unlock(rwlock_t
*lock
);
51 void __lockfunc
_read_unlock(rwlock_t
*lock
);
52 unsigned long __lockfunc
_spin_lock_irqsave(spinlock_t
*lock
);
53 unsigned long __lockfunc
_read_lock_irqsave(rwlock_t
*lock
);
54 unsigned long __lockfunc
_write_lock_irqsave(rwlock_t
*lock
);
55 void __lockfunc
_spin_lock_irq(spinlock_t
*lock
);
56 void __lockfunc
_spin_lock_bh(spinlock_t
*lock
);
57 void __lockfunc
_read_lock_irq(rwlock_t
*lock
);
58 void __lockfunc
_read_lock_bh(rwlock_t
*lock
);
59 void __lockfunc
_write_lock_irq(rwlock_t
*lock
);
60 void __lockfunc
_write_lock_bh(rwlock_t
*lock
);
61 void __lockfunc
_spin_unlock_irqrestore(spinlock_t
*lock
, unsigned long flags
);
62 void __lockfunc
_spin_unlock_irq(spinlock_t
*lock
);
63 void __lockfunc
_spin_unlock_bh(spinlock_t
*lock
);
64 void __lockfunc
_read_unlock_irqrestore(rwlock_t
*lock
, unsigned long flags
);
65 void __lockfunc
_read_unlock_irq(rwlock_t
*lock
);
66 void __lockfunc
_read_unlock_bh(rwlock_t
*lock
);
67 void __lockfunc
_write_unlock_irqrestore(rwlock_t
*lock
, unsigned long flags
);
68 void __lockfunc
_write_unlock_irq(rwlock_t
*lock
);
69 void __lockfunc
_write_unlock_bh(rwlock_t
*lock
);
70 int __lockfunc
_spin_trylock_bh(spinlock_t
*lock
);
71 int in_lock_functions(unsigned long addr
);
74 #define in_lock_functions(ADDR) 0
76 #if !defined(CONFIG_PREEMPT) && !defined(CONFIG_DEBUG_SPINLOCK)
77 # define atomic_dec_and_lock(atomic,lock) atomic_dec_and_test(atomic)
78 # define ATOMIC_DEC_AND_LOCK
81 #ifdef CONFIG_DEBUG_SPINLOCK
83 #define SPINLOCK_MAGIC 0x1D244B3C
86 volatile unsigned long lock
;
87 volatile unsigned int babble
;
92 #define SPIN_LOCK_UNLOCKED (spinlock_t) { SPINLOCK_MAGIC, 0, 10, __FILE__ , NULL, 0}
94 #define spin_lock_init(x) \
96 (x)->magic = SPINLOCK_MAGIC; \
99 (x)->module = __FILE__; \
104 #define CHECK_LOCK(x) \
106 if ((x)->magic != SPINLOCK_MAGIC) { \
107 printk(KERN_ERR "%s:%d: spin_is_locked on uninitialized spinlock %p.\n", \
108 __FILE__, __LINE__, (x)); \
112 #define _raw_spin_lock(x) \
115 if ((x)->lock&&(x)->babble) { \
117 printk("%s:%d: spin_lock(%s:%p) already locked by %s/%d\n", \
118 __FILE__,__LINE__, (x)->module, \
119 (x), (x)->owner, (x)->oline); \
122 (x)->owner = __FILE__; \
123 (x)->oline = __LINE__; \
126 /* without debugging, spin_is_locked on UP always says
127 * FALSE. --> printk if already locked. */
128 #define spin_is_locked(x) \
131 if ((x)->lock&&(x)->babble) { \
133 printk("%s:%d: spin_is_locked(%s:%p) already locked by %s/%d\n", \
134 __FILE__,__LINE__, (x)->module, \
135 (x), (x)->owner, (x)->oline); \
140 /* without debugging, spin_trylock on UP always says
141 * TRUE. --> printk if already locked. */
142 #define _raw_spin_trylock(x) \
145 if ((x)->lock&&(x)->babble) { \
147 printk("%s:%d: spin_trylock(%s:%p) already locked by %s/%d\n", \
148 __FILE__,__LINE__, (x)->module, \
149 (x), (x)->owner, (x)->oline); \
152 (x)->owner = __FILE__; \
153 (x)->oline = __LINE__; \
157 #define spin_unlock_wait(x) \
160 if ((x)->lock&&(x)->babble) { \
162 printk("%s:%d: spin_unlock_wait(%s:%p) owned by %s/%d\n", \
163 __FILE__,__LINE__, (x)->module, (x), \
164 (x)->owner, (x)->oline); \
168 #define _raw_spin_unlock(x) \
171 if (!(x)->lock&&(x)->babble) { \
173 printk("%s:%d: spin_unlock(%s:%p) not locked\n", \
174 __FILE__,__LINE__, (x)->module, (x));\
180 * gcc versions before ~2.95 have a nasty bug with empty initializers.
183 typedef struct { } spinlock_t
;
184 #define SPIN_LOCK_UNLOCKED (spinlock_t) { }
186 typedef struct { int gcc_is_buggy
; } spinlock_t
;
187 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 0 }
191 * If CONFIG_SMP is unset, declare the _raw_* definitions as nops
193 #define spin_lock_init(lock) do { (void)(lock); } while(0)
194 #define _raw_spin_lock(lock) do { (void)(lock); } while(0)
195 #define spin_is_locked(lock) ((void)(lock), 0)
196 #define _raw_spin_trylock(lock) (((void)(lock), 1))
197 #define spin_unlock_wait(lock) (void)(lock);
198 #define _raw_spin_unlock(lock) do { (void)(lock); } while(0)
199 #endif /* CONFIG_DEBUG_SPINLOCK */
201 /* RW spinlocks: No debug version */
204 typedef struct { } rwlock_t
;
205 #define RW_LOCK_UNLOCKED (rwlock_t) { }
207 typedef struct { int gcc_is_buggy
; } rwlock_t
;
208 #define RW_LOCK_UNLOCKED (rwlock_t) { 0 }
211 #define rwlock_init(lock) do { (void)(lock); } while(0)
212 #define _raw_read_lock(lock) do { (void)(lock); } while(0)
213 #define _raw_read_unlock(lock) do { (void)(lock); } while(0)
214 #define _raw_write_lock(lock) do { (void)(lock); } while(0)
215 #define _raw_write_unlock(lock) do { (void)(lock); } while(0)
216 #define _raw_write_trylock(lock) ({ (void)(lock); (1); })
218 #define _spin_trylock(lock) ({preempt_disable(); _raw_spin_trylock(lock) ? \
219 1 : ({preempt_enable(); 0;});})
221 #define _write_trylock(lock) ({preempt_disable(); _raw_write_trylock(lock) ? \
222 1 : ({preempt_enable(); 0;});})
224 #define _spin_trylock_bh(lock) ({preempt_disable(); local_bh_disable(); \
225 _raw_spin_trylock(lock) ? \
226 1 : ({preempt_enable(); local_bh_enable(); 0;});})
228 #define _spin_lock(lock) \
231 _raw_spin_lock(lock); \
234 #define _write_lock(lock) \
237 _raw_write_lock(lock); \
240 #define _read_lock(lock) \
243 _raw_read_lock(lock); \
246 #define _spin_unlock(lock) \
248 _raw_spin_unlock(lock); \
252 #define _write_unlock(lock) \
254 _raw_write_unlock(lock); \
258 #define _read_unlock(lock) \
260 _raw_read_unlock(lock); \
264 #define _spin_lock_irqsave(lock, flags) \
266 local_irq_save(flags); \
268 _raw_spin_lock(lock); \
271 #define _spin_lock_irq(lock) \
273 local_irq_disable(); \
275 _raw_spin_lock(lock); \
278 #define _spin_lock_bh(lock) \
280 local_bh_disable(); \
282 _raw_spin_lock(lock); \
285 #define _read_lock_irqsave(lock, flags) \
287 local_irq_save(flags); \
289 _raw_read_lock(lock); \
292 #define _read_lock_irq(lock) \
294 local_irq_disable(); \
296 _raw_read_lock(lock); \
299 #define _read_lock_bh(lock) \
301 local_bh_disable(); \
303 _raw_read_lock(lock); \
306 #define _write_lock_irqsave(lock, flags) \
308 local_irq_save(flags); \
310 _raw_write_lock(lock); \
313 #define _write_lock_irq(lock) \
315 local_irq_disable(); \
317 _raw_write_lock(lock); \
320 #define _write_lock_bh(lock) \
322 local_bh_disable(); \
324 _raw_write_lock(lock); \
327 #define _spin_unlock_irqrestore(lock, flags) \
329 _raw_spin_unlock(lock); \
330 local_irq_restore(flags); \
334 #define _raw_spin_unlock_irqrestore(lock, flags) \
336 _raw_spin_unlock(lock); \
337 local_irq_restore(flags); \
340 #define _spin_unlock_irq(lock) \
342 _raw_spin_unlock(lock); \
343 local_irq_enable(); \
347 #define _spin_unlock_bh(lock) \
349 _raw_spin_unlock(lock); \
354 #define _write_unlock_bh(lock) \
356 _raw_write_unlock(lock); \
361 #define _read_unlock_irqrestore(lock, flags) \
363 _raw_read_unlock(lock); \
364 local_irq_restore(flags); \
368 #define _write_unlock_irqrestore(lock, flags) \
370 _raw_write_unlock(lock); \
371 local_irq_restore(flags); \
375 #define _read_unlock_irq(lock) \
377 _raw_read_unlock(lock); \
378 local_irq_enable(); \
382 #define _read_unlock_bh(lock) \
384 _raw_read_unlock(lock); \
389 #define _write_unlock_irq(lock) \
391 _raw_write_unlock(lock); \
392 local_irq_enable(); \
399 * Define the various spin_lock and rw_lock methods. Note we define these
400 * regardless of whether CONFIG_SMP or CONFIG_PREEMPT are set. The various
401 * methods are defined as nops in the case they are not required.
403 #define spin_trylock(lock) _spin_trylock(lock)
404 #define write_trylock(lock) _write_trylock(lock)
406 /* Where's read_trylock? */
408 #define spin_lock(lock) _spin_lock(lock)
409 #define write_lock(lock) _write_lock(lock)
410 #define read_lock(lock) _read_lock(lock)
411 #define spin_unlock(lock) _spin_unlock(lock)
412 #define write_unlock(lock) _write_unlock(lock)
413 #define read_unlock(lock) _read_unlock(lock)
416 #define spin_lock_irqsave(lock, flags) flags = _spin_lock_irqsave(lock)
417 #define read_lock_irqsave(lock, flags) flags = _read_lock_irqsave(lock)
418 #define write_lock_irqsave(lock, flags) flags = _write_lock_irqsave(lock)
420 #define spin_lock_irqsave(lock, flags) _spin_lock_irqsave(lock, flags)
421 #define read_lock_irqsave(lock, flags) _read_lock_irqsave(lock, flags)
422 #define write_lock_irqsave(lock, flags) _write_lock_irqsave(lock, flags)
425 #define spin_lock_irq(lock) _spin_lock_irq(lock)
426 #define spin_lock_bh(lock) _spin_lock_bh(lock)
428 #define read_lock_irq(lock) _read_lock_irq(lock)
429 #define read_lock_bh(lock) _read_lock_bh(lock)
431 #define write_lock_irq(lock) _write_lock_irq(lock)
432 #define write_lock_bh(lock) _write_lock_bh(lock)
433 #define spin_unlock_irqrestore(lock, flags) _spin_unlock_irqrestore(lock, flags)
434 #define spin_unlock_irq(lock) _spin_unlock_irq(lock)
435 #define spin_unlock_bh(lock) _spin_unlock_bh(lock)
437 #define read_unlock_irqrestore(lock, flags) _read_unlock_irqrestore(lock, flags)
438 #define read_unlock_irq(lock) _read_unlock_irq(lock)
439 #define read_unlock_bh(lock) _read_unlock_bh(lock)
441 #define write_unlock_irqrestore(lock, flags) _write_unlock_irqrestore(lock, flags)
442 #define write_unlock_irq(lock) _write_unlock_irq(lock)
443 #define write_unlock_bh(lock) _write_unlock_bh(lock)
445 #define spin_trylock_bh(lock) _spin_trylock_bh(lock)
447 #ifdef CONFIG_LOCKMETER
448 extern void _metered_spin_lock (spinlock_t
*lock
);
449 extern void _metered_spin_unlock (spinlock_t
*lock
);
450 extern int _metered_spin_trylock(spinlock_t
*lock
);
451 extern void _metered_read_lock (rwlock_t
*lock
);
452 extern void _metered_read_unlock (rwlock_t
*lock
);
453 extern void _metered_write_lock (rwlock_t
*lock
);
454 extern void _metered_write_unlock (rwlock_t
*lock
);
455 extern int _metered_write_trylock(rwlock_t
*lock
);
458 /* "lock on reference count zero" */
459 #ifndef ATOMIC_DEC_AND_LOCK
460 #include <asm/atomic.h>
461 extern int atomic_dec_and_lock(atomic_t
*atomic
, spinlock_t
*lock
);
465 * bit-based spin_lock()
467 * Don't use this unless you really need to: spin_lock() and spin_unlock()
468 * are significantly faster.
470 static inline void bit_spin_lock(int bitnum
, unsigned long *addr
)
473 * Assuming the lock is uncontended, this never enters
474 * the body of the outer loop. If it is contended, then
475 * within the inner loop a non-atomic test is used to
476 * busywait with less bus contention for a good time to
477 * attempt to acquire the lock bit.
480 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
481 while (test_and_set_bit(bitnum
, addr
)) {
482 while (test_bit(bitnum
, addr
))
489 * Return true if it was acquired
491 static inline int bit_spin_trylock(int bitnum
, unsigned long *addr
)
493 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
497 ret
= !test_and_set_bit(bitnum
, addr
);
508 * bit-based spin_unlock()
510 static inline void bit_spin_unlock(int bitnum
, unsigned long *addr
)
512 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
513 BUG_ON(!test_bit(bitnum
, addr
));
514 smp_mb__before_clear_bit();
515 clear_bit(bitnum
, addr
);
521 * Return true if the lock is held.
523 static inline int bit_spin_is_locked(int bitnum
, unsigned long *addr
)
525 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
526 return test_bit(bitnum
, addr
);
527 #elif defined CONFIG_PREEMPT
528 return preempt_count();
534 #endif /* __LINUX_SPINLOCK_H */