Scheduler: Don't search threads spawned before the current thread to find children
All checks were successful
continuous-integration/drone/push Build is passing

Children will always be spawned later (and thus have a higher PID) than their parent.
This commit is contained in:
apio 2023-04-28 15:19:01 +02:00
parent bcdec62f51
commit 7b4cfd52cd
Signed by: apio
GPG Key ID: B8A7D06E42258954
4 changed files with 13 additions and 13 deletions

View File

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

View File

@ -27,11 +27,11 @@ Result<u64> sys_waitpid(Registers*, SyscallArgs args)
}
else if (pid == -1)
{
if (!Scheduler::has_children((pid_t)current->id)) return err(ECHILD);
if (!Scheduler::has_children(current)) return err(ECHILD);
Option<Thread*> child;
while (child = Scheduler::find_exited_child((pid_t)current->id), !child.has_value())
while (child = Scheduler::find_exited_child(current), !child.has_value())
{
if (options & WNOHANG) return err(EAGAIN);
kernel_sleep(10);

View File

@ -285,11 +285,11 @@ namespace Scheduler
return {};
}
bool has_children(pid_t pid)
bool has_children(Thread* thread)
{
bool result { false };
for_each_child(pid, [&](Thread*) {
for_each_child(thread, [&](Thread*) {
result = true;
return false;
});
@ -297,11 +297,11 @@ namespace Scheduler
return result;
}
Option<Thread*> find_exited_child(pid_t pid)
Option<Thread*> find_exited_child(Thread* thread)
{
Option<Thread*> result;
for_each_child(pid, [&](Thread* child) {
for_each_child(thread, [&](Thread* child) {
if (!result.has_value() && child->state == ThreadState::Exited)
{
result = child;

View File

@ -29,21 +29,21 @@ namespace Scheduler
Option<Thread*> find_by_pid(pid_t pid);
template <typename Callback> void for_each_child(pid_t pid, Callback callback)
template <typename Callback> void for_each_child(Thread* thread, Callback callback)
{
for (auto* const thread : g_threads)
for (Thread* current = thread; current; current = g_threads.next(current).value_or(nullptr))
{
if (thread->parent_id == (u64)pid)
if (current && current->parent_id == thread->id)
{
bool should_continue = callback(thread);
bool should_continue = callback(current);
if (!should_continue) return;
}
}
}
bool has_children(pid_t pid);
bool has_children(Thread* thread);
Option<Thread*> find_exited_child(pid_t pid);
Option<Thread*> find_exited_child(Thread* thread);
}
extern "C" void kernel_yield();