From 3a27790fa7f583eacfde659a7ba7c3e63fa1e766 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 4 Jan 2020 18:56:04 +0100 Subject: [PATCH] Kernel: Use Thread::from_tid() in more places --- Kernel/Process.cpp | 63 +++++++++------------------------------------- Kernel/Thread.cpp | 6 +++-- 2 files changed, 16 insertions(+), 53 deletions(-) diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index f1bcb368c5d..9de9624f14d 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -3194,16 +3194,8 @@ void Process::sys$exit_thread(void* exit_value) int Process::sys$detach_thread(int tid) { - Thread* thread = nullptr; - for_each_thread([&](auto& child_thread) { - if (child_thread.tid() == tid) { - thread = &child_thread; - return IterationDecision::Break; - } - return IterationDecision::Continue; - }); - - if (!thread) + auto* thread = Thread::from_tid(tid); + if (!thread || thread->pid() != pid()) return -ESRCH; if (!thread->is_joinable()) @@ -3218,16 +3210,8 @@ int Process::sys$join_thread(int tid, void** exit_value) if (exit_value && !validate_write_typed(exit_value)) return -EFAULT; - Thread* thread = nullptr; - for_each_thread([&](auto& child_thread) { - if (child_thread.tid() == tid) { - thread = &child_thread; - return IterationDecision::Break; - } - return IterationDecision::Continue; - }); - - if (!thread) + auto* thread = Thread::from_tid(tid); + if (!thread || thread->pid() != pid()) return -ESRCH; if (thread == current) @@ -3269,16 +3253,8 @@ int Process::sys$set_thread_name(int tid, const char* buffer, int buffer_size) if (strnlen(buffer, (size_t)buffer_size) > max_thread_name_size) return -EINVAL; - Thread* thread = nullptr; - for_each_thread([&](auto& child_thread) { - if (child_thread.tid() == tid) { - thread = &child_thread; - return IterationDecision::Break; - } - return IterationDecision::Continue; - }); - - if (!thread) + auto* thread = Thread::from_tid(tid); + if (!thread || thread->pid() != pid()) return -ESRCH; thread->set_name({ buffer, (size_t)buffer_size }); @@ -3292,16 +3268,8 @@ int Process::sys$get_thread_name(int tid, char* buffer, int buffer_size) if (!validate_write(buffer, buffer_size)) return -EFAULT; - Thread* thread = nullptr; - for_each_thread([&](auto& child_thread) { - if (child_thread.tid() == tid) { - thread = &child_thread; - return IterationDecision::Break; - } - return IterationDecision::Continue; - }); - - if (!thread) + auto* thread = Thread::from_tid(tid); + if (!thread || thread->pid() != pid()) return -ESRCH; if (thread->name().length() >= (size_t)buffer_size) @@ -3321,17 +3289,10 @@ int Process::sys$donate(int tid) if (tid < 0) return -EINVAL; InterruptDisabler disabler; - Thread* beneficiary = nullptr; - for_each_thread([&](Thread& thread) { - if (thread.tid() == tid) { - beneficiary = &thread; - return IterationDecision::Break; - } - return IterationDecision::Continue; - }); - if (!beneficiary) - return -ENOTHREAD; - Scheduler::donate_to(beneficiary, "sys$donate"); + auto* thread = Thread::from_tid(tid); + if (!thread || thread->pid() != pid()) + return -ESRCH; + Scheduler::donate_to(thread, "sys$donate"); return 0; } diff --git a/Kernel/Thread.cpp b/Kernel/Thread.cpp index f4409df8a3e..00946db42d5 100644 --- a/Kernel/Thread.cpp +++ b/Kernel/Thread.cpp @@ -794,11 +794,13 @@ void Thread::wake_from_queue() Thread* Thread::from_tid(int tid) { - ASSERT_INTERRUPTS_DISABLED(); + InterruptDisabler disabler; Thread* found_thread = nullptr; Thread::for_each([&](auto& thread) { - if (thread.tid() == tid) + if (thread.tid() == tid) { found_thread = &thread; + return IterationDecision::Break; + } return IterationDecision::Continue; }); return found_thread;