Rev 2307 | Rev 2416 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | jermar | 1 | /* |
| 2336 | mencl | 2 | * Copyright (C) 2001-2004 Jakub Jermar |
| 1 | jermar | 3 | * All rights reserved. |
| 4 | * |
||
| 5 | * Redistribution and use in source and binary forms, with or without |
||
| 6 | * modification, are permitted provided that the following conditions |
||
| 7 | * are met: |
||
| 8 | * |
||
| 9 | * - Redistributions of source code must retain the above copyright |
||
| 10 | * notice, this list of conditions and the following disclaimer. |
||
| 11 | * - Redistributions in binary form must reproduce the above copyright |
||
| 12 | * notice, this list of conditions and the following disclaimer in the |
||
| 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 |
||
| 15 | * derived from this software without specific prior written permission. |
||
| 16 | * |
||
| 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 |
||
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
| 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 |
||
| 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 |
||
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
| 27 | */ |
||
| 28 | |||
| 1731 | jermar | 29 | /** @addtogroup time |
| 1702 | cejka | 30 | * @{ |
| 31 | */ |
||
| 32 | |||
| 1264 | jermar | 33 | /** |
| 1702 | cejka | 34 | * @file |
| 1264 | jermar | 35 | * @brief Timeout management functions. |
| 36 | */ |
||
| 37 | |||
| 1 | jermar | 38 | #include <time/timeout.h> |
| 39 | #include <arch/types.h> |
||
| 40 | #include <config.h> |
||
| 68 | decky | 41 | #include <panic.h> |
| 1 | jermar | 42 | #include <synch/spinlock.h> |
| 43 | #include <func.h> |
||
| 44 | #include <cpu.h> |
||
| 45 | #include <arch/asm.h> |
||
| 46 | #include <arch.h> |
||
| 47 | |||
| 2336 | mencl | 48 | |
| 107 | decky | 49 | /** Initialize timeouts |
| 50 | * |
||
| 51 | * Initialize kernel timeouts. |
||
| 52 | * |
||
| 53 | */ |
||
| 1 | jermar | 54 | void timeout_init(void) |
| 55 | { |
||
| 552 | palkovsky | 56 | spinlock_initialize(&CPU->timeoutlock, "timeout_lock"); |
| 2336 | mencl | 57 | |
| 58 | #ifdef CONFIG_TIMEOUT_EXTAVL_TREE |
||
| 59 | extavltree_create(&CPU->timeout_active_tree); |
||
| 60 | #else |
||
| 15 | jermar | 61 | list_initialize(&CPU->timeout_active_head); |
| 2336 | mencl | 62 | #endif |
| 1 | jermar | 63 | } |
| 64 | |||
| 65 | |||
| 411 | jermar | 66 | /** Reinitialize timeout |
| 107 | decky | 67 | * |
| 411 | jermar | 68 | * Initialize all members except the lock. |
| 107 | decky | 69 | * |
| 411 | jermar | 70 | * @param t Timeout to be initialized. |
| 107 | decky | 71 | * |
| 72 | */ |
||
| 1 | jermar | 73 | void timeout_reinitialize(timeout_t *t) |
| 74 | { |
||
| 75 | t->cpu = NULL; |
||
| 76 | t->handler = NULL; |
||
| 77 | t->arg = NULL; |
||
| 2336 | mencl | 78 | |
| 79 | #ifdef CONFIG_TIMEOUT_EXTAVL_TREE |
||
| 80 | extavltree_node_initialize(&t->node); |
||
| 81 | #else |
||
| 82 | t->ticks = 0; |
||
| 1 | jermar | 83 | link_initialize(&t->link); |
| 2336 | mencl | 84 | #endif |
| 1 | jermar | 85 | } |
| 86 | |||
| 107 | decky | 87 | |
| 411 | jermar | 88 | /** Initialize timeout |
| 107 | decky | 89 | * |
| 411 | jermar | 90 | * Initialize all members including the lock. |
| 107 | decky | 91 | * |
| 411 | jermar | 92 | * @param t Timeout to be initialized. |
| 107 | decky | 93 | * |
| 94 | */ |
||
| 1 | jermar | 95 | void timeout_initialize(timeout_t *t) |
| 96 | { |
||
| 552 | palkovsky | 97 | spinlock_initialize(&t->lock, "timeout_t_lock"); |
| 1 | jermar | 98 | timeout_reinitialize(t); |
| 99 | } |
||
| 100 | |||
| 2336 | mencl | 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; |
||
| 107 | decky | 118 | |
| 2336 | mencl | 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 |
||
| 184 | |||
| 411 | jermar | 185 | /** Register timeout |
| 107 | decky | 186 | * |
| 411 | jermar | 187 | * Insert timeout handler f (with argument arg) |
| 188 | * to timeout list and make it execute in |
||
| 107 | decky | 189 | * time microseconds (or slightly more). |
| 190 | * |
||
| 411 | jermar | 191 | * @param t Timeout structure. |
| 404 | jermar | 192 | * @param time Number of usec in the future to execute |
| 107 | decky | 193 | * the handler. |
| 194 | * @param f Timeout handler function. |
||
| 195 | * @param arg Timeout handler argument. |
||
| 196 | * |
||
| 1 | jermar | 197 | */ |
| 1780 | jermar | 198 | void timeout_register(timeout_t *t, uint64_t time, timeout_handler_t f, void *arg) |
| 1 | jermar | 199 | { |
| 659 | jermar | 200 | timeout_t *hlp = NULL; |
| 1 | jermar | 201 | link_t *l, *m; |
| 413 | jermar | 202 | ipl_t ipl; |
| 1780 | jermar | 203 | uint64_t sum; |
| 1 | jermar | 204 | |
| 413 | jermar | 205 | ipl = interrupts_disable(); |
| 15 | jermar | 206 | spinlock_lock(&CPU->timeoutlock); |
| 1 | jermar | 207 | spinlock_lock(&t->lock); |
| 125 | jermar | 208 | |
| 1 | jermar | 209 | if (t->cpu) |
| 103 | jermar | 210 | panic("t->cpu != 0"); |
| 1 | jermar | 211 | |
| 15 | jermar | 212 | t->cpu = CPU; |
| 1 | jermar | 213 | t->ticks = us2ticks(time); |
| 214 | |||
| 215 | t->handler = f; |
||
| 216 | t->arg = arg; |
||
| 125 | jermar | 217 | |
| 1 | jermar | 218 | /* |
| 219 | * Insert t into the active timeouts list according to t->ticks. |
||
| 220 | */ |
||
| 221 | sum = 0; |
||
| 15 | jermar | 222 | l = CPU->timeout_active_head.next; |
| 223 | while (l != &CPU->timeout_active_head) { |
||
| 1 | jermar | 224 | hlp = list_get_instance(l, timeout_t, link); |
| 225 | spinlock_lock(&hlp->lock); |
||
| 226 | if (t->ticks < sum + hlp->ticks) { |
||
| 227 | spinlock_unlock(&hlp->lock); |
||
| 228 | break; |
||
| 229 | } |
||
| 230 | sum += hlp->ticks; |
||
| 231 | spinlock_unlock(&hlp->lock); |
||
| 232 | l = l->next; |
||
| 233 | } |
||
| 239 | vana | 234 | |
| 1 | jermar | 235 | m = l->prev; |
| 236 | list_prepend(&t->link, m); /* avoid using l->prev */ |
||
| 237 | |||
| 238 | /* |
||
| 239 | * Adjust t->ticks according to ticks accumulated in h's predecessors. |
||
| 240 | */ |
||
| 241 | t->ticks -= sum; |
||
| 242 | |||
| 243 | /* |
||
| 244 | * Decrease ticks of t's immediate succesor by t->ticks. |
||
| 245 | */ |
||
| 15 | jermar | 246 | if (l != &CPU->timeout_active_head) { |
| 1 | jermar | 247 | spinlock_lock(&hlp->lock); |
| 248 | hlp->ticks -= t->ticks; |
||
| 249 | spinlock_unlock(&hlp->lock); |
||
| 250 | } |
||
| 251 | |||
| 252 | spinlock_unlock(&t->lock); |
||
| 15 | jermar | 253 | spinlock_unlock(&CPU->timeoutlock); |
| 413 | jermar | 254 | interrupts_restore(ipl); |
| 1 | jermar | 255 | } |
| 256 | |||
| 107 | decky | 257 | |
| 411 | jermar | 258 | /** Unregister timeout |
| 107 | decky | 259 | * |
| 260 | * Remove timeout from timeout list. |
||
| 261 | * |
||
| 262 | * @param t Timeout to unregister. |
||
| 263 | * |
||
| 411 | jermar | 264 | * @return true on success, false on failure. |
| 107 | decky | 265 | */ |
| 411 | jermar | 266 | bool timeout_unregister(timeout_t *t) |
| 1 | jermar | 267 | { |
| 268 | timeout_t *hlp; |
||
| 269 | link_t *l; |
||
| 413 | jermar | 270 | ipl_t ipl; |
| 1 | jermar | 271 | |
| 272 | grab_locks: |
||
| 413 | jermar | 273 | ipl = interrupts_disable(); |
| 1 | jermar | 274 | spinlock_lock(&t->lock); |
| 275 | if (!t->cpu) { |
||
| 276 | spinlock_unlock(&t->lock); |
||
| 413 | jermar | 277 | interrupts_restore(ipl); |
| 411 | jermar | 278 | return false; |
| 1 | jermar | 279 | } |
| 280 | if (!spinlock_trylock(&t->cpu->timeoutlock)) { |
||
| 281 | spinlock_unlock(&t->lock); |
||
| 2336 | mencl | 282 | interrupts_restore(ipl); |
| 1 | jermar | 283 | goto grab_locks; |
| 284 | } |
||
| 285 | |||
| 286 | /* |
||
| 287 | * Now we know for sure that t hasn't been activated yet |
||
| 288 | * and is lurking in t->cpu->timeout_active_head queue. |
||
| 289 | */ |
||
| 290 | |||
| 291 | l = t->link.next; |
||
| 292 | if (l != &t->cpu->timeout_active_head) { |
||
| 293 | hlp = list_get_instance(l, timeout_t, link); |
||
| 294 | spinlock_lock(&hlp->lock); |
||
| 295 | hlp->ticks += t->ticks; |
||
| 296 | spinlock_unlock(&hlp->lock); |
||
| 297 | } |
||
| 298 | |||
| 299 | list_remove(&t->link); |
||
| 300 | spinlock_unlock(&t->cpu->timeoutlock); |
||
| 301 | |||
| 302 | timeout_reinitialize(t); |
||
| 303 | spinlock_unlock(&t->lock); |
||
| 304 | |||
| 413 | jermar | 305 | interrupts_restore(ipl); |
| 411 | jermar | 306 | return true; |
| 1 | jermar | 307 | } |
| 1702 | cejka | 308 | |
| 2336 | mencl | 309 | #endif |
| 1731 | jermar | 310 | /** @} |
| 1702 | cejka | 311 | */ |