Subversion Repositories HelenOS

Rev

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

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