source: mainline/src/synch/waitq.c@ 22f7769

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

Rename cpu_priority_{high|low|restore|read} functions to interrupts_{disable|enable|restore|read}.
Rename pri_t to ipl_t (Interrupt Priority Level).
Rename thread_t::pri to thread_t::priority.

  • Property mode set to 100644
File size: 7.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#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 in microseconds.
112 * @param nonblocking Blocking vs. non-blocking operation mode switch.
113 *
114 * If usec is greater than zero, regardless of the value of nonblocking,
115 * the call will not return until either timeout or wakeup comes.
116 *
117 * If usec is zero and nonblocking is zero (false), the call
118 * will not return until wakeup comes.
119 *
120 * If usec is zero and nonblocking is non-zero (true), the call will
121 * immediately return, reporting either success or failure.
122 *
123 * @return Returns one of: ESYNCH_WOULD_BLOCK, ESYNCH_TIMEOUT,
124 * ESYNCH_OK_ATOMIC, ESYNCH_OK_BLOCKED.
125 *
126 * ESYNCH_WOULD_BLOCK means that the sleep failed because at the time
127 * of the call there was no pending wakeup.
128 *
129 * ESYNCH_TIMEOUT means that the sleep timed out.
130 *
131 * ESYNCH_OK_ATOMIC means that the sleep succeeded and that there was
132 * a pending wakeup at the time of the call. The caller was not put
133 * asleep at all.
134 *
135 * ESYNCH_OK_BLOCKED means that the sleep succeeded; the full sleep was
136 * attempted.
137 */
138int waitq_sleep_timeout(waitq_t *wq, __u32 usec, int nonblocking)
139{
140 volatile ipl_t ipl; /* must be live after context_restore() */
141
142
143restart:
144 ipl = interrupts_disable();
145
146 /*
147 * Busy waiting for a delayed timeout.
148 * This is an important fix for the race condition between
149 * a delayed timeout and a next call to waitq_sleep_timeout().
150 * Simply, the thread is not allowed to go to sleep if
151 * there are timeouts in progress.
152 */
153 spinlock_lock(&THREAD->lock);
154 if (THREAD->timeout_pending) {
155 spinlock_unlock(&THREAD->lock);
156 interrupts_restore(ipl);
157 goto restart;
158 }
159 spinlock_unlock(&THREAD->lock);
160
161 spinlock_lock(&wq->lock);
162
163 /* checks whether to go to sleep at all */
164 if (wq->missed_wakeups) {
165 wq->missed_wakeups--;
166 spinlock_unlock(&wq->lock);
167 interrupts_restore(ipl);
168 return ESYNCH_OK_ATOMIC;
169 }
170 else {
171 if (nonblocking && (usec == 0)) {
172 /* return immediatelly instead of going to sleep */
173 spinlock_unlock(&wq->lock);
174 interrupts_restore(ipl);
175 return ESYNCH_WOULD_BLOCK;
176 }
177 }
178
179
180 /*
181 * Now we are firmly decided to go to sleep.
182 */
183 spinlock_lock(&THREAD->lock);
184 if (usec) {
185 /* We use the timeout variant. */
186 if (!context_save(&THREAD->sleep_timeout_context)) {
187 /*
188 * Short emulation of scheduler() return code.
189 */
190 before_thread_runs();
191 spinlock_unlock(&THREAD->lock);
192 interrupts_restore(ipl);
193 return ESYNCH_TIMEOUT;
194 }
195 THREAD->timeout_pending = 1;
196 timeout_register(&THREAD->sleep_timeout, (__u64) usec, waitq_interrupted_sleep, THREAD);
197 }
198
199 list_append(&THREAD->wq_link, &wq->head);
200
201 /*
202 * Suspend execution.
203 */
204 THREAD->state = Sleeping;
205 THREAD->sleep_queue = wq;
206
207 spinlock_unlock(&THREAD->lock);
208
209 scheduler(); /* wq->lock is released in scheduler_separated_stack() */
210 interrupts_restore(ipl);
211
212 return ESYNCH_OK_BLOCKED;
213}
214
215
216/** Wake up first thread sleeping in a wait queue
217 *
218 * Wake up first thread sleeping in a wait queue.
219 * This is the SMP- and IRQ-safe wrapper meant for
220 * general use.
221 *
222 * Besides its 'normal' wakeup operation, it attempts
223 * to unregister possible timeout.
224 *
225 * @param wq Pointer to wait queue.
226 * @param all If this is non-zero, all sleeping threads
227 * will be woken up and missed count will be zeroed.
228 */
229void waitq_wakeup(waitq_t *wq, int all)
230{
231 ipl_t ipl;
232
233 ipl = interrupts_disable();
234 spinlock_lock(&wq->lock);
235
236 _waitq_wakeup_unsafe(wq, all);
237
238 spinlock_unlock(&wq->lock);
239 interrupts_restore(ipl);
240}
241
242/** Internal SMP- and IRQ-unsafe version of waitq_wakeup()
243 *
244 * This is the internal SMP- and IRQ-unsafe version
245 * of waitq_wakeup(). It assumes wq->lock is already
246 * locked and interrupts are already disabled.
247 *
248 * @param wq Pointer to wait queue.
249 * @param all If this is non-zero, all sleeping threads
250 * will be woken up and missed count will be zeroed.
251 */
252void _waitq_wakeup_unsafe(waitq_t *wq, int all)
253{
254 thread_t *t;
255
256loop:
257 if (list_empty(&wq->head)) {
258 wq->missed_wakeups++;
259 if (all) wq->missed_wakeups = 0;
260 return;
261 }
262
263 t = list_get_instance(wq->head.next, thread_t, wq_link);
264
265 list_remove(&t->wq_link);
266 spinlock_lock(&t->lock);
267 if (t->timeout_pending && timeout_unregister(&t->sleep_timeout))
268 t->timeout_pending = 0;
269 t->sleep_queue = NULL;
270 spinlock_unlock(&t->lock);
271
272 thread_ready(t);
273
274 if (all) goto loop;
275}
Note: See TracBrowser for help on using the repository browser.