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 static inline int __raw_spin_is_locked(raw_spinlock_t *lock)
21 return *(volatile signed int *)(&(lock)->slock) <= 0;
24 static inline void __raw_spin_lock(raw_spinlock_t *lock)
28 LOCK_PREFIX " ; decl %0\n\t"
35 "2:\t" : "=m" (lock->slock) : : "memory");
38 #define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock)
40 static inline int __raw_spin_trylock(raw_spinlock_t *lock)
46 :"=q" (oldval), "=m" (lock->slock)
52 static inline void __raw_spin_unlock(raw_spinlock_t *lock)
54 asm volatile("movl $1,%0" :"=m" (lock->slock) :: "memory");
57 static inline void __raw_spin_unlock_wait(raw_spinlock_t *lock)
59 while (__raw_spin_is_locked(lock))
64 * Read-write spinlocks, allowing multiple readers
65 * but only one writer.
67 * NOTE! it is quite common to have readers in interrupts
68 * but no interrupt writers. For those circumstances we
69 * can "mix" irq-safe locks - any writer needs to get a
70 * irq-safe write-lock, but readers can get non-irqsafe
73 * On x86, we implement read-write locks as a 32-bit counter
74 * with the high bit (sign) being the "contended" bit.
77 static inline int __raw_read_can_lock(raw_rwlock_t *lock)
79 return (int)(lock)->lock > 0;
82 static inline int __raw_write_can_lock(raw_rwlock_t *lock)
84 return (lock)->lock == RW_LOCK_BIAS;
87 static inline void __raw_read_lock(raw_rwlock_t *rw)
89 asm volatile(LOCK_PREFIX "subl $1,(%0)\n\t"
91 "call __read_lock_failed\n"
93 ::"D" (rw), "i" (RW_LOCK_BIAS) : "memory");
96 static inline void __raw_write_lock(raw_rwlock_t *rw)
98 asm volatile(LOCK_PREFIX "subl %1,(%0)\n\t"
100 "\tcall __write_lock_failed\n\t"
102 ::"D" (rw), "i" (RW_LOCK_BIAS) : "memory");
105 static inline int __raw_read_trylock(raw_rwlock_t *lock)
107 atomic_t *count = (atomic_t *)lock;
109 if (atomic_read(count) >= 0)
115 static inline int __raw_write_trylock(raw_rwlock_t *lock)
117 atomic_t *count = (atomic_t *)lock;
118 if (atomic_sub_and_test(RW_LOCK_BIAS, count))
120 atomic_add(RW_LOCK_BIAS, count);
124 static inline void __raw_read_unlock(raw_rwlock_t *rw)
126 asm volatile(LOCK_PREFIX " ; incl %0" :"=m" (rw->lock) : : "memory");
129 static inline void __raw_write_unlock(raw_rwlock_t *rw)
131 asm volatile(LOCK_PREFIX " ; addl $" RW_LOCK_BIAS_STR ",%0"
132 : "=m" (rw->lock) : : "memory");
135 #endif /* __ASM_SPINLOCK_H */