From 021ea1063b9fb2ce9dbf4127a4b34fee25b45483 Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 7 Dec 2022 15:14:58 +0100 Subject: [PATCH] Add a state to Thread --- kernel/src/arch/x86_64/Thread.cpp | 2 +- kernel/src/thread/Scheduler.cpp | 6 +++--- kernel/src/thread/Thread.h | 14 +++++++++++++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/kernel/src/arch/x86_64/Thread.cpp b/kernel/src/arch/x86_64/Thread.cpp index 1abd34a0..7eb3160e 100644 --- a/kernel/src/arch/x86_64/Thread.cpp +++ b/kernel/src/arch/x86_64/Thread.cpp @@ -43,7 +43,7 @@ void Thread::set_arguments(u64 arg1, u64 arg2, u64 arg3, u64 arg4) void switch_context(Thread* old_thread, Thread* new_thread, Registers* regs) { - if (!old_thread->is_idle) memcpy(&old_thread->regs, regs, sizeof(Registers)); + if (!old_thread->is_idle()) memcpy(&old_thread->regs, regs, sizeof(Registers)); memcpy(regs, &new_thread->regs, sizeof(Registers)); } \ No newline at end of file diff --git a/kernel/src/thread/Scheduler.cpp b/kernel/src/thread/Scheduler.cpp index c4058e24..04dd4e1f 100644 --- a/kernel/src/thread/Scheduler.cpp +++ b/kernel/src/thread/Scheduler.cpp @@ -18,7 +18,7 @@ namespace Scheduler g_idle.id = 0; g_idle.init_regs_kernel(); g_idle.set_ip((u64)CPU::idle_loop); - g_idle.is_idle = true; + g_idle.state = ThreadState::Idle; g_idle.ticks_left = 1; @@ -86,7 +86,7 @@ namespace Scheduler Thread* pick_task() { Thread* old = g_current; - if (old->is_idle) + if (old->is_idle()) { auto maybe_first = g_threads.last(); if (maybe_first.has_error()) // No threads!! @@ -111,7 +111,7 @@ namespace Scheduler { if (old_thread != new_thread) switch_context(old_thread, new_thread, regs); - if (new_thread->is_idle) + if (new_thread->is_idle()) { new_thread->ticks_left = 1; // The idle task only runs for 1 tick so we can check for new runnable tasks // as fast as possible. diff --git a/kernel/src/thread/Thread.h b/kernel/src/thread/Thread.h index 3b3b7a39..d63e58c4 100644 --- a/kernel/src/thread/Thread.h +++ b/kernel/src/thread/Thread.h @@ -10,6 +10,13 @@ #error "Unknown architecture." #endif +enum class ThreadState +{ + Idle, + Runnable, + Sleeping +}; + struct Thread : public DoublyLinkedListNode { Registers regs; @@ -22,7 +29,12 @@ struct Thread : public DoublyLinkedListNode u64 ticks_left; - bool is_idle = false; + ThreadState state = ThreadState::Runnable; + + bool is_idle() + { + return state == ThreadState::Idle; + } void init_regs_kernel(); void init_regs_user();