kernel: Move thread exit code into a separate common function
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-06-19 12:33:25 +02:00
parent f052d8630d
commit e60b2a3d2f
Signed by: apio
GPG Key ID: B8A7D06E42258954
4 changed files with 27 additions and 54 deletions

View File

@ -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();

View File

@ -12,20 +12,5 @@ Result<u64> 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);
}

View File

@ -1,5 +1,6 @@
#include "thread/Thread.h"
#include "memory/MemoryManager.h"
#include "thread/Scheduler.h"
#include <bits/atfile.h>
#include <bits/open-flags.h>
#include <luna/Alloc.h>
@ -68,6 +69,27 @@ Result<SharedPtr<VFS::Inode>> 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;

View File

@ -99,6 +99,8 @@ struct Thread : public LinkedListNode<Thread>
PageDirectory* directory;
[[noreturn]] void exit_and_signal_parent(u8 status);
bool is_idle()
{
return state == ThreadState::Idle;