Pelago
Concurrency

Concurrency

Tasks, channels, scopes, cancellation, select, locks and atomics, async functions and the runtime, one page each.

Concurrent code in Pelago builds on the ownership rules: a task owns what it is given, so tasks cannot share a changing value by accident. There are no data races to debug, because a value that two tasks could both change is rejected when the program is compiled.

FeatureWhat it doesPage
spawn, joinStart a task and wait for its result.Tasks
chan<T>(), send, recvPass values between tasks.Channels
scopeRun tasks that borrow from the surrounding code and wait for all of them.Scopes
cancel, cancelled(), yield_now()Cooperative stopping.Cancellation
select, timeout, time.sleepWait for several things at once, or give up after a time.select and timeouts
lock, atomic, semaphore, barrierShare state safely when channels are not enough.Sharing state
async fn, await, blockOnA function that runs as a task and gives a future.Async functions
coroutine, emitA generator: a function that produces values one at a time.Coroutines
Stacks, ending the program, deadlocksHow the runtime runs tasks.The runtime

The model in four rules

  1. A task is a closure that runs on a stack of its own. It can use only what it is given by its capture list: [sink x] moves a value in, [inout x] lets a scoped task change a variable.
  2. Values move between tasks through channels, or come back through join. Nothing is shared by accident.
  3. Stopping is cooperative: nothing interrupts a task and nothing unwinds.
  4. When every task is waiting and nothing can wake one, the program stops with a deadlock report instead of hanging.

On this page