Skip to content

Commit a995d9d

Browse files
Mikhail Kushnerovnashif
authored andcommitted
kernel: fix k_sleep in no multi-threading mode
Fix k_sleep implementation for no multi-threading mode. Absolute value of timeout expiration was fed to the k_busy_wait() function instead of delta value. That caused bug like incrementing of sleep time in geometric progression (while actual function argument is constant) during program running. Signed-off-by: Mikhail Kushnerov <[email protected]>
1 parent c9d6c4c commit a995d9d

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

kernel/nothread.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ bool k_is_in_isr(void)
2020
int32_t z_impl_k_sleep(k_timeout_t timeout)
2121
{
2222
k_ticks_t ticks;
23-
uint32_t expected_wakeup_ticks;
23+
uint32_t ticks_to_wait;
2424

2525
__ASSERT(!arch_is_in_isr(), "");
2626

@@ -37,12 +37,20 @@ int32_t z_impl_k_sleep(k_timeout_t timeout)
3737

3838
ticks = timeout.ticks;
3939
if (Z_TICK_ABS(ticks) <= 0) {
40-
expected_wakeup_ticks = ticks + sys_clock_tick_get_32();
40+
/* ticks is delta timeout */
41+
ticks_to_wait = ticks;
4142
} else {
42-
expected_wakeup_ticks = Z_TICK_ABS(ticks);
43+
/* ticks is absolute timeout expiration */
44+
uint32_t curr_ticks = sys_clock_tick_get_32();
45+
46+
if (Z_TICK_ABS(ticks) > curr_ticks) {
47+
ticks_to_wait = Z_TICK_ABS(ticks) - curr_ticks;
48+
} else {
49+
ticks_to_wait = 0;
50+
}
4351
}
4452
/* busy wait to be time coherent since subsystems may depend on it */
45-
z_impl_k_busy_wait(k_ticks_to_us_ceil32(expected_wakeup_ticks));
53+
z_impl_k_busy_wait(k_ticks_to_us_ceil32(ticks_to_wait));
4654

4755
int32_t ret = k_ticks_to_ms_ceil64(0);
4856

0 commit comments

Comments
 (0)