1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 02:08:11 +00:00

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

This commit is contained in:
Andreas Kling 2020-01-04 18:56:04 +01:00
parent 95ba0d5a02
commit 3a27790fa7
2 changed files with 16 additions and 53 deletions

View file

@ -3194,16 +3194,8 @@ void Process::sys$exit_thread(void* exit_value)
int Process::sys$detach_thread(int tid) int Process::sys$detach_thread(int tid)
{ {
Thread* thread = nullptr; auto* thread = Thread::from_tid(tid);
for_each_thread([&](auto& child_thread) { if (!thread || thread->pid() != pid())
if (child_thread.tid() == tid) {
thread = &child_thread;
return IterationDecision::Break;
}
return IterationDecision::Continue;
});
if (!thread)
return -ESRCH; return -ESRCH;
if (!thread->is_joinable()) 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)) if (exit_value && !validate_write_typed(exit_value))
return -EFAULT; return -EFAULT;
Thread* thread = nullptr; auto* thread = Thread::from_tid(tid);
for_each_thread([&](auto& child_thread) { if (!thread || thread->pid() != pid())
if (child_thread.tid() == tid) {
thread = &child_thread;
return IterationDecision::Break;
}
return IterationDecision::Continue;
});
if (!thread)
return -ESRCH; return -ESRCH;
if (thread == current) 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) if (strnlen(buffer, (size_t)buffer_size) > max_thread_name_size)
return -EINVAL; return -EINVAL;
Thread* thread = nullptr; auto* thread = Thread::from_tid(tid);
for_each_thread([&](auto& child_thread) { if (!thread || thread->pid() != pid())
if (child_thread.tid() == tid) {
thread = &child_thread;
return IterationDecision::Break;
}
return IterationDecision::Continue;
});
if (!thread)
return -ESRCH; return -ESRCH;
thread->set_name({ buffer, (size_t)buffer_size }); 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)) if (!validate_write(buffer, buffer_size))
return -EFAULT; return -EFAULT;
Thread* thread = nullptr; auto* thread = Thread::from_tid(tid);
for_each_thread([&](auto& child_thread) { if (!thread || thread->pid() != pid())
if (child_thread.tid() == tid) {
thread = &child_thread;
return IterationDecision::Break;
}
return IterationDecision::Continue;
});
if (!thread)
return -ESRCH; return -ESRCH;
if (thread->name().length() >= (size_t)buffer_size) if (thread->name().length() >= (size_t)buffer_size)
@ -3321,17 +3289,10 @@ int Process::sys$donate(int tid)
if (tid < 0) if (tid < 0)
return -EINVAL; return -EINVAL;
InterruptDisabler disabler; InterruptDisabler disabler;
Thread* beneficiary = nullptr; auto* thread = Thread::from_tid(tid);
for_each_thread([&](Thread& thread) { if (!thread || thread->pid() != pid())
if (thread.tid() == tid) { return -ESRCH;
beneficiary = &thread; Scheduler::donate_to(thread, "sys$donate");
return IterationDecision::Break;
}
return IterationDecision::Continue;
});
if (!beneficiary)
return -ENOTHREAD;
Scheduler::donate_to(beneficiary, "sys$donate");
return 0; return 0;
} }

View file

@ -794,11 +794,13 @@ void Thread::wake_from_queue()
Thread* Thread::from_tid(int tid) Thread* Thread::from_tid(int tid)
{ {
ASSERT_INTERRUPTS_DISABLED(); InterruptDisabler disabler;
Thread* found_thread = nullptr; Thread* found_thread = nullptr;
Thread::for_each([&](auto& thread) { Thread::for_each([&](auto& thread) {
if (thread.tid() == tid) if (thread.tid() == tid) {
found_thread = &thread; found_thread = &thread;
return IterationDecision::Break;
}
return IterationDecision::Continue; return IterationDecision::Continue;
}); });
return found_thread; return found_thread;