1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 01:07:35 +00:00

Kernel: Move process thread lists into protected data

This commit is contained in:
Andreas Kling 2021-03-11 14:12:55 +01:00
parent 1b2ea12062
commit 4916b5c130
4 changed files with 22 additions and 4 deletions

View file

@ -1344,7 +1344,7 @@ KResult ProcFSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntr
auto process = Process::from_pid(pid);
if (!process)
return ENOENT;
process->for_each_thread([&](Thread& thread) -> IterationDecision {
process->for_each_thread([&](const Thread& thread) -> IterationDecision {
int tid = thread.tid().value();
callback({ String::number(tid), to_identifier_with_stack(fsid(), tid), 0 });
return IterationDecision::Continue;

View file

@ -693,6 +693,7 @@ bool Process::create_perf_events_buffer_if_needed()
bool Process::remove_thread(Thread& thread)
{
ProtectedDataMutationScope scope { *this };
auto thread_cnt_before = m_thread_count.fetch_sub(1, AK::MemoryOrder::memory_order_acq_rel);
VERIFY(thread_cnt_before != 0);
ScopedSpinLock thread_list_lock(m_thread_list_lock);
@ -702,6 +703,7 @@ bool Process::remove_thread(Thread& thread)
bool Process::add_thread(Thread& thread)
{
ProtectedDataMutationScope scope { *this };
bool is_first = m_thread_count.fetch_add(1, AK::MemoryOrder::memory_order_relaxed) == 0;
ScopedSpinLock thread_list_lock(m_thread_list_lock);
m_thread_list.append(thread);

View file

@ -119,6 +119,8 @@ protected:
u32 m_execpromises { 0 };
mode_t m_umask { 022 };
VirtualAddress m_signal_trampoline;
Atomic<u32> m_thread_count { 0 };
IntrusiveList<Thread, &Thread::m_process_thread_list_node> m_thread_list;
};
class ProcessBase : public ProtectedProcessBase {
@ -245,8 +247,11 @@ public:
static void for_each_in_pgrp(ProcessGroupID, Callback);
template<typename Callback>
void for_each_child(Callback);
template<typename Callback>
IterationDecision for_each_thread(Callback) const;
IterationDecision for_each_thread(Callback);
template<typename Callback>
IterationDecision for_each_thread(Callback callback) const;
void die();
void finalize();
@ -576,8 +581,6 @@ private:
u8 m_termination_status { 0 };
u8 m_termination_signal { 0 };
Atomic<u32> m_thread_count { 0 };
mutable IntrusiveList<Thread, &Thread::m_process_thread_list_node> m_thread_list;
mutable RecursiveSpinLock m_thread_list_lock;
const bool m_is_kernel_process;
@ -669,6 +672,18 @@ inline IterationDecision Process::for_each_thread(Callback callback) const
return IterationDecision::Continue;
}
template<typename Callback>
inline IterationDecision Process::for_each_thread(Callback callback)
{
ScopedSpinLock thread_list_lock(m_thread_list_lock);
for (auto& thread : m_thread_list) {
IterationDecision decision = callback(thread);
if (decision != IterationDecision::Continue)
return decision;
}
return IterationDecision::Continue;
}
template<typename Callback>
inline void Process::for_each_in_pgrp(ProcessGroupID pgid, Callback callback)
{

View file

@ -86,6 +86,7 @@ class Thread
AK_MAKE_NONMOVABLE(Thread);
friend class Process;
friend class ProtectedProcessBase;
friend class Scheduler;
friend class ThreadReadyQueue;