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>
43 #define _raw_spin_lock_flags(lock, flags) _raw_spin_lock(lock)
45 #if !defined(CONFIG_PREEMPT) && !defined(CONFIG_DEBUG_SPINLOCK)
46 # define atomic_dec_and_lock(atomic,lock) atomic_dec_and_test(atomic)
47 # define ATOMIC_DEC_AND_LOCK
50 #ifdef CONFIG_DEBUG_SPINLOCK
52 #define SPINLOCK_MAGIC 0x1D244B3C
55 volatile unsigned long lock;
56 volatile unsigned int babble;
61 #define SPIN_LOCK_UNLOCKED (spinlock_t) { SPINLOCK_MAGIC, 0, 10, __FILE__ , NULL, 0}
63 #define spin_lock_init(x) \
65 (x)->magic = SPINLOCK_MAGIC; \
68 (x)->module = __FILE__; \
73 #define CHECK_LOCK(x) \
75 if ((x)->magic != SPINLOCK_MAGIC) { \
76 printk(KERN_ERR "%s:%d: spin_is_locked on uninitialized spinlock %p.\n", \
77 __FILE__, __LINE__, (x)); \
81 #define _raw_spin_lock(x) \
84 if ((x)->lock&&(x)->babble) { \
86 printk("%s:%d: spin_lock(%s:%p) already locked by %s/%d\n", \
87 __FILE__,__LINE__, (x)->module, \
88 (x), (x)->owner, (x)->oline); \
91 (x)->owner = __FILE__; \
92 (x)->oline = __LINE__; \
95 /* without debugging, spin_is_locked on UP always says
96 * FALSE. --> printk if already locked. */
97 #define spin_is_locked(x) \
100 if ((x)->lock&&(x)->babble) { \
102 printk("%s:%d: spin_is_locked(%s:%p) already locked by %s/%d\n", \
103 __FILE__,__LINE__, (x)->module, \
104 (x), (x)->owner, (x)->oline); \
109 /* without debugging, spin_trylock on UP always says
110 * TRUE. --> printk if already locked. */
111 #define _raw_spin_trylock(x) \
114 if ((x)->lock&&(x)->babble) { \
116 printk("%s:%d: spin_trylock(%s:%p) already locked by %s/%d\n", \
117 __FILE__,__LINE__, (x)->module, \
118 (x), (x)->owner, (x)->oline); \
121 (x)->owner = __FILE__; \
122 (x)->oline = __LINE__; \
126 #define spin_unlock_wait(x) \
129 if ((x)->lock&&(x)->babble) { \
131 printk("%s:%d: spin_unlock_wait(%s:%p) owned by %s/%d\n", \
132 __FILE__,__LINE__, (x)->module, (x), \
133 (x)->owner, (x)->oline); \
137 #define _raw_spin_unlock(x) \
140 if (!(x)->lock&&(x)->babble) { \
142 printk("%s:%d: spin_unlock(%s:%p) not locked\n", \
143 __FILE__,__LINE__, (x)->module, (x));\
149 * gcc versions before ~2.95 have a nasty bug with empty initializers.
152 typedef struct { } spinlock_t;
153 #define SPIN_LOCK_UNLOCKED (spinlock_t) { }
155 typedef struct { int gcc_is_buggy; } spinlock_t;
156 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 0 }
160 * If CONFIG_SMP is unset, declare the _raw_* definitions as nops
162 #define spin_lock_init(lock) do { (void)(lock); } while(0)
163 #define _raw_spin_lock(lock) do { (void)(lock); } while(0)
164 #define spin_is_locked(lock) ((void)(lock), 0)
165 #define _raw_spin_trylock(lock) ((void)(lock), 1)
166 #define spin_unlock_wait(lock) do { (void)(lock); } while(0)
167 #define _raw_spin_unlock(lock) do { (void)(lock); } while(0)
168 #endif /* CONFIG_DEBUG_SPINLOCK */
170 /* RW spinlocks: No debug version */
173 typedef struct { } rwlock_t;
174 #define RW_LOCK_UNLOCKED (rwlock_t) { }
176 typedef struct { int gcc_is_buggy; } rwlock_t;
177 #define RW_LOCK_UNLOCKED (rwlock_t) { 0 }
180 #define rwlock_init(lock) do { (void)(lock); } while(0)
181 #define _raw_read_lock(lock) do { (void)(lock); } while(0)
182 #define _raw_read_unlock(lock) do { (void)(lock); } while(0)
183 #define _raw_write_lock(lock) do { (void)(lock); } while(0)
184 #define _raw_write_unlock(lock) do { (void)(lock); } while(0)
185 #define _raw_write_trylock(lock) ({ (void)(lock); (1); })
190 * Define the various spin_lock and rw_lock methods. Note we define these
191 * regardless of whether CONFIG_SMP or CONFIG_PREEMPT are set. The various
192 * methods are defined as nops in the case they are not required.
194 #define spin_trylock(lock) ({preempt_disable(); _raw_spin_trylock(lock) ? \
195 1 : ({preempt_enable(); 0;});})
197 #define write_trylock(lock) ({preempt_disable();_raw_write_trylock(lock) ? \
198 1 : ({preempt_enable(); 0;});})
200 /* Where's read_trylock? */
202 #if defined(CONFIG_SMP) && defined(CONFIG_PREEMPT)
203 void __preempt_spin_lock(spinlock_t *lock);
204 void __preempt_write_lock(rwlock_t *lock);
206 #define spin_lock(lock) \
209 if (unlikely(!_raw_spin_trylock(lock))) \
210 __preempt_spin_lock(lock); \
213 #define write_lock(lock) \
216 if (unlikely(!_raw_write_trylock(lock))) \
217 __preempt_write_lock(lock); \
221 #define spin_lock(lock) \
224 _raw_spin_lock(lock); \
227 #define write_lock(lock) \
230 _raw_write_lock(lock); \
234 #define read_lock(lock) \
237 _raw_read_lock(lock); \
240 #define spin_unlock(lock) \
242 _raw_spin_unlock(lock); \
246 #define write_unlock(lock) \
248 _raw_write_unlock(lock); \
252 #define read_unlock(lock) \
254 _raw_read_unlock(lock); \
258 #define spin_lock_irqsave(lock, flags) \
260 local_irq_save(flags); \
262 _raw_spin_lock_flags(lock, flags); \
265 #define spin_lock_irq(lock) \
267 local_irq_disable(); \
269 _raw_spin_lock(lock); \
272 #define spin_lock_bh(lock) \
274 local_bh_disable(); \
276 _raw_spin_lock(lock); \
279 #define read_lock_irqsave(lock, flags) \
281 local_irq_save(flags); \
283 _raw_read_lock(lock); \
286 #define read_lock_irq(lock) \
288 local_irq_disable(); \
290 _raw_read_lock(lock); \
293 #define read_lock_bh(lock) \
295 local_bh_disable(); \
297 _raw_read_lock(lock); \
300 #define write_lock_irqsave(lock, flags) \
302 local_irq_save(flags); \
304 _raw_write_lock(lock); \
307 #define write_lock_irq(lock) \
309 local_irq_disable(); \
311 _raw_write_lock(lock); \
314 #define write_lock_bh(lock) \
316 local_bh_disable(); \
318 _raw_write_lock(lock); \
321 #define spin_unlock_irqrestore(lock, flags) \
323 _raw_spin_unlock(lock); \
324 local_irq_restore(flags); \
328 #define _raw_spin_unlock_irqrestore(lock, flags) \
330 _raw_spin_unlock(lock); \
331 local_irq_restore(flags); \
334 #define spin_unlock_irq(lock) \
336 _raw_spin_unlock(lock); \
337 local_irq_enable(); \
341 #define spin_unlock_bh(lock) \
343 _raw_spin_unlock(lock); \
348 #define read_unlock_irqrestore(lock, flags) \
350 _raw_read_unlock(lock); \
351 local_irq_restore(flags); \
355 #define read_unlock_irq(lock) \
357 _raw_read_unlock(lock); \
358 local_irq_enable(); \
362 #define read_unlock_bh(lock) \
364 _raw_read_unlock(lock); \
369 #define write_unlock_irqrestore(lock, flags) \
371 _raw_write_unlock(lock); \
372 local_irq_restore(flags); \
376 #define write_unlock_irq(lock) \
378 _raw_write_unlock(lock); \
379 local_irq_enable(); \
383 #define write_unlock_bh(lock) \
385 _raw_write_unlock(lock); \
390 #define spin_trylock_bh(lock) ({ local_bh_disable(); preempt_disable(); \
391 _raw_spin_trylock(lock) ? 1 : \
392 ({preempt_enable(); local_bh_enable(); 0;});})
394 /* "lock on reference count zero" */
395 #ifndef ATOMIC_DEC_AND_LOCK
396 #include <asm/atomic.h>
397 extern int atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock);
401 * bit-based spin_lock()
403 * Don't use this unless you really need to: spin_lock() and spin_unlock()
404 * are significantly faster.
406 static inline void bit_spin_lock(int bitnum, unsigned long *addr)
409 * Assuming the lock is uncontended, this never enters
410 * the body of the outer loop. If it is contended, then
411 * within the inner loop a non-atomic test is used to
412 * busywait with less bus contention for a good time to
413 * attempt to acquire the lock bit.
416 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
417 while (test_and_set_bit(bitnum, addr)) {
418 while (test_bit(bitnum, addr))
425 * Return true if it was acquired
427 static inline int bit_spin_trylock(int bitnum, unsigned long *addr)
429 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
433 ret = !test_and_set_bit(bitnum, addr);
444 * bit-based spin_unlock()
446 static inline void bit_spin_unlock(int bitnum, unsigned long *addr)
448 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
449 BUG_ON(!test_bit(bitnum, addr));
450 smp_mb__before_clear_bit();
451 clear_bit(bitnum, addr);
457 * Return true if the lock is held.
459 static inline int bit_spin_is_locked(int bitnum, unsigned long *addr)
461 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
462 return test_bit(bitnum, addr);
463 #elif defined CONFIG_PREEMPT
464 return preempt_count();
470 #endif /* __LINUX_SPINLOCK_H */