Subversion Repositories HelenOS

Rev

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

Rev 2307 Rev 2336
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
/** Initialize timeouts
49
/** Initialize timeouts
49
 *
50
 *
50
 * Initialize kernel timeouts.
51
 * Initialize kernel timeouts.
51
 *
52
 *
52
 */
53
 */
53
void timeout_init(void)
54
void timeout_init(void)
54
{
55
{
55
    spinlock_initialize(&CPU->timeoutlock, "timeout_lock");
56
    spinlock_initialize(&CPU->timeoutlock, "timeout_lock");
-
 
57
 
-
 
58
#ifdef CONFIG_TIMEOUT_EXTAVL_TREE
-
 
59
    extavltree_create(&CPU->timeout_active_tree);
-
 
60
#else
56
    list_initialize(&CPU->timeout_active_head);
61
    list_initialize(&CPU->timeout_active_head);
-
 
62
#endif
57
}
63
}
58
 
64
 
59
 
65
 
60
/** Reinitialize timeout
66
/** Reinitialize timeout
61
 *
67
 *
62
 * Initialize all members except the lock.
68
 * Initialize all members except the lock.
63
 *
69
 *
64
 * @param t Timeout to be initialized.
70
 * @param t Timeout to be initialized.
65
 *
71
 *
66
 */
72
 */
67
void timeout_reinitialize(timeout_t *t)
73
void timeout_reinitialize(timeout_t *t)
68
{
74
{
69
    t->cpu = NULL;
75
    t->cpu = NULL;
70
    t->ticks = 0;
-
 
71
    t->handler = NULL;
76
    t->handler = NULL;
72
    t->arg = NULL;
77
    t->arg = NULL;
-
 
78
   
-
 
79
#ifdef CONFIG_TIMEOUT_EXTAVL_TREE
-
 
80
    extavltree_node_initialize(&t->node);
-
 
81
#else
-
 
82
    t->ticks = 0;
73
    link_initialize(&t->link);
83
    link_initialize(&t->link);
-
 
84
#endif
74
}
85
}
75
 
86
 
76
 
87
 
77
/** Initialize timeout
88
/** Initialize timeout
78
 *
89
 *
79
 * Initialize all members including the lock.
90
 * Initialize all members including the lock.
80
 *
91
 *
81
 * @param t Timeout to be initialized.
92
 * @param t Timeout to be initialized.
82
 *
93
 *
83
 */
94
 */
84
void timeout_initialize(timeout_t *t)
95
void timeout_initialize(timeout_t *t)
85
{
96
{
86
    spinlock_initialize(&t->lock, "timeout_t_lock");
97
    spinlock_initialize(&t->lock, "timeout_t_lock");
87
    timeout_reinitialize(t);
98
    timeout_reinitialize(t);
88
}
99
}
89
 
100
 
-
 
101
#ifdef CONFIG_TIMEOUT_EXTAVL_TREE
-
 
102
/** Register timeout
-
 
103
 *
-
 
104
 * Insert timeout handler f (with argument arg)
-
 
105
 * to timeout list and make it execute in
-
 
106
 * time microseconds (or slightly more).
-
 
107
 *
-
 
108
 * @param t    Timeout structure.
-
 
109
 * @param time Number of usec in the future to execute
-
 
110
 *             the handler.
-
 
111
 * @param f    Timeout handler function.
-
 
112
 * @param arg  Timeout handler argument.
-
 
113
 *
-
 
114
 */
-
 
115
void timeout_register(timeout_t *t, uint64_t time, timeout_handler_t f, void *arg)
-
 
116
{
-
 
117
    ipl_t ipl;
-
 
118
 
-
 
119
    ipl = interrupts_disable();
-
 
120
    spinlock_lock(&CPU->timeoutlock);
-
 
121
    spinlock_lock(&t->lock);
-
 
122
 
-
 
123
    if (t->cpu)
-
 
124
        panic("t->cpu != 0");
-
 
125
   
-
 
126
    t->cpu = CPU;
-
 
127
   
-
 
128
    //tiky nejsou, musim zmenit klice primo v uzlech
-
 
129
   
-
 
130
    t->handler = f;
-
 
131
    t->arg = arg;
-
 
132
 
-
 
133
    extavltree_insert(&CPU->timeout_active_tree,&t->node);
-
 
134
   
-
 
135
    spinlock_unlock(&t->lock);
-
 
136
    spinlock_unlock(&CPU->timeoutlock);
-
 
137
    interrupts_restore(ipl);
-
 
138
}
-
 
139
 
-
 
140
 
-
 
141
/** Unregister timeout
-
 
142
 *
-
 
143
 * Remove timeout from timeout list.
-
 
144
 *
-
 
145
 * @param t Timeout to unregister.
-
 
146
 *
-
 
147
 * @return true on success, false on failure.
-
 
148
 */
-
 
149
bool timeout_unregister(timeout_t *t)
-
 
150
{
-
 
151
    ipl_t ipl;
-
 
152
 
-
 
153
grab_locks:
-
 
154
    ipl = interrupts_disable();
-
 
155
    spinlock_lock(&t->lock);
-
 
156
    if (!t->cpu) {
-
 
157
        spinlock_unlock(&t->lock);
-
 
158
        interrupts_restore(ipl);
-
 
159
        return false;
-
 
160
    }
-
 
161
    if (!spinlock_trylock(&t->cpu->timeoutlock)) {
-
 
162
        spinlock_unlock(&t->lock);
-
 
163
        interrupts_restore(ipl);       
-
 
164
        goto grab_locks;
-
 
165
    }
-
 
166
   
-
 
167
    /*
-
 
168
     * Now we know for sure that t hasn't been activated yet
-
 
169
     * and is lurking in t->cpu->timeout_active_head queue.
-
 
170
     */
-
 
171
 
-
 
172
    extavltree_delete(&CPU->timeout_active_tree,&t->node);
-
 
173
   
-
 
174
    spinlock_unlock(&t->cpu->timeoutlock);
-
 
175
 
-
 
176
    timeout_reinitialize(t);
-
 
177
    spinlock_unlock(&t->lock);
-
 
178
 
-
 
179
    interrupts_restore(ipl);
-
 
180
    return true;
-
 
181
}
-
 
182
 
-
 
183
#else
90
 
184
 
91
/** Register timeout
185
/** Register timeout
92
 *
186
 *
93
 * Insert timeout handler f (with argument arg)
187
 * Insert timeout handler f (with argument arg)
94
 * to timeout list and make it execute in
188
 * to timeout list and make it execute in
95
 * time microseconds (or slightly more).
189
 * time microseconds (or slightly more).
96
 *
190
 *
97
 * @param t    Timeout structure.
191
 * @param t    Timeout structure.
98
 * @param time Number of usec in the future to execute
192
 * @param time Number of usec in the future to execute
99
 *             the handler.
193
 *             the handler.
100
 * @param f    Timeout handler function.
194
 * @param f    Timeout handler function.
101
 * @param arg  Timeout handler argument.
195
 * @param arg  Timeout handler argument.
102
 *
196
 *
103
 */
197
 */
104
void timeout_register(timeout_t *t, uint64_t time, timeout_handler_t f, void *arg)
198
void timeout_register(timeout_t *t, uint64_t time, timeout_handler_t f, void *arg)
105
{
199
{
106
    timeout_t *hlp = NULL;
200
    timeout_t *hlp = NULL;
107
    link_t *l, *m;
201
    link_t *l, *m;
108
    ipl_t ipl;
202
    ipl_t ipl;
109
    uint64_t sum;
203
    uint64_t sum;
110
 
204
 
111
    ipl = interrupts_disable();
205
    ipl = interrupts_disable();
112
    spinlock_lock(&CPU->timeoutlock);
206
    spinlock_lock(&CPU->timeoutlock);
113
    spinlock_lock(&t->lock);
207
    spinlock_lock(&t->lock);
114
 
208
 
115
    if (t->cpu)
209
    if (t->cpu)
116
        panic("t->cpu != 0");
210
        panic("t->cpu != 0");
117
 
211
 
118
    t->cpu = CPU;
212
    t->cpu = CPU;
119
    t->ticks = us2ticks(time);
213
    t->ticks = us2ticks(time);
120
   
214
   
121
    t->handler = f;
215
    t->handler = f;
122
    t->arg = arg;
216
    t->arg = arg;
123
 
217
 
124
    /*
218
    /*
125
     * Insert t into the active timeouts list according to t->ticks.
219
     * Insert t into the active timeouts list according to t->ticks.
126
     */
220
     */
127
    sum = 0;
221
    sum = 0;
128
    l = CPU->timeout_active_head.next;
222
    l = CPU->timeout_active_head.next;
129
    while (l != &CPU->timeout_active_head) {
223
    while (l != &CPU->timeout_active_head) {
130
        hlp = list_get_instance(l, timeout_t, link);
224
        hlp = list_get_instance(l, timeout_t, link);
131
        spinlock_lock(&hlp->lock);
225
        spinlock_lock(&hlp->lock);
132
        if (t->ticks < sum + hlp->ticks) {
226
        if (t->ticks < sum + hlp->ticks) {
133
            spinlock_unlock(&hlp->lock);
227
            spinlock_unlock(&hlp->lock);
134
            break;
228
            break;
135
        }
229
        }
136
        sum += hlp->ticks;
230
        sum += hlp->ticks;
137
        spinlock_unlock(&hlp->lock);
231
        spinlock_unlock(&hlp->lock);
138
        l = l->next;
232
        l = l->next;
139
    }
233
    }
140
 
234
 
141
    m = l->prev;
235
    m = l->prev;
142
    list_prepend(&t->link, m); /* avoid using l->prev */
236
    list_prepend(&t->link, m); /* avoid using l->prev */
143
 
237
 
144
    /*
238
    /*
145
     * Adjust t->ticks according to ticks accumulated in h's predecessors.
239
     * Adjust t->ticks according to ticks accumulated in h's predecessors.
146
     */
240
     */
147
    t->ticks -= sum;
241
    t->ticks -= sum;
148
 
242
 
149
    /*
243
    /*
150
     * Decrease ticks of t's immediate succesor by t->ticks.
244
     * Decrease ticks of t's immediate succesor by t->ticks.
151
     */
245
     */
152
    if (l != &CPU->timeout_active_head) {
246
    if (l != &CPU->timeout_active_head) {
153
        spinlock_lock(&hlp->lock);
247
        spinlock_lock(&hlp->lock);
154
        hlp->ticks -= t->ticks;
248
        hlp->ticks -= t->ticks;
155
        spinlock_unlock(&hlp->lock);
249
        spinlock_unlock(&hlp->lock);
156
    }
250
    }
157
 
251
 
158
    spinlock_unlock(&t->lock);
252
    spinlock_unlock(&t->lock);
159
    spinlock_unlock(&CPU->timeoutlock);
253
    spinlock_unlock(&CPU->timeoutlock);
160
    interrupts_restore(ipl);
254
    interrupts_restore(ipl);
161
}
255
}
162
 
256
 
163
 
257
 
164
/** Unregister timeout
258
/** Unregister timeout
165
 *
259
 *
166
 * Remove timeout from timeout list.
260
 * Remove timeout from timeout list.
167
 *
261
 *
168
 * @param t Timeout to unregister.
262
 * @param t Timeout to unregister.
169
 *
263
 *
170
 * @return true on success, false on failure.
264
 * @return true on success, false on failure.
171
 */
265
 */
172
bool timeout_unregister(timeout_t *t)
266
bool timeout_unregister(timeout_t *t)
173
{
267
{
174
    timeout_t *hlp;
268
    timeout_t *hlp;
175
    link_t *l;
269
    link_t *l;
176
    ipl_t ipl;
270
    ipl_t ipl;
177
    DEADLOCK_PROBE_INIT(p_tolock);
-
 
178
 
271
 
179
grab_locks:
272
grab_locks:
180
    ipl = interrupts_disable();
273
    ipl = interrupts_disable();
181
    spinlock_lock(&t->lock);
274
    spinlock_lock(&t->lock);
182
    if (!t->cpu) {
275
    if (!t->cpu) {
183
        spinlock_unlock(&t->lock);
276
        spinlock_unlock(&t->lock);
184
        interrupts_restore(ipl);
277
        interrupts_restore(ipl);
185
        return false;
278
        return false;
186
    }
279
    }
187
    if (!spinlock_trylock(&t->cpu->timeoutlock)) {
280
    if (!spinlock_trylock(&t->cpu->timeoutlock)) {
188
        spinlock_unlock(&t->lock);
281
        spinlock_unlock(&t->lock);
189
        interrupts_restore(ipl);
282
        interrupts_restore(ipl);       
190
        DEADLOCK_PROBE(p_tolock, DEADLOCK_THRESHOLD);
-
 
191
        goto grab_locks;
283
        goto grab_locks;
192
    }
284
    }
193
   
285
   
194
    /*
286
    /*
195
     * Now we know for sure that t hasn't been activated yet
287
     * Now we know for sure that t hasn't been activated yet
196
     * and is lurking in t->cpu->timeout_active_head queue.
288
     * and is lurking in t->cpu->timeout_active_head queue.
197
     */
289
     */
198
 
290
 
199
    l = t->link.next;
291
    l = t->link.next;
200
    if (l != &t->cpu->timeout_active_head) {
292
    if (l != &t->cpu->timeout_active_head) {
201
        hlp = list_get_instance(l, timeout_t, link);
293
        hlp = list_get_instance(l, timeout_t, link);
202
        spinlock_lock(&hlp->lock);
294
        spinlock_lock(&hlp->lock);
203
        hlp->ticks += t->ticks;
295
        hlp->ticks += t->ticks;
204
        spinlock_unlock(&hlp->lock);
296
        spinlock_unlock(&hlp->lock);
205
    }
297
    }
206
   
298
   
207
    list_remove(&t->link);
299
    list_remove(&t->link);
208
    spinlock_unlock(&t->cpu->timeoutlock);
300
    spinlock_unlock(&t->cpu->timeoutlock);
209
 
301
 
210
    timeout_reinitialize(t);
302
    timeout_reinitialize(t);
211
    spinlock_unlock(&t->lock);
303
    spinlock_unlock(&t->lock);
212
 
304
 
213
    interrupts_restore(ipl);
305
    interrupts_restore(ipl);
214
    return true;
306
    return true;
215
}
307
}
216
 
308
 
-
 
309
#endif
217
/** @}
310
/** @}
218
 */
311
 */
219
 
312