1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __ASM_SPINLOCK_H
3 #define __ASM_SPINLOCK_H
5 #include <asm/barrier.h>
7 #include <asm/processor.h>
8 #include <asm/spinlock_types.h>
10 static inline int arch_spin_is_locked(arch_spinlock_t *x)
12 volatile unsigned int *a = __ldcw_align(x);
16 #define arch_spin_lock(lock) arch_spin_lock_flags(lock, 0)
18 static inline void arch_spin_lock_flags(arch_spinlock_t *x,
21 volatile unsigned int *a;
24 while (__ldcw(a) == 0)
26 if (flags & PSW_SM_I) {
34 static inline void arch_spin_unlock(arch_spinlock_t *x)
36 volatile unsigned int *a;
43 static inline int arch_spin_trylock(arch_spinlock_t *x)
45 volatile unsigned int *a;
55 * Read-write spinlocks, allowing multiple readers but only one writer.
56 * Linux rwlocks are unfair to writers; they can be starved for an indefinite
57 * time by readers. With care, they can also be taken in interrupt context.
59 * In the PA-RISC implementation, we have a spinlock and a counter.
60 * Readers use the lock to serialise their access to the counter (which
61 * records how many readers currently hold the lock).
62 * Writers hold the spinlock, preventing any readers or other writers from
63 * grabbing the rwlock.
66 /* Note that we have to ensure interrupts are disabled in case we're
67 * interrupted by some other code that wants to grab the same read lock */
68 static __inline__ void arch_read_lock(arch_rwlock_t *rw)
71 local_irq_save(flags);
72 arch_spin_lock_flags(&rw->lock, flags);
74 arch_spin_unlock(&rw->lock);
75 local_irq_restore(flags);
78 /* Note that we have to ensure interrupts are disabled in case we're
79 * interrupted by some other code that wants to grab the same read lock */
80 static __inline__ void arch_read_unlock(arch_rwlock_t *rw)
83 local_irq_save(flags);
84 arch_spin_lock_flags(&rw->lock, flags);
86 arch_spin_unlock(&rw->lock);
87 local_irq_restore(flags);
90 /* Note that we have to ensure interrupts are disabled in case we're
91 * interrupted by some other code that wants to grab the same read lock */
92 static __inline__ int arch_read_trylock(arch_rwlock_t *rw)
96 local_irq_save(flags);
97 if (arch_spin_trylock(&rw->lock)) {
99 arch_spin_unlock(&rw->lock);
100 local_irq_restore(flags);
104 local_irq_restore(flags);
105 /* If write-locked, we fail to acquire the lock */
109 /* Wait until we have a realistic chance at the lock */
110 while (arch_spin_is_locked(&rw->lock) && rw->counter >= 0)
116 /* Note that we have to ensure interrupts are disabled in case we're
117 * interrupted by some other code that wants to read_trylock() this lock */
118 static __inline__ void arch_write_lock(arch_rwlock_t *rw)
122 local_irq_save(flags);
123 arch_spin_lock_flags(&rw->lock, flags);
125 if (rw->counter != 0) {
126 arch_spin_unlock(&rw->lock);
127 local_irq_restore(flags);
129 while (rw->counter != 0)
135 rw->counter = -1; /* mark as write-locked */
137 local_irq_restore(flags);
140 static __inline__ void arch_write_unlock(arch_rwlock_t *rw)
143 arch_spin_unlock(&rw->lock);
146 /* Note that we have to ensure interrupts are disabled in case we're
147 * interrupted by some other code that wants to read_trylock() this lock */
148 static __inline__ int arch_write_trylock(arch_rwlock_t *rw)
153 local_irq_save(flags);
154 if (arch_spin_trylock(&rw->lock)) {
155 if (rw->counter == 0) {
159 /* Read-locked. Oh well. */
160 arch_spin_unlock(&rw->lock);
163 local_irq_restore(flags);
169 * read_can_lock - would read_trylock() succeed?
170 * @lock: the rwlock in question.
172 static __inline__ int arch_read_can_lock(arch_rwlock_t *rw)
174 return rw->counter >= 0;
178 * write_can_lock - would write_trylock() succeed?
179 * @lock: the rwlock in question.
181 static __inline__ int arch_write_can_lock(arch_rwlock_t *rw)
186 #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
187 #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
189 #endif /* __ASM_SPINLOCK_H */