ソースを参照

Kernel: Use Thread::from_tid() in more places

Andreas Kling 5 年 前
コミット
3a27790fa7
2 ファイル変更16 行追加53 行削除
  1. 12 51
      Kernel/Process.cpp
  2. 4 2
      Kernel/Thread.cpp

+ 12 - 51
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;
 }
 

+ 4 - 2
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;