MultiTaskingRedesign
The legacy implementation of multitasking in translated euphoria code needs to be rethought. The way it uses the stack is apparently unsafe, and does not work on more recent operating systems (or at least, no one can figure out how to make them work). Also, translated and interpreted tasks cannot coexist, meaning that the front end of the interpreter cannot have any tasks (though it's hard to imagine a situation where they would be necessary), nor can a euphoria shared library use tasks with either a euphoria interpreted or translated main program.
The goal here is to solve the problem of translated tasks. Later, integration of tasking can be done, based on the redesign done here.
Rather than expanding the stack, and attempting to maintain the stack spaces for various stacks, the goal is to use native threads to implement the cooperative multitasking. The task scheduler and individual tasks can use a pairs of mutexes to ensure that only one task runs at a time. So even though we're not allowing true multi-threaded execution, we can still allow the operating system or native threading library to manage stack spaces for us.
Refactoring goals:
- Separate the code that calls translated and interpreted tasks. This means that we need to track which type of task each task is.
- Each task gets a pair of mutexes, initially owned by the task scheduler.
- Each task is created as a thread that calls a function in the back end, whose first action is to attempt to get ownership of one of the mutexes. When it gets ownership, it calls the euphoria routine (translated or interpreted) specified when the task was created.
- When a task yields, it first releases ownership of its mutex, and attempts to gain ownership of the other mutex.
- Upon yielding, the task scheduler will acquire ownership of both mutexes for a task, until it decides which task to schedule
- It may be necessary to have three mutexes, so that an any time either the scheduler or the task owns at least one mutex, and when the task owns two of the three, it executes, otherwise some other task executes.