[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 |
|
---|
[e88eb48] | 29 | /** @addtogroup kernel_time
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
[cf26ba9] | 33 | /**
|
---|
[b45c443] | 34 | * @file
|
---|
[da1bafb] | 35 | * @brief Timeout management functions.
|
---|
[cf26ba9] | 36 | */
|
---|
| 37 |
|
---|
[f761f1eb] | 38 | #include <time/timeout.h>
|
---|
[d99c1d2] | 39 | #include <typedefs.h>
|
---|
[f761f1eb] | 40 | #include <config.h>
|
---|
[02a99d2] | 41 | #include <panic.h>
|
---|
[f761f1eb] | 42 | #include <synch/spinlock.h>
|
---|
[b2e121a] | 43 | #include <halt.h>
|
---|
[f761f1eb] | 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 | {
|
---|
[da1bafb] | 55 | irq_spinlock_initialize(&CPU->timeoutlock, "cpu.timeoutlock");
|
---|
[55b77d9] | 56 | list_initialize(&CPU->timeout_active_list);
|
---|
[f761f1eb] | 57 | }
|
---|
| 58 |
|
---|
[ba1b2194] | 59 | /** Initialize timeout
|
---|
[70527f1] | 60 | *
|
---|
[ba1b2194] | 61 | * Initialize all members including the lock.
|
---|
[70527f1] | 62 | *
|
---|
[da1bafb] | 63 | * @param timeout Timeout to be initialized.
|
---|
[70527f1] | 64 | *
|
---|
| 65 | */
|
---|
[da1bafb] | 66 | void timeout_initialize(timeout_t *timeout)
|
---|
[f761f1eb] | 67 | {
|
---|
[ad58fd2] | 68 | link_initialize(&timeout->link);
|
---|
| 69 | timeout->cpu = NULL;
|
---|
[f761f1eb] | 70 | }
|
---|
| 71 |
|
---|
[ba1b2194] | 72 | /** Register timeout
|
---|
[70527f1] | 73 | *
|
---|
[ba1b2194] | 74 | * Insert timeout handler f (with argument arg)
|
---|
| 75 | * to timeout list and make it execute in
|
---|
[70527f1] | 76 | * time microseconds (or slightly more).
|
---|
| 77 | *
|
---|
[da1bafb] | 78 | * @param timeout Timeout structure.
|
---|
| 79 | * @param time Number of usec in the future to execute the handler.
|
---|
| 80 | * @param handler Timeout handler function.
|
---|
| 81 | * @param arg Timeout handler argument.
|
---|
[70527f1] | 82 | *
|
---|
[f761f1eb] | 83 | */
|
---|
[da1bafb] | 84 | void timeout_register(timeout_t *timeout, uint64_t time,
|
---|
| 85 | timeout_handler_t handler, void *arg)
|
---|
[f761f1eb] | 86 | {
|
---|
[da1bafb] | 87 | irq_spinlock_lock(&CPU->timeoutlock, true);
|
---|
[a35b458] | 88 |
|
---|
[da1bafb] | 89 | timeout->cpu = CPU;
|
---|
[742f95ec] | 90 | timeout->deadline = CPU->current_clock_tick + us2ticks(time);
|
---|
[da1bafb] | 91 | timeout->handler = handler;
|
---|
| 92 | timeout->arg = arg;
|
---|
[a35b458] | 93 |
|
---|
[f761f1eb] | 94 | /*
|
---|
[742f95ec] | 95 | * Insert timeout into the active timeouts list according to timeout->deadline.
|
---|
[f761f1eb] | 96 | */
|
---|
[da1bafb] | 97 | timeout_t *target = NULL;
|
---|
[583c2a3] | 98 | link_t *cur, *prev;
|
---|
| 99 | prev = NULL;
|
---|
| 100 | for (cur = list_first(&CPU->timeout_active_list);
|
---|
| 101 | cur != NULL; cur = list_next(cur, &CPU->timeout_active_list)) {
|
---|
[da1bafb] | 102 | target = list_get_instance(cur, timeout_t, link);
|
---|
[a35b458] | 103 |
|
---|
[46b305a] | 104 | if (timeout->deadline < target->deadline)
|
---|
[f761f1eb] | 105 | break;
|
---|
[a35b458] | 106 |
|
---|
[583c2a3] | 107 | prev = cur;
|
---|
[f761f1eb] | 108 | }
|
---|
[a35b458] | 109 |
|
---|
[583c2a3] | 110 | if (prev == NULL)
|
---|
| 111 | list_prepend(&timeout->link, &CPU->timeout_active_list);
|
---|
| 112 | else
|
---|
| 113 | list_insert_after(&timeout->link, prev);
|
---|
[a35b458] | 114 |
|
---|
[da1bafb] | 115 | irq_spinlock_unlock(&CPU->timeoutlock, true);
|
---|
[f761f1eb] | 116 | }
|
---|
| 117 |
|
---|
[ba1b2194] | 118 | /** Unregister timeout
|
---|
[70527f1] | 119 | *
|
---|
| 120 | * Remove timeout from timeout list.
|
---|
| 121 | *
|
---|
[da1bafb] | 122 | * @param timeout Timeout to unregister.
|
---|
| 123 | *
|
---|
| 124 | * @return True on success, false on failure.
|
---|
[70527f1] | 125 | *
|
---|
| 126 | */
|
---|
[da1bafb] | 127 | bool timeout_unregister(timeout_t *timeout)
|
---|
[f761f1eb] | 128 | {
|
---|
[ad58fd2] | 129 | assert(timeout->cpu);
|
---|
[a35b458] | 130 |
|
---|
[ad58fd2] | 131 | irq_spinlock_lock(&timeout->cpu->timeoutlock, true);
|
---|
[a35b458] | 132 |
|
---|
[ad58fd2] | 133 | bool success = link_in_use(&timeout->link);
|
---|
| 134 | if (success) {
|
---|
| 135 | list_remove(&timeout->link);
|
---|
[f761f1eb] | 136 | }
|
---|
[a35b458] | 137 |
|
---|
[ad58fd2] | 138 | irq_spinlock_unlock(&timeout->cpu->timeoutlock, true);
|
---|
| 139 | return success;
|
---|
[f761f1eb] | 140 | }
|
---|
[b45c443] | 141 |
|
---|
[1bb2e7a] | 142 | /** @}
|
---|
[b45c443] | 143 | */
|
---|