Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 1997-2016 Wind River Systems, Inc. |
| 3 | * |
David B. Kinder | ac74d8b | 2017-01-18 17:01:01 -0800 | [diff] [blame] | 4 | * SPDX-License-Identifier: Apache-2.0 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 5 | */ |
| 6 | |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 7 | #include <zephyr/kernel.h> |
Anas Nashif | 4d994af | 2021-04-18 23:24:40 -0400 | [diff] [blame] | 8 | |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 9 | #include <zephyr/init.h> |
Anas Nashif | 4e39617 | 2023-09-26 22:46:01 +0000 | [diff] [blame] | 10 | #include <zephyr/internal/syscall_handler.h> |
Flavio Ceolin | 76b3518 | 2018-12-16 12:48:29 -0800 | [diff] [blame] | 11 | #include <stdbool.h> |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 12 | #include <zephyr/spinlock.h> |
Anas Nashif | 8634c3b | 2023-08-29 17:03:12 +0000 | [diff] [blame] | 13 | #include <ksched.h> |
| 14 | #include <wait_q.h> |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 15 | |
Andy Ross | b29fb22 | 2019-02-05 16:19:30 -0800 | [diff] [blame] | 16 | static struct k_spinlock lock; |
| 17 | |
Peter Mitsis | 6df8efe | 2023-05-11 14:06:46 -0400 | [diff] [blame] | 18 | #ifdef CONFIG_OBJ_CORE_TIMER |
| 19 | static struct k_obj_type obj_type_timer; |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 20 | #endif /* CONFIG_OBJ_CORE_TIMER */ |
Peter Mitsis | 6df8efe | 2023-05-11 14:06:46 -0400 | [diff] [blame] | 21 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 22 | /** |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 23 | * @brief Handle expiration of a kernel timer object. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 24 | * |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 25 | * @param t Timeout used by the timer. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 26 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 27 | void z_timer_expiration_handler(struct _timeout *t) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 28 | { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 29 | struct k_timer *timer = CONTAINER_OF(t, struct k_timer, timeout); |
Benjamin Walsh | b889fa8 | 2016-12-07 22:39:31 -0500 | [diff] [blame] | 30 | struct k_thread *thread; |
Chen Peng1 | dde3d6c | 2021-09-29 10:21:58 +0800 | [diff] [blame] | 31 | k_spinlock_key_t key = k_spin_lock(&lock); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 32 | |
Andrzej Głąbek | e60af79 | 2023-02-07 16:21:54 +0100 | [diff] [blame] | 33 | /* In sys_clock_announce(), when a timeout expires, it is first removed |
| 34 | * from the timeout list, then its expiration handler is called (with |
| 35 | * unlocked interrupts). For kernel timers, the expiration handler is |
| 36 | * this function. Usually, the timeout structure related to the timer |
| 37 | * that is handled here will not be linked to the timeout list at this |
| 38 | * point. But it may happen that before this function is executed and |
| 39 | * interrupts are locked again, a given timer gets restarted from an |
| 40 | * interrupt context that has a priority higher than the system timer |
| 41 | * interrupt. Then, the timeout structure for this timer will turn out |
| 42 | * to be linked to the timeout list. And in such case, since the timer |
| 43 | * was restarted, its expiration handler should not be executed then, |
| 44 | * so the function exits immediately. |
| 45 | */ |
| 46 | if (sys_dnode_is_linked(&t->node)) { |
| 47 | k_spin_unlock(&lock, key); |
| 48 | return; |
| 49 | } |
| 50 | |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 51 | /* |
| 52 | * if the timer is periodic, start it again; don't add _TICK_ALIGN |
| 53 | * since we're already aligned to a tick boundary |
| 54 | */ |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 55 | if (!K_TIMEOUT_EQ(timer->period, K_NO_WAIT) && |
| 56 | !K_TIMEOUT_EQ(timer->period, K_FOREVER)) { |
Andy Ross | 7a59ceb | 2022-04-07 13:49:20 -0700 | [diff] [blame] | 57 | k_timeout_t next = timer->period; |
| 58 | |
Nicolas Pitre | 6a51a10 | 2023-02-13 00:43:47 -0500 | [diff] [blame] | 59 | /* see note about z_add_timeout() in z_impl_k_timer_start() */ |
| 60 | next.ticks = MAX(next.ticks - 1, 0); |
| 61 | |
Andy Ross | 7a59ceb | 2022-04-07 13:49:20 -0700 | [diff] [blame] | 62 | #ifdef CONFIG_TIMEOUT_64BIT |
| 63 | /* Exploit the fact that uptime during a kernel |
| 64 | * timeout handler reflects the time of the scheduled |
| 65 | * event and not real time to get some inexpensive |
| 66 | * protection against late interrupts. If we're |
| 67 | * delayed for any reason, we still end up calculating |
| 68 | * the next expiration as a regular stride from where |
| 69 | * we "should" have run. Requires absolute timeouts. |
| 70 | * (Note offset by one: we're nominally at the |
| 71 | * beginning of a tick, so need to defeat the "round |
| 72 | * down" behavior on timeout addition). |
| 73 | */ |
Nicolas Pitre | 6a51a10 | 2023-02-13 00:43:47 -0500 | [diff] [blame] | 74 | next = K_TIMEOUT_ABS_TICKS(k_uptime_ticks() + 1 + next.ticks); |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 75 | #endif /* CONFIG_TIMEOUT_64BIT */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 76 | z_add_timeout(&timer->timeout, z_timer_expiration_handler, |
Andy Ross | 7a59ceb | 2022-04-07 13:49:20 -0700 | [diff] [blame] | 77 | next); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 78 | } |
| 79 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 80 | /* update timer's status */ |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 81 | timer->status += 1U; |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 82 | |
| 83 | /* invoke timer expiry function */ |
Flavio Ceolin | 76b3518 | 2018-12-16 12:48:29 -0800 | [diff] [blame] | 84 | if (timer->expiry_fn != NULL) { |
Krzysztof Chruscinski | 8979fbc | 2021-11-03 16:24:11 +0100 | [diff] [blame] | 85 | /* Unlock for user handler. */ |
| 86 | k_spin_unlock(&lock, key); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 87 | timer->expiry_fn(timer); |
Krzysztof Chruscinski | 8979fbc | 2021-11-03 16:24:11 +0100 | [diff] [blame] | 88 | key = k_spin_lock(&lock); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 89 | } |
Benjamin Walsh | b889fa8 | 2016-12-07 22:39:31 -0500 | [diff] [blame] | 90 | |
Krzysztof Chruscinski | dd0715c | 2021-04-14 13:36:58 +0200 | [diff] [blame] | 91 | if (!IS_ENABLED(CONFIG_MULTITHREADING)) { |
Chen Peng1 | dde3d6c | 2021-09-29 10:21:58 +0800 | [diff] [blame] | 92 | k_spin_unlock(&lock, key); |
Krzysztof Chruscinski | dd0715c | 2021-04-14 13:36:58 +0200 | [diff] [blame] | 93 | return; |
| 94 | } |
| 95 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 96 | thread = z_waitq_head(&timer->wait_q); |
Benjamin Walsh | b889fa8 | 2016-12-07 22:39:31 -0500 | [diff] [blame] | 97 | |
Flavio Ceolin | 4218d5f | 2018-09-17 09:39:51 -0700 | [diff] [blame] | 98 | if (thread == NULL) { |
Chen Peng1 | dde3d6c | 2021-09-29 10:21:58 +0800 | [diff] [blame] | 99 | k_spin_unlock(&lock, key); |
Benjamin Walsh | b889fa8 | 2016-12-07 22:39:31 -0500 | [diff] [blame] | 100 | return; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 101 | } |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 102 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 103 | z_unpend_thread_no_timeout(thread); |
Benjamin Walsh | b889fa8 | 2016-12-07 22:39:31 -0500 | [diff] [blame] | 104 | |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 105 | arch_thread_return_value_set(thread, 0); |
James Harris | c7bb423 | 2021-03-02 13:22:52 -0800 | [diff] [blame] | 106 | |
Chen Peng1 | dde3d6c | 2021-09-29 10:21:58 +0800 | [diff] [blame] | 107 | k_spin_unlock(&lock, key); |
| 108 | |
James Harris | c7bb423 | 2021-03-02 13:22:52 -0800 | [diff] [blame] | 109 | z_ready_thread(thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 113 | void k_timer_init(struct k_timer *timer, |
Flavio Ceolin | 118715c | 2018-11-16 19:52:37 -0800 | [diff] [blame] | 114 | k_timer_expiry_t expiry_fn, |
| 115 | k_timer_stop_t stop_fn) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 116 | { |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 117 | timer->expiry_fn = expiry_fn; |
| 118 | timer->stop_fn = stop_fn; |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 119 | timer->status = 0U; |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 120 | |
Krzysztof Chruscinski | dd0715c | 2021-04-14 13:36:58 +0200 | [diff] [blame] | 121 | if (IS_ENABLED(CONFIG_MULTITHREADING)) { |
| 122 | z_waitq_init(&timer->wait_q); |
| 123 | } |
| 124 | |
Peter A. Bigot | 5639ea0 | 2019-09-27 09:20:26 -0500 | [diff] [blame] | 125 | z_init_timeout(&timer->timeout); |
Torbjörn Leksell | 3a66d6c | 2021-03-26 14:09:10 +0100 | [diff] [blame] | 126 | |
| 127 | SYS_PORT_TRACING_OBJ_INIT(k_timer, timer); |
| 128 | |
Maciek Borzecki | 4fef760 | 2017-05-18 08:49:50 +0200 | [diff] [blame] | 129 | timer->user_data = NULL; |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 130 | |
Anas Nashif | c91cad7 | 2023-09-26 21:32:13 +0000 | [diff] [blame] | 131 | k_object_init(timer); |
Peter Mitsis | 6df8efe | 2023-05-11 14:06:46 -0400 | [diff] [blame] | 132 | |
| 133 | #ifdef CONFIG_OBJ_CORE_TIMER |
| 134 | k_obj_core_init_and_link(K_OBJ_CORE(timer), &obj_type_timer); |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 135 | #endif /* CONFIG_OBJ_CORE_TIMER */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 139 | void z_impl_k_timer_start(struct k_timer *timer, k_timeout_t duration, |
| 140 | k_timeout_t period) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 141 | { |
Anas Nashif | 6a9540a | 2022-04-12 15:29:37 -0400 | [diff] [blame] | 142 | SYS_PORT_TRACING_OBJ_FUNC(k_timer, start, timer, duration, period); |
Torbjörn Leksell | 3a66d6c | 2021-03-26 14:09:10 +0100 | [diff] [blame] | 143 | |
Pedro Sousa | 4207f4a | 2023-10-03 22:35:05 +0100 | [diff] [blame] | 144 | /* Acquire spinlock to ensure safety during concurrent calls to |
| 145 | * k_timer_start for scheduling or rescheduling. This is necessary |
| 146 | * since k_timer_start can be preempted, especially for the same |
| 147 | * timer instance. |
| 148 | */ |
| 149 | k_spinlock_key_t key = k_spin_lock(&lock); |
| 150 | |
Andy Ross | a343cf9 | 2020-05-29 07:36:39 -0700 | [diff] [blame] | 151 | if (K_TIMEOUT_EQ(duration, K_FOREVER)) { |
Pedro Sousa | 4207f4a | 2023-10-03 22:35:05 +0100 | [diff] [blame] | 152 | k_spin_unlock(&lock, key); |
Andy Ross | a343cf9 | 2020-05-29 07:36:39 -0700 | [diff] [blame] | 153 | return; |
| 154 | } |
| 155 | |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 156 | /* z_add_timeout() always adds one to the incoming tick count |
| 157 | * to round up to the next tick (by convention it waits for |
| 158 | * "at least as long as the specified timeout"), but the |
| 159 | * period interval is always guaranteed to be reset from |
Nicolas Pitre | 6a51a10 | 2023-02-13 00:43:47 -0500 | [diff] [blame] | 160 | * within the timer ISR, so no round up is desired and 1 is |
| 161 | * subtracted in there. |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 162 | * |
| 163 | * Note that the duration (!) value gets the same treatment |
| 164 | * for backwards compatibility. This is unfortunate |
| 165 | * (i.e. k_timer_start() doesn't treat its initial sleep |
| 166 | * argument the same way k_sleep() does), but historical. The |
| 167 | * timer_api test relies on this behavior. |
| 168 | */ |
Andy Ross | 4c7b77a | 2020-03-09 09:35:35 -0700 | [diff] [blame] | 169 | if (Z_TICK_ABS(duration.ticks) < 0) { |
| 170 | duration.ticks = MAX(duration.ticks - 1, 0); |
| 171 | } |
Benjamin Walsh | 6ca6c28 | 2016-12-09 13:39:00 -0500 | [diff] [blame] | 172 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 173 | (void)z_abort_timeout(&timer->timeout); |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 174 | timer->period = period; |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 175 | timer->status = 0U; |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 176 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 177 | z_add_timeout(&timer->timeout, z_timer_expiration_handler, |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 178 | duration); |
Pedro Sousa | 4207f4a | 2023-10-03 22:35:05 +0100 | [diff] [blame] | 179 | |
| 180 | k_spin_unlock(&lock, key); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 181 | } |
| 182 | |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 183 | #ifdef CONFIG_USERSPACE |
Andy Ross | 643701a | 2019-08-13 12:58:38 -0700 | [diff] [blame] | 184 | static inline void z_vrfy_k_timer_start(struct k_timer *timer, |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 185 | k_timeout_t duration, |
| 186 | k_timeout_t period) |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 187 | { |
Anas Nashif | a08bfeb | 2023-09-27 11:20:28 +0000 | [diff] [blame] | 188 | K_OOPS(K_SYSCALL_OBJ(timer, K_OBJ_TIMER)); |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 189 | z_impl_k_timer_start(timer, duration, period); |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 190 | } |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 191 | #include <syscalls/k_timer_start_mrsh.c> |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 192 | #endif /* CONFIG_USERSPACE */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 193 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 194 | void z_impl_k_timer_stop(struct k_timer *timer) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 195 | { |
Torbjörn Leksell | 3a66d6c | 2021-03-26 14:09:10 +0100 | [diff] [blame] | 196 | SYS_PORT_TRACING_OBJ_FUNC(k_timer, stop, timer); |
| 197 | |
Simon Hein | 02cfbfe | 2022-07-19 22:30:17 +0200 | [diff] [blame] | 198 | bool inactive = (z_abort_timeout(&timer->timeout) != 0); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 199 | |
Benjamin Walsh | d211a52 | 2016-12-06 11:44:01 -0500 | [diff] [blame] | 200 | if (inactive) { |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 201 | return; |
| 202 | } |
| 203 | |
Flavio Ceolin | 76b3518 | 2018-12-16 12:48:29 -0800 | [diff] [blame] | 204 | if (timer->stop_fn != NULL) { |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 205 | timer->stop_fn(timer); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 206 | } |
| 207 | |
Krzysztof Chruscinski | dd0715c | 2021-04-14 13:36:58 +0200 | [diff] [blame] | 208 | if (IS_ENABLED(CONFIG_MULTITHREADING)) { |
| 209 | struct k_thread *pending_thread = z_unpend1_no_timeout(&timer->wait_q); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 210 | |
Krzysztof Chruscinski | dd0715c | 2021-04-14 13:36:58 +0200 | [diff] [blame] | 211 | if (pending_thread != NULL) { |
| 212 | z_ready_thread(pending_thread); |
| 213 | z_reschedule_unlocked(); |
| 214 | } |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 215 | } |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 216 | } |
| 217 | |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 218 | #ifdef CONFIG_USERSPACE |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 219 | static inline void z_vrfy_k_timer_stop(struct k_timer *timer) |
| 220 | { |
Anas Nashif | a08bfeb | 2023-09-27 11:20:28 +0000 | [diff] [blame] | 221 | K_OOPS(K_SYSCALL_OBJ(timer, K_OBJ_TIMER)); |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 222 | z_impl_k_timer_stop(timer); |
| 223 | } |
| 224 | #include <syscalls/k_timer_stop_mrsh.c> |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 225 | #endif /* CONFIG_USERSPACE */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 226 | |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 227 | uint32_t z_impl_k_timer_status_get(struct k_timer *timer) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 228 | { |
Andy Ross | b29fb22 | 2019-02-05 16:19:30 -0800 | [diff] [blame] | 229 | k_spinlock_key_t key = k_spin_lock(&lock); |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 230 | uint32_t result = timer->status; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 231 | |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 232 | timer->status = 0U; |
Andy Ross | b29fb22 | 2019-02-05 16:19:30 -0800 | [diff] [blame] | 233 | k_spin_unlock(&lock, key); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 234 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 235 | return result; |
| 236 | } |
| 237 | |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 238 | #ifdef CONFIG_USERSPACE |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 239 | static inline uint32_t z_vrfy_k_timer_status_get(struct k_timer *timer) |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 240 | { |
Anas Nashif | a08bfeb | 2023-09-27 11:20:28 +0000 | [diff] [blame] | 241 | K_OOPS(K_SYSCALL_OBJ(timer, K_OBJ_TIMER)); |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 242 | return z_impl_k_timer_status_get(timer); |
| 243 | } |
| 244 | #include <syscalls/k_timer_status_get_mrsh.c> |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 245 | #endif /* CONFIG_USERSPACE */ |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 246 | |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 247 | uint32_t z_impl_k_timer_status_sync(struct k_timer *timer) |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 248 | { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 249 | __ASSERT(!arch_is_in_isr(), ""); |
Torbjörn Leksell | 3a66d6c | 2021-03-26 14:09:10 +0100 | [diff] [blame] | 250 | SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_timer, status_sync, timer); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 251 | |
Krzysztof Chruscinski | dd0715c | 2021-04-14 13:36:58 +0200 | [diff] [blame] | 252 | if (!IS_ENABLED(CONFIG_MULTITHREADING)) { |
| 253 | uint32_t result; |
| 254 | |
| 255 | do { |
| 256 | k_spinlock_key_t key = k_spin_lock(&lock); |
| 257 | |
| 258 | if (!z_is_inactive_timeout(&timer->timeout)) { |
| 259 | result = *(volatile uint32_t *)&timer->status; |
| 260 | timer->status = 0U; |
| 261 | k_spin_unlock(&lock, key); |
| 262 | if (result > 0) { |
| 263 | break; |
| 264 | } |
| 265 | } else { |
| 266 | result = timer->status; |
| 267 | k_spin_unlock(&lock, key); |
| 268 | break; |
| 269 | } |
| 270 | } while (true); |
| 271 | |
| 272 | return result; |
| 273 | } |
| 274 | |
Andy Ross | b29fb22 | 2019-02-05 16:19:30 -0800 | [diff] [blame] | 275 | k_spinlock_key_t key = k_spin_lock(&lock); |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 276 | uint32_t result = timer->status; |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 277 | |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 278 | if (result == 0U) { |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 279 | if (!z_is_inactive_timeout(&timer->timeout)) { |
Torbjörn Leksell | 3a66d6c | 2021-03-26 14:09:10 +0100 | [diff] [blame] | 280 | SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_timer, status_sync, timer, K_FOREVER); |
| 281 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 282 | /* wait for timer to expire or stop */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 283 | (void)z_pend_curr(&lock, key, &timer->wait_q, K_FOREVER); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 284 | |
| 285 | /* get updated timer status */ |
Andy Ross | b29fb22 | 2019-02-05 16:19:30 -0800 | [diff] [blame] | 286 | key = k_spin_lock(&lock); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 287 | result = timer->status; |
| 288 | } else { |
| 289 | /* timer is already stopped */ |
| 290 | } |
| 291 | } else { |
| 292 | /* timer has already expired at least once */ |
| 293 | } |
| 294 | |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 295 | timer->status = 0U; |
Andy Ross | b29fb22 | 2019-02-05 16:19:30 -0800 | [diff] [blame] | 296 | k_spin_unlock(&lock, key); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 297 | |
Torbjörn Leksell | 3a66d6c | 2021-03-26 14:09:10 +0100 | [diff] [blame] | 298 | /** |
| 299 | * @note New tracing hook |
| 300 | */ |
| 301 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_timer, status_sync, timer, result); |
| 302 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 303 | return result; |
| 304 | } |
| 305 | |
Andrew Boie | a354d49 | 2017-09-29 16:22:28 -0700 | [diff] [blame] | 306 | #ifdef CONFIG_USERSPACE |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 307 | static inline uint32_t z_vrfy_k_timer_status_sync(struct k_timer *timer) |
Andrew Boie | 225e4c0 | 2017-10-12 09:54:26 -0700 | [diff] [blame] | 308 | { |
Anas Nashif | a08bfeb | 2023-09-27 11:20:28 +0000 | [diff] [blame] | 309 | K_OOPS(K_SYSCALL_OBJ(timer, K_OBJ_TIMER)); |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 310 | return z_impl_k_timer_status_sync(timer); |
Andrew Boie | 225e4c0 | 2017-10-12 09:54:26 -0700 | [diff] [blame] | 311 | } |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 312 | #include <syscalls/k_timer_status_sync_mrsh.c> |
| 313 | |
Peter Bigot | 0ab314f | 2020-11-16 15:28:59 -0600 | [diff] [blame] | 314 | static inline k_ticks_t z_vrfy_k_timer_remaining_ticks( |
| 315 | const struct k_timer *timer) |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 316 | { |
Anas Nashif | a08bfeb | 2023-09-27 11:20:28 +0000 | [diff] [blame] | 317 | K_OOPS(K_SYSCALL_OBJ(timer, K_OBJ_TIMER)); |
Andy Ross | 5a5d3da | 2020-03-09 13:59:15 -0700 | [diff] [blame] | 318 | return z_impl_k_timer_remaining_ticks(timer); |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 319 | } |
Andy Ross | 5a5d3da | 2020-03-09 13:59:15 -0700 | [diff] [blame] | 320 | #include <syscalls/k_timer_remaining_ticks_mrsh.c> |
| 321 | |
Peter Bigot | 0ab314f | 2020-11-16 15:28:59 -0600 | [diff] [blame] | 322 | static inline k_ticks_t z_vrfy_k_timer_expires_ticks( |
| 323 | const struct k_timer *timer) |
Andy Ross | 5a5d3da | 2020-03-09 13:59:15 -0700 | [diff] [blame] | 324 | { |
Anas Nashif | a08bfeb | 2023-09-27 11:20:28 +0000 | [diff] [blame] | 325 | K_OOPS(K_SYSCALL_OBJ(timer, K_OBJ_TIMER)); |
Andy Ross | 5a5d3da | 2020-03-09 13:59:15 -0700 | [diff] [blame] | 326 | return z_impl_k_timer_expires_ticks(timer); |
| 327 | } |
| 328 | #include <syscalls/k_timer_expires_ticks_mrsh.c> |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 329 | |
Peter A. Bigot | f1b86ca | 2020-09-18 16:24:57 -0500 | [diff] [blame] | 330 | static inline void *z_vrfy_k_timer_user_data_get(const struct k_timer *timer) |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 331 | { |
Anas Nashif | a08bfeb | 2023-09-27 11:20:28 +0000 | [diff] [blame] | 332 | K_OOPS(K_SYSCALL_OBJ(timer, K_OBJ_TIMER)); |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 333 | return z_impl_k_timer_user_data_get(timer); |
| 334 | } |
| 335 | #include <syscalls/k_timer_user_data_get_mrsh.c> |
| 336 | |
Andy Ross | 643701a | 2019-08-13 12:58:38 -0700 | [diff] [blame] | 337 | static inline void z_vrfy_k_timer_user_data_set(struct k_timer *timer, |
| 338 | void *user_data) |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 339 | { |
Anas Nashif | a08bfeb | 2023-09-27 11:20:28 +0000 | [diff] [blame] | 340 | K_OOPS(K_SYSCALL_OBJ(timer, K_OBJ_TIMER)); |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 341 | z_impl_k_timer_user_data_set(timer, user_data); |
| 342 | } |
| 343 | #include <syscalls/k_timer_user_data_set_mrsh.c> |
| 344 | |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 345 | #endif /* CONFIG_USERSPACE */ |
Peter Mitsis | 6df8efe | 2023-05-11 14:06:46 -0400 | [diff] [blame] | 346 | |
| 347 | #ifdef CONFIG_OBJ_CORE_TIMER |
| 348 | static int init_timer_obj_core_list(void) |
| 349 | { |
| 350 | /* Initialize timer object type */ |
| 351 | |
| 352 | z_obj_type_init(&obj_type_timer, K_OBJ_TYPE_TIMER_ID, |
| 353 | offsetof(struct k_timer, obj_core)); |
| 354 | |
| 355 | /* Initialize and link statically defined timers */ |
| 356 | |
| 357 | STRUCT_SECTION_FOREACH(k_timer, timer) { |
| 358 | k_obj_core_init_and_link(K_OBJ_CORE(timer), &obj_type_timer); |
| 359 | } |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | SYS_INIT(init_timer_obj_core_list, PRE_KERNEL_1, |
| 364 | CONFIG_KERNEL_INIT_PRIORITY_OBJECTS); |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 365 | #endif /* CONFIG_OBJ_CORE_TIMER */ |