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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b3f8fb7 was b3f8fb7, checked in by Martin Decky <martin@…>, 18 years ago

huge type system cleanup
remove cyclical type dependencies across multiple header files
many minor coding style fixes

  • Property mode set to 100644
File size: 4.9 KB
RevLine 
[f761f1eb]1/*
[df4ed85]2 * Copyright (c) 2001-2004 Jakub Jermar
[f761f1eb]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
[1bb2e7a]29/** @addtogroup time
[b45c443]30 * @{
31 */
32
[cf26ba9]33/**
[b45c443]34 * @file
[cf26ba9]35 * @brief Timeout management functions.
36 */
37
[f761f1eb]38#include <time/timeout.h>
39#include <arch/types.h>
40#include <config.h>
[02a99d2]41#include <panic.h>
[f761f1eb]42#include <synch/spinlock.h>
43#include <func.h>
44#include <cpu.h>
45#include <arch/asm.h>
46#include <arch.h>
47
[70527f1]48
49/** Initialize timeouts
50 *
51 * Initialize kernel timeouts.
52 *
53 */
[f761f1eb]54void timeout_init(void)
55{
[2d93f1f9]56 spinlock_initialize(&CPU->timeoutlock, "timeout_lock");
[43114c5]57 list_initialize(&CPU->timeout_active_head);
[f761f1eb]58}
59
60
[ba1b2194]61/** Reinitialize timeout
[70527f1]62 *
[ba1b2194]63 * Initialize all members except the lock.
[70527f1]64 *
[ba1b2194]65 * @param t Timeout to be initialized.
[70527f1]66 *
67 */
[f761f1eb]68void timeout_reinitialize(timeout_t *t)
69{
70 t->cpu = NULL;
71 t->ticks = 0;
72 t->handler = NULL;
73 t->arg = NULL;
74 link_initialize(&t->link);
75}
76
[70527f1]77
[ba1b2194]78/** Initialize timeout
[70527f1]79 *
[ba1b2194]80 * Initialize all members including the lock.
[70527f1]81 *
[ba1b2194]82 * @param t Timeout to be initialized.
[70527f1]83 *
84 */
[f761f1eb]85void timeout_initialize(timeout_t *t)
86{
[2d93f1f9]87 spinlock_initialize(&t->lock, "timeout_t_lock");
[f761f1eb]88 timeout_reinitialize(t);
89}
90
[70527f1]91
[ba1b2194]92/** Register timeout
[70527f1]93 *
[ba1b2194]94 * Insert timeout handler f (with argument arg)
95 * to timeout list and make it execute in
[70527f1]96 * time microseconds (or slightly more).
97 *
[ba1b2194]98 * @param t Timeout structure.
[a783ca4]99 * @param time Number of usec in the future to execute
[70527f1]100 * the handler.
101 * @param f Timeout handler function.
102 * @param arg Timeout handler argument.
103 *
[f761f1eb]104 */
[7f1c620]105void timeout_register(timeout_t *t, uint64_t time, timeout_handler_t f, void *arg)
[f761f1eb]106{
[2cf5634]107 timeout_t *hlp = NULL;
[f761f1eb]108 link_t *l, *m;
[22f7769]109 ipl_t ipl;
[7f1c620]110 uint64_t sum;
[f761f1eb]111
[22f7769]112 ipl = interrupts_disable();
[43114c5]113 spinlock_lock(&CPU->timeoutlock);
[f761f1eb]114 spinlock_lock(&t->lock);
[76cec1e]115
[f761f1eb]116 if (t->cpu)
[747a2476]117 panic("t->cpu != 0");
[f761f1eb]118
[43114c5]119 t->cpu = CPU;
[f761f1eb]120 t->ticks = us2ticks(time);
121
122 t->handler = f;
123 t->arg = arg;
[76cec1e]124
[f761f1eb]125 /*
126 * Insert t into the active timeouts list according to t->ticks.
127 */
128 sum = 0;
[43114c5]129 l = CPU->timeout_active_head.next;
130 while (l != &CPU->timeout_active_head) {
[f761f1eb]131 hlp = list_get_instance(l, timeout_t, link);
132 spinlock_lock(&hlp->lock);
133 if (t->ticks < sum + hlp->ticks) {
134 spinlock_unlock(&hlp->lock);
135 break;
136 }
137 sum += hlp->ticks;
138 spinlock_unlock(&hlp->lock);
139 l = l->next;
140 }
[5a2e9bbb]141
[f761f1eb]142 m = l->prev;
143 list_prepend(&t->link, m); /* avoid using l->prev */
144
145 /*
146 * Adjust t->ticks according to ticks accumulated in h's predecessors.
147 */
148 t->ticks -= sum;
149
150 /*
151 * Decrease ticks of t's immediate succesor by t->ticks.
152 */
[43114c5]153 if (l != &CPU->timeout_active_head) {
[f761f1eb]154 spinlock_lock(&hlp->lock);
155 hlp->ticks -= t->ticks;
156 spinlock_unlock(&hlp->lock);
157 }
158
159 spinlock_unlock(&t->lock);
[43114c5]160 spinlock_unlock(&CPU->timeoutlock);
[22f7769]161 interrupts_restore(ipl);
[f761f1eb]162}
163
[70527f1]164
[ba1b2194]165/** Unregister timeout
[70527f1]166 *
167 * Remove timeout from timeout list.
168 *
169 * @param t Timeout to unregister.
170 *
[ba1b2194]171 * @return true on success, false on failure.
[70527f1]172 */
[ba1b2194]173bool timeout_unregister(timeout_t *t)
[f761f1eb]174{
175 timeout_t *hlp;
176 link_t *l;
[22f7769]177 ipl_t ipl;
[f761f1eb]178
179grab_locks:
[22f7769]180 ipl = interrupts_disable();
[f761f1eb]181 spinlock_lock(&t->lock);
182 if (!t->cpu) {
183 spinlock_unlock(&t->lock);
[22f7769]184 interrupts_restore(ipl);
[ba1b2194]185 return false;
[f761f1eb]186 }
187 if (!spinlock_trylock(&t->cpu->timeoutlock)) {
188 spinlock_unlock(&t->lock);
[22f7769]189 interrupts_restore(ipl);
[f761f1eb]190 goto grab_locks;
191 }
192
193 /*
194 * Now we know for sure that t hasn't been activated yet
195 * and is lurking in t->cpu->timeout_active_head queue.
196 */
197
198 l = t->link.next;
199 if (l != &t->cpu->timeout_active_head) {
200 hlp = list_get_instance(l, timeout_t, link);
201 spinlock_lock(&hlp->lock);
202 hlp->ticks += t->ticks;
203 spinlock_unlock(&hlp->lock);
204 }
205
206 list_remove(&t->link);
207 spinlock_unlock(&t->cpu->timeoutlock);
208
209 timeout_reinitialize(t);
210 spinlock_unlock(&t->lock);
211
[22f7769]212 interrupts_restore(ipl);
[ba1b2194]213 return true;
[f761f1eb]214}
[b45c443]215
[1bb2e7a]216/** @}
[b45c443]217 */
Note: See TracBrowser for help on using the repository browser.