1 | /*
|
---|
2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
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
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
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
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
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
|
---|
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
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup kernel_time
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file
|
---|
35 | * @brief Timeout management functions.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <time/timeout.h>
|
---|
39 | #include <typedefs.h>
|
---|
40 | #include <config.h>
|
---|
41 | #include <panic.h>
|
---|
42 | #include <synch/spinlock.h>
|
---|
43 | #include <halt.h>
|
---|
44 | #include <cpu.h>
|
---|
45 | #include <arch/asm.h>
|
---|
46 | #include <arch.h>
|
---|
47 |
|
---|
48 | /** Initialize timeouts
|
---|
49 | *
|
---|
50 | * Initialize kernel timeouts.
|
---|
51 | *
|
---|
52 | */
|
---|
53 | void timeout_init(void)
|
---|
54 | {
|
---|
55 | irq_spinlock_initialize(&CPU->timeoutlock, "cpu.timeoutlock");
|
---|
56 | list_initialize(&CPU->timeout_active_list);
|
---|
57 | }
|
---|
58 |
|
---|
59 | /** Reinitialize timeout
|
---|
60 | *
|
---|
61 | * Initialize all members except the lock.
|
---|
62 | *
|
---|
63 | * @param timeout Timeout to be initialized.
|
---|
64 | *
|
---|
65 | */
|
---|
66 | void timeout_reinitialize(timeout_t *timeout)
|
---|
67 | {
|
---|
68 | timeout->cpu = NULL;
|
---|
69 | timeout->ticks = 0;
|
---|
70 | timeout->handler = NULL;
|
---|
71 | timeout->arg = NULL;
|
---|
72 | link_initialize(&timeout->link);
|
---|
73 | }
|
---|
74 |
|
---|
75 | /** Initialize timeout
|
---|
76 | *
|
---|
77 | * Initialize all members including the lock.
|
---|
78 | *
|
---|
79 | * @param timeout Timeout to be initialized.
|
---|
80 | *
|
---|
81 | */
|
---|
82 | void timeout_initialize(timeout_t *timeout)
|
---|
83 | {
|
---|
84 | irq_spinlock_initialize(&timeout->lock, "timeout_t_lock");
|
---|
85 | timeout_reinitialize(timeout);
|
---|
86 | }
|
---|
87 |
|
---|
88 | /** Register timeout
|
---|
89 | *
|
---|
90 | * Insert timeout handler f (with argument arg)
|
---|
91 | * to timeout list and make it execute in
|
---|
92 | * time microseconds (or slightly more).
|
---|
93 | *
|
---|
94 | * @param timeout Timeout structure.
|
---|
95 | * @param time Number of usec in the future to execute the handler.
|
---|
96 | * @param handler Timeout handler function.
|
---|
97 | * @param arg Timeout handler argument.
|
---|
98 | *
|
---|
99 | */
|
---|
100 | void timeout_register(timeout_t *timeout, uint64_t time,
|
---|
101 | timeout_handler_t handler, void *arg)
|
---|
102 | {
|
---|
103 | irq_spinlock_lock(&CPU->timeoutlock, true);
|
---|
104 | irq_spinlock_lock(&timeout->lock, false);
|
---|
105 |
|
---|
106 | if (timeout->cpu)
|
---|
107 | panic("Unexpected: timeout->cpu != 0.");
|
---|
108 |
|
---|
109 | timeout->cpu = CPU;
|
---|
110 | timeout->ticks = us2ticks(time);
|
---|
111 |
|
---|
112 | timeout->handler = handler;
|
---|
113 | timeout->arg = arg;
|
---|
114 |
|
---|
115 | /*
|
---|
116 | * Insert timeout into the active timeouts list according to timeout->ticks.
|
---|
117 | */
|
---|
118 | uint64_t sum = 0;
|
---|
119 | timeout_t *target = NULL;
|
---|
120 | link_t *cur, *prev;
|
---|
121 | prev = NULL;
|
---|
122 | for (cur = list_first(&CPU->timeout_active_list);
|
---|
123 | cur != NULL; cur = list_next(cur, &CPU->timeout_active_list)) {
|
---|
124 | target = list_get_instance(cur, timeout_t, link);
|
---|
125 | irq_spinlock_lock(&target->lock, false);
|
---|
126 |
|
---|
127 | if (timeout->ticks < sum + target->ticks) {
|
---|
128 | irq_spinlock_unlock(&target->lock, false);
|
---|
129 | break;
|
---|
130 | }
|
---|
131 |
|
---|
132 | sum += target->ticks;
|
---|
133 | irq_spinlock_unlock(&target->lock, false);
|
---|
134 | prev = cur;
|
---|
135 | }
|
---|
136 |
|
---|
137 | if (prev == NULL)
|
---|
138 | list_prepend(&timeout->link, &CPU->timeout_active_list);
|
---|
139 | else
|
---|
140 | list_insert_after(&timeout->link, prev);
|
---|
141 |
|
---|
142 | /*
|
---|
143 | * Adjust timeout->ticks according to ticks
|
---|
144 | * accumulated in target's predecessors.
|
---|
145 | */
|
---|
146 | timeout->ticks -= sum;
|
---|
147 |
|
---|
148 | /*
|
---|
149 | * Decrease ticks of timeout's immediate succesor by timeout->ticks.
|
---|
150 | */
|
---|
151 | if (cur != NULL) {
|
---|
152 | irq_spinlock_lock(&target->lock, false);
|
---|
153 | target->ticks -= timeout->ticks;
|
---|
154 | irq_spinlock_unlock(&target->lock, false);
|
---|
155 | }
|
---|
156 |
|
---|
157 | irq_spinlock_unlock(&timeout->lock, false);
|
---|
158 | irq_spinlock_unlock(&CPU->timeoutlock, true);
|
---|
159 | }
|
---|
160 |
|
---|
161 | /** Unregister timeout
|
---|
162 | *
|
---|
163 | * Remove timeout from timeout list.
|
---|
164 | *
|
---|
165 | * @param timeout Timeout to unregister.
|
---|
166 | *
|
---|
167 | * @return True on success, false on failure.
|
---|
168 | *
|
---|
169 | */
|
---|
170 | bool timeout_unregister(timeout_t *timeout)
|
---|
171 | {
|
---|
172 | DEADLOCK_PROBE_INIT(p_tolock);
|
---|
173 |
|
---|
174 | grab_locks:
|
---|
175 | irq_spinlock_lock(&timeout->lock, true);
|
---|
176 | if (!timeout->cpu) {
|
---|
177 | irq_spinlock_unlock(&timeout->lock, true);
|
---|
178 | return false;
|
---|
179 | }
|
---|
180 |
|
---|
181 | if (!irq_spinlock_trylock(&timeout->cpu->timeoutlock)) {
|
---|
182 | irq_spinlock_unlock(&timeout->lock, true);
|
---|
183 | DEADLOCK_PROBE(p_tolock, DEADLOCK_THRESHOLD);
|
---|
184 | goto grab_locks;
|
---|
185 | }
|
---|
186 |
|
---|
187 | /*
|
---|
188 | * Now we know for sure that timeout hasn't been activated yet
|
---|
189 | * and is lurking in timeout->cpu->timeout_active_list.
|
---|
190 | */
|
---|
191 |
|
---|
192 | link_t *cur = list_next(&timeout->link,
|
---|
193 | &timeout->cpu->timeout_active_list);
|
---|
194 | if (cur != NULL) {
|
---|
195 | timeout_t *tmp = list_get_instance(cur, timeout_t, link);
|
---|
196 | irq_spinlock_lock(&tmp->lock, false);
|
---|
197 | tmp->ticks += timeout->ticks;
|
---|
198 | irq_spinlock_unlock(&tmp->lock, false);
|
---|
199 | }
|
---|
200 |
|
---|
201 | list_remove(&timeout->link);
|
---|
202 | irq_spinlock_unlock(&timeout->cpu->timeoutlock, false);
|
---|
203 |
|
---|
204 | timeout_reinitialize(timeout);
|
---|
205 | irq_spinlock_unlock(&timeout->lock, true);
|
---|
206 |
|
---|
207 | return true;
|
---|
208 | }
|
---|
209 |
|
---|
210 | /** @}
|
---|
211 | */
|
---|