Rev 1787 | Rev 2071 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | jermar | 1 | /* |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
||
3 | * All rights reserved. |
||
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * - The name of the author may not be used to endorse or promote products |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
1757 | jermar | 29 | /** @addtogroup sync |
1702 | cejka | 30 | * @{ |
31 | */ |
||
32 | |||
1248 | jermar | 33 | /** |
1702 | cejka | 34 | * @file |
1248 | jermar | 35 | * @brief Reader/Writer locks. |
557 | jermar | 36 | * |
37 | * A reader/writer lock can be held by multiple readers at a time. |
||
38 | * Or it can be exclusively held by a sole writer at a time. |
||
1248 | jermar | 39 | * |
1 | jermar | 40 | * These locks are not recursive. |
1757 | jermar | 41 | * Because a technique called direct hand-off is used and because |
42 | * waiting takes place in a single wait queue, neither readers |
||
1248 | jermar | 43 | * nor writers will suffer starvation. |
1 | jermar | 44 | * |
45 | * If there is a writer followed by a reader waiting for the rwlock |
||
46 | * and the writer times out, all leading readers are automatically woken up |
||
47 | * and allowed in. |
||
48 | */ |
||
49 | |||
50 | /* |
||
51 | * NOTE ON rwlock_holder_type |
||
52 | * This field is set on an attempt to acquire the exclusive mutex |
||
53 | * to the respective value depending whether the caller is a reader |
||
54 | * or a writer. The field is examined only if the thread had been |
||
55 | * previously blocked on the exclusive mutex. Thus it is save |
||
56 | * to store the rwlock type in the thread structure, because |
||
57 | * each thread can block on only one rwlock at a time. |
||
58 | */ |
||
59 | |||
60 | #include <synch/rwlock.h> |
||
61 | #include <synch/spinlock.h> |
||
62 | #include <synch/mutex.h> |
||
63 | #include <synch/waitq.h> |
||
385 | jermar | 64 | #include <synch/synch.h> |
788 | jermar | 65 | #include <adt/list.h> |
1 | jermar | 66 | #include <typedefs.h> |
67 | #include <arch/asm.h> |
||
68 | #include <arch.h> |
||
69 | #include <proc/thread.h> |
||
70 | #include <panic.h> |
||
71 | |||
72 | #define ALLOW_ALL 0 |
||
73 | #define ALLOW_READERS_ONLY 1 |
||
74 | |||
75 | static void let_others_in(rwlock_t *rwl, int readers_only); |
||
76 | static void release_spinlock(void *arg); |
||
77 | |||
385 | jermar | 78 | /** Initialize reader/writer lock |
79 | * |
||
80 | * Initialize reader/writer lock. |
||
81 | * |
||
82 | * @param rwl Reader/Writer lock. |
||
83 | */ |
||
1 | jermar | 84 | void rwlock_initialize(rwlock_t *rwl) { |
557 | jermar | 85 | spinlock_initialize(&rwl->lock, "rwlock_t"); |
1 | jermar | 86 | mutex_initialize(&rwl->exclusive); |
87 | rwl->readers_in = 0; |
||
88 | } |
||
89 | |||
385 | jermar | 90 | /** Acquire reader/writer lock for reading |
91 | * |
||
92 | * Acquire reader/writer lock for reading. |
||
93 | * Timeout and willingness to block may be specified. |
||
94 | * |
||
95 | * @param rwl Reader/Writer lock. |
||
96 | * @param usec Timeout in microseconds. |
||
1502 | jermar | 97 | * @param flags Specify mode of operation. |
385 | jermar | 98 | * |
99 | * For exact description of possible combinations of |
||
1502 | jermar | 100 | * usec and flags, see comment for waitq_sleep_timeout(). |
385 | jermar | 101 | * |
102 | * @return See comment for waitq_sleep_timeout(). |
||
103 | */ |
||
1780 | jermar | 104 | int _rwlock_write_lock_timeout(rwlock_t *rwl, uint32_t usec, int flags) |
1 | jermar | 105 | { |
413 | jermar | 106 | ipl_t ipl; |
1 | jermar | 107 | int rc; |
108 | |||
413 | jermar | 109 | ipl = interrupts_disable(); |
15 | jermar | 110 | spinlock_lock(&THREAD->lock); |
111 | THREAD->rwlock_holder_type = RWLOCK_WRITER; |
||
112 | spinlock_unlock(&THREAD->lock); |
||
413 | jermar | 113 | interrupts_restore(ipl); |
1 | jermar | 114 | |
115 | /* |
||
116 | * Writers take the easy part. |
||
117 | * They just need to acquire the exclusive mutex. |
||
118 | */ |
||
1502 | jermar | 119 | rc = _mutex_lock_timeout(&rwl->exclusive, usec, flags); |
1 | jermar | 120 | if (SYNCH_FAILED(rc)) { |
121 | |||
122 | /* |
||
1502 | jermar | 123 | * Lock operation timed out or was interrupted. |
1 | jermar | 124 | * The state of rwl is UNKNOWN at this point. |
125 | * No claims about its holder can be made. |
||
126 | */ |
||
127 | |||
413 | jermar | 128 | ipl = interrupts_disable(); |
1 | jermar | 129 | spinlock_lock(&rwl->lock); |
130 | /* |
||
131 | * Now when rwl is locked, we can inspect it again. |
||
132 | * If it is held by some readers already, we can let |
||
133 | * readers from the head of the wait queue in. |
||
134 | */ |
||
135 | if (rwl->readers_in) |
||
136 | let_others_in(rwl, ALLOW_READERS_ONLY); |
||
137 | spinlock_unlock(&rwl->lock); |
||
413 | jermar | 138 | interrupts_restore(ipl); |
1 | jermar | 139 | } |
140 | |||
141 | return rc; |
||
142 | } |
||
143 | |||
385 | jermar | 144 | /** Acquire reader/writer lock for writing |
145 | * |
||
146 | * Acquire reader/writer lock for writing. |
||
147 | * Timeout and willingness to block may be specified. |
||
148 | * |
||
149 | * @param rwl Reader/Writer lock. |
||
150 | * @param usec Timeout in microseconds. |
||
1502 | jermar | 151 | * @param flags Select mode of operation. |
385 | jermar | 152 | * |
153 | * For exact description of possible combinations of |
||
1502 | jermar | 154 | * usec and flags, see comment for waitq_sleep_timeout(). |
385 | jermar | 155 | * |
156 | * @return See comment for waitq_sleep_timeout(). |
||
157 | */ |
||
1780 | jermar | 158 | int _rwlock_read_lock_timeout(rwlock_t *rwl, uint32_t usec, int flags) |
1 | jermar | 159 | { |
160 | int rc; |
||
413 | jermar | 161 | ipl_t ipl; |
1 | jermar | 162 | |
413 | jermar | 163 | ipl = interrupts_disable(); |
15 | jermar | 164 | spinlock_lock(&THREAD->lock); |
165 | THREAD->rwlock_holder_type = RWLOCK_READER; |
||
166 | spinlock_unlock(&THREAD->lock); |
||
1 | jermar | 167 | |
168 | spinlock_lock(&rwl->lock); |
||
169 | |||
170 | /* |
||
171 | * Find out whether we can get what we want without blocking. |
||
172 | */ |
||
173 | rc = mutex_trylock(&rwl->exclusive); |
||
174 | if (SYNCH_FAILED(rc)) { |
||
175 | |||
176 | /* |
||
177 | * 'exclusive' mutex is being held by someone else. |
||
178 | * If the holder is a reader and there is no one |
||
179 | * else waiting for it, we can enter the critical |
||
180 | * section. |
||
181 | */ |
||
182 | |||
183 | if (rwl->readers_in) { |
||
184 | spinlock_lock(&rwl->exclusive.sem.wq.lock); |
||
185 | if (list_empty(&rwl->exclusive.sem.wq.head)) { |
||
186 | /* |
||
187 | * We can enter. |
||
188 | */ |
||
189 | spinlock_unlock(&rwl->exclusive.sem.wq.lock); |
||
190 | goto shortcut; |
||
191 | } |
||
192 | spinlock_unlock(&rwl->exclusive.sem.wq.lock); |
||
193 | } |
||
194 | |||
195 | /* |
||
196 | * In order to prevent a race condition when a reader |
||
197 | * could block another reader at the head of the waitq, |
||
198 | * we register a function to unlock rwl->lock |
||
199 | * after this thread is put asleep. |
||
200 | */ |
||
623 | jermar | 201 | #ifdef CONFIG_SMP |
1 | jermar | 202 | thread_register_call_me(release_spinlock, &rwl->lock); |
623 | jermar | 203 | #else |
204 | thread_register_call_me(release_spinlock, NULL); |
||
205 | #endif |
||
1 | jermar | 206 | |
1502 | jermar | 207 | rc = _mutex_lock_timeout(&rwl->exclusive, usec, flags); |
1 | jermar | 208 | switch (rc) { |
1888 | jermar | 209 | case ESYNCH_WOULD_BLOCK: |
210 | /* |
||
211 | * release_spinlock() wasn't called |
||
212 | */ |
||
213 | thread_register_call_me(NULL, NULL); |
||
214 | spinlock_unlock(&rwl->lock); |
||
215 | case ESYNCH_TIMEOUT: |
||
216 | case ESYNCH_INTERRUPTED: |
||
217 | /* |
||
218 | * The sleep timed out. |
||
219 | * We just restore interrupt priority level. |
||
220 | */ |
||
221 | case ESYNCH_OK_BLOCKED: |
||
222 | /* |
||
223 | * We were woken with rwl->readers_in already incremented. |
||
224 | * Note that this arrangement avoids race condition between |
||
225 | * two concurrent readers. (Race is avoided if 'exclusive' is |
||
226 | * locked at the same time as 'readers_in' is incremented. |
||
227 | * Same time means both events happen atomically when |
||
228 | * rwl->lock is held.) |
||
229 | */ |
||
230 | interrupts_restore(ipl); |
||
231 | break; |
||
232 | case ESYNCH_OK_ATOMIC: |
||
233 | panic("_mutex_lock_timeout()==ESYNCH_OK_ATOMIC\n"); |
||
234 | break; |
||
235 | default: |
||
236 | panic("invalid ESYNCH\n"); |
||
237 | break; |
||
1 | jermar | 238 | } |
239 | return rc; |
||
240 | } |
||
241 | |||
242 | shortcut: |
||
243 | |||
244 | /* |
||
245 | * We can increment readers_in only if we didn't go to sleep. |
||
246 | * For sleepers, rwlock_let_others_in() will do the job. |
||
247 | */ |
||
248 | rwl->readers_in++; |
||
249 | |||
250 | spinlock_unlock(&rwl->lock); |
||
413 | jermar | 251 | interrupts_restore(ipl); |
1 | jermar | 252 | |
253 | return ESYNCH_OK_ATOMIC; |
||
254 | } |
||
255 | |||
385 | jermar | 256 | /** Release reader/writer lock held by writer |
257 | * |
||
258 | * Release reader/writer lock held by writer. |
||
259 | * Handoff reader/writer lock ownership directly |
||
260 | * to waiting readers or a writer. |
||
261 | * |
||
262 | * @param rwl Reader/Writer lock. |
||
263 | */ |
||
1 | jermar | 264 | void rwlock_write_unlock(rwlock_t *rwl) |
265 | { |
||
413 | jermar | 266 | ipl_t ipl; |
1 | jermar | 267 | |
413 | jermar | 268 | ipl = interrupts_disable(); |
1 | jermar | 269 | spinlock_lock(&rwl->lock); |
270 | let_others_in(rwl, ALLOW_ALL); |
||
271 | spinlock_unlock(&rwl->lock); |
||
413 | jermar | 272 | interrupts_restore(ipl); |
1 | jermar | 273 | |
274 | } |
||
275 | |||
385 | jermar | 276 | /** Release reader/writer lock held by reader |
277 | * |
||
278 | * Release reader/writer lock held by reader. |
||
279 | * Handoff reader/writer lock ownership directly |
||
280 | * to a waiting writer or don't do anything if more |
||
281 | * readers poses the lock. |
||
282 | * |
||
283 | * @param rwl Reader/Writer lock. |
||
284 | */ |
||
1 | jermar | 285 | void rwlock_read_unlock(rwlock_t *rwl) |
286 | { |
||
413 | jermar | 287 | ipl_t ipl; |
1 | jermar | 288 | |
413 | jermar | 289 | ipl = interrupts_disable(); |
1 | jermar | 290 | spinlock_lock(&rwl->lock); |
291 | if (!--rwl->readers_in) |
||
292 | let_others_in(rwl, ALLOW_ALL); |
||
293 | spinlock_unlock(&rwl->lock); |
||
413 | jermar | 294 | interrupts_restore(ipl); |
1 | jermar | 295 | } |
296 | |||
297 | |||
557 | jermar | 298 | /** Direct handoff of reader/writer lock ownership. |
385 | jermar | 299 | * |
300 | * Direct handoff of reader/writer lock ownership |
||
301 | * to waiting readers or a writer. |
||
302 | * |
||
1 | jermar | 303 | * Must be called with rwl->lock locked. |
413 | jermar | 304 | * Must be called with interrupts_disable()'d. |
385 | jermar | 305 | * |
306 | * @param rwl Reader/Writer lock. |
||
307 | * @param readers_only See the description below. |
||
308 | * |
||
1 | jermar | 309 | * If readers_only is false: (unlock scenario) |
310 | * Let the first sleeper on 'exclusive' mutex in, no matter |
||
311 | * whether it is a reader or a writer. If there are more leading |
||
312 | * readers in line, let each of them in. |
||
313 | * |
||
314 | * Otherwise: (timeout scenario) |
||
315 | * Let all leading readers in. |
||
316 | */ |
||
317 | void let_others_in(rwlock_t *rwl, int readers_only) |
||
318 | { |
||
319 | rwlock_type_t type = RWLOCK_NONE; |
||
320 | thread_t *t = NULL; |
||
557 | jermar | 321 | bool one_more = true; |
1 | jermar | 322 | |
323 | spinlock_lock(&rwl->exclusive.sem.wq.lock); |
||
324 | |||
325 | if (!list_empty(&rwl->exclusive.sem.wq.head)) |
||
326 | t = list_get_instance(rwl->exclusive.sem.wq.head.next, thread_t, wq_link); |
||
327 | do { |
||
328 | if (t) { |
||
329 | spinlock_lock(&t->lock); |
||
330 | type = t->rwlock_holder_type; |
||
331 | spinlock_unlock(&t->lock); |
||
332 | } |
||
333 | |||
334 | /* |
||
335 | * If readers_only is true, we wake all leading readers |
||
336 | * if and only if rwl is locked by another reader. |
||
337 | * Assumption: readers_only ==> rwl->readers_in |
||
338 | */ |
||
339 | if (readers_only && (type != RWLOCK_READER)) |
||
340 | break; |
||
341 | |||
342 | |||
343 | if (type == RWLOCK_READER) { |
||
344 | /* |
||
345 | * Waking up a reader. |
||
346 | * We are responsible for incrementing rwl->readers_in for it. |
||
347 | */ |
||
348 | rwl->readers_in++; |
||
349 | } |
||
350 | |||
351 | /* |
||
352 | * Only the last iteration through this loop can increment |
||
353 | * rwl->exclusive.sem.wq.missed_wakeup's. All preceeding |
||
354 | * iterations will wake up a thread. |
||
355 | */ |
||
356 | /* We call the internal version of waitq_wakeup, which |
||
357 | * relies on the fact that the waitq is already locked. |
||
358 | */ |
||
359 | _waitq_wakeup_unsafe(&rwl->exclusive.sem.wq, WAKEUP_FIRST); |
||
360 | |||
361 | t = NULL; |
||
362 | if (!list_empty(&rwl->exclusive.sem.wq.head)) { |
||
363 | t = list_get_instance(rwl->exclusive.sem.wq.head.next, thread_t, wq_link); |
||
364 | if (t) { |
||
365 | spinlock_lock(&t->lock); |
||
366 | if (t->rwlock_holder_type != RWLOCK_READER) |
||
557 | jermar | 367 | one_more = false; |
1 | jermar | 368 | spinlock_unlock(&t->lock); |
369 | } |
||
370 | } |
||
371 | } while ((type == RWLOCK_READER) && t && one_more); |
||
372 | |||
373 | spinlock_unlock(&rwl->exclusive.sem.wq.lock); |
||
374 | } |
||
375 | |||
385 | jermar | 376 | /** Release spinlock callback |
377 | * |
||
378 | * This is a callback function invoked from the scheduler. |
||
379 | * The callback is registered in _rwlock_read_lock_timeout(). |
||
380 | * |
||
381 | * @param arg Spinlock. |
||
382 | */ |
||
1 | jermar | 383 | void release_spinlock(void *arg) |
384 | { |
||
385 | spinlock_unlock((spinlock_t *) arg); |
||
386 | } |
||
1702 | cejka | 387 | |
1757 | jermar | 388 | /** @} |
1702 | cejka | 389 | */ |