Rev 1 | Rev 68 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1 | Rev 15 | ||
---|---|---|---|
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 <synch/spinlock.h> |
33 | #include <synch/spinlock.h> |
34 | #include <func.h> |
34 | #include <func.h> |
35 | #include <cpu.h> |
35 | #include <cpu.h> |
36 | #include <print.h> |
36 | #include <print.h> |
37 | #include <arch/asm.h> |
37 | #include <arch/asm.h> |
38 | #include <arch.h> |
38 | #include <arch.h> |
39 | 39 | ||
40 | void timeout_init(void) |
40 | void timeout_init(void) |
41 | { |
41 | { |
42 | spinlock_initialize(&the->cpu->timeoutlock); |
42 | spinlock_initialize(&CPU->timeoutlock); |
43 | list_initialize(&the->cpu->timeout_active_head); |
43 | list_initialize(&CPU->timeout_active_head); |
44 | } |
44 | } |
45 | 45 | ||
46 | 46 | ||
47 | void timeout_reinitialize(timeout_t *t) |
47 | void timeout_reinitialize(timeout_t *t) |
48 | { |
48 | { |
49 | t->cpu = NULL; |
49 | t->cpu = NULL; |
50 | t->ticks = 0; |
50 | t->ticks = 0; |
51 | t->handler = NULL; |
51 | t->handler = NULL; |
52 | t->arg = NULL; |
52 | t->arg = NULL; |
53 | link_initialize(&t->link); |
53 | link_initialize(&t->link); |
54 | } |
54 | } |
55 | 55 | ||
56 | void timeout_initialize(timeout_t *t) |
56 | void timeout_initialize(timeout_t *t) |
57 | { |
57 | { |
58 | spinlock_initialize(&t->lock); |
58 | spinlock_initialize(&t->lock); |
59 | timeout_reinitialize(t); |
59 | timeout_reinitialize(t); |
60 | } |
60 | } |
61 | 61 | ||
62 | /* |
62 | /* |
63 | * This function registers f for execution in about time microseconds. |
63 | * This function registers f for execution in about time microseconds. |
64 | */ |
64 | */ |
65 | void timeout_register(timeout_t *t, __u64 time, timeout_handler f, void *arg) |
65 | void timeout_register(timeout_t *t, __u64 time, timeout_handler f, void *arg) |
66 | { |
66 | { |
67 | timeout_t *hlp; |
67 | timeout_t *hlp; |
68 | link_t *l, *m; |
68 | link_t *l, *m; |
69 | pri_t pri; |
69 | pri_t pri; |
70 | __u64 sum; |
70 | __u64 sum; |
71 | 71 | ||
72 | pri = cpu_priority_high(); |
72 | pri = cpu_priority_high(); |
73 | spinlock_lock(&the->cpu->timeoutlock); |
73 | spinlock_lock(&CPU->timeoutlock); |
74 | spinlock_lock(&t->lock); |
74 | spinlock_lock(&t->lock); |
75 | 75 | ||
76 | if (t->cpu) |
76 | if (t->cpu) |
77 | panic("timeout_register: t->cpu != 0"); |
77 | panic("timeout_register: t->cpu != 0"); |
78 | 78 | ||
79 | t->cpu = the->cpu; |
79 | t->cpu = CPU; |
80 | t->ticks = us2ticks(time); |
80 | t->ticks = us2ticks(time); |
81 | 81 | ||
82 | t->handler = f; |
82 | t->handler = f; |
83 | t->arg = arg; |
83 | t->arg = arg; |
84 | 84 | ||
85 | /* |
85 | /* |
86 | * Insert t into the active timeouts list according to t->ticks. |
86 | * Insert t into the active timeouts list according to t->ticks. |
87 | */ |
87 | */ |
88 | sum = 0; |
88 | sum = 0; |
89 | l = the->cpu->timeout_active_head.next; |
89 | l = CPU->timeout_active_head.next; |
90 | while (l != &the->cpu->timeout_active_head) { |
90 | while (l != &CPU->timeout_active_head) { |
91 | hlp = list_get_instance(l, timeout_t, link); |
91 | hlp = list_get_instance(l, timeout_t, link); |
92 | spinlock_lock(&hlp->lock); |
92 | spinlock_lock(&hlp->lock); |
93 | if (t->ticks < sum + hlp->ticks) { |
93 | if (t->ticks < sum + hlp->ticks) { |
94 | spinlock_unlock(&hlp->lock); |
94 | spinlock_unlock(&hlp->lock); |
95 | break; |
95 | break; |
96 | } |
96 | } |
97 | sum += hlp->ticks; |
97 | sum += hlp->ticks; |
98 | spinlock_unlock(&hlp->lock); |
98 | spinlock_unlock(&hlp->lock); |
99 | l = l->next; |
99 | l = l->next; |
100 | } |
100 | } |
101 | m = l->prev; |
101 | m = l->prev; |
102 | list_prepend(&t->link, m); /* avoid using l->prev */ |
102 | list_prepend(&t->link, m); /* avoid using l->prev */ |
103 | 103 | ||
104 | /* |
104 | /* |
105 | * Adjust t->ticks according to ticks accumulated in h's predecessors. |
105 | * Adjust t->ticks according to ticks accumulated in h's predecessors. |
106 | */ |
106 | */ |
107 | t->ticks -= sum; |
107 | t->ticks -= sum; |
108 | 108 | ||
109 | /* |
109 | /* |
110 | * Decrease ticks of t's immediate succesor by t->ticks. |
110 | * Decrease ticks of t's immediate succesor by t->ticks. |
111 | */ |
111 | */ |
112 | if (l != &the->cpu->timeout_active_head) { |
112 | if (l != &CPU->timeout_active_head) { |
113 | spinlock_lock(&hlp->lock); |
113 | spinlock_lock(&hlp->lock); |
114 | hlp->ticks -= t->ticks; |
114 | hlp->ticks -= t->ticks; |
115 | spinlock_unlock(&hlp->lock); |
115 | spinlock_unlock(&hlp->lock); |
116 | } |
116 | } |
117 | 117 | ||
118 | spinlock_unlock(&t->lock); |
118 | spinlock_unlock(&t->lock); |
119 | spinlock_unlock(&the->cpu->timeoutlock); |
119 | spinlock_unlock(&CPU->timeoutlock); |
120 | cpu_priority_restore(pri); |
120 | cpu_priority_restore(pri); |
121 | } |
121 | } |
122 | 122 | ||
123 | int timeout_unregister(timeout_t *t) |
123 | int timeout_unregister(timeout_t *t) |
124 | { |
124 | { |
125 | timeout_t *hlp; |
125 | timeout_t *hlp; |
126 | link_t *l; |
126 | link_t *l; |
127 | pri_t pri; |
127 | pri_t pri; |
128 | 128 | ||
129 | grab_locks: |
129 | grab_locks: |
130 | pri = cpu_priority_high(); |
130 | pri = cpu_priority_high(); |
131 | spinlock_lock(&t->lock); |
131 | spinlock_lock(&t->lock); |
132 | if (!t->cpu) { |
132 | if (!t->cpu) { |
133 | spinlock_unlock(&t->lock); |
133 | spinlock_unlock(&t->lock); |
134 | cpu_priority_restore(pri); |
134 | cpu_priority_restore(pri); |
135 | return 0; |
135 | return 0; |
136 | } |
136 | } |
137 | if (!spinlock_trylock(&t->cpu->timeoutlock)) { |
137 | if (!spinlock_trylock(&t->cpu->timeoutlock)) { |
138 | spinlock_unlock(&t->lock); |
138 | spinlock_unlock(&t->lock); |
139 | cpu_priority_restore(pri); |
139 | cpu_priority_restore(pri); |
140 | goto grab_locks; |
140 | goto grab_locks; |
141 | } |
141 | } |
142 | 142 | ||
143 | /* |
143 | /* |
144 | * Now we know for sure that t hasn't been activated yet |
144 | * Now we know for sure that t hasn't been activated yet |
145 | * and is lurking in t->cpu->timeout_active_head queue. |
145 | * and is lurking in t->cpu->timeout_active_head queue. |
146 | */ |
146 | */ |
147 | 147 | ||
148 | l = t->link.next; |
148 | l = t->link.next; |
149 | if (l != &t->cpu->timeout_active_head) { |
149 | if (l != &t->cpu->timeout_active_head) { |
150 | hlp = list_get_instance(l, timeout_t, link); |
150 | hlp = list_get_instance(l, timeout_t, link); |
151 | spinlock_lock(&hlp->lock); |
151 | spinlock_lock(&hlp->lock); |
152 | hlp->ticks += t->ticks; |
152 | hlp->ticks += t->ticks; |
153 | spinlock_unlock(&hlp->lock); |
153 | spinlock_unlock(&hlp->lock); |
154 | } |
154 | } |
155 | 155 | ||
156 | list_remove(&t->link); |
156 | list_remove(&t->link); |
157 | spinlock_unlock(&t->cpu->timeoutlock); |
157 | spinlock_unlock(&t->cpu->timeoutlock); |
158 | 158 | ||
159 | timeout_reinitialize(t); |
159 | timeout_reinitialize(t); |
160 | spinlock_unlock(&t->lock); |
160 | spinlock_unlock(&t->lock); |
161 | 161 | ||
162 | cpu_priority_restore(pri); |
162 | cpu_priority_restore(pri); |
163 | return 1; |
163 | return 1; |
164 | } |
164 | } |
165 | 165 |