mjdi docsv0.10.0latest
Concurrency contract (P1-19)
The supported compiler (Mojo 1.2.0, build 2016d0cd) ships no
Source: mjdi/docs/concurrency.md
The supported compiler (Mojo 1.2.0, build 2016d0cd) ships no threading primitives in the standard library surface mjdi can use (no threads, no spawn/join, no concurrent futures), so no multi-threaded mjdi program can be built to test against today. This page states the contract accordingly: what is guaranteed, what is prohibited, and what would change if the toolchain grows threads.
The current contract: single-thread-only per container
A Container (and each Scope it creates) is not thread-safe:
- Containers and scopes own mutable runtime state (value slots, singleton caches, trace logs, statistics counters, re-entrancy flags), with no synchronization.
- Concurrent resolutions on one container race the caches and the re-entrancy guard (the guard's flag array and index stack are plain per-container state, not atomic).
- Concurrent
close()/close_scope()races the release paths.
Prohibited: sharing a container or scope across execution contexts without external serialization. Allowed: separate containers — one per thread/context — over the same registry type (the registry itself is immutable, compile-time data; per-container state never crosses containers).
Thread-compatible, externally synchronized
mjdi's shared data is immutable or comptime: registries are type-level values (nothing mutable at all — the library has no mutable global state by design), and all graph validation happens at compile time. The mutable surface is exactly the per-container/ per-scope state listed above. So mjdi is fairly described as thread-compatible but not thread-safe: external serialization (a lock around every container call) makes use from multiple threads correct, at the cost of serializing resolution.
Why no synchronization is built in
- The supported compiler exposes no threading primitives to synchronize with — code written against hypothetical primitives could not be compiled or tested here, and untested safety claims are exactly what this project refuses to make.
- Synchronization would tax the single-threaded fast paths every benchmark measures (see Performance).
What changes if threads arrive
Should a later Mojo toolchain ship real threading, the re-entrancy guard and the cache slots are the two pieces that would need synchronization or per-thread state (per-thread guard state, in fact, to keep cross-thread cycles detectable without false positives). That work is deliberately deferred: it cannot be validated on the target compiler today, and the contract above would change with it.
Related
- Re-entrancy — the guard whose state is per-container.
- Cache statistics — counters with the same single-threaded assumption.
- Known limitations — the full compiler-boundary list.