source: mainline/kernel/generic/src/time/timeout.c

Last change on this file was 4760793, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 18 months ago

Add CPU_LOCAL alongside CPU and segregate fields that are only used locally

This makes it more clear which fields can be used without synchronization
and which need more care.

  • Property mode set to 100644
File size: 4.9 KB
RevLine 
[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]54void 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]67void timeout_initialize(timeout_t *timeout)
[f761f1eb]68{
[ad58fd2]69 link_initialize(&timeout->link);
70 timeout->cpu = NULL;
[f761f1eb]71}
72
[111b9b9]73/* Only call when interrupts are disabled. */
74deadline_t timeout_deadline_in_usec(uint32_t usec)
[f761f1eb]75{
[111b9b9]76 if (usec == 0)
77 return 0;
78
[4760793]79 return CPU_LOCAL->current_clock_tick + us2ticks(usec);
[111b9b9]80}
[a35b458]81
[111b9b9]82static void timeout_register_deadline_locked(timeout_t *timeout, deadline_t deadline,
83 timeout_handler_t handler, void *arg)
84{
[61ae4b0]85 assert(!link_in_use(&timeout->link));
86
[ba25c4b]87 *timeout = (timeout_t) {
88 .cpu = CPU,
[111b9b9]89 .deadline = deadline,
[ba25c4b]90 .handler = handler,
91 .arg = arg,
92 .finished = ATOMIC_VAR_INIT(false),
93 };
[a35b458]94
[61ae4b0]95 /* Insert timeout into the active timeouts list according to timeout->deadline. */
[a35b458]96
[61ae4b0]97 link_t *last = list_last(&CPU->timeout_active_list);
98 if (last == NULL || timeout->deadline >= list_get_instance(last, timeout_t, link)->deadline) {
99 list_append(&timeout->link, &CPU->timeout_active_list);
100 } else {
101 for (link_t *cur = list_first(&CPU->timeout_active_list); cur != NULL;
102 cur = list_next(cur, &CPU->timeout_active_list)) {
[a35b458]103
[61ae4b0]104 if (timeout->deadline < list_get_instance(cur, timeout_t, link)->deadline) {
105 list_insert_before(&timeout->link, cur);
106 break;
107 }
108 }
[f761f1eb]109 }
[111b9b9]110}
111
112/** Register timeout
113 *
114 * Insert timeout handler f (with argument arg)
115 * to timeout list and make it execute in
116 * time microseconds (or slightly more).
117 *
118 * @param timeout Timeout structure.
119 * @param time Number of usec in the future to execute the handler.
120 * @param handler Timeout handler function.
121 * @param arg Timeout handler argument.
122 *
123 */
124void timeout_register(timeout_t *timeout, uint64_t time,
125 timeout_handler_t handler, void *arg)
126{
127 irq_spinlock_lock(&CPU->timeoutlock, true);
128 timeout_register_deadline_locked(timeout, timeout_deadline_in_usec(time), handler, arg);
129 irq_spinlock_unlock(&CPU->timeoutlock, true);
130}
[a35b458]131
[111b9b9]132void timeout_register_deadline(timeout_t *timeout, deadline_t deadline,
133 timeout_handler_t handler, void *arg)
134{
135 irq_spinlock_lock(&CPU->timeoutlock, true);
136 timeout_register_deadline_locked(timeout, deadline, handler, arg);
[da1bafb]137 irq_spinlock_unlock(&CPU->timeoutlock, true);
[f761f1eb]138}
139
[ba1b2194]140/** Unregister timeout
[70527f1]141 *
142 * Remove timeout from timeout list.
143 *
[da1bafb]144 * @param timeout Timeout to unregister.
145 *
146 * @return True on success, false on failure.
[70527f1]147 *
148 */
[da1bafb]149bool timeout_unregister(timeout_t *timeout)
[f761f1eb]150{
[83789ea2]151 if (atomic_load_explicit(&timeout->finished, memory_order_acquire))
152 /* The timeout fired and finished already, no need to check the list. */
153 return false;
154
[ad58fd2]155 assert(timeout->cpu);
[a35b458]156
[ad58fd2]157 irq_spinlock_lock(&timeout->cpu->timeoutlock, true);
[a35b458]158
[ad58fd2]159 bool success = link_in_use(&timeout->link);
160 if (success) {
161 list_remove(&timeout->link);
[f761f1eb]162 }
[a35b458]163
[ad58fd2]164 irq_spinlock_unlock(&timeout->cpu->timeoutlock, true);
[ba25c4b]165
166 if (!success) {
167 /* Timeout was fired, we need to wait for the callback to finish. */
168 while (!atomic_load_explicit(&timeout->finished, memory_order_acquire))
169 cpu_spin_hint();
170 }
171
[ad58fd2]172 return success;
[f761f1eb]173}
[b45c443]174
[1bb2e7a]175/** @}
[b45c443]176 */
Note: See TracBrowser for help on using the repository browser.