source: mainline/kernel/generic/src/synch/waitq.c@ 0cfc18d3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0cfc18d3 was 9fe9d296, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

Fix: waitq_sleep_timeout() waits for its waitq_wakeup() to complete unconditionally. No need to wait explicitly via waitq_complete_wakeup() anymore.

  • Property mode set to 100644
File size: 16.5 KB
Line 
1/*
2 * Copyright (c) 2001-2004 Jakub 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
29/** @addtogroup sync
30 * @{
31 */
32
33/**
34 * @file
35 * @brief Wait queue.
36 *
37 * Wait queue is the basic synchronization primitive upon which all
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 */
45
46#include <synch/waitq.h>
47#include <synch/spinlock.h>
48#include <proc/thread.h>
49#include <proc/scheduler.h>
50#include <arch/asm.h>
51#include <typedefs.h>
52#include <time/timeout.h>
53#include <arch.h>
54#include <context.h>
55#include <adt/list.h>
56#include <arch/cycle.h>
57
58static void waitq_sleep_timed_out(void *);
59static void waitq_complete_wakeup(waitq_t *);
60
61
62/** Initialize wait queue
63 *
64 * Initialize wait queue.
65 *
66 * @param wq Pointer to wait queue to be initialized.
67 *
68 */
69void waitq_initialize(waitq_t *wq)
70{
71 irq_spinlock_initialize(&wq->lock, "wq.lock");
72 list_initialize(&wq->sleepers);
73 wq->missed_wakeups = 0;
74}
75
76/** Handle timeout during waitq_sleep_timeout() call
77 *
78 * This routine is called when waitq_sleep_timeout() times out.
79 * Interrupts are disabled.
80 *
81 * It is supposed to try to remove 'its' thread from the wait queue;
82 * it can eventually fail to achieve this goal when these two events
83 * overlap. In that case it behaves just as though there was no
84 * timeout at all.
85 *
86 * @param data Pointer to the thread that called waitq_sleep_timeout().
87 *
88 */
89void waitq_sleep_timed_out(void *data)
90{
91 thread_t *thread = (thread_t *) data;
92 bool do_wakeup = false;
93 DEADLOCK_PROBE_INIT(p_wqlock);
94
95 irq_spinlock_lock(&threads_lock, false);
96 if (!thread_exists(thread))
97 goto out;
98
99grab_locks:
100 irq_spinlock_lock(&thread->lock, false);
101
102 waitq_t *wq;
103 if ((wq = thread->sleep_queue)) { /* Assignment */
104 if (!irq_spinlock_trylock(&wq->lock)) {
105 irq_spinlock_unlock(&thread->lock, false);
106 DEADLOCK_PROBE(p_wqlock, DEADLOCK_THRESHOLD);
107 /* Avoid deadlock */
108 goto grab_locks;
109 }
110
111 list_remove(&thread->wq_link);
112 thread->saved_context = thread->sleep_timeout_context;
113 do_wakeup = true;
114 thread->sleep_queue = NULL;
115 irq_spinlock_unlock(&wq->lock, false);
116 }
117
118 thread->timeout_pending = false;
119 irq_spinlock_unlock(&thread->lock, false);
120
121 if (do_wakeup)
122 thread_ready(thread);
123
124out:
125 irq_spinlock_unlock(&threads_lock, false);
126}
127
128/** Interrupt sleeping thread.
129 *
130 * This routine attempts to interrupt a thread from its sleep in
131 * a waitqueue. If the thread is not found sleeping, no action
132 * is taken.
133 *
134 * The threads_lock must be already held and interrupts must be
135 * disabled upon calling this function.
136 *
137 * @param thread Thread to be interrupted.
138 *
139 */
140void waitq_interrupt_sleep(thread_t *thread)
141{
142 bool do_wakeup = false;
143 DEADLOCK_PROBE_INIT(p_wqlock);
144
145 /*
146 * The thread is quaranteed to exist because
147 * threads_lock is held.
148 */
149
150grab_locks:
151 irq_spinlock_lock(&thread->lock, false);
152
153 waitq_t *wq;
154 if ((wq = thread->sleep_queue)) { /* Assignment */
155 if (!(thread->sleep_interruptible)) {
156 /*
157 * The sleep cannot be interrupted.
158 */
159 irq_spinlock_unlock(&thread->lock, false);
160 return;
161 }
162
163 if (!irq_spinlock_trylock(&wq->lock)) {
164 /* Avoid deadlock */
165 irq_spinlock_unlock(&thread->lock, false);
166 DEADLOCK_PROBE(p_wqlock, DEADLOCK_THRESHOLD);
167 goto grab_locks;
168 }
169
170 if ((thread->timeout_pending) &&
171 (timeout_unregister(&thread->sleep_timeout)))
172 thread->timeout_pending = false;
173
174 list_remove(&thread->wq_link);
175 thread->saved_context = thread->sleep_interruption_context;
176 do_wakeup = true;
177 thread->sleep_queue = NULL;
178 irq_spinlock_unlock(&wq->lock, false);
179 }
180
181 irq_spinlock_unlock(&thread->lock, false);
182
183 if (do_wakeup)
184 thread_ready(thread);
185}
186
187/** Interrupt the first thread sleeping in the wait queue.
188 *
189 * Note that the caller somehow needs to know that the thread to be interrupted
190 * is sleeping interruptibly.
191 *
192 * @param wq Pointer to wait queue.
193 *
194 */
195void waitq_unsleep(waitq_t *wq)
196{
197 irq_spinlock_lock(&wq->lock, true);
198
199 if (!list_empty(&wq->sleepers)) {
200 thread_t *thread = list_get_instance(list_first(&wq->sleepers),
201 thread_t, wq_link);
202
203 irq_spinlock_lock(&thread->lock, false);
204
205 ASSERT(thread->sleep_interruptible);
206
207 if ((thread->timeout_pending) &&
208 (timeout_unregister(&thread->sleep_timeout)))
209 thread->timeout_pending = false;
210
211 list_remove(&thread->wq_link);
212 thread->saved_context = thread->sleep_interruption_context;
213 thread->sleep_queue = NULL;
214
215 irq_spinlock_unlock(&thread->lock, false);
216 thread_ready(thread);
217 }
218
219 irq_spinlock_unlock(&wq->lock, true);
220}
221
222#define PARAM_NON_BLOCKING(flags, usec) \
223 (((flags) & SYNCH_FLAGS_NON_BLOCKING) && ((usec) == 0))
224
225/** Sleep until either wakeup, timeout or interruption occurs
226 *
227 * This is a sleep implementation which allows itself to time out or to be
228 * interrupted from the sleep, restoring a failover context.
229 *
230 * Sleepers are organised in a FIFO fashion in a structure called wait queue.
231 *
232 * This function is really basic in that other functions as waitq_sleep()
233 * and all the *_timeout() functions use it.
234 *
235 * @param wq Pointer to wait queue.
236 * @param usec Timeout in microseconds.
237 * @param flags Specify mode of the sleep.
238 *
239 * The sleep can be interrupted only if the
240 * SYNCH_FLAGS_INTERRUPTIBLE bit is specified in flags.
241 *
242 * If usec is greater than zero, regardless of the value of the
243 * SYNCH_FLAGS_NON_BLOCKING bit in flags, the call will not return until either
244 * timeout, interruption or wakeup comes.
245 *
246 * If usec is zero and the SYNCH_FLAGS_NON_BLOCKING bit is not set in flags,
247 * the call will not return until wakeup or interruption comes.
248 *
249 * If usec is zero and the SYNCH_FLAGS_NON_BLOCKING bit is set in flags, the
250 * call will immediately return, reporting either success or failure.
251 *
252 * @return ESYNCH_WOULD_BLOCK, meaning that the sleep failed because at the
253 * time of the call there was no pending wakeup
254 * @return ESYNCH_TIMEOUT, meaning that the sleep timed out.
255 * @return ESYNCH_INTERRUPTED, meaning that somebody interrupted the sleeping
256 * thread.
257 * @return ESYNCH_OK_ATOMIC, meaning that the sleep succeeded and that there
258 * was a pending wakeup at the time of the call. The caller was not put
259 * asleep at all.
260 * @return ESYNCH_OK_BLOCKED, meaning that the sleep succeeded; the full sleep
261 * was attempted.
262 *
263 */
264int waitq_sleep_timeout(waitq_t *wq, uint32_t usec, unsigned int flags)
265{
266 ASSERT((!PREEMPTION_DISABLED) || (PARAM_NON_BLOCKING(flags, usec)));
267
268 ipl_t ipl = waitq_sleep_prepare(wq);
269 int rc = waitq_sleep_timeout_unsafe(wq, usec, flags);
270 waitq_sleep_finish(wq, rc, ipl);
271 return rc;
272}
273
274/** Prepare to sleep in a waitq.
275 *
276 * This function will return holding the lock of the wait queue
277 * and interrupts disabled.
278 *
279 * @param wq Wait queue.
280 *
281 * @return Interrupt level as it existed on entry to this function.
282 *
283 */
284ipl_t waitq_sleep_prepare(waitq_t *wq)
285{
286 ipl_t ipl;
287
288restart:
289 ipl = interrupts_disable();
290
291 if (THREAD) { /* Needed during system initiailzation */
292 /*
293 * Busy waiting for a delayed timeout.
294 * This is an important fix for the race condition between
295 * a delayed timeout and a next call to waitq_sleep_timeout().
296 * Simply, the thread is not allowed to go to sleep if
297 * there are timeouts in progress.
298 *
299 */
300 irq_spinlock_lock(&THREAD->lock, false);
301
302 if (THREAD->timeout_pending) {
303 irq_spinlock_unlock(&THREAD->lock, false);
304 interrupts_restore(ipl);
305 goto restart;
306 }
307
308 irq_spinlock_unlock(&THREAD->lock, false);
309 }
310
311 irq_spinlock_lock(&wq->lock, false);
312 return ipl;
313}
314
315/** Finish waiting in a wait queue.
316 *
317 * This function restores interrupts to the state that existed prior
318 * to the call to waitq_sleep_prepare(). If necessary, the wait queue
319 * lock is released.
320 *
321 * @param wq Wait queue.
322 * @param rc Return code of waitq_sleep_timeout_unsafe().
323 * @param ipl Interrupt level returned by waitq_sleep_prepare().
324 *
325 */
326void waitq_sleep_finish(waitq_t *wq, int rc, ipl_t ipl)
327{
328 switch (rc) {
329 case ESYNCH_WOULD_BLOCK:
330 case ESYNCH_OK_ATOMIC:
331 irq_spinlock_unlock(&wq->lock, false);
332 break;
333 default:
334 /*
335 * Wait for a waitq_wakeup() or waitq_unsleep() to complete
336 * before returning from waitq_sleep() to the caller. Otherwise
337 * the caller might expect that the wait queue is no longer used
338 * and deallocate it (although the wakeup on a another cpu has
339 * not yet completed and is using the wait queue).
340 *
341 * Note that we have to do this for ESYNCH_OK_BLOCKED and
342 * ESYNCH_INTERRUPTED, but not necessarily for ESYNCH_TIMEOUT
343 * where the timeout handler stops using the waitq before waking
344 * us up. To be on the safe side, ensure the waitq is not in use
345 * anymore in this case as well.
346 */
347 waitq_complete_wakeup(wq);
348 break;
349 }
350
351 interrupts_restore(ipl);
352}
353
354/** Internal implementation of waitq_sleep_timeout().
355 *
356 * This function implements logic of sleeping in a wait queue.
357 * This call must be preceded by a call to waitq_sleep_prepare()
358 * and followed by a call to waitq_sleep_finish().
359 *
360 * @param wq See waitq_sleep_timeout().
361 * @param usec See waitq_sleep_timeout().
362 * @param flags See waitq_sleep_timeout().
363 *
364 * @return See waitq_sleep_timeout().
365 *
366 */
367int waitq_sleep_timeout_unsafe(waitq_t *wq, uint32_t usec, unsigned int flags)
368{
369 /* Checks whether to go to sleep at all */
370 if (wq->missed_wakeups) {
371 wq->missed_wakeups--;
372 return ESYNCH_OK_ATOMIC;
373 } else {
374 if (PARAM_NON_BLOCKING(flags, usec)) {
375 /* Return immediately instead of going to sleep */
376 return ESYNCH_WOULD_BLOCK;
377 }
378 }
379
380 /*
381 * Now we are firmly decided to go to sleep.
382 *
383 */
384 irq_spinlock_lock(&THREAD->lock, false);
385
386 if (flags & SYNCH_FLAGS_INTERRUPTIBLE) {
387 /*
388 * If the thread was already interrupted,
389 * don't go to sleep at all.
390 */
391 if (THREAD->interrupted) {
392 irq_spinlock_unlock(&THREAD->lock, false);
393 irq_spinlock_unlock(&wq->lock, false);
394 return ESYNCH_INTERRUPTED;
395 }
396
397 /*
398 * Set context that will be restored if the sleep
399 * of this thread is ever interrupted.
400 */
401 THREAD->sleep_interruptible = true;
402 if (!context_save(&THREAD->sleep_interruption_context)) {
403 /* Short emulation of scheduler() return code. */
404 THREAD->last_cycle = get_cycle();
405 irq_spinlock_unlock(&THREAD->lock, false);
406 return ESYNCH_INTERRUPTED;
407 }
408 } else
409 THREAD->sleep_interruptible = false;
410
411 if (usec) {
412 /* We use the timeout variant. */
413 if (!context_save(&THREAD->sleep_timeout_context)) {
414 /* Short emulation of scheduler() return code. */
415 THREAD->last_cycle = get_cycle();
416 irq_spinlock_unlock(&THREAD->lock, false);
417 return ESYNCH_TIMEOUT;
418 }
419
420 THREAD->timeout_pending = true;
421 timeout_register(&THREAD->sleep_timeout, (uint64_t) usec,
422 waitq_sleep_timed_out, THREAD);
423 }
424
425 list_append(&THREAD->wq_link, &wq->sleepers);
426
427 /*
428 * Suspend execution.
429 *
430 */
431 THREAD->state = Sleeping;
432 THREAD->sleep_queue = wq;
433
434 irq_spinlock_unlock(&THREAD->lock, false);
435
436 /* wq->lock is released in scheduler_separated_stack() */
437 scheduler();
438
439 return ESYNCH_OK_BLOCKED;
440}
441
442/** Wake up first thread sleeping in a wait queue
443 *
444 * Wake up first thread sleeping in a wait queue. This is the SMP- and IRQ-safe
445 * wrapper meant for general use.
446 *
447 * Besides its 'normal' wakeup operation, it attempts to unregister possible
448 * timeout.
449 *
450 * @param wq Pointer to wait queue.
451 * @param mode Wakeup mode.
452 *
453 */
454void waitq_wakeup(waitq_t *wq, wakeup_mode_t mode)
455{
456 irq_spinlock_lock(&wq->lock, true);
457 _waitq_wakeup_unsafe(wq, mode);
458 irq_spinlock_unlock(&wq->lock, true);
459}
460
461/** If there is a wakeup in progress actively waits for it to complete.
462 *
463 * The function returns once the concurrently running waitq_wakeup()
464 * exits. It returns immediately if there are no concurrent wakeups
465 * at the time.
466 *
467 * Interrupts must be disabled.
468 *
469 * Example usage:
470 * @code
471 * void callback(waitq *wq)
472 * {
473 * // Do something and notify wait_for_completion() that we're done.
474 * waitq_wakeup(wq);
475 * }
476 * void wait_for_completion(void)
477 * {
478 * waitq wg;
479 * waitq_initialize(&wq);
480 * // Run callback() in the background, pass it wq.
481 * do_asynchronously(callback, &wq);
482 * // Wait for callback() to complete its work.
483 * waitq_sleep(&wq);
484 * // callback() completed its work, but it may still be accessing
485 * // wq in waitq_wakeup(). Therefore it is not yet safe to return
486 * // from waitq_sleep() or it would clobber up our stack (where wq
487 * // is stored). waitq_sleep() ensures the wait queue is no longer
488 * // in use by invoking waitq_complete_wakeup() internally.
489 *
490 * // waitq_sleep() returned, it is safe to free wq.
491 * }
492 * @endcode
493 *
494 * @param wq Pointer to a wait queue.
495 */
496static void waitq_complete_wakeup(waitq_t *wq)
497{
498 ASSERT(interrupts_disabled());
499
500 irq_spinlock_lock(&wq->lock, false);
501 irq_spinlock_unlock(&wq->lock, false);
502}
503
504
505/** Internal SMP- and IRQ-unsafe version of waitq_wakeup()
506 *
507 * This is the internal SMP- and IRQ-unsafe version of waitq_wakeup(). It
508 * assumes wq->lock is already locked and interrupts are already disabled.
509 *
510 * @param wq Pointer to wait queue.
511 * @param mode If mode is WAKEUP_FIRST, then the longest waiting
512 * thread, if any, is woken up. If mode is WAKEUP_ALL, then
513 * all waiting threads, if any, are woken up. If there are
514 * no waiting threads to be woken up, the missed wakeup is
515 * recorded in the wait queue.
516 *
517 */
518void _waitq_wakeup_unsafe(waitq_t *wq, wakeup_mode_t mode)
519{
520 size_t count = 0;
521
522 ASSERT(interrupts_disabled());
523 ASSERT(irq_spinlock_locked(&wq->lock));
524
525loop:
526 if (list_empty(&wq->sleepers)) {
527 wq->missed_wakeups++;
528 if ((count) && (mode == WAKEUP_ALL))
529 wq->missed_wakeups--;
530
531 return;
532 }
533
534 count++;
535 thread_t *thread = list_get_instance(list_first(&wq->sleepers),
536 thread_t, wq_link);
537
538 /*
539 * Lock the thread prior to removing it from the wq.
540 * This is not necessary because of mutual exclusion
541 * (the link belongs to the wait queue), but because
542 * of synchronization with waitq_sleep_timed_out()
543 * and thread_interrupt_sleep().
544 *
545 * In order for these two functions to work, the following
546 * invariant must hold:
547 *
548 * thread->sleep_queue != NULL <=> thread sleeps in a wait queue
549 *
550 * For an observer who locks the thread, the invariant
551 * holds only when the lock is held prior to removing
552 * it from the wait queue.
553 *
554 */
555 irq_spinlock_lock(&thread->lock, false);
556 list_remove(&thread->wq_link);
557
558 if ((thread->timeout_pending) &&
559 (timeout_unregister(&thread->sleep_timeout)))
560 thread->timeout_pending = false;
561
562 thread->sleep_queue = NULL;
563 irq_spinlock_unlock(&thread->lock, false);
564
565 thread_ready(thread);
566
567 if (mode == WAKEUP_ALL)
568 goto loop;
569}
570
571/** Get the missed wakeups count.
572 *
573 * @param wq Pointer to wait queue.
574 * @return The wait queue's missed_wakeups count.
575 */
576int waitq_count_get(waitq_t *wq)
577{
578 int cnt;
579
580 irq_spinlock_lock(&wq->lock, true);
581 cnt = wq->missed_wakeups;
582 irq_spinlock_unlock(&wq->lock, true);
583
584 return cnt;
585}
586
587/** Set the missed wakeups count.
588 *
589 * @param wq Pointer to wait queue.
590 * @param val New value of the missed_wakeups count.
591 */
592void waitq_count_set(waitq_t *wq, int val)
593{
594 irq_spinlock_lock(&wq->lock, true);
595 wq->missed_wakeups = val;
596 irq_spinlock_unlock(&wq->lock, true);
597}
598
599/** @}
600 */
Note: See TracBrowser for help on using the repository browser.