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