Subversion Repositories HelenOS

Rev

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

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