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 slock;
19 #ifdef CONFIG_DEBUG_SPINLOCK
23 unsigned int break_lock;
27 #define SPINLOCK_MAGIC 0xdead4ead
29 #ifdef CONFIG_DEBUG_SPINLOCK
30 #define SPINLOCK_MAGIC_INIT , SPINLOCK_MAGIC
32 #define SPINLOCK_MAGIC_INIT /* */
35 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 1 SPINLOCK_MAGIC_INIT }
37 #define spin_lock_init(x) do { *(x) = SPIN_LOCK_UNLOCKED; } while(0)
40 * Simple spin lock operations. There are two variants, one clears IRQ's
41 * on the local processor, one does not.
43 * We make no fairness assumptions. They have a cost.
46 #define spin_is_locked(x) (*(volatile signed char *)(&(x)->slock) <= 0)
47 #define spin_unlock_wait(x) do { barrier(); } while(spin_is_locked(x))
49 #define spin_lock_string \
51 "lock ; decb %0\n\t" \
60 #define spin_lock_string_flags \
62 "lock ; decb %0\n\t" \
65 "testl $0x200, %1\n\t" \
77 * This works. Despite all the confusion.
78 * (except on PPro SMP or if we are using OOSTORE)
79 * (PPro errata 66, 92)
82 #if !defined(CONFIG_X86_OOSTORE) && !defined(CONFIG_X86_PPRO_FENCE)
84 #define spin_unlock_string \
86 :"=m" (lock->slock) : : "memory"
89 static inline void _raw_spin_unlock(spinlock_t *lock)
91 #ifdef CONFIG_DEBUG_SPINLOCK
92 BUG_ON(lock->magic != SPINLOCK_MAGIC);
93 BUG_ON(!spin_is_locked(lock));
102 #define spin_unlock_string \
104 :"=q" (oldval), "=m" (lock->slock) \
105 :"0" (oldval) : "memory"
107 static inline void _raw_spin_unlock(spinlock_t *lock)
110 #ifdef CONFIG_DEBUG_SPINLOCK
111 BUG_ON(lock->magic != SPINLOCK_MAGIC);
112 BUG_ON(!spin_is_locked(lock));
114 __asm__ __volatile__(
121 static inline int _raw_spin_trylock(spinlock_t *lock)
124 __asm__ __volatile__(
126 :"=q" (oldval), "=m" (lock->slock)
127 :"0" (0) : "memory");
131 static inline void _raw_spin_lock(spinlock_t *lock)
133 #ifdef CONFIG_DEBUG_SPINLOCK
134 if (unlikely(lock->magic != SPINLOCK_MAGIC)) {
135 printk("eip: %p\n", __builtin_return_address(0));
139 __asm__ __volatile__(
141 :"=m" (lock->slock) : : "memory");
144 static inline void _raw_spin_lock_flags (spinlock_t *lock, unsigned long flags)
146 #ifdef CONFIG_DEBUG_SPINLOCK
147 if (unlikely(lock->magic != SPINLOCK_MAGIC)) {
148 printk("eip: %p\n", __builtin_return_address(0));
152 __asm__ __volatile__(
153 spin_lock_string_flags
154 :"=m" (lock->slock) : "r" (flags) : "memory");
158 * Read-write spinlocks, allowing multiple readers
159 * but only one writer.
161 * NOTE! it is quite common to have readers in interrupts
162 * but no interrupt writers. For those circumstances we
163 * can "mix" irq-safe locks - any writer needs to get a
164 * irq-safe write-lock, but readers can get non-irqsafe
168 volatile unsigned int lock;
169 #ifdef CONFIG_DEBUG_SPINLOCK
172 #ifdef CONFIG_PREEMPT
173 unsigned int break_lock;
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)
190 * read_can_lock - would read_trylock() succeed?
191 * @lock: the rwlock in question.
193 #define read_can_lock(x) ((int)(x)->lock > 0)
196 * write_can_lock - would write_trylock() succeed?
197 * @lock: the rwlock in question.
199 #define write_can_lock(x) ((x)->lock == RW_LOCK_BIAS)
202 * On x86, we implement read-write locks as a 32-bit counter
203 * with the high bit (sign) being the "contended" bit.
205 * The inline assembly is non-obvious. Think about it.
207 * Changed to use the same technique as rw semaphores. See
208 * semaphore.h for details. -ben
210 /* the spinlock helpers are in arch/i386/kernel/semaphore.c */
212 static inline void _raw_read_lock(rwlock_t *rw)
214 #ifdef CONFIG_DEBUG_SPINLOCK
215 BUG_ON(rw->magic != RWLOCK_MAGIC);
217 __build_read_lock(rw, "__read_lock_failed");
220 static inline void _raw_write_lock(rwlock_t *rw)
222 #ifdef CONFIG_DEBUG_SPINLOCK
223 BUG_ON(rw->magic != RWLOCK_MAGIC);
225 __build_write_lock(rw, "__write_lock_failed");
228 #define _raw_read_unlock(rw) asm volatile("lock ; incl %0" :"=m" ((rw)->lock) : : "memory")
229 #define _raw_write_unlock(rw) asm volatile("lock ; addl $" RW_LOCK_BIAS_STR ",%0":"=m" ((rw)->lock) : : "memory")
231 static inline int _raw_read_trylock(rwlock_t *lock)
233 atomic_t *count = (atomic_t *)lock;
235 if (atomic_read(count) >= 0)
241 static inline int _raw_write_trylock(rwlock_t *lock)
243 atomic_t *count = (atomic_t *)lock;
244 if (atomic_sub_and_test(RW_LOCK_BIAS, count))
246 atomic_add(RW_LOCK_BIAS, count);
250 #endif /* __ASM_SPINLOCK_H */