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>
10 #ifdef CONFIG_PARAVIRT
11 #include <asm/paravirt.h>
13 #define CLI_STRING "cli"
14 #define STI_STRING "sti"
15 #endif /* CONFIG_PARAVIRT */
18 * Your basic SMP spinlocks, allowing only a single CPU anywhere
20 * Simple spin lock operations. There are two variants, one clears IRQ's
21 * on the local processor, one does not.
23 * We make no fairness assumptions. They have a cost.
25 * (the type definitions are in asm/spinlock_types.h)
28 static inline int __raw_spin_is_locked(raw_spinlock_t *x)
30 return *(volatile signed char *)(&(x)->slock) <= 0;
33 static inline void __raw_spin_lock(raw_spinlock_t *lock)
36 LOCK_PREFIX " ; decb %0\n\t"
44 : "+m" (lock->slock) : : "memory");
48 * It is easier for the lock validator if interrupts are not re-enabled
49 * in the middle of a lock-acquire. This is a performance feature anyway
52 * NOTE: there's an irqs-on section here, which normally would have to be
53 * irq-traced, but on CONFIG_TRACE_IRQFLAGS we never use this variant.
55 #ifndef CONFIG_PROVE_LOCKING
56 static inline void __raw_spin_lock_flags(raw_spinlock_t *lock, unsigned long flags)
60 LOCK_PREFIX " ; decb %0\n\t"
63 "testl $0x200, %1\n\t"
78 : "+m" (lock->slock) : "r" (flags) : "memory");
82 static inline int __raw_spin_trylock(raw_spinlock_t *lock)
87 :"=q" (oldval), "+m" (lock->slock)
93 * __raw_spin_unlock based on writing $1 to the low byte.
94 * This method works. Despite all the confusion.
95 * (except on PPro SMP or if we are using OOSTORE, so we use xchgb there)
96 * (PPro errata 66, 92)
99 #if !defined(CONFIG_X86_OOSTORE) && !defined(CONFIG_X86_PPRO_FENCE)
101 static inline void __raw_spin_unlock(raw_spinlock_t *lock)
103 asm volatile("movb $1,%0" : "+m" (lock->slock) :: "memory");
108 static inline void __raw_spin_unlock(raw_spinlock_t *lock)
112 asm volatile("xchgb %b0, %1"
113 : "=q" (oldval), "+m" (lock->slock)
114 : "0" (oldval) : "memory");
119 static inline void __raw_spin_unlock_wait(raw_spinlock_t *lock)
121 while (__raw_spin_is_locked(lock))
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 * On x86, we implement read-write locks as a 32-bit counter
136 * with the high bit (sign) being the "contended" bit.
138 * The inline assembly is non-obvious. Think about it.
140 * Changed to use the same technique as rw semaphores. See
141 * semaphore.h for details. -ben
143 * the helpers are in arch/i386/kernel/semaphore.c
147 * read_can_lock - would read_trylock() succeed?
148 * @lock: the rwlock in question.
150 static inline int __raw_read_can_lock(raw_rwlock_t *x)
152 return (int)(x)->lock > 0;
156 * write_can_lock - would write_trylock() succeed?
157 * @lock: the rwlock in question.
159 static inline int __raw_write_can_lock(raw_rwlock_t *x)
161 return (x)->lock == RW_LOCK_BIAS;
164 static inline void __raw_read_lock(raw_rwlock_t *rw)
166 asm volatile(LOCK_PREFIX " subl $1,(%0)\n\t"
168 "call __read_lock_failed\n\t"
170 ::"a" (rw) : "memory");
173 static inline void __raw_write_lock(raw_rwlock_t *rw)
175 asm volatile(LOCK_PREFIX " subl $" RW_LOCK_BIAS_STR ",(%0)\n\t"
177 "call __write_lock_failed\n\t"
179 ::"a" (rw) : "memory");
182 static inline int __raw_read_trylock(raw_rwlock_t *lock)
184 atomic_t *count = (atomic_t *)lock;
186 if (atomic_read(count) >= 0)
192 static inline int __raw_write_trylock(raw_rwlock_t *lock)
194 atomic_t *count = (atomic_t *)lock;
195 if (atomic_sub_and_test(RW_LOCK_BIAS, count))
197 atomic_add(RW_LOCK_BIAS, count);
201 static inline void __raw_read_unlock(raw_rwlock_t *rw)
203 asm volatile(LOCK_PREFIX "incl %0" :"+m" (rw->lock) : : "memory");
206 static inline void __raw_write_unlock(raw_rwlock_t *rw)
208 asm volatile(LOCK_PREFIX "addl $" RW_LOCK_BIAS_STR ", %0"
209 : "+m" (rw->lock) : : "memory");
212 #define _raw_spin_relax(lock) cpu_relax()
213 #define _raw_read_relax(lock) cpu_relax()
214 #define _raw_write_relax(lock) cpu_relax()
216 #endif /* __ASM_SPINLOCK_H */