source: mainline/kernel/generic/src/synch/waitq.c@ 3061bc1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3061bc1 was 1b20da0, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

style: Remove trailing whitespace on non-empty lines, in certain file types.

Command used: tools/srepl '\([^[:space:]]\)\s\+$' '\1' -- *.c *.h *.py *.sh *.s *.S *.ag

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