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