source: mainline/generic/src/synch/waitq.c@ 0182a665

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

Avoid tricky race condition between waitq_wakeup() and the pair
of waitq_timeouted_sleep() and waitq_interrupt_sleep().
Mutual exclusion != Synchronization.

  • Property mode set to 100644
File size: 12.0 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 t->sleep_queue = NULL;
[4b74488]103 spinlock_unlock(&wq->lock);
[f761f1eb]104 }
105
[05e2a7ad]106 t->timeout_pending = false;
[f761f1eb]107 spinlock_unlock(&t->lock);
108
[05e2a7ad]109 if (do_wakeup)
110 thread_ready(t);
[f761f1eb]111
112out:
113 spinlock_unlock(&threads_lock);
114}
115
[203f4c3]116/** Interrupt sleeping thread.
117 *
118 * This routine attempts to interrupt a thread from its sleep in a waitqueue.
119 * If the thread is not found sleeping, no action is taken.
120 *
121 * @param t Thread to be interrupted.
122 */
123void waitq_interrupt_sleep(thread_t *t)
124{
125 waitq_t *wq;
126 bool do_wakeup = false;
127 ipl_t ipl;
128
129 ipl = interrupts_disable();
130 spinlock_lock(&threads_lock);
[016acbe]131 if (!thread_exists(t))
[203f4c3]132 goto out;
133
134grab_locks:
135 spinlock_lock(&t->lock);
136 if ((wq = t->sleep_queue)) { /* assignment */
[116d1ef4]137 if (!(t->sleep_interruptible)) {
138 /*
139 * The sleep cannot be interrupted.
140 */
141 spinlock_unlock(&t->lock);
142 goto out;
143 }
144
[203f4c3]145 if (!spinlock_trylock(&wq->lock)) {
146 spinlock_unlock(&t->lock);
147 goto grab_locks; /* avoid deadlock */
148 }
149
[c74804f]150 if (t->timeout_pending && timeout_unregister(&t->sleep_timeout))
151 t->timeout_pending = false;
152
[203f4c3]153 list_remove(&t->wq_link);
154 t->saved_context = t->sleep_interruption_context;
155 do_wakeup = true;
156 t->sleep_queue = NULL;
[4b74488]157 spinlock_unlock(&wq->lock);
[203f4c3]158 }
159 spinlock_unlock(&t->lock);
160
161 if (do_wakeup)
162 thread_ready(t);
163
164out:
165 spinlock_unlock(&threads_lock);
166 interrupts_restore(ipl);
167}
168
169/** Sleep until either wakeup, timeout or interruption occurs
[922c7ce]170 *
[116d1ef4]171 * This is a sleep implementation which allows itself to time out or to be
[f761f1eb]172 * interrupted from the sleep, restoring a failover context.
173 *
[c0bc189]174 * Sleepers are organised in a FIFO fashion in a structure called wait queue.
[922c7ce]175 *
[f761f1eb]176 * This function is really basic in that other functions as waitq_sleep()
177 * and all the *_timeout() functions use it.
178 *
[922c7ce]179 * @param wq Pointer to wait queue.
[a783ca4]180 * @param usec Timeout in microseconds.
[116d1ef4]181 * @param flags Specify mode of the sleep.
[922c7ce]182 *
[116d1ef4]183 * The sleep can be interrupted only if the
184 * SYNCH_FLAGS_INTERRUPTIBLE bit is specified in flags.
185
186 * If usec is greater than zero, regardless of the value of the
187 * SYNCH_FLAGS_NON_BLOCKING bit in flags, the call will not return until either timeout,
188 * interruption or wakeup comes.
[f761f1eb]189 *
[116d1ef4]190 * If usec is zero and the SYNCH_FLAGS_NON_BLOCKING bit is not set in flags, the call
191 * will not return until wakeup or interruption comes.
[a783ca4]192 *
[116d1ef4]193 * If usec is zero and the SYNCH_FLAGS_NON_BLOCKING bit is set in flags, the call will
[a783ca4]194 * immediately return, reporting either success or failure.
[f761f1eb]195 *
[116d1ef4]196 * @return Returns one of: ESYNCH_WOULD_BLOCK, ESYNCH_TIMEOUT, ESYNCH_INTERRUPTED,
[9179d0a]197 * ESYNCH_OK_ATOMIC, ESYNCH_OK_BLOCKED.
[922c7ce]198 *
[9179d0a]199 * @li ESYNCH_WOULD_BLOCK means that the sleep failed because at the time
[a783ca4]200 * of the call there was no pending wakeup.
201 *
[9179d0a]202 * @li ESYNCH_TIMEOUT means that the sleep timed out.
[922c7ce]203 *
[9179d0a]204 * @li ESYNCH_INTERRUPTED means that somebody interrupted the sleeping thread.
[203f4c3]205 *
[9179d0a]206 * @li ESYNCH_OK_ATOMIC means that the sleep succeeded and that there was
[a783ca4]207 * a pending wakeup at the time of the call. The caller was not put
208 * asleep at all.
209 *
[9179d0a]210 * @li ESYNCH_OK_BLOCKED means that the sleep succeeded; the full sleep was
[a783ca4]211 * attempted.
[f761f1eb]212 */
[116d1ef4]213int waitq_sleep_timeout(waitq_t *wq, __u32 usec, int flags)
[f761f1eb]214{
[c0bc189]215 ipl_t ipl;
216 int rc;
[f761f1eb]217
[c0bc189]218 ipl = waitq_sleep_prepare(wq);
[116d1ef4]219 rc = waitq_sleep_timeout_unsafe(wq, usec, flags);
[c0bc189]220 waitq_sleep_finish(wq, rc, ipl);
221 return rc;
222}
223
224/** Prepare to sleep in a waitq.
225 *
226 * This function will return holding the lock of the wait queue
227 * and interrupts disabled.
228 *
229 * @param wq Wait queue.
230 *
231 * @return Interrupt level as it existed on entry to this function.
232 */
233ipl_t waitq_sleep_prepare(waitq_t *wq)
234{
235 ipl_t ipl;
[f761f1eb]236
237restart:
[22f7769]238 ipl = interrupts_disable();
[c0bc189]239
[343fc179]240 if (THREAD) { /* needed during system initiailzation */
241 /*
242 * Busy waiting for a delayed timeout.
243 * This is an important fix for the race condition between
244 * a delayed timeout and a next call to waitq_sleep_timeout().
245 * Simply, the thread is not allowed to go to sleep if
246 * there are timeouts in progress.
247 */
248 spinlock_lock(&THREAD->lock);
249 if (THREAD->timeout_pending) {
250 spinlock_unlock(&THREAD->lock);
251 interrupts_restore(ipl);
252 goto restart;
253 }
[43114c5]254 spinlock_unlock(&THREAD->lock);
[f761f1eb]255 }
[c0bc189]256
[f761f1eb]257 spinlock_lock(&wq->lock);
[c0bc189]258 return ipl;
259}
260
261/** Finish waiting in a wait queue.
262 *
263 * This function restores interrupts to the state that existed prior
264 * to the call to waitq_sleep_prepare(). If necessary, the wait queue
265 * lock is released.
266 *
267 * @param wq Wait queue.
268 * @param rc Return code of waitq_sleep_timeout_unsafe().
269 * @param ipl Interrupt level returned by waitq_sleep_prepare().
270 */
271void waitq_sleep_finish(waitq_t *wq, int rc, ipl_t ipl)
272{
273 switch (rc) {
274 case ESYNCH_WOULD_BLOCK:
275 case ESYNCH_OK_ATOMIC:
276 spinlock_unlock(&wq->lock);
277 break;
278 default:
279 break;
280 }
281 interrupts_restore(ipl);
282}
283
284/** Internal implementation of waitq_sleep_timeout().
285 *
286 * This function implements logic of sleeping in a wait queue.
287 * This call must be preceeded by a call to waitq_sleep_prepare()
288 * and followed by a call to waitq_slee_finish().
289 *
290 * @param wq See waitq_sleep_timeout().
291 * @param usec See waitq_sleep_timeout().
[116d1ef4]292 * @param flags See waitq_sleep_timeout().
[c0bc189]293 *
294 * @return See waitq_sleep_timeout().
295 */
[116d1ef4]296int waitq_sleep_timeout_unsafe(waitq_t *wq, __u32 usec, int flags)
[c0bc189]297{
[f761f1eb]298 /* checks whether to go to sleep at all */
299 if (wq->missed_wakeups) {
300 wq->missed_wakeups--;
301 return ESYNCH_OK_ATOMIC;
302 }
303 else {
[116d1ef4]304 if ((flags & SYNCH_FLAGS_NON_BLOCKING) && (usec == 0)) {
[f761f1eb]305 /* return immediatelly instead of going to sleep */
306 return ESYNCH_WOULD_BLOCK;
307 }
308 }
309
310 /*
311 * Now we are firmly decided to go to sleep.
312 */
[43114c5]313 spinlock_lock(&THREAD->lock);
[203f4c3]314
[116d1ef4]315 if (flags & SYNCH_FLAGS_INTERRUPTIBLE) {
[34dcd3f]316
317 /*
318 * If the thread was already interrupted,
319 * don't go to sleep at all.
320 */
321 if (THREAD->interrupted) {
322 spinlock_unlock(&THREAD->lock);
323 spinlock_unlock(&wq->lock);
324 return ESYNCH_INTERRUPTED;
325 }
326
[116d1ef4]327 /*
328 * Set context that will be restored if the sleep
329 * of this thread is ever interrupted.
330 */
331 THREAD->sleep_interruptible = true;
332 if (!context_save(&THREAD->sleep_interruption_context)) {
333 /* Short emulation of scheduler() return code. */
334 spinlock_unlock(&THREAD->lock);
335 return ESYNCH_INTERRUPTED;
336 }
[34dcd3f]337
[116d1ef4]338 } else {
339 THREAD->sleep_interruptible = false;
[203f4c3]340 }
341
[f761f1eb]342 if (usec) {
343 /* We use the timeout variant. */
[43114c5]344 if (!context_save(&THREAD->sleep_timeout_context)) {
[203f4c3]345 /* Short emulation of scheduler() return code. */
[43114c5]346 spinlock_unlock(&THREAD->lock);
[f761f1eb]347 return ESYNCH_TIMEOUT;
348 }
[05e2a7ad]349 THREAD->timeout_pending = true;
[203f4c3]350 timeout_register(&THREAD->sleep_timeout, (__u64) usec, waitq_timeouted_sleep, THREAD);
[f761f1eb]351 }
352
[43114c5]353 list_append(&THREAD->wq_link, &wq->head);
[f761f1eb]354
355 /*
356 * Suspend execution.
357 */
[43114c5]358 THREAD->state = Sleeping;
359 THREAD->sleep_queue = wq;
[f761f1eb]360
[43114c5]361 spinlock_unlock(&THREAD->lock);
[f761f1eb]362
363 scheduler(); /* wq->lock is released in scheduler_separated_stack() */
364
365 return ESYNCH_OK_BLOCKED;
366}
367
368
[922c7ce]369/** Wake up first thread sleeping in a wait queue
370 *
371 * Wake up first thread sleeping in a wait queue.
372 * This is the SMP- and IRQ-safe wrapper meant for
373 * general use.
374 *
375 * Besides its 'normal' wakeup operation, it attempts
376 * to unregister possible timeout.
377 *
378 * @param wq Pointer to wait queue.
379 * @param all If this is non-zero, all sleeping threads
380 * will be woken up and missed count will be zeroed.
[f761f1eb]381 */
[05e2a7ad]382void waitq_wakeup(waitq_t *wq, bool all)
[f761f1eb]383{
[22f7769]384 ipl_t ipl;
[f761f1eb]385
[22f7769]386 ipl = interrupts_disable();
[f761f1eb]387 spinlock_lock(&wq->lock);
388
389 _waitq_wakeup_unsafe(wq, all);
390
391 spinlock_unlock(&wq->lock);
[22f7769]392 interrupts_restore(ipl);
[f761f1eb]393}
394
[922c7ce]395/** Internal SMP- and IRQ-unsafe version of waitq_wakeup()
396 *
397 * This is the internal SMP- and IRQ-unsafe version
398 * of waitq_wakeup(). It assumes wq->lock is already
399 * locked and interrupts are already disabled.
400 *
401 * @param wq Pointer to wait queue.
402 * @param all If this is non-zero, all sleeping threads
403 * will be woken up and missed count will be zeroed.
[f761f1eb]404 */
[05e2a7ad]405void _waitq_wakeup_unsafe(waitq_t *wq, bool all)
[f761f1eb]406{
407 thread_t *t;
408
409loop:
410 if (list_empty(&wq->head)) {
411 wq->missed_wakeups++;
[05e2a7ad]412 if (all)
413 wq->missed_wakeups = 0;
[f761f1eb]414 return;
415 }
416
417 t = list_get_instance(wq->head.next, thread_t, wq_link);
418
[4b74488]419 /*
420 * Lock the thread prior to removing it from the wq.
421 * This is not necessary because of mutual exclusion
422 * (the link belongs to the wait queue), but because
423 * of synchronization with waitq_timeouted_sleep()
424 * and waitq_interrupt_sleep().
425 *
426 * In order for these two functions to work, the following
427 * invariant must hold:
428 *
429 * t->sleep_queue != NULL <=> t sleeps in a wait queue
430 *
431 * For an observer who locks the thread, the invariant
432 * holds only when the lock is held prior to removing
433 * it from the wait queue.
434 */
[f761f1eb]435 spinlock_lock(&t->lock);
[4b74488]436 list_remove(&t->wq_link);
437
[f761f1eb]438 if (t->timeout_pending && timeout_unregister(&t->sleep_timeout))
[05e2a7ad]439 t->timeout_pending = false;
[f761f1eb]440 t->sleep_queue = NULL;
441 spinlock_unlock(&t->lock);
442
443 thread_ready(t);
444
[05e2a7ad]445 if (all)
446 goto loop;
[f761f1eb]447}
Note: See TracBrowser for help on using the repository browser.