[f761f1eb] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
[f761f1eb] | 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 |
|
---|
[1bb2e7a] | 29 | /** @addtogroup time
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
[cf26ba9] | 33 | /**
|
---|
[b45c443] | 34 | * @file
|
---|
[83512f21] | 35 | * @brief Timeout management functions.
|
---|
[cf26ba9] | 36 | */
|
---|
| 37 |
|
---|
[f761f1eb] | 38 | #include <time/timeout.h>
|
---|
| 39 | #include <arch/types.h>
|
---|
| 40 | #include <config.h>
|
---|
[02a99d2] | 41 | #include <panic.h>
|
---|
[f761f1eb] | 42 | #include <synch/spinlock.h>
|
---|
| 43 | #include <func.h>
|
---|
| 44 | #include <cpu.h>
|
---|
| 45 | #include <arch/asm.h>
|
---|
| 46 | #include <arch.h>
|
---|
| 47 |
|
---|
[70527f1] | 48 | /** Initialize timeouts
|
---|
| 49 | *
|
---|
| 50 | * Initialize kernel timeouts.
|
---|
| 51 | *
|
---|
| 52 | */
|
---|
[f761f1eb] | 53 | void timeout_init(void)
|
---|
| 54 | {
|
---|
[2d93f1f9] | 55 | spinlock_initialize(&CPU->timeoutlock, "timeout_lock");
|
---|
[43114c5] | 56 | list_initialize(&CPU->timeout_active_head);
|
---|
[f761f1eb] | 57 | }
|
---|
| 58 |
|
---|
| 59 |
|
---|
[ba1b2194] | 60 | /** Reinitialize timeout
|
---|
[70527f1] | 61 | *
|
---|
[ba1b2194] | 62 | * Initialize all members except the lock.
|
---|
[70527f1] | 63 | *
|
---|
[83512f21] | 64 | * @param t Timeout to be initialized.
|
---|
[70527f1] | 65 | *
|
---|
| 66 | */
|
---|
[f761f1eb] | 67 | void timeout_reinitialize(timeout_t *t)
|
---|
| 68 | {
|
---|
| 69 | t->cpu = NULL;
|
---|
| 70 | t->ticks = 0;
|
---|
| 71 | t->handler = NULL;
|
---|
| 72 | t->arg = NULL;
|
---|
| 73 | link_initialize(&t->link);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[70527f1] | 76 |
|
---|
[ba1b2194] | 77 | /** Initialize timeout
|
---|
[70527f1] | 78 | *
|
---|
[ba1b2194] | 79 | * Initialize all members including the lock.
|
---|
[70527f1] | 80 | *
|
---|
[83512f21] | 81 | * @param t Timeout to be initialized.
|
---|
[70527f1] | 82 | *
|
---|
| 83 | */
|
---|
[f761f1eb] | 84 | void timeout_initialize(timeout_t *t)
|
---|
| 85 | {
|
---|
[2d93f1f9] | 86 | spinlock_initialize(&t->lock, "timeout_t_lock");
|
---|
[f761f1eb] | 87 | timeout_reinitialize(t);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[70527f1] | 90 |
|
---|
[ba1b2194] | 91 | /** Register timeout
|
---|
[70527f1] | 92 | *
|
---|
[ba1b2194] | 93 | * Insert timeout handler f (with argument arg)
|
---|
| 94 | * to timeout list and make it execute in
|
---|
[70527f1] | 95 | * time microseconds (or slightly more).
|
---|
| 96 | *
|
---|
[83512f21] | 97 | * @param t Timeout structure.
|
---|
| 98 | * @param time Number of usec in the future to execute the handler.
|
---|
| 99 | * @param f Timeout handler function.
|
---|
| 100 | * @param arg Timeout handler argument.
|
---|
[70527f1] | 101 | *
|
---|
[f761f1eb] | 102 | */
|
---|
[83512f21] | 103 | void
|
---|
| 104 | timeout_register(timeout_t *t, uint64_t time, timeout_handler_t f, void *arg)
|
---|
[f761f1eb] | 105 | {
|
---|
[2cf5634] | 106 | timeout_t *hlp = NULL;
|
---|
[f761f1eb] | 107 | link_t *l, *m;
|
---|
[22f7769] | 108 | ipl_t ipl;
|
---|
[7f1c620] | 109 | uint64_t sum;
|
---|
[f761f1eb] | 110 |
|
---|
[22f7769] | 111 | ipl = interrupts_disable();
|
---|
[43114c5] | 112 | spinlock_lock(&CPU->timeoutlock);
|
---|
[f761f1eb] | 113 | spinlock_lock(&t->lock);
|
---|
[76cec1e] | 114 |
|
---|
[f761f1eb] | 115 | if (t->cpu)
|
---|
[f651e80] | 116 | panic("Unexpected: t->cpu != 0.");
|
---|
[f761f1eb] | 117 |
|
---|
[43114c5] | 118 | t->cpu = CPU;
|
---|
[f761f1eb] | 119 | t->ticks = us2ticks(time);
|
---|
| 120 |
|
---|
| 121 | t->handler = f;
|
---|
| 122 | t->arg = arg;
|
---|
[76cec1e] | 123 |
|
---|
[f761f1eb] | 124 | /*
|
---|
| 125 | * Insert t into the active timeouts list according to t->ticks.
|
---|
| 126 | */
|
---|
| 127 | sum = 0;
|
---|
[43114c5] | 128 | l = CPU->timeout_active_head.next;
|
---|
| 129 | while (l != &CPU->timeout_active_head) {
|
---|
[f761f1eb] | 130 | hlp = list_get_instance(l, timeout_t, link);
|
---|
| 131 | spinlock_lock(&hlp->lock);
|
---|
| 132 | if (t->ticks < sum + hlp->ticks) {
|
---|
| 133 | spinlock_unlock(&hlp->lock);
|
---|
| 134 | break;
|
---|
| 135 | }
|
---|
| 136 | sum += hlp->ticks;
|
---|
| 137 | spinlock_unlock(&hlp->lock);
|
---|
| 138 | l = l->next;
|
---|
| 139 | }
|
---|
[5a2e9bbb] | 140 |
|
---|
[f761f1eb] | 141 | m = l->prev;
|
---|
| 142 | list_prepend(&t->link, m); /* avoid using l->prev */
|
---|
| 143 |
|
---|
| 144 | /*
|
---|
| 145 | * Adjust t->ticks according to ticks accumulated in h's predecessors.
|
---|
| 146 | */
|
---|
| 147 | t->ticks -= sum;
|
---|
| 148 |
|
---|
| 149 | /*
|
---|
| 150 | * Decrease ticks of t's immediate succesor by t->ticks.
|
---|
| 151 | */
|
---|
[43114c5] | 152 | if (l != &CPU->timeout_active_head) {
|
---|
[f761f1eb] | 153 | spinlock_lock(&hlp->lock);
|
---|
| 154 | hlp->ticks -= t->ticks;
|
---|
| 155 | spinlock_unlock(&hlp->lock);
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | spinlock_unlock(&t->lock);
|
---|
[43114c5] | 159 | spinlock_unlock(&CPU->timeoutlock);
|
---|
[22f7769] | 160 | interrupts_restore(ipl);
|
---|
[f761f1eb] | 161 | }
|
---|
| 162 |
|
---|
[70527f1] | 163 |
|
---|
[ba1b2194] | 164 | /** Unregister timeout
|
---|
[70527f1] | 165 | *
|
---|
| 166 | * Remove timeout from timeout list.
|
---|
| 167 | *
|
---|
[83512f21] | 168 | * @param t Timeout to unregister.
|
---|
[70527f1] | 169 | *
|
---|
[83512f21] | 170 | * @return True on success, false on failure.
|
---|
[70527f1] | 171 | */
|
---|
[ba1b2194] | 172 | bool timeout_unregister(timeout_t *t)
|
---|
[f761f1eb] | 173 | {
|
---|
| 174 | timeout_t *hlp;
|
---|
| 175 | link_t *l;
|
---|
[22f7769] | 176 | ipl_t ipl;
|
---|
[31d8e10] | 177 | DEADLOCK_PROBE_INIT(p_tolock);
|
---|
[f761f1eb] | 178 |
|
---|
| 179 | grab_locks:
|
---|
[22f7769] | 180 | ipl = interrupts_disable();
|
---|
[f761f1eb] | 181 | spinlock_lock(&t->lock);
|
---|
| 182 | if (!t->cpu) {
|
---|
| 183 | spinlock_unlock(&t->lock);
|
---|
[22f7769] | 184 | interrupts_restore(ipl);
|
---|
[ba1b2194] | 185 | return false;
|
---|
[f761f1eb] | 186 | }
|
---|
| 187 | if (!spinlock_trylock(&t->cpu->timeoutlock)) {
|
---|
| 188 | spinlock_unlock(&t->lock);
|
---|
[31d8e10] | 189 | interrupts_restore(ipl);
|
---|
| 190 | DEADLOCK_PROBE(p_tolock, DEADLOCK_THRESHOLD);
|
---|
[f761f1eb] | 191 | goto grab_locks;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | /*
|
---|
| 195 | * Now we know for sure that t hasn't been activated yet
|
---|
| 196 | * and is lurking in t->cpu->timeout_active_head queue.
|
---|
| 197 | */
|
---|
| 198 |
|
---|
| 199 | l = t->link.next;
|
---|
| 200 | if (l != &t->cpu->timeout_active_head) {
|
---|
| 201 | hlp = list_get_instance(l, timeout_t, link);
|
---|
| 202 | spinlock_lock(&hlp->lock);
|
---|
| 203 | hlp->ticks += t->ticks;
|
---|
| 204 | spinlock_unlock(&hlp->lock);
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | list_remove(&t->link);
|
---|
| 208 | spinlock_unlock(&t->cpu->timeoutlock);
|
---|
| 209 |
|
---|
| 210 | timeout_reinitialize(t);
|
---|
| 211 | spinlock_unlock(&t->lock);
|
---|
| 212 |
|
---|
[22f7769] | 213 | interrupts_restore(ipl);
|
---|
[ba1b2194] | 214 | return true;
|
---|
[f761f1eb] | 215 | }
|
---|
[b45c443] | 216 |
|
---|
[1bb2e7a] | 217 | /** @}
|
---|
[b45c443] | 218 | */
|
---|