[f761f1eb] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
[eda43238] | 3 | * Copyright (c) 2022 Jiří Zárevúcky
|
---|
[f761f1eb] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[e88eb48] | 30 | /** @addtogroup kernel_time
|
---|
[b45c443] | 31 | * @{
|
---|
| 32 | */
|
---|
| 33 |
|
---|
[cf26ba9] | 34 | /**
|
---|
[b45c443] | 35 | * @file
|
---|
[da1bafb] | 36 | * @brief Timeout management functions.
|
---|
[cf26ba9] | 37 | */
|
---|
| 38 |
|
---|
[f761f1eb] | 39 | #include <time/timeout.h>
|
---|
[d99c1d2] | 40 | #include <typedefs.h>
|
---|
[f761f1eb] | 41 | #include <config.h>
|
---|
[02a99d2] | 42 | #include <panic.h>
|
---|
[f761f1eb] | 43 | #include <synch/spinlock.h>
|
---|
[b2e121a] | 44 | #include <halt.h>
|
---|
[f761f1eb] | 45 | #include <cpu.h>
|
---|
| 46 | #include <arch/asm.h>
|
---|
| 47 | #include <arch.h>
|
---|
| 48 |
|
---|
[70527f1] | 49 | /** Initialize timeouts
|
---|
| 50 | *
|
---|
| 51 | * Initialize kernel timeouts.
|
---|
| 52 | *
|
---|
| 53 | */
|
---|
[f761f1eb] | 54 | void timeout_init(void)
|
---|
| 55 | {
|
---|
[da1bafb] | 56 | irq_spinlock_initialize(&CPU->timeoutlock, "cpu.timeoutlock");
|
---|
[55b77d9] | 57 | list_initialize(&CPU->timeout_active_list);
|
---|
[f761f1eb] | 58 | }
|
---|
| 59 |
|
---|
[ba1b2194] | 60 | /** Initialize timeout
|
---|
[70527f1] | 61 | *
|
---|
[ba1b2194] | 62 | * Initialize all members including the lock.
|
---|
[70527f1] | 63 | *
|
---|
[da1bafb] | 64 | * @param timeout Timeout to be initialized.
|
---|
[70527f1] | 65 | *
|
---|
| 66 | */
|
---|
[da1bafb] | 67 | void timeout_initialize(timeout_t *timeout)
|
---|
[f761f1eb] | 68 | {
|
---|
[ad58fd2] | 69 | link_initialize(&timeout->link);
|
---|
| 70 | timeout->cpu = NULL;
|
---|
[f761f1eb] | 71 | }
|
---|
| 72 |
|
---|
[ba1b2194] | 73 | /** Register timeout
|
---|
[70527f1] | 74 | *
|
---|
[ba1b2194] | 75 | * Insert timeout handler f (with argument arg)
|
---|
| 76 | * to timeout list and make it execute in
|
---|
[70527f1] | 77 | * time microseconds (or slightly more).
|
---|
| 78 | *
|
---|
[da1bafb] | 79 | * @param timeout Timeout structure.
|
---|
| 80 | * @param time Number of usec in the future to execute the handler.
|
---|
| 81 | * @param handler Timeout handler function.
|
---|
| 82 | * @param arg Timeout handler argument.
|
---|
[70527f1] | 83 | *
|
---|
[f761f1eb] | 84 | */
|
---|
[da1bafb] | 85 | void timeout_register(timeout_t *timeout, uint64_t time,
|
---|
| 86 | timeout_handler_t handler, void *arg)
|
---|
[f761f1eb] | 87 | {
|
---|
[da1bafb] | 88 | irq_spinlock_lock(&CPU->timeoutlock, true);
|
---|
[a35b458] | 89 |
|
---|
[61ae4b0] | 90 | assert(!link_in_use(&timeout->link));
|
---|
| 91 |
|
---|
[da1bafb] | 92 | timeout->cpu = CPU;
|
---|
[742f95ec] | 93 | timeout->deadline = CPU->current_clock_tick + us2ticks(time);
|
---|
[da1bafb] | 94 | timeout->handler = handler;
|
---|
| 95 | timeout->arg = arg;
|
---|
[a35b458] | 96 |
|
---|
[61ae4b0] | 97 | /* Insert timeout into the active timeouts list according to timeout->deadline. */
|
---|
[a35b458] | 98 |
|
---|
[61ae4b0] | 99 | link_t *last = list_last(&CPU->timeout_active_list);
|
---|
| 100 | if (last == NULL || timeout->deadline >= list_get_instance(last, timeout_t, link)->deadline) {
|
---|
| 101 | list_append(&timeout->link, &CPU->timeout_active_list);
|
---|
| 102 | } else {
|
---|
| 103 | for (link_t *cur = list_first(&CPU->timeout_active_list); cur != NULL;
|
---|
| 104 | cur = list_next(cur, &CPU->timeout_active_list)) {
|
---|
[a35b458] | 105 |
|
---|
[61ae4b0] | 106 | if (timeout->deadline < list_get_instance(cur, timeout_t, link)->deadline) {
|
---|
| 107 | list_insert_before(&timeout->link, cur);
|
---|
| 108 | break;
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
[f761f1eb] | 111 | }
|
---|
[a35b458] | 112 |
|
---|
[da1bafb] | 113 | irq_spinlock_unlock(&CPU->timeoutlock, true);
|
---|
[f761f1eb] | 114 | }
|
---|
| 115 |
|
---|
[ba1b2194] | 116 | /** Unregister timeout
|
---|
[70527f1] | 117 | *
|
---|
| 118 | * Remove timeout from timeout list.
|
---|
| 119 | *
|
---|
[da1bafb] | 120 | * @param timeout Timeout to unregister.
|
---|
| 121 | *
|
---|
| 122 | * @return True on success, false on failure.
|
---|
[70527f1] | 123 | *
|
---|
| 124 | */
|
---|
[da1bafb] | 125 | bool timeout_unregister(timeout_t *timeout)
|
---|
[f761f1eb] | 126 | {
|
---|
[ad58fd2] | 127 | assert(timeout->cpu);
|
---|
[a35b458] | 128 |
|
---|
[ad58fd2] | 129 | irq_spinlock_lock(&timeout->cpu->timeoutlock, true);
|
---|
[a35b458] | 130 |
|
---|
[ad58fd2] | 131 | bool success = link_in_use(&timeout->link);
|
---|
| 132 | if (success) {
|
---|
| 133 | list_remove(&timeout->link);
|
---|
[f761f1eb] | 134 | }
|
---|
[a35b458] | 135 |
|
---|
[ad58fd2] | 136 | irq_spinlock_unlock(&timeout->cpu->timeoutlock, true);
|
---|
| 137 | return success;
|
---|
[f761f1eb] | 138 | }
|
---|
[b45c443] | 139 |
|
---|
[1bb2e7a] | 140 | /** @}
|
---|
[b45c443] | 141 | */
|
---|