std.task
Cooperative tasks: spawn a Coroutine<Wait>, then run() drives every task to its end.
A task settles its own errors — spawn takes the non-throwing coroutine type, because the
scheduler has nobody to hand an exception to. A panic anywhere ends the program, as panics
do.
Wait enum
pub enum Wait
What a yielded value asks the scheduler for.
Now
Run me again once everyone before me had a turn — the cooperative "make room".
Sleep(int)
Wake me after this many milliseconds. Sleepers wake in deadline order — two deadlines that pass in the same poll still wake nearest-first, and a deadline already in the past (a zero or negative wait) is due at the very next poll.
Readable(int)
Wake me when this descriptor has bytes to read. Descriptors come from std.io.net.
Writable(int)
Wake me when this descriptor can take bytes.
Interrupt
Wake me on an interrupt: Ctrl+C in a standalone program, or interrupt() from inside.
The clean-shutdown seat — a server parks a task here, and the wake is its signal to
close listeners and let run drain. Every parked task wakes on one interrupt.
stdlib/std/task.lyr:18
interrupt function
pub fn interrupt(): void
Wakes every task parked on Wait.Interrupt, exactly as Ctrl+C does — a server telling
itself to shut down: a task that decides "we are done" interrupts, the parked shutdown
task closes the listeners, and run drains. With nobody parked the interrupt is
REMEMBERED and taken by the next task to park — a signal, not a broadcast into the void.
stdlib/std/task.lyr:48
spawn function
pub fn spawn(task: Coroutine<Wait>): void
Adds a task to the back of the run queue. Nothing runs before run does; spawning from
inside a running task is ordinary and the new task gets its turn in the same round.
stdlib/std/task.lyr:98
pending function
pub fn pending(): int
How many tasks the scheduler holds, whatever they are waiting for. Zero after run
returns; useful to a host deciding whether another run is worth calling.
stdlib/std/task.lyr:114
run function
pub fn run(): void
Drives every task to its end and returns when none is left.
Ready tasks run round-robin, one step per turn. When none is ready, the scheduler blocks in
poll until the nearest deadline passes, a descriptor readies, or — with a task parked on
Wait.Interrupt — Ctrl+C arrives; the thread sleeps, it does not spin. A run holding ONLY
Interrupt tasks therefore waits, quietly and indefinitely, for the interrupt that is its
whole purpose.
stdlib/std/task.lyr:129