From d0b65674e6990cc2cfc2f4d3a2503a212df8578c Mon Sep 17 00:00:00 2001 From: apio Date: Thu, 8 Jun 2023 19:57:38 +0200 Subject: [PATCH] kernel: Fix 0-delay sleeps blocking the thread forever --- kernel/src/sys/usleep.cpp | 3 +++ kernel/src/thread/Scheduler.cpp | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/kernel/src/sys/usleep.cpp b/kernel/src/sys/usleep.cpp index 970b887b..dbf853ae 100644 --- a/kernel/src/sys/usleep.cpp +++ b/kernel/src/sys/usleep.cpp @@ -6,6 +6,9 @@ Result sys_usleep(Registers*, SyscallArgs args) { useconds_t us = (useconds_t)args[0]; + // FIXME: Allow usleep() to use a more precise resolution. + if (us < 1000) return 0; + kernel_sleep(us / 1000); return 0; diff --git a/kernel/src/thread/Scheduler.cpp b/kernel/src/thread/Scheduler.cpp index a40af402..4e331010 100644 --- a/kernel/src/thread/Scheduler.cpp +++ b/kernel/src/thread/Scheduler.cpp @@ -271,7 +271,7 @@ namespace Scheduler { if (thread->state == ThreadState::Sleeping) { - if (--thread->sleep_ticks_left == 0) thread->wake_up(); + if (thread->sleep_ticks_left == 0 || --thread->sleep_ticks_left == 0) thread->wake_up(); } }