Subversion Repositories HelenOS-historic

Rev

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

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