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>
9 extern int printk(const char * fmt, ...)
10 __attribute__ ((format (printf, 1, 2)));
13 * Your basic SMP spinlocks, allowing only a single CPU anywhere
17 volatile unsigned int lock;
18 #ifdef CONFIG_DEBUG_SPINLOCK
22 unsigned int break_lock;
26 #define SPINLOCK_MAGIC 0xdead4ead
28 #ifdef CONFIG_DEBUG_SPINLOCK
29 #define SPINLOCK_MAGIC_INIT , SPINLOCK_MAGIC
31 #define SPINLOCK_MAGIC_INIT /* */
34 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 1 SPINLOCK_MAGIC_INIT }
36 #define spin_lock_init(x) do { *(x) = SPIN_LOCK_UNLOCKED; } while(0)
39 * Simple spin lock operations. There are two variants, one clears IRQ's
40 * on the local processor, one does not.
42 * We make no fairness assumptions. They have a cost.
45 #define spin_is_locked(x) (*(volatile signed char *)(&(x)->lock) <= 0)
46 #define spin_unlock_wait(x) do { barrier(); } while(spin_is_locked(x))
47 #define _raw_spin_lock_flags(lock, flags) _raw_spin_lock(lock)
49 #define spin_lock_string \
51 "lock ; decb %0\n\t" \
53 LOCK_SECTION_START("") \
62 * This works. Despite all the confusion.
63 * (except on PPro SMP or if we are using OOSTORE)
64 * (PPro errata 66, 92)
67 #if !defined(CONFIG_X86_OOSTORE) && !defined(CONFIG_X86_PPRO_FENCE)
69 #define spin_unlock_string \
71 :"=m" (lock->lock) : : "memory"
74 static inline void _raw_spin_unlock(spinlock_t *lock)
76 #ifdef CONFIG_DEBUG_SPINLOCK
77 BUG_ON(lock->magic != SPINLOCK_MAGIC);
78 assert_spin_locked(lock);
87 #define spin_unlock_string \
89 :"=q" (oldval), "=m" (lock->lock) \
90 :"0" (oldval) : "memory"
92 static inline void _raw_spin_unlock(spinlock_t *lock)
95 #ifdef CONFIG_DEBUG_SPINLOCK
96 BUG_ON(lock->magic != SPINLOCK_MAGIC);
97 assert_spin_locked(lock);
106 static inline int _raw_spin_trylock(spinlock_t *lock)
109 __asm__ __volatile__(
111 :"=q" (oldval), "=m" (lock->lock)
112 :"0" (0) : "memory");
116 static inline void _raw_spin_lock(spinlock_t *lock)
118 #ifdef CONFIG_DEBUG_SPINLOCK
119 if (lock->magic != SPINLOCK_MAGIC) {
120 printk("eip: %p\n", __builtin_return_address(0));
124 __asm__ __volatile__(
126 :"=m" (lock->lock) : : "memory");
131 * Read-write spinlocks, allowing multiple readers
132 * but only one writer.
134 * NOTE! it is quite common to have readers in interrupts
135 * but no interrupt writers. For those circumstances we
136 * can "mix" irq-safe locks - any writer needs to get a
137 * irq-safe write-lock, but readers can get non-irqsafe
141 volatile unsigned int lock;
142 #ifdef CONFIG_DEBUG_SPINLOCK
145 #ifdef CONFIG_PREEMPT
146 unsigned int break_lock;
150 #define RWLOCK_MAGIC 0xdeaf1eed
152 #ifdef CONFIG_DEBUG_SPINLOCK
153 #define RWLOCK_MAGIC_INIT , RWLOCK_MAGIC
155 #define RWLOCK_MAGIC_INIT /* */
158 #define RW_LOCK_UNLOCKED (rwlock_t) { RW_LOCK_BIAS RWLOCK_MAGIC_INIT }
160 #define rwlock_init(x) do { *(x) = RW_LOCK_UNLOCKED; } while(0)
162 #define read_can_lock(x) ((int)(x)->lock > 0)
163 #define write_can_lock(x) ((x)->lock == RW_LOCK_BIAS)
166 * On x86, we implement read-write locks as a 32-bit counter
167 * with the high bit (sign) being the "contended" bit.
169 * The inline assembly is non-obvious. Think about it.
171 * Changed to use the same technique as rw semaphores. See
172 * semaphore.h for details. -ben
174 /* the spinlock helpers are in arch/i386/kernel/semaphore.c */
176 static inline void _raw_read_lock(rwlock_t *rw)
178 #ifdef CONFIG_DEBUG_SPINLOCK
179 BUG_ON(rw->magic != RWLOCK_MAGIC);
181 __build_read_lock(rw, "__read_lock_failed");
184 static inline void _raw_write_lock(rwlock_t *rw)
186 #ifdef CONFIG_DEBUG_SPINLOCK
187 BUG_ON(rw->magic != RWLOCK_MAGIC);
189 __build_write_lock(rw, "__write_lock_failed");
192 #define _raw_read_unlock(rw) asm volatile("lock ; incl %0" :"=m" ((rw)->lock) : : "memory")
193 #define _raw_write_unlock(rw) asm volatile("lock ; addl $" RW_LOCK_BIAS_STR ",%0":"=m" ((rw)->lock) : : "memory")
195 static inline int _raw_read_trylock(rwlock_t *lock)
197 atomic_t *count = (atomic_t *)lock;
199 if (atomic_read(count) >= 0)
205 static inline int _raw_write_trylock(rwlock_t *lock)
207 atomic_t *count = (atomic_t *)lock;
208 if (atomic_sub_and_test(RW_LOCK_BIAS, count))
210 atomic_add(RW_LOCK_BIAS, count);
214 #endif /* __ASM_SPINLOCK_H */