spec: accept RFC 0012 and integrate waits, channels, select and mutex
revision 2 per owner review: reserved-feature dependencies may change until publication (general rule, DD-014); task.join.cancel returns the handle on cancelled with loans still LOANED; channel.sender.dup from ref<sender<T>> (MPSC, receiver_dup reserved); task.wait required by every suspending operation, task.join included, only in modules declaring cancellation; sent values and handles come back as block parameters on closed/cancelled edges; explicit channel states spec 04 §37-§41 and pointers in §11-§16, §35, §36; spec 05 §9, 07 §8.11, 07a, 08 §14, 11 §6-§7; features cancellation, channels, sync published (implementation pending); rwlock, channel_transfer, receiver_dup, guard_transfer reserved; effect task.wait; E4304, E4305; Q-013 resolved; bundle +15.8 % tokens (recorded)
This commit is contained in:
1 parent
4230c78dd1
commit
b82bc35c5f
19 files changed
+387
-67
No files matched your search
@@ -119,6 +119,8 @@ This profile can eliminate a class of lock-order deadlocks but is not required f
|
||||
|
||||
Fairness is runtime-specific unless a runtime profile promises otherwise.
|
||||
|
||||
AIR format 0 defers `RwLock` (reserved feature `rwlock`; RFC 0012). The AIR mutex is specified in §40–§41.
|
||||
|
||||
## 12. Channels
|
||||
|
||||
Channels transfer ownership.
|
||||
@@ -137,6 +139,8 @@ Unbounded queues are library constructs whose allocation behavior must be explic
|
||||
|
||||
A successful send synchronizes with the receive that obtains the transferred value.
|
||||
|
||||
AIR realization: §38 (separate `sender<T>`/`receiver<T>` endpoints, MPSC).
|
||||
|
||||
## 13. Island messaging
|
||||
|
||||
`Island<T>` is the preferred representation for large mutable messages and object graphs.
|
||||
@@ -158,6 +162,8 @@ select {
|
||||
|
||||
When multiple branches are ready, normal mode does not promise source-order selection. Deterministic mode must define a stable choice.
|
||||
|
||||
AIR realization: §39 (channel arms only; seeded choice among ready arms).
|
||||
|
||||
## 15. Cancellation
|
||||
|
||||
Cancellation is cooperative.
|
||||
@@ -168,6 +174,8 @@ Cancellation is observed only at explicit cancellation points, including selecte
|
||||
|
||||
Structured cancellation propagates from parent to descendants.
|
||||
|
||||
AIR realization: §37. In format 0 propagation is explicit: `task.join.cancel` lets a parent observe its own cancellation and forward it with `task.cancel`.
|
||||
|
||||
## 16. Cancellation shielding
|
||||
|
||||
A bounded scope may defer cancellation observation:
|
||||
@@ -180,6 +188,8 @@ task.cancel_shield {
|
||||
|
||||
The cancellation request remains pending and is observed after leaving the shield.
|
||||
|
||||
AIR realization: `task.shield.begin`/`task.shield.end` (§37.3).
|
||||
|
||||
## 17. Task failure
|
||||
|
||||
Task functions return normal values or `Result<T, E>`.
|
||||
@@ -419,7 +429,9 @@ Example: two workers square the two halves of one buffer in parallel; both loans
|
||||
| `ref<T>` | if `T` is `Share` | if `T` is `Share` |
|
||||
| `mut<T>` | if `T` is `Send` | no |
|
||||
| `slice<T>` / `slice_mut<T>` | as `ref<array>` / `mut<array>` | as `ref` / `mut` |
|
||||
| `task<T>`, `scope` | no (`Local`) | no |
|
||||
| `task<T>`, `scope`, `shield`, `guard<T>` | no (`Local`) | no |
|
||||
| `sender<T>`, `receiver<T>` | yes | no |
|
||||
| `mutex<T>` | if `T` is | if `T` is `Send` (the lock synchronizes access) |
|
||||
| struct, enum, array | if every field/payload/element is | if every field/payload/element is |
|
||||
|
||||
So an aggregate holding capabilities is `Send` and `Share` exactly when its other members are (Q-014).
|
||||
@@ -438,7 +450,7 @@ mut<T> → T: Send otherwise E4001
|
||||
|
||||
### 36.1 Model
|
||||
|
||||
In both profiles of the reference interpreter, tasks are cooperative execution units on one host thread. A task runs until it reaches a **scheduling point** — `task.spawn`, `task.join`, `task.cancel`, `task.cancelled`, a waiting operation (RFC 0012, I/O), or the end of its **quantum** — and then the scheduler chooses the next task to run among the runnable ones.
|
||||
In both profiles of the reference interpreter, tasks are cooperative execution units on one host thread. A task runs until it reaches a **scheduling point** — `task.spawn`, `task.join`, `task.cancel`, `task.cancelled`, a waiting operation or another scheduling point of §37–§40, I/O, or the end of its **quantum** — and then the scheduler chooses the next task to run among the runnable ones.
|
||||
|
||||
Native backends (Phase 7) may run tasks in parallel; by invariant 5 they may only produce outcomes that some interleaving of this model produces. This guarantee applies to safe programs that use neither the future weak-memory atomic facilities nor unsafe code; atomic memory ordering is governed by the Aji memory model (spec 04 §20–§31).
|
||||
|
||||
@@ -468,8 +480,199 @@ Varying the schedule seed while keeping `--seed` explores interleavings without
|
||||
|
||||
### 36.4 Run record
|
||||
|
||||
The record of a run that spawned tasks adds `"schedule":{"seed":S,"quantum":Q,"choices":C}`, where `S` is the schedule seed and `C` the number of scheduler choices made; two runs with equal records made the same choices.
|
||||
The record of a run that spawned tasks or drew among `select` arms adds `"schedule":{"seed":S,"quantum":Q,"choices":C}`, where `S` is the schedule seed and `C` the number of scheduler choices made, including draws among ready `select` arms (§39); two runs with equal records made the same choices. No draw is made when there is only one candidate.
|
||||
|
||||
### 36.5 Schedule exploration
|
||||
|
||||
The conformance runner gains `schedules: N`: the test runs with schedule seeds `0 … N−1` and an unchanged program seed, and every run must satisfy the expectation. Systematic exploration (DPOR) is Phase 5 work.
|
||||
The conformance runner gains `schedules: N`: the test runs with schedule seeds `0 … N−1` and an unchanged program seed, and every run must satisfy the expectation. Task choices and `select` choices vary together, from one stream. Systematic exploration (DPOR) is Phase 5 work.
|
||||
|
||||
## 37. Waiting and cancellation in AIR (feature `cancellation`)
|
||||
|
||||
Decision record: RFC 0012. Depends on `effects`, `tasks`.
|
||||
|
||||
Invariants of §37–§41:
|
||||
|
||||
1. **Explicit outcomes.** Every waiting operation completes on exactly one of its declared edges (`ok`, `closed`, `cancelled`). Cancellation is never a trap.
|
||||
2. **Ownership transfer.** A value sent on a channel is owned by the sending operation until it completes. On `ok` it belongs to the channel and then to the receiver; on `closed` or `cancelled` it is handed back as a block parameter. A value is never observable by two tasks.
|
||||
3. **No silent drop.** A value leaves a channel only through a receive. A receiver is consumed only when the channel is empty and no further value can enter it.
|
||||
4. **No wait under a lock.** While a task holds a `guard<T>`, it executes no operation that may suspend. This is checked statically, including through calls (§41).
|
||||
5. **Cooperative cancellation.** Cancellation is observed only at cancellation points (§37.2). An operation that can complete without waiting completes. Inside a shield cancellation is never observed. Shields nest properly.
|
||||
6. **Reproducibility.** Task choices and choices among ready `select` arms are drawn from the one schedule generator (§36.2).
|
||||
7. **No hidden cleanup.** Nothing is unlocked, closed or dropped implicitly. A trap while holding a guard terminates the whole execution (§33.2).
|
||||
|
||||
### 37.1 The waiting-operation rule
|
||||
|
||||
The **waiting operations** are `task.join.cancel`, `channel.send`, `channel.recv`, `select` and `mutex.lock`. Each has a ready condition. One rule covers all of them:
|
||||
|
||||
```text
|
||||
1. If the operation is ready, it completes with that outcome (ok or closed).
|
||||
Pending cancellation is not consulted.
|
||||
2. Otherwise, if cancellation is observable, it completes on its cancelled edge.
|
||||
3. Otherwise the task waits. It becomes runnable when the operation becomes ready
|
||||
or cancellation becomes observable. When it resumes, go to 1.
|
||||
```
|
||||
|
||||
**Cancellation is observable** in a task when its cancellation-request flag (§33.2) is set and its shield depth is 0. A waiting operation is a scheduling point when it completes and when it begins to wait; fuel is paid once, when it first begins (§36.3). A waiting task counts as waiting for global quiescence (spec 11 §8). The plain `task.join` and the I/O intrinsics are not cancellation points in format 0.
|
||||
|
||||
### 37.2 Cancellation points
|
||||
|
||||
| Point | Observes cancellation |
|
||||
|---|---|
|
||||
| `task.check_cancel` | always when observable |
|
||||
| `task.join.cancel`, `channel.send`, `channel.recv`, `select`, `mutex.lock` | only when it would otherwise wait (§37.1 step 2) |
|
||||
|
||||
`task.cancelled` (§33.2) is not a cancellation point: it returns the raw request flag and ignores shields.
|
||||
|
||||
### 37.3 Instructions
|
||||
|
||||
| Instruction | Operands → result | Effect | Verifier | Runtime |
|
||||
|---|---|---|---|---|
|
||||
| `task.check_cancel, ok -> @ok, cancelled -> @c` (terminator) | targets without parameters | — | — | `@c` if cancellation is observable, else `@ok`; a scheduling point |
|
||||
| `task.join.cancel %t, ok -> @ok, cancelled -> @c` (terminator) | `task<T>` (consumed on both edges); `@ok(T)`, `@c(task<T>)` | `task.wait` | `ok`: as `task.join`, the task is joined and its loans end. `cancelled`: the same task is the parameter of `@c`; it is not joined and its loans stay `LOANED`, owned by the new handle value | waiting operation; ready when the task has returned |
|
||||
| `%sh = task.shield.begin` | → `shield` | — | pushes `%sh` on the function's shield stack | increments the task's shield depth |
|
||||
| `task.shield.end %sh` | `shield` (consumed) | — | `%sh` MUST be the innermost live shield (`E4305`) | decrements the shield depth |
|
||||
|
||||
Loan state machine of `task.join.cancel` (extends §34):
|
||||
|
||||
```text
|
||||
task<T> --task.join.cancel--+-- ok --------> joined; loans end; result T
|
||||
+-- cancelled -> same task<T> as a block parameter;
|
||||
not joined; loans stay LOANED
|
||||
```
|
||||
|
||||
If the child has already returned, the join completes on `ok` even when cancellation is pending. Cancellation does not propagate to children automatically; a parent forwards it explicitly:
|
||||
|
||||
```text
|
||||
cancelled(%t2: task<u64>):
|
||||
task.cancel %t2
|
||||
%r = task.join %t2
|
||||
```
|
||||
|
||||
`shield` is linear, function-bound (§33.1) and `Local`. The stack of live shields is part of the verifier state and MUST be identical on every edge into a block (`E3002`); every shield is ended before `ret` (`E3002`). Shield depth is dynamic per task, so a function called inside a shield runs shielded.
|
||||
|
||||
```text
|
||||
%a = task.shield.begin
|
||||
%b = task.shield.begin
|
||||
task.shield.end %a ; E4305
|
||||
task.shield.end %b ; ok
|
||||
task.shield.end %a ; ok
|
||||
```
|
||||
|
||||
### 37.4 Effect `task.wait`
|
||||
|
||||
The effect `task.wait` (no capability) states that a function may wait for another task. **In a module declaring `cancellation`**, every operation that may suspend requires it: `task.join`, `task.join.cancel`, `channel.send`, `channel.recv`, `select` and `mutex.lock` (`E3101`, spec 05 §9.3). A module that does not declare `cancellation` keeps the semantics of §33 unchanged; `channels` and `sync` imply `cancellation`.
|
||||
|
||||
## 38. Channels in AIR (feature `channels`)
|
||||
|
||||
Decision record: RFC 0012. Depends on `cancellation`, `heap`.
|
||||
|
||||
### 38.1 Types
|
||||
|
||||
| Type | Meaning | Class |
|
||||
|---|---|---|
|
||||
| `sender<T>` | a sending endpoint | linear |
|
||||
| `receiver<T>` | the receiving endpoint, exactly one per channel | linear |
|
||||
|
||||
Endpoints may be block arguments, function parameters and return values, and may be moved into tasks. They may not be aggregate members (reserved `linear_aggregates`) or heap contents. Channels are MPSC: several receivers are the reserved feature `receiver_dup`.
|
||||
|
||||
**Element rules** for `T` (also used by `mutex<T>`, §40):
|
||||
|
||||
- `T: Send` (`E4001`);
|
||||
- `T` contains no borrow, `scope`, `task<…>`, `shield` or `guard<…>` (`E1001`);
|
||||
- `T` contains no `sender<…>` or `receiver<…>`: reserved feature `channel_transfer` (`E0006`).
|
||||
|
||||
Linear elements (`own<T>`, `file<…>`, `stream`, …) are allowed.
|
||||
|
||||
### 38.2 Instructions
|
||||
|
||||
| Instruction | Operands → targets | Effect | Verifier | Runtime |
|
||||
|---|---|---|---|---|
|
||||
| `channel.make T, %n, %mem, ok -> @ok, err -> @err` (terminator) | `%n: usize`, `%mem: cap<mem.alloc>`; `@ok(sender<T>, receiver<T>)`, `@err(alloc_error)` | `mem.alloc` | element rules | charges `%n × size(T)` bytes (spec 03 §19); capacity 0 is a rendezvous channel; never waits |
|
||||
| `%tx2 = channel.sender.dup %txr` | `ref<sender<T>>` → `sender<T>` | — | — | a new, separately linear sender of the same channel; `open_senders += 1` |
|
||||
| `channel.send %txb, %v, ok -> @ok, closed -> @closed, cancelled -> @c` (terminator) | `mut<sender<T>>`, `T` (consumed on every edge); `@ok()`, `@closed(T)`, `@c(T)` | `task.wait` | on `closed` and `cancelled` the value comes back as the target's parameter | waiting operation (§38.3) |
|
||||
| `channel.recv %rxb, ok -> @ok, closed -> @closed, cancelled -> @c` (terminator) | `mut<receiver<T>>`; `@ok(T)`, `@closed()`, `@c()` | `task.wait` | — | waiting operation (§38.3) |
|
||||
| `channel.close_sender %tx` | `sender<T>` (consumed) | — | — | `open_senders -= 1`; a scheduling point |
|
||||
| `channel.close_receiver %rx, ok -> @ok, pending -> @pending` (terminator) | `receiver<T>` (consumed on both edges); `@ok()`, `@pending(receiver<T>)` | — | — | §38.4; a scheduling point |
|
||||
|
||||
### 38.3 States and ready conditions
|
||||
|
||||
A channel has a FIFO buffer, a count `open_senders` and a receiver state:
|
||||
|
||||
| State | Meaning |
|
||||
|---|---|
|
||||
| `OPEN` | receiver open and `open_senders > 0`: sends are accepted |
|
||||
| `RECEIVER_CLOSING` | the receiver has started closing: no send is accepted; buffered values remain and MUST be received |
|
||||
| `DRAINED` | no future value can arrive (`open_senders = 0`, or `RECEIVER_CLOSING`) and the buffer is empty |
|
||||
| `CLOSED` | the receiver has been consumed |
|
||||
|
||||
New values stop arriving in exactly two ways: the last sender closes, or the receiver starts closing. In both cases buffered values are received, never destroyed.
|
||||
|
||||
| Operation | `ok`-ready | `closed`-ready |
|
||||
|---|---|---|
|
||||
| send, capacity `n > 0` | `OPEN` and fewer than `n` values buffered | `RECEIVER_CLOSING` or `CLOSED` |
|
||||
| send, capacity 0 | `OPEN` and the receiver waits in `channel.recv` or in a `select` with a receive arm on this channel | `RECEIVER_CLOSING` or `CLOSED` |
|
||||
| recv, capacity `n > 0` | a value is buffered | `DRAINED` |
|
||||
| recv, capacity 0 | `OPEN` and a sender waits in `channel.send` or in a `select` with a send arm on this channel | `DRAINED` |
|
||||
|
||||
A waiting rendezvous sender holds its value until a receiver takes it; waiting rendezvous senders are matched in the order they began to wait. When an operation completes against a waiting `select`, that `select` resumes on the matched arm without a draw. A successful send synchronizes with the receive that obtains the value (§12).
|
||||
|
||||
### 38.4 Closing the receiver
|
||||
|
||||
- If the buffer is empty, the receiver is consumed, the channel becomes `CLOSED` (waiting and future sends end `closed`) and control continues at `@ok`.
|
||||
- Otherwise the channel becomes `RECEIVER_CLOSING` (sends end `closed`, including waiting rendezvous senders) and the receiver comes back on `@pending`. The program receives until `closed` and closes again.
|
||||
|
||||
The drain terminates because no value can enter. The channel's memory returns to the `mem.alloc` budget at the close that leaves the channel `CLOSED` with `open_senders = 0`.
|
||||
|
||||
## 39. `select` in AIR (feature `channels`)
|
||||
|
||||
Decision record: RFC 0012. Resolves Q-013.
|
||||
|
||||
```text
|
||||
select [recv %rxb, ok -> @a, closed -> @a_done], [send %txb, %v, ok -> @sent, closed -> @gone], cancelled -> @c
|
||||
```
|
||||
|
||||
- **Arms** are channel operations only: `[recv %rxb, ok -> @x, closed -> @y]` and `[send %txb, %v, ok -> @x, closed -> @y]`. A `select` has at least one arm and exactly one `cancelled` target. It requires `task.wait`.
|
||||
- **Readiness.** An arm is ready when its operation is `ok`-ready or `closed`-ready (§38.3). The `select` is a waiting operation (§37.1) that is ready when at least one arm is.
|
||||
- **Choice.** One ready arm is taken. With `k ≥ 2` ready arms the scheduler draws `x = next()` from the schedule generator (§36.2) and takes ready arm `x mod k` in textual order. The draw is a scheduler choice; textual order alone never decides.
|
||||
- **Ownership along edges.** Let `V` be the distinct values named by send arms, in order of first appearance. The `select` consumes every value of `V` on every edge. Each target receives, after its own parameters (the received `T` on a receive arm's `ok`), the values of `V` not sent on that edge, in the order of `V`:
|
||||
|
||||
```text
|
||||
select [send %tx1, %x, ok -> @to1, closed -> @c1], [send %tx2, %x, ok -> @to2, closed -> @c2], cancelled -> @c
|
||||
; @to1() @to2() %x was sent
|
||||
; @c1(%x) @c2(%x) @c(%x) %x comes back
|
||||
```
|
||||
|
||||
A plain `channel.send` is the one-arm case. Arms may name the same endpoint borrow more than once.
|
||||
|
||||
## 40. Mutex in AIR (feature `sync`)
|
||||
|
||||
Decision record: RFC 0012. Depends on `cancellation`. `RwLock` is deferred (reserved feature `rwlock`).
|
||||
|
||||
| Type | Meaning | Class |
|
||||
|---|---|---|
|
||||
| `mutex<T>` | mutual exclusion around a `T` (element rules of §38.1) | move-only; linear if `T` is |
|
||||
| `guard<T>` | the current task holds the lock | linear, function-bound, `Local` |
|
||||
|
||||
`mutex<T>` is opaque: no `borrow.field`, `load` or `store` reaches its contents; it is the verifier-known interior-mutability abstraction of §8. Tasks share it through a `ref<mutex<T>>` loan (§34). `guard<T>` as a parameter, return type or aggregate member is the reserved feature `guard_transfer` (`E0006`).
|
||||
|
||||
| Instruction | Operands → result | Effect | Verifier | Runtime |
|
||||
|---|---|---|---|---|
|
||||
| `%m = mutex.make %v` | `T` (consumed) → `mutex<T>` | — | element rules | unlocked; no allocation |
|
||||
| `%v = mutex.take %m` | `mutex<T>` (consumed) → `T` | — | move rules | — |
|
||||
| `mutex.lock %mr, ok -> @ok, cancelled -> @c` (terminator) | `ref<mutex<T>>`; `@ok(guard<T>)`, `@c()` | `task.wait` | the guard is a child of `%mr` (ending `%mr` while it is live is `E2102`); §41 | waiting operation; ready when unlocked; acquire |
|
||||
| `%p = mutex.borrow %g` | `guard<T>` (not consumed) → `mut<T>` | — | child borrow of `%g`, ended before `mutex.unlock %g` (`E2102`) | — |
|
||||
| `mutex.unlock %g` | `guard<T>` (consumed) | — | — | release; all tasks waiting on this mutex become runnable and the first scheduled takes it; a scheduling point |
|
||||
|
||||
No fairness is promised (§32). There is no poisoning and no implicit unlock; an unconsumed guard is `E3002`.
|
||||
|
||||
## 41. The critical-section rule
|
||||
|
||||
On every path where a `guard<T>` is live, a function MUST NOT execute an instruction that requires the effect `task.wait` or `task.spawn`, nor call a function whose declared effects include `task.wait` or `task.spawn` (`E4304`, `sync.wait_while_locked`, naming the guard and the instruction).
|
||||
|
||||
Consequences:
|
||||
|
||||
- a task holds at most one guard: `mutex.lock` requires `task.wait`, so `lock A; lock B` is `E4304` and lock-order deadlock cannot be expressed in safe AIR 0;
|
||||
- a lock holder never waits for another task, so mutexes alone cannot deadlock;
|
||||
- a critical section may call functions without those effects and pass them the `mut<T>` from `mutex.borrow`.
|
||||
|
||||
The check uses only the guard state and the callees' declared effects, so it stays intraprocedural (§33 invariant 6).
|
||||
Reference in new issue
Block a user