1 #ifndef __ASM_SPINLOCK_H
2 #define __ASM_SPINLOCK_H
4 #include <asm/atomic.h>
5 #include <asm/rwlock.h>
7 #include <linux/config.h>
8 #include <linux/compiler.h>
10 asmlinkage int printk(const char * fmt, ...)
11 __attribute__ ((format (printf, 1, 2)));
14 * Your basic SMP spinlocks, allowing only a single CPU anywhere
18 volatile unsigned int lock;
19 #ifdef CONFIG_DEBUG_SPINLOCK
24 #define SPINLOCK_MAGIC 0xdead4ead
26 #ifdef CONFIG_DEBUG_SPINLOCK
27 #define SPINLOCK_MAGIC_INIT , SPINLOCK_MAGIC
29 #define SPINLOCK_MAGIC_INIT /* */
32 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 1 SPINLOCK_MAGIC_INIT }
34 #define spin_lock_init(x) do { *(x) = SPIN_LOCK_UNLOCKED; } while(0)
37 * Simple spin lock operations. There are two variants, one clears IRQ's
38 * on the local processor, one does not.
40 * We make no fairness assumptions. They have a cost.
43 #define spin_is_locked(x) (*(volatile signed char *)(&(x)->lock) <= 0)
44 #define spin_unlock_wait(x) do { barrier(); } while(spin_is_locked(x))
46 #define spin_lock_string \
48 "lock ; decb %0\n\t" \
50 LOCK_SECTION_START("") \
58 #define spin_lock_string_flags \
60 "lock ; decb %0\n\t" \
62 LOCK_SECTION_START("") \
64 "testl $0x200, %1\n\t" \
76 * This works. Despite all the confusion.
77 * (except on PPro SMP or if we are using OOSTORE)
78 * (PPro errata 66, 92)
81 #if !defined(CONFIG_X86_OOSTORE) && !defined(CONFIG_X86_PPRO_FENCE)
83 #define spin_unlock_string \
85 :"=m" (lock->lock) : : "memory"
88 static inline void _raw_spin_unlock(spinlock_t *lock)
90 #ifdef CONFIG_DEBUG_SPINLOCK
91 BUG_ON(lock->magic != SPINLOCK_MAGIC);
92 BUG_ON(!spin_is_locked(lock));
101 #define spin_unlock_string \
103 :"=q" (oldval), "=m" (lock->lock) \
104 :"0" (oldval) : "memory"
106 static inline void _raw_spin_unlock(spinlock_t *lock)
109 #ifdef CONFIG_DEBUG_SPINLOCK
110 BUG_ON(lock->magic != SPINLOCK_MAGIC);
111 BUG_ON(!spin_is_locked(lock));
113 __asm__ __volatile__(
120 static inline int _raw_spin_trylock(spinlock_t *lock)
123 __asm__ __volatile__(
125 :"=q" (oldval), "=m" (lock->lock)
126 :"0" (0) : "memory");
130 static inline void _raw_spin_lock(spinlock_t *lock)
132 #ifdef CONFIG_DEBUG_SPINLOCK
135 if (unlikely(lock->magic != SPINLOCK_MAGIC)) {
136 printk("eip: %p\n", &&here);
140 __asm__ __volatile__(
142 :"=m" (lock->lock) : : "memory");
145 static inline void _raw_spin_lock_flags (spinlock_t *lock, unsigned long flags)
147 #ifdef CONFIG_DEBUG_SPINLOCK
150 if (unlikely(lock->magic != SPINLOCK_MAGIC)) {
151 printk("eip: %p\n", &&here);
155 __asm__ __volatile__(
156 spin_lock_string_flags
157 :"=m" (lock->lock) : "r" (flags) : "memory");
161 * Read-write spinlocks, allowing multiple readers
162 * but only one writer.
164 * NOTE! it is quite common to have readers in interrupts
165 * but no interrupt writers. For those circumstances we
166 * can "mix" irq-safe locks - any writer needs to get a
167 * irq-safe write-lock, but readers can get non-irqsafe
171 volatile unsigned int lock;
172 #ifdef CONFIG_DEBUG_SPINLOCK
177 #define RWLOCK_MAGIC 0xdeaf1eed
179 #ifdef CONFIG_DEBUG_SPINLOCK
180 #define RWLOCK_MAGIC_INIT , RWLOCK_MAGIC
182 #define RWLOCK_MAGIC_INIT /* */
185 #define RW_LOCK_UNLOCKED (rwlock_t) { RW_LOCK_BIAS RWLOCK_MAGIC_INIT }
187 #define rwlock_init(x) do { *(x) = RW_LOCK_UNLOCKED; } while(0)
189 #define rwlock_is_locked(x) ((x)->lock != RW_LOCK_BIAS)
192 * On x86, we implement read-write locks as a 32-bit counter
193 * with the high bit (sign) being the "contended" bit.
195 * The inline assembly is non-obvious. Think about it.
197 * Changed to use the same technique as rw semaphores. See
198 * semaphore.h for details. -ben
200 /* the spinlock helpers are in arch/i386/kernel/semaphore.c */
202 static inline void _raw_read_lock(rwlock_t *rw)
204 #ifdef CONFIG_DEBUG_SPINLOCK
205 BUG_ON(rw->magic != RWLOCK_MAGIC);
207 __build_read_lock(rw, "__read_lock_failed");
210 static inline void _raw_write_lock(rwlock_t *rw)
212 #ifdef CONFIG_DEBUG_SPINLOCK
213 BUG_ON(rw->magic != RWLOCK_MAGIC);
215 __build_write_lock(rw, "__write_lock_failed");
218 #define _raw_read_unlock(rw) asm volatile("lock ; incl %0" :"=m" ((rw)->lock) : : "memory")
219 #define _raw_write_unlock(rw) asm volatile("lock ; addl $" RW_LOCK_BIAS_STR ",%0":"=m" ((rw)->lock) : : "memory")
221 static inline int _raw_write_trylock(rwlock_t *lock)
223 atomic_t *count = (atomic_t *)lock;
224 if (atomic_sub_and_test(RW_LOCK_BIAS, count))
226 atomic_add(RW_LOCK_BIAS, count);
230 #endif /* __ASM_SPINLOCK_H */