Rev 2087 | Rev 2109 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | jermar | 1 | /* |
2071 | jermar | 2 | * Copyright (c) 2001-2004 Jakub Jermar |
1 | jermar | 3 | * All rights reserved. |
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * - The name of the author may not be used to endorse or promote products |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
1757 | jermar | 29 | /** @addtogroup sync |
1702 | cejka | 30 | * @{ |
31 | */ |
||
32 | |||
1248 | jermar | 33 | /** |
1702 | cejka | 34 | * @file |
1248 | jermar | 35 | * @brief Wait queue. |
36 | * |
||
1288 | jermar | 37 | * Wait queue is the basic synchronization primitive upon which all |
1248 | jermar | 38 | * other synchronization primitives build. |
39 | * |
||
40 | * It allows threads to wait for an event in first-come, first-served |
||
41 | * fashion. Conditional operation as well as timeouts and interruptions |
||
42 | * are supported. |
||
43 | */ |
||
44 | |||
382 | jermar | 45 | #include <synch/waitq.h> |
1 | jermar | 46 | #include <synch/synch.h> |
47 | #include <synch/spinlock.h> |
||
382 | jermar | 48 | #include <proc/thread.h> |
414 | jermar | 49 | #include <proc/scheduler.h> |
1 | jermar | 50 | #include <arch/asm.h> |
51 | #include <arch/types.h> |
||
382 | jermar | 52 | #include <time/timeout.h> |
1 | jermar | 53 | #include <arch.h> |
382 | jermar | 54 | #include <context.h> |
788 | jermar | 55 | #include <adt/list.h> |
1 | jermar | 56 | |
1156 | jermar | 57 | static void waitq_timeouted_sleep(void *data); |
58 | |||
382 | jermar | 59 | /** Initialize wait queue |
60 | * |
||
61 | * Initialize wait queue. |
||
62 | * |
||
63 | * @param wq Pointer to wait queue to be initialized. |
||
64 | */ |
||
1 | jermar | 65 | void waitq_initialize(waitq_t *wq) |
66 | { |
||
552 | palkovsky | 67 | spinlock_initialize(&wq->lock, "waitq_lock"); |
1 | jermar | 68 | list_initialize(&wq->head); |
69 | wq->missed_wakeups = 0; |
||
70 | } |
||
71 | |||
382 | jermar | 72 | /** Handle timeout during waitq_sleep_timeout() call |
1 | jermar | 73 | * |
382 | jermar | 74 | * This routine is called when waitq_sleep_timeout() timeouts. |
75 | * Interrupts are disabled. |
||
76 | * |
||
77 | * It is supposed to try to remove 'its' thread from the wait queue; |
||
78 | * it can eventually fail to achieve this goal when these two events |
||
79 | * overlap. In that case it behaves just as though there was no |
||
80 | * timeout at all. |
||
81 | * |
||
82 | * @param data Pointer to the thread that called waitq_sleep_timeout(). |
||
1 | jermar | 83 | */ |
1156 | jermar | 84 | void waitq_timeouted_sleep(void *data) |
1 | jermar | 85 | { |
86 | thread_t *t = (thread_t *) data; |
||
87 | waitq_t *wq; |
||
557 | jermar | 88 | bool do_wakeup = false; |
1 | jermar | 89 | |
90 | spinlock_lock(&threads_lock); |
||
1158 | jermar | 91 | if (!thread_exists(t)) |
1 | jermar | 92 | goto out; |
93 | |||
94 | grab_locks: |
||
95 | spinlock_lock(&t->lock); |
||
615 | palkovsky | 96 | if ((wq = t->sleep_queue)) { /* assignment */ |
1 | jermar | 97 | if (!spinlock_trylock(&wq->lock)) { |
98 | spinlock_unlock(&t->lock); |
||
557 | jermar | 99 | goto grab_locks; /* avoid deadlock */ |
1 | jermar | 100 | } |
101 | |||
102 | list_remove(&t->wq_link); |
||
103 | t->saved_context = t->sleep_timeout_context; |
||
557 | jermar | 104 | do_wakeup = true; |
1681 | jermar | 105 | t->sleep_queue = NULL; |
1 | jermar | 106 | spinlock_unlock(&wq->lock); |
107 | } |
||
108 | |||
557 | jermar | 109 | t->timeout_pending = false; |
1 | jermar | 110 | spinlock_unlock(&t->lock); |
111 | |||
557 | jermar | 112 | if (do_wakeup) |
113 | thread_ready(t); |
||
1 | jermar | 114 | |
115 | out: |
||
116 | spinlock_unlock(&threads_lock); |
||
117 | } |
||
118 | |||
1156 | jermar | 119 | |
120 | /** Sleep until either wakeup, timeout or interruption occurs |
||
121 | * |
||
1502 | jermar | 122 | * This is a sleep implementation which allows itself to time out or to be |
1 | jermar | 123 | * interrupted from the sleep, restoring a failover context. |
124 | * |
||
1375 | jermar | 125 | * Sleepers are organised in a FIFO fashion in a structure called wait queue. |
382 | jermar | 126 | * |
1 | jermar | 127 | * This function is really basic in that other functions as waitq_sleep() |
128 | * and all the *_timeout() functions use it. |
||
129 | * |
||
382 | jermar | 130 | * @param wq Pointer to wait queue. |
404 | jermar | 131 | * @param usec Timeout in microseconds. |
1502 | jermar | 132 | * @param flags Specify mode of the sleep. |
1 | jermar | 133 | * |
1502 | jermar | 134 | * The sleep can be interrupted only if the |
135 | * SYNCH_FLAGS_INTERRUPTIBLE bit is specified in flags. |
||
2087 | jermar | 136 | * |
1502 | jermar | 137 | * If usec is greater than zero, regardless of the value of the |
2067 | jermar | 138 | * SYNCH_FLAGS_NON_BLOCKING bit in flags, the call will not return until either |
139 | * timeout, interruption or wakeup comes. |
||
382 | jermar | 140 | * |
2067 | jermar | 141 | * If usec is zero and the SYNCH_FLAGS_NON_BLOCKING bit is not set in flags, |
142 | * the call will not return until wakeup or interruption comes. |
||
1 | jermar | 143 | * |
2067 | jermar | 144 | * If usec is zero and the SYNCH_FLAGS_NON_BLOCKING bit is set in flags, the |
145 | * call will immediately return, reporting either success or failure. |
||
404 | jermar | 146 | * |
2067 | jermar | 147 | * @return One of: ESYNCH_WOULD_BLOCK, ESYNCH_TIMEOUT, ESYNCH_INTERRUPTED, |
148 | * ESYNCH_OK_ATOMIC, ESYNCH_OK_BLOCKED. |
||
382 | jermar | 149 | * |
2067 | jermar | 150 | * @li ESYNCH_WOULD_BLOCK means that the sleep failed because at the time of the |
151 | * call there was no pending wakeup. |
||
382 | jermar | 152 | * |
1248 | jermar | 153 | * @li ESYNCH_TIMEOUT means that the sleep timed out. |
404 | jermar | 154 | * |
1248 | jermar | 155 | * @li ESYNCH_INTERRUPTED means that somebody interrupted the sleeping thread. |
1156 | jermar | 156 | * |
1248 | jermar | 157 | * @li ESYNCH_OK_ATOMIC means that the sleep succeeded and that there was |
404 | jermar | 158 | * a pending wakeup at the time of the call. The caller was not put |
159 | * asleep at all. |
||
160 | * |
||
1248 | jermar | 161 | * @li ESYNCH_OK_BLOCKED means that the sleep succeeded; the full sleep was |
404 | jermar | 162 | * attempted. |
1 | jermar | 163 | */ |
1780 | jermar | 164 | int waitq_sleep_timeout(waitq_t *wq, uint32_t usec, int flags) |
1 | jermar | 165 | { |
1375 | jermar | 166 | ipl_t ipl; |
167 | int rc; |
||
1 | jermar | 168 | |
1375 | jermar | 169 | ipl = waitq_sleep_prepare(wq); |
1502 | jermar | 170 | rc = waitq_sleep_timeout_unsafe(wq, usec, flags); |
1375 | jermar | 171 | waitq_sleep_finish(wq, rc, ipl); |
172 | return rc; |
||
173 | } |
||
174 | |||
175 | /** Prepare to sleep in a waitq. |
||
176 | * |
||
177 | * This function will return holding the lock of the wait queue |
||
178 | * and interrupts disabled. |
||
179 | * |
||
180 | * @param wq Wait queue. |
||
181 | * |
||
182 | * @return Interrupt level as it existed on entry to this function. |
||
183 | */ |
||
184 | ipl_t waitq_sleep_prepare(waitq_t *wq) |
||
185 | { |
||
186 | ipl_t ipl; |
||
1 | jermar | 187 | |
188 | restart: |
||
413 | jermar | 189 | ipl = interrupts_disable(); |
1375 | jermar | 190 | |
1467 | jermar | 191 | if (THREAD) { /* needed during system initiailzation */ |
192 | /* |
||
193 | * Busy waiting for a delayed timeout. |
||
194 | * This is an important fix for the race condition between |
||
195 | * a delayed timeout and a next call to waitq_sleep_timeout(). |
||
196 | * Simply, the thread is not allowed to go to sleep if |
||
197 | * there are timeouts in progress. |
||
198 | */ |
||
199 | spinlock_lock(&THREAD->lock); |
||
200 | if (THREAD->timeout_pending) { |
||
201 | spinlock_unlock(&THREAD->lock); |
||
202 | interrupts_restore(ipl); |
||
203 | goto restart; |
||
204 | } |
||
15 | jermar | 205 | spinlock_unlock(&THREAD->lock); |
1 | jermar | 206 | } |
1375 | jermar | 207 | |
1 | jermar | 208 | spinlock_lock(&wq->lock); |
1375 | jermar | 209 | return ipl; |
210 | } |
||
211 | |||
212 | /** Finish waiting in a wait queue. |
||
213 | * |
||
214 | * This function restores interrupts to the state that existed prior |
||
215 | * to the call to waitq_sleep_prepare(). If necessary, the wait queue |
||
216 | * lock is released. |
||
217 | * |
||
218 | * @param wq Wait queue. |
||
219 | * @param rc Return code of waitq_sleep_timeout_unsafe(). |
||
220 | * @param ipl Interrupt level returned by waitq_sleep_prepare(). |
||
221 | */ |
||
222 | void waitq_sleep_finish(waitq_t *wq, int rc, ipl_t ipl) |
||
223 | { |
||
224 | switch (rc) { |
||
225 | case ESYNCH_WOULD_BLOCK: |
||
226 | case ESYNCH_OK_ATOMIC: |
||
227 | spinlock_unlock(&wq->lock); |
||
228 | break; |
||
229 | default: |
||
230 | break; |
||
231 | } |
||
232 | interrupts_restore(ipl); |
||
233 | } |
||
234 | |||
235 | /** Internal implementation of waitq_sleep_timeout(). |
||
236 | * |
||
237 | * This function implements logic of sleeping in a wait queue. |
||
238 | * This call must be preceeded by a call to waitq_sleep_prepare() |
||
239 | * and followed by a call to waitq_slee_finish(). |
||
240 | * |
||
241 | * @param wq See waitq_sleep_timeout(). |
||
242 | * @param usec See waitq_sleep_timeout(). |
||
1502 | jermar | 243 | * @param flags See waitq_sleep_timeout(). |
1375 | jermar | 244 | * |
245 | * @return See waitq_sleep_timeout(). |
||
246 | */ |
||
1780 | jermar | 247 | int waitq_sleep_timeout_unsafe(waitq_t *wq, uint32_t usec, int flags) |
1375 | jermar | 248 | { |
1 | jermar | 249 | /* checks whether to go to sleep at all */ |
250 | if (wq->missed_wakeups) { |
||
251 | wq->missed_wakeups--; |
||
252 | return ESYNCH_OK_ATOMIC; |
||
253 | } |
||
254 | else { |
||
1502 | jermar | 255 | if ((flags & SYNCH_FLAGS_NON_BLOCKING) && (usec == 0)) { |
1 | jermar | 256 | /* return immediatelly instead of going to sleep */ |
257 | return ESYNCH_WOULD_BLOCK; |
||
258 | } |
||
259 | } |
||
260 | |||
261 | /* |
||
262 | * Now we are firmly decided to go to sleep. |
||
263 | */ |
||
15 | jermar | 264 | spinlock_lock(&THREAD->lock); |
1156 | jermar | 265 | |
1580 | jermar | 266 | if (flags & SYNCH_FLAGS_INTERRUPTIBLE) { |
1579 | jermar | 267 | |
1502 | jermar | 268 | /* |
1580 | jermar | 269 | * If the thread was already interrupted, |
270 | * don't go to sleep at all. |
||
271 | */ |
||
272 | if (THREAD->interrupted) { |
||
273 | spinlock_unlock(&THREAD->lock); |
||
274 | spinlock_unlock(&wq->lock); |
||
275 | return ESYNCH_INTERRUPTED; |
||
276 | } |
||
277 | |||
278 | /* |
||
1502 | jermar | 279 | * Set context that will be restored if the sleep |
280 | * of this thread is ever interrupted. |
||
281 | */ |
||
282 | THREAD->sleep_interruptible = true; |
||
283 | if (!context_save(&THREAD->sleep_interruption_context)) { |
||
284 | /* Short emulation of scheduler() return code. */ |
||
285 | spinlock_unlock(&THREAD->lock); |
||
286 | return ESYNCH_INTERRUPTED; |
||
287 | } |
||
1580 | jermar | 288 | |
1502 | jermar | 289 | } else { |
290 | THREAD->sleep_interruptible = false; |
||
1156 | jermar | 291 | } |
292 | |||
1 | jermar | 293 | if (usec) { |
294 | /* We use the timeout variant. */ |
||
15 | jermar | 295 | if (!context_save(&THREAD->sleep_timeout_context)) { |
1156 | jermar | 296 | /* Short emulation of scheduler() return code. */ |
15 | jermar | 297 | spinlock_unlock(&THREAD->lock); |
1 | jermar | 298 | return ESYNCH_TIMEOUT; |
299 | } |
||
557 | jermar | 300 | THREAD->timeout_pending = true; |
2067 | jermar | 301 | timeout_register(&THREAD->sleep_timeout, (uint64_t) usec, |
2087 | jermar | 302 | waitq_timeouted_sleep, THREAD); |
1 | jermar | 303 | } |
304 | |||
15 | jermar | 305 | list_append(&THREAD->wq_link, &wq->head); |
1 | jermar | 306 | |
307 | /* |
||
308 | * Suspend execution. |
||
309 | */ |
||
15 | jermar | 310 | THREAD->state = Sleeping; |
311 | THREAD->sleep_queue = wq; |
||
1 | jermar | 312 | |
15 | jermar | 313 | spinlock_unlock(&THREAD->lock); |
1 | jermar | 314 | |
2067 | jermar | 315 | /* wq->lock is released in scheduler_separated_stack() */ |
316 | scheduler(); |
||
1 | jermar | 317 | |
318 | return ESYNCH_OK_BLOCKED; |
||
319 | } |
||
320 | |||
321 | |||
382 | jermar | 322 | /** Wake up first thread sleeping in a wait queue |
323 | * |
||
2067 | jermar | 324 | * Wake up first thread sleeping in a wait queue. This is the SMP- and IRQ-safe |
325 | * wrapper meant for general use. |
||
382 | jermar | 326 | * |
2067 | jermar | 327 | * Besides its 'normal' wakeup operation, it attempts to unregister possible |
328 | * timeout. |
||
382 | jermar | 329 | * |
330 | * @param wq Pointer to wait queue. |
||
2067 | jermar | 331 | * @param all If this is non-zero, all sleeping threads will be woken up and |
332 | * missed count will be zeroed. |
||
1 | jermar | 333 | */ |
557 | jermar | 334 | void waitq_wakeup(waitq_t *wq, bool all) |
1 | jermar | 335 | { |
413 | jermar | 336 | ipl_t ipl; |
1 | jermar | 337 | |
413 | jermar | 338 | ipl = interrupts_disable(); |
1 | jermar | 339 | spinlock_lock(&wq->lock); |
340 | |||
341 | _waitq_wakeup_unsafe(wq, all); |
||
342 | |||
343 | spinlock_unlock(&wq->lock); |
||
413 | jermar | 344 | interrupts_restore(ipl); |
1 | jermar | 345 | } |
346 | |||
382 | jermar | 347 | /** Internal SMP- and IRQ-unsafe version of waitq_wakeup() |
348 | * |
||
2067 | jermar | 349 | * This is the internal SMP- and IRQ-unsafe version of waitq_wakeup(). It |
350 | * assumes wq->lock is already locked and interrupts are already disabled. |
||
382 | jermar | 351 | * |
352 | * @param wq Pointer to wait queue. |
||
2067 | jermar | 353 | * @param all If this is non-zero, all sleeping threads will be woken up and |
354 | * missed count will be zeroed. |
||
1 | jermar | 355 | */ |
557 | jermar | 356 | void _waitq_wakeup_unsafe(waitq_t *wq, bool all) |
1 | jermar | 357 | { |
358 | thread_t *t; |
||
359 | |||
360 | loop: |
||
361 | if (list_empty(&wq->head)) { |
||
362 | wq->missed_wakeups++; |
||
557 | jermar | 363 | if (all) |
364 | wq->missed_wakeups = 0; |
||
1 | jermar | 365 | return; |
366 | } |
||
367 | |||
368 | t = list_get_instance(wq->head.next, thread_t, wq_link); |
||
369 | |||
1681 | jermar | 370 | /* |
371 | * Lock the thread prior to removing it from the wq. |
||
372 | * This is not necessary because of mutual exclusion |
||
373 | * (the link belongs to the wait queue), but because |
||
374 | * of synchronization with waitq_timeouted_sleep() |
||
2089 | decky | 375 | * and thread_interrupt_sleep(). |
1681 | jermar | 376 | * |
377 | * In order for these two functions to work, the following |
||
378 | * invariant must hold: |
||
379 | * |
||
380 | * t->sleep_queue != NULL <=> t sleeps in a wait queue |
||
381 | * |
||
382 | * For an observer who locks the thread, the invariant |
||
383 | * holds only when the lock is held prior to removing |
||
384 | * it from the wait queue. |
||
385 | */ |
||
386 | spinlock_lock(&t->lock); |
||
1 | jermar | 387 | list_remove(&t->wq_link); |
1681 | jermar | 388 | |
1 | jermar | 389 | if (t->timeout_pending && timeout_unregister(&t->sleep_timeout)) |
557 | jermar | 390 | t->timeout_pending = false; |
1 | jermar | 391 | t->sleep_queue = NULL; |
392 | spinlock_unlock(&t->lock); |
||
393 | |||
394 | thread_ready(t); |
||
395 | |||
557 | jermar | 396 | if (all) |
397 | goto loop; |
||
1 | jermar | 398 | } |
1702 | cejka | 399 | |
1757 | jermar | 400 | /** @} |
1702 | cejka | 401 | */ |