Skip to content

Commit b4ece0a

Browse files
pabigotcarlescufi
authored andcommitted
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 <[email protected]>
1 parent 4863aa8 commit b4ece0a

File tree

6 files changed

+15
-17
lines changed

6 files changed

+15
-17
lines changed

include/kernel.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -873,9 +873,6 @@ __syscall void k_thread_start(k_tid_t thread);
873873
/* timeout has timed out and is not on _timeout_q anymore */
874874
#define _EXPIRED (-2)
875875

876-
/* timeout is not in use */
877-
#define _INACTIVE (-1)
878-
879876
struct _static_thread_data {
880877
struct k_thread *init_thread;
881878
k_thread_stack_t *init_stack;
@@ -1334,7 +1331,7 @@ struct k_timer {
13341331

13351332
#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
13361333
{ \
1337-
.timeout.dticks = _INACTIVE, \
1334+
.timeout.dticks = 0, \
13381335
.timeout.fn = _timer_expiration_handler, \
13391336
.wait_q = _WAIT_Q_INIT(&obj.wait_q), \
13401337
.expiry_fn = expiry, \

kernel/include/ksched.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define ZEPHYR_KERNEL_INCLUDE_KSCHED_H_
99

1010
#include <kernel_structs.h>
11+
#include <timeout_q.h>
1112
#include <tracing.h>
1213
#include <stdbool.h>
1314

@@ -83,11 +84,7 @@ static inline int _is_thread_prevented_from_running(struct k_thread *thread)
8384

8485
static inline bool _is_thread_timeout_active(struct k_thread *thread)
8586
{
86-
#ifdef CONFIG_SYS_CLOCK_EXISTS
87-
return thread->base.timeout.dticks != _INACTIVE;
88-
#else
89-
return false;
90-
#endif
87+
return !_is_inactive_timeout(&thread->base.timeout);
9188
}
9289

9390
static inline bool _is_thread_ready(struct k_thread *thread)

kernel/include/timeout_q.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@ extern "C" {
2323
static inline void _init_timeout(struct _timeout *t, _timeout_func_t fn)
2424
{
2525
sys_dnode_init(&t->node);
26-
t->dticks = _INACTIVE;
2726
}
2827

2928
void _add_timeout(struct _timeout *to, _timeout_func_t fn, s32_t ticks);
3029

3130
int _abort_timeout(struct _timeout *to);
3231

32+
static inline bool _is_inactive_timeout(struct _timeout *t)
33+
{
34+
return !sys_dnode_is_linked(&t->node);
35+
}
36+
3337
static inline void _init_thread_timeout(struct _thread_base *thread_base)
3438
{
3539
_init_timeout(&thread_base->timeout, NULL);
@@ -59,6 +63,7 @@ s32_t z_timeout_remaining(struct _timeout *timeout);
5963
#define _init_thread_timeout(t) do {} while (0)
6064
#define _add_thread_timeout(th, to) do {} while (0 && (void *)to && (void *)th)
6165
#define _abort_thread_timeout(t) (0)
66+
#define _is_inactive_timeout(t) 0
6267
#define _get_next_timeout_expiry() (K_FOREVER)
6368
#define z_set_timeout_expiry(t, i) do {} while (0)
6469

kernel/sched.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ void _impl_k_wakeup(k_tid_t thread)
896896
return;
897897
}
898898

899-
if (_abort_thread_timeout(thread) == _INACTIVE) {
899+
if (_abort_thread_timeout(thread) < 0) {
900900
irq_unlock(key);
901901
return;
902902
}

kernel/timeout.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ static void remove_timeout(struct _timeout *t)
5151
}
5252

5353
sys_dlist_remove(&t->node);
54-
t->dticks = _INACTIVE;
5554
}
5655

5756
static s32_t elapsed(void)
@@ -75,7 +74,7 @@ static s32_t next_timeout(void)
7574

7675
void _add_timeout(struct _timeout *to, _timeout_func_t fn, s32_t ticks)
7776
{
78-
__ASSERT(to->dticks < 0, "");
77+
__ASSERT(!sys_dnode_is_linked(&to->node), "");
7978
to->fn = fn;
8079
ticks = max(1, ticks);
8180

@@ -107,7 +106,7 @@ void _add_timeout(struct _timeout *to, _timeout_func_t fn, s32_t ticks)
107106

108107
int _abort_timeout(struct _timeout *to)
109108
{
110-
int ret = _INACTIVE;
109+
int ret = -EINVAL;
111110

112111
LOCKED(&timeout_lock) {
113112
if (sys_dnode_is_linked(&to->node)) {
@@ -123,7 +122,7 @@ s32_t z_timeout_remaining(struct _timeout *timeout)
123122
{
124123
s32_t ticks = 0;
125124

126-
if (timeout->dticks == _INACTIVE) {
125+
if (_is_inactive_timeout(timeout)) {
127126
return 0;
128127
}
129128

kernel/timer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ Z_SYSCALL_HANDLER(k_timer_start, timer, duration_p, period_p)
150150
void _impl_k_timer_stop(struct k_timer *timer)
151151
{
152152
unsigned int key = irq_lock();
153-
bool inactive = (_abort_timeout(&timer->timeout) == _INACTIVE);
153+
int inactive = _abort_timeout(&timer->timeout) != 0;
154154

155155
irq_unlock(key);
156156

@@ -203,7 +203,7 @@ u32_t _impl_k_timer_status_sync(struct k_timer *timer)
203203
u32_t result = timer->status;
204204

205205
if (result == 0) {
206-
if (timer->timeout.dticks != _INACTIVE) {
206+
if (!_is_inactive_timeout(&timer->timeout)) {
207207
/* wait for timer to expire or stop */
208208
(void)_pend_current_thread(key, &timer->wait_q, K_FOREVER);
209209

0 commit comments

Comments
 (0)