mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 19:57:41 +00:00
Kernel: Make process list a singleton
This commit is contained in:
parent
626b99ce1c
commit
8554b66d09
3 changed files with 22 additions and 18 deletions
|
@ -45,7 +45,7 @@ static void create_signal_trampoline();
|
||||||
|
|
||||||
RecursiveSpinLock g_processes_lock;
|
RecursiveSpinLock g_processes_lock;
|
||||||
static Atomic<pid_t> next_pid;
|
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 HashMap<String, OwnPtr<Module>>* g_modules;
|
||||||
READONLY_AFTER_INIT Memory::Region* g_signal_trampoline_region;
|
READONLY_AFTER_INIT Memory::Region* g_signal_trampoline_region;
|
||||||
|
|
||||||
|
@ -56,6 +56,11 @@ ProtectedValue<String>& hostname()
|
||||||
return *s_hostname;
|
return *s_hostname;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Process::List& processes()
|
||||||
|
{
|
||||||
|
return *s_processes;
|
||||||
|
}
|
||||||
|
|
||||||
ProcessID Process::allocate_pid()
|
ProcessID Process::allocate_pid()
|
||||||
{
|
{
|
||||||
// Overflow is UB, and negative PIDs wreck havoc.
|
// 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>>;
|
g_modules = new HashMap<String, OwnPtr<Module>>;
|
||||||
|
|
||||||
next_pid.store(0, AK::MemoryOrder::memory_order_release);
|
next_pid.store(0, AK::MemoryOrder::memory_order_release);
|
||||||
g_processes = new Process::List();
|
|
||||||
g_process_groups = new ProcessGroup::List();
|
g_process_groups = new ProcessGroup::List();
|
||||||
|
|
||||||
hostname().with_exclusive([&](auto& name) {
|
hostname().with_exclusive([&](auto& name) {
|
||||||
|
@ -85,20 +89,20 @@ Vector<ProcessID> Process::all_pids()
|
||||||
{
|
{
|
||||||
Vector<ProcessID> pids;
|
Vector<ProcessID> pids;
|
||||||
ScopedSpinLock lock(g_processes_lock);
|
ScopedSpinLock lock(g_processes_lock);
|
||||||
pids.ensure_capacity(g_processes->size_slow());
|
pids.ensure_capacity(processes().size_slow());
|
||||||
for (auto& process : *g_processes)
|
for (auto& process : processes())
|
||||||
pids.append(process.pid());
|
pids.append(process.pid());
|
||||||
return pids;
|
return pids;
|
||||||
}
|
}
|
||||||
|
|
||||||
NonnullRefPtrVector<Process> Process::all_processes()
|
NonnullRefPtrVector<Process> Process::all_processes()
|
||||||
{
|
{
|
||||||
NonnullRefPtrVector<Process> processes;
|
NonnullRefPtrVector<Process> output;
|
||||||
ScopedSpinLock lock(g_processes_lock);
|
ScopedSpinLock lock(g_processes_lock);
|
||||||
processes.ensure_capacity(g_processes->size_slow());
|
output.ensure_capacity(processes().size_slow());
|
||||||
for (auto& process : *g_processes)
|
for (auto& process : processes())
|
||||||
processes.append(NonnullRefPtr<Process>(process));
|
output.append(NonnullRefPtr<Process>(process));
|
||||||
return processes;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Process::in_group(gid_t gid) const
|
bool Process::in_group(gid_t gid) const
|
||||||
|
@ -147,7 +151,7 @@ void Process::register_new(Process& process)
|
||||||
RefPtr<Process> new_process = process;
|
RefPtr<Process> new_process = process;
|
||||||
{
|
{
|
||||||
ScopedSpinLock lock(g_processes_lock);
|
ScopedSpinLock lock(g_processes_lock);
|
||||||
g_processes->prepend(process);
|
processes().prepend(process);
|
||||||
}
|
}
|
||||||
ProcFSComponentRegistry::the().register_new_process(process);
|
ProcFSComponentRegistry::the().register_new_process(process);
|
||||||
}
|
}
|
||||||
|
@ -307,7 +311,7 @@ Process::~Process()
|
||||||
{
|
{
|
||||||
ScopedSpinLock processes_lock(g_processes_lock);
|
ScopedSpinLock processes_lock(g_processes_lock);
|
||||||
if (m_list_node.is_in_list())
|
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)
|
RefPtr<Process> Process::from_pid(ProcessID pid)
|
||||||
{
|
{
|
||||||
ScopedSpinLock lock(g_processes_lock);
|
ScopedSpinLock lock(g_processes_lock);
|
||||||
for (auto& process : *g_processes) {
|
for (auto& process : processes()) {
|
||||||
process.pid();
|
process.pid();
|
||||||
if (process.pid() == pid)
|
if (process.pid() == pid)
|
||||||
return &process;
|
return &process;
|
||||||
|
@ -690,7 +694,7 @@ void Process::die()
|
||||||
|
|
||||||
{
|
{
|
||||||
ScopedSpinLock lock(g_processes_lock);
|
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;
|
auto& process = *it;
|
||||||
++it;
|
++it;
|
||||||
if (process.has_tracee_thread(pid())) {
|
if (process.has_tracee_thread(pid())) {
|
||||||
|
|
|
@ -780,7 +780,7 @@ public:
|
||||||
using List = IntrusiveList<Process, RawPtr<Process>, &Process::m_list_node>;
|
using List = IntrusiveList<Process, RawPtr<Process>, &Process::m_list_node>;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern Process::List* g_processes;
|
Process::List& processes();
|
||||||
extern RecursiveSpinLock g_processes_lock;
|
extern RecursiveSpinLock g_processes_lock;
|
||||||
|
|
||||||
template<IteratorFunction<Process&> Callback>
|
template<IteratorFunction<Process&> Callback>
|
||||||
|
@ -788,7 +788,7 @@ inline void Process::for_each(Callback callback)
|
||||||
{
|
{
|
||||||
VERIFY_INTERRUPTS_DISABLED();
|
VERIFY_INTERRUPTS_DISABLED();
|
||||||
ScopedSpinLock lock(g_processes_lock);
|
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;
|
auto& process = *it;
|
||||||
++it;
|
++it;
|
||||||
if (callback(process) == IterationDecision::Break)
|
if (callback(process) == IterationDecision::Break)
|
||||||
|
@ -802,7 +802,7 @@ inline void Process::for_each_child(Callback callback)
|
||||||
VERIFY_INTERRUPTS_DISABLED();
|
VERIFY_INTERRUPTS_DISABLED();
|
||||||
ProcessID my_pid = pid();
|
ProcessID my_pid = pid();
|
||||||
ScopedSpinLock lock(g_processes_lock);
|
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;
|
auto& process = *it;
|
||||||
++it;
|
++it;
|
||||||
if (process.ppid() == my_pid || process.has_tracee_thread(pid())) {
|
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();
|
VERIFY_INTERRUPTS_DISABLED();
|
||||||
ScopedSpinLock lock(g_processes_lock);
|
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;
|
auto& process = *it;
|
||||||
++it;
|
++it;
|
||||||
if (!process.is_dead() && process.pgid() == pgid) {
|
if (!process.is_dead() && process.pgid() == pgid) {
|
||||||
|
|
|
@ -66,7 +66,7 @@ KResult Process::do_killall(int signal)
|
||||||
|
|
||||||
// Send the signal to all processes we have access to for.
|
// Send the signal to all processes we have access to for.
|
||||||
ScopedSpinLock lock(g_processes_lock);
|
ScopedSpinLock lock(g_processes_lock);
|
||||||
for (auto& process : *g_processes) {
|
for (auto& process : processes()) {
|
||||||
KResult res = KSuccess;
|
KResult res = KSuccess;
|
||||||
if (process.pid() == pid())
|
if (process.pid() == pid())
|
||||||
res = do_killself(signal);
|
res = do_killself(signal);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue