| 1 | /*
|
|---|
| 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
|---|
| 3 | * Copyright (c) 2022 Jiří Zárevúcky
|
|---|
| 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 |
|
|---|
| 30 | /** @addtogroup kernel_time
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * @file
|
|---|
| 36 | * @brief Timeout management functions.
|
|---|
| 37 | */
|
|---|
| 38 |
|
|---|
| 39 | #include <time/timeout.h>
|
|---|
| 40 | #include <typedefs.h>
|
|---|
| 41 | #include <config.h>
|
|---|
| 42 | #include <panic.h>
|
|---|
| 43 | #include <synch/spinlock.h>
|
|---|
| 44 | #include <halt.h>
|
|---|
| 45 | #include <cpu.h>
|
|---|
| 46 | #include <arch/asm.h>
|
|---|
| 47 | #include <arch.h>
|
|---|
| 48 |
|
|---|
| 49 | /** Initialize timeouts
|
|---|
| 50 | *
|
|---|
| 51 | * Initialize kernel timeouts.
|
|---|
| 52 | *
|
|---|
| 53 | */
|
|---|
| 54 | void timeout_init(void)
|
|---|
| 55 | {
|
|---|
| 56 | irq_spinlock_initialize(&CPU->timeoutlock, "cpu.timeoutlock");
|
|---|
| 57 | list_initialize(&CPU->timeout_active_list);
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | /** Initialize timeout
|
|---|
| 61 | *
|
|---|
| 62 | * Initialize all members including the lock.
|
|---|
| 63 | *
|
|---|
| 64 | * @param timeout Timeout to be initialized.
|
|---|
| 65 | *
|
|---|
| 66 | */
|
|---|
| 67 | void timeout_initialize(timeout_t *timeout)
|
|---|
| 68 | {
|
|---|
| 69 | link_initialize(&timeout->link);
|
|---|
| 70 | timeout->cpu = NULL;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /** Register timeout
|
|---|
| 74 | *
|
|---|
| 75 | * Insert timeout handler f (with argument arg)
|
|---|
| 76 | * to timeout list and make it execute in
|
|---|
| 77 | * time microseconds (or slightly more).
|
|---|
| 78 | *
|
|---|
| 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.
|
|---|
| 83 | *
|
|---|
| 84 | */
|
|---|
| 85 | void timeout_register(timeout_t *timeout, uint64_t time,
|
|---|
| 86 | timeout_handler_t handler, void *arg)
|
|---|
| 87 | {
|
|---|
| 88 | irq_spinlock_lock(&CPU->timeoutlock, true);
|
|---|
| 89 |
|
|---|
| 90 | assert(!link_in_use(&timeout->link));
|
|---|
| 91 |
|
|---|
| 92 | timeout->cpu = CPU;
|
|---|
| 93 | timeout->deadline = CPU->current_clock_tick + us2ticks(time);
|
|---|
| 94 | timeout->handler = handler;
|
|---|
| 95 | timeout->arg = arg;
|
|---|
| 96 |
|
|---|
| 97 | /* Insert timeout into the active timeouts list according to timeout->deadline. */
|
|---|
| 98 |
|
|---|
| 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)) {
|
|---|
| 105 |
|
|---|
| 106 | if (timeout->deadline < list_get_instance(cur, timeout_t, link)->deadline) {
|
|---|
| 107 | list_insert_before(&timeout->link, cur);
|
|---|
| 108 | break;
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | irq_spinlock_unlock(&CPU->timeoutlock, true);
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | /** Unregister timeout
|
|---|
| 117 | *
|
|---|
| 118 | * Remove timeout from timeout list.
|
|---|
| 119 | *
|
|---|
| 120 | * @param timeout Timeout to unregister.
|
|---|
| 121 | *
|
|---|
| 122 | * @return True on success, false on failure.
|
|---|
| 123 | *
|
|---|
| 124 | */
|
|---|
| 125 | bool timeout_unregister(timeout_t *timeout)
|
|---|
| 126 | {
|
|---|
| 127 | assert(timeout->cpu);
|
|---|
| 128 |
|
|---|
| 129 | irq_spinlock_lock(&timeout->cpu->timeoutlock, true);
|
|---|
| 130 |
|
|---|
| 131 | bool success = link_in_use(&timeout->link);
|
|---|
| 132 | if (success) {
|
|---|
| 133 | list_remove(&timeout->link);
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | irq_spinlock_unlock(&timeout->cpu->timeoutlock, true);
|
|---|
| 137 | return success;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | /** @}
|
|---|
| 141 | */
|
|---|