Concurrency
Task groups, atomics, mutexes, channels, actors — structured, and enforced at runtime.
The model
Your script runs on one thread until you say otherwise. Concurrency is explicit and structured: spawn functors into a task_group, or declare a functor async and await its handle. Five primitives coordinate shared state:
atomic(value)— a shareable cell.atomic/getsnapshots,atomic/setstores, and plain assignment to the binding writes through the wrapper.mutex(value)— a lock guarding a value.mutex/lock/mutex/unlock/mutex/try_lock, or the block formmutex/with(m) { ... }which releases on the way out.channel(capacity = 0)— FIFO handoff between tasks;send, blockingrecv, non-blockingtry_recv,close.actor(handler)— a message queue bound to a handler functor;sendenqueues,step/rundrain.task_group()— spawn, join, cancel, and clean up a set of related tasks as a unit.
The rule: in safe code, unsynchronized cross-task mutation is a runtime error, not a latent bug. Values carry concurrency markers (local / sendable / shareable); anything mutated from more than one task must live behind an atomic or a mutex, and mutable borrows cannot cross an async boundary at all.
Atomic counters
hits := atomic(0)
// Read a snapshot, write through the wrapper — both spellings store:
hits = atomic/get(hits) + 1
atomic/set(hits, atomic/get(hits) + 1)
print (atomic/get(hits) + 0) // → 2atomic/get returns a read-only snapshot — mutating it throws, which is the point: you can hand it to anything without aliasing the live cell. Assigning to the binding (hits = ...) dispatches through the wrapper and stays atomic.
Mutex for compound state
initial: map(string, string) := []
cache := mutex(initial)
put::(key: string, value: string)
{
mutex/with(cache)
{
// reads are const snapshots; take a mutable clone to edit
next: map(string, string) := mutable(clone(mutex/get(cache)))
next[key] = value
mutex/set(cache, next)
}
}
put("greeting", "hello")
print string(mutex/get(cache)/greeting) // → "hello"mutex/with(m) { ... } runs the block while holding the lock and releases it on the way out — including on a thrown error, so the lock cannot leak. Explicit mutex/lock / mutex/unlock exist when the critical section doesn't fit a block.
Structured task groups
// Fan out, fan in.
square::(x: int) { return x * x }
g := task_group()
g/spawn(square, 3)
g/spawn(square, 4)
rs := g/join_all(timeout_ms = 5000)
print rs[0]/ok // → true
print rs[0]/value + rs[1]/value // → 25
g/destroy_all()join_all returns one record per spawn with ready, ok, value, error, and cancellation and timeout status. join_any returns the first ready task with its index. Use timeout_ms to bound the join and abort_on_timeout = true to cancel queued work and interrupt running Elder work at cooperative checkpoints. Native work can continue beyond the deadline.
Channels and actors
// Channel: FIFO handoff. capacity = 0 means unbounded.
ch := channel(capacity = 4)
channel/send(ch, "one")
channel/send(ch, "two")
print channel/recv(ch) // → "one"
channel/close(ch)
// Actor: a queue bound to a handler.
seen := atomic(0)
handle_msg::(msg: int) { seen = atomic/get(seen) + msg }
a := actor(handle_msg)
actor/send(a, 2)
actor/send(a, 3)
actor/run(a) // drain the queue
print (atomic/get(seen) + 0) // → 5Async functors and await
// A functor typed `async` returns a task handle instead of blocking.
fetch_async: async
(x)
{
sleep(20)
return x + 1
}
t := fetch_async(41)
print await(t) // → 42; this/ok, this/value, this/error report detailWhat we explicitly don't have
- No callback-driven IO.
io/*calls block the calling task; concurrency comes from spawning more tasks, not from callback pyramids. Any functor can be spawned —asyncis a marker on the definition, not a color that infects every caller. - No silent sharing. Contexts are
localby default and cannot cross a task boundary; you opt values in withborrow/move/atomic/mutex, and the runtime checks it. - No green-thread mythology. Tasks run on a real worker pool. You can run a lot of them; the design goal is correctness under load, not a millions-of-fibers benchmark.
Serving traffic
The same rules carry into servers: under io/apie/serve every request handler is a task, so shared state in a handler must be atomic / mutex — the runtime rejects the unsynchronized write instead of corrupting it. For state that must outlive the process or span workers, use the persistent store — see IO & storage.