blob: d338c172220fdcf14cda38841757a94b32cfdef2 [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 Nashif569f0b42016-12-17 13:18:45 -05008#include <debug/object_tracing_common.h>
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
Anas Nashif2f203c22016-12-18 06:57:45 -050018#ifdef CONFIG_OBJECT_TRACING
Allan Stephense7d2cc22016-10-19 16:10:46 -050019
Maciek Borzecki059544d2017-05-18 12:16:45 +020020struct k_timer *_trace_list_k_timer;
21
Allan Stephense7d2cc22016-10-19 16:10:46 -050022/*
23 * Complete initialization of statically defined timers.
24 */
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +020025static int init_timer_module(const struct device *dev)
Allan Stephense7d2cc22016-10-19 16:10:46 -050026{
27 ARG_UNUSED(dev);
28
Nicolas Pitreaa9228852019-06-03 13:01:43 -040029 Z_STRUCT_SECTION_FOREACH(k_timer, timer) {
Allan Stephense7d2cc22016-10-19 16:10:46 -050030 SYS_TRACING_OBJ_INIT(k_timer, timer);
31 }
32 return 0;
33}
34
Andrew Boie0b474ee2016-11-08 11:06:55 -080035SYS_INIT(init_timer_module, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_OBJECTS);
Allan Stephense7d2cc22016-10-19 16:10:46 -050036
Anas Nashif2f203c22016-12-18 06:57:45 -050037#endif /* CONFIG_OBJECT_TRACING */
Allan Stephense7d2cc22016-10-19 16:10:46 -050038
Benjamin Walsh456c6da2016-09-02 18:55:39 -040039/**
Allan Stephens45bfa372016-10-12 12:39:42 -050040 * @brief Handle expiration of a kernel timer object.
Benjamin Walsh456c6da2016-09-02 18:55:39 -040041 *
Allan Stephens45bfa372016-10-12 12:39:42 -050042 * @param t Timeout used by the timer.
Benjamin Walsh456c6da2016-09-02 18:55:39 -040043 *
44 * @return N/A
45 */
Patrik Flykt4344e272019-03-08 14:19:05 -070046void z_timer_expiration_handler(struct _timeout *t)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040047{
Benjamin Walsh456c6da2016-09-02 18:55:39 -040048 struct k_timer *timer = CONTAINER_OF(t, struct k_timer, timeout);
Benjamin Walshb889fa82016-12-07 22:39:31 -050049 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040050
Allan Stephens6c98c4d2016-10-17 14:34:53 -050051 /*
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 Ross78327382020-03-05 15:18:14 -080055 if (!K_TIMEOUT_EQ(timer->period, K_NO_WAIT) &&
56 !K_TIMEOUT_EQ(timer->period, K_FOREVER)) {
Patrik Flykt4344e272019-03-08 14:19:05 -070057 z_add_timeout(&timer->timeout, z_timer_expiration_handler,
Andy Rossfe82f1c2018-09-26 15:14:21 -070058 timer->period);
Benjamin Walsh456c6da2016-09-02 18:55:39 -040059 }
60
Allan Stephens45bfa372016-10-12 12:39:42 -050061 /* update timer's status */
Patrik Flykt24d71432019-03-26 19:57:45 -060062 timer->status += 1U;
Allan Stephens45bfa372016-10-12 12:39:42 -050063
64 /* invoke timer expiry function */
Flavio Ceolin76b35182018-12-16 12:48:29 -080065 if (timer->expiry_fn != NULL) {
Allan Stephens45bfa372016-10-12 12:39:42 -050066 timer->expiry_fn(timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -040067 }
Benjamin Walshb889fa82016-12-07 22:39:31 -050068
Patrik Flykt4344e272019-03-08 14:19:05 -070069 thread = z_waitq_head(&timer->wait_q);
Benjamin Walshb889fa82016-12-07 22:39:31 -050070
Flavio Ceolin4218d5f2018-09-17 09:39:51 -070071 if (thread == NULL) {
Benjamin Walshb889fa82016-12-07 22:39:31 -050072 return;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040073 }
Allan Stephens45bfa372016-10-12 12:39:42 -050074
Benjamin Walshb889fa82016-12-07 22:39:31 -050075 /*
Andy Ross22642cf2018-04-02 18:24:58 -070076 * Interrupts _DO NOT_ have to be locked in this specific
77 * instance of thread unpending because a) this is the only
78 * place a thread can be taken off this pend queue, and b) the
79 * only place a thread can be put on the pend queue is at
80 * thread level, which of course cannot interrupt the current
81 * context.
Benjamin Walshb889fa82016-12-07 22:39:31 -050082 */
Patrik Flykt4344e272019-03-08 14:19:05 -070083 z_unpend_thread_no_timeout(thread);
Benjamin Walshb889fa82016-12-07 22:39:31 -050084
Patrik Flykt4344e272019-03-08 14:19:05 -070085 z_ready_thread(thread);
Benjamin Walshb889fa82016-12-07 22:39:31 -050086
Andrew Boie4f77c2a2019-11-07 12:43:29 -080087 arch_thread_return_value_set(thread, 0);
Benjamin Walsh456c6da2016-09-02 18:55:39 -040088}
89
90
Allan Stephens45bfa372016-10-12 12:39:42 -050091void k_timer_init(struct k_timer *timer,
Flavio Ceolin118715c2018-11-16 19:52:37 -080092 k_timer_expiry_t expiry_fn,
93 k_timer_stop_t stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040094{
Allan Stephens45bfa372016-10-12 12:39:42 -050095 timer->expiry_fn = expiry_fn;
96 timer->stop_fn = stop_fn;
Patrik Flykt24d71432019-03-26 19:57:45 -060097 timer->status = 0U;
Allan Stephens45bfa372016-10-12 12:39:42 -050098
Patrik Flykt4344e272019-03-08 14:19:05 -070099 z_waitq_init(&timer->wait_q);
Peter A. Bigot5639ea02019-09-27 09:20:26 -0500100 z_init_timeout(&timer->timeout);
Allan Stephense7d2cc22016-10-19 16:10:46 -0500101 SYS_TRACING_OBJ_INIT(k_timer, timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500102
Maciek Borzecki4fef7602017-05-18 08:49:50 +0200103 timer->user_data = NULL;
Andrew Boie945af952017-08-22 13:15:23 -0700104
Patrik Flykt4344e272019-03-08 14:19:05 -0700105 z_object_init(timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400106}
107
108
Andy Ross78327382020-03-05 15:18:14 -0800109void z_impl_k_timer_start(struct k_timer *timer, k_timeout_t duration,
110 k_timeout_t period)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400111{
Andy Rossa343cf92020-05-29 07:36:39 -0700112 if (K_TIMEOUT_EQ(duration, K_FOREVER)) {
113 return;
114 }
115
Andy Ross78327382020-03-05 15:18:14 -0800116#ifdef CONFIG_LEGACY_TIMEOUT_API
117 duration = k_ms_to_ticks_ceil32(duration);
118 period = k_ms_to_ticks_ceil32(period);
119#else
120 /* z_add_timeout() always adds one to the incoming tick count
121 * to round up to the next tick (by convention it waits for
122 * "at least as long as the specified timeout"), but the
123 * period interval is always guaranteed to be reset from
124 * within the timer ISR, so no round up is desired. Subtract
125 * one.
126 *
127 * Note that the duration (!) value gets the same treatment
128 * for backwards compatibility. This is unfortunate
129 * (i.e. k_timer_start() doesn't treat its initial sleep
130 * argument the same way k_sleep() does), but historical. The
131 * timer_api test relies on this behavior.
132 */
Andy Ross3e729b22020-04-23 10:05:31 -0700133 if (period.ticks != 0 && Z_TICK_ABS(period.ticks) < 0) {
134 period.ticks = MAX(period.ticks - 1, 1);
135 }
Andy Ross4c7b77a2020-03-09 09:35:35 -0700136 if (Z_TICK_ABS(duration.ticks) < 0) {
137 duration.ticks = MAX(duration.ticks - 1, 0);
138 }
Andy Ross78327382020-03-05 15:18:14 -0800139#endif
Benjamin Walsh6ca6c282016-12-09 13:39:00 -0500140
Patrik Flykt4344e272019-03-08 14:19:05 -0700141 (void)z_abort_timeout(&timer->timeout);
Andy Ross78327382020-03-05 15:18:14 -0800142 timer->period = period;
Patrik Flykt24d71432019-03-26 19:57:45 -0600143 timer->status = 0U;
Andy Ross78327382020-03-05 15:18:14 -0800144
Patrik Flykt4344e272019-03-08 14:19:05 -0700145 z_add_timeout(&timer->timeout, z_timer_expiration_handler,
Andy Ross78327382020-03-05 15:18:14 -0800146 duration);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400147}
148
Andrew Boiea354d492017-09-29 16:22:28 -0700149#ifdef CONFIG_USERSPACE
Andy Ross643701a2019-08-13 12:58:38 -0700150static inline void z_vrfy_k_timer_start(struct k_timer *timer,
Andy Ross78327382020-03-05 15:18:14 -0800151 k_timeout_t duration,
152 k_timeout_t period)
Andrew Boiea354d492017-09-29 16:22:28 -0700153{
Andrew Boie8345e5e2018-05-04 15:57:57 -0700154 Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
Andy Ross65649742019-08-06 13:34:31 -0700155 z_impl_k_timer_start(timer, duration, period);
Andrew Boiea354d492017-09-29 16:22:28 -0700156}
Andy Ross65649742019-08-06 13:34:31 -0700157#include <syscalls/k_timer_start_mrsh.c>
Andrew Boiea354d492017-09-29 16:22:28 -0700158#endif
159
Patrik Flykt4344e272019-03-08 14:19:05 -0700160void z_impl_k_timer_stop(struct k_timer *timer)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400161{
Patrik Flykt4344e272019-03-08 14:19:05 -0700162 int inactive = z_abort_timeout(&timer->timeout) != 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400163
Benjamin Walshd211a522016-12-06 11:44:01 -0500164 if (inactive) {
Allan Stephens45bfa372016-10-12 12:39:42 -0500165 return;
166 }
167
Flavio Ceolin76b35182018-12-16 12:48:29 -0800168 if (timer->stop_fn != NULL) {
Allan Stephens45bfa372016-10-12 12:39:42 -0500169 timer->stop_fn(timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400170 }
171
Patrik Flykt4344e272019-03-08 14:19:05 -0700172 struct k_thread *pending_thread = z_unpend1_no_timeout(&timer->wait_q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400173
Flavio Ceolin4218d5f2018-09-17 09:39:51 -0700174 if (pending_thread != NULL) {
Patrik Flykt4344e272019-03-08 14:19:05 -0700175 z_ready_thread(pending_thread);
176 z_reschedule_unlocked();
Allan Stephens45bfa372016-10-12 12:39:42 -0500177 }
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400178}
179
Andrew Boiea354d492017-09-29 16:22:28 -0700180#ifdef CONFIG_USERSPACE
Andy Ross65649742019-08-06 13:34:31 -0700181static inline void z_vrfy_k_timer_stop(struct k_timer *timer)
182{
183 Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
184 z_impl_k_timer_stop(timer);
185}
186#include <syscalls/k_timer_stop_mrsh.c>
Andrew Boiea354d492017-09-29 16:22:28 -0700187#endif
188
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500189uint32_t z_impl_k_timer_status_get(struct k_timer *timer)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400190{
Andy Rossb29fb222019-02-05 16:19:30 -0800191 k_spinlock_key_t key = k_spin_lock(&lock);
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500192 uint32_t result = timer->status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400193
Patrik Flykt24d71432019-03-26 19:57:45 -0600194 timer->status = 0U;
Andy Rossb29fb222019-02-05 16:19:30 -0800195 k_spin_unlock(&lock, key);
Allan Stephens45bfa372016-10-12 12:39:42 -0500196
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400197 return result;
198}
199
Andrew Boiea354d492017-09-29 16:22:28 -0700200#ifdef CONFIG_USERSPACE
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500201static inline uint32_t z_vrfy_k_timer_status_get(struct k_timer *timer)
Andy Ross65649742019-08-06 13:34:31 -0700202{
203 Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
204 return z_impl_k_timer_status_get(timer);
205}
206#include <syscalls/k_timer_status_get_mrsh.c>
Andrew Boiea354d492017-09-29 16:22:28 -0700207#endif
208
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500209uint32_t z_impl_k_timer_status_sync(struct k_timer *timer)
Allan Stephens45bfa372016-10-12 12:39:42 -0500210{
Andrew Boie4f77c2a2019-11-07 12:43:29 -0800211 __ASSERT(!arch_is_in_isr(), "");
Allan Stephens45bfa372016-10-12 12:39:42 -0500212
Andy Rossb29fb222019-02-05 16:19:30 -0800213 k_spinlock_key_t key = k_spin_lock(&lock);
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500214 uint32_t result = timer->status;
Allan Stephens45bfa372016-10-12 12:39:42 -0500215
Patrik Flykt24d71432019-03-26 19:57:45 -0600216 if (result == 0U) {
Patrik Flykt4344e272019-03-08 14:19:05 -0700217 if (!z_is_inactive_timeout(&timer->timeout)) {
Allan Stephens45bfa372016-10-12 12:39:42 -0500218 /* wait for timer to expire or stop */
Patrik Flykt4344e272019-03-08 14:19:05 -0700219 (void)z_pend_curr(&lock, key, &timer->wait_q, K_FOREVER);
Allan Stephens45bfa372016-10-12 12:39:42 -0500220
221 /* get updated timer status */
Andy Rossb29fb222019-02-05 16:19:30 -0800222 key = k_spin_lock(&lock);
Allan Stephens45bfa372016-10-12 12:39:42 -0500223 result = timer->status;
224 } else {
225 /* timer is already stopped */
226 }
227 } else {
228 /* timer has already expired at least once */
229 }
230
Patrik Flykt24d71432019-03-26 19:57:45 -0600231 timer->status = 0U;
Andy Rossb29fb222019-02-05 16:19:30 -0800232 k_spin_unlock(&lock, key);
Allan Stephens45bfa372016-10-12 12:39:42 -0500233
234 return result;
235}
236
Andrew Boiea354d492017-09-29 16:22:28 -0700237#ifdef CONFIG_USERSPACE
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500238static inline uint32_t z_vrfy_k_timer_status_sync(struct k_timer *timer)
Andrew Boie225e4c02017-10-12 09:54:26 -0700239{
Andrew Boie8345e5e2018-05-04 15:57:57 -0700240 Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
Andy Ross65649742019-08-06 13:34:31 -0700241 return z_impl_k_timer_status_sync(timer);
Andrew Boie225e4c02017-10-12 09:54:26 -0700242}
Andy Ross65649742019-08-06 13:34:31 -0700243#include <syscalls/k_timer_status_sync_mrsh.c>
244
Andy Ross5a5d3da2020-03-09 13:59:15 -0700245static inline k_ticks_t z_vrfy_k_timer_remaining_ticks(struct k_timer *timer)
Andy Ross65649742019-08-06 13:34:31 -0700246{
247 Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
Andy Ross5a5d3da2020-03-09 13:59:15 -0700248 return z_impl_k_timer_remaining_ticks(timer);
Andy Ross65649742019-08-06 13:34:31 -0700249}
Andy Ross5a5d3da2020-03-09 13:59:15 -0700250#include <syscalls/k_timer_remaining_ticks_mrsh.c>
251
252static inline k_ticks_t z_vrfy_k_timer_expires_ticks(struct k_timer *timer)
253{
254 Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
255 return z_impl_k_timer_expires_ticks(timer);
256}
257#include <syscalls/k_timer_expires_ticks_mrsh.c>
Andy Ross65649742019-08-06 13:34:31 -0700258
Peter A. Bigotf1b86ca2020-09-18 16:24:57 -0500259static inline void *z_vrfy_k_timer_user_data_get(const struct k_timer *timer)
Andy Ross65649742019-08-06 13:34:31 -0700260{
261 Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
262 return z_impl_k_timer_user_data_get(timer);
263}
264#include <syscalls/k_timer_user_data_get_mrsh.c>
265
Andy Ross643701a2019-08-13 12:58:38 -0700266static inline void z_vrfy_k_timer_user_data_set(struct k_timer *timer,
267 void *user_data)
Andy Ross65649742019-08-06 13:34:31 -0700268{
269 Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
270 z_impl_k_timer_user_data_set(timer, user_data);
271}
272#include <syscalls/k_timer_user_data_set_mrsh.c>
273
Andrew Boie225e4c02017-10-12 09:54:26 -0700274#endif