source: mainline/generic/src/synch/waitq.c@ 116d1ef4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 116d1ef4 was 116d1ef4, checked in by Jakub Jermar <jakub@…>, 19 years ago

Replace nonblocking argument of waitq_sleep_timeout with flags that specify mode of operation.
Now a flag can be used to specify interruptible sleep.
Modify waitq_interrupt_sleep() to only interrupt threads that used this flag.
O

  • Property mode set to 100644
File size: 11.1 KB
RevLine 
[f761f1eb]1/*
2 * Copyright (C) 2001-2004 Jakub Jermar
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
[9179d0a]29/**
30 * @file waitq.c
31 * @brief Wait queue.
32 *
[e3c762cd]33 * Wait queue is the basic synchronization primitive upon which all
[9179d0a]34 * other synchronization primitives build.
35 *
36 * It allows threads to wait for an event in first-come, first-served
37 * fashion. Conditional operation as well as timeouts and interruptions
38 * are supported.
39 */
40
[f761f1eb]41#include <synch/waitq.h>
[922c7ce]42#include <synch/synch.h>
[f761f1eb]43#include <synch/spinlock.h>
[922c7ce]44#include <proc/thread.h>
[4b2c872d]45#include <proc/scheduler.h>
[f761f1eb]46#include <arch/asm.h>
47#include <arch/types.h>
[05e2a7ad]48#include <typedefs.h>
[922c7ce]49#include <time/timeout.h>
[f761f1eb]50#include <arch.h>
[922c7ce]51#include <context.h>
[5c9a08b]52#include <adt/list.h>
[f761f1eb]53
[203f4c3]54static void waitq_timeouted_sleep(void *data);
55
[922c7ce]56/** Initialize wait queue
57 *
58 * Initialize wait queue.
59 *
60 * @param wq Pointer to wait queue to be initialized.
61 */
[f761f1eb]62void waitq_initialize(waitq_t *wq)
63{
[2d93f1f9]64 spinlock_initialize(&wq->lock, "waitq_lock");
[f761f1eb]65 list_initialize(&wq->head);
66 wq->missed_wakeups = 0;
67}
68
[922c7ce]69/** Handle timeout during waitq_sleep_timeout() call
70 *
71 * This routine is called when waitq_sleep_timeout() timeouts.
72 * Interrupts are disabled.
[f761f1eb]73 *
[922c7ce]74 * It is supposed to try to remove 'its' thread from the wait queue;
75 * it can eventually fail to achieve this goal when these two events
76 * overlap. In that case it behaves just as though there was no
77 * timeout at all.
78 *
79 * @param data Pointer to the thread that called waitq_sleep_timeout().
[f761f1eb]80 */
[203f4c3]81void waitq_timeouted_sleep(void *data)
[f761f1eb]82{
83 thread_t *t = (thread_t *) data;
84 waitq_t *wq;
[05e2a7ad]85 bool do_wakeup = false;
[f761f1eb]86
87 spinlock_lock(&threads_lock);
[016acbe]88 if (!thread_exists(t))
[f761f1eb]89 goto out;
90
91grab_locks:
92 spinlock_lock(&t->lock);
[5a95b25]93 if ((wq = t->sleep_queue)) { /* assignment */
[f761f1eb]94 if (!spinlock_trylock(&wq->lock)) {
95 spinlock_unlock(&t->lock);
[05e2a7ad]96 goto grab_locks; /* avoid deadlock */
[f761f1eb]97 }
98
99 list_remove(&t->wq_link);
100 t->saved_context = t->sleep_timeout_context;
[05e2a7ad]101 do_wakeup = true;
[f761f1eb]102
103 spinlock_unlock(&wq->lock);
104 t->sleep_queue = NULL;
105 }
106
[05e2a7ad]107 t->timeout_pending = false;
[f761f1eb]108 spinlock_unlock(&t->lock);
109
[05e2a7ad]110 if (do_wakeup)
111 thread_ready(t);
[f761f1eb]112
113out:
114 spinlock_unlock(&threads_lock);
115}
116
[203f4c3]117/** Interrupt sleeping thread.
118 *
119 * This routine attempts to interrupt a thread from its sleep in a waitqueue.
120 * If the thread is not found sleeping, no action is taken.
121 *
122 * @param t Thread to be interrupted.
123 */
124void waitq_interrupt_sleep(thread_t *t)
125{
126 waitq_t *wq;
127 bool do_wakeup = false;
128 ipl_t ipl;
129
130 ipl = interrupts_disable();
131 spinlock_lock(&threads_lock);
[016acbe]132 if (!thread_exists(t))
[203f4c3]133 goto out;
134
135grab_locks:
136 spinlock_lock(&t->lock);
137 if ((wq = t->sleep_queue)) { /* assignment */
[116d1ef4]138 if (!(t->sleep_interruptible)) {
139 /*
140 * The sleep cannot be interrupted.
141 */
142 spinlock_unlock(&t->lock);
143 goto out;
144 }
145
[203f4c3]146 if (!spinlock_trylock(&wq->lock)) {
147 spinlock_unlock(&t->lock);
148 goto grab_locks; /* avoid deadlock */
149 }
150
151 list_remove(&t->wq_link);
152 t->saved_context = t->sleep_interruption_context;
153 do_wakeup = true;
154
155 spinlock_unlock(&wq->lock);
156 t->sleep_queue = NULL;
157 }
158 spinlock_unlock(&t->lock);
159
160 if (do_wakeup)
161 thread_ready(t);
162
163out:
164 spinlock_unlock(&threads_lock);
165 interrupts_restore(ipl);
166}
167
168/** Sleep until either wakeup, timeout or interruption occurs
[922c7ce]169 *
[116d1ef4]170 * This is a sleep implementation which allows itself to time out or to be
[f761f1eb]171 * interrupted from the sleep, restoring a failover context.
172 *
[c0bc189]173 * Sleepers are organised in a FIFO fashion in a structure called wait queue.
[922c7ce]174 *
[f761f1eb]175 * This function is really basic in that other functions as waitq_sleep()
176 * and all the *_timeout() functions use it.
177 *
[922c7ce]178 * @param wq Pointer to wait queue.
[a783ca4]179 * @param usec Timeout in microseconds.
[116d1ef4]180 * @param flags Specify mode of the sleep.
[922c7ce]181 *
[116d1ef4]182 * The sleep can be interrupted only if the
183 * SYNCH_FLAGS_INTERRUPTIBLE bit is specified in flags.
184
185 * If usec is greater than zero, regardless of the value of the
186 * SYNCH_FLAGS_NON_BLOCKING bit in flags, the call will not return until either timeout,
187 * interruption or wakeup comes.
[f761f1eb]188 *
[116d1ef4]189 * If usec is zero and the SYNCH_FLAGS_NON_BLOCKING bit is not set in flags, the call
190 * will not return until wakeup or interruption comes.
[a783ca4]191 *
[116d1ef4]192 * If usec is zero and the SYNCH_FLAGS_NON_BLOCKING bit is set in flags, the call will
[a783ca4]193 * immediately return, reporting either success or failure.
[f761f1eb]194 *
[116d1ef4]195 * @return Returns one of: ESYNCH_WOULD_BLOCK, ESYNCH_TIMEOUT, ESYNCH_INTERRUPTED,
[9179d0a]196 * ESYNCH_OK_ATOMIC, ESYNCH_OK_BLOCKED.
[922c7ce]197 *
[9179d0a]198 * @li ESYNCH_WOULD_BLOCK means that the sleep failed because at the time
[a783ca4]199 * of the call there was no pending wakeup.
200 *
[9179d0a]201 * @li ESYNCH_TIMEOUT means that the sleep timed out.
[922c7ce]202 *
[9179d0a]203 * @li ESYNCH_INTERRUPTED means that somebody interrupted the sleeping thread.
[203f4c3]204 *
[9179d0a]205 * @li ESYNCH_OK_ATOMIC means that the sleep succeeded and that there was
[a783ca4]206 * a pending wakeup at the time of the call. The caller was not put
207 * asleep at all.
208 *
[9179d0a]209 * @li ESYNCH_OK_BLOCKED means that the sleep succeeded; the full sleep was
[a783ca4]210 * attempted.
[f761f1eb]211 */
[116d1ef4]212int waitq_sleep_timeout(waitq_t *wq, __u32 usec, int flags)
[f761f1eb]213{
[c0bc189]214 ipl_t ipl;
215 int rc;
[f761f1eb]216
[c0bc189]217 ipl = waitq_sleep_prepare(wq);
[116d1ef4]218 rc = waitq_sleep_timeout_unsafe(wq, usec, flags);
[c0bc189]219 waitq_sleep_finish(wq, rc, ipl);
220 return rc;
221}
222
223/** Prepare to sleep in a waitq.
224 *
225 * This function will return holding the lock of the wait queue
226 * and interrupts disabled.
227 *
228 * @param wq Wait queue.
229 *
230 * @return Interrupt level as it existed on entry to this function.
231 */
232ipl_t waitq_sleep_prepare(waitq_t *wq)
233{
234 ipl_t ipl;
[f761f1eb]235
236restart:
[22f7769]237 ipl = interrupts_disable();
[c0bc189]238
[343fc179]239 if (THREAD) { /* needed during system initiailzation */
240 /*
241 * Busy waiting for a delayed timeout.
242 * This is an important fix for the race condition between
243 * a delayed timeout and a next call to waitq_sleep_timeout().
244 * Simply, the thread is not allowed to go to sleep if
245 * there are timeouts in progress.
246 */
247 spinlock_lock(&THREAD->lock);
248 if (THREAD->timeout_pending) {
249 spinlock_unlock(&THREAD->lock);
250 interrupts_restore(ipl);
251 goto restart;
252 }
[43114c5]253 spinlock_unlock(&THREAD->lock);
[f761f1eb]254 }
[c0bc189]255
[f761f1eb]256 spinlock_lock(&wq->lock);
[c0bc189]257 return ipl;
258}
259
260/** Finish waiting in a wait queue.
261 *
262 * This function restores interrupts to the state that existed prior
263 * to the call to waitq_sleep_prepare(). If necessary, the wait queue
264 * lock is released.
265 *
266 * @param wq Wait queue.
267 * @param rc Return code of waitq_sleep_timeout_unsafe().
268 * @param ipl Interrupt level returned by waitq_sleep_prepare().
269 */
270void waitq_sleep_finish(waitq_t *wq, int rc, ipl_t ipl)
271{
272 switch (rc) {
273 case ESYNCH_WOULD_BLOCK:
274 case ESYNCH_OK_ATOMIC:
275 spinlock_unlock(&wq->lock);
276 break;
277 default:
278 break;
279 }
280 interrupts_restore(ipl);
281}
282
283/** Internal implementation of waitq_sleep_timeout().
284 *
285 * This function implements logic of sleeping in a wait queue.
286 * This call must be preceeded by a call to waitq_sleep_prepare()
287 * and followed by a call to waitq_slee_finish().
288 *
289 * @param wq See waitq_sleep_timeout().
290 * @param usec See waitq_sleep_timeout().
[116d1ef4]291 * @param flags See waitq_sleep_timeout().
[c0bc189]292 *
293 * @return See waitq_sleep_timeout().
294 */
[116d1ef4]295int waitq_sleep_timeout_unsafe(waitq_t *wq, __u32 usec, int flags)
[c0bc189]296{
[f761f1eb]297 /* checks whether to go to sleep at all */
298 if (wq->missed_wakeups) {
299 wq->missed_wakeups--;
300 return ESYNCH_OK_ATOMIC;
301 }
302 else {
[116d1ef4]303 if ((flags & SYNCH_FLAGS_NON_BLOCKING) && (usec == 0)) {
[f761f1eb]304 /* return immediatelly instead of going to sleep */
305 return ESYNCH_WOULD_BLOCK;
306 }
307 }
308
309 /*
310 * Now we are firmly decided to go to sleep.
311 */
[43114c5]312 spinlock_lock(&THREAD->lock);
[203f4c3]313
[116d1ef4]314 if (flags & SYNCH_FLAGS_INTERRUPTIBLE) {
315 /*
316 * Set context that will be restored if the sleep
317 * of this thread is ever interrupted.
318 */
319 THREAD->sleep_interruptible = true;
320 if (!context_save(&THREAD->sleep_interruption_context)) {
321 /* Short emulation of scheduler() return code. */
322 spinlock_unlock(&THREAD->lock);
323 return ESYNCH_INTERRUPTED;
324 }
325 } else {
326 THREAD->sleep_interruptible = false;
[203f4c3]327 }
328
[f761f1eb]329 if (usec) {
330 /* We use the timeout variant. */
[43114c5]331 if (!context_save(&THREAD->sleep_timeout_context)) {
[203f4c3]332 /* Short emulation of scheduler() return code. */
[43114c5]333 spinlock_unlock(&THREAD->lock);
[f761f1eb]334 return ESYNCH_TIMEOUT;
335 }
[05e2a7ad]336 THREAD->timeout_pending = true;
[203f4c3]337 timeout_register(&THREAD->sleep_timeout, (__u64) usec, waitq_timeouted_sleep, THREAD);
[f761f1eb]338 }
339
[43114c5]340 list_append(&THREAD->wq_link, &wq->head);
[f761f1eb]341
342 /*
343 * Suspend execution.
344 */
[43114c5]345 THREAD->state = Sleeping;
346 THREAD->sleep_queue = wq;
[f761f1eb]347
[43114c5]348 spinlock_unlock(&THREAD->lock);
[f761f1eb]349
350 scheduler(); /* wq->lock is released in scheduler_separated_stack() */
351
352 return ESYNCH_OK_BLOCKED;
353}
354
355
[922c7ce]356/** Wake up first thread sleeping in a wait queue
357 *
358 * Wake up first thread sleeping in a wait queue.
359 * This is the SMP- and IRQ-safe wrapper meant for
360 * general use.
361 *
362 * Besides its 'normal' wakeup operation, it attempts
363 * to unregister possible timeout.
364 *
365 * @param wq Pointer to wait queue.
366 * @param all If this is non-zero, all sleeping threads
367 * will be woken up and missed count will be zeroed.
[f761f1eb]368 */
[05e2a7ad]369void waitq_wakeup(waitq_t *wq, bool all)
[f761f1eb]370{
[22f7769]371 ipl_t ipl;
[f761f1eb]372
[22f7769]373 ipl = interrupts_disable();
[f761f1eb]374 spinlock_lock(&wq->lock);
375
376 _waitq_wakeup_unsafe(wq, all);
377
378 spinlock_unlock(&wq->lock);
[22f7769]379 interrupts_restore(ipl);
[f761f1eb]380}
381
[922c7ce]382/** Internal SMP- and IRQ-unsafe version of waitq_wakeup()
383 *
384 * This is the internal SMP- and IRQ-unsafe version
385 * of waitq_wakeup(). It assumes wq->lock is already
386 * locked and interrupts are already disabled.
387 *
388 * @param wq Pointer to wait queue.
389 * @param all If this is non-zero, all sleeping threads
390 * will be woken up and missed count will be zeroed.
[f761f1eb]391 */
[05e2a7ad]392void _waitq_wakeup_unsafe(waitq_t *wq, bool all)
[f761f1eb]393{
394 thread_t *t;
395
396loop:
397 if (list_empty(&wq->head)) {
398 wq->missed_wakeups++;
[05e2a7ad]399 if (all)
400 wq->missed_wakeups = 0;
[f761f1eb]401 return;
402 }
403
404 t = list_get_instance(wq->head.next, thread_t, wq_link);
405
406 list_remove(&t->wq_link);
407 spinlock_lock(&t->lock);
408 if (t->timeout_pending && timeout_unregister(&t->sleep_timeout))
[05e2a7ad]409 t->timeout_pending = false;
[f761f1eb]410 t->sleep_queue = NULL;
411 spinlock_unlock(&t->lock);
412
413 thread_ready(t);
414
[05e2a7ad]415 if (all)
416 goto loop;
[f761f1eb]417}
Note: See TracBrowser for help on using the repository browser.