Kernel: Make process list a singleton

This commit is contained in:
Jean-Baptiste Boric 2021-07-20 12:47:46 +02:00 committed by Andreas Kling
parent 626b99ce1c
commit 8554b66d09
Notes: sideshowbarker 2024-07-18 07:20:03 +09:00
3 changed files with 22 additions and 18 deletions

View File

@ -45,7 +45,7 @@ static void create_signal_trampoline();
RecursiveSpinLock g_processes_lock;
static Atomic<pid_t> next_pid;
READONLY_AFTER_INIT Process::List* g_processes;
static AK::Singleton<Process::List> s_processes;
READONLY_AFTER_INIT HashMap<String, OwnPtr<Module>>* g_modules;
READONLY_AFTER_INIT Memory::Region* g_signal_trampoline_region;
@ -56,6 +56,11 @@ ProtectedValue<String>& hostname()
return *s_hostname;
}
Process::List& processes()
{
return *s_processes;
}
ProcessID Process::allocate_pid()
{
// Overflow is UB, and negative PIDs wreck havoc.
@ -71,7 +76,6 @@ UNMAP_AFTER_INIT void Process::initialize()
g_modules = new HashMap<String, OwnPtr<Module>>;
next_pid.store(0, AK::MemoryOrder::memory_order_release);
g_processes = new Process::List();
g_process_groups = new ProcessGroup::List();
hostname().with_exclusive([&](auto& name) {
@ -85,20 +89,20 @@ Vector<ProcessID> Process::all_pids()
{
Vector<ProcessID> pids;
ScopedSpinLock lock(g_processes_lock);
pids.ensure_capacity(g_processes->size_slow());
for (auto& process : *g_processes)
pids.ensure_capacity(processes().size_slow());
for (auto& process : processes())
pids.append(process.pid());
return pids;
}
NonnullRefPtrVector<Process> Process::all_processes()
{
NonnullRefPtrVector<Process> processes;
NonnullRefPtrVector<Process> output;
ScopedSpinLock lock(g_processes_lock);
processes.ensure_capacity(g_processes->size_slow());
for (auto& process : *g_processes)
processes.append(NonnullRefPtr<Process>(process));
return processes;
output.ensure_capacity(processes().size_slow());
for (auto& process : processes())
output.append(NonnullRefPtr<Process>(process));
return output;
}
bool Process::in_group(gid_t gid) const
@ -147,7 +151,7 @@ void Process::register_new(Process& process)
RefPtr<Process> new_process = process;
{
ScopedSpinLock lock(g_processes_lock);
g_processes->prepend(process);
processes().prepend(process);
}
ProcFSComponentRegistry::the().register_new_process(process);
}
@ -307,7 +311,7 @@ Process::~Process()
{
ScopedSpinLock processes_lock(g_processes_lock);
if (m_list_node.is_in_list())
g_processes->remove(*this);
processes().remove(*this);
}
}
@ -414,7 +418,7 @@ void Process::crash(int signal, FlatPtr ip, bool out_of_memory)
RefPtr<Process> Process::from_pid(ProcessID pid)
{
ScopedSpinLock lock(g_processes_lock);
for (auto& process : *g_processes) {
for (auto& process : processes()) {
process.pid();
if (process.pid() == pid)
return &process;
@ -690,7 +694,7 @@ void Process::die()
{
ScopedSpinLock lock(g_processes_lock);
for (auto it = g_processes->begin(); it != g_processes->end();) {
for (auto it = processes().begin(); it != processes().end();) {
auto& process = *it;
++it;
if (process.has_tracee_thread(pid())) {

View File

@ -780,7 +780,7 @@ public:
using List = IntrusiveList<Process, RawPtr<Process>, &Process::m_list_node>;
};
extern Process::List* g_processes;
Process::List& processes();
extern RecursiveSpinLock g_processes_lock;
template<IteratorFunction<Process&> Callback>
@ -788,7 +788,7 @@ inline void Process::for_each(Callback callback)
{
VERIFY_INTERRUPTS_DISABLED();
ScopedSpinLock lock(g_processes_lock);
for (auto it = g_processes->begin(); it != g_processes->end();) {
for (auto it = processes().begin(); it != processes().end();) {
auto& process = *it;
++it;
if (callback(process) == IterationDecision::Break)
@ -802,7 +802,7 @@ inline void Process::for_each_child(Callback callback)
VERIFY_INTERRUPTS_DISABLED();
ProcessID my_pid = pid();
ScopedSpinLock lock(g_processes_lock);
for (auto it = g_processes->begin(); it != g_processes->end();) {
for (auto it = processes().begin(); it != processes().end();) {
auto& process = *it;
++it;
if (process.ppid() == my_pid || process.has_tracee_thread(pid())) {
@ -841,7 +841,7 @@ inline void Process::for_each_in_pgrp(ProcessGroupID pgid, Callback callback)
{
VERIFY_INTERRUPTS_DISABLED();
ScopedSpinLock lock(g_processes_lock);
for (auto it = g_processes->begin(); it != g_processes->end();) {
for (auto it = processes().begin(); it != processes().end();) {
auto& process = *it;
++it;
if (!process.is_dead() && process.pgid() == pgid) {

View File

@ -66,7 +66,7 @@ KResult Process::do_killall(int signal)
// Send the signal to all processes we have access to for.
ScopedSpinLock lock(g_processes_lock);
for (auto& process : *g_processes) {
for (auto& process : processes()) {
KResult res = KSuccess;
if (process.pid() == pid())
res = do_killself(signal);