Subversion Repositories HelenOS-historic

Rev

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

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