From 293a992133d4f9482a726658c8870d27d6d76f34 Mon Sep 17 00:00:00 2001 From: apio Date: Thu, 4 May 2023 23:43:00 +0200 Subject: [PATCH] kernel: Add a kernel_wait_for_event() function to avoid weird calls to kernel_wait in kthreads --- kernel/src/arch/x86_64/CPU.cpp | 4 ++-- kernel/src/main.cpp | 2 +- kernel/src/thread/Scheduler.cpp | 6 ++++++ kernel/src/thread/Scheduler.h | 3 +++ 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/kernel/src/arch/x86_64/CPU.cpp b/kernel/src/arch/x86_64/CPU.cpp index 30a28324..19901b5e 100644 --- a/kernel/src/arch/x86_64/CPU.cpp +++ b/kernel/src/arch/x86_64/CPU.cpp @@ -128,10 +128,10 @@ void io_thread() while (true) { u8 scancode; - while (!scancode_queue.try_pop(scancode)) { kernel_wait(0); } + while (!scancode_queue.try_pop(scancode)) kernel_wait_for_event(); char key; - if (Keyboard::decode_scancode(scancode).try_set_value(key)) { ConsoleDevice::did_press_key(key); } + if (Keyboard::decode_scancode(scancode).try_set_value(key)) ConsoleDevice::did_press_key(key); } } diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp index 26fd999a..f6729c5a 100644 --- a/kernel/src/main.cpp +++ b/kernel/src/main.cpp @@ -30,7 +30,7 @@ void reap_thread() dying_threads.consume([](Thread* thread) { Scheduler::reap_thread(thread); }); - kernel_wait(0); + kernel_wait_for_event(); } } diff --git a/kernel/src/thread/Scheduler.cpp b/kernel/src/thread/Scheduler.cpp index cfbf8f20..45887a76 100644 --- a/kernel/src/thread/Scheduler.cpp +++ b/kernel/src/thread/Scheduler.cpp @@ -365,6 +365,12 @@ void kernel_wait(pid_t pid) kernel_yield(); } +void kernel_wait_for_event() +{ + g_current->state = ThreadState::Waiting; + kernel_yield(); +} + [[noreturn]] void kernel_exit() { g_current->state = ThreadState::Dying; diff --git a/kernel/src/thread/Scheduler.h b/kernel/src/thread/Scheduler.h index f34a731d..4816b459 100644 --- a/kernel/src/thread/Scheduler.h +++ b/kernel/src/thread/Scheduler.h @@ -56,3 +56,6 @@ extern "C" void kernel_yield(); void kernel_wait(pid_t pid); void kernel_sleep(u64 ms); [[noreturn]] void kernel_exit(); + +// Freezes the current thread until someone else calls wake_up() on this thread. +void kernel_wait_for_event();