Rev 25 | Rev 404 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | jermar | 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 | |||
382 | jermar | 29 | #include <synch/waitq.h> |
1 | jermar | 30 | #include <synch/synch.h> |
31 | #include <synch/spinlock.h> |
||
382 | jermar | 32 | #include <proc/thread.h> |
1 | jermar | 33 | #include <arch/asm.h> |
34 | #include <arch/types.h> |
||
382 | jermar | 35 | #include <time/timeout.h> |
1 | jermar | 36 | #include <arch.h> |
382 | jermar | 37 | #include <context.h> |
1 | jermar | 38 | #include <list.h> |
39 | |||
382 | jermar | 40 | /** Initialize wait queue |
41 | * |
||
42 | * Initialize wait queue. |
||
43 | * |
||
44 | * @param wq Pointer to wait queue to be initialized. |
||
45 | */ |
||
1 | jermar | 46 | void waitq_initialize(waitq_t *wq) |
47 | { |
||
48 | spinlock_initialize(&wq->lock); |
||
49 | list_initialize(&wq->head); |
||
50 | wq->missed_wakeups = 0; |
||
51 | } |
||
52 | |||
382 | jermar | 53 | /** Handle timeout during waitq_sleep_timeout() call |
1 | jermar | 54 | * |
382 | jermar | 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(). |
||
1 | jermar | 64 | */ |
65 | void 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 | |||
75 | grab_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 | |||
96 | out: |
||
97 | spinlock_unlock(&threads_lock); |
||
98 | } |
||
99 | |||
382 | jermar | 100 | /** Sleep until either wakeup or timeout occurs |
101 | * |
||
1 | jermar | 102 | * This is a sleep implementation which allows itself to be |
103 | * interrupted from the sleep, restoring a failover context. |
||
104 | * |
||
382 | jermar | 105 | * Sleepers are organised in FIFO fashion in a structure called wait queue. |
106 | * |
||
1 | jermar | 107 | * This function is really basic in that other functions as waitq_sleep() |
108 | * and all the *_timeout() functions use it. |
||
109 | * |
||
382 | jermar | 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. |
||
1 | jermar | 114 | * |
382 | jermar | 115 | * Relation between 'usec' and 'nonblocking' is described by this table: |
116 | * |
||
1 | jermar | 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 | * |
||
382 | jermar | 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. |
||
1 | jermar | 134 | */ |
135 | int waitq_sleep_timeout(waitq_t *wq, __u32 usec, int nonblocking) |
||
136 | { |
||
137 | volatile pri_t pri; /* must be live after context_restore() */ |
||
138 | |||
139 | |||
140 | restart: |
||
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 | */ |
||
15 | jermar | 150 | spinlock_lock(&THREAD->lock); |
151 | if (THREAD->timeout_pending) { |
||
152 | spinlock_unlock(&THREAD->lock); |
||
1 | jermar | 153 | cpu_priority_restore(pri); |
154 | goto restart; |
||
155 | } |
||
15 | jermar | 156 | spinlock_unlock(&THREAD->lock); |
1 | jermar | 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 | */ |
||
15 | jermar | 180 | spinlock_lock(&THREAD->lock); |
1 | jermar | 181 | if (usec) { |
182 | /* We use the timeout variant. */ |
||
15 | jermar | 183 | if (!context_save(&THREAD->sleep_timeout_context)) { |
1 | jermar | 184 | /* |
185 | * Short emulation of scheduler() return code. |
||
186 | */ |
||
25 | jermar | 187 | before_thread_runs(); |
15 | jermar | 188 | spinlock_unlock(&THREAD->lock); |
1 | jermar | 189 | cpu_priority_restore(pri); |
190 | return ESYNCH_TIMEOUT; |
||
191 | } |
||
15 | jermar | 192 | THREAD->timeout_pending = 1; |
193 | timeout_register(&THREAD->sleep_timeout, (__u64) usec, waitq_interrupted_sleep, THREAD); |
||
1 | jermar | 194 | } |
195 | |||
15 | jermar | 196 | list_append(&THREAD->wq_link, &wq->head); |
1 | jermar | 197 | |
198 | /* |
||
199 | * Suspend execution. |
||
200 | */ |
||
15 | jermar | 201 | THREAD->state = Sleeping; |
202 | THREAD->sleep_queue = wq; |
||
1 | jermar | 203 | |
15 | jermar | 204 | spinlock_unlock(&THREAD->lock); |
1 | jermar | 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 | |||
382 | jermar | 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. |
||
1 | jermar | 225 | */ |
226 | void 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 | |||
382 | jermar | 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. |
||
1 | jermar | 248 | */ |
249 | void _waitq_wakeup_unsafe(waitq_t *wq, int all) |
||
250 | { |
||
251 | thread_t *t; |
||
252 | |||
253 | loop: |
||
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 | } |