1 #ifndef __ASM_SPINLOCK_H
2 #define __ASM_SPINLOCK_H
4 #include <asm/atomic.h>
5 #include <asm/rwlock.h>
7 #include <asm/processor.h>
8 #include <linux/compiler.h>
11 * Your basic SMP spinlocks, allowing only a single CPU anywhere
13 * Simple spin lock operations. There are two variants, one clears IRQ's
14 * on the local processor, one does not.
16 * We make no fairness assumptions. They have a cost.
18 * (the type definitions are in asm/spinlock_types.h)
21 static inline int __raw_spin_is_locked(raw_spinlock_t *x)
23 return *(volatile signed char *)(&(x)->slock) <= 0;
26 static inline void __raw_spin_lock(raw_spinlock_t *lock)
29 LOCK_PREFIX " ; decb %0\n\t"
37 : "+m" (lock->slock) : : "memory");
41 * It is easier for the lock validator if interrupts are not re-enabled
42 * in the middle of a lock-acquire. This is a performance feature anyway
45 * NOTE: there's an irqs-on section here, which normally would have to be
46 * irq-traced, but on CONFIG_TRACE_IRQFLAGS we never use this variant.
48 #ifndef CONFIG_PROVE_LOCKING
49 static inline void __raw_spin_lock_flags(raw_spinlock_t *lock, unsigned long flags)
53 LOCK_PREFIX " ; decb %0\n\t"
56 "testl $0x200, %1\n\t"
71 : "+m" (lock->slock) : "r" (flags) : "memory");
75 static inline int __raw_spin_trylock(raw_spinlock_t *lock)
80 :"=q" (oldval), "+m" (lock->slock)
86 * __raw_spin_unlock based on writing $1 to the low byte.
87 * This method works. Despite all the confusion.
88 * (except on PPro SMP or if we are using OOSTORE, so we use xchgb there)
89 * (PPro errata 66, 92)
92 #if !defined(CONFIG_X86_OOSTORE) && !defined(CONFIG_X86_PPRO_FENCE)
94 static inline void __raw_spin_unlock(raw_spinlock_t *lock)
96 asm volatile("movb $1,%0" : "+m" (lock->slock) :: "memory");
101 static inline void __raw_spin_unlock(raw_spinlock_t *lock)
105 asm volatile("xchgb %b0, %1"
106 : "=q" (oldval), "+m" (lock->slock)
107 : "0" (oldval) : "memory");
112 static inline void __raw_spin_unlock_wait(raw_spinlock_t *lock)
114 while (__raw_spin_is_locked(lock))
119 * Read-write spinlocks, allowing multiple readers
120 * but only one writer.
122 * NOTE! it is quite common to have readers in interrupts
123 * but no interrupt writers. For those circumstances we
124 * can "mix" irq-safe locks - any writer needs to get a
125 * irq-safe write-lock, but readers can get non-irqsafe
128 * On x86, we implement read-write locks as a 32-bit counter
129 * with the high bit (sign) being the "contended" bit.
131 * The inline assembly is non-obvious. Think about it.
133 * Changed to use the same technique as rw semaphores. See
134 * semaphore.h for details. -ben
136 * the helpers are in arch/i386/kernel/semaphore.c
140 * read_can_lock - would read_trylock() succeed?
141 * @lock: the rwlock in question.
143 static inline int __raw_read_can_lock(raw_rwlock_t *x)
145 return (int)(x)->lock > 0;
149 * write_can_lock - would write_trylock() succeed?
150 * @lock: the rwlock in question.
152 static inline int __raw_write_can_lock(raw_rwlock_t *x)
154 return (x)->lock == RW_LOCK_BIAS;
157 static inline void __raw_read_lock(raw_rwlock_t *rw)
159 asm volatile(LOCK_PREFIX " subl $1,(%0)\n\t"
161 "call __read_lock_failed\n\t"
163 ::"a" (rw) : "memory");
166 static inline void __raw_write_lock(raw_rwlock_t *rw)
168 asm volatile(LOCK_PREFIX " subl $" RW_LOCK_BIAS_STR ",(%0)\n\t"
170 "call __write_lock_failed\n\t"
172 ::"a" (rw) : "memory");
175 static inline int __raw_read_trylock(raw_rwlock_t *lock)
177 atomic_t *count = (atomic_t *)lock;
179 if (atomic_read(count) >= 0)
185 static inline int __raw_write_trylock(raw_rwlock_t *lock)
187 atomic_t *count = (atomic_t *)lock;
188 if (atomic_sub_and_test(RW_LOCK_BIAS, count))
190 atomic_add(RW_LOCK_BIAS, count);
194 static inline void __raw_read_unlock(raw_rwlock_t *rw)
196 asm volatile(LOCK_PREFIX "incl %0" :"+m" (rw->lock) : : "memory");
199 static inline void __raw_write_unlock(raw_rwlock_t *rw)
201 asm volatile(LOCK_PREFIX "addl $" RW_LOCK_BIAS_STR ", %0"
202 : "+m" (rw->lock) : : "memory");
205 #endif /* __ASM_SPINLOCK_H */