kernel: Store a thread's parent directly instead of its parent's PID

This commit is contained in:
apio 2023-05-04 22:58:04 +02:00
parent 58eb2d7703
commit 4616997f5b
Signed by: apio
GPG Key ID: B8A7D06E42258954
7 changed files with 18 additions and 10 deletions

View File

@ -125,12 +125,12 @@ Result<u64> sys_fork(Registers* regs, SyscallArgs)
thread->state = ThreadState::Runnable; thread->state = ThreadState::Runnable;
thread->is_kernel = false; thread->is_kernel = false;
thread->parent_id = current->id;
thread->fp_data.save(); thread->fp_data.save();
thread->name = current->name; thread->name = current->name;
thread->auth = current->auth; thread->auth = current->auth;
thread->current_directory = current->current_directory; thread->current_directory = current->current_directory;
thread->current_directory_path = move(current_directory_path); thread->current_directory_path = move(current_directory_path);
thread->parent = current;
for (int i = 0; i < FD_MAX; i++) { thread->fd_table[i] = current->fd_table[i]; } for (int i = 0; i < FD_MAX; i++) { thread->fd_table[i] = current->fd_table[i]; }

View File

@ -8,7 +8,7 @@ Result<u64> sys_exit(Registers*, SyscallArgs args)
Thread* current = Scheduler::current(); Thread* current = Scheduler::current();
Scheduler::for_each_child(current, [](Thread* child) { Scheduler::for_each_child(current, [](Thread* child) {
child->parent_id = 1; child->parent = Scheduler::init_thread();
return true; return true;
}); });

View File

@ -9,7 +9,9 @@ Result<u64> sys_getpid(Registers*, SyscallArgs)
Result<u64> sys_getppid(Registers*, SyscallArgs) Result<u64> sys_getppid(Registers*, SyscallArgs)
{ {
return Scheduler::current()->parent_id; auto* parent = Scheduler::current()->parent;
return parent ? parent->id : 0;
} }
Result<u64> sys_getuid(Registers*, SyscallArgs) Result<u64> sys_getuid(Registers*, SyscallArgs)

View File

@ -17,7 +17,7 @@ Result<u64> sys_waitpid(Registers*, SyscallArgs args)
{ {
thread = TRY(Result<Thread*>::from_option(Scheduler::find_by_pid(pid), ESRCH)); thread = TRY(Result<Thread*>::from_option(Scheduler::find_by_pid(pid), ESRCH));
if (thread->parent_id != current->id) return err(ECHILD); if (thread->parent && thread->parent != current) return err(ECHILD);
while (thread->state != ThreadState::Exited) while (thread->state != ThreadState::Exited)
{ {

View File

@ -11,6 +11,7 @@
static Thread g_idle; static Thread g_idle;
static Thread* g_current = nullptr; static Thread* g_current = nullptr;
static Thread* g_init = nullptr;
static const usize TICKS_PER_TIMESLICE = 20; static const usize TICKS_PER_TIMESLICE = 20;
@ -23,7 +24,7 @@ namespace Scheduler
g_idle.set_ip((u64)CPU::idle_loop); g_idle.set_ip((u64)CPU::idle_loop);
g_idle.state = ThreadState::Idle; g_idle.state = ThreadState::Idle;
g_idle.is_kernel = true; g_idle.is_kernel = true;
g_idle.parent_id = 0; g_idle.parent = nullptr;
g_idle.name = "[idle]"; g_idle.name = "[idle]";
g_idle.ticks_left = 1; g_idle.ticks_left = 1;
@ -52,6 +53,11 @@ namespace Scheduler
return &g_idle; return &g_idle;
} }
Thread* init_thread()
{
return g_init;
}
Result<void> new_kernel_thread_impl(Thread* thread, const char* name) Result<void> new_kernel_thread_impl(Thread* thread, const char* name)
{ {
// If anything fails, make sure to clean up. // If anything fails, make sure to clean up.
@ -66,8 +72,6 @@ namespace Scheduler
thread->stack = thread_stack; thread->stack = thread_stack;
thread->parent_id = 0;
thread->name = name; thread->name = name;
thread->is_kernel = true; thread->is_kernel = true;
@ -119,7 +123,6 @@ namespace Scheduler
thread->is_kernel = false; thread->is_kernel = false;
thread->id = 1; thread->id = 1;
thread->name = name; thread->name = name;
thread->parent_id = 0;
thread->auth = Credentials { .uid = 0, .euid = 0, .suid = 0, .gid = 0, .egid = 0, .sgid = 0 }; thread->auth = Credentials { .uid = 0, .euid = 0, .suid = 0, .gid = 0, .egid = 0, .sgid = 0 };
Vector<String> args; Vector<String> args;
@ -143,6 +146,7 @@ namespace Scheduler
thread->sp(), thread->kernel_stack.top()); thread->sp(), thread->kernel_stack.top());
g_threads.append(thread); g_threads.append(thread);
g_init = thread;
return thread; return thread;
} }

View File

@ -8,6 +8,7 @@ namespace Scheduler
Thread* current(); Thread* current();
Thread* idle(); Thread* idle();
Thread* init_thread();
Result<void> new_kernel_thread(u64 address, const char* name); Result<void> new_kernel_thread(u64 address, const char* name);
Result<void> new_kernel_thread(void (*func)(void), const char* name); Result<void> new_kernel_thread(void (*func)(void), const char* name);
@ -33,7 +34,7 @@ namespace Scheduler
{ {
for (Thread* current = thread; current; current = g_threads.next(current).value_or(nullptr)) for (Thread* current = thread; current; current = g_threads.next(current).value_or(nullptr))
{ {
if (current->parent_id == thread->id) if (current->parent == thread)
{ {
bool should_continue = callback(current); bool should_continue = callback(current);
if (!should_continue) return; if (!should_continue) return;

View File

@ -55,7 +55,6 @@ struct Thread : public LinkedListNode<Thread>
Registers regs; Registers regs;
u64 id; u64 id;
u64 parent_id;
Credentials auth; Credentials auth;
@ -90,6 +89,8 @@ struct Thread : public LinkedListNode<Thread>
String current_directory_path = {}; String current_directory_path = {};
SharedPtr<VFS::Inode> current_directory = {}; SharedPtr<VFS::Inode> current_directory = {};
Thread* parent { nullptr };
PageDirectory* directory; PageDirectory* directory;
bool is_idle() bool is_idle()