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:

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)    // → 2

atomic/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)  // → 5

Async 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 detail

What we explicitly don't have

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.