source: mainline/src/synch/waitq.c@ 922c7ce

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

Doxygen-style comments for waitq.c.

  • Property mode set to 100644
File size: 7.7 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#include <synch/waitq.h>
30#include <synch/synch.h>
31#include <synch/spinlock.h>
32#include <proc/thread.h>
33#include <arch/asm.h>
34#include <arch/types.h>
35#include <time/timeout.h>
36#include <arch.h>
37#include <context.h>
38#include <list.h>
39
40/** Initialize wait queue
41 *
42 * Initialize wait queue.
43 *
44 * @param wq Pointer to wait queue to be initialized.
45 */
46void waitq_initialize(waitq_t *wq)
47{
48 spinlock_initialize(&wq->lock);
49 list_initialize(&wq->head);
50 wq->missed_wakeups = 0;
51}
52
53/** Handle timeout during waitq_sleep_timeout() call
54 *
55 * This routine is called when waitq_sleep_timeout() timeouts.
56 * Interrupts are disabled.
57 *
58 * It is supposed to try to remove 'its' thread from the wait queue;
59 * it can eventually fail to achieve this goal when these two events
60 * overlap. In that case it behaves just as though there was no
61 * timeout at all.
62 *
63 * @param data Pointer to the thread that called waitq_sleep_timeout().
64 */
65void waitq_interrupted_sleep(void *data)
66{
67 thread_t *t = (thread_t *) data;
68 waitq_t *wq;
69 int do_wakeup = 0;
70
71 spinlock_lock(&threads_lock);
72 if (!list_member(&t->threads_link, &threads_head))
73 goto out;
74
75grab_locks:
76 spinlock_lock(&t->lock);
77 if (wq = t->sleep_queue) {
78 if (!spinlock_trylock(&wq->lock)) {
79 spinlock_unlock(&t->lock);
80 goto grab_locks; /* avoid deadlock */
81 }
82
83 list_remove(&t->wq_link);
84 t->saved_context = t->sleep_timeout_context;
85 do_wakeup = 1;
86
87 spinlock_unlock(&wq->lock);
88 t->sleep_queue = NULL;
89 }
90
91 t->timeout_pending = 0;
92 spinlock_unlock(&t->lock);
93
94 if (do_wakeup) thread_ready(t);
95
96out:
97 spinlock_unlock(&threads_lock);
98}
99
100/** Sleep until either wakeup or timeout occurs
101 *
102 * This is a sleep implementation which allows itself to be
103 * interrupted from the sleep, restoring a failover context.
104 *
105 * Sleepers are organised in FIFO fashion in a structure called wait queue.
106 *
107 * This function is really basic in that other functions as waitq_sleep()
108 * and all the *_timeout() functions use it.
109 *
110 * @param wq Pointer to wait queue.
111 * @param usec Timeout value in microseconds.
112 * @param nonblocking Controls whether only a conditional sleep
113 * (non-blocking sleep) is called for when the usec argument is 0.
114 *
115 * Relation between 'usec' and 'nonblocking' is described by this table:
116 *
117 * usec | nonblocking | what happens if there is no missed_wakeup
118 * -----+-------------+--------------------------------------------
119 * 0 | 0 | blocks without timeout until wakeup
120 * 0 | <> 0 | immediately returns ESYNCH_WOULD_BLOCK
121 * > 0 | x | blocks with timeout until timeout or wakeup
122 *
123 * @return Returns one of: ESYNCH_WOULD_BLOCK, ESYNCH_TIMEOUT,
124 * ESYNCH_OK_ATOMIC, ESYNCH_OK_BLOCKED.
125 *
126 * Meaning of the return values is described by the following chart:
127 *
128 * ESYNCH_WOULD_BLOCK Sleep failed because at the time of the call,
129 * there was no pending wakeup.
130 * ESYNCH_TIMEOUT Sleep timed out.
131 * ESYNCH_OK_ATOMIC Sleep succeeded; at the time of the call,
132 * where was a pending wakeup.
133 * ESYNCH_OK_BLOCKED Sleep succeeded; the full sleep was attempted.
134 */
135int waitq_sleep_timeout(waitq_t *wq, __u32 usec, int nonblocking)
136{
137 volatile pri_t pri; /* must be live after context_restore() */
138
139
140restart:
141 pri = cpu_priority_high();
142
143 /*
144 * Busy waiting for a delayed timeout.
145 * This is an important fix for the race condition between
146 * a delayed timeout and a next call to waitq_sleep_timeout().
147 * Simply, the thread is not allowed to go to sleep if
148 * there are timeouts in progress.
149 */
150 spinlock_lock(&THREAD->lock);
151 if (THREAD->timeout_pending) {
152 spinlock_unlock(&THREAD->lock);
153 cpu_priority_restore(pri);
154 goto restart;
155 }
156 spinlock_unlock(&THREAD->lock);
157
158 spinlock_lock(&wq->lock);
159
160 /* checks whether to go to sleep at all */
161 if (wq->missed_wakeups) {
162 wq->missed_wakeups--;
163 spinlock_unlock(&wq->lock);
164 cpu_priority_restore(pri);
165 return ESYNCH_OK_ATOMIC;
166 }
167 else {
168 if (nonblocking && (usec == 0)) {
169 /* return immediatelly instead of going to sleep */
170 spinlock_unlock(&wq->lock);
171 cpu_priority_restore(pri);
172 return ESYNCH_WOULD_BLOCK;
173 }
174 }
175
176
177 /*
178 * Now we are firmly decided to go to sleep.
179 */
180 spinlock_lock(&THREAD->lock);
181 if (usec) {
182 /* We use the timeout variant. */
183 if (!context_save(&THREAD->sleep_timeout_context)) {
184 /*
185 * Short emulation of scheduler() return code.
186 */
187 before_thread_runs();
188 spinlock_unlock(&THREAD->lock);
189 cpu_priority_restore(pri);
190 return ESYNCH_TIMEOUT;
191 }
192 THREAD->timeout_pending = 1;
193 timeout_register(&THREAD->sleep_timeout, (__u64) usec, waitq_interrupted_sleep, THREAD);
194 }
195
196 list_append(&THREAD->wq_link, &wq->head);
197
198 /*
199 * Suspend execution.
200 */
201 THREAD->state = Sleeping;
202 THREAD->sleep_queue = wq;
203
204 spinlock_unlock(&THREAD->lock);
205
206 scheduler(); /* wq->lock is released in scheduler_separated_stack() */
207 cpu_priority_restore(pri);
208
209 return ESYNCH_OK_BLOCKED;
210}
211
212
213/** Wake up first thread sleeping in a wait queue
214 *
215 * Wake up first thread sleeping in a wait queue.
216 * This is the SMP- and IRQ-safe wrapper meant for
217 * general use.
218 *
219 * Besides its 'normal' wakeup operation, it attempts
220 * to unregister possible timeout.
221 *
222 * @param wq Pointer to wait queue.
223 * @param all If this is non-zero, all sleeping threads
224 * will be woken up and missed count will be zeroed.
225 */
226void waitq_wakeup(waitq_t *wq, int all)
227{
228 pri_t pri;
229
230 pri = cpu_priority_high();
231 spinlock_lock(&wq->lock);
232
233 _waitq_wakeup_unsafe(wq, all);
234
235 spinlock_unlock(&wq->lock);
236 cpu_priority_restore(pri);
237}
238
239/** Internal SMP- and IRQ-unsafe version of waitq_wakeup()
240 *
241 * This is the internal SMP- and IRQ-unsafe version
242 * of waitq_wakeup(). It assumes wq->lock is already
243 * locked and interrupts are already disabled.
244 *
245 * @param wq Pointer to wait queue.
246 * @param all If this is non-zero, all sleeping threads
247 * will be woken up and missed count will be zeroed.
248 */
249void _waitq_wakeup_unsafe(waitq_t *wq, int all)
250{
251 thread_t *t;
252
253loop:
254 if (list_empty(&wq->head)) {
255 wq->missed_wakeups++;
256 if (all) wq->missed_wakeups = 0;
257 return;
258 }
259
260 t = list_get_instance(wq->head.next, thread_t, wq_link);
261
262 list_remove(&t->wq_link);
263 spinlock_lock(&t->lock);
264 if (t->timeout_pending && timeout_unregister(&t->sleep_timeout))
265 t->timeout_pending = 0;
266 t->sleep_queue = NULL;
267 spinlock_unlock(&t->lock);
268
269 thread_ready(t);
270
271 if (all) goto loop;
272}
Note: See TracBrowser for help on using the repository browser.