Now that I have pre-emptive task switching between green threads basically working, I need to figure out what the API to interact with them wants to look like. Additionally, I need to clean up this scheduler thing that I've mauled in order to make the API and terminology consistent.
I'm going to call a green thread a Task (what Parrot used to call a Task is now called an Event - Task was too good a word for this apparently unused feature). I plan to implement the following Task API:
- Create a task (new 'Task'; schedule opcode)
- List all tasks (new 'Scheduler'; active_tasks method)
Anyone with a task handle can:
- Send it a message (Task#send)
- Wait for it to finish (wait opcode)
- Kill it (Task#kill)
The active task can:
- Exit (end opcode)
- Yield the rest of its quantum to the next task in the queue (pass opcode)
- Receive a message (receive opcode)
Events probably won't be cleaned up and made to work, but if they were it would look something like this:
- Register an EventHandler
- Send an Event for handling
When an event gets shipped, it's matched to an appropriate handler and that combination gets scheduled as a Task.