|
10 | 10 | static inline int arch_spin_is_locked(arch_spinlock_t *x)
|
11 | 11 | {
|
12 | 12 | volatile unsigned int *a = __ldcw_align(x);
|
| 13 | + smp_mb(); |
13 | 14 | return *a == 0;
|
14 | 15 | }
|
15 | 16 |
|
16 |
| -#define arch_spin_lock(lock) arch_spin_lock_flags(lock, 0) |
| 17 | +static inline void arch_spin_lock(arch_spinlock_t *x) |
| 18 | +{ |
| 19 | + volatile unsigned int *a; |
| 20 | + |
| 21 | + a = __ldcw_align(x); |
| 22 | + while (__ldcw(a) == 0) |
| 23 | + while (*a == 0) |
| 24 | + cpu_relax(); |
| 25 | +} |
17 | 26 |
|
18 | 27 | static inline void arch_spin_lock_flags(arch_spinlock_t *x,
|
19 | 28 | unsigned long flags)
|
20 | 29 | {
|
21 | 30 | volatile unsigned int *a;
|
| 31 | + unsigned long flags_dis; |
22 | 32 |
|
23 | 33 | a = __ldcw_align(x);
|
24 |
| - while (__ldcw(a) == 0) |
| 34 | + while (__ldcw(a) == 0) { |
| 35 | + local_save_flags(flags_dis); |
| 36 | + local_irq_restore(flags); |
25 | 37 | while (*a == 0)
|
26 |
| - if (flags & PSW_SM_I) { |
27 |
| - local_irq_enable(); |
28 |
| - cpu_relax(); |
29 |
| - local_irq_disable(); |
30 |
| - } else |
31 |
| - cpu_relax(); |
| 38 | + cpu_relax(); |
| 39 | + local_irq_restore(flags_dis); |
| 40 | + } |
32 | 41 | }
|
33 | 42 | #define arch_spin_lock_flags arch_spin_lock_flags
|
34 | 43 |
|
@@ -58,116 +67,93 @@ static inline int arch_spin_trylock(arch_spinlock_t *x)
|
58 | 67 |
|
59 | 68 | /*
|
60 | 69 | * Read-write spinlocks, allowing multiple readers but only one writer.
|
61 |
| - * Linux rwlocks are unfair to writers; they can be starved for an indefinite |
62 |
| - * time by readers. With care, they can also be taken in interrupt context. |
| 70 | + * Unfair locking as Writers could be starved indefinitely by Reader(s) |
63 | 71 | *
|
64 |
| - * In the PA-RISC implementation, we have a spinlock and a counter. |
65 |
| - * Readers use the lock to serialise their access to the counter (which |
66 |
| - * records how many readers currently hold the lock). |
67 |
| - * Writers hold the spinlock, preventing any readers or other writers from |
68 |
| - * grabbing the rwlock. |
| 72 | + * The spinlock itself is contained in @counter and access to it is |
| 73 | + * serialized with @lock_mutex. |
69 | 74 | */
|
70 | 75 |
|
71 |
| -/* Note that we have to ensure interrupts are disabled in case we're |
72 |
| - * interrupted by some other code that wants to grab the same read lock */ |
73 |
| -static __inline__ void arch_read_lock(arch_rwlock_t *rw) |
| 76 | +/* 1 - lock taken successfully */ |
| 77 | +static inline int arch_read_trylock(arch_rwlock_t *rw) |
74 | 78 | {
|
| 79 | + int ret = 0; |
75 | 80 | unsigned long flags;
|
76 |
| - local_irq_save(flags); |
77 |
| - arch_spin_lock_flags(&rw->lock, flags); |
78 |
| - rw->counter++; |
79 |
| - arch_spin_unlock(&rw->lock); |
80 |
| - local_irq_restore(flags); |
81 |
| -} |
82 | 81 |
|
83 |
| -/* Note that we have to ensure interrupts are disabled in case we're |
84 |
| - * interrupted by some other code that wants to grab the same read lock */ |
85 |
| -static __inline__ void arch_read_unlock(arch_rwlock_t *rw) |
86 |
| -{ |
87 |
| - unsigned long flags; |
88 | 82 | local_irq_save(flags);
|
89 |
| - arch_spin_lock_flags(&rw->lock, flags); |
90 |
| - rw->counter--; |
91 |
| - arch_spin_unlock(&rw->lock); |
| 83 | + arch_spin_lock(&(rw->lock_mutex)); |
| 84 | + |
| 85 | + /* |
| 86 | + * zero means writer holds the lock exclusively, deny Reader. |
| 87 | + * Otherwise grant lock to first/subseq reader |
| 88 | + */ |
| 89 | + if (rw->counter > 0) { |
| 90 | + rw->counter--; |
| 91 | + ret = 1; |
| 92 | + } |
| 93 | + |
| 94 | + arch_spin_unlock(&(rw->lock_mutex)); |
92 | 95 | local_irq_restore(flags);
|
| 96 | + |
| 97 | + return ret; |
93 | 98 | }
|
94 | 99 |
|
95 |
| -/* Note that we have to ensure interrupts are disabled in case we're |
96 |
| - * interrupted by some other code that wants to grab the same read lock */ |
97 |
| -static __inline__ int arch_read_trylock(arch_rwlock_t *rw) |
| 100 | +/* 1 - lock taken successfully */ |
| 101 | +static inline int arch_write_trylock(arch_rwlock_t *rw) |
98 | 102 | {
|
| 103 | + int ret = 0; |
99 | 104 | unsigned long flags;
|
100 |
| - retry: |
| 105 | + |
101 | 106 | local_irq_save(flags);
|
102 |
| - if (arch_spin_trylock(&rw->lock)) { |
103 |
| - rw->counter++; |
104 |
| - arch_spin_unlock(&rw->lock); |
105 |
| - local_irq_restore(flags); |
106 |
| - return 1; |
| 107 | + arch_spin_lock(&(rw->lock_mutex)); |
| 108 | + |
| 109 | + /* |
| 110 | + * If reader(s) hold lock (lock < __ARCH_RW_LOCK_UNLOCKED__), |
| 111 | + * deny writer. Otherwise if unlocked grant to writer |
| 112 | + * Hence the claim that Linux rwlocks are unfair to writers. |
| 113 | + * (can be starved for an indefinite time by readers). |
| 114 | + */ |
| 115 | + if (rw->counter == __ARCH_RW_LOCK_UNLOCKED__) { |
| 116 | + rw->counter = 0; |
| 117 | + ret = 1; |
107 | 118 | }
|
108 |
| - |
| 119 | + arch_spin_unlock(&(rw->lock_mutex)); |
109 | 120 | local_irq_restore(flags);
|
110 |
| - /* If write-locked, we fail to acquire the lock */ |
111 |
| - if (rw->counter < 0) |
112 |
| - return 0; |
113 | 121 |
|
114 |
| - /* Wait until we have a realistic chance at the lock */ |
115 |
| - while (arch_spin_is_locked(&rw->lock) && rw->counter >= 0) |
| 122 | + return ret; |
| 123 | +} |
| 124 | + |
| 125 | +static inline void arch_read_lock(arch_rwlock_t *rw) |
| 126 | +{ |
| 127 | + while (!arch_read_trylock(rw)) |
116 | 128 | cpu_relax();
|
| 129 | +} |
117 | 130 |
|
118 |
| - goto retry; |
| 131 | +static inline void arch_write_lock(arch_rwlock_t *rw) |
| 132 | +{ |
| 133 | + while (!arch_write_trylock(rw)) |
| 134 | + cpu_relax(); |
119 | 135 | }
|
120 | 136 |
|
121 |
| -/* Note that we have to ensure interrupts are disabled in case we're |
122 |
| - * interrupted by some other code that wants to read_trylock() this lock */ |
123 |
| -static __inline__ void arch_write_lock(arch_rwlock_t *rw) |
| 137 | +static inline void arch_read_unlock(arch_rwlock_t *rw) |
124 | 138 | {
|
125 | 139 | unsigned long flags;
|
126 |
| -retry: |
127 |
| - local_irq_save(flags); |
128 |
| - arch_spin_lock_flags(&rw->lock, flags); |
129 | 140 |
|
130 |
| - if (rw->counter != 0) { |
131 |
| - arch_spin_unlock(&rw->lock); |
132 |
| - local_irq_restore(flags); |
133 |
| - |
134 |
| - while (rw->counter != 0) |
135 |
| - cpu_relax(); |
136 |
| - |
137 |
| - goto retry; |
138 |
| - } |
139 |
| - |
140 |
| - rw->counter = -1; /* mark as write-locked */ |
141 |
| - mb(); |
| 141 | + local_irq_save(flags); |
| 142 | + arch_spin_lock(&(rw->lock_mutex)); |
| 143 | + rw->counter++; |
| 144 | + arch_spin_unlock(&(rw->lock_mutex)); |
142 | 145 | local_irq_restore(flags);
|
143 | 146 | }
|
144 | 147 |
|
145 |
| -static __inline__ void arch_write_unlock(arch_rwlock_t *rw) |
146 |
| -{ |
147 |
| - rw->counter = 0; |
148 |
| - arch_spin_unlock(&rw->lock); |
149 |
| -} |
150 |
| - |
151 |
| -/* Note that we have to ensure interrupts are disabled in case we're |
152 |
| - * interrupted by some other code that wants to read_trylock() this lock */ |
153 |
| -static __inline__ int arch_write_trylock(arch_rwlock_t *rw) |
| 148 | +static inline void arch_write_unlock(arch_rwlock_t *rw) |
154 | 149 | {
|
155 | 150 | unsigned long flags;
|
156 |
| - int result = 0; |
157 | 151 |
|
158 | 152 | local_irq_save(flags);
|
159 |
| - if (arch_spin_trylock(&rw->lock)) { |
160 |
| - if (rw->counter == 0) { |
161 |
| - rw->counter = -1; |
162 |
| - result = 1; |
163 |
| - } else { |
164 |
| - /* Read-locked. Oh well. */ |
165 |
| - arch_spin_unlock(&rw->lock); |
166 |
| - } |
167 |
| - } |
| 153 | + arch_spin_lock(&(rw->lock_mutex)); |
| 154 | + rw->counter = __ARCH_RW_LOCK_UNLOCKED__; |
| 155 | + arch_spin_unlock(&(rw->lock_mutex)); |
168 | 156 | local_irq_restore(flags);
|
169 |
| - |
170 |
| - return result; |
171 | 157 | }
|
172 | 158 |
|
173 | 159 | #endif /* __ASM_SPINLOCK_H */
|
0 commit comments