kernel: timeout: detect inactive timeouts using dnode linked state

Whether a timeout is linked into the timeout queue can be determined
from the corresponding sys_dnode_t linked state.  This removes the need
to use a special flag value in dticks to determine that the timeout is
inactive.

Update _abort_timeout to return an error code, rather than the flag
value, when the timeout to be aborted was not active.

Remove the _INACTIVE flag value, and replace its external uses with an
internal API function that checks whether a timeout is inactive.

Signed-off-by: Peter A. Bigot <pab@pabigot.com>
This commit is contained in:
Peter A. Bigot 2019-01-02 08:29:43 -06:00 committed by Carles Cufí
parent 4863aa809c
commit b4ece0ad44
6 changed files with 15 additions and 17 deletions

View file

@ -873,9 +873,6 @@ __syscall void k_thread_start(k_tid_t thread);
/* timeout has timed out and is not on _timeout_q anymore */ /* timeout has timed out and is not on _timeout_q anymore */
#define _EXPIRED (-2) #define _EXPIRED (-2)
/* timeout is not in use */
#define _INACTIVE (-1)
struct _static_thread_data { struct _static_thread_data {
struct k_thread *init_thread; struct k_thread *init_thread;
k_thread_stack_t *init_stack; k_thread_stack_t *init_stack;
@ -1334,7 +1331,7 @@ struct k_timer {
#define _K_TIMER_INITIALIZER(obj, expiry, stop) \ #define _K_TIMER_INITIALIZER(obj, expiry, stop) \
{ \ { \
.timeout.dticks = _INACTIVE, \ .timeout.dticks = 0, \
.timeout.fn = _timer_expiration_handler, \ .timeout.fn = _timer_expiration_handler, \
.wait_q = _WAIT_Q_INIT(&obj.wait_q), \ .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
.expiry_fn = expiry, \ .expiry_fn = expiry, \

View file

@ -8,6 +8,7 @@
#define ZEPHYR_KERNEL_INCLUDE_KSCHED_H_ #define ZEPHYR_KERNEL_INCLUDE_KSCHED_H_
#include <kernel_structs.h> #include <kernel_structs.h>
#include <timeout_q.h>
#include <tracing.h> #include <tracing.h>
#include <stdbool.h> #include <stdbool.h>
@ -83,11 +84,7 @@ static inline int _is_thread_prevented_from_running(struct k_thread *thread)
static inline bool _is_thread_timeout_active(struct k_thread *thread) static inline bool _is_thread_timeout_active(struct k_thread *thread)
{ {
#ifdef CONFIG_SYS_CLOCK_EXISTS return !_is_inactive_timeout(&thread->base.timeout);
return thread->base.timeout.dticks != _INACTIVE;
#else
return false;
#endif
} }
static inline bool _is_thread_ready(struct k_thread *thread) static inline bool _is_thread_ready(struct k_thread *thread)

View file

@ -23,13 +23,17 @@ extern "C" {
static inline void _init_timeout(struct _timeout *t, _timeout_func_t fn) static inline void _init_timeout(struct _timeout *t, _timeout_func_t fn)
{ {
sys_dnode_init(&t->node); sys_dnode_init(&t->node);
t->dticks = _INACTIVE;
} }
void _add_timeout(struct _timeout *to, _timeout_func_t fn, s32_t ticks); void _add_timeout(struct _timeout *to, _timeout_func_t fn, s32_t ticks);
int _abort_timeout(struct _timeout *to); int _abort_timeout(struct _timeout *to);
static inline bool _is_inactive_timeout(struct _timeout *t)
{
return !sys_dnode_is_linked(&t->node);
}
static inline void _init_thread_timeout(struct _thread_base *thread_base) static inline void _init_thread_timeout(struct _thread_base *thread_base)
{ {
_init_timeout(&thread_base->timeout, NULL); _init_timeout(&thread_base->timeout, NULL);
@ -59,6 +63,7 @@ s32_t z_timeout_remaining(struct _timeout *timeout);
#define _init_thread_timeout(t) do {} while (0) #define _init_thread_timeout(t) do {} while (0)
#define _add_thread_timeout(th, to) do {} while (0 && (void *)to && (void *)th) #define _add_thread_timeout(th, to) do {} while (0 && (void *)to && (void *)th)
#define _abort_thread_timeout(t) (0) #define _abort_thread_timeout(t) (0)
#define _is_inactive_timeout(t) 0
#define _get_next_timeout_expiry() (K_FOREVER) #define _get_next_timeout_expiry() (K_FOREVER)
#define z_set_timeout_expiry(t, i) do {} while (0) #define z_set_timeout_expiry(t, i) do {} while (0)

View file

@ -896,7 +896,7 @@ void _impl_k_wakeup(k_tid_t thread)
return; return;
} }
if (_abort_thread_timeout(thread) == _INACTIVE) { if (_abort_thread_timeout(thread) < 0) {
irq_unlock(key); irq_unlock(key);
return; return;
} }

View file

@ -51,7 +51,6 @@ static void remove_timeout(struct _timeout *t)
} }
sys_dlist_remove(&t->node); sys_dlist_remove(&t->node);
t->dticks = _INACTIVE;
} }
static s32_t elapsed(void) static s32_t elapsed(void)
@ -75,7 +74,7 @@ static s32_t next_timeout(void)
void _add_timeout(struct _timeout *to, _timeout_func_t fn, s32_t ticks) void _add_timeout(struct _timeout *to, _timeout_func_t fn, s32_t ticks)
{ {
__ASSERT(to->dticks < 0, ""); __ASSERT(!sys_dnode_is_linked(&to->node), "");
to->fn = fn; to->fn = fn;
ticks = max(1, ticks); ticks = max(1, ticks);
@ -107,7 +106,7 @@ void _add_timeout(struct _timeout *to, _timeout_func_t fn, s32_t ticks)
int _abort_timeout(struct _timeout *to) int _abort_timeout(struct _timeout *to)
{ {
int ret = _INACTIVE; int ret = -EINVAL;
LOCKED(&timeout_lock) { LOCKED(&timeout_lock) {
if (sys_dnode_is_linked(&to->node)) { if (sys_dnode_is_linked(&to->node)) {
@ -123,7 +122,7 @@ s32_t z_timeout_remaining(struct _timeout *timeout)
{ {
s32_t ticks = 0; s32_t ticks = 0;
if (timeout->dticks == _INACTIVE) { if (_is_inactive_timeout(timeout)) {
return 0; return 0;
} }

View file

@ -150,7 +150,7 @@ Z_SYSCALL_HANDLER(k_timer_start, timer, duration_p, period_p)
void _impl_k_timer_stop(struct k_timer *timer) void _impl_k_timer_stop(struct k_timer *timer)
{ {
unsigned int key = irq_lock(); unsigned int key = irq_lock();
bool inactive = (_abort_timeout(&timer->timeout) == _INACTIVE); int inactive = _abort_timeout(&timer->timeout) != 0;
irq_unlock(key); irq_unlock(key);
@ -203,7 +203,7 @@ u32_t _impl_k_timer_status_sync(struct k_timer *timer)
u32_t result = timer->status; u32_t result = timer->status;
if (result == 0) { if (result == 0) {
if (timer->timeout.dticks != _INACTIVE) { if (!_is_inactive_timeout(&timer->timeout)) {
/* wait for timer to expire or stop */ /* wait for timer to expire or stop */
(void)_pend_current_thread(key, &timer->wait_q, K_FOREVER); (void)_pend_current_thread(key, &timer->wait_q, K_FOREVER);