From c1145d28f882c24ebfd2a8328318303caebd307b Mon Sep 17 00:00:00 2001 From: Eggert Jung Date: Tue, 17 Jun 2025 13:56:33 +0200 Subject: [PATCH] context switching between two functions --- main.cc | 31 ++++++++++++++++++++++--------- thread/thread.cc | 15 ++++++++------- thread/thread.h | 5 ++++- user/app1/appl.cc | 20 ++++++++++---------- 4 files changed, 44 insertions(+), 27 deletions(-) diff --git a/main.cc b/main.cc index cc82be0..973b948 100644 --- a/main.cc +++ b/main.cc @@ -33,10 +33,6 @@ Ticketlock koutlock; // TextStream(0 ,0 ,0, 0,false), //}; -Context test1; -uint8_t test1_stack[256]; -Application application = Application(test1_stack); - TextStream dout[Core::MAX] = { @@ -74,17 +70,32 @@ OutputStream* copyout[Core::MAX]{ unsigned int testx, testy; +Context test1; +uint8_t test1_stack[256]; +Thread test1_thread = Thread(&test1_stack[sizeof(test1_stack)-1]); +//Application application = Application(test1_stack); + +Context test2; +uint8_t test2_stack[256]; +Thread test2_thread = Thread(&test2_stack[sizeof(test2_stack)-1]); + void test_func1(){ while(1){ - DBG << "test 1\n"; - //context_switch(Context *next, Context *current); + { + Guarded g = Guard::enter(); + g.vault().kout << "test 1\n" << flush; + } + context_switch(&test2, &test1); } } void test_func2(){ while(1){ - DBG << "test 2\n"; - //context_switch(Context *next, Context *current); + { + Guarded g = Guard::enter(); + g.vault().kout << "test 2\n" << flush; + } + context_switch(&test1, &test2); } } @@ -115,7 +126,9 @@ extern "C" int main() { PS2Controller::drainBuffer(); DBG << "Main CPU " << static_cast(LAPIC::getID()) << endl << flush; - prepareContext(&test1_stack[sizeof(test1_stack)-1], test1, (void(*)(void*)) &(Application::kickoff),(Thread*)&application); + + prepareContext(&test1_stack[sizeof(test1_stack)-1], test1, (void(*)(void*)) &(test_func1)); + prepareContext(&test2_stack[sizeof(test2_stack)-1], test2, (void(*)(void*)) &(test_func2)); context_launch(&test1); diff --git a/thread/thread.cc b/thread/thread.cc index db40208..7761f68 100644 --- a/thread/thread.cc +++ b/thread/thread.cc @@ -6,17 +6,18 @@ typedef void (*kickoff_t)(void*); void Thread::kickoff(Thread* object) { - //Guard::leave(); - object->action(); } + object->action(); +} Thread::Thread(void* tos) { - volatile bool kill_flag; - Context context; - Thread* queue_link; } -void Thread::resume(Thread* next) { (void)next; } +void Thread::resume(Thread* next) { + +} -void Thread::go() {} +void Thread::go() { + +} void Thread::action() {} diff --git a/thread/thread.h b/thread/thread.h index 62be4b7..4fe032a 100644 --- a/thread/thread.h +++ b/thread/thread.h @@ -36,9 +36,12 @@ class Thread { * * \param object Thread to be started */ - /// \todo(14) Implement Method static void kickoff(Thread* object); + volatile bool kill_flag; + Context context; + Thread* queue_link; + public: /*! \brief Marker for a dying thread */ diff --git a/user/app1/appl.cc b/user/app1/appl.cc index 8216a72..507fcdd 100644 --- a/user/app1/appl.cc +++ b/user/app1/appl.cc @@ -35,17 +35,17 @@ void activeWaitDelay(uint64_t cycles) { void Application::action() { // NOLINT uint16_t cnt = 0; while (1) { - //koutlock.lock(); + //koutlock.lock(); { - Guarded g = Guard::enter(); - //g.vault(); - g.vault().kout.setPos((unsigned)0,(unsigned)Core::getID()*2+1); - - g.vault().kout << cnt++ << flush; - - //g.vault().kout << endl << flush; - //Guard::leave(); - //koutlock.unlock(); + Guarded g = Guard::enter(); + //g.vault(); + g.vault().kout.setPos((unsigned)0,(unsigned)Core::getID()*2+1); + + g.vault().kout << cnt++ << flush; + + //g.vault().kout << endl << flush; + //Guard::leave(); + //koutlock.unlock(); } Core::pause(); //activeWaitDelay(1000000000);