1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 19:57:41 +00:00

Kernel: Port Process to ListedRefCounted

This commit is contained in:
Idan Horowitz 2021-12-29 00:46:21 +02:00 committed by Andreas Kling
parent 3d0b5efcfc
commit d7ec5d042f
4 changed files with 14 additions and 36 deletions

View file

@ -534,7 +534,7 @@ private:
{ {
auto array = json.add_array("processes"); auto array = json.add_array("processes");
build_process(array, *Scheduler::colonel()); build_process(array, *Scheduler::colonel());
processes().with([&](auto& processes) { Process::all_instances().with([&](auto& processes) {
for (auto& process : processes) for (auto& process : processes)
build_process(array, process); build_process(array, process);
}); });
@ -942,7 +942,7 @@ ErrorOr<void> ProcFSRootDirectory::traverse_as_directory(FileSystemID fsid, Func
TRY(callback({ component.name(), identifier, 0 })); TRY(callback({ component.name(), identifier, 0 }));
} }
return processes().with([&](auto& list) -> ErrorOr<void> { return Process::all_instances().with([&](auto& list) -> ErrorOr<void> {
for (auto& process : list) { for (auto& process : list) {
VERIFY(!(process.pid() < 0)); VERIFY(!(process.pid() < 0));
u64 process_id = (u64)process.pid().value(); u64 process_id = (u64)process.pid().value();

View file

@ -43,7 +43,7 @@ static void create_signal_trampoline();
RecursiveSpinlock g_profiling_lock; RecursiveSpinlock g_profiling_lock;
static Atomic<pid_t> next_pid; static Atomic<pid_t> next_pid;
static Singleton<SpinlockProtected<Process::List>> s_processes; static Singleton<SpinlockProtected<Process::List>> s_all_instances;
READONLY_AFTER_INIT Memory::Region* g_signal_trampoline_region; READONLY_AFTER_INIT Memory::Region* g_signal_trampoline_region;
static Singleton<MutexProtected<String>> s_hostname; static Singleton<MutexProtected<String>> s_hostname;
@ -53,9 +53,9 @@ MutexProtected<String>& hostname()
return *s_hostname; return *s_hostname;
} }
SpinlockProtected<Process::List>& processes() SpinlockProtected<Process::List>& Process::all_instances()
{ {
return *s_processes; return *s_all_instances;
} }
ProcessID Process::allocate_pid() ProcessID Process::allocate_pid()
@ -123,7 +123,7 @@ void Process::register_new(Process& process)
{ {
// Note: this is essentially the same like process->ref() // Note: this is essentially the same like process->ref()
RefPtr<Process> new_process = process; RefPtr<Process> new_process = process;
processes().with([&](auto& list) { all_instances().with([&](auto& list) {
list.prepend(process); list.prepend(process);
}); });
} }
@ -268,26 +268,6 @@ Process::~Process()
PerformanceManager::add_process_exit_event(*this); PerformanceManager::add_process_exit_event(*this);
} }
bool Process::unref() const
{
// NOTE: We need to obtain the process list lock before doing anything,
// because otherwise someone might get in between us lowering the
// refcount and acquiring the lock.
auto did_hit_zero = processes().with([&](auto& list) {
auto new_ref_count = deref_base();
if (new_ref_count > 0)
return false;
if (m_list_node.is_in_list())
list.remove(*const_cast<Process*>(this));
return true;
});
if (did_hit_zero)
delete this;
return did_hit_zero;
}
// Make sure the compiler doesn't "optimize away" this function: // Make sure the compiler doesn't "optimize away" this function:
extern void signal_trampoline_dummy() __attribute__((used)); extern void signal_trampoline_dummy() __attribute__((used));
void signal_trampoline_dummy() void signal_trampoline_dummy()
@ -390,7 +370,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)
{ {
return processes().with([&](const auto& list) -> RefPtr<Process> { return all_instances().with([&](const auto& list) -> RefPtr<Process> {
for (auto const& process : list) { for (auto const& process : list) {
if (process.pid() == pid) if (process.pid() == pid)
return &process; return &process;
@ -690,7 +670,7 @@ void Process::die()
m_threads_for_coredump.append(thread); m_threads_for_coredump.append(thread);
}); });
processes().with([&](const auto& list) { all_instances().with([&](const auto& list) {
for (auto it = list.begin(); it != list.end();) { for (auto it = list.begin(); it != list.end();) {
auto& process = *it; auto& process = *it;
++it; ++it;

View file

@ -86,7 +86,7 @@ using FutexQueues = HashMap<FlatPtr, RefPtr<FutexQueue>>;
struct LoadResult; struct LoadResult;
class Process final class Process final
: public AK::RefCountedBase : public ListedRefCounted<Process, LockType::Spinlock>
, public Weakable<Process> { , public Weakable<Process> {
class ProtectedValues { class ProtectedValues {
@ -183,7 +183,6 @@ public:
static ErrorOr<NonnullRefPtr<Process>> try_create_user_process(RefPtr<Thread>& first_thread, StringView path, UserID, GroupID, NonnullOwnPtrVector<KString> arguments, NonnullOwnPtrVector<KString> environment, TTY*); static ErrorOr<NonnullRefPtr<Process>> try_create_user_process(RefPtr<Thread>& first_thread, StringView path, UserID, GroupID, NonnullOwnPtrVector<KString> arguments, NonnullOwnPtrVector<KString> environment, TTY*);
static void register_new(Process&); static void register_new(Process&);
bool unref() const;
~Process(); ~Process();
RefPtr<Thread> create_kernel_thread(void (*entry)(void*), void* entry_data, u32 priority, NonnullOwnPtr<KString> name, u32 affinity = THREAD_AFFINITY_DEFAULT, bool joinable = true); RefPtr<Thread> create_kernel_thread(void (*entry)(void*), void* entry_data, u32 priority, NonnullOwnPtr<KString> name, u32 affinity = THREAD_AFFINITY_DEFAULT, bool joinable = true);
@ -808,6 +807,7 @@ private:
public: public:
using List = IntrusiveListRelaxedConst<&Process::m_list_node>; using List = IntrusiveListRelaxedConst<&Process::m_list_node>;
static SpinlockProtected<Process::List>& all_instances();
}; };
// Note: Process object should be 2 pages of 4096 bytes each. // Note: Process object should be 2 pages of 4096 bytes each.
@ -818,13 +818,11 @@ static_assert(AssertSize<Process, (PAGE_SIZE * 2)>());
extern RecursiveSpinlock g_profiling_lock; extern RecursiveSpinlock g_profiling_lock;
SpinlockProtected<Process::List>& processes();
template<IteratorFunction<Process&> Callback> template<IteratorFunction<Process&> Callback>
inline void Process::for_each(Callback callback) inline void Process::for_each(Callback callback)
{ {
VERIFY_INTERRUPTS_DISABLED(); VERIFY_INTERRUPTS_DISABLED();
processes().with([&](const auto& list) { Process::all_instances().with([&](const auto& list) {
for (auto it = list.begin(); it != list.end();) { for (auto it = list.begin(); it != list.end();) {
auto& process = *it; auto& process = *it;
++it; ++it;
@ -838,7 +836,7 @@ template<IteratorFunction<Process&> Callback>
inline void Process::for_each_child(Callback callback) inline void Process::for_each_child(Callback callback)
{ {
ProcessID my_pid = pid(); ProcessID my_pid = pid();
processes().with([&](const auto& list) { Process::all_instances().with([&](const auto& list) {
for (auto it = list.begin(); it != list.end();) { for (auto it = list.begin(); it != list.end();) {
auto& process = *it; auto& process = *it;
++it; ++it;
@ -879,7 +877,7 @@ inline IterationDecision Process::for_each_thread(Callback callback)
template<IteratorFunction<Process&> Callback> template<IteratorFunction<Process&> Callback>
inline void Process::for_each_in_pgrp(ProcessGroupID pgid, Callback callback) inline void Process::for_each_in_pgrp(ProcessGroupID pgid, Callback callback)
{ {
processes().with([&](const auto& list) { Process::all_instances().with([&](const auto& list) {
for (auto it = list.begin(); it != list.end();) { for (auto it = list.begin(); it != list.end();) {
auto& process = *it; auto& process = *it;
++it; ++it;

View file

@ -65,7 +65,7 @@ ErrorOr<void> Process::do_killall(int signal)
ErrorOr<void> error; ErrorOr<void> error;
// Send the signal to all processes we have access to for. // Send the signal to all processes we have access to for.
processes().for_each([&](auto& process) { Process::all_instances().for_each([&](auto& process) {
ErrorOr<void> res; ErrorOr<void> res;
if (process.pid() == pid()) if (process.pid() == pid())
res = do_killself(signal); res = do_killself(signal);