source: mainline/generic/src/time/timeout.c@ 2d93f1f9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2d93f1f9 was 2d93f1f9, checked in by Ondrej Palkovsky <ondrap@…>, 20 years ago

Named spinlocks

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