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