From 52e444bc05db2475223beaf1fb75ce660211593f Mon Sep 17 00:00:00 2001 From: Andy Ross Date: Fri, 28 Sep 2018 09:06:37 -0700 Subject: [PATCH] kernel: Move timeout_remaining API _timeout_remaining_get() was a function on a struct _timeout, doing iteration on the timeout list, but it was defined in timer.c (the higher level abstraction). Move it to where it belongs. Also have it return ticks instead of ms to conform to scheme in the rest of the timeout API. And rename it to a more standard zephyr name. Signed-off-by: Andy Ross --- include/kernel.h | 6 ++++-- include/sys_clock.h | 2 -- kernel/include/timeout_q.h | 4 ++++ kernel/sys_clock.c | 27 +++++++++++++++++++++++++++ kernel/timer.c | 27 --------------------------- 5 files changed, 35 insertions(+), 31 deletions(-) diff --git a/include/kernel.h b/include/kernel.h index 6f497ae46de..ba6c20f935b 100644 --- a/include/kernel.h +++ b/include/kernel.h @@ -1483,6 +1483,8 @@ __syscall u32_t k_timer_status_get(struct k_timer *timer); */ __syscall u32_t k_timer_status_sync(struct k_timer *timer); +extern s32_t z_timeout_remaining(struct _timeout *timeout); + /** * @brief Get time remaining before a timer next expires. * @@ -1497,7 +1499,7 @@ __syscall s32_t k_timer_remaining_get(struct k_timer *timer); static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer) { - return _timeout_remaining_get(&timer->timeout); + return __ticks_to_ms(z_timeout_remaining(&timer->timeout)); } /** @@ -2730,7 +2732,7 @@ static inline int k_delayed_work_submit(struct k_delayed_work *work, */ static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work) { - return _timeout_remaining_get(&work->timeout); + return __ticks_to_ms(z_timeout_remaining(&work->timeout)); } /** @} */ diff --git a/include/sys_clock.h b/include/sys_clock.h index b9a5049362a..869d1e95cfb 100644 --- a/include/sys_clock.h +++ b/include/sys_clock.h @@ -220,8 +220,6 @@ struct _timeout { _timeout_func_t func; }; -extern s32_t _timeout_remaining_get(struct _timeout *timeout); - /* * Number of ticks for x seconds. NOTE: With MSEC() or USEC(), * since it does an integer division, x must be greater or equal to diff --git a/kernel/include/timeout_q.h b/kernel/include/timeout_q.h index 73a31606ea8..82adc3e9019 100644 --- a/kernel/include/timeout_q.h +++ b/kernel/include/timeout_q.h @@ -20,6 +20,8 @@ extern "C" { #ifdef CONFIG_SYS_CLOCK_EXISTS +struct _thread_base; + extern u64_t z_last_tick_announced; void _init_timeout(struct _timeout *t, _timeout_func_t fn); @@ -37,6 +39,8 @@ int _abort_thread_timeout(struct k_thread *thread); s32_t _get_next_timeout_expiry(void); +s32_t z_timeout_remaining(struct _timeout *timeout); + #else /* Stubs when !CONFIG_SYS_CLOCK_EXISTS */ diff --git a/kernel/sys_clock.c b/kernel/sys_clock.c index 5c7e5f3b76d..07d6e582682 100644 --- a/kernel/sys_clock.c +++ b/kernel/sys_clock.c @@ -574,4 +574,31 @@ void _add_thread_timeout(struct k_thread *thread, s32_t timeout_in_ticks) _add_timeout(&thread->base.timeout, NULL, timeout_in_ticks); } +s32_t z_timeout_remaining(struct _timeout *timeout) +{ + unsigned int key = irq_lock(); + s32_t remaining_ticks; + + if (timeout->delta_ticks_from_prev == _INACTIVE) { + remaining_ticks = 0; + } else { + /* + * compute remaining ticks by walking the timeout list + * and summing up the various tick deltas involved + */ + struct _timeout *t = + (struct _timeout *)sys_dlist_peek_head(&_timeout_q); + + remaining_ticks = t->delta_ticks_from_prev; + while (t != timeout) { + t = (struct _timeout *)sys_dlist_peek_next(&_timeout_q, + &t->node); + remaining_ticks += t->delta_ticks_from_prev; + } + } + + irq_unlock(key); + return remaining_ticks; +} + #endif /* CONFIG_SYS_CLOCK_EXISTS */ diff --git a/kernel/timer.c b/kernel/timer.c index 423f55ae372..1a0d7a5e4d0 100644 --- a/kernel/timer.c +++ b/kernel/timer.c @@ -226,33 +226,6 @@ u32_t _impl_k_timer_status_sync(struct k_timer *timer) Z_SYSCALL_HANDLER1_SIMPLE(k_timer_status_sync, K_OBJ_TIMER, struct k_timer *); #endif -s32_t _timeout_remaining_get(struct _timeout *timeout) -{ - unsigned int key = irq_lock(); - s32_t remaining_ticks; - - if (timeout->delta_ticks_from_prev == _INACTIVE) { - remaining_ticks = 0; - } else { - /* - * compute remaining ticks by walking the timeout list - * and summing up the various tick deltas involved - */ - struct _timeout *t = - (struct _timeout *)sys_dlist_peek_head(&_timeout_q); - - remaining_ticks = t->delta_ticks_from_prev; - while (t != timeout) { - t = (struct _timeout *)sys_dlist_peek_next(&_timeout_q, - &t->node); - remaining_ticks += t->delta_ticks_from_prev; - } - } - - irq_unlock(key); - return __ticks_to_ms(remaining_ticks); -} - #ifdef CONFIG_USERSPACE Z_SYSCALL_HANDLER1_SIMPLE(k_timer_remaining_get, K_OBJ_TIMER, struct k_timer *); Z_SYSCALL_HANDLER1_SIMPLE(k_timer_user_data_get, K_OBJ_TIMER, struct k_timer *);