Rev 1156 | Rev 1229 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1156 | Rev 1158 | ||
---|---|---|---|
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 | #include <synch/waitq.h> |
29 | #include <synch/waitq.h> |
30 | #include <synch/synch.h> |
30 | #include <synch/synch.h> |
31 | #include <synch/spinlock.h> |
31 | #include <synch/spinlock.h> |
32 | #include <proc/thread.h> |
32 | #include <proc/thread.h> |
33 | #include <proc/scheduler.h> |
33 | #include <proc/scheduler.h> |
34 | #include <arch/asm.h> |
34 | #include <arch/asm.h> |
35 | #include <arch/types.h> |
35 | #include <arch/types.h> |
36 | #include <typedefs.h> |
36 | #include <typedefs.h> |
37 | #include <time/timeout.h> |
37 | #include <time/timeout.h> |
38 | #include <arch.h> |
38 | #include <arch.h> |
39 | #include <context.h> |
39 | #include <context.h> |
40 | #include <adt/list.h> |
40 | #include <adt/list.h> |
41 | 41 | ||
42 | static void waitq_timeouted_sleep(void *data); |
42 | static void waitq_timeouted_sleep(void *data); |
43 | 43 | ||
44 | /** Initialize wait queue |
44 | /** Initialize wait queue |
45 | * |
45 | * |
46 | * Initialize wait queue. |
46 | * Initialize wait queue. |
47 | * |
47 | * |
48 | * @param wq Pointer to wait queue to be initialized. |
48 | * @param wq Pointer to wait queue to be initialized. |
49 | */ |
49 | */ |
50 | void waitq_initialize(waitq_t *wq) |
50 | void waitq_initialize(waitq_t *wq) |
51 | { |
51 | { |
52 | spinlock_initialize(&wq->lock, "waitq_lock"); |
52 | spinlock_initialize(&wq->lock, "waitq_lock"); |
53 | list_initialize(&wq->head); |
53 | list_initialize(&wq->head); |
54 | wq->missed_wakeups = 0; |
54 | wq->missed_wakeups = 0; |
55 | } |
55 | } |
56 | 56 | ||
57 | /** Handle timeout during waitq_sleep_timeout() call |
57 | /** Handle timeout during waitq_sleep_timeout() call |
58 | * |
58 | * |
59 | * This routine is called when waitq_sleep_timeout() timeouts. |
59 | * This routine is called when waitq_sleep_timeout() timeouts. |
60 | * Interrupts are disabled. |
60 | * Interrupts are disabled. |
61 | * |
61 | * |
62 | * It is supposed to try to remove 'its' thread from the wait queue; |
62 | * It is supposed to try to remove 'its' thread from the wait queue; |
63 | * it can eventually fail to achieve this goal when these two events |
63 | * it can eventually fail to achieve this goal when these two events |
64 | * overlap. In that case it behaves just as though there was no |
64 | * overlap. In that case it behaves just as though there was no |
65 | * timeout at all. |
65 | * timeout at all. |
66 | * |
66 | * |
67 | * @param data Pointer to the thread that called waitq_sleep_timeout(). |
67 | * @param data Pointer to the thread that called waitq_sleep_timeout(). |
68 | */ |
68 | */ |
69 | void waitq_timeouted_sleep(void *data) |
69 | void waitq_timeouted_sleep(void *data) |
70 | { |
70 | { |
71 | thread_t *t = (thread_t *) data; |
71 | thread_t *t = (thread_t *) data; |
72 | waitq_t *wq; |
72 | waitq_t *wq; |
73 | bool do_wakeup = false; |
73 | bool do_wakeup = false; |
74 | 74 | ||
75 | spinlock_lock(&threads_lock); |
75 | spinlock_lock(&threads_lock); |
76 | if (!list_member(&t->threads_link, &threads_head)) |
76 | if (!thread_exists(t)) |
77 | goto out; |
77 | goto out; |
78 | 78 | ||
79 | grab_locks: |
79 | grab_locks: |
80 | spinlock_lock(&t->lock); |
80 | spinlock_lock(&t->lock); |
81 | if ((wq = t->sleep_queue)) { /* assignment */ |
81 | if ((wq = t->sleep_queue)) { /* assignment */ |
82 | if (!spinlock_trylock(&wq->lock)) { |
82 | if (!spinlock_trylock(&wq->lock)) { |
83 | spinlock_unlock(&t->lock); |
83 | spinlock_unlock(&t->lock); |
84 | goto grab_locks; /* avoid deadlock */ |
84 | goto grab_locks; /* avoid deadlock */ |
85 | } |
85 | } |
86 | 86 | ||
87 | list_remove(&t->wq_link); |
87 | list_remove(&t->wq_link); |
88 | t->saved_context = t->sleep_timeout_context; |
88 | t->saved_context = t->sleep_timeout_context; |
89 | do_wakeup = true; |
89 | do_wakeup = true; |
90 | 90 | ||
91 | spinlock_unlock(&wq->lock); |
91 | spinlock_unlock(&wq->lock); |
92 | t->sleep_queue = NULL; |
92 | t->sleep_queue = NULL; |
93 | } |
93 | } |
94 | 94 | ||
95 | t->timeout_pending = false; |
95 | t->timeout_pending = false; |
96 | spinlock_unlock(&t->lock); |
96 | spinlock_unlock(&t->lock); |
97 | 97 | ||
98 | if (do_wakeup) |
98 | if (do_wakeup) |
99 | thread_ready(t); |
99 | thread_ready(t); |
100 | 100 | ||
101 | out: |
101 | out: |
102 | spinlock_unlock(&threads_lock); |
102 | spinlock_unlock(&threads_lock); |
103 | } |
103 | } |
104 | 104 | ||
105 | /** Interrupt sleeping thread. |
105 | /** Interrupt sleeping thread. |
106 | * |
106 | * |
107 | * This routine attempts to interrupt a thread from its sleep in a waitqueue. |
107 | * This routine attempts to interrupt a thread from its sleep in a waitqueue. |
108 | * If the thread is not found sleeping, no action is taken. |
108 | * If the thread is not found sleeping, no action is taken. |
109 | * |
109 | * |
110 | * @param t Thread to be interrupted. |
110 | * @param t Thread to be interrupted. |
111 | */ |
111 | */ |
112 | void waitq_interrupt_sleep(thread_t *t) |
112 | void waitq_interrupt_sleep(thread_t *t) |
113 | { |
113 | { |
114 | waitq_t *wq; |
114 | waitq_t *wq; |
115 | bool do_wakeup = false; |
115 | bool do_wakeup = false; |
116 | ipl_t ipl; |
116 | ipl_t ipl; |
117 | 117 | ||
118 | ipl = interrupts_disable(); |
118 | ipl = interrupts_disable(); |
119 | spinlock_lock(&threads_lock); |
119 | spinlock_lock(&threads_lock); |
120 | if (!list_member(&t->threads_link, &threads_head)) |
120 | if (!thread_exists(t)) |
121 | goto out; |
121 | goto out; |
122 | 122 | ||
123 | grab_locks: |
123 | grab_locks: |
124 | spinlock_lock(&t->lock); |
124 | spinlock_lock(&t->lock); |
125 | if ((wq = t->sleep_queue)) { /* assignment */ |
125 | if ((wq = t->sleep_queue)) { /* assignment */ |
126 | if (!spinlock_trylock(&wq->lock)) { |
126 | if (!spinlock_trylock(&wq->lock)) { |
127 | spinlock_unlock(&t->lock); |
127 | spinlock_unlock(&t->lock); |
128 | goto grab_locks; /* avoid deadlock */ |
128 | goto grab_locks; /* avoid deadlock */ |
129 | } |
129 | } |
130 | 130 | ||
131 | list_remove(&t->wq_link); |
131 | list_remove(&t->wq_link); |
132 | t->saved_context = t->sleep_interruption_context; |
132 | t->saved_context = t->sleep_interruption_context; |
133 | do_wakeup = true; |
133 | do_wakeup = true; |
134 | 134 | ||
135 | spinlock_unlock(&wq->lock); |
135 | spinlock_unlock(&wq->lock); |
136 | t->sleep_queue = NULL; |
136 | t->sleep_queue = NULL; |
137 | } |
137 | } |
138 | spinlock_unlock(&t->lock); |
138 | spinlock_unlock(&t->lock); |
139 | 139 | ||
140 | if (do_wakeup) |
140 | if (do_wakeup) |
141 | thread_ready(t); |
141 | thread_ready(t); |
142 | 142 | ||
143 | out: |
143 | out: |
144 | spinlock_unlock(&threads_lock); |
144 | spinlock_unlock(&threads_lock); |
145 | interrupts_restore(ipl); |
145 | interrupts_restore(ipl); |
146 | } |
146 | } |
147 | 147 | ||
148 | 148 | ||
149 | /** Sleep until either wakeup, timeout or interruption occurs |
149 | /** Sleep until either wakeup, timeout or interruption occurs |
150 | * |
150 | * |
151 | * This is a sleep implementation which allows itself to be |
151 | * This is a sleep implementation which allows itself to be |
152 | * interrupted from the sleep, restoring a failover context. |
152 | * interrupted from the sleep, restoring a failover context. |
153 | * |
153 | * |
154 | * Sleepers are organised in FIFO fashion in a structure called wait queue. |
154 | * Sleepers are organised in FIFO fashion in a structure called wait queue. |
155 | * |
155 | * |
156 | * This function is really basic in that other functions as waitq_sleep() |
156 | * This function is really basic in that other functions as waitq_sleep() |
157 | * and all the *_timeout() functions use it. |
157 | * and all the *_timeout() functions use it. |
158 | * |
158 | * |
159 | * @param wq Pointer to wait queue. |
159 | * @param wq Pointer to wait queue. |
160 | * @param usec Timeout in microseconds. |
160 | * @param usec Timeout in microseconds. |
161 | * @param nonblocking Blocking vs. non-blocking operation mode switch. |
161 | * @param nonblocking Blocking vs. non-blocking operation mode switch. |
162 | * |
162 | * |
163 | * If usec is greater than zero, regardless of the value of nonblocking, |
163 | * If usec is greater than zero, regardless of the value of nonblocking, |
164 | * the call will not return until either timeout or wakeup comes. |
164 | * the call will not return until either timeout or wakeup comes. |
165 | * |
165 | * |
166 | * If usec is zero and nonblocking is zero (false), the call |
166 | * If usec is zero and nonblocking is zero (false), the call |
167 | * will not return until wakeup comes. |
167 | * will not return until wakeup comes. |
168 | * |
168 | * |
169 | * If usec is zero and nonblocking is non-zero (true), the call will |
169 | * If usec is zero and nonblocking is non-zero (true), the call will |
170 | * immediately return, reporting either success or failure. |
170 | * immediately return, reporting either success or failure. |
171 | * |
171 | * |
172 | * @return Returns one of: ESYNCH_WOULD_BLOCK, ESYNCH_TIMEOUT, |
172 | * @return Returns one of: ESYNCH_WOULD_BLOCK, ESYNCH_TIMEOUT, |
173 | * ESYNCH_OK_ATOMIC, ESYNCH_OK_BLOCKED. |
173 | * ESYNCH_OK_ATOMIC, ESYNCH_OK_BLOCKED. |
174 | * |
174 | * |
175 | * ESYNCH_WOULD_BLOCK means that the sleep failed because at the time |
175 | * ESYNCH_WOULD_BLOCK means that the sleep failed because at the time |
176 | * of the call there was no pending wakeup. |
176 | * of the call there was no pending wakeup. |
177 | * |
177 | * |
178 | * ESYNCH_TIMEOUT means that the sleep timed out. |
178 | * ESYNCH_TIMEOUT means that the sleep timed out. |
179 | * |
179 | * |
180 | * ESYNCH_INTERRUPTED means that somebody interrupted the sleeping thread. |
180 | * ESYNCH_INTERRUPTED means that somebody interrupted the sleeping thread. |
181 | * |
181 | * |
182 | * ESYNCH_OK_ATOMIC means that the sleep succeeded and that there was |
182 | * ESYNCH_OK_ATOMIC means that the sleep succeeded and that there was |
183 | * a pending wakeup at the time of the call. The caller was not put |
183 | * a pending wakeup at the time of the call. The caller was not put |
184 | * asleep at all. |
184 | * asleep at all. |
185 | * |
185 | * |
186 | * ESYNCH_OK_BLOCKED means that the sleep succeeded; the full sleep was |
186 | * ESYNCH_OK_BLOCKED means that the sleep succeeded; the full sleep was |
187 | * attempted. |
187 | * attempted. |
188 | */ |
188 | */ |
189 | int waitq_sleep_timeout(waitq_t *wq, __u32 usec, int nonblocking) |
189 | int waitq_sleep_timeout(waitq_t *wq, __u32 usec, int nonblocking) |
190 | { |
190 | { |
191 | volatile ipl_t ipl; /* must be live after context_restore() */ |
191 | volatile ipl_t ipl; /* must be live after context_restore() */ |
192 | 192 | ||
193 | 193 | ||
194 | restart: |
194 | restart: |
195 | ipl = interrupts_disable(); |
195 | ipl = interrupts_disable(); |
196 | 196 | ||
197 | /* |
197 | /* |
198 | * Busy waiting for a delayed timeout. |
198 | * Busy waiting for a delayed timeout. |
199 | * This is an important fix for the race condition between |
199 | * This is an important fix for the race condition between |
200 | * a delayed timeout and a next call to waitq_sleep_timeout(). |
200 | * a delayed timeout and a next call to waitq_sleep_timeout(). |
201 | * Simply, the thread is not allowed to go to sleep if |
201 | * Simply, the thread is not allowed to go to sleep if |
202 | * there are timeouts in progress. |
202 | * there are timeouts in progress. |
203 | */ |
203 | */ |
204 | spinlock_lock(&THREAD->lock); |
204 | spinlock_lock(&THREAD->lock); |
205 | if (THREAD->timeout_pending) { |
205 | if (THREAD->timeout_pending) { |
206 | spinlock_unlock(&THREAD->lock); |
206 | spinlock_unlock(&THREAD->lock); |
207 | interrupts_restore(ipl); |
207 | interrupts_restore(ipl); |
208 | goto restart; |
208 | goto restart; |
209 | } |
209 | } |
210 | spinlock_unlock(&THREAD->lock); |
210 | spinlock_unlock(&THREAD->lock); |
211 | 211 | ||
212 | spinlock_lock(&wq->lock); |
212 | spinlock_lock(&wq->lock); |
213 | 213 | ||
214 | /* checks whether to go to sleep at all */ |
214 | /* checks whether to go to sleep at all */ |
215 | if (wq->missed_wakeups) { |
215 | if (wq->missed_wakeups) { |
216 | wq->missed_wakeups--; |
216 | wq->missed_wakeups--; |
217 | spinlock_unlock(&wq->lock); |
217 | spinlock_unlock(&wq->lock); |
218 | interrupts_restore(ipl); |
218 | interrupts_restore(ipl); |
219 | return ESYNCH_OK_ATOMIC; |
219 | return ESYNCH_OK_ATOMIC; |
220 | } |
220 | } |
221 | else { |
221 | else { |
222 | if (nonblocking && (usec == 0)) { |
222 | if (nonblocking && (usec == 0)) { |
223 | /* return immediatelly instead of going to sleep */ |
223 | /* return immediatelly instead of going to sleep */ |
224 | spinlock_unlock(&wq->lock); |
224 | spinlock_unlock(&wq->lock); |
225 | interrupts_restore(ipl); |
225 | interrupts_restore(ipl); |
226 | return ESYNCH_WOULD_BLOCK; |
226 | return ESYNCH_WOULD_BLOCK; |
227 | } |
227 | } |
228 | } |
228 | } |
229 | 229 | ||
230 | /* |
230 | /* |
231 | * Now we are firmly decided to go to sleep. |
231 | * Now we are firmly decided to go to sleep. |
232 | */ |
232 | */ |
233 | spinlock_lock(&THREAD->lock); |
233 | spinlock_lock(&THREAD->lock); |
234 | 234 | ||
235 | /* |
235 | /* |
236 | * Set context that will be restored if the sleep |
236 | * Set context that will be restored if the sleep |
237 | * of this thread is ever interrupted. |
237 | * of this thread is ever interrupted. |
238 | */ |
238 | */ |
239 | if (!context_save(&THREAD->sleep_interruption_context)) { |
239 | if (!context_save(&THREAD->sleep_interruption_context)) { |
240 | /* Short emulation of scheduler() return code. */ |
240 | /* Short emulation of scheduler() return code. */ |
241 | spinlock_unlock(&THREAD->lock); |
241 | spinlock_unlock(&THREAD->lock); |
242 | interrupts_restore(ipl); |
242 | interrupts_restore(ipl); |
243 | return ESYNCH_INTERRUPTED; |
243 | return ESYNCH_INTERRUPTED; |
244 | } |
244 | } |
245 | 245 | ||
246 | if (usec) { |
246 | if (usec) { |
247 | /* We use the timeout variant. */ |
247 | /* We use the timeout variant. */ |
248 | if (!context_save(&THREAD->sleep_timeout_context)) { |
248 | if (!context_save(&THREAD->sleep_timeout_context)) { |
249 | /* Short emulation of scheduler() return code. */ |
249 | /* Short emulation of scheduler() return code. */ |
250 | spinlock_unlock(&THREAD->lock); |
250 | spinlock_unlock(&THREAD->lock); |
251 | interrupts_restore(ipl); |
251 | interrupts_restore(ipl); |
252 | return ESYNCH_TIMEOUT; |
252 | return ESYNCH_TIMEOUT; |
253 | } |
253 | } |
254 | THREAD->timeout_pending = true; |
254 | THREAD->timeout_pending = true; |
255 | timeout_register(&THREAD->sleep_timeout, (__u64) usec, waitq_timeouted_sleep, THREAD); |
255 | timeout_register(&THREAD->sleep_timeout, (__u64) usec, waitq_timeouted_sleep, THREAD); |
256 | } |
256 | } |
257 | 257 | ||
258 | list_append(&THREAD->wq_link, &wq->head); |
258 | list_append(&THREAD->wq_link, &wq->head); |
259 | 259 | ||
260 | /* |
260 | /* |
261 | * Suspend execution. |
261 | * Suspend execution. |
262 | */ |
262 | */ |
263 | THREAD->state = Sleeping; |
263 | THREAD->state = Sleeping; |
264 | THREAD->sleep_queue = wq; |
264 | THREAD->sleep_queue = wq; |
265 | 265 | ||
266 | spinlock_unlock(&THREAD->lock); |
266 | spinlock_unlock(&THREAD->lock); |
267 | 267 | ||
268 | scheduler(); /* wq->lock is released in scheduler_separated_stack() */ |
268 | scheduler(); /* wq->lock is released in scheduler_separated_stack() */ |
269 | interrupts_restore(ipl); |
269 | interrupts_restore(ipl); |
270 | 270 | ||
271 | return ESYNCH_OK_BLOCKED; |
271 | return ESYNCH_OK_BLOCKED; |
272 | } |
272 | } |
273 | 273 | ||
274 | 274 | ||
275 | /** Wake up first thread sleeping in a wait queue |
275 | /** Wake up first thread sleeping in a wait queue |
276 | * |
276 | * |
277 | * Wake up first thread sleeping in a wait queue. |
277 | * Wake up first thread sleeping in a wait queue. |
278 | * This is the SMP- and IRQ-safe wrapper meant for |
278 | * This is the SMP- and IRQ-safe wrapper meant for |
279 | * general use. |
279 | * general use. |
280 | * |
280 | * |
281 | * Besides its 'normal' wakeup operation, it attempts |
281 | * Besides its 'normal' wakeup operation, it attempts |
282 | * to unregister possible timeout. |
282 | * to unregister possible timeout. |
283 | * |
283 | * |
284 | * @param wq Pointer to wait queue. |
284 | * @param wq Pointer to wait queue. |
285 | * @param all If this is non-zero, all sleeping threads |
285 | * @param all If this is non-zero, all sleeping threads |
286 | * will be woken up and missed count will be zeroed. |
286 | * will be woken up and missed count will be zeroed. |
287 | */ |
287 | */ |
288 | void waitq_wakeup(waitq_t *wq, bool all) |
288 | void waitq_wakeup(waitq_t *wq, bool all) |
289 | { |
289 | { |
290 | ipl_t ipl; |
290 | ipl_t ipl; |
291 | 291 | ||
292 | ipl = interrupts_disable(); |
292 | ipl = interrupts_disable(); |
293 | spinlock_lock(&wq->lock); |
293 | spinlock_lock(&wq->lock); |
294 | 294 | ||
295 | _waitq_wakeup_unsafe(wq, all); |
295 | _waitq_wakeup_unsafe(wq, all); |
296 | 296 | ||
297 | spinlock_unlock(&wq->lock); |
297 | spinlock_unlock(&wq->lock); |
298 | interrupts_restore(ipl); |
298 | interrupts_restore(ipl); |
299 | } |
299 | } |
300 | 300 | ||
301 | /** Internal SMP- and IRQ-unsafe version of waitq_wakeup() |
301 | /** Internal SMP- and IRQ-unsafe version of waitq_wakeup() |
302 | * |
302 | * |
303 | * This is the internal SMP- and IRQ-unsafe version |
303 | * This is the internal SMP- and IRQ-unsafe version |
304 | * of waitq_wakeup(). It assumes wq->lock is already |
304 | * of waitq_wakeup(). It assumes wq->lock is already |
305 | * locked and interrupts are already disabled. |
305 | * locked and interrupts are already disabled. |
306 | * |
306 | * |
307 | * @param wq Pointer to wait queue. |
307 | * @param wq Pointer to wait queue. |
308 | * @param all If this is non-zero, all sleeping threads |
308 | * @param all If this is non-zero, all sleeping threads |
309 | * will be woken up and missed count will be zeroed. |
309 | * will be woken up and missed count will be zeroed. |
310 | */ |
310 | */ |
311 | void _waitq_wakeup_unsafe(waitq_t *wq, bool all) |
311 | void _waitq_wakeup_unsafe(waitq_t *wq, bool all) |
312 | { |
312 | { |
313 | thread_t *t; |
313 | thread_t *t; |
314 | 314 | ||
315 | loop: |
315 | loop: |
316 | if (list_empty(&wq->head)) { |
316 | if (list_empty(&wq->head)) { |
317 | wq->missed_wakeups++; |
317 | wq->missed_wakeups++; |
318 | if (all) |
318 | if (all) |
319 | wq->missed_wakeups = 0; |
319 | wq->missed_wakeups = 0; |
320 | return; |
320 | return; |
321 | } |
321 | } |
322 | 322 | ||
323 | t = list_get_instance(wq->head.next, thread_t, wq_link); |
323 | t = list_get_instance(wq->head.next, thread_t, wq_link); |
324 | 324 | ||
325 | list_remove(&t->wq_link); |
325 | list_remove(&t->wq_link); |
326 | spinlock_lock(&t->lock); |
326 | spinlock_lock(&t->lock); |
327 | if (t->timeout_pending && timeout_unregister(&t->sleep_timeout)) |
327 | if (t->timeout_pending && timeout_unregister(&t->sleep_timeout)) |
328 | t->timeout_pending = false; |
328 | t->timeout_pending = false; |
329 | t->sleep_queue = NULL; |
329 | t->sleep_queue = NULL; |
330 | spinlock_unlock(&t->lock); |
330 | spinlock_unlock(&t->lock); |
331 | 331 | ||
332 | thread_ready(t); |
332 | thread_ready(t); |
333 | 333 | ||
334 | if (all) |
334 | if (all) |
335 | goto loop; |
335 | goto loop; |
336 | } |
336 | } |
337 | 337 |