Selaa lähdekoodia

Kernel: Port Process to ListedRefCounted

Idan Horowitz 3 vuotta sitten
vanhempi
commit
d7ec5d042f
4 muutettua tiedostoa jossa 14 lisäystä ja 36 poistoa
  1. 2 2
      Kernel/GlobalProcessExposed.cpp
  2. 6 26
      Kernel/Process.cpp
  3. 5 7
      Kernel/Process.h
  4. 1 1
      Kernel/Syscalls/kill.cpp

+ 2 - 2
Kernel/GlobalProcessExposed.cpp

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

+ 6 - 26
Kernel/Process.cpp

@@ -43,7 +43,7 @@ static void create_signal_trampoline();
 
 RecursiveSpinlock g_profiling_lock;
 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;
 
 static Singleton<MutexProtected<String>> s_hostname;
@@ -53,9 +53,9 @@ MutexProtected<String>& 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()
@@ -123,7 +123,7 @@ void Process::register_new(Process& process)
 {
     // Note: this is essentially the same like process->ref()
     RefPtr<Process> new_process = process;
-    processes().with([&](auto& list) {
+    all_instances().with([&](auto& list) {
         list.prepend(process);
     });
 }
@@ -268,26 +268,6 @@ Process::~Process()
     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:
 extern void signal_trampoline_dummy() __attribute__((used));
 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)
 {
-    return processes().with([&](const auto& list) -> RefPtr<Process> {
+    return all_instances().with([&](const auto& list) -> RefPtr<Process> {
         for (auto const& process : list) {
             if (process.pid() == pid)
                 return &process;
@@ -690,7 +670,7 @@ void Process::die()
         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();) {
             auto& process = *it;
             ++it;

+ 5 - 7
Kernel/Process.h

@@ -86,7 +86,7 @@ using FutexQueues = HashMap<FlatPtr, RefPtr<FutexQueue>>;
 struct LoadResult;
 
 class Process final
-    : public AK::RefCountedBase
+    : public ListedRefCounted<Process, LockType::Spinlock>
     , public Weakable<Process> {
 
     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 void register_new(Process&);
 
-    bool unref() const;
     ~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);
@@ -808,6 +807,7 @@ private:
 
 public:
     using List = IntrusiveListRelaxedConst<&Process::m_list_node>;
+    static SpinlockProtected<Process::List>& all_instances();
 };
 
 // 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;
 
-SpinlockProtected<Process::List>& processes();
-
 template<IteratorFunction<Process&> Callback>
 inline void Process::for_each(Callback callback)
 {
     VERIFY_INTERRUPTS_DISABLED();
-    processes().with([&](const auto& list) {
+    Process::all_instances().with([&](const auto& list) {
         for (auto it = list.begin(); it != list.end();) {
             auto& process = *it;
             ++it;
@@ -838,7 +836,7 @@ template<IteratorFunction<Process&> Callback>
 inline void Process::for_each_child(Callback callback)
 {
     ProcessID my_pid = pid();
-    processes().with([&](const auto& list) {
+    Process::all_instances().with([&](const auto& list) {
         for (auto it = list.begin(); it != list.end();) {
             auto& process = *it;
             ++it;
@@ -879,7 +877,7 @@ inline IterationDecision Process::for_each_thread(Callback callback)
 template<IteratorFunction<Process&> 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();) {
             auto& process = *it;
             ++it;

+ 1 - 1
Kernel/Syscalls/kill.cpp

@@ -65,7 +65,7 @@ ErrorOr<void> Process::do_killall(int signal)
     ErrorOr<void> error;
 
     // 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;
         if (process.pid() == pid())
             res = do_killself(signal);