1 #ifndef __LINUX_SPINLOCK_H
2 #define __LINUX_SPINLOCK_H
4 #include <linux/config.h>
7 * These are the generic versions of the spinlocks and read-write
10 #define spin_lock_irqsave(lock, flags) do { local_irq_save(flags); spin_lock(lock); } while (0)
11 #define spin_lock_irq(lock) do { local_irq_disable(); spin_lock(lock); } while (0)
12 #define spin_lock_bh(lock) do { local_bh_disable(); spin_lock(lock); } while (0)
14 #define read_lock_irqsave(lock, flags) do { local_irq_save(flags); read_lock(lock); } while (0)
15 #define read_lock_irq(lock) do { local_irq_disable(); read_lock(lock); } while (0)
16 #define read_lock_bh(lock) do { local_bh_disable(); read_lock(lock); } while (0)
18 #define write_lock_irqsave(lock, flags) do { local_irq_save(flags); write_lock(lock); } while (0)
19 #define write_lock_irq(lock) do { local_irq_disable(); write_lock(lock); } while (0)
20 #define write_lock_bh(lock) do { local_bh_disable(); write_lock(lock); } while (0)
22 #define spin_unlock_irqrestore(lock, flags) do { spin_unlock(lock); local_irq_restore(flags); } while (0)
23 #define spin_unlock_irq(lock) do { spin_unlock(lock); local_irq_enable(); } while (0)
24 #define spin_unlock_bh(lock) do { spin_unlock(lock); local_bh_enable(); } while (0)
26 #define read_unlock_irqrestore(lock, flags) do { read_unlock(lock); local_irq_restore(flags); } while (0)
27 #define read_unlock_irq(lock) do { read_unlock(lock); local_irq_enable(); } while (0)
28 #define read_unlock_bh(lock) do { read_unlock(lock); local_bh_enable(); } while (0)
30 #define write_unlock_irqrestore(lock, flags) do { write_unlock(lock); local_irq_restore(flags); } while (0)
31 #define write_unlock_irq(lock) do { write_unlock(lock); local_irq_enable(); } while (0)
32 #define write_unlock_bh(lock) do { write_unlock(lock); local_bh_enable(); } while (0)
33 #define spin_trylock_bh(lock) ({ int __r; local_bh_disable();\
34 __r = spin_trylock(lock); \
35 if (!__r) local_bh_enable(); \
38 /* Must define these before including other files, inline functions need them */
40 #include <linux/stringify.h>
42 #define LOCK_SECTION_NAME \
43 ".text.lock." __stringify(KBUILD_BASENAME)
45 #define LOCK_SECTION_START(extra) \
48 ".ifndef " LOCK_SECTION_NAME "\n\t" \
49 LOCK_SECTION_NAME ":\n\t" \
52 #define LOCK_SECTION_END \
56 #include <asm/spinlock.h>
58 #elif !defined(spin_lock_init) /* !SMP and spin_lock_init not previously
59 defined (e.g. by including asm/spinlock.h */
61 #define DEBUG_SPINLOCKS 0 /* 0 == no debugging, 1 == maintain lock state, 2 == full debug */
63 #if (DEBUG_SPINLOCKS < 1)
65 #define atomic_dec_and_lock(atomic,lock) atomic_dec_and_test(atomic)
66 #define ATOMIC_DEC_AND_LOCK
69 * Your basic spinlocks, allowing only a single CPU anywhere
71 * Some older gcc versions had a nasty bug with empty initializers.
72 * (XXX: could someone please confirm whether egcs 1.1 still has this bug?)
74 #if (__GNUC__ > 2 || __GNUC_MINOR__ > 95)
75 typedef struct { } spinlock_t;
76 #define SPIN_LOCK_UNLOCKED (spinlock_t) { }
78 typedef struct { int gcc_is_buggy; } spinlock_t;
79 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 0 }
82 #define spin_lock_init(lock) do { } while(0)
83 #define spin_lock(lock) (void)(lock) /* Not "unused variable". */
84 #define spin_is_locked(lock) (0)
85 #define spin_trylock(lock) ({1; })
86 #define spin_unlock_wait(lock) do { } while(0)
87 #define spin_unlock(lock) do { } while(0)
89 #elif (DEBUG_SPINLOCKS < 2)
92 volatile unsigned long lock;
94 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 0 }
96 #define spin_lock_init(x) do { (x)->lock = 0; } while (0)
97 #define spin_is_locked(lock) (test_bit(0,(lock)))
98 #define spin_trylock(lock) (!test_and_set_bit(0,(lock)))
100 #define spin_lock(x) do { (x)->lock = 1; } while (0)
101 #define spin_unlock_wait(x) do { } while (0)
102 #define spin_unlock(x) do { (x)->lock = 0; } while (0)
104 #else /* (DEBUG_SPINLOCKS >= 2) */
107 volatile unsigned long lock;
108 volatile unsigned int babble;
111 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 0, 25, __BASE_FILE__ }
113 #include <linux/kernel.h>
115 #define spin_lock_init(x) do { (x)->lock = 0; } while (0)
116 #define spin_is_locked(lock) (test_bit(0,(lock)))
117 #define spin_trylock(lock) (!test_and_set_bit(0,(lock)))
119 #define spin_lock(x) do {unsigned long __spinflags; save_flags(__spinflags); cli(); if ((x)->lock&&(x)->babble) {printk("%s:%d: spin_lock(%s:%p) already locked\n", __BASE_FILE__,__LINE__, (x)->module, (x));(x)->babble--;} (x)->lock = 1; restore_flags(__spinflags);} while (0)
120 #define spin_unlock_wait(x) do {unsigned long __spinflags; save_flags(__spinflags); cli(); if ((x)->lock&&(x)->babble) {printk("%s:%d: spin_unlock_wait(%s:%p) deadlock\n", __BASE_FILE__,__LINE__, (x)->module, (x));(x)->babble--;} restore_flags(__spinflags);} while (0)
121 #define spin_unlock(x) do {unsigned long __spinflags; save_flags(__spinflags); cli(); if (!(x)->lock&&(x)->babble) {printk("%s:%d: spin_unlock(%s:%p) not locked\n", __BASE_FILE__,__LINE__, (x)->module, (x));(x)->babble--;} (x)->lock = 0; restore_flags(__spinflags);} while (0)
123 #endif /* DEBUG_SPINLOCKS */
126 * Read-write spinlocks, allowing multiple readers
127 * but only one writer.
129 * NOTE! it is quite common to have readers in interrupts
130 * but no interrupt writers. For those circumstances we
131 * can "mix" irq-safe locks - any writer needs to get a
132 * irq-safe write-lock, but readers can get non-irqsafe
135 * Some older gcc versions had a nasty bug with empty initializers.
136 * (XXX: could someone please confirm whether egcs 1.1 still has this bug?)
138 #if (__GNUC__ > 2 || __GNUC_MINOR__ > 91)
139 typedef struct { } rwlock_t;
140 #define RW_LOCK_UNLOCKED (rwlock_t) { }
142 typedef struct { int gcc_is_buggy; } rwlock_t;
143 #define RW_LOCK_UNLOCKED (rwlock_t) { 0 }
146 #define rwlock_init(lock) do { } while(0)
147 #define read_lock(lock) (void)(lock) /* Not "unused variable". */
148 #define read_unlock(lock) do { } while(0)
149 #define write_lock(lock) (void)(lock) /* Not "unused variable". */
150 #define write_unlock(lock) do { } while(0)
154 /* "lock on reference count zero" */
155 #ifndef ATOMIC_DEC_AND_LOCK
156 #include <asm/atomic.h>
157 extern int atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock);
161 #include <linux/cache.h>
165 char fill_up[(SMP_CACHE_BYTES)];
166 } spinlock_cacheline_t __attribute__ ((aligned(SMP_CACHE_BYTES)));
172 } spinlock_cacheline_t;
176 #endif /* __LINUX_SPINLOCK_H */