Kernel: Scheduler now uses the new LinkedList API for moving threads

Scheduler doesn't have to depend on the fact that allocations should
work when same amount of memory is just deallocated
This commit is contained in:
Bananymous 2024-02-01 14:22:08 +02:00
parent c4bf1641bd
commit c69efc040c
2 changed files with 52 additions and 60 deletions

View File

@ -44,34 +44,26 @@ namespace Kernel
BAN::ErrorOr<void> add_thread(Thread*);
private:
struct ActiveThread
struct SchedulerThread
{
ActiveThread(Thread* thread) : thread(thread) {}
Thread* thread;
uint64_t padding;
};
SchedulerThread(Thread* thread)
: thread(thread)
{}
struct SleepingThread
{
SleepingThread(Thread* thread, uint64_t wake_time) : thread(thread), wake_time(wake_time) {}
Thread* thread;
uint64_t wake_time;
};
struct BlockingThread
{
BlockingThread(Thread* thread, Semaphore* semaphore) : thread(thread), semaphore(semaphore) {}
Thread* thread;
Semaphore* semaphore;
uint8_t padding[sizeof(uint64_t) - sizeof(Semaphore*)];
union
{
uint64_t wake_time;
Semaphore* semaphore;
};
};
Thread* m_idle_thread { nullptr };
BAN::LinkedList<ActiveThread> m_active_threads;
BAN::LinkedList<SleepingThread> m_sleeping_threads;
BAN::LinkedList<BlockingThread> m_blocking_threads;
BAN::LinkedList<SchedulerThread> m_active_threads;
BAN::LinkedList<SchedulerThread> m_sleeping_threads;
BAN::LinkedList<SchedulerThread> m_blocking_threads;
BAN::LinkedList<ActiveThread>::iterator m_current_thread;
BAN::LinkedList<SchedulerThread>::iterator m_current_thread;
friend class Process;
};

View File

@ -5,7 +5,7 @@
#include <kernel/InterruptController.h>
#include <kernel/Process.h>
#include <kernel/Scheduler.h>
#include <kernel/Timer/PIT.h>
#include <kernel/Timer/Timer.h>
#define SCHEDULER_VERIFY_STACK 1
#define SCHEDULER_VERIFY_INTERRUPT_STATE 1
@ -116,12 +116,11 @@ namespace Kernel
uint64_t current_time = SystemTimer::get().ms_since_boot();
while (!m_sleeping_threads.empty() && m_sleeping_threads.front().wake_time <= current_time)
{
Thread* thread = m_sleeping_threads.front().thread;
m_sleeping_threads.remove(m_sleeping_threads.begin());
// This should work as we released enough memory from sleeping thread
static_assert(sizeof(ActiveThread) == sizeof(SleepingThread));
MUST(m_active_threads.emplace_back(thread));
m_sleeping_threads.move_element_to_other_linked_list(
m_active_threads,
m_active_threads.end(),
m_sleeping_threads.begin()
);
}
}
@ -286,23 +285,26 @@ namespace Kernel
ASSERT(m_current_thread);
Thread* sleeping = m_current_thread->thread;
if (save_current_thread())
{
ENABLE_INTERRUPTS();
return;
}
remove_and_advance_current_thread();
auto it = m_sleeping_threads.begin();
for (; it != m_sleeping_threads.end(); it++)
if (wake_time <= it->wake_time)
break;
// This should work as we released enough memory from active thread
static_assert(sizeof(ActiveThread) == sizeof(SleepingThread));
MUST(m_sleeping_threads.emplace(it, sleeping, wake_time));
m_current_thread->wake_time = wake_time;
m_active_threads.move_element_to_other_linked_list(
m_sleeping_threads,
it,
m_current_thread
);
m_current_thread = {};
advance_current_thread();
execute_current_thread();
ASSERT_NOT_REACHED();
@ -315,18 +317,21 @@ namespace Kernel
ASSERT(m_current_thread);
Thread* blocking = m_current_thread->thread;
if (save_current_thread())
{
ENABLE_INTERRUPTS();
return;
}
remove_and_advance_current_thread();
// This should work as we released enough memory from active thread
static_assert(sizeof(ActiveThread) == sizeof(BlockingThread));
MUST(m_blocking_threads.emplace_back(blocking, semaphore));
m_current_thread->semaphore = semaphore;
m_active_threads.move_element_to_other_linked_list(
m_blocking_threads,
m_blocking_threads.end(),
m_current_thread
);
m_current_thread = {};
advance_current_thread();
execute_current_thread();
ASSERT_NOT_REACHED();
@ -340,12 +345,11 @@ namespace Kernel
{
if (it->semaphore == semaphore)
{
auto thread = it->thread;
it = m_blocking_threads.remove(it);
// This should work as we released enough memory from active thread
static_assert(sizeof(ActiveThread) == sizeof(BlockingThread));
MUST(m_active_threads.emplace_back(thread));
it = m_blocking_threads.move_element_to_other_linked_list(
m_active_threads,
m_active_threads.end(),
it
);
}
else
{
@ -362,13 +366,11 @@ namespace Kernel
{
if (it->thread->tid() == tid)
{
Thread* thread = it->thread;
m_blocking_threads.remove(it);
// This should work as we released enough memory from active thread
static_assert(sizeof(ActiveThread) == sizeof(BlockingThread));
MUST(m_active_threads.emplace_back(thread));
m_blocking_threads.move_element_to_other_linked_list(
m_active_threads,
m_active_threads.end(),
it
);
return;
}
}
@ -377,13 +379,11 @@ namespace Kernel
{
if (it->thread->tid() == tid)
{
Thread* thread = it->thread;
m_sleeping_threads.remove(it);
// This should work as we released enough memory from active thread
static_assert(sizeof(ActiveThread) == sizeof(BlockingThread));
MUST(m_active_threads.emplace_back(thread));
m_sleeping_threads.move_element_to_other_linked_list(
m_active_threads,
m_active_threads.end(),
it
);
return;
}
}