Skip to the content
Diaspar

Decision D035 · accepted in part; the runtime proposed; cowns an experiment

Concurrency model: tasks at the core, state in cells, actors in a library

A record of the language's design, as it stood on 2026-11-18: written for the people who build Diaspar, and kept as it was written. Its code is a sketch, not a program of the site, and an older record may show syntax that a later one changed. It names files of the language's source, which is not public yet.

Status: accepted (2026-11-17) for tasks as the core, the layers, actors and supervisors as a library, bounded channels only, behaviours and actors that are not reentrant, and the sim scheduler whose scheduling points are events of the language; proposed for the rest; proposed as an experiment for std.cown

Research: reports/Concurrency models for Diaspar, with its notes. Examples, semantics and the runtime: design/concurrency.md.

Decision

D006 made structured tasks the way to do things at once. This decision places everything else around them. A concurrent program answers four separate questions, and Diaspar has one tool for each:

Question Tool Who writes it
How does code wait without holding an OS thread? fibers nobody: the runtime, under every task, actor and behaviour
How long does work live, and where do its errors go? scope, spawn, join (D006) every program that does two things at once
Who owns state that changes? an atom (D027), an actor (std.actor), a cown (std.cown, an experiment) rarely
How does a stream of data flow between tasks? a bounded channel (std.chan) pipelines, producers and consumers

Choosing the owner of state:

  • One cell, changed by a pure function: an atom. No waiting and no locks; under contention the function runs again (D027).
  • State with a life of its own, with its own timers, a connection, an identity, restarts: an actor, a fiber that reads a bounded mailbox in a loop.
  • Several cells changed together, with effects: a cown. It is the one thing the other two cannot do without a protocol of their own, and it is an experiment.

Fibers are not a fourth model next to these: tasks, actors and behaviours all run on them, and a program never names one.

Actors and cowns overlap: both let one piece of state be changed by one piece of work at a time. They differ in the rest (design §2.1):

  • An actor has its own thread of control, a bounded mailbox, messages that are data, and a life that a supervisor can restart.
  • A cown has none of these. Only a cown changes several cells atomically.

An actor needs nothing new from the runtime, while a cown is a new primitive of it. So if one of the two is redundant, it is the cown.

Tasks are the core (accepted)

As D006: tasks return values, a task does not outlive its scope, a failure cancels its siblings and reaches the parent as an ordinary error, and functions are not coloured. Every root (D005) opens a root scope: a program's main, each test, a REPL session, a live connection. Combinators (task.all, task.race, task.timeout, task.pmap) are library code over scope; Java's StructuredTaskScope, still a preview after seven rounds, shows that join policies belong in a library.

Actors and supervisors are a library (accepted)

An actor is a task that reads a bounded channel in a loop; it can be written by hand from spawn and std.chan. std.actor adds call and reply, a lane for control messages, timers, and supervision. A supervisor is a scope in which the failure of one child does not cancel the others, with a restart policy. The runtime has no actors. This answers D006's question about actors and supervisors.

  • Immutable values already make messages cheap: a value is marked shared once, then each send is one atomic increment. Actors in the runtime would buy nothing on memory. They would bring the control problems every actor system has had to patch: reentrancy (Swift, Orleans), overload on unbounded mailboxes (Erlang's OTP 27 and 28), no atomicity across actors, and cycles between actor references, which reference counting cannot free without a detector (Pony).
  • Ferron, with actors in the runtime under one lock, paid about 10,000 instructions per message, and copying between actors took 45% of the cycles of a parallel workload.
  • What actors are good for, supervision and identity, is built from scopes in the library, and can change without changing the runtime.

Mailboxes are bounded. There is no selective receive. Control messages (stop, the exit of a child) have their own lane ahead of ordinary mail, which is the lesson of Erlang's EEP 76.

Channels are bounded (accepted)

The capacity of a channel is required, and 0 means a rendezvous. A full channel suspends the writer: backpressure is the default, and std has no unbounded queue. try-send, timeouts, and chan.select over several channels and timers come with it. A channel has two ends, one to write and one to read; when the last writer end is gone the channel closes, and a writer whose readers are all gone gets an error instead of waiting forever (design §4.5).

Behaviours and actors are not reentrant (accepted)

An actor handles one message at a time, and a behaviour runs to its end before the next one on the same cown starts, also across waits: what a handler read before a wait is still true after it. This is Erlang's and Orleans' default and Ferron's choice; Swift's reentrant actors traded it for bugs where state changes across an await.

The deadlock it allows, waiting for work that waits for you, is reported where it happens: an actor.call to an actor already waiting in the same chain of calls, and a join inside a behaviour on a behaviour that needs one of its cowns, raise an error instead of hanging.

The sim scheduler; scheduling points are events of the language (accepted)

The scheduler handles the effects of waiting (D005, D006), and sim is a second implementation of it. It runs all fibers of a test on one thread and takes every decision from the test's seed (D016, D024). A failure prints the seed and the schedule, and diaspar test --failed replays it.

Its scheduling points are events of the language: spawn, join, operations on channels, actors, cowns and atoms (reading an atom included), sleep, I/O, and the delivery of cancellation. Preemption polls are not scheduling points, and sim never preempts. Nothing is lost by this: values are immutable, so code between two such events cannot observe another task, and a switch there changes no result. In return, a schedule is the same in compiled code and in the evaluator, at every optimization level, so the differential harness (D003) compares concurrent programs too.

diaspar test runs every test under sim; schedules: N runs a test under N schedules (design §6).

Prepared now, in stage C (accepted, 2026-11-17)

Three changes cost little before the seed exists and would mean regenerating the seed (self-hosting.md 5.3) after it, so they are made in stage C, before any of the steps of design §8:

  • dsp_ctx belongs to the fiber (design §5.1): the state of the computation (status, unwinding, failures, handlers, trace, spill area, resume, pending releases, stack_limit) stays in dsp_ctx; the allocator hooks, the free stack and the counters move to a dsp_worker that the context points to.
  • The hooks of preemption, inert: the C back end emits DSP_POLL(ctx) at the back-edge of every loop whose body calls no compiled function, and the macro expands to nothing until step 2; stack_limit is read with a relaxed atomic load (the same instruction); the slow path of the prologue check tells a poisoned limit (yield, then continue) from a stack overflow. Step 2 turns preemption on in the runtime without changing generated code, and D020's interrupt uses the same points.
  • Every mutable global of the C runtime is listed in design §5.9 with how it is protected, and mise run lint fails on one that is not listed, so no hidden shared state reaches stage D.

Proposed

  • ^:deterministic. A function so marked is checked against its inferred effects: no atoms, channels, actors, cowns, I/O, task.race or task.timeout. Then its result does not depend on the schedule. As every effect check, it warns (D005, D007), and the REPL checks marked functions again when something they call is redefined (design §4.8).
  • Errors in sequential order for task.all, task.pmap and (scope ordered: true …): when several tasks fail, the error raised is that of the task started first, the error running them one after another would give; the others go under :suppressed. A plain scope cancels at the first failure, for latency (design §4.2).
  • Values are marked shared where they are published: the values spawn captures, values stored in a shared atom or sent on a channel. The result of join and the contents of a cown are not marked (design §4.4; the shared counts of D028).
  • The scheduler follows Go's: a run queue per worker thread with a runnext slot, stealing, a global queue, a monitor thread, one poller. dsp_ctx belongs to the fiber, not to the thread (design §5.1, §5.2).
  • Preemption by polls. The monitor poisons a running fiber's stack limit, so the prologue check that compiled functions already have (D026) sends it to the scheduler. A loop that calls no compiled function polls at its back-edge, with the polls D020 puts in --repl builds. Signals only set flags. The same poll delivers cancellation and the REPL's interrupt (design §5.3). This answers D006's question about checks in long loops.
  • Blocking C calls declared blocking: hand their worker's slot to another thread, as Go does for system calls; a fiber with a C frame on its stack resumes only on its own thread (design §5.5; D021).
  • A main executor. main runs on the process's main thread and stays there, for graphics, windows, and C libraries bound to one thread; task.on-main runs code there from any task (design §5.2).
  • Two priorities, normal and background. A background task (loading a level, a compaction) runs when no normal task is ready, so it does not take cores from a frame or a request (design §5.2).
  • OS processes stay, and follow the structure of tasks (design §4.9).
    • Running a program does not block a thread. process.run parks the fiber, so a parallel build is a task.pmap over process.run.
    • A child process belongs to the scope that started it. When the scope is cancelled, the child and its own children get SIGTERM, then SIGKILL after the cleanup deadline, and are waited for. A daemon opts out with detach: true.
    • process.start gives a handle whose streams are channel ends; releasing the handle stops the process.
  • The OS process is the one boundary of isolation. Tasks share the address space, so a crash in C code, abort, or running out of memory ends them all, and a supervisor has nothing left to restart.
    • Code that may crash (an untrusted C library, a plugin, someone else's code) runs in a process of its own.
    • Actors in another process, whose messages are data, are a library for later and the first step to distribution.
  • process.exit cancels the root scope. Every task runs its cleanup and child processes are stopped, then the program exits with the code (D005).
  • No fork without exec, and no API for raw OS threads. A task that needs a thread of its own is pinned.
  • std.cown, an experiment. Behaviour-oriented concurrency, from Project Verona: a behaviour takes a set of cowns at once and runs exactly once, so it may perform effects; it changes several cells atomically and cannot deadlock. The model has no production use yet.
    • Its queue of behaviours is unbounded: cown.update never waits. That goes against this decision's rule for channels and mailboxes.
    • Bounding the queue plainly brings deadlocks back. The candidate answer is to bound it only for callers outside any behaviour (design §4.7).
    • The module comes when a real Diaspar program needs it and the questions of design §4.7 are answered.

Rejected alternatives

Alternative Where Why not
Actors in the runtime Erlang, Pony, Ferron above
Unbounded mailboxes or channels Erlang, Ferron's mailboxes overload shows in production, not in tests; "queues don't fix overload" (Hebert)
Selective receive Erlang O(N) in the length of the queue; it stays usable only through an idiom the compiler recognizes
Reentrant actors Swift state changes across a wait
STM now Haskell, Clojure's refs retries forbid effects, which is the case atoms leave open; refs are rare in Clojure practice; it can come later as a library over atoms, as OCaml's kcas
One lock over the scheduler Ferron about 10,000 instructions per message; Go's scheduler of 2012 hit the same wall
Copying values between threads, or pinning them for a scope Ferron copies took 45% of the cycles; pins needed a global lock and a search per handle
Preemption by signals that stop code anywhere Go since 1.14 generated C has no safe points the compiler knows of, and a count updated halfway is a race

Open questions

In design/concurrency.md §10: the API, the name and the unbounded queue of cowns; which collection protocols a channel's reading end implements; the semaphore; how background tasks are kept from starving; the deadline of cleanup under cancellation.