From 0d9088eddce67c6d583d5cdc6e9c395998b7ddd6 Mon Sep 17 00:00:00 2001 From: wheremyfoodat Date: Tue, 20 Sep 2022 03:20:51 +0300 Subject: [PATCH] Add status variable to threads --- include/kernel/kernel.hpp | 2 +- include/kernel/kernel_types.hpp | 16 ++++++++++++++-- src/core/kernel/address_arbiter.cpp | 2 +- src/core/kernel/kernel.cpp | 2 +- src/core/kernel/threads.cpp | 6 +++--- 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/include/kernel/kernel.hpp b/include/kernel/kernel.hpp index 978856d4..03c4ab7d 100644 --- a/include/kernel/kernel.hpp +++ b/include/kernel/kernel.hpp @@ -59,7 +59,7 @@ class Kernel { Handle makeProcess(); Handle makePort(const char* name); Handle makeSession(Handle port); - Handle makeThread(u32 entrypoint, u32 initialSP, u32 priority, u32 id); + Handle makeThread(u32 entrypoint, u32 initialSP, u32 priority, u32 id, ThreadStatus status = ThreadStatus::Dormant); std::optional getPortHandle(const char* name); void deleteObjectData(KernelObject& object); diff --git a/include/kernel/kernel_types.hpp b/include/kernel/kernel_types.hpp index b7e81fcf..4719866e 100644 --- a/include/kernel/kernel_types.hpp +++ b/include/kernel/kernel_types.hpp @@ -86,14 +86,26 @@ struct Session { Session(Handle portHandle) : portHandle(portHandle) {} }; +enum class ThreadStatus { + Running, // Currently running + Ready, // Ready to run + WaitArb, // Waiting on an address arbiter + WaitSleep, // Waiting due to a SleepThread SVC + WaitIPC, // Waiting for the reply from an IPC request + Dormant, // Created but not yet made ready + Dead // Run to completion, or forcefully terminated +}; + struct Thread { u32 initialSP; // Initial r13 value u32 entrypoint; // Initial r15 value u32 priority; u32 processorID; - Thread(u32 initialSP, u32 entrypoint, u32 priority, u32 processorID) : initialSP(initialSP), entrypoint(entrypoint), - priority(priority), processorID(processorID) {} + ThreadStatus status; + + Thread(u32 initialSP, u32 entrypoint, u32 priority, u32 processorID, ThreadStatus status = ThreadStatus::Dormant) + : initialSP(initialSP), entrypoint(entrypoint), priority(priority), processorID(processorID), status(status) {} }; static const char* kernelObjectTypeToString(KernelObjectType t) { diff --git a/src/core/kernel/address_arbiter.cpp b/src/core/kernel/address_arbiter.cpp index 8421a035..2cd9b749 100644 --- a/src/core/kernel/address_arbiter.cpp +++ b/src/core/kernel/address_arbiter.cpp @@ -46,7 +46,7 @@ void Kernel::arbitrateAddress() { return; } - if (address & 3) { + if (address & 3) [[unlikely]] { Helpers::panic("ArbitrateAddres:: Unaligned address"); } diff --git a/src/core/kernel/kernel.cpp b/src/core/kernel/kernel.cpp index 9c6871ce..83352bc6 100644 --- a/src/core/kernel/kernel.cpp +++ b/src/core/kernel/kernel.cpp @@ -74,7 +74,7 @@ void Kernel::reset() { currentProcess = makeProcess(); // Make main thread object. We do not have to set the entrypoint and SP for it as the ROM loader does. // Main thread seems to have a priority of 0x30 - mainThread = makeThread(0, 0, 0x30, 0); + mainThread = makeThread(0, 0, 0x30, 0, ThreadStatus::Running); currentThread = mainThread; // Create global service manager port diff --git a/src/core/kernel/threads.cpp b/src/core/kernel/threads.cpp index dab3be72..7fc33477 100644 --- a/src/core/kernel/threads.cpp +++ b/src/core/kernel/threads.cpp @@ -2,14 +2,14 @@ #include "resource_limits.hpp" // Internal OS function to spawn a thread -Handle Kernel::makeThread(u32 entrypoint, u32 initialSP, u32 priority, u32 id) { +Handle Kernel::makeThread(u32 entrypoint, u32 initialSP, u32 priority, u32 id, ThreadStatus status) { if (threadCount >= appResourceLimits.maxThreads) { Helpers::panic("Overflowed the number of threads"); } threadCount++; Handle ret = makeObject(KernelObjectType::Thread); - objects[ret].data = new Thread(initialSP, entrypoint, priority, id); + objects[ret].data = new Thread(initialSP, entrypoint, priority, id, status); return ret; } @@ -23,7 +23,7 @@ void Kernel::createThread() { printf("CreateThread(entry = %08X, stacktop = %08X, priority = %X, processor ID = %d)\n", entrypoint, initialSP, priority, id); - if (!(priority <= 0x3F)) [[unlikely]] { + if (priority > 0x3F) [[unlikely]] { Helpers::panic("Created thread with bad priority value %X", priority); regs[0] = SVCResult::BadThreadPriority; return;