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