Rev 25 | Rev 404 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 25 | Rev 382 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | #include <context.h> |
- | |
30 | #include <proc/thread.h> |
- | |
31 | - | ||
32 | #include <synch/synch.h> |
- | |
33 | #include <synch/waitq.h> |
29 | #include <synch/waitq.h> |
- | 30 | #include <synch/synch.h> |
|
34 | #include <synch/spinlock.h> |
31 | #include <synch/spinlock.h> |
35 | - | ||
- | 32 | #include <proc/thread.h> |
|
36 | #include <arch/asm.h> |
33 | #include <arch/asm.h> |
37 | #include <arch/types.h> |
34 | #include <arch/types.h> |
- | 35 | #include <time/timeout.h> |
|
38 | #include <arch.h> |
36 | #include <arch.h> |
39 | - | ||
- | 37 | #include <context.h> |
|
40 | #include <list.h> |
38 | #include <list.h> |
41 | 39 | ||
42 | #include <time/timeout.h> |
40 | /** Initialize wait queue |
43 | 41 | * |
|
- | 42 | * Initialize wait queue. |
|
- | 43 | * |
|
- | 44 | * @param wq Pointer to wait queue to be initialized. |
|
- | 45 | */ |
|
44 | void waitq_initialize(waitq_t *wq) |
46 | void waitq_initialize(waitq_t *wq) |
45 | { |
47 | { |
46 | spinlock_initialize(&wq->lock); |
48 | spinlock_initialize(&wq->lock); |
47 | list_initialize(&wq->head); |
49 | list_initialize(&wq->head); |
48 | wq->missed_wakeups = 0; |
50 | wq->missed_wakeups = 0; |
49 | } |
51 | } |
50 | 52 | ||
- | 53 | /** Handle timeout during waitq_sleep_timeout() call |
|
51 | /* |
54 | * |
52 | * Called with interrupts disabled from clock() when sleep_timeout |
55 | * This routine is called when waitq_sleep_timeout() timeouts. |
53 | * timeouts. This function is not allowed to enable interrupts. |
56 | * Interrupts are disabled. |
54 | * |
57 | * |
55 | * It is supposed to try to remove 'its' thread from the waitqueue; it |
58 | * It is supposed to try to remove 'its' thread from the wait queue; |
56 | * can eventually fail to achieve this goal when these two events |
59 | * it can eventually fail to achieve this goal when these two events |
57 | * overlap; in that case it behaves just as though there was no |
60 | * overlap. In that case it behaves just as though there was no |
58 | * timeout at all |
61 | * timeout at all. |
- | 62 | * |
|
- | 63 | * @param data Pointer to the thread that called waitq_sleep_timeout(). |
|
59 | */ |
64 | */ |
60 | void waitq_interrupted_sleep(void *data) |
65 | void waitq_interrupted_sleep(void *data) |
61 | { |
66 | { |
62 | thread_t *t = (thread_t *) data; |
67 | thread_t *t = (thread_t *) data; |
63 | waitq_t *wq; |
68 | waitq_t *wq; |
64 | int do_wakeup = 0; |
69 | int do_wakeup = 0; |
65 | 70 | ||
66 | spinlock_lock(&threads_lock); |
71 | spinlock_lock(&threads_lock); |
67 | if (!list_member(&t->threads_link, &threads_head)) |
72 | if (!list_member(&t->threads_link, &threads_head)) |
68 | goto out; |
73 | goto out; |
69 | 74 | ||
70 | grab_locks: |
75 | grab_locks: |
71 | spinlock_lock(&t->lock); |
76 | spinlock_lock(&t->lock); |
72 | if (wq = t->sleep_queue) { |
77 | if (wq = t->sleep_queue) { |
73 | if (!spinlock_trylock(&wq->lock)) { |
78 | if (!spinlock_trylock(&wq->lock)) { |
74 | spinlock_unlock(&t->lock); |
79 | spinlock_unlock(&t->lock); |
75 | goto grab_locks; /* avoid deadlock */ |
80 | goto grab_locks; /* avoid deadlock */ |
76 | } |
81 | } |
77 | 82 | ||
78 | list_remove(&t->wq_link); |
83 | list_remove(&t->wq_link); |
79 | t->saved_context = t->sleep_timeout_context; |
84 | t->saved_context = t->sleep_timeout_context; |
80 | do_wakeup = 1; |
85 | do_wakeup = 1; |
81 | 86 | ||
82 | spinlock_unlock(&wq->lock); |
87 | spinlock_unlock(&wq->lock); |
83 | t->sleep_queue = NULL; |
88 | t->sleep_queue = NULL; |
84 | } |
89 | } |
85 | 90 | ||
86 | t->timeout_pending = 0; |
91 | t->timeout_pending = 0; |
87 | spinlock_unlock(&t->lock); |
92 | spinlock_unlock(&t->lock); |
88 | 93 | ||
89 | if (do_wakeup) thread_ready(t); |
94 | if (do_wakeup) thread_ready(t); |
90 | 95 | ||
91 | out: |
96 | out: |
92 | spinlock_unlock(&threads_lock); |
97 | spinlock_unlock(&threads_lock); |
93 | } |
98 | } |
94 | 99 | ||
- | 100 | /** Sleep until either wakeup or timeout occurs |
|
95 | /* |
101 | * |
96 | * This is a sleep implementation which allows itself to be |
102 | * This is a sleep implementation which allows itself to be |
97 | * interrupted from the sleep, restoring a failover context. |
103 | * interrupted from the sleep, restoring a failover context. |
98 | * |
104 | * |
- | 105 | * Sleepers are organised in FIFO fashion in a structure called wait queue. |
|
- | 106 | * |
|
99 | * This function is really basic in that other functions as waitq_sleep() |
107 | * This function is really basic in that other functions as waitq_sleep() |
100 | * and all the *_timeout() functions use it. |
108 | * and all the *_timeout() functions use it. |
101 | * |
109 | * |
- | 110 | * @param wq Pointer to wait queue. |
|
- | 111 | * @param usec Timeout value in microseconds. |
|
102 | * The third argument controls whether only a conditional sleep |
112 | * @param nonblocking Controls whether only a conditional sleep |
103 | * (non-blocking sleep) is called for when the second argument is 0. |
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: |
|
104 | * |
116 | * |
105 | * usec | nonblocking | what happens if there is no missed_wakeup |
117 | * usec | nonblocking | what happens if there is no missed_wakeup |
106 | * -----+-------------+-------------------------------------------- |
118 | * -----+-------------+-------------------------------------------- |
107 | * 0 | 0 | blocks without timeout until wakeup |
119 | * 0 | 0 | blocks without timeout until wakeup |
108 | * 0 | <> 0 | immediately returns ESYNCH_WOULD_BLOCK |
120 | * 0 | <> 0 | immediately returns ESYNCH_WOULD_BLOCK |
109 | * > 0 | x | blocks with timeout until timeout or wakeup |
121 | * > 0 | x | blocks with timeout until timeout or wakeup |
110 | * |
122 | * |
111 | * return values: |
123 | * @return Returns one of: ESYNCH_WOULD_BLOCK, ESYNCH_TIMEOUT, |
112 | * ESYNCH_WOULD_BLOCK |
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. |
|
113 | * ESYNCH_TIMEOUT |
130 | * ESYNCH_TIMEOUT Sleep timed out. |
114 | * ESYNCH_OK_ATOMIC |
131 | * ESYNCH_OK_ATOMIC Sleep succeeded; at the time of the call, |
115 | * ESYNCH_OK_BLOCKED |
132 | * where was a pending wakeup. |
- | 133 | * ESYNCH_OK_BLOCKED Sleep succeeded; the full sleep was attempted. |
|
116 | */ |
134 | */ |
117 | int waitq_sleep_timeout(waitq_t *wq, __u32 usec, int nonblocking) |
135 | int waitq_sleep_timeout(waitq_t *wq, __u32 usec, int nonblocking) |
118 | { |
136 | { |
119 | volatile pri_t pri; /* must be live after context_restore() */ |
137 | volatile pri_t pri; /* must be live after context_restore() */ |
120 | 138 | ||
121 | 139 | ||
122 | restart: |
140 | restart: |
123 | pri = cpu_priority_high(); |
141 | pri = cpu_priority_high(); |
124 | 142 | ||
125 | /* |
143 | /* |
126 | * Busy waiting for a delayed timeout. |
144 | * Busy waiting for a delayed timeout. |
127 | * This is an important fix for the race condition between |
145 | * This is an important fix for the race condition between |
128 | * a delayed timeout and a next call to waitq_sleep_timeout(). |
146 | * a delayed timeout and a next call to waitq_sleep_timeout(). |
129 | * Simply, the thread is not allowed to go to sleep if |
147 | * Simply, the thread is not allowed to go to sleep if |
130 | * there are timeouts in progress. |
148 | * there are timeouts in progress. |
131 | */ |
149 | */ |
132 | spinlock_lock(&THREAD->lock); |
150 | spinlock_lock(&THREAD->lock); |
133 | if (THREAD->timeout_pending) { |
151 | if (THREAD->timeout_pending) { |
134 | spinlock_unlock(&THREAD->lock); |
152 | spinlock_unlock(&THREAD->lock); |
135 | cpu_priority_restore(pri); |
153 | cpu_priority_restore(pri); |
136 | goto restart; |
154 | goto restart; |
137 | } |
155 | } |
138 | spinlock_unlock(&THREAD->lock); |
156 | spinlock_unlock(&THREAD->lock); |
139 | 157 | ||
140 | spinlock_lock(&wq->lock); |
158 | spinlock_lock(&wq->lock); |
141 | 159 | ||
142 | /* checks whether to go to sleep at all */ |
160 | /* checks whether to go to sleep at all */ |
143 | if (wq->missed_wakeups) { |
161 | if (wq->missed_wakeups) { |
144 | wq->missed_wakeups--; |
162 | wq->missed_wakeups--; |
145 | spinlock_unlock(&wq->lock); |
163 | spinlock_unlock(&wq->lock); |
146 | cpu_priority_restore(pri); |
164 | cpu_priority_restore(pri); |
147 | return ESYNCH_OK_ATOMIC; |
165 | return ESYNCH_OK_ATOMIC; |
148 | } |
166 | } |
149 | else { |
167 | else { |
150 | if (nonblocking && (usec == 0)) { |
168 | if (nonblocking && (usec == 0)) { |
151 | /* return immediatelly instead of going to sleep */ |
169 | /* return immediatelly instead of going to sleep */ |
152 | spinlock_unlock(&wq->lock); |
170 | spinlock_unlock(&wq->lock); |
153 | cpu_priority_restore(pri); |
171 | cpu_priority_restore(pri); |
154 | return ESYNCH_WOULD_BLOCK; |
172 | return ESYNCH_WOULD_BLOCK; |
155 | } |
173 | } |
156 | } |
174 | } |
157 | 175 | ||
158 | 176 | ||
159 | /* |
177 | /* |
160 | * Now we are firmly decided to go to sleep. |
178 | * Now we are firmly decided to go to sleep. |
161 | */ |
179 | */ |
162 | spinlock_lock(&THREAD->lock); |
180 | spinlock_lock(&THREAD->lock); |
163 | if (usec) { |
181 | if (usec) { |
164 | /* We use the timeout variant. */ |
182 | /* We use the timeout variant. */ |
165 | if (!context_save(&THREAD->sleep_timeout_context)) { |
183 | if (!context_save(&THREAD->sleep_timeout_context)) { |
166 | /* |
184 | /* |
167 | * Short emulation of scheduler() return code. |
185 | * Short emulation of scheduler() return code. |
168 | */ |
186 | */ |
169 | before_thread_runs(); |
187 | before_thread_runs(); |
170 | spinlock_unlock(&THREAD->lock); |
188 | spinlock_unlock(&THREAD->lock); |
171 | cpu_priority_restore(pri); |
189 | cpu_priority_restore(pri); |
172 | return ESYNCH_TIMEOUT; |
190 | return ESYNCH_TIMEOUT; |
173 | } |
191 | } |
174 | THREAD->timeout_pending = 1; |
192 | THREAD->timeout_pending = 1; |
175 | timeout_register(&THREAD->sleep_timeout, (__u64) usec, waitq_interrupted_sleep, THREAD); |
193 | timeout_register(&THREAD->sleep_timeout, (__u64) usec, waitq_interrupted_sleep, THREAD); |
176 | } |
194 | } |
177 | 195 | ||
178 | list_append(&THREAD->wq_link, &wq->head); |
196 | list_append(&THREAD->wq_link, &wq->head); |
179 | 197 | ||
180 | /* |
198 | /* |
181 | * Suspend execution. |
199 | * Suspend execution. |
182 | */ |
200 | */ |
183 | THREAD->state = Sleeping; |
201 | THREAD->state = Sleeping; |
184 | THREAD->sleep_queue = wq; |
202 | THREAD->sleep_queue = wq; |
185 | 203 | ||
186 | spinlock_unlock(&THREAD->lock); |
204 | spinlock_unlock(&THREAD->lock); |
187 | 205 | ||
188 | scheduler(); /* wq->lock is released in scheduler_separated_stack() */ |
206 | scheduler(); /* wq->lock is released in scheduler_separated_stack() */ |
189 | cpu_priority_restore(pri); |
207 | cpu_priority_restore(pri); |
190 | 208 | ||
191 | return ESYNCH_OK_BLOCKED; |
209 | return ESYNCH_OK_BLOCKED; |
192 | } |
210 | } |
193 | 211 | ||
194 | 212 | ||
- | 213 | /** Wake up first thread sleeping in a wait queue |
|
195 | /* |
214 | * |
- | 215 | * Wake up first thread sleeping in a wait queue. |
|
196 | * This is the SMP- and IRQ-safe wrapper meant for general use. |
216 | * This is the SMP- and IRQ-safe wrapper meant for |
- | 217 | * general use. |
|
197 | */ |
218 | * |
- | 219 | * Besides its 'normal' wakeup operation, it attempts |
|
- | 220 | * to unregister possible timeout. |
|
198 | /* |
221 | * |
- | 222 | * @param wq Pointer to wait queue. |
|
- | 223 | * @param all If this is non-zero, all sleeping threads |
|
199 | * Besides its 'normal' wakeup operation, it attempts to unregister possible timeout. |
224 | * will be woken up and missed count will be zeroed. |
200 | */ |
225 | */ |
201 | void waitq_wakeup(waitq_t *wq, int all) |
226 | void waitq_wakeup(waitq_t *wq, int all) |
202 | { |
227 | { |
203 | pri_t pri; |
228 | pri_t pri; |
204 | 229 | ||
205 | pri = cpu_priority_high(); |
230 | pri = cpu_priority_high(); |
206 | spinlock_lock(&wq->lock); |
231 | spinlock_lock(&wq->lock); |
207 | 232 | ||
208 | _waitq_wakeup_unsafe(wq, all); |
233 | _waitq_wakeup_unsafe(wq, all); |
209 | 234 | ||
210 | spinlock_unlock(&wq->lock); |
235 | spinlock_unlock(&wq->lock); |
211 | cpu_priority_restore(pri); |
236 | cpu_priority_restore(pri); |
212 | } |
237 | } |
213 | 238 | ||
- | 239 | /** Internal SMP- and IRQ-unsafe version of waitq_wakeup() |
|
214 | /* |
240 | * |
215 | * This is the internal SMP- and IRQ-unsafe version of waitq_wakeup. |
241 | * This is the internal SMP- and IRQ-unsafe version |
216 | * It assumes wq->lock is already locked. |
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. |
|
217 | */ |
248 | */ |
218 | void _waitq_wakeup_unsafe(waitq_t *wq, int all) |
249 | void _waitq_wakeup_unsafe(waitq_t *wq, int all) |
219 | { |
250 | { |
220 | thread_t *t; |
251 | thread_t *t; |
221 | 252 | ||
222 | loop: |
253 | loop: |
223 | if (list_empty(&wq->head)) { |
254 | if (list_empty(&wq->head)) { |
224 | wq->missed_wakeups++; |
255 | wq->missed_wakeups++; |
225 | if (all) wq->missed_wakeups = 0; |
256 | if (all) wq->missed_wakeups = 0; |
226 | return; |
257 | return; |
227 | } |
258 | } |
228 | 259 | ||
229 | t = list_get_instance(wq->head.next, thread_t, wq_link); |
260 | t = list_get_instance(wq->head.next, thread_t, wq_link); |
230 | 261 | ||
231 | list_remove(&t->wq_link); |
262 | list_remove(&t->wq_link); |
232 | spinlock_lock(&t->lock); |
263 | spinlock_lock(&t->lock); |
233 | if (t->timeout_pending && timeout_unregister(&t->sleep_timeout)) |
264 | if (t->timeout_pending && timeout_unregister(&t->sleep_timeout)) |
234 | t->timeout_pending = 0; |
265 | t->timeout_pending = 0; |
235 | t->sleep_queue = NULL; |
266 | t->sleep_queue = NULL; |
236 | spinlock_unlock(&t->lock); |
267 | spinlock_unlock(&t->lock); |
237 | 268 | ||
238 | thread_ready(t); |
269 | thread_ready(t); |
239 | 270 | ||
240 | if (all) goto loop; |
271 | if (all) goto loop; |
241 | } |
272 | } |
242 | 273 |