Subversion Repositories HelenOS

Rev

Rev 2071 | Rev 2183 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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