1 #ifndef __ASM_SPINLOCK_H
2 #define __ASM_SPINLOCK_H
4 #include <asm/atomic.h>
5 #include <asm/rwlock.h>
7 #include <linux/compiler.h>
10 * Your basic SMP spinlocks, allowing only a single CPU anywhere
12 * Simple spin lock operations. There are two variants, one clears IRQ's
13 * on the local processor, one does not.
15 * We make no fairness assumptions. They have a cost.
17 * (the type definitions are in asm/spinlock_types.h)
20 #define __raw_spin_is_locked(x) \
21 (*(volatile signed char *)(&(x)->slock) <= 0)
23 #define __raw_spin_lock_string \
25 "lock ; decb %0\n\t" \
34 #define __raw_spin_lock_string_flags \
36 "lock ; decb %0\n\t" \
39 "testl $0x200, %1\n\t" \
55 #define __raw_spin_lock_string_up \
58 static inline void __raw_spin_lock(raw_spinlock_t *lock)
61 __raw_spin_lock_string,
62 __raw_spin_lock_string_up,
63 "=m" (lock->slock) : : "memory");
66 static inline void __raw_spin_lock_flags(raw_spinlock_t *lock, unsigned long flags)
69 __raw_spin_lock_string_flags,
70 __raw_spin_lock_string_up,
71 "=m" (lock->slock) : "r" (flags) : "memory");
74 static inline int __raw_spin_trylock(raw_spinlock_t *lock)
79 :"=q" (oldval), "=m" (lock->slock)
85 * __raw_spin_unlock based on writing $1 to the low byte.
86 * This method works. Despite all the confusion.
87 * (except on PPro SMP or if we are using OOSTORE, so we use xchgb there)
88 * (PPro errata 66, 92)
91 #if !defined(CONFIG_X86_OOSTORE) && !defined(CONFIG_X86_PPRO_FENCE)
93 #define __raw_spin_unlock_string \
95 :"=m" (lock->slock) : : "memory"
98 static inline void __raw_spin_unlock(raw_spinlock_t *lock)
100 __asm__ __volatile__(
101 __raw_spin_unlock_string
107 #define __raw_spin_unlock_string \
109 :"=q" (oldval), "=m" (lock->slock) \
110 :"0" (oldval) : "memory"
112 static inline void __raw_spin_unlock(raw_spinlock_t *lock)
116 __asm__ __volatile__(
117 __raw_spin_unlock_string
123 #define __raw_spin_unlock_wait(lock) \
124 do { while (__raw_spin_is_locked(lock)) cpu_relax(); } while (0)
127 * Read-write spinlocks, allowing multiple readers
128 * but only one writer.
130 * NOTE! it is quite common to have readers in interrupts
131 * but no interrupt writers. For those circumstances we
132 * can "mix" irq-safe locks - any writer needs to get a
133 * irq-safe write-lock, but readers can get non-irqsafe
136 * On x86, we implement read-write locks as a 32-bit counter
137 * with the high bit (sign) being the "contended" bit.
139 * The inline assembly is non-obvious. Think about it.
141 * Changed to use the same technique as rw semaphores. See
142 * semaphore.h for details. -ben
144 * the helpers are in arch/i386/kernel/semaphore.c
148 * read_can_lock - would read_trylock() succeed?
149 * @lock: the rwlock in question.
151 #define __raw_read_can_lock(x) ((int)(x)->lock > 0)
154 * write_can_lock - would write_trylock() succeed?
155 * @lock: the rwlock in question.
157 #define __raw_write_can_lock(x) ((x)->lock == RW_LOCK_BIAS)
159 static inline void __raw_read_lock(raw_rwlock_t *rw)
161 __build_read_lock(rw, "__read_lock_failed");
164 static inline void __raw_write_lock(raw_rwlock_t *rw)
166 __build_write_lock(rw, "__write_lock_failed");
169 static inline int __raw_read_trylock(raw_rwlock_t *lock)
171 atomic_t *count = (atomic_t *)lock;
173 if (atomic_read(count) >= 0)
179 static inline int __raw_write_trylock(raw_rwlock_t *lock)
181 atomic_t *count = (atomic_t *)lock;
182 if (atomic_sub_and_test(RW_LOCK_BIAS, count))
184 atomic_add(RW_LOCK_BIAS, count);
188 static inline void __raw_read_unlock(raw_rwlock_t *rw)
190 asm volatile(LOCK_PREFIX "incl %0" :"=m" (rw->lock) : : "memory");
193 static inline void __raw_write_unlock(raw_rwlock_t *rw)
195 asm volatile(LOCK_PREFIX "addl $" RW_LOCK_BIAS_STR ", %0"
196 : "=m" (rw->lock) : : "memory");
199 #endif /* __ASM_SPINLOCK_H */