Rev 2450 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2450 | Rev 2461 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
3 | * Copyright (C) 2007 Vojtech Mencl |
3 | * Copyright (C) 2007 Vojtech Mencl |
4 | * All rights reserved. |
4 | * All rights reserved. |
5 | * |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions |
7 | * modification, are permitted provided that the following conditions |
8 | * are met: |
8 | * are met: |
9 | * |
9 | * |
10 | * - Redistributions of source code must retain the above copyright |
10 | * - Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. |
11 | * notice, this list of conditions and the following disclaimer. |
12 | * - Redistributions in binary form must reproduce the above copyright |
12 | * - Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the |
13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. |
14 | * documentation and/or other materials provided with the distribution. |
15 | * - The name of the author may not be used to endorse or promote products |
15 | * - The name of the author may not be used to endorse or promote products |
16 | * derived from this software without specific prior written permission. |
16 | * derived from this software without specific prior written permission. |
17 | * |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
28 | */ |
29 | 29 | ||
30 | /** @addtogroup time |
30 | /** @addtogroup time |
31 | * @{ |
31 | * @{ |
32 | */ |
32 | */ |
33 | 33 | ||
34 | /** |
34 | /** |
35 | * @file |
35 | * @file |
36 | * @brief High-level clock interrupt handler. |
36 | * @brief High-level clock interrupt handler. |
37 | * |
37 | * |
38 | * This file contains the clock() function which is the source |
38 | * This file contains the clock() function which is the source |
39 | * of preemption. It is also responsible for executing expired |
39 | * of preemption. It is also responsible for executing expired |
40 | * timeouts. |
40 | * timeouts. |
41 | */ |
41 | */ |
42 | 42 | ||
43 | #include <time/clock.h> |
43 | #include <time/clock.h> |
44 | #include <time/timeout.h> |
44 | #include <time/timeout.h> |
45 | #include <config.h> |
45 | #include <config.h> |
46 | #include <synch/spinlock.h> |
46 | #include <synch/spinlock.h> |
47 | #include <synch/waitq.h> |
47 | #include <synch/waitq.h> |
48 | #include <func.h> |
48 | #include <func.h> |
49 | #include <proc/scheduler.h> |
49 | #include <proc/scheduler.h> |
50 | #include <cpu.h> |
50 | #include <cpu.h> |
51 | #include <arch.h> |
51 | #include <arch.h> |
52 | #include <adt/list.h> |
52 | #include <adt/list.h> |
53 | #include <atomic.h> |
53 | #include <atomic.h> |
54 | #include <proc/thread.h> |
54 | #include <proc/thread.h> |
55 | #include <sysinfo/sysinfo.h> |
55 | #include <sysinfo/sysinfo.h> |
56 | #include <arch/barrier.h> |
56 | #include <arch/barrier.h> |
57 | #include <mm/frame.h> |
57 | #include <mm/frame.h> |
58 | #include <ddi/ddi.h> |
58 | #include <ddi/ddi.h> |
59 | #if defined CONFIG_TIMEOUT_AVL_TREE || defined CONFIG_TIMEOUT_EXTAVL_TREE |
- | |
60 | #include <arch/asm.h> |
- | |
61 | #include <arch/types.h> |
- | |
62 | #include <panic.h> |
- | |
- | 59 | ||
63 | #endif |
60 | |
64 | /* Pointer to variable with uptime */ |
61 | /* Pointer to variable with uptime */ |
65 | uptime_t *uptime; |
62 | uptime_t *uptime; |
66 | 63 | ||
67 | /** Physical memory area of the real time clock */ |
64 | /** Physical memory area of the real time clock */ |
68 | static parea_t clock_parea; |
65 | static parea_t clock_parea; |
69 | 66 | ||
70 | /* Variable holding fragment of second, so that we would update |
67 | /* Variable holding fragment of second, so that we would update |
71 | * seconds correctly |
68 | * seconds correctly |
72 | */ |
69 | */ |
73 | static unative_t secfrag = 0; |
70 | static unative_t secfrag = 0; |
74 | 71 | ||
75 | /** Initialize realtime clock counter |
72 | /** Initialize realtime clock counter |
76 | * |
73 | * |
77 | * The applications (and sometimes kernel) need to access accurate |
74 | * The applications (and sometimes kernel) need to access accurate |
78 | * information about realtime data. We allocate 1 page with these |
75 | * information about realtime data. We allocate 1 page with these |
79 | * data and update it periodically. |
76 | * data and update it periodically. |
80 | */ |
77 | */ |
81 | void clock_counter_init(void) |
78 | void clock_counter_init(void) |
82 | { |
79 | { |
83 | void *faddr; |
80 | void *faddr; |
84 | 81 | ||
85 | faddr = frame_alloc(ONE_FRAME, FRAME_ATOMIC); |
82 | faddr = frame_alloc(ONE_FRAME, FRAME_ATOMIC); |
86 | if (!faddr) |
83 | if (!faddr) |
87 | panic("Cannot allocate page for clock"); |
84 | panic("Cannot allocate page for clock"); |
88 | 85 | ||
89 | uptime = (uptime_t *) PA2KA(faddr); |
86 | uptime = (uptime_t *) PA2KA(faddr); |
90 | 87 | ||
91 | uptime->seconds1 = 0; |
88 | uptime->seconds1 = 0; |
92 | uptime->seconds2 = 0; |
89 | uptime->seconds2 = 0; |
93 | uptime->useconds = 0; |
90 | uptime->useconds = 0; |
94 | 91 | ||
95 | clock_parea.pbase = (uintptr_t) faddr; |
92 | clock_parea.pbase = (uintptr_t) faddr; |
96 | clock_parea.vbase = (uintptr_t) uptime; |
93 | clock_parea.vbase = (uintptr_t) uptime; |
97 | clock_parea.frames = 1; |
94 | clock_parea.frames = 1; |
98 | clock_parea.cacheable = true; |
95 | clock_parea.cacheable = true; |
99 | ddi_parea_register(&clock_parea); |
96 | ddi_parea_register(&clock_parea); |
100 | 97 | ||
101 | /* |
98 | /* |
102 | * Prepare information for the userspace so that it can successfully |
99 | * Prepare information for the userspace so that it can successfully |
103 | * physmem_map() the clock_parea. |
100 | * physmem_map() the clock_parea. |
104 | */ |
101 | */ |
105 | sysinfo_set_item_val("clock.cacheable", NULL, (unative_t) true); |
102 | sysinfo_set_item_val("clock.cacheable", NULL, (unative_t) true); |
106 | sysinfo_set_item_val("clock.faddr", NULL, (unative_t) faddr); |
103 | sysinfo_set_item_val("clock.faddr", NULL, (unative_t) faddr); |
107 | } |
104 | } |
108 | 105 | ||
109 | 106 | ||
110 | /** Update public counters |
107 | /** Update public counters |
111 | * |
108 | * |
112 | * Update it only on first processor |
109 | * Update it only on first processor |
113 | * TODO: Do we really need so many write barriers? |
110 | * TODO: Do we really need so many write barriers? |
114 | */ |
111 | */ |
115 | static void clock_update_counters(void) |
112 | static void clock_update_counters(void) |
116 | { |
113 | { |
117 | if (CPU->id == 0) { |
114 | if (CPU->id == 0) { |
118 | secfrag += 1000000 / HZ; |
115 | secfrag += 1000000 / HZ; |
119 | if (secfrag >= 1000000) { |
116 | if (secfrag >= 1000000) { |
120 | secfrag -= 1000000; |
117 | secfrag -= 1000000; |
121 | uptime->seconds1++; |
118 | uptime->seconds1++; |
122 | write_barrier(); |
119 | write_barrier(); |
123 | uptime->useconds = secfrag; |
120 | uptime->useconds = secfrag; |
124 | write_barrier(); |
121 | write_barrier(); |
125 | uptime->seconds2 = uptime->seconds1; |
122 | uptime->seconds2 = uptime->seconds1; |
126 | } else |
123 | } else |
127 | uptime->useconds += 1000000 / HZ; |
124 | uptime->useconds += 1000000 / HZ; |
128 | } |
125 | } |
129 | } |
126 | } |
130 | 127 | ||
131 | #if defined CONFIG_TIMEOUT_AVL_TREE |
128 | #if defined CONFIG_TIMEOUT_AVL_TREE |
132 | 129 | ||
133 | /** Clock routine |
130 | /** Clock routine |
134 | * |
131 | * |
135 | * Clock routine executed from clock interrupt handler |
132 | * Clock routine executed from clock interrupt handler |
136 | * (assuming interrupts_disable()'d). Runs expired timeouts |
133 | * (assuming interrupts_disable()'d). Runs expired timeouts |
137 | * and preemptive scheduling. |
134 | * and preemptive scheduling. |
138 | * |
135 | * |
139 | */ |
136 | */ |
140 | void clock(void) |
137 | void clock(void) |
141 | { |
138 | { |
142 | timeout_t *h; |
139 | timeout_t *h; |
143 | timeout_handler_t f; |
140 | timeout_handler_t f; |
144 | void *arg; |
141 | void *arg; |
145 | count_t missed_clock_ticks = CPU->missed_clock_ticks; |
142 | count_t missed_clock_ticks = CPU->missed_clock_ticks; |
146 | uint64_t i = CPU->timeout_active_tree.base; |
143 | uint64_t i = CPU->timeout_active_tree.base; |
147 | uint64_t last_clock_tick = i + missed_clock_ticks; |
144 | uint64_t last_clock_tick = i + missed_clock_ticks; |
148 | avltree_node_t *expnode; |
145 | avltree_node_t *expnode; |
149 | 146 | ||
150 | /* |
147 | /* |
151 | * To avoid lock ordering problems, |
148 | * To avoid lock ordering problems, |
152 | * run all expired timeouts as you visit them. |
149 | * run all expired timeouts as you visit them. |
153 | */ |
150 | */ |
154 | - | ||
155 | 151 | ||
156 | for (; i <= last_clock_tick; i++) { |
152 | for (; i <= last_clock_tick; i++) { |
157 | clock_update_counters(); |
153 | clock_update_counters(); |
158 | spinlock_lock(&CPU->timeoutlock); |
154 | spinlock_lock(&CPU->timeoutlock); |
159 | 155 | ||
160 | /* |
156 | /* |
161 | * Check whether first timeout (with the smallest key in the tree) time out. If so perform |
157 | * Check whether first timeout (with the smallest key in the tree) time out. If so perform |
162 | * callback function and try next timeout (more timeouts can have same timeout). |
158 | * callback function and try next timeout (more timeouts can have same timeout). |
163 | */ |
159 | */ |
164 | while ((expnode = avltree_find_min(&CPU->timeout_active_tree)) != NULL) { |
160 | while ((expnode = avltree_find_min(&CPU->timeout_active_tree)) != NULL) { |
165 | h = avltree_get_instance(expnode,timeout_t,node); |
161 | h = avltree_get_instance(expnode,timeout_t,node); |
166 | spinlock_lock(&h->lock); |
162 | spinlock_lock(&h->lock); |
167 | if (expnode->key != i) { |
163 | if (expnode->key != i) { |
168 | /* |
164 | /* |
169 | * Base is increased every for cycle. |
165 | * Base is increased every for cycle. |
170 | */ |
166 | */ |
171 | (CPU->timeout_active_tree.base)++; |
167 | (CPU->timeout_active_tree.base)++; |
172 | spinlock_unlock(&h->lock); |
168 | spinlock_unlock(&h->lock); |
173 | break; |
169 | break; |
174 | } |
170 | } |
175 | 171 | ||
176 | /* |
172 | /* |
177 | * Delete minimal key from the tree and repair tree structure in |
173 | * Delete minimal key from the tree and repair tree structure in |
178 | * logarithmic time. |
174 | * logarithmic time. |
179 | */ |
175 | */ |
180 | avltree_delete_min(&CPU->timeout_active_tree); |
176 | avltree_delete_min(&CPU->timeout_active_tree); |
181 | 177 | ||
- | 178 | f = h->handler; |
|
- | 179 | arg = h->arg; |
|
- | 180 | timeout_reinitialize(h); |
|
- | 181 | spinlock_unlock(&h->lock); |
|
- | 182 | spinlock_unlock(&CPU->timeoutlock); |
|
- | 183 | ||
- | 184 | f(arg); |
|
- | 185 | ||
- | 186 | spinlock_lock(&CPU->timeoutlock); |
|
- | 187 | } |
|
- | 188 | spinlock_unlock(&CPU->timeoutlock); |
|
- | 189 | } |
|
- | 190 | ||
- | 191 | CPU->missed_clock_ticks = 0; |
|
- | 192 | ||
- | 193 | /* |
|
- | 194 | * Do CPU usage accounting and find out whether to preempt THREAD. |
|
- | 195 | */ |
|
- | 196 | if (THREAD) { |
|
- | 197 | uint64_t ticks; |
|
- | 198 | ||
- | 199 | spinlock_lock(&CPU->lock); |
|
- | 200 | CPU->needs_relink += 1 + missed_clock_ticks; |
|
- | 201 | spinlock_unlock(&CPU->lock); |
|
- | 202 | ||
- | 203 | spinlock_lock(&THREAD->lock); |
|
- | 204 | if ((ticks = THREAD->ticks)) { |
|
- | 205 | if (ticks >= 1 + missed_clock_ticks) |
|
- | 206 | THREAD->ticks -= 1 + missed_clock_ticks; |
|
- | 207 | else |
|
- | 208 | THREAD->ticks = 0; |
|
- | 209 | } |
|
- | 210 | spinlock_unlock(&THREAD->lock); |
|
- | 211 | ||
- | 212 | if (!ticks && !PREEMPTION_DISABLED) { |
|
- | 213 | scheduler(); |
|
- | 214 | } |
|
- | 215 | } |
|
- | 216 | } |
|
- | 217 | ||
- | 218 | #elif defined CONFIG_TIMEOUT_FAVL_TREE |
|
- | 219 | ||
- | 220 | /** Clock routine |
|
- | 221 | * |
|
- | 222 | * Clock routine executed from clock interrupt handler |
|
- | 223 | * (assuming interrupts_disable()'d). Runs expired timeouts |
|
- | 224 | * and preemptive scheduling. |
|
- | 225 | * |
|
- | 226 | */ |
|
- | 227 | void clock(void) |
|
- | 228 | { |
|
- | 229 | timeout_t *h; |
|
- | 230 | timeout_handler_t f; |
|
- | 231 | void *arg; |
|
- | 232 | count_t missed_clock_ticks = CPU->missed_clock_ticks; |
|
- | 233 | uint64_t i = CPU->timeout_active_tree.base; |
|
- | 234 | uint64_t last_clock_tick = i + missed_clock_ticks; |
|
- | 235 | favltree_node_t *expnode; |
|
- | 236 | ||
- | 237 | /* |
|
- | 238 | * To avoid lock ordering problems, |
|
- | 239 | * run all expired timeouts as you visit them. |
|
- | 240 | */ |
|
- | 241 | ||
- | 242 | for (; i <= last_clock_tick; i++) { |
|
- | 243 | clock_update_counters(); |
|
- | 244 | spinlock_lock(&CPU->timeoutlock); |
|
- | 245 | ||
- | 246 | /* |
|
- | 247 | * Check whether first timeout (with the smallest key in the tree) time out. If so perform |
|
- | 248 | * callback function and try next timeout (more timeouts can have same timeout). |
|
- | 249 | * Function favltree_find_min works in contant time. |
|
- | 250 | */ |
|
- | 251 | while ((expnode = favltree_find_min(&CPU->timeout_active_tree)) != NULL) { |
|
- | 252 | h = favltree_get_instance(expnode,timeout_t,node); |
|
- | 253 | spinlock_lock(&h->lock); |
|
- | 254 | if (expnode->key != i) { |
|
- | 255 | /* |
|
- | 256 | * Base is increased every for cycle. |
|
- | 257 | */ |
|
- | 258 | (CPU->timeout_active_tree.base)++; |
|
- | 259 | spinlock_unlock(&h->lock); |
|
- | 260 | break; |
|
- | 261 | } |
|
- | 262 | ||
- | 263 | /* |
|
- | 264 | * Delete minimal key from the tree and repair tree structure in |
|
- | 265 | * logarithmic time. |
|
- | 266 | */ |
|
- | 267 | favltree_delete_min(&CPU->timeout_active_tree); |
|
- | 268 | ||
182 | f = h->handler; |
269 | f = h->handler; |
183 | arg = h->arg; |
270 | arg = h->arg; |
184 | timeout_reinitialize(h); |
271 | timeout_reinitialize(h); |
185 | spinlock_unlock(&h->lock); |
272 | spinlock_unlock(&h->lock); |
186 | spinlock_unlock(&CPU->timeoutlock); |
273 | spinlock_unlock(&CPU->timeoutlock); |
187 | 274 | ||
188 | f(arg); |
275 | f(arg); |
189 | 276 | ||
190 | spinlock_lock(&CPU->timeoutlock); |
277 | spinlock_lock(&CPU->timeoutlock); |
191 | } |
278 | } |
192 | spinlock_unlock(&CPU->timeoutlock); |
279 | spinlock_unlock(&CPU->timeoutlock); |
193 | } |
280 | } |
194 | 281 | ||
195 | CPU->missed_clock_ticks = 0; |
282 | CPU->missed_clock_ticks = 0; |
196 | 283 | ||
197 | /* |
284 | /* |
198 | * Do CPU usage accounting and find out whether to preempt THREAD. |
285 | * Do CPU usage accounting and find out whether to preempt THREAD. |
199 | */ |
286 | */ |
200 | if (THREAD) { |
287 | if (THREAD) { |
201 | uint64_t ticks; |
288 | uint64_t ticks; |
202 | 289 | ||
203 | spinlock_lock(&CPU->lock); |
290 | spinlock_lock(&CPU->lock); |
204 | CPU->needs_relink += 1 + missed_clock_ticks; |
291 | CPU->needs_relink += 1 + missed_clock_ticks; |
205 | spinlock_unlock(&CPU->lock); |
292 | spinlock_unlock(&CPU->lock); |
206 | 293 | ||
207 | spinlock_lock(&THREAD->lock); |
294 | spinlock_lock(&THREAD->lock); |
208 | if ((ticks = THREAD->ticks)) { |
295 | if ((ticks = THREAD->ticks)) { |
209 | if (ticks >= 1 + missed_clock_ticks) |
296 | if (ticks >= 1 + missed_clock_ticks) |
210 | THREAD->ticks -= 1 + missed_clock_ticks; |
297 | THREAD->ticks -= 1 + missed_clock_ticks; |
211 | else |
298 | else |
212 | THREAD->ticks = 0; |
299 | THREAD->ticks = 0; |
213 | } |
300 | } |
214 | spinlock_unlock(&THREAD->lock); |
301 | spinlock_unlock(&THREAD->lock); |
215 | 302 | ||
216 | if (!ticks && !PREEMPTION_DISABLED) { |
303 | if (!ticks && !PREEMPTION_DISABLED) { |
217 | scheduler(); |
304 | scheduler(); |
218 | } |
305 | } |
219 | } |
306 | } |
220 | } |
307 | } |
221 | 308 | ||
222 | #elif defined CONFIG_TIMEOUT_EXTAVL_TREE |
309 | #elif defined CONFIG_TIMEOUT_EXTAVL_TREE |
223 | 310 | ||
224 | /** Clock routine |
311 | /** Clock routine |
225 | * |
312 | * |
226 | * Clock routine executed from clock interrupt handler |
313 | * Clock routine executed from clock interrupt handler |
227 | * (assuming interrupts_disable()'d). Runs expired timeouts |
314 | * (assuming interrupts_disable()'d). Runs expired timeouts |
228 | * and preemptive scheduling. |
315 | * and preemptive scheduling. |
229 | * |
316 | * |
230 | */ |
317 | */ |
231 | void clock(void) |
318 | void clock(void) |
232 | { |
319 | { |
233 | timeout_t *h; |
320 | timeout_t *h; |
234 | timeout_handler_t f; |
321 | timeout_handler_t f; |
235 | void *arg; |
322 | void *arg; |
236 | count_t missed_clock_ticks = CPU->missed_clock_ticks; |
323 | count_t missed_clock_ticks = CPU->missed_clock_ticks; |
237 | uint64_t i = CPU->timeout_active_tree.base; |
324 | uint64_t i = CPU->timeout_active_tree.base; |
238 | uint64_t last_clock_tick = i + missed_clock_ticks; |
325 | uint64_t last_clock_tick = i + missed_clock_ticks; |
239 | extavltree_node_t *expnode; |
326 | extavltree_node_t *expnode; |
240 | //ipl_t ipl; |
327 | //ipl_t ipl; |
241 | 328 | ||
242 | /* |
329 | /* |
243 | * To avoid lock ordering problems, |
330 | * To avoid lock ordering problems, |
244 | * run all expired timeouts as you visit them. |
331 | * run all expired timeouts as you visit them. |
245 | */ |
332 | */ |
246 | 333 | ||
247 | for (; i <= last_clock_tick; i++) { |
334 | for (; i <= last_clock_tick; i++) { |
248 | clock_update_counters(); |
335 | clock_update_counters(); |
249 | spinlock_lock(&CPU->timeoutlock); |
336 | spinlock_lock(&CPU->timeoutlock); |
250 | 337 | ||
251 | /* |
338 | /* |
252 | * Check whether first timeout in list time out. If so perform callback function and try |
339 | * Check whether first timeout in list time out. If so perform callback function and try |
253 | * next timeout (more timeouts can have same timeout). |
340 | * next timeout (more timeouts can have same timeout). |
254 | */ |
341 | */ |
255 | while ((expnode = CPU->timeout_active_tree.head.next) != &(CPU->timeout_active_tree.head)) { |
342 | while ((expnode = CPU->timeout_active_tree.head.next) != &(CPU->timeout_active_tree.head)) { |
256 | h = extavltree_get_instance(expnode,timeout_t,node); |
343 | h = extavltree_get_instance(expnode,timeout_t,node); |
257 | spinlock_lock(&h->lock); |
344 | spinlock_lock(&h->lock); |
258 | if (expnode->key != i) { |
345 | if (expnode->key != i) { |
259 | /* |
346 | /* |
260 | * Base is increased every for cycle. |
347 | * Base is increased every for cycle. |
261 | */ |
348 | */ |
262 | (CPU->timeout_active_tree.base)++; |
349 | (CPU->timeout_active_tree.base)++; |
263 | spinlock_unlock(&h->lock); |
350 | spinlock_unlock(&h->lock); |
264 | break; |
351 | break; |
265 | } |
352 | } |
266 | 353 | ||
267 | /* |
354 | /* |
268 | * Delete first node in the list and repair tree structure in |
355 | * Delete first node in the list and repair tree structure in |
269 | * constant time. |
356 | * constant time. |
270 | */ |
357 | */ |
271 | extavltree_delete_min(&CPU->timeout_active_tree); |
358 | extavltree_delete_min(&CPU->timeout_active_tree); |
272 | 359 | ||
273 | f = h->handler; |
360 | f = h->handler; |
274 | arg = h->arg; |
361 | arg = h->arg; |
275 | timeout_reinitialize(h); |
362 | timeout_reinitialize(h); |
276 | spinlock_unlock(&h->lock); |
363 | spinlock_unlock(&h->lock); |
277 | spinlock_unlock(&CPU->timeoutlock); |
364 | spinlock_unlock(&CPU->timeoutlock); |
278 | 365 | ||
279 | f(arg); |
366 | f(arg); |
280 | 367 | ||
281 | spinlock_lock(&CPU->timeoutlock); |
368 | spinlock_lock(&CPU->timeoutlock); |
282 | } |
369 | } |
283 | spinlock_unlock(&CPU->timeoutlock); |
370 | spinlock_unlock(&CPU->timeoutlock); |
284 | } |
371 | } |
285 | 372 | ||
286 | CPU->missed_clock_ticks = 0; |
373 | CPU->missed_clock_ticks = 0; |
287 | 374 | ||
288 | /* |
375 | /* |
289 | * Do CPU usage accounting and find out whether to preempt THREAD. |
376 | * Do CPU usage accounting and find out whether to preempt THREAD. |
290 | */ |
377 | */ |
291 | if (THREAD) { |
378 | if (THREAD) { |
292 | uint64_t ticks; |
379 | uint64_t ticks; |
293 | 380 | ||
294 | spinlock_lock(&CPU->lock); |
381 | spinlock_lock(&CPU->lock); |
295 | CPU->needs_relink += 1 + missed_clock_ticks; |
382 | CPU->needs_relink += 1 + missed_clock_ticks; |
296 | spinlock_unlock(&CPU->lock); |
383 | spinlock_unlock(&CPU->lock); |
297 | 384 | ||
298 | spinlock_lock(&THREAD->lock); |
385 | spinlock_lock(&THREAD->lock); |
299 | if ((ticks = THREAD->ticks)) { |
386 | if ((ticks = THREAD->ticks)) { |
300 | if (ticks >= 1 + missed_clock_ticks) |
387 | if (ticks >= 1 + missed_clock_ticks) |
301 | THREAD->ticks -= 1 + missed_clock_ticks; |
388 | THREAD->ticks -= 1 + missed_clock_ticks; |
302 | else |
389 | else |
303 | THREAD->ticks = 0; |
390 | THREAD->ticks = 0; |
304 | } |
391 | } |
305 | spinlock_unlock(&THREAD->lock); |
392 | spinlock_unlock(&THREAD->lock); |
306 | 393 | ||
307 | if (!ticks && !PREEMPTION_DISABLED) { |
394 | if (!ticks && !PREEMPTION_DISABLED) { |
308 | scheduler(); |
395 | scheduler(); |
309 | } |
396 | } |
310 | } |
397 | } |
311 | } |
398 | } |
312 | 399 | ||
313 | #elif defined CONFIG_TIMEOUT_EXTAVLREL_TREE |
400 | #elif defined CONFIG_TIMEOUT_EXTAVLREL_TREE |
314 | 401 | ||
315 | /** Clock routine |
402 | /** Clock routine |
316 | * |
403 | * |
317 | * Clock routine executed from clock interrupt handler |
404 | * Clock routine executed from clock interrupt handler |
318 | * (assuming interrupts_disable()'d). Runs expired timeouts |
405 | * (assuming interrupts_disable()'d). Runs expired timeouts |
319 | * and preemptive scheduling. |
406 | * and preemptive scheduling. |
320 | * |
407 | * |
321 | */ |
408 | */ |
322 | void clock(void) |
409 | void clock(void) |
323 | { |
410 | { |
324 | extavlreltree_node_t *expnode; |
411 | extavlreltree_node_t *expnode; |
325 | timeout_t *h; |
412 | timeout_t *h; |
326 | timeout_handler_t f; |
413 | timeout_handler_t f; |
327 | void *arg; |
414 | void *arg; |
328 | count_t missed_clock_ticks = CPU->missed_clock_ticks; |
415 | count_t missed_clock_ticks = CPU->missed_clock_ticks; |
329 | int i; |
416 | int i; |
330 | 417 | ||
331 | /* |
418 | /* |
332 | * To avoid lock ordering problems, |
419 | * To avoid lock ordering problems, |
333 | * run all expired timeouts as you visit them. |
420 | * run all expired timeouts as you visit them. |
334 | */ |
421 | */ |
335 | for (i = 0; i <= missed_clock_ticks; i++) { |
422 | for (i = 0; i <= missed_clock_ticks; i++) { |
336 | clock_update_counters(); |
423 | clock_update_counters(); |
337 | spinlock_lock(&CPU->timeoutlock); |
424 | spinlock_lock(&CPU->timeoutlock); |
338 | 425 | ||
339 | /* |
426 | /* |
340 | * Check whether first timeout in list time out. If so perform callback function and try |
427 | * Check whether first timeout in list time out. If so perform callback function and try |
341 | * next timeout (more timeouts can have same timeout). |
428 | * next timeout (more timeouts can have same timeout). |
342 | */ |
429 | */ |
343 | while ((expnode = CPU->timeout_active_tree.head.next) != &(CPU->timeout_active_tree.head)) { |
430 | while ((expnode = CPU->timeout_active_tree.head.next) != &(CPU->timeout_active_tree.head)) { |
344 | h = extavlreltree_get_instance(expnode,timeout_t,node); |
431 | h = extavlreltree_get_instance(expnode,timeout_t,node); |
345 | spinlock_lock(&h->lock); |
432 | spinlock_lock(&h->lock); |
346 | if (expnode->key != 0) { |
433 | if (expnode->key != 0) { |
347 | expnode->key--; |
434 | expnode->key--; |
348 | spinlock_unlock(&h->lock); |
435 | spinlock_unlock(&h->lock); |
349 | break; |
436 | break; |
350 | } |
437 | } |
351 | 438 | ||
352 | /* |
439 | /* |
353 | * Delete first node in the list and repair tree structure in |
440 | * Delete first node in the list and repair tree structure in |
354 | * constant time. Be careful of expnode's key, it must be 0! |
441 | * constant time. Be careful of expnode's key, it must be 0! |
355 | */ |
442 | */ |
356 | extavlreltree_delete_min(&CPU->timeout_active_tree); |
443 | extavlreltree_delete_min(&CPU->timeout_active_tree); |
357 | 444 | ||
358 | f = h->handler; |
445 | f = h->handler; |
359 | arg = h->arg; |
446 | arg = h->arg; |
360 | timeout_reinitialize(h); |
447 | timeout_reinitialize(h); |
361 | spinlock_unlock(&h->lock); |
448 | spinlock_unlock(&h->lock); |
362 | spinlock_unlock(&CPU->timeoutlock); |
449 | spinlock_unlock(&CPU->timeoutlock); |
363 | 450 | ||
364 | f(arg); |
451 | f(arg); |
365 | 452 | ||
366 | spinlock_lock(&CPU->timeoutlock); |
453 | spinlock_lock(&CPU->timeoutlock); |
367 | } |
454 | } |
368 | spinlock_unlock(&CPU->timeoutlock); |
455 | spinlock_unlock(&CPU->timeoutlock); |
369 | } |
456 | } |
370 | CPU->missed_clock_ticks = 0; |
457 | CPU->missed_clock_ticks = 0; |
371 | 458 | ||
372 | /* |
459 | /* |
373 | * Do CPU usage accounting and find out whether to preempt THREAD. |
460 | * Do CPU usage accounting and find out whether to preempt THREAD. |
374 | */ |
461 | */ |
375 | 462 | ||
376 | if (THREAD) { |
463 | if (THREAD) { |
377 | uint64_t ticks; |
464 | uint64_t ticks; |
378 | 465 | ||
379 | spinlock_lock(&CPU->lock); |
466 | spinlock_lock(&CPU->lock); |
380 | CPU->needs_relink += 1 + missed_clock_ticks; |
467 | CPU->needs_relink += 1 + missed_clock_ticks; |
381 | spinlock_unlock(&CPU->lock); |
468 | spinlock_unlock(&CPU->lock); |
382 | 469 | ||
383 | spinlock_lock(&THREAD->lock); |
470 | spinlock_lock(&THREAD->lock); |
384 | if ((ticks = THREAD->ticks)) { |
471 | if ((ticks = THREAD->ticks)) { |
385 | if (ticks >= 1 + missed_clock_ticks) |
472 | if (ticks >= 1 + missed_clock_ticks) |
386 | THREAD->ticks -= 1 + missed_clock_ticks; |
473 | THREAD->ticks -= 1 + missed_clock_ticks; |
387 | else |
474 | else |
388 | THREAD->ticks = 0; |
475 | THREAD->ticks = 0; |
389 | } |
476 | } |
390 | spinlock_unlock(&THREAD->lock); |
477 | spinlock_unlock(&THREAD->lock); |
391 | 478 | ||
392 | if (!ticks && !PREEMPTION_DISABLED) { |
479 | if (!ticks && !PREEMPTION_DISABLED) { |
393 | scheduler(); |
480 | scheduler(); |
394 | } |
481 | } |
395 | } |
482 | } |
396 | } |
483 | } |
397 | 484 | ||
398 | 485 | ||
399 | 486 | ||
400 | #else |
487 | #else |
401 | 488 | ||
402 | 489 | ||
403 | /** Clock routine |
490 | /** Clock routine |
404 | * |
491 | * |
405 | * Clock routine executed from clock interrupt handler |
492 | * Clock routine executed from clock interrupt handler |
406 | * (assuming interrupts_disable()'d). Runs expired timeouts |
493 | * (assuming interrupts_disable()'d). Runs expired timeouts |
407 | * and preemptive scheduling. |
494 | * and preemptive scheduling. |
408 | * |
495 | * |
409 | */ |
496 | */ |
410 | void clock(void) |
497 | void clock(void) |
411 | { |
498 | { |
412 | link_t *l; |
499 | link_t *l; |
413 | timeout_t *h; |
500 | timeout_t *h; |
414 | timeout_handler_t f; |
501 | timeout_handler_t f; |
415 | void *arg; |
502 | void *arg; |
416 | count_t missed_clock_ticks = CPU->missed_clock_ticks; |
503 | count_t missed_clock_ticks = CPU->missed_clock_ticks; |
417 | int i; |
504 | int i; |
418 | 505 | ||
419 | /* |
506 | /* |
420 | * To avoid lock ordering problems, |
507 | * To avoid lock ordering problems, |
421 | * run all expired timeouts as you visit them. |
508 | * run all expired timeouts as you visit them. |
422 | */ |
509 | */ |
423 | for (i = 0; i <= missed_clock_ticks; i++) { |
510 | for (i = 0; i <= missed_clock_ticks; i++) { |
424 | clock_update_counters(); |
511 | clock_update_counters(); |
425 | spinlock_lock(&CPU->timeoutlock); |
512 | spinlock_lock(&CPU->timeoutlock); |
426 | while ((l = CPU->timeout_active_head.next) != &CPU->timeout_active_head) { |
513 | while ((l = CPU->timeout_active_head.next) != &CPU->timeout_active_head) { |
427 | h = list_get_instance(l, timeout_t, link); |
514 | h = list_get_instance(l, timeout_t, link); |
428 | spinlock_lock(&h->lock); |
515 | spinlock_lock(&h->lock); |
429 | if (h->ticks-- != 0) { |
516 | if (h->ticks-- != 0) { |
430 | spinlock_unlock(&h->lock); |
517 | spinlock_unlock(&h->lock); |
431 | break; |
518 | break; |
432 | } |
519 | } |
433 | list_remove(l); |
520 | list_remove(l); |
434 | f = h->handler; |
521 | f = h->handler; |
435 | arg = h->arg; |
522 | arg = h->arg; |
436 | timeout_reinitialize(h); |
523 | timeout_reinitialize(h); |
437 | spinlock_unlock(&h->lock); |
524 | spinlock_unlock(&h->lock); |
438 | spinlock_unlock(&CPU->timeoutlock); |
525 | spinlock_unlock(&CPU->timeoutlock); |
439 | 526 | ||
440 | f(arg); |
527 | f(arg); |
441 | 528 | ||
442 | spinlock_lock(&CPU->timeoutlock); |
529 | spinlock_lock(&CPU->timeoutlock); |
443 | } |
530 | } |
444 | spinlock_unlock(&CPU->timeoutlock); |
531 | spinlock_unlock(&CPU->timeoutlock); |
445 | } |
532 | } |
446 | CPU->missed_clock_ticks = 0; |
533 | CPU->missed_clock_ticks = 0; |
447 | 534 | ||
448 | /* |
535 | /* |
449 | * Do CPU usage accounting and find out whether to preempt THREAD. |
536 | * Do CPU usage accounting and find out whether to preempt THREAD. |
450 | */ |
537 | */ |
451 | 538 | ||
452 | if (THREAD) { |
539 | if (THREAD) { |
453 | uint64_t ticks; |
540 | uint64_t ticks; |
454 | 541 | ||
455 | spinlock_lock(&CPU->lock); |
542 | spinlock_lock(&CPU->lock); |
456 | CPU->needs_relink += 1 + missed_clock_ticks; |
543 | CPU->needs_relink += 1 + missed_clock_ticks; |
457 | spinlock_unlock(&CPU->lock); |
544 | spinlock_unlock(&CPU->lock); |
458 | 545 | ||
459 | spinlock_lock(&THREAD->lock); |
546 | spinlock_lock(&THREAD->lock); |
460 | if ((ticks = THREAD->ticks)) { |
547 | if ((ticks = THREAD->ticks)) { |
461 | if (ticks >= 1 + missed_clock_ticks) |
548 | if (ticks >= 1 + missed_clock_ticks) |
462 | THREAD->ticks -= 1 + missed_clock_ticks; |
549 | THREAD->ticks -= 1 + missed_clock_ticks; |
463 | else |
550 | else |
464 | THREAD->ticks = 0; |
551 | THREAD->ticks = 0; |
465 | } |
552 | } |
466 | spinlock_unlock(&THREAD->lock); |
553 | spinlock_unlock(&THREAD->lock); |
467 | 554 | ||
468 | if (!ticks && !PREEMPTION_DISABLED) { |
555 | if (!ticks && !PREEMPTION_DISABLED) { |
469 | scheduler(); |
556 | scheduler(); |
470 | } |
557 | } |
471 | } |
558 | } |
472 | } |
559 | } |
473 | 560 | ||
474 | #endif |
561 | #endif |
475 | /** @} |
562 | /** @} |
476 | */ |
563 | */ |
477 | 564 |