Rev 15 | Rev 382 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 15 | Rev 25 | ||
---|---|---|---|
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> |
29 | #include <context.h> |
30 | #include <proc/thread.h> |
30 | #include <proc/thread.h> |
31 | 31 | ||
32 | #include <synch/synch.h> |
32 | #include <synch/synch.h> |
33 | #include <synch/waitq.h> |
33 | #include <synch/waitq.h> |
34 | #include <synch/spinlock.h> |
34 | #include <synch/spinlock.h> |
35 | 35 | ||
36 | #include <arch/asm.h> |
36 | #include <arch/asm.h> |
37 | #include <arch/types.h> |
37 | #include <arch/types.h> |
38 | #include <arch.h> |
38 | #include <arch.h> |
39 | 39 | ||
40 | #include <list.h> |
40 | #include <list.h> |
41 | 41 | ||
42 | #include <time/timeout.h> |
42 | #include <time/timeout.h> |
43 | 43 | ||
44 | void waitq_initialize(waitq_t *wq) |
44 | void waitq_initialize(waitq_t *wq) |
45 | { |
45 | { |
46 | spinlock_initialize(&wq->lock); |
46 | spinlock_initialize(&wq->lock); |
47 | list_initialize(&wq->head); |
47 | list_initialize(&wq->head); |
48 | wq->missed_wakeups = 0; |
48 | wq->missed_wakeups = 0; |
49 | } |
49 | } |
50 | 50 | ||
51 | /* |
51 | /* |
52 | * Called with interrupts disabled from clock() when sleep_timeout |
52 | * Called with interrupts disabled from clock() when sleep_timeout |
53 | * timeouts. This function is not allowed to enable interrupts. |
53 | * timeouts. This function is not allowed to enable interrupts. |
54 | * |
54 | * |
55 | * It is supposed to try to remove 'its' thread from the waitqueue; it |
55 | * It is supposed to try to remove 'its' thread from the waitqueue; it |
56 | * can eventually fail to achieve this goal when these two events |
56 | * can eventually fail to achieve this goal when these two events |
57 | * overlap; in that case it behaves just as though there was no |
57 | * overlap; in that case it behaves just as though there was no |
58 | * timeout at all |
58 | * timeout at all |
59 | */ |
59 | */ |
60 | void waitq_interrupted_sleep(void *data) |
60 | void waitq_interrupted_sleep(void *data) |
61 | { |
61 | { |
62 | thread_t *t = (thread_t *) data; |
62 | thread_t *t = (thread_t *) data; |
63 | waitq_t *wq; |
63 | waitq_t *wq; |
64 | int do_wakeup = 0; |
64 | int do_wakeup = 0; |
65 | 65 | ||
66 | spinlock_lock(&threads_lock); |
66 | spinlock_lock(&threads_lock); |
67 | if (!list_member(&t->threads_link, &threads_head)) |
67 | if (!list_member(&t->threads_link, &threads_head)) |
68 | goto out; |
68 | goto out; |
69 | 69 | ||
70 | grab_locks: |
70 | grab_locks: |
71 | spinlock_lock(&t->lock); |
71 | spinlock_lock(&t->lock); |
72 | if (wq = t->sleep_queue) { |
72 | if (wq = t->sleep_queue) { |
73 | if (!spinlock_trylock(&wq->lock)) { |
73 | if (!spinlock_trylock(&wq->lock)) { |
74 | spinlock_unlock(&t->lock); |
74 | spinlock_unlock(&t->lock); |
75 | goto grab_locks; /* avoid deadlock */ |
75 | goto grab_locks; /* avoid deadlock */ |
76 | } |
76 | } |
77 | 77 | ||
78 | list_remove(&t->wq_link); |
78 | list_remove(&t->wq_link); |
79 | t->saved_context = t->sleep_timeout_context; |
79 | t->saved_context = t->sleep_timeout_context; |
80 | do_wakeup = 1; |
80 | do_wakeup = 1; |
81 | 81 | ||
82 | spinlock_unlock(&wq->lock); |
82 | spinlock_unlock(&wq->lock); |
83 | t->sleep_queue = NULL; |
83 | t->sleep_queue = NULL; |
84 | } |
84 | } |
85 | 85 | ||
86 | t->timeout_pending = 0; |
86 | t->timeout_pending = 0; |
87 | spinlock_unlock(&t->lock); |
87 | spinlock_unlock(&t->lock); |
88 | 88 | ||
89 | if (do_wakeup) thread_ready(t); |
89 | if (do_wakeup) thread_ready(t); |
90 | 90 | ||
91 | out: |
91 | out: |
92 | spinlock_unlock(&threads_lock); |
92 | spinlock_unlock(&threads_lock); |
93 | } |
93 | } |
94 | 94 | ||
95 | /* |
95 | /* |
96 | * This is a sleep implementation which allows itself to be |
96 | * This is a sleep implementation which allows itself to be |
97 | * interrupted from the sleep, restoring a failover context. |
97 | * interrupted from the sleep, restoring a failover context. |
98 | * |
98 | * |
99 | * This function is really basic in that other functions as waitq_sleep() |
99 | * This function is really basic in that other functions as waitq_sleep() |
100 | * and all the *_timeout() functions use it. |
100 | * and all the *_timeout() functions use it. |
101 | * |
101 | * |
102 | * The third argument controls whether only a conditional sleep |
102 | * The third argument controls whether only a conditional sleep |
103 | * (non-blocking sleep) is called for when the second argument is 0. |
103 | * (non-blocking sleep) is called for when the second argument is 0. |
104 | * |
104 | * |
105 | * usec | nonblocking | what happens if there is no missed_wakeup |
105 | * usec | nonblocking | what happens if there is no missed_wakeup |
106 | * -----+-------------+-------------------------------------------- |
106 | * -----+-------------+-------------------------------------------- |
107 | * 0 | 0 | blocks without timeout until wakeup |
107 | * 0 | 0 | blocks without timeout until wakeup |
108 | * 0 | <> 0 | immediately returns ESYNCH_WOULD_BLOCK |
108 | * 0 | <> 0 | immediately returns ESYNCH_WOULD_BLOCK |
109 | * > 0 | x | blocks with timeout until timeout or wakeup |
109 | * > 0 | x | blocks with timeout until timeout or wakeup |
110 | * |
110 | * |
111 | * return values: |
111 | * return values: |
112 | * ESYNCH_WOULD_BLOCK |
112 | * ESYNCH_WOULD_BLOCK |
113 | * ESYNCH_TIMEOUT |
113 | * ESYNCH_TIMEOUT |
114 | * ESYNCH_OK_ATOMIC |
114 | * ESYNCH_OK_ATOMIC |
115 | * ESYNCH_OK_BLOCKED |
115 | * ESYNCH_OK_BLOCKED |
116 | */ |
116 | */ |
117 | int waitq_sleep_timeout(waitq_t *wq, __u32 usec, int nonblocking) |
117 | int waitq_sleep_timeout(waitq_t *wq, __u32 usec, int nonblocking) |
118 | { |
118 | { |
119 | volatile pri_t pri; /* must be live after context_restore() */ |
119 | volatile pri_t pri; /* must be live after context_restore() */ |
120 | 120 | ||
121 | 121 | ||
122 | restart: |
122 | restart: |
123 | pri = cpu_priority_high(); |
123 | pri = cpu_priority_high(); |
124 | 124 | ||
125 | /* |
125 | /* |
126 | * Busy waiting for a delayed timeout. |
126 | * Busy waiting for a delayed timeout. |
127 | * This is an important fix for the race condition between |
127 | * This is an important fix for the race condition between |
128 | * a delayed timeout and a next call to waitq_sleep_timeout(). |
128 | * a delayed timeout and a next call to waitq_sleep_timeout(). |
129 | * Simply, the thread is not allowed to go to sleep if |
129 | * Simply, the thread is not allowed to go to sleep if |
130 | * there are timeouts in progress. |
130 | * there are timeouts in progress. |
131 | */ |
131 | */ |
132 | spinlock_lock(&THREAD->lock); |
132 | spinlock_lock(&THREAD->lock); |
133 | if (THREAD->timeout_pending) { |
133 | if (THREAD->timeout_pending) { |
134 | spinlock_unlock(&THREAD->lock); |
134 | spinlock_unlock(&THREAD->lock); |
135 | cpu_priority_restore(pri); |
135 | cpu_priority_restore(pri); |
136 | goto restart; |
136 | goto restart; |
137 | } |
137 | } |
138 | spinlock_unlock(&THREAD->lock); |
138 | spinlock_unlock(&THREAD->lock); |
139 | 139 | ||
140 | spinlock_lock(&wq->lock); |
140 | spinlock_lock(&wq->lock); |
141 | 141 | ||
142 | /* checks whether to go to sleep at all */ |
142 | /* checks whether to go to sleep at all */ |
143 | if (wq->missed_wakeups) { |
143 | if (wq->missed_wakeups) { |
144 | wq->missed_wakeups--; |
144 | wq->missed_wakeups--; |
145 | spinlock_unlock(&wq->lock); |
145 | spinlock_unlock(&wq->lock); |
146 | cpu_priority_restore(pri); |
146 | cpu_priority_restore(pri); |
147 | return ESYNCH_OK_ATOMIC; |
147 | return ESYNCH_OK_ATOMIC; |
148 | } |
148 | } |
149 | else { |
149 | else { |
150 | if (nonblocking && (usec == 0)) { |
150 | if (nonblocking && (usec == 0)) { |
151 | /* return immediatelly instead of going to sleep */ |
151 | /* return immediatelly instead of going to sleep */ |
152 | spinlock_unlock(&wq->lock); |
152 | spinlock_unlock(&wq->lock); |
153 | cpu_priority_restore(pri); |
153 | cpu_priority_restore(pri); |
154 | return ESYNCH_WOULD_BLOCK; |
154 | return ESYNCH_WOULD_BLOCK; |
155 | } |
155 | } |
156 | } |
156 | } |
157 | 157 | ||
158 | 158 | ||
159 | /* |
159 | /* |
160 | * Now we are firmly decided to go to sleep. |
160 | * Now we are firmly decided to go to sleep. |
161 | */ |
161 | */ |
162 | spinlock_lock(&THREAD->lock); |
162 | spinlock_lock(&THREAD->lock); |
163 | if (usec) { |
163 | if (usec) { |
164 | /* We use the timeout variant. */ |
164 | /* We use the timeout variant. */ |
165 | if (!context_save(&THREAD->sleep_timeout_context)) { |
165 | if (!context_save(&THREAD->sleep_timeout_context)) { |
166 | /* |
166 | /* |
167 | * Short emulation of scheduler() return code. |
167 | * Short emulation of scheduler() return code. |
168 | */ |
168 | */ |
- | 169 | before_thread_runs(); |
|
169 | spinlock_unlock(&THREAD->lock); |
170 | spinlock_unlock(&THREAD->lock); |
170 | cpu_priority_restore(pri); |
171 | cpu_priority_restore(pri); |
171 | return ESYNCH_TIMEOUT; |
172 | return ESYNCH_TIMEOUT; |
172 | } |
173 | } |
173 | THREAD->timeout_pending = 1; |
174 | THREAD->timeout_pending = 1; |
174 | timeout_register(&THREAD->sleep_timeout, (__u64) usec, waitq_interrupted_sleep, THREAD); |
175 | timeout_register(&THREAD->sleep_timeout, (__u64) usec, waitq_interrupted_sleep, THREAD); |
175 | } |
176 | } |
176 | 177 | ||
177 | list_append(&THREAD->wq_link, &wq->head); |
178 | list_append(&THREAD->wq_link, &wq->head); |
178 | 179 | ||
179 | /* |
180 | /* |
180 | * Suspend execution. |
181 | * Suspend execution. |
181 | */ |
182 | */ |
182 | THREAD->state = Sleeping; |
183 | THREAD->state = Sleeping; |
183 | THREAD->sleep_queue = wq; |
184 | THREAD->sleep_queue = wq; |
184 | 185 | ||
185 | spinlock_unlock(&THREAD->lock); |
186 | spinlock_unlock(&THREAD->lock); |
186 | 187 | ||
187 | scheduler(); /* wq->lock is released in scheduler_separated_stack() */ |
188 | scheduler(); /* wq->lock is released in scheduler_separated_stack() */ |
188 | cpu_priority_restore(pri); |
189 | cpu_priority_restore(pri); |
189 | 190 | ||
190 | return ESYNCH_OK_BLOCKED; |
191 | return ESYNCH_OK_BLOCKED; |
191 | } |
192 | } |
192 | 193 | ||
193 | 194 | ||
194 | /* |
195 | /* |
195 | * This is the SMP- and IRQ-safe wrapper meant for general use. |
196 | * This is the SMP- and IRQ-safe wrapper meant for general use. |
196 | */ |
197 | */ |
197 | /* |
198 | /* |
198 | * Besides its 'normal' wakeup operation, it attempts to unregister possible timeout. |
199 | * Besides its 'normal' wakeup operation, it attempts to unregister possible timeout. |
199 | */ |
200 | */ |
200 | void waitq_wakeup(waitq_t *wq, int all) |
201 | void waitq_wakeup(waitq_t *wq, int all) |
201 | { |
202 | { |
202 | pri_t pri; |
203 | pri_t pri; |
203 | 204 | ||
204 | pri = cpu_priority_high(); |
205 | pri = cpu_priority_high(); |
205 | spinlock_lock(&wq->lock); |
206 | spinlock_lock(&wq->lock); |
206 | 207 | ||
207 | _waitq_wakeup_unsafe(wq, all); |
208 | _waitq_wakeup_unsafe(wq, all); |
208 | 209 | ||
209 | spinlock_unlock(&wq->lock); |
210 | spinlock_unlock(&wq->lock); |
210 | cpu_priority_restore(pri); |
211 | cpu_priority_restore(pri); |
211 | } |
212 | } |
212 | 213 | ||
213 | /* |
214 | /* |
214 | * This is the internal SMP- and IRQ-unsafe version of waitq_wakeup. |
215 | * This is the internal SMP- and IRQ-unsafe version of waitq_wakeup. |
215 | * It assumes wq->lock is already locked. |
216 | * It assumes wq->lock is already locked. |
216 | */ |
217 | */ |
217 | void _waitq_wakeup_unsafe(waitq_t *wq, int all) |
218 | void _waitq_wakeup_unsafe(waitq_t *wq, int all) |
218 | { |
219 | { |
219 | thread_t *t; |
220 | thread_t *t; |
220 | 221 | ||
221 | loop: |
222 | loop: |
222 | if (list_empty(&wq->head)) { |
223 | if (list_empty(&wq->head)) { |
223 | wq->missed_wakeups++; |
224 | wq->missed_wakeups++; |
224 | if (all) wq->missed_wakeups = 0; |
225 | if (all) wq->missed_wakeups = 0; |
225 | return; |
226 | return; |
226 | } |
227 | } |
227 | 228 | ||
228 | t = list_get_instance(wq->head.next, thread_t, wq_link); |
229 | t = list_get_instance(wq->head.next, thread_t, wq_link); |
229 | 230 | ||
230 | list_remove(&t->wq_link); |
231 | list_remove(&t->wq_link); |
231 | spinlock_lock(&t->lock); |
232 | spinlock_lock(&t->lock); |
232 | if (t->timeout_pending && timeout_unregister(&t->sleep_timeout)) |
233 | if (t->timeout_pending && timeout_unregister(&t->sleep_timeout)) |
233 | t->timeout_pending = 0; |
234 | t->timeout_pending = 0; |
234 | t->sleep_queue = NULL; |
235 | t->sleep_queue = NULL; |
235 | spinlock_unlock(&t->lock); |
236 | spinlock_unlock(&t->lock); |
236 | 237 | ||
237 | thread_ready(t); |
238 | thread_ready(t); |
238 | 239 | ||
239 | if (all) goto loop; |
240 | if (all) goto loop; |
240 | } |
241 | } |
241 | 242 |