diff --git a/Assignment/Task 3/fiber.cpp b/Assignment/Task 3/fiber.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a42f83352ec3b529670765987711f6ee5a6fd997 --- /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); +}