zephyrproject-rtos / zephyr

Primary Git Repository for the Zephyr Project. Zephyr is a new generation, scalable, optimized, secure RTOS for multiple hardware architectures.
https://docs.zephyrproject.org
Apache License 2.0
10.86k stars 6.62k forks source link

kernel: k_sleep bug in no multi-threading mode #81210

Open KushnerovMikhail opened 2 days ago

KushnerovMikhail commented 2 days ago

Describe the bug Bug in 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.

Suggested solution

80979

To Reproduce

  1. Replace hello_world sample source with simple code:
    
    #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;

}

2. write `CONFIG_MULTITHREADING=n` flag in `prj.conf`
3. `cd zephyr`
4. `west build -b qemu-riscv64 samples/hello_world`
5. `west build -t run`
6. See, that waiting time between console outputs increases, even though constant value is passed into `k_sleep`

**Expected behavior**
Expected, that `k_sleep` will wait actual amount of time, specified in function argument

**Impact**
Programs start to sleep a lot of time, when compile for no multi-threading mode

**Logs and console output**
Output from qemu_riscv64:

100 200 400 800 ...



**Environment (please complete the following information):**
- OS: Linux
- Toolchain: zephyr-sdk-0.16.1
- Commit SHA or Version used: https://github.com/zephyrproject-rtos/zephyr/commit/c3466b14d09b1e51b6ca472f3b3654e40cba12d8

**Additional context**
NA
github-actions[bot] commented 2 days ago

Hi @KushnerovMikhail! We appreciate you submitting your first issue for our open-source project. 🌟

Even though I'm a bot, I can assure you that the whole community is genuinely grateful for your time and effort. 🤖💙