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>
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 #define __raw_spin_is_locked(x) \
22 (*(volatile signed char *)(&(x)->slock) <= 0)
24 #define __raw_spin_lock_string \
26 "lock ; decb %0\n\t" \
35 #define __raw_spin_lock_string_flags \
37 "lock ; decb %0\n\t" \
40 "testl $0x200, %1\n\t" \
51 #define __raw_spin_lock_string_up \
54 static inline void __raw_spin_lock(raw_spinlock_t *lock)
57 __raw_spin_lock_string,
58 __raw_spin_lock_string_up,
59 "=m" (lock->slock) : : "memory");
62 static inline void __raw_spin_lock_flags(raw_spinlock_t *lock, unsigned long flags)
65 __raw_spin_lock_string_flags,
66 __raw_spin_lock_string_up,
67 "=m" (lock->slock) : "r" (flags) : "memory");
70 static inline int __raw_spin_trylock(raw_spinlock_t *lock)
75 :"=q" (oldval), "=m" (lock->slock)
81 * __raw_spin_unlock based on writing $1 to the low byte.
82 * This method works. Despite all the confusion.
83 * (except on PPro SMP or if we are using OOSTORE, so we use xchgb there)
84 * (PPro errata 66, 92)
87 #if !defined(CONFIG_X86_OOSTORE) && !defined(CONFIG_X86_PPRO_FENCE)
89 #define __raw_spin_unlock_string \
91 :"=m" (lock->slock) : : "memory"
94 static inline void __raw_spin_unlock(raw_spinlock_t *lock)
97 __raw_spin_unlock_string
103 #define __raw_spin_unlock_string \
105 :"=q" (oldval), "=m" (lock->slock) \
106 :"0" (oldval) : "memory"
108 static inline void __raw_spin_unlock(raw_spinlock_t *lock)
112 __asm__ __volatile__(
113 __raw_spin_unlock_string
119 #define __raw_spin_unlock_wait(lock) \
120 do { while (__raw_spin_is_locked(lock)) cpu_relax(); } while (0)
123 * Read-write spinlocks, allowing multiple readers
124 * but only one writer.
126 * NOTE! it is quite common to have readers in interrupts
127 * but no interrupt writers. For those circumstances we
128 * can "mix" irq-safe locks - any writer needs to get a
129 * irq-safe write-lock, but readers can get non-irqsafe
132 * On x86, we implement read-write locks as a 32-bit counter
133 * with the high bit (sign) being the "contended" bit.
135 * The inline assembly is non-obvious. Think about it.
137 * Changed to use the same technique as rw semaphores. See
138 * semaphore.h for details. -ben
140 * the helpers are in arch/i386/kernel/semaphore.c
144 * read_can_lock - would read_trylock() succeed?
145 * @lock: the rwlock in question.
147 #define __raw_read_can_lock(x) ((int)(x)->lock > 0)
150 * write_can_lock - would write_trylock() succeed?
151 * @lock: the rwlock in question.
153 #define __raw_write_can_lock(x) ((x)->lock == RW_LOCK_BIAS)
155 static inline void __raw_read_lock(raw_rwlock_t *rw)
157 __build_read_lock(rw, "__read_lock_failed");
160 static inline void __raw_write_lock(raw_rwlock_t *rw)
162 __build_write_lock(rw, "__write_lock_failed");
165 static inline int __raw_read_trylock(raw_rwlock_t *lock)
167 atomic_t *count = (atomic_t *)lock;
169 if (atomic_read(count) >= 0)
175 static inline int __raw_write_trylock(raw_rwlock_t *lock)
177 atomic_t *count = (atomic_t *)lock;
178 if (atomic_sub_and_test(RW_LOCK_BIAS, count))
180 atomic_add(RW_LOCK_BIAS, count);
184 static inline void __raw_read_unlock(raw_rwlock_t *rw)
186 asm volatile(LOCK_PREFIX "incl %0" :"=m" (rw->lock) : : "memory");
189 static inline void __raw_write_unlock(raw_rwlock_t *rw)
191 asm volatile(LOCK_PREFIX "addl $" RW_LOCK_BIAS_STR ", %0"
192 : "=m" (rw->lock) : : "memory");
195 #endif /* __ASM_SPINLOCK_H */