Subversion Repositories HelenOS-historic

Rev

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