Subversion Repositories HelenOS-historic

Rev

Rev 125 | Rev 404 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 jermar 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>
68 decky 33
#include <panic.h>
1 jermar 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>
239 vana 40
#include <print.h>
1 jermar 41
 
107 decky 42
 
43
/** Initialize timeouts
44
 *
45
 * Initialize kernel timeouts.
46
 *
47
 */
1 jermar 48
void timeout_init(void)
49
{
15 jermar 50
    spinlock_initialize(&CPU->timeoutlock);
51
    list_initialize(&CPU->timeout_active_head);
1 jermar 52
}
53
 
54
 
107 decky 55
/** Initialize empty timeout list
56
 *
57
 * Initialize the timeout list to be empty.
58
 *
59
 * @param t Timeout list to be initialized.
60
 *
61
 */
1 jermar 62
void 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
 
107 decky 71
 
72
/** Initialize timeout list
73
 *
74
 * Initialize the timeout list and its spinlock.
75
 *
76
 * @param t Timeout list to be initialized.
77
 *
78
 */
1 jermar 79
void timeout_initialize(timeout_t *t)
80
{
81
    spinlock_initialize(&t->lock);
82
    timeout_reinitialize(t);
83
}
84
 
107 decky 85
 
86
/** Register timeout callback
87
 *
88
 * Insert the timeout handler f (with argument arg)
89
 * to the timeout list and make it execute in
90
 * time microseconds (or slightly more).
91
 *
92
 * @param t    Timeout list.
93
 * @patam time Number of usec in the future to execute
94
 *             the handler.
95
 * @param f    Timeout handler function.
96
 * @param arg  Timeout handler argument.
97
 *
1 jermar 98
 */
99
void timeout_register(timeout_t *t, __u64 time, timeout_handler f, void *arg)
100
{
101
    timeout_t *hlp;
102
    link_t *l, *m;
103
    pri_t pri;
104
    __u64 sum;
105
 
106
    pri = cpu_priority_high();
15 jermar 107
    spinlock_lock(&CPU->timeoutlock);
1 jermar 108
    spinlock_lock(&t->lock);
125 jermar 109
 
1 jermar 110
    if (t->cpu)
103 jermar 111
        panic("t->cpu != 0");
1 jermar 112
 
15 jermar 113
    t->cpu = CPU;
1 jermar 114
    t->ticks = us2ticks(time);
115
 
116
    t->handler = f;
117
    t->arg = arg;
125 jermar 118
 
1 jermar 119
    /*
120
     * Insert t into the active timeouts list according to t->ticks.
121
     */
122
    sum = 0;
15 jermar 123
    l = CPU->timeout_active_head.next;
124
    while (l != &CPU->timeout_active_head) {
1 jermar 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
    }
239 vana 135
 
1 jermar 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
     */
15 jermar 147
    if (l != &CPU->timeout_active_head) {
1 jermar 148
        spinlock_lock(&hlp->lock);
149
        hlp->ticks -= t->ticks;
150
        spinlock_unlock(&hlp->lock);
151
    }
152
 
153
    spinlock_unlock(&t->lock);
15 jermar 154
    spinlock_unlock(&CPU->timeoutlock);
1 jermar 155
    cpu_priority_restore(pri);
156
}
157
 
107 decky 158
 
159
/** Unregister timeout callback
160
 *
161
 * Remove timeout from timeout list.
162
 *
163
 * @param t Timeout to unregister.
164
 *
165
 */
1 jermar 166
int timeout_unregister(timeout_t *t)
167
{
168
    timeout_t *hlp;
169
    link_t *l;
170
    pri_t pri;
171
 
172
grab_locks:
173
    pri = cpu_priority_high();
174
    spinlock_lock(&t->lock);
175
    if (!t->cpu) {
176
        spinlock_unlock(&t->lock);
177
        cpu_priority_restore(pri);
178
        return 0;
179
    }
180
    if (!spinlock_trylock(&t->cpu->timeoutlock)) {
181
        spinlock_unlock(&t->lock);
182
        cpu_priority_restore(pri);     
183
        goto grab_locks;
184
    }
185
 
186
    /*
187
     * Now we know for sure that t hasn't been activated yet
188
     * and is lurking in t->cpu->timeout_active_head queue.
189
     */
190
 
191
    l = t->link.next;
192
    if (l != &t->cpu->timeout_active_head) {
193
        hlp = list_get_instance(l, timeout_t, link);
194
        spinlock_lock(&hlp->lock);
195
        hlp->ticks += t->ticks;
196
        spinlock_unlock(&hlp->lock);
197
    }
198
 
199
    list_remove(&t->link);
200
    spinlock_unlock(&t->cpu->timeoutlock);
201
 
202
    timeout_reinitialize(t);
203
    spinlock_unlock(&t->lock);
204
 
205
    cpu_priority_restore(pri);
206
    return 1;
207
}