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>
diff --git a/include/kernel.h b/include/kernel.h
index fa18338..2edd113 100644
--- a/include/kernel.h
+++ b/include/kernel.h
@@ -873,9 +873,6 @@
/* timeout has timed out and is not on _timeout_q anymore */
#define _EXPIRED (-2)
-/* timeout is not in use */
-#define _INACTIVE (-1)
-
struct _static_thread_data {
struct k_thread *init_thread;
k_thread_stack_t *init_stack;
@@ -1334,7 +1331,7 @@
#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
{ \
- .timeout.dticks = _INACTIVE, \
+ .timeout.dticks = 0, \
.timeout.fn = _timer_expiration_handler, \
.wait_q = _WAIT_Q_INIT(&obj.wait_q), \
.expiry_fn = expiry, \
diff --git a/kernel/include/ksched.h b/kernel/include/ksched.h
index f44e88d..f44df94 100644
--- a/kernel/include/ksched.h
+++ b/kernel/include/ksched.h
@@ -8,6 +8,7 @@
#define ZEPHYR_KERNEL_INCLUDE_KSCHED_H_
#include <kernel_structs.h>
+#include <timeout_q.h>
#include <tracing.h>
#include <stdbool.h>
@@ -83,11 +84,7 @@
static inline bool _is_thread_timeout_active(struct k_thread *thread)
{
-#ifdef CONFIG_SYS_CLOCK_EXISTS
- return thread->base.timeout.dticks != _INACTIVE;
-#else
- return false;
-#endif
+ return !_is_inactive_timeout(&thread->base.timeout);
}
static inline bool _is_thread_ready(struct k_thread *thread)
diff --git a/kernel/include/timeout_q.h b/kernel/include/timeout_q.h
index 75d3aa3..3438957 100644
--- a/kernel/include/timeout_q.h
+++ b/kernel/include/timeout_q.h
@@ -23,13 +23,17 @@
static inline void _init_timeout(struct _timeout *t, _timeout_func_t fn)
{
sys_dnode_init(&t->node);
- t->dticks = _INACTIVE;
}
void _add_timeout(struct _timeout *to, _timeout_func_t fn, s32_t ticks);
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)
{
_init_timeout(&thread_base->timeout, NULL);
@@ -59,6 +63,7 @@
#define _init_thread_timeout(t) do {} while (0)
#define _add_thread_timeout(th, to) do {} while (0 && (void *)to && (void *)th)
#define _abort_thread_timeout(t) (0)
+#define _is_inactive_timeout(t) 0
#define _get_next_timeout_expiry() (K_FOREVER)
#define z_set_timeout_expiry(t, i) do {} while (0)
diff --git a/kernel/sched.c b/kernel/sched.c
index d704141..e757624 100644
--- a/kernel/sched.c
+++ b/kernel/sched.c
@@ -896,7 +896,7 @@
return;
}
- if (_abort_thread_timeout(thread) == _INACTIVE) {
+ if (_abort_thread_timeout(thread) < 0) {
irq_unlock(key);
return;
}
diff --git a/kernel/timeout.c b/kernel/timeout.c
index 4cd4c05..2c4db74 100644
--- a/kernel/timeout.c
+++ b/kernel/timeout.c
@@ -51,7 +51,6 @@
}
sys_dlist_remove(&t->node);
- t->dticks = _INACTIVE;
}
static s32_t elapsed(void)
@@ -75,7 +74,7 @@
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;
ticks = max(1, ticks);
@@ -107,7 +106,7 @@
int _abort_timeout(struct _timeout *to)
{
- int ret = _INACTIVE;
+ int ret = -EINVAL;
LOCKED(&timeout_lock) {
if (sys_dnode_is_linked(&to->node)) {
@@ -123,7 +122,7 @@
{
s32_t ticks = 0;
- if (timeout->dticks == _INACTIVE) {
+ if (_is_inactive_timeout(timeout)) {
return 0;
}
diff --git a/kernel/timer.c b/kernel/timer.c
index 544c5b1..ce49859 100644
--- a/kernel/timer.c
+++ b/kernel/timer.c
@@ -150,7 +150,7 @@
void _impl_k_timer_stop(struct k_timer *timer)
{
unsigned int key = irq_lock();
- bool inactive = (_abort_timeout(&timer->timeout) == _INACTIVE);
+ int inactive = _abort_timeout(&timer->timeout) != 0;
irq_unlock(key);
@@ -203,7 +203,7 @@
u32_t result = timer->status;
if (result == 0) {
- if (timer->timeout.dticks != _INACTIVE) {
+ if (!_is_inactive_timeout(&timer->timeout)) {
/* wait for timer to expire or stop */
(void)_pend_current_thread(key, &timer->wait_q, K_FOREVER);