mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 03:08:11 +00:00
Kernel: Refactor thread scheduling a bit, breaking it into multiple lists.
There are now two thread lists, one for runnable threads and one for non- runnable threads. Thread::set_state() is responsible for moving threads between the lists. Each thread also has a back-pointer to the list it's currently in.
This commit is contained in:
parent
99dd60611f
commit
8c7d5abdc4
4 changed files with 104 additions and 25 deletions
|
@ -403,7 +403,15 @@ inline void Process::for_each_thread(Callback callback) const
|
||||||
{
|
{
|
||||||
InterruptDisabler disabler;
|
InterruptDisabler disabler;
|
||||||
pid_t my_pid = pid();
|
pid_t my_pid = pid();
|
||||||
for (auto* thread = g_threads->head(); thread;) {
|
for (auto* thread = g_runnable_threads->head(); thread;) {
|
||||||
|
auto* next_thread = thread->next();
|
||||||
|
if (thread->pid() == my_pid) {
|
||||||
|
if (callback(*thread) == IterationDecision::Abort)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
thread = next_thread;
|
||||||
|
}
|
||||||
|
for (auto* thread = g_nonrunnable_threads->head(); thread;) {
|
||||||
auto* next_thread = thread->next();
|
auto* next_thread = thread->next();
|
||||||
if (thread->pid() == my_pid) {
|
if (thread->pid() == my_pid) {
|
||||||
if (callback(*thread) == IterationDecision::Abort)
|
if (callback(*thread) == IterationDecision::Abort)
|
||||||
|
|
|
@ -72,7 +72,7 @@ bool Scheduler::pick_next()
|
||||||
auto now_usec = now.tv_usec;
|
auto now_usec = now.tv_usec;
|
||||||
|
|
||||||
// Check and unblock threads whose wait conditions have been met.
|
// Check and unblock threads whose wait conditions have been met.
|
||||||
Thread::for_each([&] (Thread& thread) {
|
Thread::for_each_nonrunnable([&] (Thread& thread) {
|
||||||
auto& process = thread.process();
|
auto& process = thread.process();
|
||||||
|
|
||||||
if (thread.state() == Thread::BlockedSleep) {
|
if (thread.state() == Thread::BlockedSleep) {
|
||||||
|
@ -223,8 +223,8 @@ bool Scheduler::pick_next()
|
||||||
});
|
});
|
||||||
|
|
||||||
#ifdef SCHEDULER_DEBUG
|
#ifdef SCHEDULER_DEBUG
|
||||||
dbgprintf("Scheduler choices:\n");
|
dbgprintf("Scheduler choices: (runnable threads: %p)\n", g_runnable_threads);
|
||||||
for (auto* thread = g_threads->head(); thread; thread = thread->next()) {
|
for (auto* thread = g_runnable_threads->head(); thread; thread = thread->next()) {
|
||||||
//if (process->state() == Thread::BlockedWait || process->state() == Thread::BlockedSleep)
|
//if (process->state() == Thread::BlockedWait || process->state() == Thread::BlockedSleep)
|
||||||
// continue;
|
// continue;
|
||||||
auto* process = &thread->process();
|
auto* process = &thread->process();
|
||||||
|
@ -232,11 +232,11 @@ bool Scheduler::pick_next()
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
auto* previous_head = g_threads->head();
|
auto* previous_head = g_runnable_threads->head();
|
||||||
for (;;) {
|
for (;;) {
|
||||||
// Move head to tail.
|
// Move head to tail.
|
||||||
g_threads->append(g_threads->remove_head());
|
g_runnable_threads->append(g_runnable_threads->remove_head());
|
||||||
auto* thread = g_threads->head();
|
auto* thread = g_runnable_threads->head();
|
||||||
|
|
||||||
if (!thread->process().is_being_inspected() && (thread->state() == Thread::Runnable || thread->state() == Thread::Running)) {
|
if (!thread->process().is_being_inspected() && (thread->state() == Thread::Runnable || thread->state() == Thread::Running)) {
|
||||||
#ifdef SCHEDULER_DEBUG
|
#ifdef SCHEDULER_DEBUG
|
||||||
|
|
|
@ -5,7 +5,18 @@
|
||||||
#include <Kernel/VM/MemoryManager.h>
|
#include <Kernel/VM/MemoryManager.h>
|
||||||
#include <LibC/signal_numbers.h>
|
#include <LibC/signal_numbers.h>
|
||||||
|
|
||||||
InlineLinkedList<Thread>* g_threads;
|
HashTable<Thread*>& thread_table()
|
||||||
|
{
|
||||||
|
ASSERT_INTERRUPTS_DISABLED();
|
||||||
|
static HashTable<Thread*>* table;
|
||||||
|
if (!table)
|
||||||
|
table = new HashTable<Thread*>;
|
||||||
|
return *table;
|
||||||
|
}
|
||||||
|
|
||||||
|
InlineLinkedList<Thread>* g_runnable_threads;
|
||||||
|
InlineLinkedList<Thread>* g_nonrunnable_threads;
|
||||||
|
|
||||||
static const dword default_kernel_stack_size = 16384;
|
static const dword default_kernel_stack_size = 16384;
|
||||||
static const dword default_userspace_stack_size = 65536;
|
static const dword default_userspace_stack_size = 65536;
|
||||||
|
|
||||||
|
@ -61,7 +72,9 @@ Thread::Thread(Process& process)
|
||||||
|
|
||||||
if (m_process.pid() != 0) {
|
if (m_process.pid() != 0) {
|
||||||
InterruptDisabler disabler;
|
InterruptDisabler disabler;
|
||||||
g_threads->prepend(this);
|
thread_table().set(this);
|
||||||
|
g_nonrunnable_threads->prepend(this);
|
||||||
|
m_thread_list = g_nonrunnable_threads;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,7 +84,9 @@ Thread::~Thread()
|
||||||
kfree_aligned(m_fpu_state);
|
kfree_aligned(m_fpu_state);
|
||||||
{
|
{
|
||||||
InterruptDisabler disabler;
|
InterruptDisabler disabler;
|
||||||
g_threads->remove(this);
|
if (m_thread_list)
|
||||||
|
m_thread_list->remove(this);
|
||||||
|
thread_table().remove(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (g_last_fpu_thread == this)
|
if (g_last_fpu_thread == this)
|
||||||
|
@ -85,11 +100,11 @@ void Thread::unblock()
|
||||||
{
|
{
|
||||||
m_blocked_descriptor = nullptr;
|
m_blocked_descriptor = nullptr;
|
||||||
if (current == this) {
|
if (current == this) {
|
||||||
m_state = Thread::Running;
|
set_state(Thread::Running);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ASSERT(m_state != Thread::Runnable && m_state != Thread::Running);
|
ASSERT(m_state != Thread::Runnable && m_state != Thread::Running);
|
||||||
m_state = Thread::Runnable;
|
set_state(Thread::Runnable);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Thread::snooze_until(Alarm& alarm)
|
void Thread::snooze_until(Alarm& alarm)
|
||||||
|
@ -509,7 +524,8 @@ KResult Thread::wait_for_connect(FileDescriptor& descriptor)
|
||||||
|
|
||||||
void Thread::initialize()
|
void Thread::initialize()
|
||||||
{
|
{
|
||||||
g_threads = new InlineLinkedList<Thread>;
|
g_runnable_threads = new InlineLinkedList<Thread>;
|
||||||
|
g_nonrunnable_threads = new InlineLinkedList<Thread>;
|
||||||
Scheduler::initialize();
|
Scheduler::initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -517,17 +533,26 @@ Vector<Thread*> Thread::all_threads()
|
||||||
{
|
{
|
||||||
Vector<Thread*> threads;
|
Vector<Thread*> threads;
|
||||||
InterruptDisabler disabler;
|
InterruptDisabler disabler;
|
||||||
for (auto* thread = g_threads->head(); thread; thread = thread->next())
|
threads.ensure_capacity(thread_table().size());
|
||||||
threads.append(thread);
|
for (auto* thread : thread_table())
|
||||||
|
threads.unchecked_append(thread);
|
||||||
return threads;
|
return threads;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Thread::is_thread(void* ptr)
|
bool Thread::is_thread(void* ptr)
|
||||||
{
|
{
|
||||||
ASSERT_INTERRUPTS_DISABLED();
|
ASSERT_INTERRUPTS_DISABLED();
|
||||||
for (auto* thread = g_threads->head(); thread; thread = thread->next()) {
|
return thread_table().contains((Thread*)ptr);
|
||||||
if (thread == ptr)
|
}
|
||||||
return true;
|
|
||||||
}
|
void Thread::set_state(State new_state)
|
||||||
return false;
|
{
|
||||||
|
m_state = new_state;
|
||||||
|
auto* new_thread_list = thread_list_for_state(new_state);
|
||||||
|
if (m_thread_list == new_thread_list)
|
||||||
|
return;
|
||||||
|
if (m_thread_list)
|
||||||
|
m_thread_list->remove(this);
|
||||||
|
new_thread_list->append(this);
|
||||||
|
m_thread_list = new_thread_list;
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,7 @@ class Alarm;
|
||||||
class FileDescriptor;
|
class FileDescriptor;
|
||||||
class Process;
|
class Process;
|
||||||
class Region;
|
class Region;
|
||||||
|
class Thread;
|
||||||
|
|
||||||
enum class ShouldUnblockThread { No = 0, Yes };
|
enum class ShouldUnblockThread { No = 0, Yes };
|
||||||
|
|
||||||
|
@ -24,6 +25,9 @@ struct SignalActionData {
|
||||||
int flags { 0 };
|
int flags { 0 };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
extern InlineLinkedList<Thread>* g_runnable_threads;
|
||||||
|
extern InlineLinkedList<Thread>* g_nonrunnable_threads;
|
||||||
|
|
||||||
class Thread : public InlineLinkedListNode<Thread> {
|
class Thread : public InlineLinkedListNode<Thread> {
|
||||||
friend class Process;
|
friend class Process;
|
||||||
friend class Scheduler;
|
friend class Scheduler;
|
||||||
|
@ -105,7 +109,7 @@ public:
|
||||||
dword kernel_stack_for_signal_handler_base() const { return m_kernel_stack_for_signal_handler_region ? m_kernel_stack_for_signal_handler_region->laddr().get() : 0; }
|
dword kernel_stack_for_signal_handler_base() const { return m_kernel_stack_for_signal_handler_region ? m_kernel_stack_for_signal_handler_region->laddr().get() : 0; }
|
||||||
|
|
||||||
void set_selector(word s) { m_far_ptr.selector = s; }
|
void set_selector(word s) { m_far_ptr.selector = s; }
|
||||||
void set_state(State s) { m_state = s; }
|
void set_state(State);
|
||||||
|
|
||||||
void send_signal(byte signal, Process* sender);
|
void send_signal(byte signal, Process* sender);
|
||||||
|
|
||||||
|
@ -129,10 +133,26 @@ public:
|
||||||
Thread* m_prev { nullptr };
|
Thread* m_prev { nullptr };
|
||||||
Thread* m_next { nullptr };
|
Thread* m_next { nullptr };
|
||||||
|
|
||||||
|
InlineLinkedList<Thread>* thread_list() { return m_thread_list; }
|
||||||
|
|
||||||
template<typename Callback> static void for_each_in_state(State, Callback);
|
template<typename Callback> static void for_each_in_state(State, Callback);
|
||||||
template<typename Callback> static void for_each_living(Callback);
|
template<typename Callback> static void for_each_living(Callback);
|
||||||
|
template<typename Callback> static void for_each_runnable(Callback);
|
||||||
|
template<typename Callback> static void for_each_nonrunnable(Callback);
|
||||||
template<typename Callback> static void for_each(Callback);
|
template<typename Callback> static void for_each(Callback);
|
||||||
|
|
||||||
|
static bool is_runnable_state(Thread::State state)
|
||||||
|
{
|
||||||
|
return state == Thread::State::Running || state == Thread::State::Runnable;
|
||||||
|
}
|
||||||
|
|
||||||
|
static InlineLinkedList<Thread>* thread_list_for_state(Thread::State state)
|
||||||
|
{
|
||||||
|
if (is_runnable_state(state))
|
||||||
|
return g_runnable_threads;
|
||||||
|
return g_nonrunnable_threads;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Process& m_process;
|
Process& m_process;
|
||||||
int m_tid { -1 };
|
int m_tid { -1 };
|
||||||
|
@ -158,13 +178,14 @@ private:
|
||||||
Vector<int> m_select_write_fds;
|
Vector<int> m_select_write_fds;
|
||||||
Vector<int> m_select_exceptional_fds;
|
Vector<int> m_select_exceptional_fds;
|
||||||
FPUState* m_fpu_state { nullptr };
|
FPUState* m_fpu_state { nullptr };
|
||||||
|
InlineLinkedList<Thread>* m_thread_list { nullptr };
|
||||||
State m_state { Invalid };
|
State m_state { Invalid };
|
||||||
bool m_select_has_timeout { false };
|
bool m_select_has_timeout { false };
|
||||||
bool m_has_used_fpu { false };
|
bool m_has_used_fpu { false };
|
||||||
bool m_was_interrupted_while_blocked { false };
|
bool m_was_interrupted_while_blocked { false };
|
||||||
};
|
};
|
||||||
|
|
||||||
extern InlineLinkedList<Thread>* g_threads;
|
HashTable<Thread*>& thread_table();
|
||||||
|
|
||||||
const char* to_string(Thread::State);
|
const char* to_string(Thread::State);
|
||||||
|
|
||||||
|
@ -172,7 +193,7 @@ template<typename Callback>
|
||||||
inline void Thread::for_each_in_state(State state, Callback callback)
|
inline void Thread::for_each_in_state(State state, Callback callback)
|
||||||
{
|
{
|
||||||
ASSERT_INTERRUPTS_DISABLED();
|
ASSERT_INTERRUPTS_DISABLED();
|
||||||
for (auto* thread = g_threads->head(); thread;) {
|
for (auto* thread = thread_list_for_state(state)->head(); thread;) {
|
||||||
auto* next_thread = thread->next();
|
auto* next_thread = thread->next();
|
||||||
if (thread->state() == state)
|
if (thread->state() == state)
|
||||||
callback(*thread);
|
callback(*thread);
|
||||||
|
@ -184,7 +205,13 @@ template<typename Callback>
|
||||||
inline void Thread::for_each_living(Callback callback)
|
inline void Thread::for_each_living(Callback callback)
|
||||||
{
|
{
|
||||||
ASSERT_INTERRUPTS_DISABLED();
|
ASSERT_INTERRUPTS_DISABLED();
|
||||||
for (auto* thread = g_threads->head(); thread;) {
|
for (auto* thread = g_runnable_threads->head(); thread;) {
|
||||||
|
auto* next_thread = thread->next();
|
||||||
|
if (thread->state() != Thread::State::Dead && thread->state() != Thread::State::Dying)
|
||||||
|
callback(*thread);
|
||||||
|
thread = next_thread;
|
||||||
|
}
|
||||||
|
for (auto* thread = g_nonrunnable_threads->head(); thread;) {
|
||||||
auto* next_thread = thread->next();
|
auto* next_thread = thread->next();
|
||||||
if (thread->state() != Thread::State::Dead && thread->state() != Thread::State::Dying)
|
if (thread->state() != Thread::State::Dead && thread->state() != Thread::State::Dying)
|
||||||
callback(*thread);
|
callback(*thread);
|
||||||
|
@ -196,7 +223,15 @@ template<typename Callback>
|
||||||
inline void Thread::for_each(Callback callback)
|
inline void Thread::for_each(Callback callback)
|
||||||
{
|
{
|
||||||
ASSERT_INTERRUPTS_DISABLED();
|
ASSERT_INTERRUPTS_DISABLED();
|
||||||
for (auto* thread = g_threads->head(); thread;) {
|
for_each_runnable(callback);
|
||||||
|
for_each_nonrunnable(callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Callback>
|
||||||
|
inline void Thread::for_each_runnable(Callback callback)
|
||||||
|
{
|
||||||
|
ASSERT_INTERRUPTS_DISABLED();
|
||||||
|
for (auto* thread = g_runnable_threads->head(); thread;) {
|
||||||
auto* next_thread = thread->next();
|
auto* next_thread = thread->next();
|
||||||
if (callback(*thread) == IterationDecision::Abort)
|
if (callback(*thread) == IterationDecision::Abort)
|
||||||
return;
|
return;
|
||||||
|
@ -204,3 +239,14 @@ inline void Thread::for_each(Callback callback)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename Callback>
|
||||||
|
inline void Thread::for_each_nonrunnable(Callback callback)
|
||||||
|
{
|
||||||
|
ASSERT_INTERRUPTS_DISABLED();
|
||||||
|
for (auto* thread = g_nonrunnable_threads->head(); thread;) {
|
||||||
|
auto* next_thread = thread->next();
|
||||||
|
if (callback(*thread) == IterationDecision::Abort)
|
||||||
|
return;
|
||||||
|
thread = next_thread;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue