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

Last change on this file since 5663872 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
Line 
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 */
54void 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 */
67void timeout_initialize(timeout_t *timeout)
68{
69 link_initialize(&timeout->link);
70 timeout->cpu = NULL;
71}
72
73/* Only call when interrupts are disabled. */
74deadline_t timeout_deadline_in_usec(uint32_t usec)
75{
76 if (usec == 0)
77 return 0;
78
79 return CPU_LOCAL->current_clock_tick + us2ticks(usec);
80}
81
82static void timeout_register_deadline_locked(timeout_t *timeout, deadline_t deadline,
83 timeout_handler_t handler, void *arg)
84{
85 assert(!link_in_use(&timeout->link));
86
87 *timeout = (timeout_t) {
88 .cpu = CPU,
89 .deadline = deadline,
90 .handler = handler,
91 .arg = arg,
92 .finished = ATOMIC_VAR_INIT(false),
93 };
94
95 /* Insert timeout into the active timeouts list according to timeout->deadline. */
96
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)) {
103
104 if (timeout->deadline < list_get_instance(cur, timeout_t, link)->deadline) {
105 list_insert_before(&timeout->link, cur);
106 break;
107 }
108 }
109 }
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}
131
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);
137 irq_spinlock_unlock(&CPU->timeoutlock, true);
138}
139
140/** Unregister timeout
141 *
142 * Remove timeout from timeout list.
143 *
144 * @param timeout Timeout to unregister.
145 *
146 * @return True on success, false on failure.
147 *
148 */
149bool timeout_unregister(timeout_t *timeout)
150{
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
155 assert(timeout->cpu);
156
157 irq_spinlock_lock(&timeout->cpu->timeoutlock, true);
158
159 bool success = link_in_use(&timeout->link);
160 if (success) {
161 list_remove(&timeout->link);
162 }
163
164 irq_spinlock_unlock(&timeout->cpu->timeoutlock, true);
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
172 return success;
173}
174
175/** @}
176 */
Note: See TracBrowser for help on using the repository browser.