Subversion Repositories HelenOS-historic

Rev

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

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