Cleaning up the Scheduler

In my work on Timers, I've concluded that the scheduler really is the right place for scheduling things (who would have guessed?). How the scheduler *should* work is described in pdd25. How the scheduler actually works is a bit different, and how I want the scheduler to work to implement green threads is a bit different from that...

In my vision, there are three types of Task that need to get scheduled:

  • Green Threads
  • Alarm Callbacks
  • Event Handlers

The Scheduler will manage a task queue. When a task is scheduled it is added to the queue. It goes on the end of the queue by default, but if it's marked as "immediate" then it goes at the front of the queue and triggers a reschedule (causing it to be run immediately).

When the queue is not empty, reschedules are triggered by an OS alarm every 10ms or so. When a reschedule is triggered, the current task is placed on the back of the queue and the task at the front of the queue is run/resumed.

Green Threads are the default kind of Task. They generally aren't marked as Immediate, so they can live on the end of the queue.

Alarms schedule something to run at a specific time or later. They're like timers, except they specify a target time rather than a delay. When an alarm is scheduled, it's added to an alarm queue and an OS alarm is set. Whenever an OS alarm triggers we schedule any Alarms that are due to run as immediate tasks and then reschedule.

Event Handlers are runnable code that handles some event pattern. They are registered, and then sit around waiting for events.

The scheduler maintains an event queue. New events are added to the queue, which sets a "process events" flag. When it's next convienient to handle events (which could be immediately after they're enqueued), all events are matched to handlers, those handler calls are scheduled as immediate tasks, and then it's time to reschedule.

The end result should be basically equivalent to what the current Scheduler claims to do, except that timers should work. Unless there's more to discover, and this is Parrot, so there probably is...