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