Initial import: Aji 0.1 draft spec and Zig scaffolding
Draft specification, RFCs, reference registries and examples, plus the reference-implementation skeleton: build.zig, diagnostic envelope with error-code registry synced to docs, aji CLI, conformance runner with seed tests, CI and contributor docs.
This commit is contained in:
commit
17a55a7fef
67 files changed
+4197
No files matched your search
@@ -0,0 +1,340 @@
|
||||
# 04. Concurrency and Atomics
|
||||
|
||||
This file is normative. Rationale is in RFCs 0002 and 0003.
|
||||
|
||||
## 1. Design hierarchy
|
||||
|
||||
Safe Aji SHOULD solve concurrency in this order:
|
||||
|
||||
```text
|
||||
1. ownership transfer
|
||||
2. shared immutable data
|
||||
3. synchronized shared mutable state
|
||||
4. atomics
|
||||
5. unsafe raw concurrency
|
||||
```
|
||||
|
||||
The language makes weaker, more error-prone mechanisms increasingly explicit.
|
||||
|
||||
## 2. Execution units
|
||||
|
||||
Aji distinguishes:
|
||||
|
||||
- `Task`: runtime-scheduled unit that may migrate between scheduler workers;
|
||||
- `LocalTask`: task pinned to a scheduler worker because captured state is `Local`;
|
||||
- `Thread<T>`: operating-system thread handle.
|
||||
|
||||
## 3. Structured task scopes
|
||||
|
||||
Tasks are created inside a lexical/dynamic task scope:
|
||||
|
||||
```aji
|
||||
task.scope s {
|
||||
let t = task.spawn(s, worker, move input)?;
|
||||
let result = task.join(move t)?;
|
||||
}
|
||||
```
|
||||
|
||||
A child task MUST NOT outlive its scope.
|
||||
|
||||
A scope cannot complete while live child handles exist unless they have been explicitly joined, cancel-joined or transferred to a permitted supervisor abstraction.
|
||||
|
||||
Silent detachment is prohibited.
|
||||
|
||||
## 4. Detached execution
|
||||
|
||||
Detached tasks require explicit authority such as `cap<task.detach>`.
|
||||
|
||||
Captured values must be owned, `Send`, and independent of the parent lifetime.
|
||||
|
||||
General application code SHOULD prefer supervisors or structured scopes.
|
||||
|
||||
## 5. Send and Share
|
||||
|
||||
A value moved into a migratable task or thread must satisfy `Send`.
|
||||
|
||||
A value observed concurrently through immutable borrows must satisfy `Share`.
|
||||
|
||||
Thread-affine resources are `Local` and therefore cannot enter migratable tasks.
|
||||
|
||||
## 6. Borrowing into scoped tasks
|
||||
|
||||
A structured child task may borrow parent data if the verifier proves the child completes before the borrowed storage expires.
|
||||
|
||||
Mutable data may be borrowed into multiple child tasks only when the borrowed regions are proven disjoint.
|
||||
|
||||
## 7. Shared mutable state
|
||||
|
||||
Ordinary `mut<T>` may not be concurrently shared.
|
||||
|
||||
Safe shared mutation uses approved synchronization types:
|
||||
|
||||
```text
|
||||
Mutex<T>
|
||||
RwLock<T>
|
||||
Atomic*
|
||||
Channel<T>
|
||||
```
|
||||
|
||||
## 8. Verified interior mutability
|
||||
|
||||
Synchronization wrappers such as `Mutex<T>`, `RwLock<T>` and atomic types are verifier-known interior-mutability abstractions.
|
||||
|
||||
Their internal mutable storage is not directly reachable through ordinary `ref<T>` or `mut<T>` operations. Mutation is exposed only through operations whose synchronization semantics are known to the verifier.
|
||||
|
||||
Ordinary user-defined structs cannot obtain shared interior mutation merely by hiding a mutable field behind a method. Equivalent abstractions require an audited unsafe implementation or a verifier-known intrinsic.
|
||||
|
||||
A synchronization wrapper may therefore be `Share` even though it internally changes state. Its `Share` implementation is defined by the wrapper's synchronization contract, not by ordinary field immutability.
|
||||
|
||||
## 9. Mutex
|
||||
|
||||
A mutex guard is linear and provides exclusive `mut<T>` access.
|
||||
|
||||
```aji
|
||||
let g = mutex.lock(ref m)?;
|
||||
modify(g);
|
||||
mutex.unlock(move g);
|
||||
```
|
||||
|
||||
A successful lock has acquire semantics. Unlock has release semantics.
|
||||
|
||||
Mutex guards are not `Send` by default.
|
||||
|
||||
Core mutexes do not automatically implement poisoning because Aji has no exception unwinding.
|
||||
|
||||
## 10. Ordered mutex profile
|
||||
|
||||
Aji MAY provide `OrderedMutex<Rank, T>`.
|
||||
|
||||
While holding rank `R`, acquiring a lower/equal forbidden rank is rejected according to profile rules.
|
||||
|
||||
This profile can eliminate a class of lock-order deadlocks but is not required for all programs.
|
||||
|
||||
## 11. RwLock
|
||||
|
||||
`RwLock<T>` permits:
|
||||
|
||||
- multiple read guards yielding `ref<T>`;
|
||||
- exactly one write guard yielding `mut<T>`.
|
||||
|
||||
Fairness is runtime-specific unless a runtime profile promises otherwise.
|
||||
|
||||
## 12. Channels
|
||||
|
||||
Channels transfer ownership.
|
||||
|
||||
```aji
|
||||
let (tx, rx) = channel.create<Message>(capacity = 1024)?;
|
||||
channel.send(ref tx, move message)?;
|
||||
let message = channel.recv(ref rx)?;
|
||||
```
|
||||
|
||||
Channels are bounded by default.
|
||||
|
||||
Capacity `0` denotes a rendezvous channel.
|
||||
|
||||
Unbounded queues are library constructs whose allocation behavior must be explicit.
|
||||
|
||||
A successful send synchronizes with the receive that obtains the transferred value.
|
||||
|
||||
## 13. Island messaging
|
||||
|
||||
`Island<T>` is the preferred representation for large mutable messages and object graphs.
|
||||
|
||||
Sending an island transfers ownership without exposing shared mutation.
|
||||
|
||||
## 14. select
|
||||
|
||||
Aji may wait on multiple events:
|
||||
|
||||
```aji
|
||||
select {
|
||||
recv(rx1) => ...
|
||||
recv(rx2) => ...
|
||||
timer(deadline) => ...
|
||||
cancelled(token) => ...
|
||||
}
|
||||
```
|
||||
|
||||
When multiple branches are ready, normal mode does not promise source-order selection. Deterministic mode must define a stable choice.
|
||||
|
||||
## 15. Cancellation
|
||||
|
||||
Cancellation is cooperative.
|
||||
|
||||
A task is not asynchronously interrupted at arbitrary instructions.
|
||||
|
||||
Cancellation is observed only at explicit cancellation points, including selected waits, channel operations, timer operations and `task.check_cancel`.
|
||||
|
||||
Structured cancellation propagates from parent to descendants.
|
||||
|
||||
## 16. Cancellation shielding
|
||||
|
||||
A bounded scope may defer cancellation observation:
|
||||
|
||||
```aji
|
||||
task.cancel_shield {
|
||||
commit_metadata();
|
||||
}
|
||||
```
|
||||
|
||||
The cancellation request remains pending and is observed after leaving the shield.
|
||||
|
||||
## 17. Task failure
|
||||
|
||||
Task functions return normal values or `Result<T, E>`.
|
||||
|
||||
A failing task does not implicitly terminate siblings. Supervision policy is explicit and may implement fail-fast, collect-all, first-success or restart semantics.
|
||||
|
||||
## 18. Blocking
|
||||
|
||||
Operations that block an OS thread declare `thread.block` in their effects.
|
||||
|
||||
A cooperative scheduler may reject direct blocking calls from ordinary tasks and require a blocking pool or dedicated thread.
|
||||
|
||||
## 19. Asynchronous I/O
|
||||
|
||||
An asynchronous operation retains ownership/borrow claims over its buffers until completion.
|
||||
|
||||
```aji
|
||||
let op = io.read_start(file, borrow mut buffer)?;
|
||||
// buffer unavailable
|
||||
let n = io.wait(move op)?;
|
||||
// buffer available again
|
||||
```
|
||||
|
||||
This prevents use or mutation while the OS/runtime may access the buffer.
|
||||
|
||||
## 20. Memory model relations
|
||||
|
||||
Aji defines:
|
||||
|
||||
- `sequenced-before` within one execution unit;
|
||||
- `synchronizes-with` edges from synchronization operations;
|
||||
- `happens-before` as the transitive closure of those relations;
|
||||
- per-atomic-object modification order.
|
||||
|
||||
## 21. Data race
|
||||
|
||||
Two accesses conflict when they overlap and at least one writes.
|
||||
|
||||
A data race exists when conflicting ordinary accesses occur concurrently without a happens-before ordering.
|
||||
|
||||
Safe Aji must reject programs in which such a race is possible under its verified alias/synchronization model.
|
||||
|
||||
## 22. DRF-SC guarantee
|
||||
|
||||
Programs that use only safe synchronization and contain no ordinary-memory data races receive the following guarantee:
|
||||
|
||||
> ordinary-memory observations are explainable by a sequentially consistent interleaving consistent with happens-before.
|
||||
|
||||
Explicit weak atomics may produce weaker observations for atomic coordination.
|
||||
|
||||
## 23. Atomic types
|
||||
|
||||
Initial types:
|
||||
|
||||
```text
|
||||
AtomicBool
|
||||
AtomicU8 AtomicU16 AtomicU32 AtomicU64 AtomicUsize
|
||||
AtomicI8 AtomicI16 AtomicI32 AtomicI64 AtomicIsize
|
||||
```
|
||||
|
||||
Support depends on target capabilities. Unsupported widths must be rejected or lowered through a conforming library abstraction with equivalent semantics.
|
||||
|
||||
## 24. Atomic ordering
|
||||
|
||||
Exactly these memory orders are defined:
|
||||
|
||||
```text
|
||||
relaxed
|
||||
acquire
|
||||
release
|
||||
acq_rel
|
||||
seq_cst
|
||||
```
|
||||
|
||||
There is no `consume`.
|
||||
|
||||
No atomic operation has a default ordering.
|
||||
|
||||
### 24.1 Load
|
||||
|
||||
Allowed:
|
||||
|
||||
```text
|
||||
relaxed acquire seq_cst
|
||||
```
|
||||
|
||||
### 24.2 Store
|
||||
|
||||
Allowed:
|
||||
|
||||
```text
|
||||
relaxed release seq_cst
|
||||
```
|
||||
|
||||
### 24.3 Read-modify-write
|
||||
|
||||
Allowed:
|
||||
|
||||
```text
|
||||
relaxed acquire release acq_rel seq_cst
|
||||
```
|
||||
|
||||
### 24.4 Compare exchange
|
||||
|
||||
Compare exchange declares success and failure orderings separately. Failure ordering cannot be `release` or `acq_rel`.
|
||||
|
||||
## 25. Weak atomics
|
||||
|
||||
Functions that use relaxed atomics SHOULD expose an `atomic.weak` effect or equivalent verifier-visible marker.
|
||||
|
||||
This makes weak-memory code easy to find during audits.
|
||||
|
||||
## 26. Sequential consistency
|
||||
|
||||
`seq_cst` operations participate in a single global total order consistent with each execution unit's sequenced-before relation for those operations.
|
||||
|
||||
## 27. Fences
|
||||
|
||||
Aji supports:
|
||||
|
||||
```text
|
||||
atomic.fence(acquire)
|
||||
atomic.fence(release)
|
||||
atomic.fence(acq_rel)
|
||||
atomic.fence(seq_cst)
|
||||
```
|
||||
|
||||
There is no relaxed fence.
|
||||
|
||||
## 28. Mixing atomic and ordinary access
|
||||
|
||||
Safe code cannot directly access atomic storage as ordinary storage.
|
||||
|
||||
Unsafe code that mixes conflicting atomic and non-atomic accesses must independently satisfy the memory model. Violating those unsafe preconditions is Undefined Aji Behavior.
|
||||
|
||||
## 29. Atomic pointers and reclamation
|
||||
|
||||
Safe v0.1 does not expose arbitrary atomic safe-reference manipulation because publication and reclamation are inseparable problems.
|
||||
|
||||
Lock-free pointer structures should use audited standard-library facilities such as epoch reclamation, hazard pointers or RCU-like mechanisms.
|
||||
|
||||
## 30. ABA
|
||||
|
||||
The verifier does not claim to prove general ABA absence.
|
||||
|
||||
Low-level algorithms must use explicit countermeasures such as generations, tagged values, hazard pointers or epoch reclamation.
|
||||
|
||||
## 31. Progress guarantees
|
||||
|
||||
Aji does not infer lock-free/wait-free guarantees in v0.1.
|
||||
|
||||
Libraries may document an implementation as blocking, obstruction-free, lock-free or wait-free.
|
||||
|
||||
## 32. Deadlock and liveness
|
||||
|
||||
Core Aji does not guarantee deadlock freedom, livelock freedom, starvation freedom or scheduler fairness.
|
||||
|
||||
Optional verification profiles may establish stronger properties for restricted programs.
|
||||
Reference in new issue
Block a user