diff --git a/Assignment/Task 3/scheduler.cpp b/Assignment/Task 3/scheduler.cpp new file mode 100644 index 0000000000000000000000000000000000000000..00e1c2988189a4a26865fbf6087635c168388e8c --- /dev/null +++ b/Assignment/Task 3/scheduler.cpp @@ -0,0 +1,47 @@ +#include "scheduler.h" +#include <iostream> + +void scheduler::yield(fiber* f) { + get_context(f->get_context()); + + if (!f->finished()){ + fibers_.push_back(f); + } + set_context(&context_); +} + +scheduler::scheduler() { + // Initialization, if necessary +} + +scheduler::~scheduler() { + // Clean up, if necessary +} + +void scheduler::spawn(fiber* f) { + fibers_.push_back(f); +} + +void scheduler::do_it() { + std::cout << "Entering" << std::endl; + + + + get_context(&context_); // Save the scheduler's context + + while (!fibers_.empty()) { + fiber* currentFiber = fibers_.front(); + std::cout << "Switching fiber" << std::endl; + fibers_.pop_front(); // Remove the fiber from the queue + set_context(currentFiber->get_context()); // Switch to the fiber's context + + // After fiber finishes execution, control returns here + std::cout << " returning to scheduler" << std::endl; + } + std::cout << "Exiting" << std::endl; + } + +void scheduler::fiber_exit() { + set_context(&context_); // Switch back to the scheduler's context +} +