kernel: Refactor remaining time evaluation for timeouts

Factor out the code for evaluating the remaining time for _timeout
structs so that it can also be used for other objects besides k_timer
structs (like k_delayed_work, coming in a subsequent patch).

Change-Id: I243a7b29fb2831f06e95086a31f0d3a6c37dad67
Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
This commit is contained in:
Johan Hedberg 2016-12-09 10:39:49 +02:00 committed by Benjamin Walsh
commit f99ad3f0e2
2 changed files with 9 additions and 5 deletions

View file

@ -654,6 +654,8 @@ struct _timeout {
_timeout_func_t func;
};
extern int32_t _timeout_remaining_get(struct _timeout *timeout);
/**
* INTERNAL_HIDDEN @endcond
*/
@ -852,7 +854,10 @@ extern uint32_t k_timer_status_sync(struct k_timer *timer);
*
* @return Remaining time (in milliseconds).
*/
extern int32_t k_timer_remaining_get(struct k_timer *timer);
static inline int32_t k_timer_remaining_get(struct k_timer *timer)
{
return _timeout_remaining_get(&timer->timeout);
}
/**
* @} end defgroup timer_apis

View file

@ -196,13 +196,12 @@ uint32_t k_timer_status_sync(struct k_timer *timer)
return result;
}
int32_t k_timer_remaining_get(struct k_timer *timer)
int32_t _timeout_remaining_get(struct _timeout *timeout)
{
unsigned int key = irq_lock();
int32_t remaining_ticks;
if (timer->timeout.delta_ticks_from_prev == -1) {
if (timeout->delta_ticks_from_prev == -1) {
remaining_ticks = 0;
} else {
/*
@ -213,7 +212,7 @@ int32_t k_timer_remaining_get(struct k_timer *timer)
(struct _timeout *)sys_dlist_peek_head(&_timeout_q);
remaining_ticks = t->delta_ticks_from_prev;
while (t != &timer->timeout) {
while (t != timeout) {
t = (struct _timeout *)sys_dlist_peek_next(&_timeout_q,
&t->node);
remaining_ticks += t->delta_ticks_from_prev;