From b3634c9c4bb1c939b417c586dc20386b02c95032 Mon Sep 17 00:00:00 2001
From: j2-tulloch <james2.tulloch@live.uwe.ac.uk>
Date: Fri, 5 Jan 2024 14:45:22 +0000
Subject: [PATCH] Upload New File

---
 Assignment/Task 2/scheduler.cpp | 36 +++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
 create mode 100644 Assignment/Task 2/scheduler.cpp

diff --git a/Assignment/Task 2/scheduler.cpp b/Assignment/Task 2/scheduler.cpp
new file mode 100644
index 0000000..f1ceb23
--- /dev/null
+++ b/Assignment/Task 2/scheduler.cpp	
@@ -0,0 +1,36 @@
+#include "scheduler.h"
+#include <iostream>
+
+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
+}
+
-- 
GitLab