[PATCH] I2O: Bugfixes to get I2O working again
[linux/fpc-iii.git] / include / asm-parisc / spinlock.h
bloba93960e232cffabb43de39d78a97dc9d7542f154
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 __raw_spin_is_locked(raw_spinlock_t *x)
10 volatile unsigned int *a = __ldcw_align(x);
11 return *a == 0;
14 #define __raw_spin_lock(lock) __raw_spin_lock_flags(lock, 0)
15 #define __raw_spin_unlock_wait(x) \
16 do { cpu_relax(); } while (__raw_spin_is_locked(x))
18 static inline void __raw_spin_lock_flags(raw_spinlock_t *x,
19 unsigned long flags)
21 volatile unsigned int *a;
23 mb();
24 a = __ldcw_align(x);
25 while (__ldcw(a) == 0)
26 while (*a == 0)
27 if (flags & PSW_SM_I) {
28 local_irq_enable();
29 cpu_relax();
30 local_irq_disable();
31 } else
32 cpu_relax();
33 mb();
36 static inline void __raw_spin_unlock(raw_spinlock_t *x)
38 volatile unsigned int *a;
39 mb();
40 a = __ldcw_align(x);
41 *a = 1;
42 mb();
45 static inline int __raw_spin_trylock(raw_spinlock_t *x)
47 volatile unsigned int *a;
48 int ret;
50 mb();
51 a = __ldcw_align(x);
52 ret = __ldcw(a) != 0;
53 mb();
55 return ret;
59 * Read-write spinlocks, allowing multiple readers
60 * but only one writer.
63 #define __raw_read_trylock(lock) generic__raw_read_trylock(lock)
65 /* read_lock, read_unlock are pretty straightforward. Of course it somehow
66 * sucks we end up saving/restoring flags twice for read_lock_irqsave aso. */
68 static __inline__ void __raw_read_lock(raw_rwlock_t *rw)
70 __raw_spin_lock(&rw->lock);
72 rw->counter++;
74 __raw_spin_unlock(&rw->lock);
77 static __inline__ void __raw_read_unlock(raw_rwlock_t *rw)
79 __raw_spin_lock(&rw->lock);
81 rw->counter--;
83 __raw_spin_unlock(&rw->lock);
86 /* write_lock is less trivial. We optimistically grab the lock and check
87 * if we surprised any readers. If so we release the lock and wait till
88 * they're all gone before trying again
90 * Also note that we don't use the _irqsave / _irqrestore suffixes here.
91 * If we're called with interrupts enabled and we've got readers (or other
92 * writers) in interrupt handlers someone fucked up and we'd dead-lock
93 * sooner or later anyway. prumpf */
95 static __inline__ void __raw_write_lock(raw_rwlock_t *rw)
97 retry:
98 __raw_spin_lock(&rw->lock);
100 if(rw->counter != 0) {
101 /* this basically never happens */
102 __raw_spin_unlock(&rw->lock);
104 while (rw->counter != 0)
105 cpu_relax();
107 goto retry;
110 /* got it. now leave without unlocking */
111 rw->counter = -1; /* remember we are locked */
114 /* write_unlock is absolutely trivial - we don't have to wait for anything */
116 static __inline__ void __raw_write_unlock(raw_rwlock_t *rw)
118 rw->counter = 0;
119 __raw_spin_unlock(&rw->lock);
122 static __inline__ int __raw_write_trylock(raw_rwlock_t *rw)
124 __raw_spin_lock(&rw->lock);
125 if (rw->counter != 0) {
126 /* this basically never happens */
127 __raw_spin_unlock(&rw->lock);
129 return 0;
132 /* got it. now leave without unlocking */
133 rw->counter = -1; /* remember we are locked */
134 return 1;
138 * read_can_lock - would read_trylock() succeed?
139 * @lock: the rwlock in question.
141 static __inline__ int __raw_read_can_lock(raw_rwlock_t *rw)
143 return rw->counter >= 0;
147 * write_can_lock - would write_trylock() succeed?
148 * @lock: the rwlock in question.
150 static __inline__ int __raw_write_can_lock(raw_rwlock_t *rw)
152 return !rw->counter;
155 #endif /* __ASM_SPINLOCK_H */