Green Threads and Sleeping

I've spent my Parrot time for the last week working on something I call Alarms. Alarms are like Timers, except instead of specifying a delay the user specifies when they want the Alarm to fire. This is a pretty simple concept. If the Scheduler did what I wanted, I'd be done long ago.

So I had my initial version of Alarms all working and I was finishing up the the test script when I got a great idea: I should test how this interacts with the sleep opcode. Turns out it doesn't - sleep just calls usleep(), and since my OS timer handler resumes syscalls, that means Parrot sleeps right through the alarm.

It's pretty clear how I want this to work: Sleep should pack up the current continuation into an alarm so only the current Task / Green Thread goes to sleep. Then only if *everyone* is asleep should the process call something like pause(2).

In order to do that I'll have to solve the context switching problem: how do I stop the current Task / Green Thread and schedule the next one in the middle of an executing op? That was next on my list anyway. Here are some famous last words: This should be easy. I can do it from PIR no problem...