Subversion Repositories HelenOS

Rev

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

Rev 2431 Rev 2450
1
/*
1
/*
2
 * Copyright (C) 2001-2004 Jakub Jermar
2
 * Copyright (C) 2001-2004 Jakub Jermar
3
 * Copyright (C) 2007 Vojtech Mencl
3
 * Copyright (C) 2007 Vojtech Mencl
4
 * All rights reserved.
4
 * All rights reserved.
5
 *
5
 *
6
 * Redistribution and use in source and binary forms, with or without
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
7
 * modification, are permitted provided that the following conditions
8
 * are met:
8
 * are met:
9
 *
9
 *
10
 * - Redistributions of source code must retain the above copyright
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
11
 *   notice, this list of conditions and the following disclaimer.
12
 * - Redistributions in binary form must reproduce the above copyright
12
 * - Redistributions in binary form must reproduce the above copyright
13
 *   notice, this list of conditions and the following disclaimer in the
13
 *   notice, this list of conditions and the following disclaimer in the
14
 *   documentation and/or other materials provided with the distribution.
14
 *   documentation and/or other materials provided with the distribution.
15
 * - The name of the author may not be used to endorse or promote products
15
 * - The name of the author may not be used to endorse or promote products
16
 *   derived from this software without specific prior written permission.
16
 *   derived from this software without specific prior written permission.
17
 *
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
28
 */
29
 
29
 
30
/** @addtogroup time
30
/** @addtogroup time
31
 * @{
31
 * @{
32
 */
32
 */
33
 
33
 
34
/**
34
/**
35
 * @file
35
 * @file
36
 * @brief   Timeout management functions.
36
 * @brief   Timeout management functions.
37
 */
37
 */
38
 
38
 
39
#include <time/timeout.h>
39
#include <time/timeout.h>
40
#include <arch/types.h>
40
#include <arch/types.h>
41
#include <config.h>
41
#include <config.h>
42
#include <panic.h>
42
#include <panic.h>
43
#include <synch/spinlock.h>
43
#include <synch/spinlock.h>
44
#include <func.h>
44
#include <func.h>
45
#include <cpu.h>
45
#include <cpu.h>
46
#include <arch/asm.h>
46
#include <arch/asm.h>
47
#include <arch.h>
47
#include <arch.h>
48
 
48
 
49
 
49
 
50
/** Initialize timeouts
50
/** Initialize timeouts
51
 *
51
 *
52
 * Initialize kernel timeouts.
52
 * Initialize kernel timeouts.
53
 *
53
 *
54
 */
54
 */
55
void timeout_init(void)
55
void timeout_init(void)
56
{
56
{
57
    spinlock_initialize(&CPU->timeoutlock, "timeout_lock");
57
    spinlock_initialize(&CPU->timeoutlock, "timeout_lock");
58
 
58
 
59
#if defined CONFIG_TIMEOUT_AVL_TREE
59
#if defined CONFIG_TIMEOUT_AVL_TREE
60
    avltree_create(&CPU->timeout_active_tree);
60
    avltree_create(&CPU->timeout_active_tree);
61
#elif defined CONFIG_TIMEOUT_EXTAVL_TREE
61
#elif defined CONFIG_TIMEOUT_EXTAVL_TREE
62
    extavltree_create(&CPU->timeout_active_tree);
62
    extavltree_create(&CPU->timeout_active_tree);
63
#elif defined CONFIG_TIMEOUT_EXTAVLREL_TREE
63
#elif defined CONFIG_TIMEOUT_EXTAVLREL_TREE
64
    extavlreltree_create(&CPU->timeout_active_tree);
64
    extavlreltree_create(&CPU->timeout_active_tree);
65
#else
65
#else
66
    list_initialize(&CPU->timeout_active_head);
66
    list_initialize(&CPU->timeout_active_head);
67
#endif
67
#endif
68
}
68
}
69
 
69
 
70
 
70
 
71
/** Reinitialize timeout
71
/** Reinitialize timeout
72
 *
72
 *
73
 * Initialize all members except the lock.
73
 * Initialize all members except the lock.
74
 *
74
 *
75
 * @param t Timeout to be initialized.
75
 * @param t Timeout to be initialized.
76
 *
76
 *
77
 */
77
 */
78
void timeout_reinitialize(timeout_t *t)
78
void timeout_reinitialize(timeout_t *t)
79
{
79
{
80
    t->cpu = NULL;
80
    t->cpu = NULL;
81
    t->handler = NULL;
81
    t->handler = NULL;
82
    t->arg = NULL;
82
    t->arg = NULL;
83
 
83
 
84
#if defined CONFIG_TIMEOUT_AVL_TREE
84
#if defined CONFIG_TIMEOUT_AVL_TREE
85
    avltree_node_initialize(&t->node);
85
    avltree_node_initialize(&t->node);
86
#elif defined CONFIG_TIMEOUT_EXTAVL_TREE
86
#elif defined CONFIG_TIMEOUT_EXTAVL_TREE
87
    extavltree_node_initialize(&t->node);
87
    extavltree_node_initialize(&t->node);
88
#elif defined CONFIG_TIMEOUT_EXTAVLREL_TREE
88
#elif defined CONFIG_TIMEOUT_EXTAVLREL_TREE
89
    extavlreltree_node_initialize(&t->node);
89
    extavlreltree_node_initialize(&t->node);
90
#else
90
#else
91
    t->ticks = 0;
91
    t->ticks = 0;
92
    link_initialize(&t->link);
92
    link_initialize(&t->link);
93
#endif
93
#endif
94
}
94
}
95
 
95
 
96
 
96
 
97
/** Initialize timeout
97
/** Initialize timeout
98
 *
98
 *
99
 * Initialize all members including the lock.
99
 * Initialize all members including the lock.
100
 *
100
 *
101
 * @param t Timeout to be initialized.
101
 * @param t Timeout to be initialized.
102
 *
102
 *
103
 */
103
 */
104
void timeout_initialize(timeout_t *t)
104
void timeout_initialize(timeout_t *t)
105
{
105
{
106
    spinlock_initialize(&t->lock, "timeout_t_lock");
106
    spinlock_initialize(&t->lock, "timeout_t_lock");
107
    timeout_reinitialize(t);
107
    timeout_reinitialize(t);
108
}
108
}
109
 
109
 
110
#if defined CONFIG_TIMEOUT_AVL_TREE || \
110
#if defined CONFIG_TIMEOUT_AVL_TREE || \
111
    defined CONFIG_TIMEOUT_EXTAVL_TREE || \
111
    defined CONFIG_TIMEOUT_EXTAVL_TREE || \
112
    defined CONFIG_TIMEOUT_EXTAVLREL_TREE
112
    defined CONFIG_TIMEOUT_EXTAVLREL_TREE
113
 
113
 
114
/** Register timeout
114
/** Register timeout
115
 *
115
 *
116
 * Insert timeout handler f (with argument arg)
116
 * Insert timeout handler f (with argument arg)
117
 * to timeout ExtAVL tree and make it execute in
117
 * to timeout ExtAVL tree and make it execute in
118
 * time microseconds (or slightly more).
118
 * time microseconds (or slightly more).
119
 *
119
 *
120
 * @param t    Timeout structure.
120
 * @param t    Timeout structure.
121
 * @param time Number of usec in the future to execute
121
 * @param time Number of usec in the future to execute
122
 *             the handler.
122
 *             the handler.
123
 * @param f    Timeout handler function.
123
 * @param f    Timeout handler function.
124
 * @param arg  Timeout handler argument.
124
 * @param arg  Timeout handler argument.
125
 *
125
 *
126
 */
126
 */
127
void timeout_register(timeout_t *t, uint64_t time, timeout_handler_t f, void *arg)
127
void timeout_register(timeout_t *t, uint64_t time, timeout_handler_t f, void *arg)
128
{
128
{
129
    ipl_t ipl;
129
    ipl_t ipl;
130
 
130
 
131
    ipl = interrupts_disable();
131
    ipl = interrupts_disable();
132
    spinlock_lock(&CPU->timeoutlock);
132
    spinlock_lock(&CPU->timeoutlock);
133
    spinlock_lock(&t->lock);
133
    spinlock_lock(&t->lock);
134
 
134
 
135
    if (t->cpu)
135
    if (t->cpu)
136
        panic("t->cpu != 0");
136
        panic("t->cpu != 0");
137
   
137
   
138
    t->cpu = CPU;
138
    t->cpu = CPU;
139
    t->handler = f;
139
    t->handler = f;
140
    t->arg = arg;
140
    t->arg = arg;
141
    t->node.key = us2ticks(time);
141
    t->node.key = us2ticks(time);
142
 
142
 
143
    /*
143
    /*
144
     * Put timeout into tree structure.
144
     * Put timeout into tree structure.
145
     */
145
     */
146
#if defined CONFIG_TIMEOUT_AVL_TREE
146
#if defined CONFIG_TIMEOUT_AVL_TREE
147
    avltree_insert(&CPU->timeout_active_tree,&t->node);
147
    avltree_insert(&CPU->timeout_active_tree,&t->node);
148
#elif defined CONFIG_TIMEOUT_EXTAVL_TREE
148
#elif defined CONFIG_TIMEOUT_EXTAVL_TREE
149
    extavltree_insert(&CPU->timeout_active_tree,&t->node);
149
    extavltree_insert(&CPU->timeout_active_tree,&t->node);
150
#elif defined CONFIG_TIMEOUT_EXTAVLREL_TREE
150
#elif defined CONFIG_TIMEOUT_EXTAVLREL_TREE
151
    extavlreltree_insert(&CPU->timeout_active_tree,&t->node);
151
    extavlreltree_insert(&CPU->timeout_active_tree,&t->node);
152
#endif
152
#endif
153
 
153
 
154
    spinlock_unlock(&t->lock);
154
    spinlock_unlock(&t->lock);
155
    spinlock_unlock(&CPU->timeoutlock);
155
    spinlock_unlock(&CPU->timeoutlock);
156
    interrupts_restore(ipl);
156
    interrupts_restore(ipl);
157
}
157
}
158
 
158
 
159
 
159
 
160
/** Unregister timeout
160
/** Unregister timeout
161
 *
161
 *
162
 * Remove timeout from timeout ExtAVL tree structure.
162
 * Remove timeout from timeout ExtAVL tree structure.
163
 *
163
 *
164
 * @param t Timeout to unregister.
164
 * @param t Timeout to unregister.
165
 *
165
 *
166
 * @return true on success, false on failure.
166
 * @return true on success, false on failure.
167
 */
167
 */
168
bool timeout_unregister(timeout_t *t)
168
bool timeout_unregister(timeout_t *t)
169
{
169
{
170
    ipl_t ipl;
170
    ipl_t ipl;
171
 
171
 
172
grab_locks:
172
grab_locks:
173
    ipl = interrupts_disable();
173
    ipl = interrupts_disable();
174
    spinlock_lock(&t->lock);
174
    spinlock_lock(&t->lock);
175
    if (!t->cpu) {
175
    if (!t->cpu) {
176
        spinlock_unlock(&t->lock);
176
        spinlock_unlock(&t->lock);
177
        interrupts_restore(ipl);
177
        interrupts_restore(ipl);
178
        return false;
178
        return false;
179
    }
179
    }
180
    if (!spinlock_trylock(&t->cpu->timeoutlock)) {
180
    if (!spinlock_trylock(&t->cpu->timeoutlock)) {
181
        spinlock_unlock(&t->lock);
181
        spinlock_unlock(&t->lock);
182
        interrupts_restore(ipl);       
182
        interrupts_restore(ipl);       
183
        goto grab_locks;
183
        goto grab_locks;
184
    }
184
    }
185
    /*
185
    /*
186
     * 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
187
     * and is lurking in t->cpu->timeout_active_head queue.
187
     * and is lurking in t->cpu->timeout_active_head queue.
188
     */
188
     */
189
 
189
 
190
    /*
190
    /*
191
     * Delete timeout from tree structure.
191
     * Delete timeout from tree structure.
192
     */
192
     */
193
#if defined CONFIG_TIMEOUT_AVL_TREE
193
#if defined CONFIG_TIMEOUT_AVL_TREE
194
    avltree_delete(&CPU->timeout_active_tree,&t->node);
194
    avltree_delete(&t->cpu->timeout_active_tree,&t->node);
195
#elif defined CONFIG_TIMEOUT_EXTAVL_TREE
195
#elif defined CONFIG_TIMEOUT_EXTAVL_TREE
196
    extavltree_delete(&CPU->timeout_active_tree,&t->node);
196
    extavltree_delete(&t->cpu->timeout_active_tree,&t->node);
197
#elif defined CONFIG_TIMEOUT_EXTAVLREL_TREE
197
#elif defined CONFIG_TIMEOUT_EXTAVLREL_TREE
198
    extavlreltree_delete(&CPU->timeout_active_tree,&t->node);
198
    extavlreltree_delete(&t->cpu->timeout_active_tree,&t->node);
199
#endif
199
#endif
200
 
200
 
201
    spinlock_unlock(&t->cpu->timeoutlock);
201
    spinlock_unlock(&t->cpu->timeoutlock);
202
 
202
 
203
    timeout_reinitialize(t);
203
    timeout_reinitialize(t);
204
    spinlock_unlock(&t->lock);
204
    spinlock_unlock(&t->lock);
205
 
205
 
206
    interrupts_restore(ipl);
206
    interrupts_restore(ipl);
207
    return true;
207
    return true;
208
}
208
}
209
 
209
 
210
#else
210
#else
211
 
211
 
212
/** Register timeout
212
/** Register timeout
213
 *
213
 *
214
 * Insert timeout handler f (with argument arg)
214
 * Insert timeout handler f (with argument arg)
215
 * to timeout list and make it execute in
215
 * to timeout list and make it execute in
216
 * time microseconds (or slightly more).
216
 * time microseconds (or slightly more).
217
 *
217
 *
218
 * @param t    Timeout structure.
218
 * @param t    Timeout structure.
219
 * @param time Number of usec in the future to execute
219
 * @param time Number of usec in the future to execute
220
 *             the handler.
220
 *             the handler.
221
 * @param f    Timeout handler function.
221
 * @param f    Timeout handler function.
222
 * @param arg  Timeout handler argument.
222
 * @param arg  Timeout handler argument.
223
 *
223
 *
224
 */
224
 */
225
void timeout_register(timeout_t *t, uint64_t time, timeout_handler_t f, void *arg)
225
void timeout_register(timeout_t *t, uint64_t time, timeout_handler_t f, void *arg)
226
{
226
{
227
    timeout_t *hlp = NULL;
227
    timeout_t *hlp = NULL;
228
    link_t *l, *m;
228
    link_t *l, *m;
229
    ipl_t ipl;
229
    ipl_t ipl;
230
    uint64_t sum;
230
    uint64_t sum;
231
 
231
 
232
    ipl = interrupts_disable();
232
    ipl = interrupts_disable();
233
    spinlock_lock(&CPU->timeoutlock);
233
    spinlock_lock(&CPU->timeoutlock);
234
    spinlock_lock(&t->lock);
234
    spinlock_lock(&t->lock);
235
 
235
 
236
    if (t->cpu)
236
    if (t->cpu)
237
        panic("t->cpu != 0");
237
        panic("t->cpu != 0");
238
 
238
 
239
    t->cpu = CPU;
239
    t->cpu = CPU;
240
    t->ticks = us2ticks(time);
240
    t->ticks = us2ticks(time);
241
   
241
   
242
    t->handler = f;
242
    t->handler = f;
243
    t->arg = arg;
243
    t->arg = arg;
244
 
244
 
245
    /*
245
    /*
246
     * Insert t into the active timeouts list according to t->ticks.
246
     * Insert t into the active timeouts list according to t->ticks.
247
     */
247
     */
248
    sum = 0;
248
    sum = 0;
249
    l = CPU->timeout_active_head.next;
249
    l = CPU->timeout_active_head.next;
250
    while (l != &CPU->timeout_active_head) {
250
    while (l != &CPU->timeout_active_head) {
251
        hlp = list_get_instance(l, timeout_t, link);
251
        hlp = list_get_instance(l, timeout_t, link);
252
        spinlock_lock(&hlp->lock);
252
        spinlock_lock(&hlp->lock);
253
        if (t->ticks < sum + hlp->ticks) {
253
        if (t->ticks < sum + hlp->ticks) {
254
            spinlock_unlock(&hlp->lock);
254
            spinlock_unlock(&hlp->lock);
255
            break;
255
            break;
256
        }
256
        }
257
        sum += hlp->ticks;
257
        sum += hlp->ticks;
258
        spinlock_unlock(&hlp->lock);
258
        spinlock_unlock(&hlp->lock);
259
        l = l->next;
259
        l = l->next;
260
    }
260
    }
261
 
261
 
262
    m = l->prev;
262
    m = l->prev;
263
    list_prepend(&t->link, m); /* avoid using l->prev */
263
    list_prepend(&t->link, m); /* avoid using l->prev */
264
 
264
 
265
    /*
265
    /*
266
     * Adjust t->ticks according to ticks accumulated in h's predecessors.
266
     * Adjust t->ticks according to ticks accumulated in h's predecessors.
267
     */
267
     */
268
    t->ticks -= sum;
268
    t->ticks -= sum;
269
 
269
 
270
    /*
270
    /*
271
     * Decrease ticks of t's immediate succesor by t->ticks.
271
     * Decrease ticks of t's immediate succesor by t->ticks.
272
     */
272
     */
273
    if (l != &CPU->timeout_active_head) {
273
    if (l != &CPU->timeout_active_head) {
274
        spinlock_lock(&hlp->lock);
274
        spinlock_lock(&hlp->lock);
275
        hlp->ticks -= t->ticks;
275
        hlp->ticks -= t->ticks;
276
        spinlock_unlock(&hlp->lock);
276
        spinlock_unlock(&hlp->lock);
277
    }
277
    }
278
 
278
 
279
    spinlock_unlock(&t->lock);
279
    spinlock_unlock(&t->lock);
280
    spinlock_unlock(&CPU->timeoutlock);
280
    spinlock_unlock(&CPU->timeoutlock);
281
    interrupts_restore(ipl);
281
    interrupts_restore(ipl);
282
}
282
}
283
 
283
 
284
 
284
 
285
/** Unregister timeout
285
/** Unregister timeout
286
 *
286
 *
287
 * Remove timeout from timeout list.
287
 * Remove timeout from timeout list.
288
 *
288
 *
289
 * @param t Timeout to unregister.
289
 * @param t Timeout to unregister.
290
 *
290
 *
291
 * @return true on success, false on failure.
291
 * @return true on success, false on failure.
292
 */
292
 */
293
bool timeout_unregister(timeout_t *t)
293
bool timeout_unregister(timeout_t *t)
294
{
294
{
295
    timeout_t *hlp;
295
    timeout_t *hlp;
296
    link_t *l;
296
    link_t *l;
297
    ipl_t ipl;
297
    ipl_t ipl;
298
 
298
 
299
grab_locks:
299
grab_locks:
300
    ipl = interrupts_disable();
300
    ipl = interrupts_disable();
301
    spinlock_lock(&t->lock);
301
    spinlock_lock(&t->lock);
302
    if (!t->cpu) {
302
    if (!t->cpu) {
303
        spinlock_unlock(&t->lock);
303
        spinlock_unlock(&t->lock);
304
        interrupts_restore(ipl);
304
        interrupts_restore(ipl);
305
        return false;
305
        return false;
306
    }
306
    }
307
    if (!spinlock_trylock(&t->cpu->timeoutlock)) {
307
    if (!spinlock_trylock(&t->cpu->timeoutlock)) {
308
        spinlock_unlock(&t->lock);
308
        spinlock_unlock(&t->lock);
309
        interrupts_restore(ipl);       
309
        interrupts_restore(ipl);       
310
        goto grab_locks;
310
        goto grab_locks;
311
    }
311
    }
312
   
312
   
313
    /*
313
    /*
314
     * Now we know for sure that t hasn't been activated yet
314
     * Now we know for sure that t hasn't been activated yet
315
     * and is lurking in t->cpu->timeout_active_head queue.
315
     * and is lurking in t->cpu->timeout_active_head queue.
316
     */
316
     */
317
 
317
 
318
    l = t->link.next;
318
    l = t->link.next;
319
    if (l != &t->cpu->timeout_active_head) {
319
    if (l != &t->cpu->timeout_active_head) {
320
        hlp = list_get_instance(l, timeout_t, link);
320
        hlp = list_get_instance(l, timeout_t, link);
321
        spinlock_lock(&hlp->lock);
321
        spinlock_lock(&hlp->lock);
322
        hlp->ticks += t->ticks;
322
        hlp->ticks += t->ticks;
323
        spinlock_unlock(&hlp->lock);
323
        spinlock_unlock(&hlp->lock);
324
    }
324
    }
325
   
325
   
326
    list_remove(&t->link);
326
    list_remove(&t->link);
327
    spinlock_unlock(&t->cpu->timeoutlock);
327
    spinlock_unlock(&t->cpu->timeoutlock);
328
 
328
 
329
    timeout_reinitialize(t);
329
    timeout_reinitialize(t);
330
    spinlock_unlock(&t->lock);
330
    spinlock_unlock(&t->lock);
331
 
331
 
332
    interrupts_restore(ipl);
332
    interrupts_restore(ipl);
333
    return true;
333
    return true;
334
}
334
}
335
 
335
 
336
#endif
336
#endif
337
/** @}
337
/** @}
338
 */
338
 */
339
 
339