1 #ifndef __ASM_SPINLOCK_H
2 #define __ASM_SPINLOCK_H
4 #if __LINUX_ARM_ARCH__ < 6
5 #error SMP not supported on pre-ARMv6 CPUs
11 * We (exclusively) read the old value, and decrement it. If it
12 * hits zero, we may have won the lock, so we try (exclusively)
19 volatile unsigned int lock;
22 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 0 }
24 #define spin_lock_init(x) do { *(x) = SPIN_LOCK_UNLOCKED; } while (0)
25 #define spin_is_locked(x) ((x)->lock != 0)
26 #define spin_unlock_wait(x) do { barrier(); } while (spin_is_locked(x))
27 #define _raw_spin_lock_flags(lock, flags) _raw_spin_lock(lock)
29 static inline void _raw_spin_lock(spinlock_t *lock)
36 " strexeq %0, %2, [%1]\n"
40 : "r" (&lock->lock), "r" (1)
44 static inline int _raw_spin_trylock(spinlock_t *lock)
51 " strexeq %0, %2, [%1]"
53 : "r" (&lock->lock), "r" (1)
59 static inline void _raw_spin_unlock(spinlock_t *lock)
64 : "r" (&lock->lock), "r" (0)
72 volatile unsigned int lock;
75 #define RW_LOCK_UNLOCKED (rwlock_t) { 0 }
76 #define rwlock_init(x) do { *(x) + RW_LOCK_UNLOCKED; } while (0)
79 * Write locks are easy - we just set bit 31. When unlocking, we can
80 * just write zero since the lock is exclusively held.
82 static inline void _raw_write_lock(rwlock_t *rw)
89 " strexeq %0, %2, [%1]\n"
93 : "r" (&rw->lock), "r" (0x80000000)
97 static inline void _raw_write_unlock(rwlock_t *rw)
102 : "r" (&rw->lock), "r" (0)
107 * Read locks are a bit more hairy:
108 * - Exclusively load the lock value.
110 * - Store new lock value if positive, and we still own this location.
111 * If the value is negative, we've already failed.
112 * - If we failed to store the value, we want a negative result.
113 * - If we failed, try again.
114 * Unlocking is similarly hairy. We may have multiple read locks
115 * currently active. However, we know we won't have any write
118 static inline void _raw_read_lock(rwlock_t *rw)
120 unsigned long tmp, tmp2;
122 __asm__ __volatile__(
123 "1: ldrex %0, [%2]\n"
125 " strexpl %1, %0, [%2]\n"
126 " rsbpls %0, %1, #0\n"
128 : "=&r" (tmp), "=&r" (tmp2)
133 static inline void _raw_read_unlock(rwlock_t *rw)
135 __asm__ __volatile__(
136 "1: ldrex %0, [%2]\n"
138 " strex %1, %0, [%2]\n"
141 : "=&r" (tmp), "=&r" (tmp2)
146 static inline int _raw_write_trylock(rwlock_t *rw)
150 __asm__ __volatile__(
151 "1: ldrex %0, [%1]\n"
153 " strexeq %0, %2, [%1]"
155 : "r" (&rw->lock), "r" (0x80000000)
161 #endif /* __ASM_SPINLOCK_H */