From d3b9b937fbcc63519d3fb2b86b517d04af78ac5f Mon Sep 17 00:00:00 2001 From: Christoph Urlacher Date: Mon, 23 May 2022 09:11:53 +0200 Subject: [PATCH] implement c coroutine --- c_os/kernel/corouts/Coroutine.cc | 12 +++++++++--- c_os/kernel/corouts/Coroutine.h | 31 +++++++++++++++---------------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/c_os/kernel/corouts/Coroutine.cc b/c_os/kernel/corouts/Coroutine.cc index 2acc045..e6b0712 100755 --- a/c_os/kernel/corouts/Coroutine.cc +++ b/c_os/kernel/corouts/Coroutine.cc @@ -49,7 +49,7 @@ void Coroutine_init(struct CoroutineState* regs, unsigned int* stack, void (*kic // wird, sie darf also nicht terminieren, sonst kracht's. *(--sp) = (unsigned int*)object; // Parameter - *(--sp) = (unsigned int*)0x131155; // Ruecksprungadresse + *(--sp) = (unsigned int*)0x131155; // Ruecksprungadresse (Dummy) // Nun legen wir noch die Adresse der Funktion "kickoff" ganz oben auf // den Stack. Wenn dann bei der ersten Aktivierung dieser Koroutine der @@ -67,7 +67,7 @@ void Coroutine_init(struct CoroutineState* regs, unsigned int* stack, void (*kic regs->esi = 0; regs->edi = 0; regs->ebp = 0; - regs->esp = sp; + regs->esp = sp; // esp now points to the location of the address of kickoff } /***************************************************************************** @@ -100,13 +100,15 @@ Coroutine::Coroutine(unsigned int* stack) { } /***************************************************************************** - * Methode: Coroutine::switchToNext * + * Methode: Coroutine::switchToNext * *---------------------------------------------------------------------------* * Beschreibung: Auf die nächste Koroutine umschalten. * *****************************************************************************/ void Coroutine::switchToNext() { /* hier muss Code eingefügt werden */ + + Coroutine_switch(&this->regs, &((Coroutine*)this->next)->regs); } /***************************************************************************** @@ -117,6 +119,8 @@ void Coroutine::switchToNext() { void Coroutine::start() { /* hier muss Code eingefügt werden */ + + Coroutine_start(&this->regs); } /***************************************************************************** @@ -127,4 +131,6 @@ void Coroutine::start() { void Coroutine::setNext(Chain* next) { /* hier muss Code eingefügt werden */ + + this->next = next; } diff --git a/c_os/kernel/corouts/Coroutine.h b/c_os/kernel/corouts/Coroutine.h index 3a6edfb..7d5fed5 100755 --- a/c_os/kernel/corouts/Coroutine.h +++ b/c_os/kernel/corouts/Coroutine.h @@ -27,28 +27,27 @@ #include "lib/Chain.h" class Coroutine : public Chain { - + private: - Coroutine(const Coroutine ©); // Verhindere Kopieren + Coroutine(const Coroutine& copy); // Verhindere Kopieren private: struct CoroutineState regs; public: - Coroutine (unsigned int* stack); - - // Coroutine aktivieren - void start (); - - // Auf die naechste Coroutine umschalten - void switchToNext (); - - // Methode der Coroutine, muss in Sub-Klasse implementiert werden - virtual void run () = 0; - - // Verweis auf nächste Coroutine setzen - void setNext (Chain* next); + Coroutine(unsigned int* stack); - }; + // Coroutine aktivieren + void start(); + + // Auf die naechste Coroutine umschalten + void switchToNext(); + + // Methode der Coroutine, muss in Sub-Klasse implementiert werden + virtual void run() = 0; + + // Verweis auf nächste Coroutine setzen + void setNext(Chain* next); +}; #endif