Add a basic scheduler with threads #18

Merged
apio merged 19 commits from threads into restart 2022-12-07 16:11:59 +00:00
3 changed files with 17 additions and 5 deletions
Showing only changes of commit 021ea1063b - Show all commits

View File

@ -43,7 +43,7 @@ void Thread::set_arguments(u64 arg1, u64 arg2, u64 arg3, u64 arg4)
void switch_context(Thread* old_thread, Thread* new_thread, Registers* regs)
{
if (!old_thread->is_idle) memcpy(&old_thread->regs, regs, sizeof(Registers));
if (!old_thread->is_idle()) memcpy(&old_thread->regs, regs, sizeof(Registers));
memcpy(regs, &new_thread->regs, sizeof(Registers));
}

View File

@ -18,7 +18,7 @@ namespace Scheduler
g_idle.id = 0;
g_idle.init_regs_kernel();
g_idle.set_ip((u64)CPU::idle_loop);
g_idle.is_idle = true;
g_idle.state = ThreadState::Idle;
g_idle.ticks_left = 1;
@ -86,7 +86,7 @@ namespace Scheduler
Thread* pick_task()
{
Thread* old = g_current;
if (old->is_idle)
if (old->is_idle())
{
auto maybe_first = g_threads.last();
if (maybe_first.has_error()) // No threads!!
@ -111,7 +111,7 @@ namespace Scheduler
{
if (old_thread != new_thread) switch_context(old_thread, new_thread, regs);
if (new_thread->is_idle)
if (new_thread->is_idle())
{
new_thread->ticks_left = 1; // The idle task only runs for 1 tick so we can check for new runnable tasks
// as fast as possible.

View File

@ -10,6 +10,13 @@
#error "Unknown architecture."
#endif
enum class ThreadState
{
Idle,
Runnable,
Sleeping
};
struct Thread : public DoublyLinkedListNode<Thread>
{
Registers regs;
@ -22,7 +29,12 @@ struct Thread : public DoublyLinkedListNode<Thread>
u64 ticks_left;
bool is_idle = false;
ThreadState state = ThreadState::Runnable;
bool is_idle()
{
return state == ThreadState::Idle;
}
void init_regs_kernel();
void init_regs_user();