Skip to content

Commit

Permalink
Kernel: Store whether a thread is the idle thread in Thread directly
Browse files Browse the repository at this point in the history
This solves a problem where checking whether a thread is an idle
thread may require iterating all processors if it is not the idle
thread of the current processor.
  • Loading branch information
tomuta authored and awesomekling committed May 4, 2021
1 parent 9a69b91 commit ec27cbb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
11 changes: 6 additions & 5 deletions Kernel/Arch/x86/CPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -779,11 +779,6 @@ class Processor {
return *m_mm_data;
}

ALWAYS_INLINE Thread* idle_thread() const
{
return m_idle_thread;
}

ALWAYS_INLINE void set_idle_thread(Thread& idle_thread)
{
m_idle_thread = &idle_thread;
Expand All @@ -806,6 +801,12 @@ class Processor {
write_fs_u32(__builtin_offsetof(Processor, m_current_thread), FlatPtr(&current_thread));
}

ALWAYS_INLINE static Thread* idle_thread()
{
// See comment in Processor::current_thread
return (Thread*)read_fs_u32(__builtin_offsetof(Processor, m_idle_thread));
}

ALWAYS_INLINE u32 get_id() const
{
// NOTE: This variant should only be used when iterating over all
Expand Down
12 changes: 6 additions & 6 deletions Kernel/Scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ RecursiveSpinLock g_scheduler_lock;
static u32 time_slice_for(const Thread& thread)
{
// One time slice unit == 4ms (assuming 250 ticks/second)
if (&thread == Processor::current().idle_thread())
if (thread.is_idle_thread())
return 1;
return 2;
}
Expand Down Expand Up @@ -105,12 +105,12 @@ Thread& Scheduler::pull_next_runnable_thread()
}
priority_mask &= ~(1u << priority);
}
return *Processor::current().idle_thread();
return *Processor::idle_thread();
}

bool Scheduler::dequeue_runnable_thread(Thread& thread, bool check_affinity)
{
if (&thread == Processor::current().idle_thread())
if (thread.is_idle_thread())
return true;
ScopedSpinLock lock(g_ready_queues_lock);
auto priority = thread.m_runnable_priority;
Expand All @@ -134,7 +134,7 @@ bool Scheduler::dequeue_runnable_thread(Thread& thread, bool check_affinity)
void Scheduler::queue_runnable_thread(Thread& thread)
{
VERIFY(g_scheduler_lock.own_lock());
if (&thread == Processor::current().idle_thread())
if (thread.is_idle_thread())
return;
auto priority = thread_priority_to_priority_index(thread.priority());

Expand All @@ -160,9 +160,8 @@ UNMAP_AFTER_INIT void Scheduler::start()
auto& processor = Processor::current();
processor.set_scheduler_data(*new SchedulerPerProcessorData());
VERIFY(processor.is_initialized());
auto& idle_thread = *processor.idle_thread();
auto& idle_thread = *Processor::idle_thread();
VERIFY(processor.current_thread() == &idle_thread);
VERIFY(processor.idle_thread() == &idle_thread);
idle_thread.set_ticks_left(time_slice_for(idle_thread));
idle_thread.did_schedule();
idle_thread.set_initialized(true);
Expand Down Expand Up @@ -467,6 +466,7 @@ UNMAP_AFTER_INIT void Scheduler::initialize()

UNMAP_AFTER_INIT void Scheduler::set_idle_thread(Thread* idle_thread)
{
idle_thread->set_idle_thread();
Processor::current().set_idle_thread(*idle_thread);
Processor::current().set_current_thread(*idle_thread);
}
Expand Down
3 changes: 3 additions & 0 deletions Kernel/Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,8 @@ class Thread
return m_handling_page_fault;
}
void set_handling_page_fault(bool b) { m_handling_page_fault = b; }
void set_idle_thread() { m_is_idle_thread = true; }
bool is_idle_thread() const { return m_is_idle_thread; }

private:
Thread(NonnullRefPtr<Process>, NonnullOwnPtr<Region> kernel_stack_region);
Expand Down Expand Up @@ -1248,6 +1250,7 @@ class Thread
bool m_should_die { false };
bool m_initialized { false };
bool m_in_block { false };
bool m_is_idle_thread { false };
Atomic<bool> m_have_any_unmasked_pending_signals { false };

void yield_without_holding_big_lock();
Expand Down

0 comments on commit ec27cbb

Please sign in to comment.