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 #if !defined(CONFIG_PREEMPT) && !defined(CONFIG_DEBUG_SPINLOCK)
44 # define atomic_dec_and_lock(atomic,lock) atomic_dec_and_test(atomic)
45 # define ATOMIC_DEC_AND_LOCK
48 #ifdef CONFIG_DEBUG_SPINLOCK
50 #define SPINLOCK_MAGIC 0x1D244B3C
53 volatile unsigned long lock;
54 volatile unsigned int babble;
59 #define SPIN_LOCK_UNLOCKED (spinlock_t) { SPINLOCK_MAGIC, 0, 10, __FILE__ , NULL, 0}
61 #define spin_lock_init(x) \
63 (x)->magic = SPINLOCK_MAGIC; \
66 (x)->module = __FILE__; \
71 #define CHECK_LOCK(x) \
73 if ((x)->magic != SPINLOCK_MAGIC) { \
74 printk(KERN_ERR "%s:%d: spin_is_locked on uninitialized spinlock %p.\n", \
75 __FILE__, __LINE__, (x)); \
79 #define _raw_spin_lock(x) \
82 if ((x)->lock&&(x)->babble) { \
84 printk("%s:%d: spin_lock(%s:%p) already locked by %s/%d\n", \
85 __FILE__,__LINE__, (x)->module, \
86 (x), (x)->owner, (x)->oline); \
89 (x)->owner = __FILE__; \
90 (x)->oline = __LINE__; \
93 /* without debugging, spin_is_locked on UP always says
94 * FALSE. --> printk if already locked. */
95 #define spin_is_locked(x) \
98 if ((x)->lock&&(x)->babble) { \
100 printk("%s:%d: spin_is_locked(%s:%p) already locked by %s/%d\n", \
101 __FILE__,__LINE__, (x)->module, \
102 (x), (x)->owner, (x)->oline); \
107 /* without debugging, spin_trylock on UP always says
108 * TRUE. --> printk if already locked. */
109 #define _raw_spin_trylock(x) \
112 if ((x)->lock&&(x)->babble) { \
114 printk("%s:%d: spin_trylock(%s:%p) already locked by %s/%d\n", \
115 __FILE__,__LINE__, (x)->module, \
116 (x), (x)->owner, (x)->oline); \
119 (x)->owner = __FILE__; \
120 (x)->oline = __LINE__; \
124 #define spin_unlock_wait(x) \
127 if ((x)->lock&&(x)->babble) { \
129 printk("%s:%d: spin_unlock_wait(%s:%p) owned by %s/%d\n", \
130 __FILE__,__LINE__, (x)->module, (x), \
131 (x)->owner, (x)->oline); \
135 #define _raw_spin_unlock(x) \
138 if (!(x)->lock&&(x)->babble) { \
140 printk("%s:%d: spin_unlock(%s:%p) not locked\n", \
141 __FILE__,__LINE__, (x)->module, (x));\
147 * gcc versions before ~2.95 have a nasty bug with empty initializers.
150 typedef struct { } spinlock_t;
151 #define SPIN_LOCK_UNLOCKED (spinlock_t) { }
153 typedef struct { int gcc_is_buggy; } spinlock_t;
154 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 0 }
158 * If CONFIG_SMP is unset, declare the _raw_* definitions as nops
160 #define spin_lock_init(lock) do { (void)(lock); } while(0)
161 #define _raw_spin_lock(lock) do { (void)(lock); } while(0)
162 #define spin_is_locked(lock) ((void)(lock), 0)
163 #define _raw_spin_trylock(lock) ((void)(lock), 1)
164 #define spin_unlock_wait(lock) do { (void)(lock); } while(0)
165 #define _raw_spin_unlock(lock) do { (void)(lock); } while(0)
166 #endif /* CONFIG_DEBUG_SPINLOCK */
168 /* RW spinlocks: No debug version */
171 typedef struct { } rwlock_t;
172 #define RW_LOCK_UNLOCKED (rwlock_t) { }
174 typedef struct { int gcc_is_buggy; } rwlock_t;
175 #define RW_LOCK_UNLOCKED (rwlock_t) { 0 }
178 #define rwlock_init(lock) do { (void)(lock); } while(0)
179 #define _raw_read_lock(lock) do { (void)(lock); } while(0)
180 #define _raw_read_unlock(lock) do { (void)(lock); } while(0)
181 #define _raw_write_lock(lock) do { (void)(lock); } while(0)
182 #define _raw_write_unlock(lock) do { (void)(lock); } while(0)
183 #define _raw_write_trylock(lock) ({ (void)(lock); (1); })
188 * Define the various spin_lock and rw_lock methods. Note we define these
189 * regardless of whether CONFIG_SMP or CONFIG_PREEMPT are set. The various
190 * methods are defined as nops in the case they are not required.
192 #define spin_trylock(lock) ({preempt_disable(); _raw_spin_trylock(lock) ? \
193 1 : ({preempt_enable(); 0;});})
195 #define write_trylock(lock) ({preempt_disable();_raw_write_trylock(lock) ? \
196 1 : ({preempt_enable(); 0;});})
198 /* Where's read_trylock? */
200 #if defined(CONFIG_SMP) && defined(CONFIG_PREEMPT)
201 void __preempt_spin_lock(spinlock_t *lock);
202 void __preempt_write_lock(rwlock_t *lock);
204 #define spin_lock(lock) \
207 if (unlikely(!_raw_spin_trylock(lock))) \
208 __preempt_spin_lock(lock); \
211 #define write_lock(lock) \
214 if (unlikely(!_raw_write_trylock(lock))) \
215 __preempt_write_lock(lock); \
219 #define spin_lock(lock) \
222 _raw_spin_lock(lock); \
225 #define write_lock(lock) \
228 _raw_write_lock(lock); \
232 #define read_lock(lock) \
235 _raw_read_lock(lock); \
238 #define spin_unlock(lock) \
240 _raw_spin_unlock(lock); \
244 #define write_unlock(lock) \
246 _raw_write_unlock(lock); \
250 #define read_unlock(lock) \
252 _raw_read_unlock(lock); \
256 #define spin_lock_irqsave(lock, flags) \
258 local_irq_save(flags); \
260 _raw_spin_lock(lock); \
263 #define spin_lock_irq(lock) \
265 local_irq_disable(); \
267 _raw_spin_lock(lock); \
270 #define spin_lock_bh(lock) \
272 local_bh_disable(); \
274 _raw_spin_lock(lock); \
277 #define read_lock_irqsave(lock, flags) \
279 local_irq_save(flags); \
281 _raw_read_lock(lock); \
284 #define read_lock_irq(lock) \
286 local_irq_disable(); \
288 _raw_read_lock(lock); \
291 #define read_lock_bh(lock) \
293 local_bh_disable(); \
295 _raw_read_lock(lock); \
298 #define write_lock_irqsave(lock, flags) \
300 local_irq_save(flags); \
302 _raw_write_lock(lock); \
305 #define write_lock_irq(lock) \
307 local_irq_disable(); \
309 _raw_write_lock(lock); \
312 #define write_lock_bh(lock) \
314 local_bh_disable(); \
316 _raw_write_lock(lock); \
319 #define spin_unlock_irqrestore(lock, flags) \
321 _raw_spin_unlock(lock); \
322 local_irq_restore(flags); \
326 #define _raw_spin_unlock_irqrestore(lock, flags) \
328 _raw_spin_unlock(lock); \
329 local_irq_restore(flags); \
332 #define spin_unlock_irq(lock) \
334 _raw_spin_unlock(lock); \
335 local_irq_enable(); \
339 #define spin_unlock_bh(lock) \
341 _raw_spin_unlock(lock); \
346 #define read_unlock_irqrestore(lock, flags) \
348 _raw_read_unlock(lock); \
349 local_irq_restore(flags); \
353 #define read_unlock_irq(lock) \
355 _raw_read_unlock(lock); \
356 local_irq_enable(); \
360 #define read_unlock_bh(lock) \
362 _raw_read_unlock(lock); \
367 #define write_unlock_irqrestore(lock, flags) \
369 _raw_write_unlock(lock); \
370 local_irq_restore(flags); \
374 #define write_unlock_irq(lock) \
376 _raw_write_unlock(lock); \
377 local_irq_enable(); \
381 #define write_unlock_bh(lock) \
383 _raw_write_unlock(lock); \
388 #define spin_trylock_bh(lock) ({ local_bh_disable(); preempt_disable(); \
389 _raw_spin_trylock(lock) ? 1 : \
390 ({preempt_enable(); local_bh_enable(); 0;});})
392 /* "lock on reference count zero" */
393 #ifndef ATOMIC_DEC_AND_LOCK
394 #include <asm/atomic.h>
395 extern int atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock);
399 * bit-based spin_lock()
401 * Don't use this unless you really need to: spin_lock() and spin_unlock()
402 * are significantly faster.
404 static inline void bit_spin_lock(int bitnum, unsigned long *addr)
407 * Assuming the lock is uncontended, this never enters
408 * the body of the outer loop. If it is contended, then
409 * within the inner loop a non-atomic test is used to
410 * busywait with less bus contention for a good time to
411 * attempt to acquire the lock bit.
414 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
415 while (test_and_set_bit(bitnum, addr)) {
416 while (test_bit(bitnum, addr))
423 * Return true if it was acquired
425 static inline int bit_spin_trylock(int bitnum, unsigned long *addr)
427 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
431 ret = !test_and_set_bit(bitnum, addr);
442 * bit-based spin_unlock()
444 static inline void bit_spin_unlock(int bitnum, unsigned long *addr)
446 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
447 BUG_ON(!test_bit(bitnum, addr));
448 smp_mb__before_clear_bit();
449 clear_bit(bitnum, addr);
455 * Return true if the lock is held.
457 static inline int bit_spin_is_locked(int bitnum, unsigned long *addr)
459 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
460 return test_bit(bitnum, addr);
461 #elif defined CONFIG_PREEMPT
462 return preempt_count();
468 #endif /* __LINUX_SPINLOCK_H */