Rev 2416 | Rev 2450 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2416 | Rev 2421 | ||
---|---|---|---|
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 | /** @addtogroup time |
29 | /** @addtogroup time |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
33 | /** |
34 | * @file |
34 | * @file |
35 | * @brief High-level clock interrupt handler. |
35 | * @brief High-level clock interrupt handler. |
36 | * |
36 | * |
37 | * This file contains the clock() function which is the source |
37 | * This file contains the clock() function which is the source |
38 | * of preemption. It is also responsible for executing expired |
38 | * of preemption. It is also responsible for executing expired |
39 | * timeouts. |
39 | * timeouts. |
40 | */ |
40 | */ |
41 | 41 | ||
42 | #include <time/clock.h> |
42 | #include <time/clock.h> |
43 | #include <time/timeout.h> |
43 | #include <time/timeout.h> |
44 | #include <config.h> |
44 | #include <config.h> |
45 | #include <synch/spinlock.h> |
45 | #include <synch/spinlock.h> |
46 | #include <synch/waitq.h> |
46 | #include <synch/waitq.h> |
47 | #include <func.h> |
47 | #include <func.h> |
48 | #include <proc/scheduler.h> |
48 | #include <proc/scheduler.h> |
49 | #include <cpu.h> |
49 | #include <cpu.h> |
50 | #include <arch.h> |
50 | #include <arch.h> |
51 | #include <adt/list.h> |
51 | #include <adt/list.h> |
52 | #include <atomic.h> |
52 | #include <atomic.h> |
53 | #include <proc/thread.h> |
53 | #include <proc/thread.h> |
54 | #include <sysinfo/sysinfo.h> |
54 | #include <sysinfo/sysinfo.h> |
55 | #include <arch/barrier.h> |
55 | #include <arch/barrier.h> |
56 | #include <mm/frame.h> |
56 | #include <mm/frame.h> |
57 | #include <ddi/ddi.h> |
57 | #include <ddi/ddi.h> |
58 | 58 | ||
59 | /* Pointer to variable with uptime */ |
59 | /* Pointer to variable with uptime */ |
60 | uptime_t *uptime; |
60 | uptime_t *uptime; |
61 | 61 | ||
62 | /** Physical memory area of the real time clock */ |
62 | /** Physical memory area of the real time clock */ |
63 | static parea_t clock_parea; |
63 | static parea_t clock_parea; |
64 | 64 | ||
65 | /* Variable holding fragment of second, so that we would update |
65 | /* Variable holding fragment of second, so that we would update |
66 | * seconds correctly |
66 | * seconds correctly |
67 | */ |
67 | */ |
68 | static unative_t secfrag = 0; |
68 | static unative_t secfrag = 0; |
69 | 69 | ||
70 | /** Initialize realtime clock counter |
70 | /** Initialize realtime clock counter |
71 | * |
71 | * |
72 | * The applications (and sometimes kernel) need to access accurate |
72 | * The applications (and sometimes kernel) need to access accurate |
73 | * information about realtime data. We allocate 1 page with these |
73 | * information about realtime data. We allocate 1 page with these |
74 | * data and update it periodically. |
74 | * data and update it periodically. |
75 | */ |
75 | */ |
76 | void clock_counter_init(void) |
76 | void clock_counter_init(void) |
77 | { |
77 | { |
78 | void *faddr; |
78 | void *faddr; |
79 | 79 | ||
80 | faddr = frame_alloc(ONE_FRAME, FRAME_ATOMIC); |
80 | faddr = frame_alloc(ONE_FRAME, FRAME_ATOMIC); |
81 | if (!faddr) |
81 | if (!faddr) |
82 | panic("Cannot allocate page for clock"); |
82 | panic("Cannot allocate page for clock"); |
83 | 83 | ||
84 | uptime = (uptime_t *) PA2KA(faddr); |
84 | uptime = (uptime_t *) PA2KA(faddr); |
85 | 85 | ||
86 | uptime->seconds1 = 0; |
86 | uptime->seconds1 = 0; |
87 | uptime->seconds2 = 0; |
87 | uptime->seconds2 = 0; |
88 | uptime->useconds = 0; |
88 | uptime->useconds = 0; |
89 | 89 | ||
90 | clock_parea.pbase = (uintptr_t) faddr; |
90 | clock_parea.pbase = (uintptr_t) faddr; |
91 | clock_parea.vbase = (uintptr_t) uptime; |
91 | clock_parea.vbase = (uintptr_t) uptime; |
92 | clock_parea.frames = 1; |
92 | clock_parea.frames = 1; |
93 | clock_parea.cacheable = true; |
93 | clock_parea.cacheable = true; |
94 | ddi_parea_register(&clock_parea); |
94 | ddi_parea_register(&clock_parea); |
95 | 95 | ||
96 | /* |
96 | /* |
97 | * Prepare information for the userspace so that it can successfully |
97 | * Prepare information for the userspace so that it can successfully |
98 | * physmem_map() the clock_parea. |
98 | * physmem_map() the clock_parea. |
99 | */ |
99 | */ |
100 | sysinfo_set_item_val("clock.cacheable", NULL, (unative_t) true); |
100 | sysinfo_set_item_val("clock.cacheable", NULL, (unative_t) true); |
101 | sysinfo_set_item_val("clock.faddr", NULL, (unative_t) faddr); |
101 | sysinfo_set_item_val("clock.faddr", NULL, (unative_t) faddr); |
102 | } |
102 | } |
103 | 103 | ||
104 | 104 | ||
105 | /** Update public counters |
105 | /** Update public counters |
106 | * |
106 | * |
107 | * Update it only on first processor |
107 | * Update it only on first processor |
108 | * TODO: Do we really need so many write barriers? |
108 | * TODO: Do we really need so many write barriers? |
109 | */ |
109 | */ |
110 | static void clock_update_counters(void) |
110 | static void clock_update_counters(void) |
111 | { |
111 | { |
112 | if (CPU->id == 0) { |
112 | if (CPU->id == 0) { |
113 | secfrag += 1000000 / HZ; |
113 | secfrag += 1000000 / HZ; |
114 | if (secfrag >= 1000000) { |
114 | if (secfrag >= 1000000) { |
115 | secfrag -= 1000000; |
115 | secfrag -= 1000000; |
116 | uptime->seconds1++; |
116 | uptime->seconds1++; |
117 | write_barrier(); |
117 | write_barrier(); |
118 | uptime->useconds = secfrag; |
118 | uptime->useconds = secfrag; |
119 | write_barrier(); |
119 | write_barrier(); |
120 | uptime->seconds2 = uptime->seconds1; |
120 | uptime->seconds2 = uptime->seconds1; |
121 | } else |
121 | } else |
122 | uptime->useconds += 1000000 / HZ; |
122 | uptime->useconds += 1000000 / HZ; |
123 | } |
123 | } |
124 | } |
124 | } |
125 | 125 | ||
126 | #if defined CONFIG_TIMEOUT_AVL_TREE || \ |
126 | #if defined CONFIG_TIMEOUT_AVL_TREE |
127 | defined CONFIG_TIMEOUT_EXTAVL_TREE |
- | |
128 | 127 | ||
129 | /** Clock routine |
128 | /** Clock routine |
130 | * |
129 | * |
131 | * Clock routine executed from clock interrupt handler |
130 | * Clock routine executed from clock interrupt handler |
132 | * (assuming interrupts_disable()'d). Runs expired timeouts |
131 | * (assuming interrupts_disable()'d). Runs expired timeouts |
133 | * and preemptive scheduling. |
132 | * and preemptive scheduling. |
134 | * |
133 | * |
135 | */ |
134 | */ |
136 | void clock(void) |
135 | void clock(void) |
137 | { |
136 | { |
138 | timeout_t *h; |
137 | timeout_t *h; |
139 | timeout_handler_t f; |
138 | timeout_handler_t f; |
140 | void *arg; |
139 | void *arg; |
141 | count_t missed_clock_ticks = CPU->missed_clock_ticks; |
140 | count_t missed_clock_ticks = CPU->missed_clock_ticks; |
142 | uint64_t *i = &(CPU->timeout_active_tree.base); |
141 | uint64_t *i = &(CPU->timeout_active_tree.base); |
143 | uint64_t absolute_clock_ticks = *i + missed_clock_ticks; |
142 | uint64_t absolute_clock_ticks = *i + missed_clock_ticks; |
144 | #if defined CONFIG TIMEOUT_AVL_TREE |
- | |
145 | avltree_node_t *expnode; |
143 | avltree_node_t *expnode; |
- | 144 | ||
- | 145 | /* |
|
- | 146 | * To avoid lock ordering problems, |
|
- | 147 | * run all expired timeouts as you visit them. |
|
- | 148 | */ |
|
- | 149 | ||
- | 150 | for (; *i <= absolute_clock_ticks; (*i)++) { |
|
- | 151 | /* |
|
- | 152 | * Basetime is encreased by missed clock ticks + 1 !! |
|
- | 153 | */ |
|
- | 154 | ||
- | 155 | clock_update_counters(); |
|
- | 156 | spinlock_lock(&CPU->timeoutlock); |
|
- | 157 | ||
- | 158 | ||
- | 159 | /* |
|
- | 160 | * Check whether first timeout (with the smallest key in the tree) time out. If so perform |
|
- | 161 | * callback function and try next timeout (more timeouts can have same timeout). |
|
- | 162 | */ |
|
- | 163 | while ((expnode = avltree_find_min(&CPU->timeout_active_tree)) != NULL) { |
|
- | 164 | h = avltree_get_instance(expnode,timeout_t,node); |
|
- | 165 | spinlock_lock(&h->lock); |
|
- | 166 | if (expnode->key != *i) { |
|
- | 167 | spinlock_unlock(&h->lock); |
|
- | 168 | break; |
|
- | 169 | } |
|
- | 170 | ||
- | 171 | /* |
|
- | 172 | * Delete minimal key from the tree and repair tree structure in |
|
- | 173 | * logarithmic time. |
|
- | 174 | */ |
|
- | 175 | avltree_delete_min(&CPU->timeout_active_tree); |
|
- | 176 | ||
- | 177 | f = h->handler; |
|
- | 178 | arg = h->arg; |
|
- | 179 | timeout_reinitialize(h); |
|
- | 180 | spinlock_unlock(&h->lock); |
|
- | 181 | spinlock_unlock(&CPU->timeoutlock); |
|
- | 182 | ||
- | 183 | f(arg); |
|
- | 184 | ||
- | 185 | spinlock_lock(&CPU->timeoutlock); |
|
- | 186 | } |
|
- | 187 | spinlock_unlock(&CPU->timeoutlock); |
|
- | 188 | } |
|
- | 189 | ||
- | 190 | CPU->missed_clock_ticks = 0; |
|
- | 191 | ||
- | 192 | /* |
|
- | 193 | * Do CPU usage accounting and find out whether to preempt THREAD. |
|
- | 194 | */ |
|
- | 195 | if (THREAD) { |
|
- | 196 | uint64_t ticks; |
|
- | 197 | ||
- | 198 | spinlock_lock(&CPU->lock); |
|
- | 199 | CPU->needs_relink += 1 + missed_clock_ticks; |
|
- | 200 | spinlock_unlock(&CPU->lock); |
|
- | 201 | ||
- | 202 | spinlock_lock(&THREAD->lock); |
|
- | 203 | if ((ticks = THREAD->ticks)) { |
|
- | 204 | if (ticks >= 1 + missed_clock_ticks) |
|
- | 205 | THREAD->ticks -= 1 + missed_clock_ticks; |
|
- | 206 | else |
|
- | 207 | THREAD->ticks = 0; |
|
- | 208 | } |
|
- | 209 | spinlock_unlock(&THREAD->lock); |
|
- | 210 | ||
- | 211 | if (!ticks && !PREEMPTION_DISABLED) { |
|
- | 212 | scheduler(); |
|
- | 213 | } |
|
- | 214 | } |
|
- | 215 | } |
|
- | 216 | ||
146 | #elif defined CONFIG_TIMEOUT_EXTAVL_TREE |
217 | #elif defined CONFIG_TIMEOUT_EXTAVL_TREE |
- | 218 | ||
- | 219 | /** Clock routine |
|
- | 220 | * |
|
- | 221 | * Clock routine executed from clock interrupt handler |
|
- | 222 | * (assuming interrupts_disable()'d). Runs expired timeouts |
|
- | 223 | * and preemptive scheduling. |
|
- | 224 | * |
|
- | 225 | */ |
|
- | 226 | void clock(void) |
|
- | 227 | { |
|
- | 228 | timeout_t *h; |
|
- | 229 | timeout_handler_t f; |
|
- | 230 | void *arg; |
|
- | 231 | count_t missed_clock_ticks = CPU->missed_clock_ticks; |
|
- | 232 | uint64_t *i = &(CPU->timeout_active_tree.base); |
|
- | 233 | uint64_t absolute_clock_ticks = *i + missed_clock_ticks; |
|
147 | extavltree_node_t *expnode; |
234 | extavltree_node_t *expnode; |
148 | #endif |
- | |
149 | 235 | ||
150 | /* |
236 | /* |
151 | * To avoid lock ordering problems, |
237 | * To avoid lock ordering problems, |
152 | * run all expired timeouts as you visit them. |
238 | * run all expired timeouts as you visit them. |
153 | */ |
239 | */ |
154 | 240 | ||
155 | for (; *i <= absolute_clock_ticks; (*i)++) { |
241 | for (; *i <= absolute_clock_ticks; (*i)++) { |
156 | /* |
242 | /* |
157 | * Basetime is encreased by missed clock ticks + 1 !! |
243 | * Basetime is encreased by missed clock ticks + 1 !! |
158 | */ |
244 | */ |
159 | 245 | ||
160 | clock_update_counters(); |
246 | clock_update_counters(); |
161 | spinlock_lock(&CPU->timeoutlock); |
247 | spinlock_lock(&CPU->timeoutlock); |
162 | 248 | ||
163 | /* |
249 | /* |
164 | * Check whether first timeout in list time out. If so perform callback function and try |
250 | * Check whether first timeout in list time out. If so perform callback function and try |
165 | * next timeout (more timeouts can have same timeout). |
251 | * next timeout (more timeouts can have same timeout). |
166 | */ |
252 | */ |
167 | while ((expnode = CPU->timeout_active_tree.head.next) != &(CPU->timeout_active_tree.head)) { |
253 | while ((expnode = CPU->timeout_active_tree.head.next) != &(CPU->timeout_active_tree.head)) { |
168 | h = extavltree_get_instance(expnode,timeout_t,node); |
254 | h = extavltree_get_instance(expnode,timeout_t,node); |
169 | spinlock_lock(&h->lock); |
255 | spinlock_lock(&h->lock); |
170 | if (expnode->key != *i) { |
256 | if (expnode->key != *i) { |
171 | spinlock_unlock(&h->lock); |
257 | spinlock_unlock(&h->lock); |
172 | break; |
258 | break; |
173 | } |
259 | } |
174 | 260 | ||
175 | /* |
261 | /* |
176 | * Delete first node in the list and repair tree structure in |
262 | * Delete first node in the list and repair tree structure in |
177 | * constant time. |
263 | * constant time. |
178 | */ |
264 | */ |
179 | #if defined CONFIG TIMEOUT_AVL_TREE |
- | |
180 | avltree_delete_min(&CPU->timeout_active_tree); |
- | |
181 | #elif defined CONFIG_TIMEOUT_EXTAVL_TREE |
- | |
182 | extavltree_delete_min(&CPU->timeout_active_tree); |
265 | extavltree_delete_min(&CPU->timeout_active_tree); |
183 | #endif |
- | |
184 | 266 | ||
185 | f = h->handler; |
267 | f = h->handler; |
186 | arg = h->arg; |
268 | arg = h->arg; |
187 | timeout_reinitialize(h); |
269 | timeout_reinitialize(h); |
188 | spinlock_unlock(&h->lock); |
270 | spinlock_unlock(&h->lock); |
189 | spinlock_unlock(&CPU->timeoutlock); |
271 | spinlock_unlock(&CPU->timeoutlock); |
190 | 272 | ||
191 | f(arg); |
273 | f(arg); |
192 | 274 | ||
193 | spinlock_lock(&CPU->timeoutlock); |
275 | spinlock_lock(&CPU->timeoutlock); |
194 | } |
276 | } |
195 | spinlock_unlock(&CPU->timeoutlock); |
277 | spinlock_unlock(&CPU->timeoutlock); |
196 | } |
278 | } |
197 | 279 | ||
198 | CPU->missed_clock_ticks = 0; |
280 | CPU->missed_clock_ticks = 0; |
199 | 281 | ||
200 | /* |
282 | /* |
201 | * Do CPU usage accounting and find out whether to preempt THREAD. |
283 | * Do CPU usage accounting and find out whether to preempt THREAD. |
202 | */ |
284 | */ |
203 | if (THREAD) { |
285 | if (THREAD) { |
204 | uint64_t ticks; |
286 | uint64_t ticks; |
205 | 287 | ||
206 | spinlock_lock(&CPU->lock); |
288 | spinlock_lock(&CPU->lock); |
207 | CPU->needs_relink += 1 + missed_clock_ticks; |
289 | CPU->needs_relink += 1 + missed_clock_ticks; |
208 | spinlock_unlock(&CPU->lock); |
290 | spinlock_unlock(&CPU->lock); |
209 | 291 | ||
210 | spinlock_lock(&THREAD->lock); |
292 | spinlock_lock(&THREAD->lock); |
211 | if ((ticks = THREAD->ticks)) { |
293 | if ((ticks = THREAD->ticks)) { |
212 | if (ticks >= 1 + missed_clock_ticks) |
294 | if (ticks >= 1 + missed_clock_ticks) |
213 | THREAD->ticks -= 1 + missed_clock_ticks; |
295 | THREAD->ticks -= 1 + missed_clock_ticks; |
214 | else |
296 | else |
215 | THREAD->ticks = 0; |
297 | THREAD->ticks = 0; |
216 | } |
298 | } |
217 | spinlock_unlock(&THREAD->lock); |
299 | spinlock_unlock(&THREAD->lock); |
218 | 300 | ||
219 | if (!ticks && !PREEMPTION_DISABLED) { |
301 | if (!ticks && !PREEMPTION_DISABLED) { |
220 | scheduler(); |
302 | scheduler(); |
221 | } |
303 | } |
222 | } |
304 | } |
223 | } |
305 | } |
224 | 306 | ||
225 | #elif defined CONFIG_TIMEOUT_EXTAVLREL_TREE |
307 | #elif defined CONFIG_TIMEOUT_EXTAVLREL_TREE |
226 | 308 | ||
227 | /** Clock routine |
309 | /** Clock routine |
228 | * |
310 | * |
229 | * Clock routine executed from clock interrupt handler |
311 | * Clock routine executed from clock interrupt handler |
230 | * (assuming interrupts_disable()'d). Runs expired timeouts |
312 | * (assuming interrupts_disable()'d). Runs expired timeouts |
231 | * and preemptive scheduling. |
313 | * and preemptive scheduling. |
232 | * |
314 | * |
233 | */ |
315 | */ |
234 | void clock(void) |
316 | void clock(void) |
235 | { |
317 | { |
236 | extavltree_node_t *expnode; |
318 | extavlreltree_node_t *expnode; |
237 | timeout_t *h; |
319 | timeout_t *h; |
238 | timeout_handler_t f; |
320 | timeout_handler_t f; |
239 | void *arg; |
321 | void *arg; |
240 | count_t missed_clock_ticks = CPU->missed_clock_ticks; |
322 | count_t missed_clock_ticks = CPU->missed_clock_ticks; |
241 | int i; |
323 | int i; |
242 | 324 | ||
243 | /* |
325 | /* |
244 | * To avoid lock ordering problems, |
326 | * To avoid lock ordering problems, |
245 | * run all expired timeouts as you visit them. |
327 | * run all expired timeouts as you visit them. |
246 | */ |
328 | */ |
247 | for (i = 0; i <= missed_clock_ticks; i++) { |
329 | for (i = 0; i <= missed_clock_ticks; i++) { |
248 | clock_update_counters(); |
330 | clock_update_counters(); |
249 | spinlock_lock(&CPU->timeoutlock); |
331 | spinlock_lock(&CPU->timeoutlock); |
250 | 332 | ||
251 | /* |
333 | /* |
252 | * Check whether first timeout in list time out. If so perform callback function and try |
334 | * Check whether first timeout in list time out. If so perform callback function and try |
253 | * next timeout (more timeouts can have same timeout). |
335 | * next timeout (more timeouts can have same timeout). |
254 | */ |
336 | */ |
255 | while ((expnode = CPU->timeout_active_tree.head.next) != &(CPU->timeout_active_tree.head)) { |
337 | while ((expnode = CPU->timeout_active_tree.head.next) != &(CPU->timeout_active_tree.head)) { |
256 | h = list_get_instance(l, timeout_t, link); |
338 | h = extavlreltree_get_instance(expnode,timeout_t,node); |
257 | spinlock_lock(&h->lock); |
339 | spinlock_lock(&h->lock); |
258 | if (expnode->key != 0) { |
340 | if (expnode->key != 0) { |
259 | expnode->key--; |
341 | expnode->key--; |
260 | spinlock_unlock(&h->lock); |
342 | spinlock_unlock(&h->lock); |
261 | break; |
343 | break; |
262 | } |
344 | } |
263 | 345 | ||
264 | /* |
346 | /* |
265 | * Delete first node in the list and repair tree structure in |
347 | * Delete first node in the list and repair tree structure in |
266 | * constant time. Be careful of expnode's key, it must be 0! |
348 | * constant time. Be careful of expnode's key, it must be 0! |
267 | */ |
349 | */ |
268 | extavltree_delete_min(&CPU->timeout_active_tree); |
350 | extavlreltree_delete_min(&CPU->timeout_active_tree); |
269 | 351 | ||
270 | f = h->handler; |
352 | f = h->handler; |
271 | arg = h->arg; |
353 | arg = h->arg; |
272 | timeout_reinitialize(h); |
354 | timeout_reinitialize(h); |
273 | spinlock_unlock(&h->lock); |
355 | spinlock_unlock(&h->lock); |
274 | spinlock_unlock(&CPU->timeoutlock); |
356 | spinlock_unlock(&CPU->timeoutlock); |
275 | 357 | ||
276 | f(arg); |
358 | f(arg); |
277 | 359 | ||
278 | spinlock_lock(&CPU->timeoutlock); |
360 | spinlock_lock(&CPU->timeoutlock); |
279 | } |
361 | } |
280 | spinlock_unlock(&CPU->timeoutlock); |
362 | spinlock_unlock(&CPU->timeoutlock); |
281 | } |
363 | } |
282 | CPU->missed_clock_ticks = 0; |
364 | CPU->missed_clock_ticks = 0; |
283 | 365 | ||
284 | /* |
366 | /* |
285 | * Do CPU usage accounting and find out whether to preempt THREAD. |
367 | * Do CPU usage accounting and find out whether to preempt THREAD. |
286 | */ |
368 | */ |
287 | 369 | ||
288 | if (THREAD) { |
370 | if (THREAD) { |
289 | uint64_t ticks; |
371 | uint64_t ticks; |
290 | 372 | ||
291 | spinlock_lock(&CPU->lock); |
373 | spinlock_lock(&CPU->lock); |
292 | CPU->needs_relink += 1 + missed_clock_ticks; |
374 | CPU->needs_relink += 1 + missed_clock_ticks; |
293 | spinlock_unlock(&CPU->lock); |
375 | spinlock_unlock(&CPU->lock); |
294 | 376 | ||
295 | spinlock_lock(&THREAD->lock); |
377 | spinlock_lock(&THREAD->lock); |
296 | if ((ticks = THREAD->ticks)) { |
378 | if ((ticks = THREAD->ticks)) { |
297 | if (ticks >= 1 + missed_clock_ticks) |
379 | if (ticks >= 1 + missed_clock_ticks) |
298 | THREAD->ticks -= 1 + missed_clock_ticks; |
380 | THREAD->ticks -= 1 + missed_clock_ticks; |
299 | else |
381 | else |
300 | THREAD->ticks = 0; |
382 | THREAD->ticks = 0; |
301 | } |
383 | } |
302 | spinlock_unlock(&THREAD->lock); |
384 | spinlock_unlock(&THREAD->lock); |
303 | 385 | ||
304 | if (!ticks && !PREEMPTION_DISABLED) { |
386 | if (!ticks && !PREEMPTION_DISABLED) { |
305 | scheduler(); |
387 | scheduler(); |
306 | } |
388 | } |
307 | } |
389 | } |
308 | } |
390 | } |
309 | 391 | ||
310 | 392 | ||
311 | 393 | ||
312 | #else |
394 | #else |
313 | 395 | ||
314 | 396 | ||
315 | /** Clock routine |
397 | /** Clock routine |
316 | * |
398 | * |
317 | * Clock routine executed from clock interrupt handler |
399 | * Clock routine executed from clock interrupt handler |
318 | * (assuming interrupts_disable()'d). Runs expired timeouts |
400 | * (assuming interrupts_disable()'d). Runs expired timeouts |
319 | * and preemptive scheduling. |
401 | * and preemptive scheduling. |
320 | * |
402 | * |
321 | */ |
403 | */ |
322 | void clock(void) |
404 | void clock(void) |
323 | { |
405 | { |
324 | link_t *l; |
406 | link_t *l; |
325 | timeout_t *h; |
407 | timeout_t *h; |
326 | timeout_handler_t f; |
408 | timeout_handler_t f; |
327 | void *arg; |
409 | void *arg; |
328 | count_t missed_clock_ticks = CPU->missed_clock_ticks; |
410 | count_t missed_clock_ticks = CPU->missed_clock_ticks; |
329 | int i; |
411 | int i; |
330 | 412 | ||
331 | /* |
413 | /* |
332 | * To avoid lock ordering problems, |
414 | * To avoid lock ordering problems, |
333 | * run all expired timeouts as you visit them. |
415 | * run all expired timeouts as you visit them. |
334 | */ |
416 | */ |
335 | for (i = 0; i <= missed_clock_ticks; i++) { |
417 | for (i = 0; i <= missed_clock_ticks; i++) { |
336 | clock_update_counters(); |
418 | clock_update_counters(); |
337 | spinlock_lock(&CPU->timeoutlock); |
419 | spinlock_lock(&CPU->timeoutlock); |
338 | while ((l = CPU->timeout_active_head.next) != &CPU->timeout_active_head) { |
420 | while ((l = CPU->timeout_active_head.next) != &CPU->timeout_active_head) { |
339 | h = list_get_instance(l, timeout_t, link); |
421 | h = list_get_instance(l, timeout_t, link); |
340 | spinlock_lock(&h->lock); |
422 | spinlock_lock(&h->lock); |
341 | if (h->ticks-- != 0) { |
423 | if (h->ticks-- != 0) { |
342 | spinlock_unlock(&h->lock); |
424 | spinlock_unlock(&h->lock); |
343 | break; |
425 | break; |
344 | } |
426 | } |
345 | list_remove(l); |
427 | list_remove(l); |
346 | f = h->handler; |
428 | f = h->handler; |
347 | arg = h->arg; |
429 | arg = h->arg; |
348 | timeout_reinitialize(h); |
430 | timeout_reinitialize(h); |
349 | spinlock_unlock(&h->lock); |
431 | spinlock_unlock(&h->lock); |
350 | spinlock_unlock(&CPU->timeoutlock); |
432 | spinlock_unlock(&CPU->timeoutlock); |
351 | 433 | ||
352 | f(arg); |
434 | f(arg); |
353 | 435 | ||
354 | spinlock_lock(&CPU->timeoutlock); |
436 | spinlock_lock(&CPU->timeoutlock); |
355 | } |
437 | } |
356 | spinlock_unlock(&CPU->timeoutlock); |
438 | spinlock_unlock(&CPU->timeoutlock); |
357 | } |
439 | } |
358 | CPU->missed_clock_ticks = 0; |
440 | CPU->missed_clock_ticks = 0; |
359 | 441 | ||
360 | /* |
442 | /* |
361 | * Do CPU usage accounting and find out whether to preempt THREAD. |
443 | * Do CPU usage accounting and find out whether to preempt THREAD. |
362 | */ |
444 | */ |
363 | 445 | ||
364 | if (THREAD) { |
446 | if (THREAD) { |
365 | uint64_t ticks; |
447 | uint64_t ticks; |
366 | 448 | ||
367 | spinlock_lock(&CPU->lock); |
449 | spinlock_lock(&CPU->lock); |
368 | CPU->needs_relink += 1 + missed_clock_ticks; |
450 | CPU->needs_relink += 1 + missed_clock_ticks; |
369 | spinlock_unlock(&CPU->lock); |
451 | spinlock_unlock(&CPU->lock); |
370 | 452 | ||
371 | spinlock_lock(&THREAD->lock); |
453 | spinlock_lock(&THREAD->lock); |
372 | if ((ticks = THREAD->ticks)) { |
454 | if ((ticks = THREAD->ticks)) { |
373 | if (ticks >= 1 + missed_clock_ticks) |
455 | if (ticks >= 1 + missed_clock_ticks) |
374 | THREAD->ticks -= 1 + missed_clock_ticks; |
456 | THREAD->ticks -= 1 + missed_clock_ticks; |
375 | else |
457 | else |
376 | THREAD->ticks = 0; |
458 | THREAD->ticks = 0; |
377 | } |
459 | } |
378 | spinlock_unlock(&THREAD->lock); |
460 | spinlock_unlock(&THREAD->lock); |
379 | 461 | ||
380 | if (!ticks && !PREEMPTION_DISABLED) { |
462 | if (!ticks && !PREEMPTION_DISABLED) { |
381 | scheduler(); |
463 | scheduler(); |
382 | } |
464 | } |
383 | } |
465 | } |
384 | } |
466 | } |
385 | 467 | ||
386 | #endif |
468 | #endif |
387 | /** @} |
469 | /** @} |
388 | */ |
470 | */ |
389 | 471 |