diff options
author | Andreas Kling <kling@serenityos.org> | 2021-08-16 21:52:42 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-08-17 01:21:47 +0200 |
commit | 62719b85e0373151b8a62f53da172776d493ebed (patch) | |
tree | 0d9c78a2b3c96f178c64133ee7775ae0b0dfff1a | |
parent | c410f08c2b0fad3aee0ba42c05bf4c0542d4a59d (diff) | |
download | serenity-62719b85e0373151b8a62f53da172776d493ebed.zip |
Kernel: Port Thread to ListedRefCounted
-rw-r--r-- | Kernel/Thread.cpp | 20 | ||||
-rw-r--r-- | Kernel/Thread.h | 14 |
2 files changed, 9 insertions, 25 deletions
diff --git a/Kernel/Thread.cpp b/Kernel/Thread.cpp index 506675abc7..d330ddc77c 100644 --- a/Kernel/Thread.cpp +++ b/Kernel/Thread.cpp @@ -32,25 +32,11 @@ namespace Kernel { static Singleton<SpinLockProtectedValue<Thread::GlobalList>> s_list; -SpinLockProtectedValue<Thread::GlobalList>& Thread::all_threads() +SpinLockProtectedValue<Thread::GlobalList>& 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<NonnullRefPtr<Thread>> Thread::try_create(NonnullRefPtr<Process> 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> process, NonnullOwnPtr<Memory::Region> 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<Process>) RefPtr<Thread> Thread::from_tid(ThreadID tid) { - return all_threads().with([&](auto& list) -> RefPtr<Thread> { + return Thread::all_instances().with([&](auto& list) -> RefPtr<Thread> { for (Thread& thread : list) { if (thread.tid() == tid) return thread; diff --git a/Kernel/Thread.h b/Kernel/Thread.h index 0f35f917d1..c78c13b6d0 100644 --- a/Kernel/Thread.h +++ b/Kernel/Thread.h @@ -24,6 +24,7 @@ #include <Kernel/Forward.h> #include <Kernel/KResult.h> #include <Kernel/KString.h> +#include <Kernel/Library/ListedRefCounted.h> #include <Kernel/Locking/LockLocation.h> #include <Kernel/Locking/LockMode.h> #include <Kernel/Locking/SpinLockProtectedValue.h> @@ -131,7 +132,7 @@ struct ThreadRegisters { }; class Thread - : public RefCountedBase + : public ListedRefCounted<Thread> , public Weakable<Thread> { 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 @@ public: using ListInProcess = IntrusiveList<Thread, RawPtr<Thread>, &Thread::m_process_thread_list_node>; using GlobalList = IntrusiveList<Thread, RawPtr<Thread>, &Thread::m_global_thread_list_node>; -private: - static SpinLockProtectedValue<GlobalList>& all_threads(); + static SpinLockProtectedValue<GlobalList>& all_instances(); }; AK_ENUM_BITWISE_OPERATORS(Thread::FileBlocker::BlockFlags); @@ -1387,7 +1385,7 @@ AK_ENUM_BITWISE_OPERATORS(Thread::FileBlocker::BlockFlags); template<IteratorFunction<Thread&> 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<IteratorFunction<Thread&> 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<VoidFunction<Thread&> 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; |