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

Last change on this file was 597fa24, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 months ago

Enable static initialization of kernel synchronization primitives

  • Property mode set to 100644
File size: 9.6 KB
Line 
1/*
2 * Copyright (c) 2001-2004 Jakub Jermar
3 * Copyright (c) 2022 Jiří Zárevúcky
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
30/** @addtogroup kernel_sync
31 * @{
32 */
33
34/**
35 * @file
36 * @brief Wait queue.
37 *
38 * Wait queue is the basic synchronization primitive upon which all
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.
44 *
45 */
46
47#include <assert.h>
48#include <errno.h>
49#include <synch/waitq.h>
50#include <synch/spinlock.h>
51#include <preemption.h>
52#include <proc/thread.h>
53#include <proc/scheduler.h>
54#include <arch/asm.h>
55#include <typedefs.h>
56#include <time/timeout.h>
57#include <arch.h>
58#include <context.h>
59#include <adt/list.h>
60#include <arch/cycle.h>
61#include <memw.h>
62
63/** Initialize wait queue
64 *
65 * Initialize wait queue.
66 *
67 * @param wq Pointer to wait queue to be initialized.
68 *
69 */
70void waitq_initialize(waitq_t *wq)
71{
72 *wq = WAITQ_INITIALIZER(*wq);
73}
74
75/**
76 * Initialize wait queue with an initial number of queued wakeups
77 * (or a wakeup debt if negative).
78 */
79void waitq_initialize_with_count(waitq_t *wq, int count)
80{
81 *wq = WAITQ_INITIALIZER_WITH_COUNT(*wq, count);
82}
83
84#define PARAM_NON_BLOCKING(flags, usec) \
85 (((flags) & SYNCH_FLAGS_NON_BLOCKING) && ((usec) == 0))
86
87errno_t waitq_sleep(waitq_t *wq)
88{
89 return _waitq_sleep_timeout(wq, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
90}
91
92errno_t waitq_sleep_timeout(waitq_t *wq, uint32_t usec)
93{
94 return _waitq_sleep_timeout(wq, usec, SYNCH_FLAGS_NON_BLOCKING);
95}
96
97/** Sleep until either wakeup, timeout or interruption occurs
98 *
99 * Sleepers are organised in a FIFO fashion in a structure called wait queue.
100 *
101 * Other functions as waitq_sleep() and all the *_timeout() functions are
102 * implemented using this function.
103 *
104 * @param wq Pointer to wait queue.
105 * @param usec Timeout in microseconds.
106 * @param flags Specify mode of the sleep.
107 *
108 * The sleep can be interrupted only if the
109 * SYNCH_FLAGS_INTERRUPTIBLE bit is specified in flags.
110 *
111 * If usec is greater than zero, regardless of the value of the
112 * SYNCH_FLAGS_NON_BLOCKING bit in flags, the call will not return until either
113 * timeout, interruption or wakeup comes.
114 *
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.
117 *
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.
120 *
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.
124 * @return EOK, meaning that none of the above conditions occured, and the
125 * thread was woken up successfuly by `waitq_wake_*()`.
126 *
127 */
128errno_t _waitq_sleep_timeout(waitq_t *wq, uint32_t usec, unsigned int flags)
129{
130 assert((!PREEMPTION_DISABLED) || (PARAM_NON_BLOCKING(flags, usec)));
131 return waitq_sleep_timeout_unsafe(wq, usec, flags, waitq_sleep_prepare(wq));
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 *
139 * @param wq Wait queue.
140 *
141 * @return Interrupt level as it existed on entry to this function.
142 *
143 */
144wait_guard_t waitq_sleep_prepare(waitq_t *wq)
145{
146 ipl_t ipl = interrupts_disable();
147 irq_spinlock_lock(&wq->lock, false);
148 return (wait_guard_t) {
149 .ipl = ipl,
150 };
151}
152
153errno_t waitq_sleep_unsafe(waitq_t *wq, wait_guard_t guard)
154{
155 return waitq_sleep_timeout_unsafe(wq, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE, guard);
156}
157
158/** Internal implementation of waitq_sleep_timeout().
159 *
160 * This function implements logic of sleeping in a wait queue.
161 * This call must be preceded by a call to waitq_sleep_prepare().
162 *
163 * @param wq See waitq_sleep_timeout().
164 * @param usec See waitq_sleep_timeout().
165 * @param flags See waitq_sleep_timeout().
166 *
167 * @param[out] blocked See waitq_sleep_timeout().
168 *
169 * @return See waitq_sleep_timeout().
170 *
171 */
172errno_t waitq_sleep_timeout_unsafe(waitq_t *wq, uint32_t usec, unsigned int flags, wait_guard_t guard)
173{
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 }
188
189 /* Checks whether to go to sleep at all */
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;
201 }
202
203 /* Just for debugging output. */
204 atomic_store_explicit(&THREAD->sleep_queue, wq, memory_order_relaxed);
205
206 /*
207 * This thread_t field is synchronized exclusively via
208 * waitq lock of the waitq currently listing it.
209 */
210 list_append(&THREAD->wq_link, &wq->sleepers);
211
212 /* Needs to be run when interrupts are still disabled. */
213 deadline_t deadline = usec > 0 ?
214 timeout_deadline_in_usec(usec) : DEADLINE_NEVER;
215
216 while (true) {
217 bool terminating = (thread_wait_start() == THREAD_TERMINATING);
218 if (terminating && interruptible) {
219 rc = EINTR;
220 goto exit;
221 }
222
223 irq_spinlock_unlock(&wq->lock, false);
224
225 bool timed_out = (thread_wait_finish(deadline) == THREAD_WAIT_TIMEOUT);
226
227 /*
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.
237 */
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;
247 }
248
249 if (timed_out) {
250 rc = ETIMEOUT;
251 goto exit;
252 }
253
254 /* Interrupted for some other reason. */
255 }
256
257exit:
258 if (THREAD)
259 list_remove(&THREAD->wq_link);
260
261 if (rc != EOK && sleep_composable)
262 wq->wakeup_balance--;
263
264 if (THREAD)
265 atomic_store_explicit(&THREAD->sleep_queue, NULL, memory_order_relaxed);
266
267 irq_spinlock_unlock(&wq->lock, false);
268 interrupts_restore(guard.ipl);
269 return rc;
270}
271
272static void _wake_one(waitq_t *wq)
273{
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);
278}
279
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.
284 */
285void waitq_signal(waitq_t *wq)
286{
287 irq_spinlock_lock(&wq->lock, true);
288
289 if (!list_empty(&wq->sleepers))
290 _wake_one(wq);
291
292 irq_spinlock_unlock(&wq->lock, true);
293}
294
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.
300 */
301void waitq_wake_one(waitq_t *wq)
302{
303 irq_spinlock_lock(&wq->lock, true);
304
305 if (!wq->closed) {
306 if (wq->wakeup_balance < 0 || list_empty(&wq->sleepers))
307 wq->wakeup_balance++;
308 else
309 _wake_one(wq);
310 }
311
312 irq_spinlock_unlock(&wq->lock, true);
313}
314
315static void _wake_all(waitq_t *wq)
316{
317 while (!list_empty(&wq->sleepers))
318 _wake_one(wq);
319}
320
321/**
322 * Wakes up all threads currently waiting on this waitq
323 * and makes all future sleeps return instantly.
324 */
325void 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}
333
334/**
335 * Wakes up all threads currently waiting on this waitq
336 */
337void 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);
343}
344
345/** @}
346 */
Note: See TracBrowser for help on using the repository browser.