source: mainline/kernel/generic/src/synch/waitq.c@ 82453b29

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

Make futex able to time out.

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