[f761f1eb] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
[111b9b9] | 3 | * Copyright (c) 2022 Jiří Zárevúcky
|
---|
[f761f1eb] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[e88eb48] | 30 | /** @addtogroup kernel_sync
|
---|
[b45c443] | 31 | * @{
|
---|
| 32 | */
|
---|
| 33 |
|
---|
[9179d0a] | 34 | /**
|
---|
[b45c443] | 35 | * @file
|
---|
[da1bafb] | 36 | * @brief Wait queue.
|
---|
[9179d0a] | 37 | *
|
---|
[e3c762cd] | 38 | * Wait queue is the basic synchronization primitive upon which all
|
---|
[9179d0a] | 39 | * other synchronization primitives build.
|
---|
| 40 | *
|
---|
| 41 | * It allows threads to wait for an event in first-come, first-served
|
---|
| 42 | * fashion. Conditional operation as well as timeouts and interruptions
|
---|
| 43 | * are supported.
|
---|
[da1bafb] | 44 | *
|
---|
[9179d0a] | 45 | */
|
---|
| 46 |
|
---|
[63e27ef] | 47 | #include <assert.h>
|
---|
[897fd8f1] | 48 | #include <errno.h>
|
---|
[f761f1eb] | 49 | #include <synch/waitq.h>
|
---|
| 50 | #include <synch/spinlock.h>
|
---|
[f43d8ce] | 51 | #include <preemption.h>
|
---|
[922c7ce] | 52 | #include <proc/thread.h>
|
---|
[4b2c872d] | 53 | #include <proc/scheduler.h>
|
---|
[f761f1eb] | 54 | #include <arch/asm.h>
|
---|
[d99c1d2] | 55 | #include <typedefs.h>
|
---|
[922c7ce] | 56 | #include <time/timeout.h>
|
---|
[f761f1eb] | 57 | #include <arch.h>
|
---|
[922c7ce] | 58 | #include <context.h>
|
---|
[5c9a08b] | 59 | #include <adt/list.h>
|
---|
[6ec34bb] | 60 | #include <arch/cycle.h>
|
---|
[b169619] | 61 | #include <memw.h>
|
---|
[f761f1eb] | 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 | {
|
---|
[597fa24] | 72 | *wq = WAITQ_INITIALIZER(*wq);
|
---|
[f761f1eb] | 73 | }
|
---|
| 74 |
|
---|
[111b9b9] | 75 | /**
|
---|
| 76 | * Initialize wait queue with an initial number of queued wakeups
|
---|
| 77 | * (or a wakeup debt if negative).
|
---|
[5573942] | 78 | */
|
---|
[111b9b9] | 79 | void waitq_initialize_with_count(waitq_t *wq, int count)
|
---|
[5573942] | 80 | {
|
---|
[597fa24] | 81 | *wq = WAITQ_INITIALIZER_WITH_COUNT(*wq, count);
|
---|
[5573942] | 82 | }
|
---|
[203f4c3] | 83 |
|
---|
[4039c77] | 84 | #define PARAM_NON_BLOCKING(flags, usec) \
|
---|
| 85 | (((flags) & SYNCH_FLAGS_NON_BLOCKING) && ((usec) == 0))
|
---|
| 86 |
|
---|
[5110d0a] | 87 | errno_t waitq_sleep(waitq_t *wq)
|
---|
| 88 | {
|
---|
[111b9b9] | 89 | return _waitq_sleep_timeout(wq, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | errno_t waitq_sleep_timeout(waitq_t *wq, uint32_t usec)
|
---|
| 93 | {
|
---|
| 94 | return _waitq_sleep_timeout(wq, usec, SYNCH_FLAGS_NON_BLOCKING);
|
---|
[5110d0a] | 95 | }
|
---|
| 96 |
|
---|
[203f4c3] | 97 | /** Sleep until either wakeup, timeout or interruption occurs
|
---|
[f761f1eb] | 98 | *
|
---|
[c0bc189] | 99 | * Sleepers are organised in a FIFO fashion in a structure called wait queue.
|
---|
[922c7ce] | 100 | *
|
---|
[111b9b9] | 101 | * Other functions as waitq_sleep() and all the *_timeout() functions are
|
---|
| 102 | * implemented using this function.
|
---|
[f761f1eb] | 103 | *
|
---|
[da1bafb] | 104 | * @param wq Pointer to wait queue.
|
---|
| 105 | * @param usec Timeout in microseconds.
|
---|
| 106 | * @param flags Specify mode of the sleep.
|
---|
[922c7ce] | 107 | *
|
---|
[116d1ef4] | 108 | * The sleep can be interrupted only if the
|
---|
| 109 | * SYNCH_FLAGS_INTERRUPTIBLE bit is specified in flags.
|
---|
[da1bafb] | 110 | *
|
---|
[116d1ef4] | 111 | * If usec is greater than zero, regardless of the value of the
|
---|
[4e33b6b] | 112 | * SYNCH_FLAGS_NON_BLOCKING bit in flags, the call will not return until either
|
---|
[da1bafb] | 113 | * timeout, interruption or wakeup comes.
|
---|
[f761f1eb] | 114 | *
|
---|
[4e33b6b] | 115 | * If usec is zero and the SYNCH_FLAGS_NON_BLOCKING bit is not set in flags,
|
---|
| 116 | * the call will not return until wakeup or interruption comes.
|
---|
[a783ca4] | 117 | *
|
---|
[4e33b6b] | 118 | * If usec is zero and the SYNCH_FLAGS_NON_BLOCKING bit is set in flags, the
|
---|
| 119 | * call will immediately return, reporting either success or failure.
|
---|
[f761f1eb] | 120 | *
|
---|
[111b9b9] | 121 | * @return ETIMEOUT, meaning that the sleep timed out, or a nonblocking call
|
---|
| 122 | * returned unsuccessfully.
|
---|
| 123 | * @return EINTR, meaning that somebody interrupted the sleeping thread.
|
---|
[897fd8f1] | 124 | * @return EOK, meaning that none of the above conditions occured, and the
|
---|
[111b9b9] | 125 | * thread was woken up successfuly by `waitq_wake_*()`.
|
---|
[922c7ce] | 126 | *
|
---|
[f761f1eb] | 127 | */
|
---|
[111b9b9] | 128 | errno_t _waitq_sleep_timeout(waitq_t *wq, uint32_t usec, unsigned int flags)
|
---|
[f761f1eb] | 129 | {
|
---|
[63e27ef] | 130 | assert((!PREEMPTION_DISABLED) || (PARAM_NON_BLOCKING(flags, usec)));
|
---|
[111b9b9] | 131 | return waitq_sleep_timeout_unsafe(wq, usec, flags, waitq_sleep_prepare(wq));
|
---|
[c0bc189] | 132 | }
|
---|
| 133 |
|
---|
| 134 | /** Prepare to sleep in a waitq.
|
---|
| 135 | *
|
---|
| 136 | * This function will return holding the lock of the wait queue
|
---|
| 137 | * and interrupts disabled.
|
---|
| 138 | *
|
---|
[da1bafb] | 139 | * @param wq Wait queue.
|
---|
| 140 | *
|
---|
| 141 | * @return Interrupt level as it existed on entry to this function.
|
---|
[c0bc189] | 142 | *
|
---|
| 143 | */
|
---|
[111b9b9] | 144 | wait_guard_t waitq_sleep_prepare(waitq_t *wq)
|
---|
[c0bc189] | 145 | {
|
---|
[83789ea2] | 146 | ipl_t ipl = interrupts_disable();
|
---|
[da1bafb] | 147 | irq_spinlock_lock(&wq->lock, false);
|
---|
[111b9b9] | 148 | return (wait_guard_t) {
|
---|
| 149 | .ipl = ipl,
|
---|
| 150 | };
|
---|
[c0bc189] | 151 | }
|
---|
| 152 |
|
---|
[111b9b9] | 153 | errno_t waitq_sleep_unsafe(waitq_t *wq, wait_guard_t guard)
|
---|
[5110d0a] | 154 | {
|
---|
[111b9b9] | 155 | return waitq_sleep_timeout_unsafe(wq, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE, guard);
|
---|
[5110d0a] | 156 | }
|
---|
| 157 |
|
---|
[c0bc189] | 158 | /** Internal implementation of waitq_sleep_timeout().
|
---|
| 159 | *
|
---|
| 160 | * This function implements logic of sleeping in a wait queue.
|
---|
[111b9b9] | 161 | * This call must be preceded by a call to waitq_sleep_prepare().
|
---|
[c0bc189] | 162 | *
|
---|
[da1bafb] | 163 | * @param wq See waitq_sleep_timeout().
|
---|
| 164 | * @param usec See waitq_sleep_timeout().
|
---|
| 165 | * @param flags See waitq_sleep_timeout().
|
---|
| 166 | *
|
---|
[897fd8f1] | 167 | * @param[out] blocked See waitq_sleep_timeout().
|
---|
| 168 | *
|
---|
[da1bafb] | 169 | * @return See waitq_sleep_timeout().
|
---|
[c0bc189] | 170 | *
|
---|
| 171 | */
|
---|
[111b9b9] | 172 | errno_t waitq_sleep_timeout_unsafe(waitq_t *wq, uint32_t usec, unsigned int flags, wait_guard_t guard)
|
---|
[c0bc189] | 173 | {
|
---|
[111b9b9] | 174 | errno_t rc;
|
---|
| 175 |
|
---|
| 176 | /*
|
---|
| 177 | * If true, and this thread's sleep returns without a wakeup
|
---|
| 178 | * (timed out or interrupted), waitq ignores the next wakeup.
|
---|
| 179 | * This is necessary for futex to be able to handle those conditions.
|
---|
| 180 | */
|
---|
| 181 | bool sleep_composable = (flags & SYNCH_FLAGS_FUTEX);
|
---|
| 182 | bool interruptible = (flags & SYNCH_FLAGS_INTERRUPTIBLE);
|
---|
| 183 |
|
---|
| 184 | if (wq->closed) {
|
---|
| 185 | rc = EOK;
|
---|
| 186 | goto exit;
|
---|
| 187 | }
|
---|
[897fd8f1] | 188 |
|
---|
[da1bafb] | 189 | /* Checks whether to go to sleep at all */
|
---|
[111b9b9] | 190 | if (wq->wakeup_balance > 0) {
|
---|
| 191 | wq->wakeup_balance--;
|
---|
| 192 |
|
---|
| 193 | rc = EOK;
|
---|
| 194 | goto exit;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | if (PARAM_NON_BLOCKING(flags, usec)) {
|
---|
| 198 | /* Return immediately instead of going to sleep */
|
---|
| 199 | rc = ETIMEOUT;
|
---|
| 200 | goto exit;
|
---|
[f761f1eb] | 201 | }
|
---|
[a35b458] | 202 |
|
---|
[111b9b9] | 203 | /* Just for debugging output. */
|
---|
| 204 | atomic_store_explicit(&THREAD->sleep_queue, wq, memory_order_relaxed);
|
---|
| 205 |
|
---|
[f761f1eb] | 206 | /*
|
---|
[111b9b9] | 207 | * This thread_t field is synchronized exclusively via
|
---|
| 208 | * waitq lock of the waitq currently listing it.
|
---|
[f761f1eb] | 209 | */
|
---|
[111b9b9] | 210 | list_append(&THREAD->wq_link, &wq->sleepers);
|
---|
[83789ea2] | 211 |
|
---|
[111b9b9] | 212 | /* Needs to be run when interrupts are still disabled. */
|
---|
| 213 | deadline_t deadline = usec > 0 ?
|
---|
| 214 | timeout_deadline_in_usec(usec) : DEADLINE_NEVER;
|
---|
[b59318e] | 215 |
|
---|
[111b9b9] | 216 | while (true) {
|
---|
| 217 | bool terminating = (thread_wait_start() == THREAD_TERMINATING);
|
---|
| 218 | if (terminating && interruptible) {
|
---|
| 219 | rc = EINTR;
|
---|
| 220 | goto exit;
|
---|
[34dcd3f] | 221 | }
|
---|
[a35b458] | 222 |
|
---|
[111b9b9] | 223 | irq_spinlock_unlock(&wq->lock, false);
|
---|
| 224 |
|
---|
| 225 | bool timed_out = (thread_wait_finish(deadline) == THREAD_WAIT_TIMEOUT);
|
---|
| 226 |
|
---|
[116d1ef4] | 227 | /*
|
---|
[111b9b9] | 228 | * We always need to re-lock the WQ, since concurrently running
|
---|
| 229 | * waitq_wakeup() may still not have exitted.
|
---|
| 230 | * If we didn't always do this, we'd risk waitq_wakeup() that woke us
|
---|
| 231 | * up still running on another CPU even after this function returns,
|
---|
| 232 | * and that would be an issue if the waitq is allocated locally to
|
---|
| 233 | * wait for a one-off asynchronous event. We'd need more external
|
---|
| 234 | * synchronization in that case, and that would be a pain.
|
---|
| 235 | *
|
---|
| 236 | * On the plus side, always regaining a lock simplifies cleanup.
|
---|
[116d1ef4] | 237 | */
|
---|
[111b9b9] | 238 | irq_spinlock_lock(&wq->lock, false);
|
---|
| 239 |
|
---|
| 240 | if (!link_in_use(&THREAD->wq_link)) {
|
---|
| 241 | /*
|
---|
| 242 | * We were woken up by the desired event. Return success,
|
---|
| 243 | * regardless of any concurrent timeout or interruption.
|
---|
| 244 | */
|
---|
| 245 | rc = EOK;
|
---|
| 246 | goto exit;
|
---|
[116d1ef4] | 247 | }
|
---|
[111b9b9] | 248 |
|
---|
| 249 | if (timed_out) {
|
---|
| 250 | rc = ETIMEOUT;
|
---|
| 251 | goto exit;
|
---|
[f761f1eb] | 252 | }
|
---|
[a35b458] | 253 |
|
---|
[111b9b9] | 254 | /* Interrupted for some other reason. */
|
---|
[f761f1eb] | 255 | }
|
---|
[a35b458] | 256 |
|
---|
[111b9b9] | 257 | exit:
|
---|
| 258 | if (THREAD)
|
---|
| 259 | list_remove(&THREAD->wq_link);
|
---|
[a35b458] | 260 |
|
---|
[111b9b9] | 261 | if (rc != EOK && sleep_composable)
|
---|
| 262 | wq->wakeup_balance--;
|
---|
[a35b458] | 263 |
|
---|
[111b9b9] | 264 | if (THREAD)
|
---|
| 265 | atomic_store_explicit(&THREAD->sleep_queue, NULL, memory_order_relaxed);
|
---|
[a35b458] | 266 |
|
---|
[111b9b9] | 267 | irq_spinlock_unlock(&wq->lock, false);
|
---|
| 268 | interrupts_restore(guard.ipl);
|
---|
| 269 | return rc;
|
---|
[f761f1eb] | 270 | }
|
---|
| 271 |
|
---|
[111b9b9] | 272 | static void _wake_one(waitq_t *wq)
|
---|
[f761f1eb] | 273 | {
|
---|
[111b9b9] | 274 | /* Pop one thread from the queue and wake it up. */
|
---|
| 275 | thread_t *thread = list_get_instance(list_first(&wq->sleepers), thread_t, wq_link);
|
---|
| 276 | list_remove(&thread->wq_link);
|
---|
| 277 | thread_wakeup(thread);
|
---|
[f761f1eb] | 278 | }
|
---|
| 279 |
|
---|
[111b9b9] | 280 | /**
|
---|
| 281 | * Meant for implementing condvar signal.
|
---|
| 282 | * Always wakes one thread if there are any sleeping,
|
---|
| 283 | * has no effect if no threads are waiting for wakeup.
|
---|
[3954961e] | 284 | */
|
---|
[111b9b9] | 285 | void waitq_signal(waitq_t *wq)
|
---|
[3954961e] | 286 | {
|
---|
[111b9b9] | 287 | irq_spinlock_lock(&wq->lock, true);
|
---|
[a35b458] | 288 |
|
---|
[111b9b9] | 289 | if (!list_empty(&wq->sleepers))
|
---|
| 290 | _wake_one(wq);
|
---|
| 291 |
|
---|
| 292 | irq_spinlock_unlock(&wq->lock, true);
|
---|
[3954961e] | 293 | }
|
---|
| 294 |
|
---|
[111b9b9] | 295 | /**
|
---|
| 296 | * Wakes up one thread sleeping on this waitq.
|
---|
| 297 | * If there are no threads waiting, saves the wakeup so that the next sleep
|
---|
| 298 | * returns immediately. If a previous failure in sleep created a wakeup debt
|
---|
| 299 | * (see SYNCH_FLAGS_FUTEX) this debt is annulled and no thread is woken up.
|
---|
[f761f1eb] | 300 | */
|
---|
[111b9b9] | 301 | void waitq_wake_one(waitq_t *wq)
|
---|
[f761f1eb] | 302 | {
|
---|
[111b9b9] | 303 | irq_spinlock_lock(&wq->lock, true);
|
---|
[a35b458] | 304 |
|
---|
[111b9b9] | 305 | if (!wq->closed) {
|
---|
| 306 | if (wq->wakeup_balance < 0 || list_empty(&wq->sleepers))
|
---|
| 307 | wq->wakeup_balance++;
|
---|
| 308 | else
|
---|
| 309 | _wake_one(wq);
|
---|
[f761f1eb] | 310 | }
|
---|
[a35b458] | 311 |
|
---|
[111b9b9] | 312 | irq_spinlock_unlock(&wq->lock, true);
|
---|
| 313 | }
|
---|
[a35b458] | 314 |
|
---|
[111b9b9] | 315 | static void _wake_all(waitq_t *wq)
|
---|
| 316 | {
|
---|
| 317 | while (!list_empty(&wq->sleepers))
|
---|
| 318 | _wake_one(wq);
|
---|
| 319 | }
|
---|
[a35b458] | 320 |
|
---|
[111b9b9] | 321 | /**
|
---|
| 322 | * Wakes up all threads currently waiting on this waitq
|
---|
| 323 | * and makes all future sleeps return instantly.
|
---|
| 324 | */
|
---|
| 325 | void waitq_close(waitq_t *wq)
|
---|
| 326 | {
|
---|
| 327 | irq_spinlock_lock(&wq->lock, true);
|
---|
| 328 | wq->wakeup_balance = 0;
|
---|
| 329 | wq->closed = true;
|
---|
| 330 | _wake_all(wq);
|
---|
| 331 | irq_spinlock_unlock(&wq->lock, true);
|
---|
| 332 | }
|
---|
[a35b458] | 333 |
|
---|
[111b9b9] | 334 | /**
|
---|
| 335 | * Wakes up all threads currently waiting on this waitq
|
---|
| 336 | */
|
---|
| 337 | void waitq_wake_all(waitq_t *wq)
|
---|
| 338 | {
|
---|
| 339 | irq_spinlock_lock(&wq->lock, true);
|
---|
| 340 | wq->wakeup_balance = 0;
|
---|
| 341 | _wake_all(wq);
|
---|
| 342 | irq_spinlock_unlock(&wq->lock, true);
|
---|
[f761f1eb] | 343 | }
|
---|
[b45c443] | 344 |
|
---|
[cc73a8a1] | 345 | /** @}
|
---|
[b45c443] | 346 | */
|
---|