blob: 5c338a894269195430257395d50af2781199d5f3 [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 1997-2016 Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
Allan Stephens45bfa372016-10-12 12:39:42 -05007#include <kernel.h>
Anas Nashif4d994af2021-04-18 23:24:40 -04008
Allan Stephense7d2cc22016-10-19 16:10:46 -05009#include <init.h>
Stephanos Ioannidis2d746042019-10-25 00:08:21 +090010#include <ksched.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040011#include <wait_q.h>
Andrew Boiea354d492017-09-29 16:22:28 -070012#include <syscall_handler.h>
Flavio Ceolin76b35182018-12-16 12:48:29 -080013#include <stdbool.h>
Andy Rossb29fb222019-02-05 16:19:30 -080014#include <spinlock.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040015
Andy Rossb29fb222019-02-05 16:19:30 -080016static struct k_spinlock lock;
17
Benjamin Walsh456c6da2016-09-02 18:55:39 -040018/**
Allan Stephens45bfa372016-10-12 12:39:42 -050019 * @brief Handle expiration of a kernel timer object.
Benjamin Walsh456c6da2016-09-02 18:55:39 -040020 *
Allan Stephens45bfa372016-10-12 12:39:42 -050021 * @param t Timeout used by the timer.
Benjamin Walsh456c6da2016-09-02 18:55:39 -040022 */
Patrik Flykt4344e272019-03-08 14:19:05 -070023void z_timer_expiration_handler(struct _timeout *t)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040024{
Benjamin Walsh456c6da2016-09-02 18:55:39 -040025 struct k_timer *timer = CONTAINER_OF(t, struct k_timer, timeout);
Benjamin Walshb889fa82016-12-07 22:39:31 -050026 struct k_thread *thread;
Chen Peng1dde3d6c2021-09-29 10:21:58 +080027 k_spinlock_key_t key = k_spin_lock(&lock);
Benjamin Walsh456c6da2016-09-02 18:55:39 -040028
Allan Stephens6c98c4d2016-10-17 14:34:53 -050029 /*
30 * if the timer is periodic, start it again; don't add _TICK_ALIGN
31 * since we're already aligned to a tick boundary
32 */
Andy Ross78327382020-03-05 15:18:14 -080033 if (!K_TIMEOUT_EQ(timer->period, K_NO_WAIT) &&
34 !K_TIMEOUT_EQ(timer->period, K_FOREVER)) {
Patrik Flykt4344e272019-03-08 14:19:05 -070035 z_add_timeout(&timer->timeout, z_timer_expiration_handler,
Andy Rossfe82f1c2018-09-26 15:14:21 -070036 timer->period);
Benjamin Walsh456c6da2016-09-02 18:55:39 -040037 }
38
Allan Stephens45bfa372016-10-12 12:39:42 -050039 /* update timer's status */
Patrik Flykt24d71432019-03-26 19:57:45 -060040 timer->status += 1U;
Allan Stephens45bfa372016-10-12 12:39:42 -050041
42 /* invoke timer expiry function */
Flavio Ceolin76b35182018-12-16 12:48:29 -080043 if (timer->expiry_fn != NULL) {
Krzysztof Chruscinski8979fbc2021-11-03 16:24:11 +010044 /* Unlock for user handler. */
45 k_spin_unlock(&lock, key);
Allan Stephens45bfa372016-10-12 12:39:42 -050046 timer->expiry_fn(timer);
Krzysztof Chruscinski8979fbc2021-11-03 16:24:11 +010047 key = k_spin_lock(&lock);
Benjamin Walsh456c6da2016-09-02 18:55:39 -040048 }
Benjamin Walshb889fa82016-12-07 22:39:31 -050049
Krzysztof Chruscinskidd0715c2021-04-14 13:36:58 +020050 if (!IS_ENABLED(CONFIG_MULTITHREADING)) {
Chen Peng1dde3d6c2021-09-29 10:21:58 +080051 k_spin_unlock(&lock, key);
Krzysztof Chruscinskidd0715c2021-04-14 13:36:58 +020052 return;
53 }
54
Patrik Flykt4344e272019-03-08 14:19:05 -070055 thread = z_waitq_head(&timer->wait_q);
Benjamin Walshb889fa82016-12-07 22:39:31 -050056
Flavio Ceolin4218d5f2018-09-17 09:39:51 -070057 if (thread == NULL) {
Chen Peng1dde3d6c2021-09-29 10:21:58 +080058 k_spin_unlock(&lock, key);
Benjamin Walshb889fa82016-12-07 22:39:31 -050059 return;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040060 }
Allan Stephens45bfa372016-10-12 12:39:42 -050061
Patrik Flykt4344e272019-03-08 14:19:05 -070062 z_unpend_thread_no_timeout(thread);
Benjamin Walshb889fa82016-12-07 22:39:31 -050063
Andrew Boie4f77c2a2019-11-07 12:43:29 -080064 arch_thread_return_value_set(thread, 0);
James Harrisc7bb4232021-03-02 13:22:52 -080065
Chen Peng1dde3d6c2021-09-29 10:21:58 +080066 k_spin_unlock(&lock, key);
67
James Harrisc7bb4232021-03-02 13:22:52 -080068 z_ready_thread(thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -040069}
70
71
Allan Stephens45bfa372016-10-12 12:39:42 -050072void k_timer_init(struct k_timer *timer,
Flavio Ceolin118715c2018-11-16 19:52:37 -080073 k_timer_expiry_t expiry_fn,
74 k_timer_stop_t stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040075{
Allan Stephens45bfa372016-10-12 12:39:42 -050076 timer->expiry_fn = expiry_fn;
77 timer->stop_fn = stop_fn;
Patrik Flykt24d71432019-03-26 19:57:45 -060078 timer->status = 0U;
Allan Stephens45bfa372016-10-12 12:39:42 -050079
Krzysztof Chruscinskidd0715c2021-04-14 13:36:58 +020080 if (IS_ENABLED(CONFIG_MULTITHREADING)) {
81 z_waitq_init(&timer->wait_q);
82 }
83
Peter A. Bigot5639ea02019-09-27 09:20:26 -050084 z_init_timeout(&timer->timeout);
Torbjörn Leksell3a66d6c2021-03-26 14:09:10 +010085
86 SYS_PORT_TRACING_OBJ_INIT(k_timer, timer);
87
Maciek Borzecki4fef7602017-05-18 08:49:50 +020088 timer->user_data = NULL;
Andrew Boie945af952017-08-22 13:15:23 -070089
Patrik Flykt4344e272019-03-08 14:19:05 -070090 z_object_init(timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -040091}
92
93
Andy Ross78327382020-03-05 15:18:14 -080094void z_impl_k_timer_start(struct k_timer *timer, k_timeout_t duration,
95 k_timeout_t period)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040096{
Torbjörn Leksell3a66d6c2021-03-26 14:09:10 +010097 SYS_PORT_TRACING_OBJ_FUNC(k_timer, start, timer);
98
Andy Rossa343cf92020-05-29 07:36:39 -070099 if (K_TIMEOUT_EQ(duration, K_FOREVER)) {
100 return;
101 }
102
Andy Ross78327382020-03-05 15:18:14 -0800103 /* z_add_timeout() always adds one to the incoming tick count
104 * to round up to the next tick (by convention it waits for
105 * "at least as long as the specified timeout"), but the
106 * period interval is always guaranteed to be reset from
107 * within the timer ISR, so no round up is desired. Subtract
108 * one.
109 *
110 * Note that the duration (!) value gets the same treatment
111 * for backwards compatibility. This is unfortunate
112 * (i.e. k_timer_start() doesn't treat its initial sleep
113 * argument the same way k_sleep() does), but historical. The
114 * timer_api test relies on this behavior.
115 */
Eric Johnsonb4aeef42021-03-05 08:31:50 -0600116 if (!K_TIMEOUT_EQ(period, K_FOREVER) && period.ticks != 0 &&
117 Z_TICK_ABS(period.ticks) < 0) {
Andy Ross3e729b22020-04-23 10:05:31 -0700118 period.ticks = MAX(period.ticks - 1, 1);
119 }
Andy Ross4c7b77a2020-03-09 09:35:35 -0700120 if (Z_TICK_ABS(duration.ticks) < 0) {
121 duration.ticks = MAX(duration.ticks - 1, 0);
122 }
Benjamin Walsh6ca6c282016-12-09 13:39:00 -0500123
Patrik Flykt4344e272019-03-08 14:19:05 -0700124 (void)z_abort_timeout(&timer->timeout);
Andy Ross78327382020-03-05 15:18:14 -0800125 timer->period = period;
Patrik Flykt24d71432019-03-26 19:57:45 -0600126 timer->status = 0U;
Andy Ross78327382020-03-05 15:18:14 -0800127
Patrik Flykt4344e272019-03-08 14:19:05 -0700128 z_add_timeout(&timer->timeout, z_timer_expiration_handler,
Andy Ross78327382020-03-05 15:18:14 -0800129 duration);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400130}
131
Andrew Boiea354d492017-09-29 16:22:28 -0700132#ifdef CONFIG_USERSPACE
Andy Ross643701a2019-08-13 12:58:38 -0700133static inline void z_vrfy_k_timer_start(struct k_timer *timer,
Andy Ross78327382020-03-05 15:18:14 -0800134 k_timeout_t duration,
135 k_timeout_t period)
Andrew Boiea354d492017-09-29 16:22:28 -0700136{
Andrew Boie8345e5e2018-05-04 15:57:57 -0700137 Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
Andy Ross65649742019-08-06 13:34:31 -0700138 z_impl_k_timer_start(timer, duration, period);
Andrew Boiea354d492017-09-29 16:22:28 -0700139}
Andy Ross65649742019-08-06 13:34:31 -0700140#include <syscalls/k_timer_start_mrsh.c>
Andrew Boiea354d492017-09-29 16:22:28 -0700141#endif
142
Patrik Flykt4344e272019-03-08 14:19:05 -0700143void z_impl_k_timer_stop(struct k_timer *timer)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400144{
Torbjörn Leksell3a66d6c2021-03-26 14:09:10 +0100145 SYS_PORT_TRACING_OBJ_FUNC(k_timer, stop, timer);
146
Patrik Flykt4344e272019-03-08 14:19:05 -0700147 int inactive = z_abort_timeout(&timer->timeout) != 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400148
Benjamin Walshd211a522016-12-06 11:44:01 -0500149 if (inactive) {
Allan Stephens45bfa372016-10-12 12:39:42 -0500150 return;
151 }
152
Flavio Ceolin76b35182018-12-16 12:48:29 -0800153 if (timer->stop_fn != NULL) {
Allan Stephens45bfa372016-10-12 12:39:42 -0500154 timer->stop_fn(timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400155 }
156
Krzysztof Chruscinskidd0715c2021-04-14 13:36:58 +0200157 if (IS_ENABLED(CONFIG_MULTITHREADING)) {
158 struct k_thread *pending_thread = z_unpend1_no_timeout(&timer->wait_q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400159
Krzysztof Chruscinskidd0715c2021-04-14 13:36:58 +0200160 if (pending_thread != NULL) {
161 z_ready_thread(pending_thread);
162 z_reschedule_unlocked();
163 }
Allan Stephens45bfa372016-10-12 12:39:42 -0500164 }
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400165}
166
Andrew Boiea354d492017-09-29 16:22:28 -0700167#ifdef CONFIG_USERSPACE
Andy Ross65649742019-08-06 13:34:31 -0700168static inline void z_vrfy_k_timer_stop(struct k_timer *timer)
169{
170 Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
171 z_impl_k_timer_stop(timer);
172}
173#include <syscalls/k_timer_stop_mrsh.c>
Andrew Boiea354d492017-09-29 16:22:28 -0700174#endif
175
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500176uint32_t z_impl_k_timer_status_get(struct k_timer *timer)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400177{
Andy Rossb29fb222019-02-05 16:19:30 -0800178 k_spinlock_key_t key = k_spin_lock(&lock);
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500179 uint32_t result = timer->status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400180
Patrik Flykt24d71432019-03-26 19:57:45 -0600181 timer->status = 0U;
Andy Rossb29fb222019-02-05 16:19:30 -0800182 k_spin_unlock(&lock, key);
Allan Stephens45bfa372016-10-12 12:39:42 -0500183
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400184 return result;
185}
186
Andrew Boiea354d492017-09-29 16:22:28 -0700187#ifdef CONFIG_USERSPACE
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500188static inline uint32_t z_vrfy_k_timer_status_get(struct k_timer *timer)
Andy Ross65649742019-08-06 13:34:31 -0700189{
190 Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
191 return z_impl_k_timer_status_get(timer);
192}
193#include <syscalls/k_timer_status_get_mrsh.c>
Andrew Boiea354d492017-09-29 16:22:28 -0700194#endif
195
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500196uint32_t z_impl_k_timer_status_sync(struct k_timer *timer)
Allan Stephens45bfa372016-10-12 12:39:42 -0500197{
Andrew Boie4f77c2a2019-11-07 12:43:29 -0800198 __ASSERT(!arch_is_in_isr(), "");
Torbjörn Leksell3a66d6c2021-03-26 14:09:10 +0100199 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_timer, status_sync, timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500200
Krzysztof Chruscinskidd0715c2021-04-14 13:36:58 +0200201 if (!IS_ENABLED(CONFIG_MULTITHREADING)) {
202 uint32_t result;
203
204 do {
205 k_spinlock_key_t key = k_spin_lock(&lock);
206
207 if (!z_is_inactive_timeout(&timer->timeout)) {
208 result = *(volatile uint32_t *)&timer->status;
209 timer->status = 0U;
210 k_spin_unlock(&lock, key);
211 if (result > 0) {
212 break;
213 }
214 } else {
215 result = timer->status;
216 k_spin_unlock(&lock, key);
217 break;
218 }
219 } while (true);
220
221 return result;
222 }
223
Andy Rossb29fb222019-02-05 16:19:30 -0800224 k_spinlock_key_t key = k_spin_lock(&lock);
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500225 uint32_t result = timer->status;
Allan Stephens45bfa372016-10-12 12:39:42 -0500226
Patrik Flykt24d71432019-03-26 19:57:45 -0600227 if (result == 0U) {
Patrik Flykt4344e272019-03-08 14:19:05 -0700228 if (!z_is_inactive_timeout(&timer->timeout)) {
Torbjörn Leksell3a66d6c2021-03-26 14:09:10 +0100229 SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_timer, status_sync, timer, K_FOREVER);
230
Allan Stephens45bfa372016-10-12 12:39:42 -0500231 /* wait for timer to expire or stop */
Patrik Flykt4344e272019-03-08 14:19:05 -0700232 (void)z_pend_curr(&lock, key, &timer->wait_q, K_FOREVER);
Allan Stephens45bfa372016-10-12 12:39:42 -0500233
234 /* get updated timer status */
Andy Rossb29fb222019-02-05 16:19:30 -0800235 key = k_spin_lock(&lock);
Allan Stephens45bfa372016-10-12 12:39:42 -0500236 result = timer->status;
237 } else {
238 /* timer is already stopped */
239 }
240 } else {
241 /* timer has already expired at least once */
242 }
243
Patrik Flykt24d71432019-03-26 19:57:45 -0600244 timer->status = 0U;
Andy Rossb29fb222019-02-05 16:19:30 -0800245 k_spin_unlock(&lock, key);
Allan Stephens45bfa372016-10-12 12:39:42 -0500246
Torbjörn Leksell3a66d6c2021-03-26 14:09:10 +0100247 /**
248 * @note New tracing hook
249 */
250 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_timer, status_sync, timer, result);
251
Allan Stephens45bfa372016-10-12 12:39:42 -0500252 return result;
253}
254
Andrew Boiea354d492017-09-29 16:22:28 -0700255#ifdef CONFIG_USERSPACE
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500256static inline uint32_t z_vrfy_k_timer_status_sync(struct k_timer *timer)
Andrew Boie225e4c02017-10-12 09:54:26 -0700257{
Andrew Boie8345e5e2018-05-04 15:57:57 -0700258 Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
Andy Ross65649742019-08-06 13:34:31 -0700259 return z_impl_k_timer_status_sync(timer);
Andrew Boie225e4c02017-10-12 09:54:26 -0700260}
Andy Ross65649742019-08-06 13:34:31 -0700261#include <syscalls/k_timer_status_sync_mrsh.c>
262
Peter Bigot0ab314f2020-11-16 15:28:59 -0600263static inline k_ticks_t z_vrfy_k_timer_remaining_ticks(
264 const struct k_timer *timer)
Andy Ross65649742019-08-06 13:34:31 -0700265{
266 Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
Andy Ross5a5d3da2020-03-09 13:59:15 -0700267 return z_impl_k_timer_remaining_ticks(timer);
Andy Ross65649742019-08-06 13:34:31 -0700268}
Andy Ross5a5d3da2020-03-09 13:59:15 -0700269#include <syscalls/k_timer_remaining_ticks_mrsh.c>
270
Peter Bigot0ab314f2020-11-16 15:28:59 -0600271static inline k_ticks_t z_vrfy_k_timer_expires_ticks(
272 const struct k_timer *timer)
Andy Ross5a5d3da2020-03-09 13:59:15 -0700273{
274 Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
275 return z_impl_k_timer_expires_ticks(timer);
276}
277#include <syscalls/k_timer_expires_ticks_mrsh.c>
Andy Ross65649742019-08-06 13:34:31 -0700278
Peter A. Bigotf1b86ca2020-09-18 16:24:57 -0500279static inline void *z_vrfy_k_timer_user_data_get(const struct k_timer *timer)
Andy Ross65649742019-08-06 13:34:31 -0700280{
281 Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
282 return z_impl_k_timer_user_data_get(timer);
283}
284#include <syscalls/k_timer_user_data_get_mrsh.c>
285
Andy Ross643701a2019-08-13 12:58:38 -0700286static inline void z_vrfy_k_timer_user_data_set(struct k_timer *timer,
287 void *user_data)
Andy Ross65649742019-08-06 13:34:31 -0700288{
289 Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
290 z_impl_k_timer_user_data_set(timer, user_data);
291}
292#include <syscalls/k_timer_user_data_set_mrsh.c>
293
Andrew Boie225e4c02017-10-12 09:54:26 -0700294#endif