Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kernel: fix k_sleep in no multi-threading mode #80979

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

KushnerovMikhail
Copy link

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.

You can check it by simple program with CONFIG_MULTITHREADING=n option:

#include <zephyr/kernel.h>

int main (void)
{
	uint32_t new_ticks = 0;
	uint32_t old_ticks = sys_clock_tick_get_32();

	while (1) {
		k_sleep(K_MSEC(1000));
		new_ticks = sys_clock_tick_get_32();
		printf("%u\n", new_ticks - old_ticks);
		old_ticks = new_ticks;
	}

	return 0;
}

Output from qemu_riscv64
Before fix:
100
200
400
800
...

After fix:
100
100
100
100
...

} else {
expected_wakeup_ticks = Z_TICK_ABS(ticks);
/* ticks is absolute timeout expiration */
ticks_to_wait = Z_TICK_ABS(ticks) - sys_clock_tick_get_32();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have already passed the absolute point in time (Z_TICKS_ABS(ticks) < sys_clock_tick_get_32()), we should set ticks_to_wait to 0, otherwise we are waiting when we should not be waiting.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment. Fixed it

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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants