Rev 2087 | Rev 2109 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2087 | Rev 2089 | ||
---|---|---|---|
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 Wait queue. |
35 | * @brief Wait queue. |
36 | * |
36 | * |
37 | * Wait queue is the basic synchronization primitive upon which all |
37 | * Wait queue is the basic synchronization primitive upon which all |
38 | * other synchronization primitives build. |
38 | * other synchronization primitives build. |
39 | * |
39 | * |
40 | * It allows threads to wait for an event in first-come, first-served |
40 | * It allows threads to wait for an event in first-come, first-served |
41 | * fashion. Conditional operation as well as timeouts and interruptions |
41 | * fashion. Conditional operation as well as timeouts and interruptions |
42 | * are supported. |
42 | * are supported. |
43 | */ |
43 | */ |
44 | 44 | ||
45 | #include <synch/waitq.h> |
45 | #include <synch/waitq.h> |
46 | #include <synch/synch.h> |
46 | #include <synch/synch.h> |
47 | #include <synch/spinlock.h> |
47 | #include <synch/spinlock.h> |
48 | #include <proc/thread.h> |
48 | #include <proc/thread.h> |
49 | #include <proc/scheduler.h> |
49 | #include <proc/scheduler.h> |
50 | #include <arch/asm.h> |
50 | #include <arch/asm.h> |
51 | #include <arch/types.h> |
51 | #include <arch/types.h> |
52 | #include <typedefs.h> |
- | |
53 | #include <time/timeout.h> |
52 | #include <time/timeout.h> |
54 | #include <arch.h> |
53 | #include <arch.h> |
55 | #include <context.h> |
54 | #include <context.h> |
56 | #include <adt/list.h> |
55 | #include <adt/list.h> |
57 | 56 | ||
58 | static void waitq_timeouted_sleep(void *data); |
57 | static void waitq_timeouted_sleep(void *data); |
59 | 58 | ||
60 | /** Initialize wait queue |
59 | /** Initialize wait queue |
61 | * |
60 | * |
62 | * Initialize wait queue. |
61 | * Initialize wait queue. |
63 | * |
62 | * |
64 | * @param wq Pointer to wait queue to be initialized. |
63 | * @param wq Pointer to wait queue to be initialized. |
65 | */ |
64 | */ |
66 | void waitq_initialize(waitq_t *wq) |
65 | void waitq_initialize(waitq_t *wq) |
67 | { |
66 | { |
68 | spinlock_initialize(&wq->lock, "waitq_lock"); |
67 | spinlock_initialize(&wq->lock, "waitq_lock"); |
69 | list_initialize(&wq->head); |
68 | list_initialize(&wq->head); |
70 | wq->missed_wakeups = 0; |
69 | wq->missed_wakeups = 0; |
71 | } |
70 | } |
72 | 71 | ||
73 | /** Handle timeout during waitq_sleep_timeout() call |
72 | /** Handle timeout during waitq_sleep_timeout() call |
74 | * |
73 | * |
75 | * This routine is called when waitq_sleep_timeout() timeouts. |
74 | * This routine is called when waitq_sleep_timeout() timeouts. |
76 | * Interrupts are disabled. |
75 | * Interrupts are disabled. |
77 | * |
76 | * |
78 | * It is supposed to try to remove 'its' thread from the wait queue; |
77 | * It is supposed to try to remove 'its' thread from the wait queue; |
79 | * it can eventually fail to achieve this goal when these two events |
78 | * it can eventually fail to achieve this goal when these two events |
80 | * overlap. In that case it behaves just as though there was no |
79 | * overlap. In that case it behaves just as though there was no |
81 | * timeout at all. |
80 | * timeout at all. |
82 | * |
81 | * |
83 | * @param data Pointer to the thread that called waitq_sleep_timeout(). |
82 | * @param data Pointer to the thread that called waitq_sleep_timeout(). |
84 | */ |
83 | */ |
85 | void waitq_timeouted_sleep(void *data) |
84 | void waitq_timeouted_sleep(void *data) |
86 | { |
85 | { |
87 | thread_t *t = (thread_t *) data; |
86 | thread_t *t = (thread_t *) data; |
88 | waitq_t *wq; |
87 | waitq_t *wq; |
89 | bool do_wakeup = false; |
88 | bool do_wakeup = false; |
90 | 89 | ||
91 | spinlock_lock(&threads_lock); |
90 | spinlock_lock(&threads_lock); |
92 | if (!thread_exists(t)) |
91 | if (!thread_exists(t)) |
93 | goto out; |
92 | goto out; |
94 | 93 | ||
95 | grab_locks: |
94 | grab_locks: |
96 | spinlock_lock(&t->lock); |
95 | spinlock_lock(&t->lock); |
97 | if ((wq = t->sleep_queue)) { /* assignment */ |
96 | if ((wq = t->sleep_queue)) { /* assignment */ |
98 | if (!spinlock_trylock(&wq->lock)) { |
97 | if (!spinlock_trylock(&wq->lock)) { |
99 | spinlock_unlock(&t->lock); |
98 | spinlock_unlock(&t->lock); |
100 | goto grab_locks; /* avoid deadlock */ |
99 | goto grab_locks; /* avoid deadlock */ |
101 | } |
100 | } |
102 | 101 | ||
103 | list_remove(&t->wq_link); |
102 | list_remove(&t->wq_link); |
104 | t->saved_context = t->sleep_timeout_context; |
103 | t->saved_context = t->sleep_timeout_context; |
105 | do_wakeup = true; |
104 | do_wakeup = true; |
106 | t->sleep_queue = NULL; |
105 | t->sleep_queue = NULL; |
107 | spinlock_unlock(&wq->lock); |
106 | spinlock_unlock(&wq->lock); |
108 | } |
107 | } |
109 | 108 | ||
110 | t->timeout_pending = false; |
109 | t->timeout_pending = false; |
111 | spinlock_unlock(&t->lock); |
110 | spinlock_unlock(&t->lock); |
112 | 111 | ||
113 | if (do_wakeup) |
112 | if (do_wakeup) |
114 | thread_ready(t); |
113 | thread_ready(t); |
115 | 114 | ||
116 | out: |
115 | out: |
117 | spinlock_unlock(&threads_lock); |
116 | spinlock_unlock(&threads_lock); |
118 | } |
117 | } |
119 | 118 | ||
120 | /** Interrupt sleeping thread. |
- | |
121 | * |
- | |
122 | * This routine attempts to interrupt a thread from its sleep in a waitqueue. |
- | |
123 | * If the thread is not found sleeping, no action is taken. |
- | |
124 | * |
- | |
125 | * @param t Thread to be interrupted. |
- | |
126 | */ |
- | |
127 | void waitq_interrupt_sleep(thread_t *t) |
- | |
128 | { |
- | |
129 | waitq_t *wq; |
- | |
130 | bool do_wakeup = false; |
- | |
131 | ipl_t ipl; |
- | |
132 | - | ||
133 | ipl = interrupts_disable(); |
- | |
134 | spinlock_lock(&threads_lock); |
- | |
135 | if (!thread_exists(t)) |
- | |
136 | goto out; |
- | |
137 | - | ||
138 | grab_locks: |
- | |
139 | spinlock_lock(&t->lock); |
- | |
140 | if ((wq = t->sleep_queue)) { /* assignment */ |
- | |
141 | if (!(t->sleep_interruptible)) { |
- | |
142 | /* |
- | |
143 | * The sleep cannot be interrupted. |
- | |
144 | */ |
- | |
145 | spinlock_unlock(&t->lock); |
- | |
146 | goto out; |
- | |
147 | } |
- | |
148 | - | ||
149 | if (!spinlock_trylock(&wq->lock)) { |
- | |
150 | spinlock_unlock(&t->lock); |
- | |
151 | goto grab_locks; /* avoid deadlock */ |
- | |
152 | } |
- | |
153 | - | ||
154 | if (t->timeout_pending && timeout_unregister(&t->sleep_timeout)) |
- | |
155 | t->timeout_pending = false; |
- | |
156 | - | ||
157 | list_remove(&t->wq_link); |
- | |
158 | t->saved_context = t->sleep_interruption_context; |
- | |
159 | do_wakeup = true; |
- | |
160 | t->sleep_queue = NULL; |
- | |
161 | spinlock_unlock(&wq->lock); |
- | |
162 | } |
- | |
163 | spinlock_unlock(&t->lock); |
- | |
164 | - | ||
165 | if (do_wakeup) |
- | |
166 | thread_ready(t); |
- | |
167 | - | ||
168 | out: |
- | |
169 | spinlock_unlock(&threads_lock); |
- | |
170 | interrupts_restore(ipl); |
- | |
171 | } |
- | |
172 | 119 | ||
173 | /** Sleep until either wakeup, timeout or interruption occurs |
120 | /** Sleep until either wakeup, timeout or interruption occurs |
174 | * |
121 | * |
175 | * This is a sleep implementation which allows itself to time out or to be |
122 | * This is a sleep implementation which allows itself to time out or to be |
176 | * interrupted from the sleep, restoring a failover context. |
123 | * interrupted from the sleep, restoring a failover context. |
177 | * |
124 | * |
178 | * Sleepers are organised in a FIFO fashion in a structure called wait queue. |
125 | * Sleepers are organised in a FIFO fashion in a structure called wait queue. |
179 | * |
126 | * |
180 | * This function is really basic in that other functions as waitq_sleep() |
127 | * This function is really basic in that other functions as waitq_sleep() |
181 | * and all the *_timeout() functions use it. |
128 | * and all the *_timeout() functions use it. |
182 | * |
129 | * |
183 | * @param wq Pointer to wait queue. |
130 | * @param wq Pointer to wait queue. |
184 | * @param usec Timeout in microseconds. |
131 | * @param usec Timeout in microseconds. |
185 | * @param flags Specify mode of the sleep. |
132 | * @param flags Specify mode of the sleep. |
186 | * |
133 | * |
187 | * The sleep can be interrupted only if the |
134 | * The sleep can be interrupted only if the |
188 | * SYNCH_FLAGS_INTERRUPTIBLE bit is specified in flags. |
135 | * SYNCH_FLAGS_INTERRUPTIBLE bit is specified in flags. |
189 | * |
136 | * |
190 | * If usec is greater than zero, regardless of the value of the |
137 | * If usec is greater than zero, regardless of the value of the |
191 | * SYNCH_FLAGS_NON_BLOCKING bit in flags, the call will not return until either |
138 | * SYNCH_FLAGS_NON_BLOCKING bit in flags, the call will not return until either |
192 | * timeout, interruption or wakeup comes. |
139 | * timeout, interruption or wakeup comes. |
193 | * |
140 | * |
194 | * If usec is zero and the SYNCH_FLAGS_NON_BLOCKING bit is not set in flags, |
141 | * If usec is zero and the SYNCH_FLAGS_NON_BLOCKING bit is not set in flags, |
195 | * the call will not return until wakeup or interruption comes. |
142 | * the call will not return until wakeup or interruption comes. |
196 | * |
143 | * |
197 | * If usec is zero and the SYNCH_FLAGS_NON_BLOCKING bit is set in flags, the |
144 | * If usec is zero and the SYNCH_FLAGS_NON_BLOCKING bit is set in flags, the |
198 | * call will immediately return, reporting either success or failure. |
145 | * call will immediately return, reporting either success or failure. |
199 | * |
146 | * |
200 | * @return One of: ESYNCH_WOULD_BLOCK, ESYNCH_TIMEOUT, ESYNCH_INTERRUPTED, |
147 | * @return One of: ESYNCH_WOULD_BLOCK, ESYNCH_TIMEOUT, ESYNCH_INTERRUPTED, |
201 | * ESYNCH_OK_ATOMIC, ESYNCH_OK_BLOCKED. |
148 | * ESYNCH_OK_ATOMIC, ESYNCH_OK_BLOCKED. |
202 | * |
149 | * |
203 | * @li ESYNCH_WOULD_BLOCK means that the sleep failed because at the time of the |
150 | * @li ESYNCH_WOULD_BLOCK means that the sleep failed because at the time of the |
204 | * call there was no pending wakeup. |
151 | * call there was no pending wakeup. |
205 | * |
152 | * |
206 | * @li ESYNCH_TIMEOUT means that the sleep timed out. |
153 | * @li ESYNCH_TIMEOUT means that the sleep timed out. |
207 | * |
154 | * |
208 | * @li ESYNCH_INTERRUPTED means that somebody interrupted the sleeping thread. |
155 | * @li ESYNCH_INTERRUPTED means that somebody interrupted the sleeping thread. |
209 | * |
156 | * |
210 | * @li ESYNCH_OK_ATOMIC means that the sleep succeeded and that there was |
157 | * @li ESYNCH_OK_ATOMIC means that the sleep succeeded and that there was |
211 | * a pending wakeup at the time of the call. The caller was not put |
158 | * a pending wakeup at the time of the call. The caller was not put |
212 | * asleep at all. |
159 | * asleep at all. |
213 | * |
160 | * |
214 | * @li ESYNCH_OK_BLOCKED means that the sleep succeeded; the full sleep was |
161 | * @li ESYNCH_OK_BLOCKED means that the sleep succeeded; the full sleep was |
215 | * attempted. |
162 | * attempted. |
216 | */ |
163 | */ |
217 | int waitq_sleep_timeout(waitq_t *wq, uint32_t usec, int flags) |
164 | int waitq_sleep_timeout(waitq_t *wq, uint32_t usec, int flags) |
218 | { |
165 | { |
219 | ipl_t ipl; |
166 | ipl_t ipl; |
220 | int rc; |
167 | int rc; |
221 | 168 | ||
222 | ipl = waitq_sleep_prepare(wq); |
169 | ipl = waitq_sleep_prepare(wq); |
223 | rc = waitq_sleep_timeout_unsafe(wq, usec, flags); |
170 | rc = waitq_sleep_timeout_unsafe(wq, usec, flags); |
224 | waitq_sleep_finish(wq, rc, ipl); |
171 | waitq_sleep_finish(wq, rc, ipl); |
225 | return rc; |
172 | return rc; |
226 | } |
173 | } |
227 | 174 | ||
228 | /** Prepare to sleep in a waitq. |
175 | /** Prepare to sleep in a waitq. |
229 | * |
176 | * |
230 | * This function will return holding the lock of the wait queue |
177 | * This function will return holding the lock of the wait queue |
231 | * and interrupts disabled. |
178 | * and interrupts disabled. |
232 | * |
179 | * |
233 | * @param wq Wait queue. |
180 | * @param wq Wait queue. |
234 | * |
181 | * |
235 | * @return Interrupt level as it existed on entry to this function. |
182 | * @return Interrupt level as it existed on entry to this function. |
236 | */ |
183 | */ |
237 | ipl_t waitq_sleep_prepare(waitq_t *wq) |
184 | ipl_t waitq_sleep_prepare(waitq_t *wq) |
238 | { |
185 | { |
239 | ipl_t ipl; |
186 | ipl_t ipl; |
240 | 187 | ||
241 | restart: |
188 | restart: |
242 | ipl = interrupts_disable(); |
189 | ipl = interrupts_disable(); |
243 | 190 | ||
244 | if (THREAD) { /* needed during system initiailzation */ |
191 | if (THREAD) { /* needed during system initiailzation */ |
245 | /* |
192 | /* |
246 | * Busy waiting for a delayed timeout. |
193 | * Busy waiting for a delayed timeout. |
247 | * This is an important fix for the race condition between |
194 | * This is an important fix for the race condition between |
248 | * a delayed timeout and a next call to waitq_sleep_timeout(). |
195 | * a delayed timeout and a next call to waitq_sleep_timeout(). |
249 | * Simply, the thread is not allowed to go to sleep if |
196 | * Simply, the thread is not allowed to go to sleep if |
250 | * there are timeouts in progress. |
197 | * there are timeouts in progress. |
251 | */ |
198 | */ |
252 | spinlock_lock(&THREAD->lock); |
199 | spinlock_lock(&THREAD->lock); |
253 | if (THREAD->timeout_pending) { |
200 | if (THREAD->timeout_pending) { |
254 | spinlock_unlock(&THREAD->lock); |
201 | spinlock_unlock(&THREAD->lock); |
255 | interrupts_restore(ipl); |
202 | interrupts_restore(ipl); |
256 | goto restart; |
203 | goto restart; |
257 | } |
204 | } |
258 | spinlock_unlock(&THREAD->lock); |
205 | spinlock_unlock(&THREAD->lock); |
259 | } |
206 | } |
260 | 207 | ||
261 | spinlock_lock(&wq->lock); |
208 | spinlock_lock(&wq->lock); |
262 | return ipl; |
209 | return ipl; |
263 | } |
210 | } |
264 | 211 | ||
265 | /** Finish waiting in a wait queue. |
212 | /** Finish waiting in a wait queue. |
266 | * |
213 | * |
267 | * This function restores interrupts to the state that existed prior |
214 | * This function restores interrupts to the state that existed prior |
268 | * to the call to waitq_sleep_prepare(). If necessary, the wait queue |
215 | * to the call to waitq_sleep_prepare(). If necessary, the wait queue |
269 | * lock is released. |
216 | * lock is released. |
270 | * |
217 | * |
271 | * @param wq Wait queue. |
218 | * @param wq Wait queue. |
272 | * @param rc Return code of waitq_sleep_timeout_unsafe(). |
219 | * @param rc Return code of waitq_sleep_timeout_unsafe(). |
273 | * @param ipl Interrupt level returned by waitq_sleep_prepare(). |
220 | * @param ipl Interrupt level returned by waitq_sleep_prepare(). |
274 | */ |
221 | */ |
275 | void waitq_sleep_finish(waitq_t *wq, int rc, ipl_t ipl) |
222 | void waitq_sleep_finish(waitq_t *wq, int rc, ipl_t ipl) |
276 | { |
223 | { |
277 | switch (rc) { |
224 | switch (rc) { |
278 | case ESYNCH_WOULD_BLOCK: |
225 | case ESYNCH_WOULD_BLOCK: |
279 | case ESYNCH_OK_ATOMIC: |
226 | case ESYNCH_OK_ATOMIC: |
280 | spinlock_unlock(&wq->lock); |
227 | spinlock_unlock(&wq->lock); |
281 | break; |
228 | break; |
282 | default: |
229 | default: |
283 | break; |
230 | break; |
284 | } |
231 | } |
285 | interrupts_restore(ipl); |
232 | interrupts_restore(ipl); |
286 | } |
233 | } |
287 | 234 | ||
288 | /** Internal implementation of waitq_sleep_timeout(). |
235 | /** Internal implementation of waitq_sleep_timeout(). |
289 | * |
236 | * |
290 | * This function implements logic of sleeping in a wait queue. |
237 | * This function implements logic of sleeping in a wait queue. |
291 | * This call must be preceeded by a call to waitq_sleep_prepare() |
238 | * This call must be preceeded by a call to waitq_sleep_prepare() |
292 | * and followed by a call to waitq_slee_finish(). |
239 | * and followed by a call to waitq_slee_finish(). |
293 | * |
240 | * |
294 | * @param wq See waitq_sleep_timeout(). |
241 | * @param wq See waitq_sleep_timeout(). |
295 | * @param usec See waitq_sleep_timeout(). |
242 | * @param usec See waitq_sleep_timeout(). |
296 | * @param flags See waitq_sleep_timeout(). |
243 | * @param flags See waitq_sleep_timeout(). |
297 | * |
244 | * |
298 | * @return See waitq_sleep_timeout(). |
245 | * @return See waitq_sleep_timeout(). |
299 | */ |
246 | */ |
300 | int waitq_sleep_timeout_unsafe(waitq_t *wq, uint32_t usec, int flags) |
247 | int waitq_sleep_timeout_unsafe(waitq_t *wq, uint32_t usec, int flags) |
301 | { |
248 | { |
302 | /* checks whether to go to sleep at all */ |
249 | /* checks whether to go to sleep at all */ |
303 | if (wq->missed_wakeups) { |
250 | if (wq->missed_wakeups) { |
304 | wq->missed_wakeups--; |
251 | wq->missed_wakeups--; |
305 | return ESYNCH_OK_ATOMIC; |
252 | return ESYNCH_OK_ATOMIC; |
306 | } |
253 | } |
307 | else { |
254 | else { |
308 | if ((flags & SYNCH_FLAGS_NON_BLOCKING) && (usec == 0)) { |
255 | if ((flags & SYNCH_FLAGS_NON_BLOCKING) && (usec == 0)) { |
309 | /* return immediatelly instead of going to sleep */ |
256 | /* return immediatelly instead of going to sleep */ |
310 | return ESYNCH_WOULD_BLOCK; |
257 | return ESYNCH_WOULD_BLOCK; |
311 | } |
258 | } |
312 | } |
259 | } |
313 | 260 | ||
314 | /* |
261 | /* |
315 | * Now we are firmly decided to go to sleep. |
262 | * Now we are firmly decided to go to sleep. |
316 | */ |
263 | */ |
317 | spinlock_lock(&THREAD->lock); |
264 | spinlock_lock(&THREAD->lock); |
318 | 265 | ||
319 | if (flags & SYNCH_FLAGS_INTERRUPTIBLE) { |
266 | if (flags & SYNCH_FLAGS_INTERRUPTIBLE) { |
320 | 267 | ||
321 | /* |
268 | /* |
322 | * If the thread was already interrupted, |
269 | * If the thread was already interrupted, |
323 | * don't go to sleep at all. |
270 | * don't go to sleep at all. |
324 | */ |
271 | */ |
325 | if (THREAD->interrupted) { |
272 | if (THREAD->interrupted) { |
326 | spinlock_unlock(&THREAD->lock); |
273 | spinlock_unlock(&THREAD->lock); |
327 | spinlock_unlock(&wq->lock); |
274 | spinlock_unlock(&wq->lock); |
328 | return ESYNCH_INTERRUPTED; |
275 | return ESYNCH_INTERRUPTED; |
329 | } |
276 | } |
330 | 277 | ||
331 | /* |
278 | /* |
332 | * Set context that will be restored if the sleep |
279 | * Set context that will be restored if the sleep |
333 | * of this thread is ever interrupted. |
280 | * of this thread is ever interrupted. |
334 | */ |
281 | */ |
335 | THREAD->sleep_interruptible = true; |
282 | THREAD->sleep_interruptible = true; |
336 | if (!context_save(&THREAD->sleep_interruption_context)) { |
283 | if (!context_save(&THREAD->sleep_interruption_context)) { |
337 | /* Short emulation of scheduler() return code. */ |
284 | /* Short emulation of scheduler() return code. */ |
338 | spinlock_unlock(&THREAD->lock); |
285 | spinlock_unlock(&THREAD->lock); |
339 | return ESYNCH_INTERRUPTED; |
286 | return ESYNCH_INTERRUPTED; |
340 | } |
287 | } |
341 | 288 | ||
342 | } else { |
289 | } else { |
343 | THREAD->sleep_interruptible = false; |
290 | THREAD->sleep_interruptible = false; |
344 | } |
291 | } |
345 | 292 | ||
346 | if (usec) { |
293 | if (usec) { |
347 | /* We use the timeout variant. */ |
294 | /* We use the timeout variant. */ |
348 | if (!context_save(&THREAD->sleep_timeout_context)) { |
295 | if (!context_save(&THREAD->sleep_timeout_context)) { |
349 | /* Short emulation of scheduler() return code. */ |
296 | /* Short emulation of scheduler() return code. */ |
350 | spinlock_unlock(&THREAD->lock); |
297 | spinlock_unlock(&THREAD->lock); |
351 | return ESYNCH_TIMEOUT; |
298 | return ESYNCH_TIMEOUT; |
352 | } |
299 | } |
353 | THREAD->timeout_pending = true; |
300 | THREAD->timeout_pending = true; |
354 | timeout_register(&THREAD->sleep_timeout, (uint64_t) usec, |
301 | timeout_register(&THREAD->sleep_timeout, (uint64_t) usec, |
355 | waitq_timeouted_sleep, THREAD); |
302 | waitq_timeouted_sleep, THREAD); |
356 | } |
303 | } |
357 | 304 | ||
358 | list_append(&THREAD->wq_link, &wq->head); |
305 | list_append(&THREAD->wq_link, &wq->head); |
359 | 306 | ||
360 | /* |
307 | /* |
361 | * Suspend execution. |
308 | * Suspend execution. |
362 | */ |
309 | */ |
363 | THREAD->state = Sleeping; |
310 | THREAD->state = Sleeping; |
364 | THREAD->sleep_queue = wq; |
311 | THREAD->sleep_queue = wq; |
365 | 312 | ||
366 | spinlock_unlock(&THREAD->lock); |
313 | spinlock_unlock(&THREAD->lock); |
367 | 314 | ||
368 | /* wq->lock is released in scheduler_separated_stack() */ |
315 | /* wq->lock is released in scheduler_separated_stack() */ |
369 | scheduler(); |
316 | scheduler(); |
370 | 317 | ||
371 | return ESYNCH_OK_BLOCKED; |
318 | return ESYNCH_OK_BLOCKED; |
372 | } |
319 | } |
373 | 320 | ||
374 | 321 | ||
375 | /** Wake up first thread sleeping in a wait queue |
322 | /** Wake up first thread sleeping in a wait queue |
376 | * |
323 | * |
377 | * Wake up first thread sleeping in a wait queue. This is the SMP- and IRQ-safe |
324 | * Wake up first thread sleeping in a wait queue. This is the SMP- and IRQ-safe |
378 | * wrapper meant for general use. |
325 | * wrapper meant for general use. |
379 | * |
326 | * |
380 | * Besides its 'normal' wakeup operation, it attempts to unregister possible |
327 | * Besides its 'normal' wakeup operation, it attempts to unregister possible |
381 | * timeout. |
328 | * timeout. |
382 | * |
329 | * |
383 | * @param wq Pointer to wait queue. |
330 | * @param wq Pointer to wait queue. |
384 | * @param all If this is non-zero, all sleeping threads will be woken up and |
331 | * @param all If this is non-zero, all sleeping threads will be woken up and |
385 | * missed count will be zeroed. |
332 | * missed count will be zeroed. |
386 | */ |
333 | */ |
387 | void waitq_wakeup(waitq_t *wq, bool all) |
334 | void waitq_wakeup(waitq_t *wq, bool all) |
388 | { |
335 | { |
389 | ipl_t ipl; |
336 | ipl_t ipl; |
390 | 337 | ||
391 | ipl = interrupts_disable(); |
338 | ipl = interrupts_disable(); |
392 | spinlock_lock(&wq->lock); |
339 | spinlock_lock(&wq->lock); |
393 | 340 | ||
394 | _waitq_wakeup_unsafe(wq, all); |
341 | _waitq_wakeup_unsafe(wq, all); |
395 | 342 | ||
396 | spinlock_unlock(&wq->lock); |
343 | spinlock_unlock(&wq->lock); |
397 | interrupts_restore(ipl); |
344 | interrupts_restore(ipl); |
398 | } |
345 | } |
399 | 346 | ||
400 | /** Internal SMP- and IRQ-unsafe version of waitq_wakeup() |
347 | /** Internal SMP- and IRQ-unsafe version of waitq_wakeup() |
401 | * |
348 | * |
402 | * This is the internal SMP- and IRQ-unsafe version of waitq_wakeup(). It |
349 | * This is the internal SMP- and IRQ-unsafe version of waitq_wakeup(). It |
403 | * assumes wq->lock is already locked and interrupts are already disabled. |
350 | * assumes wq->lock is already locked and interrupts are already disabled. |
404 | * |
351 | * |
405 | * @param wq Pointer to wait queue. |
352 | * @param wq Pointer to wait queue. |
406 | * @param all If this is non-zero, all sleeping threads will be woken up and |
353 | * @param all If this is non-zero, all sleeping threads will be woken up and |
407 | * missed count will be zeroed. |
354 | * missed count will be zeroed. |
408 | */ |
355 | */ |
409 | void _waitq_wakeup_unsafe(waitq_t *wq, bool all) |
356 | void _waitq_wakeup_unsafe(waitq_t *wq, bool all) |
410 | { |
357 | { |
411 | thread_t *t; |
358 | thread_t *t; |
412 | 359 | ||
413 | loop: |
360 | loop: |
414 | if (list_empty(&wq->head)) { |
361 | if (list_empty(&wq->head)) { |
415 | wq->missed_wakeups++; |
362 | wq->missed_wakeups++; |
416 | if (all) |
363 | if (all) |
417 | wq->missed_wakeups = 0; |
364 | wq->missed_wakeups = 0; |
418 | return; |
365 | return; |
419 | } |
366 | } |
420 | 367 | ||
421 | t = list_get_instance(wq->head.next, thread_t, wq_link); |
368 | t = list_get_instance(wq->head.next, thread_t, wq_link); |
422 | 369 | ||
423 | /* |
370 | /* |
424 | * Lock the thread prior to removing it from the wq. |
371 | * Lock the thread prior to removing it from the wq. |
425 | * This is not necessary because of mutual exclusion |
372 | * This is not necessary because of mutual exclusion |
426 | * (the link belongs to the wait queue), but because |
373 | * (the link belongs to the wait queue), but because |
427 | * of synchronization with waitq_timeouted_sleep() |
374 | * of synchronization with waitq_timeouted_sleep() |
428 | * and waitq_interrupt_sleep(). |
375 | * and thread_interrupt_sleep(). |
429 | * |
376 | * |
430 | * In order for these two functions to work, the following |
377 | * In order for these two functions to work, the following |
431 | * invariant must hold: |
378 | * invariant must hold: |
432 | * |
379 | * |
433 | * t->sleep_queue != NULL <=> t sleeps in a wait queue |
380 | * t->sleep_queue != NULL <=> t sleeps in a wait queue |
434 | * |
381 | * |
435 | * For an observer who locks the thread, the invariant |
382 | * For an observer who locks the thread, the invariant |
436 | * holds only when the lock is held prior to removing |
383 | * holds only when the lock is held prior to removing |
437 | * it from the wait queue. |
384 | * it from the wait queue. |
438 | */ |
385 | */ |
439 | spinlock_lock(&t->lock); |
386 | spinlock_lock(&t->lock); |
440 | list_remove(&t->wq_link); |
387 | list_remove(&t->wq_link); |
441 | 388 | ||
442 | if (t->timeout_pending && timeout_unregister(&t->sleep_timeout)) |
389 | if (t->timeout_pending && timeout_unregister(&t->sleep_timeout)) |
443 | t->timeout_pending = false; |
390 | t->timeout_pending = false; |
444 | t->sleep_queue = NULL; |
391 | t->sleep_queue = NULL; |
445 | spinlock_unlock(&t->lock); |
392 | spinlock_unlock(&t->lock); |
446 | 393 | ||
447 | thread_ready(t); |
394 | thread_ready(t); |
448 | 395 | ||
449 | if (all) |
396 | if (all) |
450 | goto loop; |
397 | goto loop; |
451 | } |
398 | } |
452 | 399 | ||
453 | /** @} |
400 | /** @} |
454 | */ |
401 | */ |
455 | 402 |