Subversion Repositories HelenOS-historic

Rev

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

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