1 #ifndef __ASM_SPINLOCK_H
2 #define __ASM_SPINLOCK_H
4 #include <asm/system.h>
5 #include <asm/processor.h>
6 #include <asm/spinlock_types.h>
8 static inline int arch_spin_is_locked(arch_spinlock_t
*x
)
10 volatile unsigned int *a
= __ldcw_align(x
);
14 #define arch_spin_lock(lock) arch_spin_lock_flags(lock, 0)
15 #define arch_spin_unlock_wait(x) \
16 do { cpu_relax(); } while (arch_spin_is_locked(x))
18 static inline void arch_spin_lock_flags(arch_spinlock_t
*x
,
21 volatile unsigned int *a
;
25 while (__ldcw(a
) == 0)
27 if (flags
& PSW_SM_I
) {
36 static inline void arch_spin_unlock(arch_spinlock_t
*x
)
38 volatile unsigned int *a
;
45 static inline int arch_spin_trylock(arch_spinlock_t
*x
)
47 volatile unsigned int *a
;
59 * Read-write spinlocks, allowing multiple readers but only one writer.
60 * Linux rwlocks are unfair to writers; they can be starved for an indefinite
61 * time by readers. With care, they can also be taken in interrupt context.
63 * In the PA-RISC implementation, we have a spinlock and a counter.
64 * Readers use the lock to serialise their access to the counter (which
65 * records how many readers currently hold the lock).
66 * Writers hold the spinlock, preventing any readers or other writers from
67 * grabbing the rwlock.
70 /* Note that we have to ensure interrupts are disabled in case we're
71 * interrupted by some other code that wants to grab the same read lock */
72 static __inline__
void arch_read_lock(arch_rwlock_t
*rw
)
75 local_irq_save(flags
);
76 arch_spin_lock_flags(&rw
->lock
, flags
);
78 arch_spin_unlock(&rw
->lock
);
79 local_irq_restore(flags
);
82 /* Note that we have to ensure interrupts are disabled in case we're
83 * interrupted by some other code that wants to grab the same read lock */
84 static __inline__
void arch_read_unlock(arch_rwlock_t
*rw
)
87 local_irq_save(flags
);
88 arch_spin_lock_flags(&rw
->lock
, flags
);
90 arch_spin_unlock(&rw
->lock
);
91 local_irq_restore(flags
);
94 /* Note that we have to ensure interrupts are disabled in case we're
95 * interrupted by some other code that wants to grab the same read lock */
96 static __inline__
int arch_read_trylock(arch_rwlock_t
*rw
)
100 local_irq_save(flags
);
101 if (arch_spin_trylock(&rw
->lock
)) {
103 arch_spin_unlock(&rw
->lock
);
104 local_irq_restore(flags
);
108 local_irq_restore(flags
);
109 /* If write-locked, we fail to acquire the lock */
113 /* Wait until we have a realistic chance at the lock */
114 while (arch_spin_is_locked(&rw
->lock
) && rw
->counter
>= 0)
120 /* Note that we have to ensure interrupts are disabled in case we're
121 * interrupted by some other code that wants to read_trylock() this lock */
122 static __inline__
void arch_write_lock(arch_rwlock_t
*rw
)
126 local_irq_save(flags
);
127 arch_spin_lock_flags(&rw
->lock
, flags
);
129 if (rw
->counter
!= 0) {
130 arch_spin_unlock(&rw
->lock
);
131 local_irq_restore(flags
);
133 while (rw
->counter
!= 0)
139 rw
->counter
= -1; /* mark as write-locked */
141 local_irq_restore(flags
);
144 static __inline__
void arch_write_unlock(arch_rwlock_t
*rw
)
147 arch_spin_unlock(&rw
->lock
);
150 /* Note that we have to ensure interrupts are disabled in case we're
151 * interrupted by some other code that wants to read_trylock() this lock */
152 static __inline__
int arch_write_trylock(arch_rwlock_t
*rw
)
157 local_irq_save(flags
);
158 if (arch_spin_trylock(&rw
->lock
)) {
159 if (rw
->counter
== 0) {
163 /* Read-locked. Oh well. */
164 arch_spin_unlock(&rw
->lock
);
167 local_irq_restore(flags
);
173 * read_can_lock - would read_trylock() succeed?
174 * @lock: the rwlock in question.
176 static __inline__
int arch_read_can_lock(arch_rwlock_t
*rw
)
178 return rw
->counter
>= 0;
182 * write_can_lock - would write_trylock() succeed?
183 * @lock: the rwlock in question.
185 static __inline__
int arch_write_can_lock(arch_rwlock_t
*rw
)
190 #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
191 #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
193 #define arch_spin_relax(lock) cpu_relax()
194 #define arch_read_relax(lock) cpu_relax()
195 #define arch_write_relax(lock) cpu_relax()
197 #endif /* __ASM_SPINLOCK_H */