1 #ifndef __ASM_SPINLOCK_H
2 #define __ASM_SPINLOCK_H
4 #include <asm/barrier.h>
6 #include <asm/processor.h>
7 #include <asm/spinlock_types.h>
9 static inline int arch_spin_is_locked(arch_spinlock_t *x)
11 volatile unsigned int *a = __ldcw_align(x);
15 #define arch_spin_lock(lock) arch_spin_lock_flags(lock, 0)
17 static inline void arch_spin_unlock_wait(arch_spinlock_t *x)
19 volatile unsigned int *a = __ldcw_align(x);
21 smp_cond_load_acquire(a, VAL);
24 static inline void arch_spin_lock_flags(arch_spinlock_t *x,
27 volatile unsigned int *a;
30 while (__ldcw(a) == 0)
32 if (flags & PSW_SM_I) {
40 static inline void arch_spin_unlock(arch_spinlock_t *x)
42 volatile unsigned int *a;
49 static inline int arch_spin_trylock(arch_spinlock_t *x)
51 volatile unsigned int *a;
61 * Read-write spinlocks, allowing multiple readers but only one writer.
62 * Linux rwlocks are unfair to writers; they can be starved for an indefinite
63 * time by readers. With care, they can also be taken in interrupt context.
65 * In the PA-RISC implementation, we have a spinlock and a counter.
66 * Readers use the lock to serialise their access to the counter (which
67 * records how many readers currently hold the lock).
68 * Writers hold the spinlock, preventing any readers or other writers from
69 * grabbing the rwlock.
72 /* Note that we have to ensure interrupts are disabled in case we're
73 * interrupted by some other code that wants to grab the same read lock */
74 static __inline__ void arch_read_lock(arch_rwlock_t *rw)
77 local_irq_save(flags);
78 arch_spin_lock_flags(&rw->lock, flags);
80 arch_spin_unlock(&rw->lock);
81 local_irq_restore(flags);
84 /* Note that we have to ensure interrupts are disabled in case we're
85 * interrupted by some other code that wants to grab the same read lock */
86 static __inline__ void arch_read_unlock(arch_rwlock_t *rw)
89 local_irq_save(flags);
90 arch_spin_lock_flags(&rw->lock, flags);
92 arch_spin_unlock(&rw->lock);
93 local_irq_restore(flags);
96 /* Note that we have to ensure interrupts are disabled in case we're
97 * interrupted by some other code that wants to grab the same read lock */
98 static __inline__ int arch_read_trylock(arch_rwlock_t *rw)
102 local_irq_save(flags);
103 if (arch_spin_trylock(&rw->lock)) {
105 arch_spin_unlock(&rw->lock);
106 local_irq_restore(flags);
110 local_irq_restore(flags);
111 /* If write-locked, we fail to acquire the lock */
115 /* Wait until we have a realistic chance at the lock */
116 while (arch_spin_is_locked(&rw->lock) && rw->counter >= 0)
122 /* Note that we have to ensure interrupts are disabled in case we're
123 * interrupted by some other code that wants to read_trylock() this lock */
124 static __inline__ void arch_write_lock(arch_rwlock_t *rw)
128 local_irq_save(flags);
129 arch_spin_lock_flags(&rw->lock, flags);
131 if (rw->counter != 0) {
132 arch_spin_unlock(&rw->lock);
133 local_irq_restore(flags);
135 while (rw->counter != 0)
141 rw->counter = -1; /* mark as write-locked */
143 local_irq_restore(flags);
146 static __inline__ void arch_write_unlock(arch_rwlock_t *rw)
149 arch_spin_unlock(&rw->lock);
152 /* Note that we have to ensure interrupts are disabled in case we're
153 * interrupted by some other code that wants to read_trylock() this lock */
154 static __inline__ int arch_write_trylock(arch_rwlock_t *rw)
159 local_irq_save(flags);
160 if (arch_spin_trylock(&rw->lock)) {
161 if (rw->counter == 0) {
165 /* Read-locked. Oh well. */
166 arch_spin_unlock(&rw->lock);
169 local_irq_restore(flags);
175 * read_can_lock - would read_trylock() succeed?
176 * @lock: the rwlock in question.
178 static __inline__ int arch_read_can_lock(arch_rwlock_t *rw)
180 return rw->counter >= 0;
184 * write_can_lock - would write_trylock() succeed?
185 * @lock: the rwlock in question.
187 static __inline__ int arch_write_can_lock(arch_rwlock_t *rw)
192 #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
193 #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
195 #endif /* __ASM_SPINLOCK_H */