Rev 125 | Rev 404 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 125 | Rev 239 | ||
---|---|---|---|
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 <time/timeout.h> |
29 | #include <time/timeout.h> |
30 | #include <typedefs.h> |
30 | #include <typedefs.h> |
31 | #include <arch/types.h> |
31 | #include <arch/types.h> |
32 | #include <config.h> |
32 | #include <config.h> |
33 | #include <panic.h> |
33 | #include <panic.h> |
34 | #include <synch/spinlock.h> |
34 | #include <synch/spinlock.h> |
35 | #include <func.h> |
35 | #include <func.h> |
36 | #include <cpu.h> |
36 | #include <cpu.h> |
37 | #include <print.h> |
37 | #include <print.h> |
38 | #include <arch/asm.h> |
38 | #include <arch/asm.h> |
39 | #include <arch.h> |
39 | #include <arch.h> |
- | 40 | #include <print.h> |
|
40 | 41 | ||
41 | 42 | ||
42 | /** Initialize timeouts |
43 | /** Initialize timeouts |
43 | * |
44 | * |
44 | * Initialize kernel timeouts. |
45 | * Initialize kernel timeouts. |
45 | * |
46 | * |
46 | */ |
47 | */ |
47 | void timeout_init(void) |
48 | void timeout_init(void) |
48 | { |
49 | { |
49 | spinlock_initialize(&CPU->timeoutlock); |
50 | spinlock_initialize(&CPU->timeoutlock); |
50 | list_initialize(&CPU->timeout_active_head); |
51 | list_initialize(&CPU->timeout_active_head); |
51 | } |
52 | } |
52 | 53 | ||
53 | 54 | ||
54 | /** Initialize empty timeout list |
55 | /** Initialize empty timeout list |
55 | * |
56 | * |
56 | * Initialize the timeout list to be empty. |
57 | * Initialize the timeout list to be empty. |
57 | * |
58 | * |
58 | * @param t Timeout list to be initialized. |
59 | * @param t Timeout list to be initialized. |
59 | * |
60 | * |
60 | */ |
61 | */ |
61 | void timeout_reinitialize(timeout_t *t) |
62 | void timeout_reinitialize(timeout_t *t) |
62 | { |
63 | { |
63 | t->cpu = NULL; |
64 | t->cpu = NULL; |
64 | t->ticks = 0; |
65 | t->ticks = 0; |
65 | t->handler = NULL; |
66 | t->handler = NULL; |
66 | t->arg = NULL; |
67 | t->arg = NULL; |
67 | link_initialize(&t->link); |
68 | link_initialize(&t->link); |
68 | } |
69 | } |
69 | 70 | ||
70 | 71 | ||
71 | /** Initialize timeout list |
72 | /** Initialize timeout list |
72 | * |
73 | * |
73 | * Initialize the timeout list and its spinlock. |
74 | * Initialize the timeout list and its spinlock. |
74 | * |
75 | * |
75 | * @param t Timeout list to be initialized. |
76 | * @param t Timeout list to be initialized. |
76 | * |
77 | * |
77 | */ |
78 | */ |
78 | void timeout_initialize(timeout_t *t) |
79 | void timeout_initialize(timeout_t *t) |
79 | { |
80 | { |
80 | spinlock_initialize(&t->lock); |
81 | spinlock_initialize(&t->lock); |
81 | timeout_reinitialize(t); |
82 | timeout_reinitialize(t); |
82 | } |
83 | } |
83 | 84 | ||
84 | 85 | ||
85 | /** Register timeout callback |
86 | /** Register timeout callback |
86 | * |
87 | * |
87 | * Insert the timeout handler f (with argument arg) |
88 | * Insert the timeout handler f (with argument arg) |
88 | * to the timeout list and make it execute in |
89 | * to the timeout list and make it execute in |
89 | * time microseconds (or slightly more). |
90 | * time microseconds (or slightly more). |
90 | * |
91 | * |
91 | * @param t Timeout list. |
92 | * @param t Timeout list. |
92 | * @patam time Number of usec in the future to execute |
93 | * @patam time Number of usec in the future to execute |
93 | * the handler. |
94 | * the handler. |
94 | * @param f Timeout handler function. |
95 | * @param f Timeout handler function. |
95 | * @param arg Timeout handler argument. |
96 | * @param arg Timeout handler argument. |
96 | * |
97 | * |
97 | */ |
98 | */ |
98 | void timeout_register(timeout_t *t, __u64 time, timeout_handler f, void *arg) |
99 | void timeout_register(timeout_t *t, __u64 time, timeout_handler f, void *arg) |
99 | { |
100 | { |
100 | timeout_t *hlp; |
101 | timeout_t *hlp; |
101 | link_t *l, *m; |
102 | link_t *l, *m; |
102 | pri_t pri; |
103 | pri_t pri; |
103 | __u64 sum; |
104 | __u64 sum; |
104 | 105 | ||
105 | pri = cpu_priority_high(); |
106 | pri = cpu_priority_high(); |
106 | spinlock_lock(&CPU->timeoutlock); |
107 | spinlock_lock(&CPU->timeoutlock); |
107 | spinlock_lock(&t->lock); |
108 | spinlock_lock(&t->lock); |
108 | 109 | ||
109 | if (t->cpu) |
110 | if (t->cpu) |
110 | panic("t->cpu != 0"); |
111 | panic("t->cpu != 0"); |
111 | 112 | ||
112 | t->cpu = CPU; |
113 | t->cpu = CPU; |
113 | t->ticks = us2ticks(time); |
114 | t->ticks = us2ticks(time); |
114 | 115 | ||
115 | t->handler = f; |
116 | t->handler = f; |
116 | t->arg = arg; |
117 | t->arg = arg; |
117 | 118 | ||
118 | /* |
119 | /* |
119 | * Insert t into the active timeouts list according to t->ticks. |
120 | * Insert t into the active timeouts list according to t->ticks. |
120 | */ |
121 | */ |
121 | sum = 0; |
122 | sum = 0; |
122 | l = CPU->timeout_active_head.next; |
123 | l = CPU->timeout_active_head.next; |
123 | while (l != &CPU->timeout_active_head) { |
124 | while (l != &CPU->timeout_active_head) { |
124 | hlp = list_get_instance(l, timeout_t, link); |
125 | hlp = list_get_instance(l, timeout_t, link); |
125 | spinlock_lock(&hlp->lock); |
126 | spinlock_lock(&hlp->lock); |
126 | if (t->ticks < sum + hlp->ticks) { |
127 | if (t->ticks < sum + hlp->ticks) { |
127 | spinlock_unlock(&hlp->lock); |
128 | spinlock_unlock(&hlp->lock); |
128 | break; |
129 | break; |
129 | } |
130 | } |
130 | sum += hlp->ticks; |
131 | sum += hlp->ticks; |
131 | spinlock_unlock(&hlp->lock); |
132 | spinlock_unlock(&hlp->lock); |
132 | l = l->next; |
133 | l = l->next; |
133 | } |
134 | } |
- | 135 | ||
134 | m = l->prev; |
136 | m = l->prev; |
135 | list_prepend(&t->link, m); /* avoid using l->prev */ |
137 | list_prepend(&t->link, m); /* avoid using l->prev */ |
136 | 138 | ||
137 | /* |
139 | /* |
138 | * Adjust t->ticks according to ticks accumulated in h's predecessors. |
140 | * Adjust t->ticks according to ticks accumulated in h's predecessors. |
139 | */ |
141 | */ |
140 | t->ticks -= sum; |
142 | t->ticks -= sum; |
141 | 143 | ||
142 | /* |
144 | /* |
143 | * Decrease ticks of t's immediate succesor by t->ticks. |
145 | * Decrease ticks of t's immediate succesor by t->ticks. |
144 | */ |
146 | */ |
145 | if (l != &CPU->timeout_active_head) { |
147 | if (l != &CPU->timeout_active_head) { |
146 | spinlock_lock(&hlp->lock); |
148 | spinlock_lock(&hlp->lock); |
147 | hlp->ticks -= t->ticks; |
149 | hlp->ticks -= t->ticks; |
148 | spinlock_unlock(&hlp->lock); |
150 | spinlock_unlock(&hlp->lock); |
149 | } |
151 | } |
150 | 152 | ||
151 | spinlock_unlock(&t->lock); |
153 | spinlock_unlock(&t->lock); |
152 | spinlock_unlock(&CPU->timeoutlock); |
154 | spinlock_unlock(&CPU->timeoutlock); |
153 | cpu_priority_restore(pri); |
155 | cpu_priority_restore(pri); |
154 | } |
156 | } |
155 | 157 | ||
156 | 158 | ||
157 | /** Unregister timeout callback |
159 | /** Unregister timeout callback |
158 | * |
160 | * |
159 | * Remove timeout from timeout list. |
161 | * Remove timeout from timeout list. |
160 | * |
162 | * |
161 | * @param t Timeout to unregister. |
163 | * @param t Timeout to unregister. |
162 | * |
164 | * |
163 | */ |
165 | */ |
164 | int timeout_unregister(timeout_t *t) |
166 | int timeout_unregister(timeout_t *t) |
165 | { |
167 | { |
166 | timeout_t *hlp; |
168 | timeout_t *hlp; |
167 | link_t *l; |
169 | link_t *l; |
168 | pri_t pri; |
170 | pri_t pri; |
169 | 171 | ||
170 | grab_locks: |
172 | grab_locks: |
171 | pri = cpu_priority_high(); |
173 | pri = cpu_priority_high(); |
172 | spinlock_lock(&t->lock); |
174 | spinlock_lock(&t->lock); |
173 | if (!t->cpu) { |
175 | if (!t->cpu) { |
174 | spinlock_unlock(&t->lock); |
176 | spinlock_unlock(&t->lock); |
175 | cpu_priority_restore(pri); |
177 | cpu_priority_restore(pri); |
176 | return 0; |
178 | return 0; |
177 | } |
179 | } |
178 | if (!spinlock_trylock(&t->cpu->timeoutlock)) { |
180 | if (!spinlock_trylock(&t->cpu->timeoutlock)) { |
179 | spinlock_unlock(&t->lock); |
181 | spinlock_unlock(&t->lock); |
180 | cpu_priority_restore(pri); |
182 | cpu_priority_restore(pri); |
181 | goto grab_locks; |
183 | goto grab_locks; |
182 | } |
184 | } |
183 | 185 | ||
184 | /* |
186 | /* |
185 | * Now we know for sure that t hasn't been activated yet |
187 | * Now we know for sure that t hasn't been activated yet |
186 | * and is lurking in t->cpu->timeout_active_head queue. |
188 | * and is lurking in t->cpu->timeout_active_head queue. |
187 | */ |
189 | */ |
188 | 190 | ||
189 | l = t->link.next; |
191 | l = t->link.next; |
190 | if (l != &t->cpu->timeout_active_head) { |
192 | if (l != &t->cpu->timeout_active_head) { |
191 | hlp = list_get_instance(l, timeout_t, link); |
193 | hlp = list_get_instance(l, timeout_t, link); |
192 | spinlock_lock(&hlp->lock); |
194 | spinlock_lock(&hlp->lock); |
193 | hlp->ticks += t->ticks; |
195 | hlp->ticks += t->ticks; |
194 | spinlock_unlock(&hlp->lock); |
196 | spinlock_unlock(&hlp->lock); |
195 | } |
197 | } |
196 | 198 | ||
197 | list_remove(&t->link); |
199 | list_remove(&t->link); |
198 | spinlock_unlock(&t->cpu->timeoutlock); |
200 | spinlock_unlock(&t->cpu->timeoutlock); |
199 | 201 | ||
200 | timeout_reinitialize(t); |
202 | timeout_reinitialize(t); |
201 | spinlock_unlock(&t->lock); |
203 | spinlock_unlock(&t->lock); |
202 | 204 | ||
203 | cpu_priority_restore(pri); |
205 | cpu_priority_restore(pri); |
204 | return 1; |
206 | return 1; |
205 | } |
207 | } |
206 | 208 |