source: mainline/kernel/generic/src/synch/waitq.c@ 41ff85b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 41ff85b was 55b77d9, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Separate list_t typedef from link_t (kernel part).

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