From 48fe2d4b04b06d3c734ab9112fbfa97c9aff9dc3 Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 7 Dec 2022 15:04:11 +0100 Subject: [PATCH] Initialize and demo the scheduler --- kernel/src/main.cpp | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp index 33f7410e..7016296d 100644 --- a/kernel/src/main.cpp +++ b/kernel/src/main.cpp @@ -1,12 +1,35 @@ #include "Log.h" #include "arch/CPU.h" +#include "arch/Serial.h" #include "arch/Timer.h" #include "boot/Init.h" #include "config.h" #include "memory/MemoryManager.h" +#include "thread/Scheduler.h" #include #include +void print_in_loop() +{ + while (true) + { + CPU::disable_interrupts(); + Serial::printf("%lu", Scheduler::current()->id); + CPU::enable_interrupts(); + } +} + +void print_one_and_then_yield() +{ + while (true) + { + CPU::disable_interrupts(); + Serial::printf("%lu", Scheduler::current()->id); + kernel_yield(); + CPU::enable_interrupts(); + } +} + Result init() { kinfoln("Starting Moon %s", MOON_VERSION); @@ -17,10 +40,6 @@ Result init() Timer::init(); - CPU::platform_finish_init(); - - CPU::enable_interrupts(); - char buffer[64]; to_dynamic_unit(MemoryManager::total(), buffer, sizeof(buffer)); kinfoln("Total memory: %s", buffer); @@ -31,6 +50,20 @@ Result init() to_dynamic_unit(MemoryManager::reserved(), buffer, sizeof(buffer)); kinfoln("Reserved memory: %s", buffer); + Scheduler::init(); + + TRY(Scheduler::new_kernel_thread(print_in_loop)); + TRY(Scheduler::new_kernel_thread(print_in_loop)); + TRY(Scheduler::new_kernel_thread(print_in_loop)); + TRY(Scheduler::new_kernel_thread(print_in_loop)); + TRY(Scheduler::new_kernel_thread(print_in_loop)); + TRY(Scheduler::new_kernel_thread(print_in_loop)); + TRY(Scheduler::new_kernel_thread(print_one_and_then_yield)); + + CPU::platform_finish_init(); + + CPU::enable_interrupts(); + return {}; } @@ -40,5 +73,5 @@ extern "C" [[noreturn]] void _start() Init::early_init(); auto rc = init(); if (rc.has_error()) kerrorln("Runtime error: %s", rc.error_string()); - CPU::efficient_halt(); + CPU::idle_loop(); } \ No newline at end of file