Reputation: 39
TLDR; towards the bottom
I am trying to build a runtime with Green Threads for my language which are inspired by Golang's go routines and scheduler.
The runtime is written in C++ and I am trying to implement context switching for OS threads preemptively.
I am focused on preemptively switching mainly so that programmers don't have to worry about blocking threads. This would allow them to simply worry about the logic and not about other threads not getting appropriate CPU time.
The runtime already has cooperative scheduling, but for heavy computation tasks, these tasks might block the entire OS thread for a significant amount of time.
[tldr] I tried an implementation in which I performed context switching inside Interrupt Handlers but that would be really unsafe because of the nature of how the CPU sets up the stack + CPU bits for handling interrupts.
My question is
How can I implement context switching in C++ (linux) such that I am able to safely switch between contexts of execution tasks preemptively with near to no performance loss.
I only need an implementation of a thread being able to switch between two tasks preemptively, safely and efficiently.
Thanking you all in advance.
Upvotes: 0
Views: 96