Skip to content

Commit e7a6ede

Browse files
ananglcarlescufi
authored andcommitted
[nrf fromtree] kernel: timeout: Fix adding of an absolute timeout
Correct the way the relative ticks value is calculated for an absolute timeout. Previously, elapsed() was called twice and the returned value was first subtracted from and then added to the ticks value. It could happen that the HW counter value read by elapsed() changed between the two calls to this function. This caused the test_timeout_abs test case from the timer_api test suite to occasionally fail, e.g. on certain nRF platforms. Signed-off-by: Andrzej Głąbek <[email protected]> (cherry picked from commit 59b21a2) Signed-off-by: Maciej Perkowski <[email protected]>
1 parent cfeafe5 commit e7a6ede

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

kernel/timeout.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,21 @@ void z_add_timeout(struct _timeout *to, _timeout_func_t fn,
9090
__ASSERT_NO_MSG(arch_mem_coherent(to));
9191
#endif
9292

93-
k_ticks_t ticks = timeout.ticks + 1;
94-
95-
if (IS_ENABLED(CONFIG_TIMEOUT_64BIT) && Z_TICK_ABS(ticks) >= 0) {
96-
ticks = Z_TICK_ABS(timeout.ticks) - (curr_tick + elapsed());
97-
}
98-
9993
__ASSERT(!sys_dnode_is_linked(&to->node), "");
10094
to->fn = fn;
101-
ticks = MAX(1, ticks);
10295

10396
LOCKED(&timeout_lock) {
10497
struct _timeout *t;
10598

106-
to->dticks = ticks + elapsed();
99+
if (IS_ENABLED(CONFIG_TIMEOUT_64BIT) &&
100+
Z_TICK_ABS(timeout.ticks) >= 0) {
101+
k_ticks_t ticks = Z_TICK_ABS(timeout.ticks) - curr_tick;
102+
103+
to->dticks = MAX(1, ticks);
104+
} else {
105+
to->dticks = timeout.ticks + 1 + elapsed();
106+
}
107+
107108
for (t = first(); t != NULL; t = next(t)) {
108109
if (t->dticks > to->dticks) {
109110
t->dticks -= to->dticks;

0 commit comments

Comments
 (0)