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