From ec6226e561dcaa965106e30cc72f6b48b95c3de5 Mon Sep 17 00:00:00 2001 From: j2-tulloch <james2.tulloch@live.uwe.ac.uk> Date: Fri, 5 Jan 2024 14:48:25 +0000 Subject: [PATCH] Upload New File --- Assignment/Task 3/fiber.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Assignment/Task 3/fiber.cpp diff --git a/Assignment/Task 3/fiber.cpp b/Assignment/Task 3/fiber.cpp new file mode 100644 index 0000000..a42f833 --- /dev/null +++ b/Assignment/Task 3/fiber.cpp @@ -0,0 +1,35 @@ +#include "fiber.h" +#include "scheduler.h" +#include <cstring> +extern scheduler s; + +fiber::fiber(void (*function)(void*), void* data) : data_(data), is_finished(false) { + // Align the stack, set RIP, RSP, etc. + // Allocate the stack + std::memset(stack_.data(), 0, stack_.size()); + stack_bottom_ = stack_.data(); + stack_top_ = stack_bottom_ + stack_.size(); + + // Align the stack top to 16 bytes as per System V ABI requirements + stack_top_ = reinterpret_cast<char*>((reinterpret_cast<uintptr_t>(stack_top_) & -16L) - 128); + + // Set the context + context_.rip = reinterpret_cast<void*>(function); + context_.rsp = reinterpret_cast<void*>(stack_top_); + // Other registers (rbx, rbp, r12-r15) will be set when saving context +} + +fiber::~fiber() { + // Clean up, if needed +} + +Context* fiber::get_context() { + return &context_; +} + +void* fiber::get_data() const { + return data_; +} +void fiber::yield() { + s.yield(this); +} -- GitLab