]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* rwsem-spinlock.c: R/W semaphores: contention handling functions for |
2 | * generic spinlock implementation | |
3 | * | |
4 | * Copyright (c) 2001 David Howells (dhowells@redhat.com). | |
5 | * - Derived partially from idea by Andrea Arcangeli <andrea@suse.de> | |
6 | * - Derived also from comments by Linus | |
7 | */ | |
8 | #include <linux/rwsem.h> | |
9 | #include <linux/sched.h> | |
8bc3bcc9 | 10 | #include <linux/export.h> |
1da177e4 | 11 | |
e2d57f78 ML |
12 | enum rwsem_waiter_type { |
13 | RWSEM_WAITING_FOR_WRITE, | |
14 | RWSEM_WAITING_FOR_READ | |
15 | }; | |
16 | ||
1da177e4 LT |
17 | struct rwsem_waiter { |
18 | struct list_head list; | |
19 | struct task_struct *task; | |
e2d57f78 | 20 | enum rwsem_waiter_type type; |
1da177e4 LT |
21 | }; |
22 | ||
29671f22 AW |
23 | int rwsem_is_locked(struct rw_semaphore *sem) |
24 | { | |
25 | int ret = 1; | |
26 | unsigned long flags; | |
27 | ||
ddb6c9b5 | 28 | if (raw_spin_trylock_irqsave(&sem->wait_lock, flags)) { |
13b9a962 | 29 | ret = (sem->count != 0); |
ddb6c9b5 | 30 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
29671f22 AW |
31 | } |
32 | return ret; | |
33 | } | |
34 | EXPORT_SYMBOL(rwsem_is_locked); | |
35 | ||
1da177e4 LT |
36 | /* |
37 | * initialise the semaphore | |
38 | */ | |
4ea2176d IM |
39 | void __init_rwsem(struct rw_semaphore *sem, const char *name, |
40 | struct lock_class_key *key) | |
1da177e4 | 41 | { |
4ea2176d IM |
42 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
43 | /* | |
44 | * Make sure we are not reinitializing a held semaphore: | |
45 | */ | |
46 | debug_check_no_locks_freed((void *)sem, sizeof(*sem)); | |
4dfbb9d8 | 47 | lockdep_init_map(&sem->dep_map, name, key, 0); |
4ea2176d | 48 | #endif |
13b9a962 | 49 | sem->count = 0; |
ddb6c9b5 | 50 | raw_spin_lock_init(&sem->wait_lock); |
1da177e4 | 51 | INIT_LIST_HEAD(&sem->wait_list); |
1da177e4 | 52 | } |
118d52da | 53 | EXPORT_SYMBOL(__init_rwsem); |
1da177e4 LT |
54 | |
55 | /* | |
56 | * handle the lock release when processes blocked on it that can now run | |
57 | * - if we come here, then: | |
58 | * - the 'active count' _reached_ zero | |
59 | * - the 'waiting count' is non-zero | |
60 | * - the spinlock must be held by the caller | |
61 | * - woken process blocks are discarded from the list after having task zeroed | |
62 | * - writers are only woken if wakewrite is non-zero | |
63 | */ | |
64 | static inline struct rw_semaphore * | |
65 | __rwsem_do_wake(struct rw_semaphore *sem, int wakewrite) | |
66 | { | |
67 | struct rwsem_waiter *waiter; | |
68 | struct task_struct *tsk; | |
69 | int woken; | |
70 | ||
1da177e4 LT |
71 | waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list); |
72 | ||
e2d57f78 | 73 | if (waiter->type == RWSEM_WAITING_FOR_WRITE) { |
8cf5322c ML |
74 | if (wakewrite) |
75 | /* Wake up a writer. Note that we do not grant it the | |
76 | * lock - it will have to acquire it when it runs. */ | |
77 | wake_up_process(waiter->task); | |
1da177e4 LT |
78 | goto out; |
79 | } | |
80 | ||
81 | /* grant an infinite number of read locks to the front of the queue */ | |
1da177e4 | 82 | woken = 0; |
8cf5322c | 83 | do { |
1da177e4 LT |
84 | struct list_head *next = waiter->list.next; |
85 | ||
86 | list_del(&waiter->list); | |
87 | tsk = waiter->task; | |
49e4b2bc DB |
88 | /* |
89 | * Make sure we do not wakeup the next reader before | |
90 | * setting the nil condition to grant the next reader; | |
91 | * otherwise we could miss the wakeup on the other | |
92 | * side and end up sleeping again. See the pairing | |
93 | * in rwsem_down_read_failed(). | |
94 | */ | |
d59dd462 | 95 | smp_mb(); |
1da177e4 LT |
96 | waiter->task = NULL; |
97 | wake_up_process(tsk); | |
98 | put_task_struct(tsk); | |
99 | woken++; | |
8cf5322c | 100 | if (next == &sem->wait_list) |
1da177e4 LT |
101 | break; |
102 | waiter = list_entry(next, struct rwsem_waiter, list); | |
8cf5322c | 103 | } while (waiter->type != RWSEM_WAITING_FOR_WRITE); |
1da177e4 | 104 | |
13b9a962 | 105 | sem->count += woken; |
1da177e4 LT |
106 | |
107 | out: | |
1da177e4 LT |
108 | return sem; |
109 | } | |
110 | ||
111 | /* | |
112 | * wake a single writer | |
113 | */ | |
114 | static inline struct rw_semaphore * | |
115 | __rwsem_wake_one_writer(struct rw_semaphore *sem) | |
116 | { | |
117 | struct rwsem_waiter *waiter; | |
1da177e4 LT |
118 | |
119 | waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list); | |
41ef8f82 | 120 | wake_up_process(waiter->task); |
1da177e4 | 121 | |
1da177e4 LT |
122 | return sem; |
123 | } | |
124 | ||
125 | /* | |
126 | * get a read lock on the semaphore | |
127 | */ | |
9f741cb8 | 128 | void __sched __down_read(struct rw_semaphore *sem) |
1da177e4 LT |
129 | { |
130 | struct rwsem_waiter waiter; | |
131 | struct task_struct *tsk; | |
3eac4aba | 132 | unsigned long flags; |
1da177e4 | 133 | |
ddb6c9b5 | 134 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
1da177e4 | 135 | |
13b9a962 | 136 | if (sem->count >= 0 && list_empty(&sem->wait_list)) { |
1da177e4 | 137 | /* granted */ |
13b9a962 | 138 | sem->count++; |
ddb6c9b5 | 139 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
1da177e4 LT |
140 | goto out; |
141 | } | |
142 | ||
143 | tsk = current; | |
144 | set_task_state(tsk, TASK_UNINTERRUPTIBLE); | |
145 | ||
146 | /* set up my own style of waitqueue */ | |
147 | waiter.task = tsk; | |
e2d57f78 | 148 | waiter.type = RWSEM_WAITING_FOR_READ; |
1da177e4 LT |
149 | get_task_struct(tsk); |
150 | ||
151 | list_add_tail(&waiter.list, &sem->wait_list); | |
152 | ||
153 | /* we don't need to touch the semaphore struct anymore */ | |
ddb6c9b5 | 154 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
1da177e4 LT |
155 | |
156 | /* wait to be given the lock */ | |
157 | for (;;) { | |
158 | if (!waiter.task) | |
159 | break; | |
160 | schedule(); | |
161 | set_task_state(tsk, TASK_UNINTERRUPTIBLE); | |
162 | } | |
163 | ||
73105994 | 164 | __set_task_state(tsk, TASK_RUNNING); |
1da177e4 | 165 | out: |
c4e05116 | 166 | ; |
1da177e4 LT |
167 | } |
168 | ||
169 | /* | |
170 | * trylock for reading -- returns 1 if successful, 0 if contention | |
171 | */ | |
9f741cb8 | 172 | int __down_read_trylock(struct rw_semaphore *sem) |
1da177e4 LT |
173 | { |
174 | unsigned long flags; | |
175 | int ret = 0; | |
176 | ||
1da177e4 | 177 | |
ddb6c9b5 | 178 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
1da177e4 | 179 | |
13b9a962 | 180 | if (sem->count >= 0 && list_empty(&sem->wait_list)) { |
1da177e4 | 181 | /* granted */ |
13b9a962 | 182 | sem->count++; |
1da177e4 LT |
183 | ret = 1; |
184 | } | |
185 | ||
ddb6c9b5 | 186 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
1da177e4 | 187 | |
1da177e4 LT |
188 | return ret; |
189 | } | |
190 | ||
191 | /* | |
192 | * get a write lock on the semaphore | |
1da177e4 | 193 | */ |
d4799608 | 194 | int __sched __down_write_common(struct rw_semaphore *sem, int state) |
1da177e4 LT |
195 | { |
196 | struct rwsem_waiter waiter; | |
197 | struct task_struct *tsk; | |
3eac4aba | 198 | unsigned long flags; |
d4799608 | 199 | int ret = 0; |
1da177e4 | 200 | |
ddb6c9b5 | 201 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
1da177e4 | 202 | |
1da177e4 | 203 | /* set up my own style of waitqueue */ |
41ef8f82 | 204 | tsk = current; |
1da177e4 | 205 | waiter.task = tsk; |
e2d57f78 | 206 | waiter.type = RWSEM_WAITING_FOR_WRITE; |
1da177e4 LT |
207 | list_add_tail(&waiter.list, &sem->wait_list); |
208 | ||
41ef8f82 | 209 | /* wait for someone to release the lock */ |
1da177e4 | 210 | for (;;) { |
41ef8f82 YL |
211 | /* |
212 | * That is the key to support write lock stealing: allows the | |
213 | * task already on CPU to get the lock soon rather than put | |
214 | * itself into sleep and waiting for system woke it or someone | |
215 | * else in the head of the wait list up. | |
216 | */ | |
13b9a962 | 217 | if (sem->count == 0) |
1da177e4 | 218 | break; |
d4799608 MH |
219 | if (signal_pending_state(state, current)) { |
220 | ret = -EINTR; | |
221 | goto out; | |
222 | } | |
223 | set_task_state(tsk, state); | |
41ef8f82 YL |
224 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
225 | schedule(); | |
226 | raw_spin_lock_irqsave(&sem->wait_lock, flags); | |
1da177e4 | 227 | } |
41ef8f82 | 228 | /* got the lock */ |
13b9a962 | 229 | sem->count = -1; |
d4799608 | 230 | out: |
41ef8f82 | 231 | list_del(&waiter.list); |
1da177e4 | 232 | |
41ef8f82 | 233 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
d4799608 MH |
234 | |
235 | return ret; | |
236 | } | |
237 | ||
238 | void __sched __down_write(struct rw_semaphore *sem) | |
239 | { | |
240 | __down_write_common(sem, TASK_UNINTERRUPTIBLE); | |
241 | } | |
242 | ||
243 | int __sched __down_write_killable(struct rw_semaphore *sem) | |
244 | { | |
245 | return __down_write_common(sem, TASK_KILLABLE); | |
1da177e4 LT |
246 | } |
247 | ||
248 | /* | |
249 | * trylock for writing -- returns 1 if successful, 0 if contention | |
250 | */ | |
9f741cb8 | 251 | int __down_write_trylock(struct rw_semaphore *sem) |
1da177e4 LT |
252 | { |
253 | unsigned long flags; | |
254 | int ret = 0; | |
255 | ||
ddb6c9b5 | 256 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
1da177e4 | 257 | |
13b9a962 | 258 | if (sem->count == 0) { |
41ef8f82 | 259 | /* got the lock */ |
13b9a962 | 260 | sem->count = -1; |
1da177e4 LT |
261 | ret = 1; |
262 | } | |
263 | ||
ddb6c9b5 | 264 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
1da177e4 | 265 | |
1da177e4 LT |
266 | return ret; |
267 | } | |
268 | ||
269 | /* | |
270 | * release a read lock on the semaphore | |
271 | */ | |
9f741cb8 | 272 | void __up_read(struct rw_semaphore *sem) |
1da177e4 LT |
273 | { |
274 | unsigned long flags; | |
275 | ||
ddb6c9b5 | 276 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
1da177e4 | 277 | |
13b9a962 | 278 | if (--sem->count == 0 && !list_empty(&sem->wait_list)) |
1da177e4 LT |
279 | sem = __rwsem_wake_one_writer(sem); |
280 | ||
ddb6c9b5 | 281 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
1da177e4 LT |
282 | } |
283 | ||
284 | /* | |
285 | * release a write lock on the semaphore | |
286 | */ | |
9f741cb8 | 287 | void __up_write(struct rw_semaphore *sem) |
1da177e4 LT |
288 | { |
289 | unsigned long flags; | |
290 | ||
ddb6c9b5 | 291 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
1da177e4 | 292 | |
13b9a962 | 293 | sem->count = 0; |
1da177e4 LT |
294 | if (!list_empty(&sem->wait_list)) |
295 | sem = __rwsem_do_wake(sem, 1); | |
296 | ||
ddb6c9b5 | 297 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
1da177e4 LT |
298 | } |
299 | ||
300 | /* | |
301 | * downgrade a write lock into a read lock | |
302 | * - just wake up any readers at the front of the queue | |
303 | */ | |
9f741cb8 | 304 | void __downgrade_write(struct rw_semaphore *sem) |
1da177e4 LT |
305 | { |
306 | unsigned long flags; | |
307 | ||
ddb6c9b5 | 308 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
1da177e4 | 309 | |
13b9a962 | 310 | sem->count = 1; |
1da177e4 LT |
311 | if (!list_empty(&sem->wait_list)) |
312 | sem = __rwsem_do_wake(sem, 0); | |
313 | ||
ddb6c9b5 | 314 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
1da177e4 LT |
315 | } |
316 |