diff --git a/Kernel/Thread.cpp b/Kernel/Thread.cpp index 506675abc71b02..d330ddc77cba7c 100644 --- a/Kernel/Thread.cpp +++ b/Kernel/Thread.cpp @@ -32,25 +32,11 @@ namespace Kernel { static Singleton> s_list; -SpinLockProtectedValue& Thread::all_threads() +SpinLockProtectedValue& Thread::all_instances() { return *s_list; } -bool Thread::unref() const -{ - bool did_hit_zero = all_threads().with([&](auto&) { - if (deref_base()) - return false; - m_global_thread_list_node.remove(); - return true; - }); - - if (did_hit_zero) - delete this; - return did_hit_zero; -} - KResultOr> Thread::try_create(NonnullRefPtr process) { auto kernel_stack_region = MM.allocate_kernel_region(default_kernel_stack_size, {}, Memory::Region::Access::ReadWrite, AllocationStrategy::AllocateNow); @@ -91,7 +77,7 @@ Thread::Thread(NonnullRefPtr process, NonnullOwnPtr ker m_kernel_stack_region->set_name(KString::try_create(string)); } - all_threads().with([&](auto& list) { + Thread::all_instances().with([&](auto& list) { list.append(*this); }); @@ -1258,7 +1244,7 @@ KResult Thread::make_thread_specific_region(Badge) RefPtr Thread::from_tid(ThreadID tid) { - return all_threads().with([&](auto& list) -> RefPtr { + return Thread::all_instances().with([&](auto& list) -> RefPtr { for (Thread& thread : list) { if (thread.tid() == tid) return thread; diff --git a/Kernel/Thread.h b/Kernel/Thread.h index 0f35f917d1ade7..c78c13b6d043dd 100644 --- a/Kernel/Thread.h +++ b/Kernel/Thread.h @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -131,7 +132,7 @@ struct ThreadRegisters { }; class Thread - : public RefCountedBase + : public ListedRefCounted , public Weakable { AK_MAKE_NONCOPYABLE(Thread); AK_MAKE_NONMOVABLE(Thread); @@ -143,8 +144,6 @@ class Thread friend struct ThreadReadyQueue; public: - bool unref() const; - inline static Thread* current() { return Processor::current_thread(); @@ -1378,8 +1377,7 @@ class Thread using ListInProcess = IntrusiveList, &Thread::m_process_thread_list_node>; using GlobalList = IntrusiveList, &Thread::m_global_thread_list_node>; -private: - static SpinLockProtectedValue& all_threads(); + static SpinLockProtectedValue& all_instances(); }; AK_ENUM_BITWISE_OPERATORS(Thread::FileBlocker::BlockFlags); @@ -1387,7 +1385,7 @@ AK_ENUM_BITWISE_OPERATORS(Thread::FileBlocker::BlockFlags); template Callback> inline IterationDecision Thread::for_each(Callback callback) { - return all_threads().with([&](auto& list) -> IterationDecision { + return Thread::all_instances().with([&](auto& list) -> IterationDecision { for (auto& thread : list) { IterationDecision decision = callback(thread); if (decision != IterationDecision::Continue) @@ -1400,7 +1398,7 @@ inline IterationDecision Thread::for_each(Callback callback) template Callback> inline IterationDecision Thread::for_each_in_state(State state, Callback callback) { - return all_threads().with([&](auto& list) -> IterationDecision { + return Thread::all_instances().with([&](auto& list) -> IterationDecision { for (auto& thread : list) { if (thread.state() != state) continue; @@ -1415,7 +1413,7 @@ inline IterationDecision Thread::for_each_in_state(State state, Callback callbac template Callback> inline IterationDecision Thread::for_each(Callback callback) { - return all_threads().with([&](auto& list) { + return Thread::all_instances().with([&](auto& list) { for (auto& thread : list) { if (callback(thread) == IterationDecision::Break) return IterationDecision::Break;