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_PREFIX " ; decb %0\n\t" \
35 * NOTE: there's an irqs-on section here, which normally would have to be
36 * irq-traced, but on CONFIG_TRACE_IRQFLAGS we never use
37 * __raw_spin_lock_string_flags().
39 #define __raw_spin_lock_string_flags \
41 LOCK_PREFIX " ; decb %0\n\t" \
44 "testl $0x200, %1\n\t" \
60 static inline void __raw_spin_lock(raw_spinlock_t
*lock
)
62 asm(__raw_spin_lock_string
: "+m" (lock
->slock
) : : "memory");
66 * It is easier for the lock validator if interrupts are not re-enabled
67 * in the middle of a lock-acquire. This is a performance feature anyway
70 #ifndef CONFIG_PROVE_LOCKING
71 static inline void __raw_spin_lock_flags(raw_spinlock_t
*lock
, unsigned long flags
)
73 asm(__raw_spin_lock_string_flags
: "+m" (lock
->slock
) : "r" (flags
) : "memory");
77 static inline int __raw_spin_trylock(raw_spinlock_t
*lock
)
82 :"=q" (oldval
), "+m" (lock
->slock
)
88 * __raw_spin_unlock based on writing $1 to the low byte.
89 * This method works. Despite all the confusion.
90 * (except on PPro SMP or if we are using OOSTORE, so we use xchgb there)
91 * (PPro errata 66, 92)
94 #if !defined(CONFIG_X86_OOSTORE) && !defined(CONFIG_X86_PPRO_FENCE)
96 #define __raw_spin_unlock_string \
98 :"+m" (lock->slock) : : "memory"
101 static inline void __raw_spin_unlock(raw_spinlock_t
*lock
)
103 __asm__
__volatile__(
104 __raw_spin_unlock_string
110 #define __raw_spin_unlock_string \
112 :"=q" (oldval), "+m" (lock->slock) \
113 :"0" (oldval) : "memory"
115 static inline void __raw_spin_unlock(raw_spinlock_t
*lock
)
119 __asm__
__volatile__(
120 __raw_spin_unlock_string
126 #define __raw_spin_unlock_wait(lock) \
127 do { while (__raw_spin_is_locked(lock)) cpu_relax(); } while (0)
130 * Read-write spinlocks, allowing multiple readers
131 * but only one writer.
133 * NOTE! it is quite common to have readers in interrupts
134 * but no interrupt writers. For those circumstances we
135 * can "mix" irq-safe locks - any writer needs to get a
136 * irq-safe write-lock, but readers can get non-irqsafe
139 * On x86, we implement read-write locks as a 32-bit counter
140 * with the high bit (sign) being the "contended" bit.
142 * The inline assembly is non-obvious. Think about it.
144 * Changed to use the same technique as rw semaphores. See
145 * semaphore.h for details. -ben
147 * the helpers are in arch/i386/kernel/semaphore.c
151 * read_can_lock - would read_trylock() succeed?
152 * @lock: the rwlock in question.
154 #define __raw_read_can_lock(x) ((int)(x)->lock > 0)
157 * write_can_lock - would write_trylock() succeed?
158 * @lock: the rwlock in question.
160 #define __raw_write_can_lock(x) ((x)->lock == RW_LOCK_BIAS)
162 static inline void __raw_read_lock(raw_rwlock_t
*rw
)
164 __build_read_lock(rw
, "__read_lock_failed");
167 static inline void __raw_write_lock(raw_rwlock_t
*rw
)
169 __build_write_lock(rw
, "__write_lock_failed");
172 static inline int __raw_read_trylock(raw_rwlock_t
*lock
)
174 atomic_t
*count
= (atomic_t
*)lock
;
176 if (atomic_read(count
) >= 0)
182 static inline int __raw_write_trylock(raw_rwlock_t
*lock
)
184 atomic_t
*count
= (atomic_t
*)lock
;
185 if (atomic_sub_and_test(RW_LOCK_BIAS
, count
))
187 atomic_add(RW_LOCK_BIAS
, count
);
191 static inline void __raw_read_unlock(raw_rwlock_t
*rw
)
193 asm volatile(LOCK_PREFIX
"incl %0" :"+m" (rw
->lock
) : : "memory");
196 static inline void __raw_write_unlock(raw_rwlock_t
*rw
)
198 asm volatile(LOCK_PREFIX
"addl $" RW_LOCK_BIAS_STR
", %0"
199 : "+m" (rw
->lock
) : : "memory");
202 #endif /* __ASM_SPINLOCK_H */