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