From e60b2a3d2f6d8f5bde6c1eee7d058c7a072f73d7 Mon Sep 17 00:00:00 2001 From: apio Date: Mon, 19 Jun 2023 12:33:25 +0200 Subject: [PATCH] kernel: Move thread exit code into a separate common function --- kernel/src/arch/x86_64/CPU.cpp | 40 ++-------------------------------- kernel/src/sys/exit.cpp | 17 +-------------- kernel/src/thread/Thread.cpp | 22 +++++++++++++++++++ kernel/src/thread/Thread.h | 2 ++ 4 files changed, 27 insertions(+), 54 deletions(-) diff --git a/kernel/src/arch/x86_64/CPU.cpp b/kernel/src/arch/x86_64/CPU.cpp index c7083ecd..1ac1d957 100644 --- a/kernel/src/arch/x86_64/CPU.cpp +++ b/kernel/src/arch/x86_64/CPU.cpp @@ -86,25 +86,7 @@ void decode_page_fault_error_code(u64 code) { // FIXME: Kill this process with SIGSEGV once we have signals and all that. kerrorln("Current task %zu was terminated because of a page fault", Scheduler::current()->id); - if (Scheduler::current()->is_kernel) Scheduler::current()->state = ThreadState::Dying; - else - { - auto* current = Scheduler::current(); - auto* parent = current->parent; - if (parent && parent->state == ThreadState::Waiting) - { - auto child = *parent->child_being_waited_for; - if (child == -1 || child == (pid_t)current->id) - { - parent->child_being_waited_for = (pid_t)current->id; - parent->wake_up(); - } - } - current->state = ThreadState::Exited; - } - Scheduler::current()->status = 127; - kernel_yield(); - unreachable(); + Scheduler::current()->exit_and_signal_parent(127); } CPU::efficient_halt(); @@ -122,25 +104,7 @@ void decode_page_fault_error_code(u64 code) { // FIXME: Kill this process with SIGSEGV once we have signals and all that. kerrorln("Current task %zu was terminated because of a general protection fault", Scheduler::current()->id); - if (Scheduler::current()->is_kernel) Scheduler::current()->state = ThreadState::Dying; - else - { - auto* current = Scheduler::current(); - auto* parent = current->parent; - if (parent && parent->state == ThreadState::Waiting) - { - auto child = *parent->child_being_waited_for; - if (child == -1 || child == (pid_t)current->id) - { - parent->child_being_waited_for = (pid_t)current->id; - parent->wake_up(); - } - } - current->state = ThreadState::Exited; - } - Scheduler::current()->status = 127; - kernel_yield(); - unreachable(); + Scheduler::current()->exit_and_signal_parent(127); } CPU::efficient_halt(); diff --git a/kernel/src/sys/exit.cpp b/kernel/src/sys/exit.cpp index 0a6ad43d..558565ff 100644 --- a/kernel/src/sys/exit.cpp +++ b/kernel/src/sys/exit.cpp @@ -12,20 +12,5 @@ Result sys_exit(Registers*, SyscallArgs args) return true; }); - auto* parent = current->parent; - if (parent && parent->state == ThreadState::Waiting) - { - auto child = *parent->child_being_waited_for; - if (child == -1 || child == (pid_t)current->id) - { - parent->child_being_waited_for = (pid_t)current->id; - parent->wake_up(); - } - } - - current->status = status; - current->state = ThreadState::Exited; - - kernel_yield(); - unreachable(); + current->exit_and_signal_parent(status); } diff --git a/kernel/src/thread/Thread.cpp b/kernel/src/thread/Thread.cpp index df5e5cd8..24621cc8 100644 --- a/kernel/src/thread/Thread.cpp +++ b/kernel/src/thread/Thread.cpp @@ -1,5 +1,6 @@ #include "thread/Thread.h" #include "memory/MemoryManager.h" +#include "thread/Scheduler.h" #include #include #include @@ -68,6 +69,27 @@ Result> Thread::resolve_atfile(int dirfd, const String& pa return VFS::resolve_path(path.chars(), this->auth, descriptor->inode, follow_last_symlink); } +[[noreturn]] void Thread::exit_and_signal_parent(u8 _status) +{ + if (is_kernel) state = ThreadState::Dying; + else + { + if (parent && parent->state == ThreadState::Waiting) + { + auto child = *parent->child_being_waited_for; + if (child == -1 || child == (pid_t)id) + { + parent->child_being_waited_for = (pid_t)id; + parent->wake_up(); + } + } + state = ThreadState::Exited; + } + status = _status; + kernel_yield(); + unreachable(); +} + bool FileDescriptor::should_append() { return flags & O_APPEND; diff --git a/kernel/src/thread/Thread.h b/kernel/src/thread/Thread.h index 7c02dadf..ad6903a6 100644 --- a/kernel/src/thread/Thread.h +++ b/kernel/src/thread/Thread.h @@ -99,6 +99,8 @@ struct Thread : public LinkedListNode PageDirectory* directory; + [[noreturn]] void exit_and_signal_parent(u8 status); + bool is_idle() { return state == ThreadState::Idle;