1 #ifndef __ASM_SPINLOCK_H
2 #define __ASM_SPINLOCK_H
4 #include <asm/atomic.h>
5 #include <asm/rwlock.h>
9 * Your basic SMP spinlocks, allowing only a single CPU anywhere
11 * Simple spin lock operations. There are two variants, one clears IRQ's
12 * on the local processor, one does not.
14 * We make no fairness assumptions. They have a cost.
16 * (the type definitions are in asm/spinlock_types.h)
19 #define __raw_spin_is_locked(x) \
20 (*(volatile signed int *)(&(x)->slock) <= 0)
22 #define __raw_spin_lock_string \
24 "lock ; decl %0\n\t" \
26 LOCK_SECTION_START("") \
34 #define __raw_spin_lock_string_up \
37 #define __raw_spin_unlock_string \
39 :"=m" (lock->slock) : : "memory"
41 static inline void __raw_spin_lock(raw_spinlock_t *lock)
44 __raw_spin_lock_string,
45 __raw_spin_lock_string_up,
46 "=m" (lock->slock) : : "memory");
49 #define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock)
51 static inline int __raw_spin_trylock(raw_spinlock_t *lock)
57 :"=q" (oldval), "=m" (lock->slock)
63 static inline void __raw_spin_unlock(raw_spinlock_t *lock)
66 __raw_spin_unlock_string
70 #define __raw_spin_unlock_wait(lock) \
71 do { while (__raw_spin_is_locked(lock)) cpu_relax(); } while (0)
74 * Read-write spinlocks, allowing multiple readers
75 * but only one writer.
77 * NOTE! it is quite common to have readers in interrupts
78 * but no interrupt writers. For those circumstances we
79 * can "mix" irq-safe locks - any writer needs to get a
80 * irq-safe write-lock, but readers can get non-irqsafe
83 * On x86, we implement read-write locks as a 32-bit counter
84 * with the high bit (sign) being the "contended" bit.
86 * The inline assembly is non-obvious. Think about it.
88 * Changed to use the same technique as rw semaphores. See
89 * semaphore.h for details. -ben
91 * the helpers are in arch/i386/kernel/semaphore.c
94 #define __raw_read_can_lock(x) ((int)(x)->lock > 0)
95 #define __raw_write_can_lock(x) ((x)->lock == RW_LOCK_BIAS)
97 static inline void __raw_read_lock(raw_rwlock_t *rw)
99 __build_read_lock(rw, "__read_lock_failed");
102 static inline void __raw_write_lock(raw_rwlock_t *rw)
104 __build_write_lock(rw, "__write_lock_failed");
107 static inline int __raw_read_trylock(raw_rwlock_t *lock)
109 atomic_t *count = (atomic_t *)lock;
111 if (atomic_read(count) >= 0)
117 static inline int __raw_write_trylock(raw_rwlock_t *lock)
119 atomic_t *count = (atomic_t *)lock;
120 if (atomic_sub_and_test(RW_LOCK_BIAS, count))
122 atomic_add(RW_LOCK_BIAS, count);
126 static inline void __raw_read_unlock(raw_rwlock_t *rw)
128 asm volatile("lock ; incl %0" :"=m" (rw->lock) : : "memory");
131 static inline void __raw_write_unlock(raw_rwlock_t *rw)
133 asm volatile("lock ; addl $" RW_LOCK_BIAS_STR ",%0"
134 : "=m" (rw->lock) : : "memory");
137 #endif /* __ASM_SPINLOCK_H */