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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9306cd7 was 63e27ef, checked in by Jiri Svoboda <jiri@…>, 8 years ago

ASSERT → assert

  • Property mode set to 100644
File size: 16.6 KB
Line 
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
29/** @addtogroup sync
30 * @{
31 */
32
33/**
34 * @file
35 * @brief Wait queue.
36 *
37 * Wait queue is the basic synchronization primitive upon which all
38 * other synchronization primitives build.
39 *
40 * It allows threads to wait for an event in first-come, first-served
41 * fashion. Conditional operation as well as timeouts and interruptions
42 * are supported.
43 *
44 */
45
46#include <assert.h>
47#include <synch/waitq.h>
48#include <synch/spinlock.h>
49#include <proc/thread.h>
50#include <proc/scheduler.h>
51#include <arch/asm.h>
52#include <typedefs.h>
53#include <time/timeout.h>
54#include <arch.h>
55#include <context.h>
56#include <adt/list.h>
57#include <arch/cycle.h>
58
59static void waitq_sleep_timed_out(void *);
60static void waitq_complete_wakeup(waitq_t *);
61
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 irq_spinlock_initialize(&wq->lock, "wq.lock");
73 list_initialize(&wq->sleepers);
74 wq->missed_wakeups = 0;
75}
76
77/** Handle timeout during waitq_sleep_timeout() call
78 *
79 * This routine is called when waitq_sleep_timeout() times out.
80 * Interrupts are disabled.
81 *
82 * It is supposed to try to remove 'its' thread from the wait queue;
83 * it can eventually fail to achieve this goal when these two events
84 * overlap. In that case it behaves just as though there was no
85 * timeout at all.
86 *
87 * @param data Pointer to the thread that called waitq_sleep_timeout().
88 *
89 */
90void waitq_sleep_timed_out(void *data)
91{
92 thread_t *thread = (thread_t *) data;
93 bool do_wakeup = false;
94 DEADLOCK_PROBE_INIT(p_wqlock);
95
96 irq_spinlock_lock(&threads_lock, false);
97 if (!thread_exists(thread))
98 goto out;
99
100grab_locks:
101 irq_spinlock_lock(&thread->lock, false);
102
103 waitq_t *wq;
104 if ((wq = thread->sleep_queue)) { /* Assignment */
105 if (!irq_spinlock_trylock(&wq->lock)) {
106 irq_spinlock_unlock(&thread->lock, false);
107 DEADLOCK_PROBE(p_wqlock, DEADLOCK_THRESHOLD);
108 /* Avoid deadlock */
109 goto grab_locks;
110 }
111
112 list_remove(&thread->wq_link);
113 thread->saved_context = thread->sleep_timeout_context;
114 do_wakeup = true;
115 thread->sleep_queue = NULL;
116 irq_spinlock_unlock(&wq->lock, false);
117 }
118
119 thread->timeout_pending = false;
120 irq_spinlock_unlock(&thread->lock, false);
121
122 if (do_wakeup)
123 thread_ready(thread);
124
125out:
126 irq_spinlock_unlock(&threads_lock, false);
127}
128
129/** Interrupt sleeping thread.
130 *
131 * This routine attempts to interrupt a thread from its sleep in
132 * a waitqueue. If the thread is not found sleeping, no action
133 * is taken.
134 *
135 * The threads_lock must be already held and interrupts must be
136 * disabled upon calling this function.
137 *
138 * @param thread Thread to be interrupted.
139 *
140 */
141void waitq_interrupt_sleep(thread_t *thread)
142{
143 bool do_wakeup = false;
144 DEADLOCK_PROBE_INIT(p_wqlock);
145
146 /*
147 * The thread is quaranteed to exist because
148 * threads_lock is held.
149 */
150
151grab_locks:
152 irq_spinlock_lock(&thread->lock, false);
153
154 waitq_t *wq;
155 if ((wq = thread->sleep_queue)) { /* Assignment */
156 if (!(thread->sleep_interruptible)) {
157 /*
158 * The sleep cannot be interrupted.
159 */
160 irq_spinlock_unlock(&thread->lock, false);
161 return;
162 }
163
164 if (!irq_spinlock_trylock(&wq->lock)) {
165 /* Avoid deadlock */
166 irq_spinlock_unlock(&thread->lock, false);
167 DEADLOCK_PROBE(p_wqlock, DEADLOCK_THRESHOLD);
168 goto grab_locks;
169 }
170
171 if ((thread->timeout_pending) &&
172 (timeout_unregister(&thread->sleep_timeout)))
173 thread->timeout_pending = false;
174
175 list_remove(&thread->wq_link);
176 thread->saved_context = thread->sleep_interruption_context;
177 do_wakeup = true;
178 thread->sleep_queue = NULL;
179 irq_spinlock_unlock(&wq->lock, false);
180 }
181
182 irq_spinlock_unlock(&thread->lock, false);
183
184 if (do_wakeup)
185 thread_ready(thread);
186}
187
188/** Interrupt the first thread sleeping in the wait queue.
189 *
190 * Note that the caller somehow needs to know that the thread to be interrupted
191 * is sleeping interruptibly.
192 *
193 * @param wq Pointer to wait queue.
194 *
195 */
196void waitq_unsleep(waitq_t *wq)
197{
198 irq_spinlock_lock(&wq->lock, true);
199
200 if (!list_empty(&wq->sleepers)) {
201 thread_t *thread = list_get_instance(list_first(&wq->sleepers),
202 thread_t, wq_link);
203
204 irq_spinlock_lock(&thread->lock, false);
205
206 assert(thread->sleep_interruptible);
207
208 if ((thread->timeout_pending) &&
209 (timeout_unregister(&thread->sleep_timeout)))
210 thread->timeout_pending = false;
211
212 list_remove(&thread->wq_link);
213 thread->saved_context = thread->sleep_interruption_context;
214 thread->sleep_queue = NULL;
215
216 irq_spinlock_unlock(&thread->lock, false);
217 thread_ready(thread);
218 }
219
220 irq_spinlock_unlock(&wq->lock, true);
221}
222
223#define PARAM_NON_BLOCKING(flags, usec) \
224 (((flags) & SYNCH_FLAGS_NON_BLOCKING) && ((usec) == 0))
225
226/** Sleep until either wakeup, timeout or interruption occurs
227 *
228 * This is a sleep implementation which allows itself to time out or to be
229 * interrupted from the sleep, restoring a failover context.
230 *
231 * Sleepers are organised in a FIFO fashion in a structure called wait queue.
232 *
233 * This function is really basic in that other functions as waitq_sleep()
234 * and all the *_timeout() functions use it.
235 *
236 * @param wq Pointer to wait queue.
237 * @param usec Timeout in microseconds.
238 * @param flags Specify mode of the sleep.
239 *
240 * The sleep can be interrupted only if the
241 * SYNCH_FLAGS_INTERRUPTIBLE bit is specified in flags.
242 *
243 * If usec is greater than zero, regardless of the value of the
244 * SYNCH_FLAGS_NON_BLOCKING bit in flags, the call will not return until either
245 * timeout, interruption or wakeup comes.
246 *
247 * If usec is zero and the SYNCH_FLAGS_NON_BLOCKING bit is not set in flags,
248 * the call will not return until wakeup or interruption comes.
249 *
250 * If usec is zero and the SYNCH_FLAGS_NON_BLOCKING bit is set in flags, the
251 * call will immediately return, reporting either success or failure.
252 *
253 * @return ESYNCH_WOULD_BLOCK, meaning that the sleep failed because at the
254 * time of the call there was no pending wakeup
255 * @return ESYNCH_TIMEOUT, meaning that the sleep timed out.
256 * @return ESYNCH_INTERRUPTED, meaning that somebody interrupted the sleeping
257 * thread.
258 * @return ESYNCH_OK_ATOMIC, meaning that the sleep succeeded and that there
259 * was a pending wakeup at the time of the call. The caller was not put
260 * asleep at all.
261 * @return ESYNCH_OK_BLOCKED, meaning that the sleep succeeded; the full sleep
262 * was attempted.
263 *
264 */
265int waitq_sleep_timeout(waitq_t *wq, uint32_t usec, unsigned int flags)
266{
267 assert((!PREEMPTION_DISABLED) || (PARAM_NON_BLOCKING(flags, usec)));
268
269 ipl_t ipl = waitq_sleep_prepare(wq);
270 int rc = waitq_sleep_timeout_unsafe(wq, usec, flags);
271 waitq_sleep_finish(wq, rc, ipl);
272 return rc;
273}
274
275/** Prepare to sleep in a waitq.
276 *
277 * This function will return holding the lock of the wait queue
278 * and interrupts disabled.
279 *
280 * @param wq Wait queue.
281 *
282 * @return Interrupt level as it existed on entry to this function.
283 *
284 */
285ipl_t waitq_sleep_prepare(waitq_t *wq)
286{
287 ipl_t ipl;
288
289restart:
290 ipl = interrupts_disable();
291
292 if (THREAD) { /* Needed during system initiailzation */
293 /*
294 * Busy waiting for a delayed timeout.
295 * This is an important fix for the race condition between
296 * a delayed timeout and a next call to waitq_sleep_timeout().
297 * Simply, the thread is not allowed to go to sleep if
298 * there are timeouts in progress.
299 *
300 */
301 irq_spinlock_lock(&THREAD->lock, false);
302
303 if (THREAD->timeout_pending) {
304 irq_spinlock_unlock(&THREAD->lock, false);
305 interrupts_restore(ipl);
306 goto restart;
307 }
308
309 irq_spinlock_unlock(&THREAD->lock, false);
310 }
311
312 irq_spinlock_lock(&wq->lock, false);
313 return ipl;
314}
315
316/** Finish waiting in a wait queue.
317 *
318 * This function restores interrupts to the state that existed prior
319 * to the call to waitq_sleep_prepare(). If necessary, the wait queue
320 * lock is released.
321 *
322 * @param wq Wait queue.
323 * @param rc Return code of waitq_sleep_timeout_unsafe().
324 * @param ipl Interrupt level returned by waitq_sleep_prepare().
325 *
326 */
327void waitq_sleep_finish(waitq_t *wq, int rc, ipl_t ipl)
328{
329 switch (rc) {
330 case ESYNCH_WOULD_BLOCK:
331 case ESYNCH_OK_ATOMIC:
332 irq_spinlock_unlock(&wq->lock, false);
333 break;
334 default:
335 /*
336 * Wait for a waitq_wakeup() or waitq_unsleep() to complete
337 * before returning from waitq_sleep() to the caller. Otherwise
338 * the caller might expect that the wait queue is no longer used
339 * and deallocate it (although the wakeup on a another cpu has
340 * not yet completed and is using the wait queue).
341 *
342 * Note that we have to do this for ESYNCH_OK_BLOCKED and
343 * ESYNCH_INTERRUPTED, but not necessarily for ESYNCH_TIMEOUT
344 * where the timeout handler stops using the waitq before waking
345 * us up. To be on the safe side, ensure the waitq is not in use
346 * anymore in this case as well.
347 */
348 waitq_complete_wakeup(wq);
349 break;
350 }
351
352 interrupts_restore(ipl);
353}
354
355/** Internal implementation of waitq_sleep_timeout().
356 *
357 * This function implements logic of sleeping in a wait queue.
358 * This call must be preceded by a call to waitq_sleep_prepare()
359 * and followed by a call to waitq_sleep_finish().
360 *
361 * @param wq See waitq_sleep_timeout().
362 * @param usec See waitq_sleep_timeout().
363 * @param flags See waitq_sleep_timeout().
364 *
365 * @return See waitq_sleep_timeout().
366 *
367 */
368int waitq_sleep_timeout_unsafe(waitq_t *wq, uint32_t usec, unsigned int flags)
369{
370 /* Checks whether to go to sleep at all */
371 if (wq->missed_wakeups) {
372 wq->missed_wakeups--;
373 return ESYNCH_OK_ATOMIC;
374 } else {
375 if (PARAM_NON_BLOCKING(flags, usec)) {
376 /* Return immediately instead of going to sleep */
377 return ESYNCH_WOULD_BLOCK;
378 }
379 }
380
381 /*
382 * Now we are firmly decided to go to sleep.
383 *
384 */
385 irq_spinlock_lock(&THREAD->lock, false);
386
387 if (flags & SYNCH_FLAGS_INTERRUPTIBLE) {
388 /*
389 * If the thread was already interrupted,
390 * don't go to sleep at all.
391 */
392 if (THREAD->interrupted) {
393 irq_spinlock_unlock(&THREAD->lock, false);
394 irq_spinlock_unlock(&wq->lock, false);
395 return ESYNCH_INTERRUPTED;
396 }
397
398 /*
399 * Set context that will be restored if the sleep
400 * of this thread is ever interrupted.
401 */
402 THREAD->sleep_interruptible = true;
403 if (!context_save(&THREAD->sleep_interruption_context)) {
404 /* Short emulation of scheduler() return code. */
405 THREAD->last_cycle = get_cycle();
406 irq_spinlock_unlock(&THREAD->lock, false);
407 return ESYNCH_INTERRUPTED;
408 }
409 } else
410 THREAD->sleep_interruptible = false;
411
412 if (usec) {
413 /* We use the timeout variant. */
414 if (!context_save(&THREAD->sleep_timeout_context)) {
415 /* Short emulation of scheduler() return code. */
416 THREAD->last_cycle = get_cycle();
417 irq_spinlock_unlock(&THREAD->lock, false);
418 return ESYNCH_TIMEOUT;
419 }
420
421 THREAD->timeout_pending = true;
422 timeout_register(&THREAD->sleep_timeout, (uint64_t) usec,
423 waitq_sleep_timed_out, THREAD);
424 }
425
426 list_append(&THREAD->wq_link, &wq->sleepers);
427
428 /*
429 * Suspend execution.
430 *
431 */
432 THREAD->state = Sleeping;
433 THREAD->sleep_queue = wq;
434
435 irq_spinlock_unlock(&THREAD->lock, false);
436
437 /* wq->lock is released in scheduler_separated_stack() */
438 scheduler();
439
440 return ESYNCH_OK_BLOCKED;
441}
442
443/** Wake up first thread sleeping in a wait queue
444 *
445 * Wake up first thread sleeping in a wait queue. This is the SMP- and IRQ-safe
446 * wrapper meant for general use.
447 *
448 * Besides its 'normal' wakeup operation, it attempts to unregister possible
449 * timeout.
450 *
451 * @param wq Pointer to wait queue.
452 * @param mode Wakeup mode.
453 *
454 */
455void waitq_wakeup(waitq_t *wq, wakeup_mode_t mode)
456{
457 irq_spinlock_lock(&wq->lock, true);
458 _waitq_wakeup_unsafe(wq, mode);
459 irq_spinlock_unlock(&wq->lock, true);
460}
461
462/** If there is a wakeup in progress actively waits for it to complete.
463 *
464 * The function returns once the concurrently running waitq_wakeup()
465 * exits. It returns immediately if there are no concurrent wakeups
466 * at the time.
467 *
468 * Interrupts must be disabled.
469 *
470 * Example usage:
471 * @code
472 * void callback(waitq *wq)
473 * {
474 * // Do something and notify wait_for_completion() that we're done.
475 * waitq_wakeup(wq);
476 * }
477 * void wait_for_completion(void)
478 * {
479 * waitq wg;
480 * waitq_initialize(&wq);
481 * // Run callback() in the background, pass it wq.
482 * do_asynchronously(callback, &wq);
483 * // Wait for callback() to complete its work.
484 * waitq_sleep(&wq);
485 * // callback() completed its work, but it may still be accessing
486 * // wq in waitq_wakeup(). Therefore it is not yet safe to return
487 * // from waitq_sleep() or it would clobber up our stack (where wq
488 * // is stored). waitq_sleep() ensures the wait queue is no longer
489 * // in use by invoking waitq_complete_wakeup() internally.
490 *
491 * // waitq_sleep() returned, it is safe to free wq.
492 * }
493 * @endcode
494 *
495 * @param wq Pointer to a wait queue.
496 */
497static void waitq_complete_wakeup(waitq_t *wq)
498{
499 assert(interrupts_disabled());
500
501 irq_spinlock_lock(&wq->lock, false);
502 irq_spinlock_unlock(&wq->lock, false);
503}
504
505
506/** Internal SMP- and IRQ-unsafe version of waitq_wakeup()
507 *
508 * This is the internal SMP- and IRQ-unsafe version of waitq_wakeup(). It
509 * assumes wq->lock is already locked and interrupts are already disabled.
510 *
511 * @param wq Pointer to wait queue.
512 * @param mode If mode is WAKEUP_FIRST, then the longest waiting
513 * thread, if any, is woken up. If mode is WAKEUP_ALL, then
514 * all waiting threads, if any, are woken up. If there are
515 * no waiting threads to be woken up, the missed wakeup is
516 * recorded in the wait queue.
517 *
518 */
519void _waitq_wakeup_unsafe(waitq_t *wq, wakeup_mode_t mode)
520{
521 size_t count = 0;
522
523 assert(interrupts_disabled());
524 assert(irq_spinlock_locked(&wq->lock));
525
526loop:
527 if (list_empty(&wq->sleepers)) {
528 wq->missed_wakeups++;
529 if ((count) && (mode == WAKEUP_ALL))
530 wq->missed_wakeups--;
531
532 return;
533 }
534
535 count++;
536 thread_t *thread = list_get_instance(list_first(&wq->sleepers),
537 thread_t, wq_link);
538
539 /*
540 * Lock the thread prior to removing it from the wq.
541 * This is not necessary because of mutual exclusion
542 * (the link belongs to the wait queue), but because
543 * of synchronization with waitq_sleep_timed_out()
544 * and thread_interrupt_sleep().
545 *
546 * In order for these two functions to work, the following
547 * invariant must hold:
548 *
549 * thread->sleep_queue != NULL <=> thread sleeps in a wait queue
550 *
551 * For an observer who locks the thread, the invariant
552 * holds only when the lock is held prior to removing
553 * it from the wait queue.
554 *
555 */
556 irq_spinlock_lock(&thread->lock, false);
557 list_remove(&thread->wq_link);
558
559 if ((thread->timeout_pending) &&
560 (timeout_unregister(&thread->sleep_timeout)))
561 thread->timeout_pending = false;
562
563 thread->sleep_queue = NULL;
564 irq_spinlock_unlock(&thread->lock, false);
565
566 thread_ready(thread);
567
568 if (mode == WAKEUP_ALL)
569 goto loop;
570}
571
572/** Get the missed wakeups count.
573 *
574 * @param wq Pointer to wait queue.
575 * @return The wait queue's missed_wakeups count.
576 */
577int waitq_count_get(waitq_t *wq)
578{
579 int cnt;
580
581 irq_spinlock_lock(&wq->lock, true);
582 cnt = wq->missed_wakeups;
583 irq_spinlock_unlock(&wq->lock, true);
584
585 return cnt;
586}
587
588/** Set the missed wakeups count.
589 *
590 * @param wq Pointer to wait queue.
591 * @param val New value of the missed_wakeups count.
592 */
593void waitq_count_set(waitq_t *wq, int val)
594{
595 irq_spinlock_lock(&wq->lock, true);
596 wq->missed_wakeups = val;
597 irq_spinlock_unlock(&wq->lock, true);
598}
599
600/** @}
601 */
Note: See TracBrowser for help on using the repository browser.