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