Kernel: Port Thread to ListedRefCounted

This commit is contained in:
Andreas Kling 2021-08-16 21:52:42 +02:00
parent c410f08c2b
commit 62719b85e0
Notes: sideshowbarker 2024-07-18 05:35:52 +09:00
2 changed files with 9 additions and 25 deletions

View file

@ -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;

View file

@ -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;