Subversion Repositories HelenOS

Rev

Rev 1964 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1964 Rev 1968
1
/*
1
/*
2
 * Copyright (C) 2001-2004 Jakub Jermar
2
 * Copyright (C) 2001-2004 Jakub Jermar
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/**
29
/**
30
 * @file    timeout.c
30
 * @file    timeout.c
31
 * @brief   Timeout management functions.
31
 * @brief   Timeout management functions.
32
 */
32
 */
33
 
33
 
34
#include <time/timeout.h>
34
#include <time/timeout.h>
35
#include <typedefs.h>
35
#include <typedefs.h>
36
#include <arch/types.h>
36
#include <arch/types.h>
37
#include <config.h>
37
#include <config.h>
38
#include <panic.h>
38
#include <panic.h>
39
#include <synch/spinlock.h>
39
#include <synch/spinlock.h>
40
#include <func.h>
40
#include <func.h>
41
#include <cpu.h>
41
#include <cpu.h>
42
#include <arch/asm.h>
42
#include <arch/asm.h>
43
#include <arch.h>
43
#include <arch.h>
44
 
44
 
45
 
45
 
46
/** Initialize timeouts
46
/** Initialize timeouts
47
 *
47
 *
48
 * Initialize kernel timeouts.
48
 * Initialize kernel timeouts.
49
 *
49
 *
50
 */
50
 */
51
void timeout_init(void)
51
void timeout_init(void)
52
{
52
{
53
    spinlock_initialize(&CPU->timeoutlock, "timeout_lock");
53
    spinlock_initialize(&CPU->timeoutlock, "timeout_lock");
54
    list_initialize(&CPU->timeout_active_head);
54
    list_initialize(&CPU->timeout_active_head);
55
}
55
}
56
 
56
 
57
 
57
 
58
/** Reinitialize timeout
58
/** Reinitialize timeout
59
 *
59
 *
60
 * Initialize all members except the lock.
60
 * Initialize all members except the lock.
61
 *
61
 *
62
 * @param t Timeout to be initialized.
62
 * @param t Timeout to be initialized.
63
 *
63
 *
64
 */
64
 */
65
void timeout_reinitialize(timeout_t *t)
65
void timeout_reinitialize(timeout_t *t)
66
{
66
{
67
    t->cpu = NULL;
67
    t->cpu = NULL;
68
    t->ticks = 0;
68
    t->ticks = 0;
69
    t->handler = NULL;
69
    t->handler = NULL;
70
    t->arg = NULL;
70
    t->arg = NULL;
71
    link_initialize(&t->link);
71
    link_initialize(&t->link);
72
}
72
}
73
 
73
 
74
 
74
 
75
/** Initialize timeout
75
/** Initialize timeout
76
 *
76
 *
77
 * Initialize all members including the lock.
77
 * Initialize all members including the lock.
78
 *
78
 *
79
 * @param t Timeout to be initialized.
79
 * @param t Timeout to be initialized.
80
 *
80
 *
81
 */
81
 */
82
void timeout_initialize(timeout_t *t)
82
void timeout_initialize(timeout_t *t)
83
{
83
{
84
    spinlock_initialize(&t->lock, "timeout_t_lock");
84
    spinlock_initialize(&t->lock, "timeout_t_lock");
85
    timeout_reinitialize(t);
85
    timeout_reinitialize(t);
86
}
86
}
87
 
87
 
88
 
88
 
89
/** Register timeout
89
/** Register timeout
90
 *
90
 *
91
 * Insert timeout handler f (with argument arg)
91
 * Insert timeout handler f (with argument arg)
92
 * to timeout list and make it execute in
92
 * to timeout list and make it execute in
93
 * time microseconds (or slightly more).
93
 * time microseconds (or slightly more).
94
 *
94
 *
95
 * @param t    Timeout structure.
95
 * @param t    Timeout structure.
96
 * @param time Number of usec in the future to execute
96
 * @param time Number of usec in the future to execute
97
 *             the handler.
97
 *             the handler.
98
 * @param f    Timeout handler function.
98
 * @param f    Timeout handler function.
99
 * @param arg  Timeout handler argument.
99
 * @param arg  Timeout handler argument.
100
 *
100
 *
101
 */
101
 */
102
void timeout_register(timeout_t *t, __u64 time, timeout_handler_t f, void *arg)
102
void timeout_register(timeout_t *t, __u64 time, timeout_handler_t f, void *arg)
103
{
103
{
104
    timeout_t *hlp = NULL;
104
    timeout_t *hlp = NULL;
105
    link_t *l, *m;
105
    link_t *l, *m;
106
    ipl_t ipl;
106
    ipl_t ipl;
107
    __u64 sum;
107
    __u64 sum;
108
 
108
 
109
    ipl = interrupts_disable();
109
    ipl = interrupts_disable();
110
    spinlock_lock(&CPU->timeoutlock);
110
    spinlock_lock(&CPU->timeoutlock);
111
    spinlock_lock(&t->lock);
111
    spinlock_lock(&t->lock);
112
 
112
 
113
    if (t->cpu)
113
    if (t->cpu)
114
        panic("t->cpu != 0");
114
        panic("t->cpu != 0");
115
 
115
 
116
    t->cpu = CPU;
116
    t->cpu = CPU;
117
    t->ticks = us2ticks(time);
117
    t->ticks = us2ticks(time);
118
   
118
   
119
    t->handler = f;
119
    t->handler = f;
120
    t->arg = arg;
120
    t->arg = arg;
121
 
121
 
122
    /*
122
    /*
123
     * Insert t into the active timeouts list according to t->ticks.
123
     * Insert t into the active timeouts list according to t->ticks.
124
     */
124
     */
125
    sum = 0;
125
    sum = 0;
126
    l = CPU->timeout_active_head.next;
126
    l = CPU->timeout_active_head.next;
127
    while (l != &CPU->timeout_active_head) {
127
    while (l != &CPU->timeout_active_head) {
128
        hlp = list_get_instance(l, timeout_t, link);
128
        hlp = list_get_instance(l, timeout_t, link);
129
        spinlock_lock(&hlp->lock);
129
        spinlock_lock(&hlp->lock);
130
        if (t->ticks < sum + hlp->ticks) {
130
        if (t->ticks < sum + hlp->ticks) {
131
            spinlock_unlock(&hlp->lock);
131
            spinlock_unlock(&hlp->lock);
132
            break;
132
            break;
133
        }
133
        }
134
        sum += hlp->ticks;
134
        sum += hlp->ticks;
135
        spinlock_unlock(&hlp->lock);
135
        spinlock_unlock(&hlp->lock);
136
        l = l->next;
136
        l = l->next;
137
    }
137
    }
138
 
138
 
139
    m = l->prev;
139
    m = l->prev;
140
    list_prepend(&t->link, m); /* avoid using l->prev */
140
    list_prepend(&t->link, m); /* avoid using l->prev */
141
 
141
 
142
    /*
142
    /*
143
     * Adjust t->ticks according to ticks accumulated in h's predecessors.
143
     * Adjust t->ticks according to ticks accumulated in h's predecessors.
144
     */
144
     */
145
    t->ticks -= sum;
145
    t->ticks -= sum;
146
 
146
 
147
    /*
147
    /*
148
     * Decrease ticks of t's immediate succesor by t->ticks.
148
     * Decrease ticks of t's immediate succesor by t->ticks.
149
     */
149
     */
150
    if (l != &CPU->timeout_active_head) {
150
    if (l != &CPU->timeout_active_head) {
151
        spinlock_lock(&hlp->lock);
151
        spinlock_lock(&hlp->lock);
152
        hlp->ticks -= t->ticks;
152
        hlp->ticks -= t->ticks;
153
        spinlock_unlock(&hlp->lock);
153
        spinlock_unlock(&hlp->lock);
154
    }
154
    }
155
 
155
 
156
    spinlock_unlock(&t->lock);
156
    spinlock_unlock(&t->lock);
157
    spinlock_unlock(&CPU->timeoutlock);
157
    spinlock_unlock(&CPU->timeoutlock);
158
    interrupts_restore(ipl);
158
    interrupts_restore(ipl);
159
}
159
}
160
 
160
 
161
 
161
 
162
/** Unregister timeout
162
/** Unregister timeout
163
 *
163
 *
164
 * Remove timeout from timeout list.
164
 * Remove timeout from timeout list.
165
 *
165
 *
166
 * @param t Timeout to unregister.
166
 * @param t Timeout to unregister.
167
 *
167
 *
168
 * @return true on success, false on failure.
168
 * @return true on success, false on failure.
169
 */
169
 */
170
bool timeout_unregister(timeout_t *t)
170
bool timeout_unregister(timeout_t *t)
171
{
171
{
172
    timeout_t *hlp;
172
    timeout_t *hlp;
173
    link_t *l;
173
    link_t *l;
174
    ipl_t ipl;
174
    ipl_t ipl;
175
 
175
 
176
grab_locks:
176
grab_locks:
177
    ipl = interrupts_disable();
177
    ipl = interrupts_disable();
178
    spinlock_lock(&t->lock);
178
    spinlock_lock(&t->lock);
179
    if (!t->cpu) {
179
    if (!t->cpu) {
180
        spinlock_unlock(&t->lock);
180
        spinlock_unlock(&t->lock);
181
        interrupts_restore(ipl);
181
        interrupts_restore(ipl);
182
        return false;
182
        return false;
183
    }
183
    }
184
    if (!spinlock_trylock(&t->cpu->timeoutlock)) {
184
    if (!spinlock_trylock(&t->cpu->timeoutlock)) {
185
        spinlock_unlock(&t->lock);
185
        spinlock_unlock(&t->lock);
186
        interrupts_restore(ipl);       
186
        interrupts_restore(ipl);       
187
        goto grab_locks;
187
        goto grab_locks;
188
    }
188
    }
189
   
189
   
190
    /*
190
    /*
191
     * Now we know for sure that t hasn't been activated yet
191
     * Now we know for sure that t hasn't been activated yet
192
     * and is lurking in t->cpu->timeout_active_head queue.
192
     * and is lurking in t->cpu->timeout_active_head queue.
193
     */
193
     */
194
 
194
 
195
    l = t->link.next;
195
    l = t->link.next;
196
    if (l != &t->cpu->timeout_active_head) {
196
    if (l != &t->cpu->timeout_active_head) {
197
        hlp = list_get_instance(l, timeout_t, link);
197
        hlp = list_get_instance(l, timeout_t, link);
198
        spinlock_lock(&hlp->lock);
198
        spinlock_lock(&hlp->lock);
199
        hlp->ticks += t->ticks;
199
        hlp->ticks += t->ticks;
200
        spinlock_unlock(&hlp->lock);
200
        spinlock_unlock(&hlp->lock);
201
    }
201
    }
202
   
202
   
203
    list_remove(&t->link);
203
    list_remove(&t->link);
204
    spinlock_unlock(&t->cpu->timeoutlock);
204
    spinlock_unlock(&t->cpu->timeoutlock);
205
 
205
 
206
    timeout_reinitialize(t);
206
    timeout_reinitialize(t);
207
    spinlock_unlock(&t->lock);
207
    spinlock_unlock(&t->lock);
208
 
208
 
209
    interrupts_restore(ipl);
209
    interrupts_restore(ipl);
210
    return true;
210
    return true;
211
}
211
}
212
 
212