Skip to content

Commit

Permalink
Kernel: Port Thread to ListedRefCounted
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomekling committed Aug 16, 2021
1 parent c410f08 commit 62719b8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 25 deletions.
20 changes: 3 additions & 17 deletions Kernel/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
});

Expand Down Expand Up @@ -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;
Expand Down
14 changes: 6 additions & 8 deletions Kernel/Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -131,7 +132,7 @@ struct ThreadRegisters {
};

class Thread
: public RefCountedBase
: public ListedRefCounted<Thread>
, public Weakable<Thread> {
AK_MAKE_NONCOPYABLE(Thread);
AK_MAKE_NONMOVABLE(Thread);
Expand All @@ -143,8 +144,6 @@ class Thread
friend struct ThreadReadyQueue;

public:
bool unref() const;

inline static Thread* current()
{
return Processor::current_thread();
Expand Down Expand Up @@ -1378,16 +1377,15 @@ class Thread
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);

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)
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 62719b8

Please sign in to comment.