Subversion Repositories HelenOS

Rev

Rev 2307 | Rev 2416 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2307 Rev 2336
Line 1... Line 1...
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:
Line 43... Line 43...
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
 *
Line 65... Line 71...
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
 *
Line 85... Line 96...
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
Line 172... Line 266...
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) {
Line 184... Line 277...
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
Line 212... Line 304...
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
 */