source: mainline/kernel/generic/src/synch/waitq.c@ c89ae25

topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c89ae25 was 111b9b9, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 years ago

Reimplement waitq using thread_wait/wakeup

This adds a few functions to the thread API which can be
summarized as "stop running until woken up by others".
The ordering and context-switching concerns are thus yeeted
to this abstraction and waitq only deals with maintaining
the queues. Overall, this makes the control flow in waitq
much easier to navigate.

  • Property mode set to 100644
File size: 9.6 KB
RevLine 
[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>
[b59318e]61#include <mem.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]70void waitq_initialize(waitq_t *wq)
71{
[b59318e]72 memsetb(wq, sizeof(*wq), 0);
[da1bafb]73 irq_spinlock_initialize(&wq->lock, "wq.lock");
[55b77d9]74 list_initialize(&wq->sleepers);
[f761f1eb]75}
76
[111b9b9]77/**
78 * Initialize wait queue with an initial number of queued wakeups
79 * (or a wakeup debt if negative).
[5573942]80 */
[111b9b9]81void waitq_initialize_with_count(waitq_t *wq, int count)
[5573942]82{
[111b9b9]83 waitq_initialize(wq);
84 wq->wakeup_balance = count;
[5573942]85}
[203f4c3]86
[4039c77]87#define PARAM_NON_BLOCKING(flags, usec) \
88 (((flags) & SYNCH_FLAGS_NON_BLOCKING) && ((usec) == 0))
89
[5110d0a]90errno_t waitq_sleep(waitq_t *wq)
91{
[111b9b9]92 return _waitq_sleep_timeout(wq, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
93}
94
95errno_t waitq_sleep_timeout(waitq_t *wq, uint32_t usec)
96{
97 return _waitq_sleep_timeout(wq, usec, SYNCH_FLAGS_NON_BLOCKING);
[5110d0a]98}
99
[203f4c3]100/** Sleep until either wakeup, timeout or interruption occurs
[f761f1eb]101 *
[c0bc189]102 * Sleepers are organised in a FIFO fashion in a structure called wait queue.
[922c7ce]103 *
[111b9b9]104 * Other functions as waitq_sleep() and all the *_timeout() functions are
105 * implemented using this function.
[f761f1eb]106 *
[da1bafb]107 * @param wq Pointer to wait queue.
108 * @param usec Timeout in microseconds.
109 * @param flags Specify mode of the sleep.
[922c7ce]110 *
[116d1ef4]111 * The sleep can be interrupted only if the
112 * SYNCH_FLAGS_INTERRUPTIBLE bit is specified in flags.
[da1bafb]113 *
[116d1ef4]114 * If usec is greater than zero, regardless of the value of the
[4e33b6b]115 * SYNCH_FLAGS_NON_BLOCKING bit in flags, the call will not return until either
[da1bafb]116 * timeout, interruption or wakeup comes.
[f761f1eb]117 *
[4e33b6b]118 * If usec is zero and the SYNCH_FLAGS_NON_BLOCKING bit is not set in flags,
119 * the call will not return until wakeup or interruption comes.
[a783ca4]120 *
[4e33b6b]121 * If usec is zero and the SYNCH_FLAGS_NON_BLOCKING bit is set in flags, the
122 * call will immediately return, reporting either success or failure.
[f761f1eb]123 *
[111b9b9]124 * @return ETIMEOUT, meaning that the sleep timed out, or a nonblocking call
125 * returned unsuccessfully.
126 * @return EINTR, meaning that somebody interrupted the sleeping thread.
[897fd8f1]127 * @return EOK, meaning that none of the above conditions occured, and the
[111b9b9]128 * thread was woken up successfuly by `waitq_wake_*()`.
[922c7ce]129 *
[f761f1eb]130 */
[111b9b9]131errno_t _waitq_sleep_timeout(waitq_t *wq, uint32_t usec, unsigned int flags)
[f761f1eb]132{
[63e27ef]133 assert((!PREEMPTION_DISABLED) || (PARAM_NON_BLOCKING(flags, usec)));
[111b9b9]134 return waitq_sleep_timeout_unsafe(wq, usec, flags, waitq_sleep_prepare(wq));
[c0bc189]135}
136
137/** Prepare to sleep in a waitq.
138 *
139 * This function will return holding the lock of the wait queue
140 * and interrupts disabled.
141 *
[da1bafb]142 * @param wq Wait queue.
143 *
144 * @return Interrupt level as it existed on entry to this function.
[c0bc189]145 *
146 */
[111b9b9]147wait_guard_t waitq_sleep_prepare(waitq_t *wq)
[c0bc189]148{
[83789ea2]149 ipl_t ipl = interrupts_disable();
[da1bafb]150 irq_spinlock_lock(&wq->lock, false);
[111b9b9]151 return (wait_guard_t) {
152 .ipl = ipl,
153 };
[c0bc189]154}
155
[111b9b9]156errno_t waitq_sleep_unsafe(waitq_t *wq, wait_guard_t guard)
[5110d0a]157{
[111b9b9]158 return waitq_sleep_timeout_unsafe(wq, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE, guard);
[5110d0a]159}
160
[c0bc189]161/** Internal implementation of waitq_sleep_timeout().
162 *
163 * This function implements logic of sleeping in a wait queue.
[111b9b9]164 * This call must be preceded by a call to waitq_sleep_prepare().
[c0bc189]165 *
[da1bafb]166 * @param wq See waitq_sleep_timeout().
167 * @param usec See waitq_sleep_timeout().
168 * @param flags See waitq_sleep_timeout().
169 *
[897fd8f1]170 * @param[out] blocked See waitq_sleep_timeout().
171 *
[da1bafb]172 * @return See waitq_sleep_timeout().
[c0bc189]173 *
174 */
[111b9b9]175errno_t waitq_sleep_timeout_unsafe(waitq_t *wq, uint32_t usec, unsigned int flags, wait_guard_t guard)
[c0bc189]176{
[111b9b9]177 errno_t rc;
178
179 /*
180 * If true, and this thread's sleep returns without a wakeup
181 * (timed out or interrupted), waitq ignores the next wakeup.
182 * This is necessary for futex to be able to handle those conditions.
183 */
184 bool sleep_composable = (flags & SYNCH_FLAGS_FUTEX);
185 bool interruptible = (flags & SYNCH_FLAGS_INTERRUPTIBLE);
186
187 if (wq->closed) {
188 rc = EOK;
189 goto exit;
190 }
[897fd8f1]191
[da1bafb]192 /* Checks whether to go to sleep at all */
[111b9b9]193 if (wq->wakeup_balance > 0) {
194 wq->wakeup_balance--;
195
196 rc = EOK;
197 goto exit;
198 }
199
200 if (PARAM_NON_BLOCKING(flags, usec)) {
201 /* Return immediately instead of going to sleep */
202 rc = ETIMEOUT;
203 goto exit;
[f761f1eb]204 }
[a35b458]205
[111b9b9]206 /* Just for debugging output. */
207 atomic_store_explicit(&THREAD->sleep_queue, wq, memory_order_relaxed);
208
[f761f1eb]209 /*
[111b9b9]210 * This thread_t field is synchronized exclusively via
211 * waitq lock of the waitq currently listing it.
[f761f1eb]212 */
[111b9b9]213 list_append(&THREAD->wq_link, &wq->sleepers);
[83789ea2]214
[111b9b9]215 /* Needs to be run when interrupts are still disabled. */
216 deadline_t deadline = usec > 0 ?
217 timeout_deadline_in_usec(usec) : DEADLINE_NEVER;
[b59318e]218
[111b9b9]219 while (true) {
220 bool terminating = (thread_wait_start() == THREAD_TERMINATING);
221 if (terminating && interruptible) {
222 rc = EINTR;
223 goto exit;
[34dcd3f]224 }
[a35b458]225
[111b9b9]226 irq_spinlock_unlock(&wq->lock, false);
227
228 bool timed_out = (thread_wait_finish(deadline) == THREAD_WAIT_TIMEOUT);
229
[116d1ef4]230 /*
[111b9b9]231 * We always need to re-lock the WQ, since concurrently running
232 * waitq_wakeup() may still not have exitted.
233 * If we didn't always do this, we'd risk waitq_wakeup() that woke us
234 * up still running on another CPU even after this function returns,
235 * and that would be an issue if the waitq is allocated locally to
236 * wait for a one-off asynchronous event. We'd need more external
237 * synchronization in that case, and that would be a pain.
238 *
239 * On the plus side, always regaining a lock simplifies cleanup.
[116d1ef4]240 */
[111b9b9]241 irq_spinlock_lock(&wq->lock, false);
242
243 if (!link_in_use(&THREAD->wq_link)) {
244 /*
245 * We were woken up by the desired event. Return success,
246 * regardless of any concurrent timeout or interruption.
247 */
248 rc = EOK;
249 goto exit;
[116d1ef4]250 }
[111b9b9]251
252 if (timed_out) {
253 rc = ETIMEOUT;
254 goto exit;
[f761f1eb]255 }
[a35b458]256
[111b9b9]257 /* Interrupted for some other reason. */
[f761f1eb]258 }
[a35b458]259
[111b9b9]260exit:
261 if (THREAD)
262 list_remove(&THREAD->wq_link);
[a35b458]263
[111b9b9]264 if (rc != EOK && sleep_composable)
265 wq->wakeup_balance--;
[a35b458]266
[111b9b9]267 if (THREAD)
268 atomic_store_explicit(&THREAD->sleep_queue, NULL, memory_order_relaxed);
[a35b458]269
[111b9b9]270 irq_spinlock_unlock(&wq->lock, false);
271 interrupts_restore(guard.ipl);
272 return rc;
[f761f1eb]273}
274
[111b9b9]275static void _wake_one(waitq_t *wq)
[f761f1eb]276{
[111b9b9]277 /* Pop one thread from the queue and wake it up. */
278 thread_t *thread = list_get_instance(list_first(&wq->sleepers), thread_t, wq_link);
279 list_remove(&thread->wq_link);
280 thread_wakeup(thread);
[f761f1eb]281}
282
[111b9b9]283/**
284 * Meant for implementing condvar signal.
285 * Always wakes one thread if there are any sleeping,
286 * has no effect if no threads are waiting for wakeup.
[3954961e]287 */
[111b9b9]288void waitq_signal(waitq_t *wq)
[3954961e]289{
[111b9b9]290 irq_spinlock_lock(&wq->lock, true);
[a35b458]291
[111b9b9]292 if (!list_empty(&wq->sleepers))
293 _wake_one(wq);
294
295 irq_spinlock_unlock(&wq->lock, true);
[3954961e]296}
297
[111b9b9]298/**
299 * Wakes up one thread sleeping on this waitq.
300 * If there are no threads waiting, saves the wakeup so that the next sleep
301 * returns immediately. If a previous failure in sleep created a wakeup debt
302 * (see SYNCH_FLAGS_FUTEX) this debt is annulled and no thread is woken up.
[f761f1eb]303 */
[111b9b9]304void waitq_wake_one(waitq_t *wq)
[f761f1eb]305{
[111b9b9]306 irq_spinlock_lock(&wq->lock, true);
[a35b458]307
[111b9b9]308 if (!wq->closed) {
309 if (wq->wakeup_balance < 0 || list_empty(&wq->sleepers))
310 wq->wakeup_balance++;
311 else
312 _wake_one(wq);
[f761f1eb]313 }
[a35b458]314
[111b9b9]315 irq_spinlock_unlock(&wq->lock, true);
316}
[a35b458]317
[111b9b9]318static void _wake_all(waitq_t *wq)
319{
320 while (!list_empty(&wq->sleepers))
321 _wake_one(wq);
322}
[a35b458]323
[111b9b9]324/**
325 * Wakes up all threads currently waiting on this waitq
326 * and makes all future sleeps return instantly.
327 */
328void waitq_close(waitq_t *wq)
329{
330 irq_spinlock_lock(&wq->lock, true);
331 wq->wakeup_balance = 0;
332 wq->closed = true;
333 _wake_all(wq);
334 irq_spinlock_unlock(&wq->lock, true);
335}
[a35b458]336
[111b9b9]337/**
338 * Wakes up all threads currently waiting on this waitq
339 */
340void waitq_wake_all(waitq_t *wq)
341{
342 irq_spinlock_lock(&wq->lock, true);
343 wq->wakeup_balance = 0;
344 _wake_all(wq);
345 irq_spinlock_unlock(&wq->lock, true);
[f761f1eb]346}
[b45c443]347
[cc73a8a1]348/** @}
[b45c443]349 */
Note: See TracBrowser for help on using the repository browser.