From e6d6e7521c9f1c597fc3813fb636bef4093a47f7 Mon Sep 17 00:00:00 2001 From: Eggert Jung Date: Mon, 23 Jun 2025 20:21:19 +0200 Subject: [PATCH] scheduler + 2 instances of application --- main.cc | 8 +++++--- thread/dispatcher.cc | 2 +- thread/dispatcher.h | 2 +- thread/scheduler.cc | 2 +- thread/scheduler.h | 13 +++++++------ thread/thread.cc | 11 ++++++++++- user/app1/appl.cc | 5 +++-- 7 files changed, 28 insertions(+), 15 deletions(-) diff --git a/main.cc b/main.cc index e49f722..d9dfbfa 100644 --- a/main.cc +++ b/main.cc @@ -71,10 +71,11 @@ OutputStream* copyout[Core::MAX]{ unsigned int testx, testy; -Context test1; uint8_t test1_stack[1024]; +uint8_t test2_stack[1024]; //Thread test1_thread = Thread(&test1_stack[sizeof(test1_stack)-1]); -Application application = Application(&test1_stack[sizeof(test1_stack)-1]); +Application application1 = Application(&test1_stack[sizeof(test1_stack)-1]); +Application application2 = Application(&test2_stack[sizeof(test2_stack)-1]); //Context test2; //uint8_t test2_stack[256]; @@ -127,7 +128,8 @@ extern "C" int main() { DBG << "Main CPU " << static_cast(LAPIC::getID()) << endl << flush; - sch.ready(&application); + sch.ready(&application1); + sch.ready(&application2); sch.schedule(); diff --git a/thread/dispatcher.cc b/thread/dispatcher.cc index 7ed5862..085d2ad 100644 --- a/thread/dispatcher.cc +++ b/thread/dispatcher.cc @@ -27,6 +27,6 @@ void Dispatcher::go(Thread *first) { } void Dispatcher::dispatch(Thread *next) { - lifePointer[Core::getID()] = next; + //lifePointer[Core::getID()] = next; lifePointer[Core::getID()]->resume(next); } diff --git a/thread/dispatcher.h b/thread/dispatcher.h index e3acce2..af291b9 100644 --- a/thread/dispatcher.h +++ b/thread/dispatcher.h @@ -24,7 +24,6 @@ class Dispatcher { */ void setActive(Thread* thread) { (void)thread; } - Thread* lifePointer[Core::MAX]; public: /*! \brief constructor @@ -32,6 +31,7 @@ class Dispatcher { * \todo(14) Implement Method */ Dispatcher(); + Thread* lifePointer[Core::MAX]; /*! \brief Returns the thread currently running on the CPU core calling * this method diff --git a/thread/scheduler.cc b/thread/scheduler.cc index ea66155..186ebdd 100644 --- a/thread/scheduler.cc +++ b/thread/scheduler.cc @@ -22,8 +22,8 @@ void Scheduler::ready(Thread* that) { } void Scheduler::resume(bool ready) { - if (ready) //if (!active()->kill_flag) + if (ready) readyList.enqueue(*active()); Thread* thread = readyList.dequeue(); assert(thread != nullptr); diff --git a/thread/scheduler.h b/thread/scheduler.h index 5261f7a..3ab9f44 100644 --- a/thread/scheduler.h +++ b/thread/scheduler.h @@ -22,12 +22,7 @@ * taken from the front of the queue. */ class Scheduler { - /*! \brief a Dispatcher object, providing the low level context switching - * routines. - */ - Dispatcher dispatcher; - - /*! \brief Helper to retrieve next Thread + /*! \brief Helper to retrieve next Thread * \return pointer of next thread */ Thread* getNext(); @@ -36,6 +31,12 @@ class Scheduler { public: Scheduler(); + /*! \brief a Dispatcher object, providing the low level context switching + * routines. + */ + Dispatcher dispatcher; + + /*! \brief Start scheduling * * This method starts the scheduling by removing the first thread from diff --git a/thread/thread.cc b/thread/thread.cc index 90146bb..8abfefe 100644 --- a/thread/thread.cc +++ b/thread/thread.cc @@ -1,6 +1,7 @@ // vim: set noet ts=4 sw=4: #include "thread.h" +#include "../debug/output.h" #include "../arch/context.h" // Alias to simplify stuff @@ -14,8 +15,16 @@ Thread::Thread(void* tos) { prepareContext(tos, context, (void(*)(void*)) &(kickoff), this); } +#include "../thread/scheduler.h" +extern Scheduler sch; void Thread::resume(Thread* next) { - context_switch(&next->context, &context); + Context *from = &sch.active()->context; + Context *to = &next->context; + + sch.dispatcher.lifePointer[Core::getID()] = next; + DBG << "from: " << hex << from << endl; + DBG << "to : " << hex << to << endl << flush; + context_switch(to, from); } void Thread::go() { diff --git a/user/app1/appl.cc b/user/app1/appl.cc index d3065e2..8ef0ea4 100644 --- a/user/app1/appl.cc +++ b/user/app1/appl.cc @@ -12,6 +12,7 @@ #include "../../thread/scheduler.h" extern Scheduler sch; +static uint8_t appl_cnt = 0; char text[] = "Ich mag\n\ Saftige Pflaumen voller Aroma\n\ @@ -40,13 +41,13 @@ void activeWaitDelay(uint64_t cycles) { void Application::action() { // NOLINT uint16_t cnt = 0; + uint8_t row = appl_cnt++; while (1) { //koutlock.lock(); { Guarded g = Guard::enter(); //g.vault(); - g.vault().kout.setPos((unsigned)0,(unsigned)Core::getID()*2+1); - + g.vault().kout.setPos((unsigned)10*row,(unsigned)Core::getID()*2+1); g.vault().kout << cnt++ << flush; //g.vault().kout << endl << flush;