From 9f75b047141070b97408608f9eaa37787fc1146d Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 27 Jul 2023 18:34:06 +0300 Subject: [PATCH] Kernel: Scheduler can now check if tid is valid Tid can become invalid if the thread is already terminated --- kernel/include/kernel/Scheduler.h | 2 ++ kernel/kernel/Scheduler.cpp | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/kernel/include/kernel/Scheduler.h b/kernel/include/kernel/Scheduler.h index 42a4ffd6d2..1b4a8fa683 100644 --- a/kernel/include/kernel/Scheduler.h +++ b/kernel/include/kernel/Scheduler.h @@ -31,6 +31,8 @@ namespace Kernel Thread& current_thread(); static pid_t current_tid(); + static bool is_valid_tid(pid_t tid); + private: Scheduler() = default; diff --git a/kernel/kernel/Scheduler.cpp b/kernel/kernel/Scheduler.cpp index a815fe8099..a65ad08bed 100644 --- a/kernel/kernel/Scheduler.cpp +++ b/kernel/kernel/Scheduler.cpp @@ -373,4 +373,26 @@ namespace Kernel } } + bool Scheduler::is_valid_tid(pid_t tid) + { + CriticalScope _; + + if (s_instance == nullptr) + return tid == 0; + + for (auto& thread : s_instance->m_active_threads) + if (thread.thread->tid() == tid) + return true; + + for (auto& thread : s_instance->m_sleeping_threads) + if (thread.thread->tid() == tid) + return true; + + for (auto& thread : s_instance->m_blocking_threads) + if (thread.thread->tid() == tid) + return true; + + return false; + } + } \ No newline at end of file