source: mainline/kernel/generic/src/time/timeout.c@ 40043e8

Last change on this file since 40043e8 was 2cc569a3, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

Removing inclusion of halt.h

Several files used to include halt.h even though
this was not necessary. The inclusion has therefore
been removed

  • Property mode set to 100644
File size: 5.4 KB
Line 
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 <cpu.h>
44#include <arch/asm.h>
45#include <arch.h>
46
47/** Initialize timeouts
48 *
49 * Initialize kernel timeouts.
50 *
51 */
52void timeout_init(void)
53{
54 irq_spinlock_initialize(&CPU->timeoutlock, "cpu.timeoutlock");
55 list_initialize(&CPU->timeout_active_list);
56}
57
58/** Reinitialize timeout
59 *
60 * Initialize all members except the lock.
61 *
62 * @param timeout Timeout to be initialized.
63 *
64 */
65void timeout_reinitialize(timeout_t *timeout)
66{
67 timeout->cpu = NULL;
68 timeout->ticks = 0;
69 timeout->handler = NULL;
70 timeout->arg = NULL;
71 link_initialize(&timeout->link);
72}
73
74/** Initialize timeout
75 *
76 * Initialize all members including the lock.
77 *
78 * @param timeout Timeout to be initialized.
79 *
80 */
81void timeout_initialize(timeout_t *timeout)
82{
83 irq_spinlock_initialize(&timeout->lock, "timeout_t_lock");
84 timeout_reinitialize(timeout);
85}
86
87/** Register timeout
88 *
89 * Insert timeout handler f (with argument arg)
90 * to timeout list and make it execute in
91 * time microseconds (or slightly more).
92 *
93 * @param timeout Timeout structure.
94 * @param time Number of usec in the future to execute the handler.
95 * @param handler Timeout handler function.
96 * @param arg Timeout handler argument.
97 *
98 */
99void timeout_register(timeout_t *timeout, uint64_t time,
100 timeout_handler_t handler, void *arg)
101{
102 irq_spinlock_lock(&CPU->timeoutlock, true);
103 irq_spinlock_lock(&timeout->lock, false);
104
105 if (timeout->cpu)
106 panic("Unexpected: timeout->cpu != 0.");
107
108 timeout->cpu = CPU;
109 timeout->ticks = us2ticks(time);
110
111 timeout->handler = handler;
112 timeout->arg = arg;
113
114 /*
115 * Insert timeout into the active timeouts list according to timeout->ticks.
116 */
117 uint64_t sum = 0;
118 timeout_t *target = NULL;
119 link_t *cur;
120 for (cur = CPU->timeout_active_list.head.next;
121 cur != &CPU->timeout_active_list.head; cur = cur->next) {
122 target = list_get_instance(cur, timeout_t, link);
123 irq_spinlock_lock(&target->lock, false);
124
125 if (timeout->ticks < sum + target->ticks) {
126 irq_spinlock_unlock(&target->lock, false);
127 break;
128 }
129
130 sum += target->ticks;
131 irq_spinlock_unlock(&target->lock, false);
132 }
133
134 /* Avoid using cur->prev directly */
135 link_t *prev = cur->prev;
136 list_insert_after(&timeout->link, prev);
137
138 /*
139 * Adjust timeout->ticks according to ticks
140 * accumulated in target's predecessors.
141 */
142 timeout->ticks -= sum;
143
144 /*
145 * Decrease ticks of timeout's immediate succesor by timeout->ticks.
146 */
147 if (cur != &CPU->timeout_active_list.head) {
148 irq_spinlock_lock(&target->lock, false);
149 target->ticks -= timeout->ticks;
150 irq_spinlock_unlock(&target->lock, false);
151 }
152
153 irq_spinlock_unlock(&timeout->lock, false);
154 irq_spinlock_unlock(&CPU->timeoutlock, true);
155}
156
157/** Unregister timeout
158 *
159 * Remove timeout from timeout list.
160 *
161 * @param timeout Timeout to unregister.
162 *
163 * @return True on success, false on failure.
164 *
165 */
166bool timeout_unregister(timeout_t *timeout)
167{
168 DEADLOCK_PROBE_INIT(p_tolock);
169
170grab_locks:
171 irq_spinlock_lock(&timeout->lock, true);
172 if (!timeout->cpu) {
173 irq_spinlock_unlock(&timeout->lock, true);
174 return false;
175 }
176
177 if (!irq_spinlock_trylock(&timeout->cpu->timeoutlock)) {
178 irq_spinlock_unlock(&timeout->lock, true);
179 DEADLOCK_PROBE(p_tolock, DEADLOCK_THRESHOLD);
180 goto grab_locks;
181 }
182
183 /*
184 * Now we know for sure that timeout hasn't been activated yet
185 * and is lurking in timeout->cpu->timeout_active_list.
186 */
187
188 link_t *cur = timeout->link.next;
189 if (cur != &timeout->cpu->timeout_active_list.head) {
190 timeout_t *tmp = list_get_instance(cur, timeout_t, link);
191 irq_spinlock_lock(&tmp->lock, false);
192 tmp->ticks += timeout->ticks;
193 irq_spinlock_unlock(&tmp->lock, false);
194 }
195
196 list_remove(&timeout->link);
197 irq_spinlock_unlock(&timeout->cpu->timeoutlock, false);
198
199 timeout_reinitialize(timeout);
200 irq_spinlock_unlock(&timeout->lock, true);
201
202 return true;
203}
204
205/** @}
206 */
Note: See TracBrowser for help on using the repository browser.