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