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
@@ -1,8 +1,10 @@
|
||||
# RFC 0012: Channels, Mutex, Select and Cancellation Points
|
||||
|
||||
Status: Proposed
|
||||
Status: Accepted (revision 2, after owner review: D1–D3 decided; `task.wait` gated by `cancellation`; values and handles returned as block parameters)
|
||||
Scope: AIR format 0, features `cancellation`, `channels`, `sync`
|
||||
Language version: 0.1
|
||||
Measured Agent Reference Bundle growth on integration: +15.8 % tokens (estimate: 12–16 %).
|
||||
Integrated into: spec 04 §37–§41; spec 04 §35; spec 05 §9; spec 07 §8.11; spec 07a; spec 08 §14; spec 11 §8; `reference/air-features.md`, `effects.md`, `error-codes.md`; `docs/design/decisions.md` DD-014; `docs/agent/bundle.txt`
|
||||
Supersedes: —
|
||||
|
||||
Resolves: Q-013 (deterministic `select` and the scheduler's choice points). Realizes spec 04 §7–§9, §12, §14–§16 for AIR. Builds on RFC 0011 (spec 04 §33–§36).
|
||||
@@ -14,7 +16,7 @@ RFC 0011: who may execute concurrently?
|
||||
RFC 0012: how do concurrent tasks communicate, wait and coordinate?
|
||||
```
|
||||
|
||||
Out of scope: `RwLock` (deferred, reserved feature `rwlock`), atomics (own RFC: after atomics the "every native outcome is an interleaving" model no longer suffices, spec 04 §36.1), timers, task-aware I/O and async buffer claims (`E4303`), multiple receivers.
|
||||
Out of scope: `RwLock` (deferred, reserved feature `rwlock`), atomics (own RFC: after atomics the "every native outcome is an interleaving" model no longer suffices, spec 04 §36.1), timers, task-aware I/O and async buffer claims (`E4303`), several receivers (channels are MPSC).
|
||||
|
||||
## Motivation
|
||||
|
||||
@@ -30,7 +32,7 @@ The design goal is a concurrency core that a weaker model can use safely. So the
|
||||
## Invariants
|
||||
|
||||
1. **Explicit outcomes.** Every waiting operation completes on exactly one of its declared edges. Cancellation is never a trap.
|
||||
2. **Ownership transfer.** A value sent on a channel is owned by the sending operation until the operation completes. On `ok` it belongs to the channel and then to the receiver. On `closed` or `cancelled` it is still the sender's. A value is never observable by two tasks.
|
||||
2. **Ownership transfer.** A value sent on a channel is owned by the sending operation until the operation completes. On `ok` it belongs to the channel and then to the receiver. On `closed` or `cancelled` it is handed back to the sender as a block parameter of the target. A value is never observable by two tasks.
|
||||
3. **No silent drop.** A value is removed from a channel only by `channel.recv` or a `select` receive arm. A receiver is consumed only when the channel is empty and no more values can enter it.
|
||||
4. **No wait under a lock.** While a task holds a `guard<T>`, it executes no waiting operation. This is checked statically, including through calls.
|
||||
5. **Cooperative cancellation.** Cancellation is observed only at the cancellation points of §2.2. An operation that can complete without waiting completes, even if cancellation is pending. Inside a shield, cancellation is never observed. Shields nest properly.
|
||||
@@ -39,11 +41,13 @@ The design goal is a concurrency core that a weaker model can use safely. So the
|
||||
|
||||
## 1. Features and dependencies
|
||||
|
||||
All three features are new to this RFC. `channels` and `sync` are reserved today with dependencies `[ownership]`. Their semantics cannot be stated without the task model, effects and, for channels, allocation budgets, so this RFC changes their reserved dependency lists (see decision D1 under Unresolved questions).
|
||||
All three features are new to this RFC. `channels` and `sync` were reserved with dependencies `[ownership]`; their semantics cannot be stated without the task model, effects and, for channels, allocation budgets.
|
||||
|
||||
**Rule for reserved dependencies (D1).** While a feature is `reserved`, no valid module can use it, so its dependency set MAY change. When the feature is published, its dependencies become part of the frozen semantics of AIR 0. This is a general registry rule (`reference/air-features.md`, DD-014), not an exception for these two features.
|
||||
|
||||
| Feature | Depends on | Semantic block |
|
||||
|---|---|---|
|
||||
| `cancellation` | `effects`, `tasks` | effect `task.wait`; the waiting-operation rule (§2); `shield`, `task.shield.*`; `task.check_cancel`; the waiting form of `task.join` |
|
||||
| `cancellation` | `effects`, `tasks` | effect `task.wait` and its gated requirement (§2.4); the waiting-operation rule (§2); `shield`, `task.shield.*`; `task.check_cancel`; `task.join.cancel` |
|
||||
| `channels` | `cancellation`, `heap` | `sender<T>`, `receiver<T>`, `channel.*`, `select` |
|
||||
| `sync` | `cancellation` | `mutex<T>`, `guard<T>`, `mutex.*`, the critical-section rule (E4304) |
|
||||
|
||||
@@ -52,16 +56,17 @@ Reserved by this RFC (all unscheduled):
|
||||
| Feature | Depends on | Semantic block |
|
||||
|---|---|---|
|
||||
| `rwlock` | `sync` | `rwlock<T>`, read and write guards (only `read_lock`, `write_lock` and `unlock`; no upgrade or downgrade) |
|
||||
| `channel_transfer` | `channels` | `sender<…>`/`receiver<…>` inside channel elements or mutex contents; several receivers |
|
||||
| `channel_transfer` | `channels` | `sender<…>`/`receiver<…>` inside channel elements or mutex contents |
|
||||
| `receiver_dup` | `channels` | several receivers of one channel (MPMC) |
|
||||
| `guard_transfer` | `sync` | `guard<T>` as a parameter, a return type or an aggregate member |
|
||||
|
||||
The published feature `tasks` is not changed (spec 07 §8.7). The new cancellation behavior applies only to new instructions. The plain `task.join` keeps its meaning, and `task.cancelled` keeps returning the raw request flag.
|
||||
The published feature `tasks` is not changed (spec 07 §8.7). The plain `task.join` is never a cancellation point, and `task.cancelled` keeps returning the raw request flag. The only rule that touches a `tasks` instruction, the `task.wait` effect of `task.join`, applies only in modules declaring `cancellation` (§2.4). A module that does not declare it keeps the RFC 0011 semantics exactly.
|
||||
|
||||
## 2. Waiting and cancellation (feature `cancellation`)
|
||||
|
||||
### 2.1 The waiting-operation rule
|
||||
|
||||
A **waiting operation** is one of `task.join` in its terminator form, `channel.send`, `channel.recv`, `select` and `mutex.lock`. Each has a **ready condition** that may hold now or later. One rule covers all of them:
|
||||
A **waiting operation** is one of `task.join.cancel`, `channel.send`, `channel.recv`, `select` and `mutex.lock`. Each has a **ready condition** that may hold now or later. One rule covers all of them:
|
||||
|
||||
```text
|
||||
1. If the operation is ready, it completes with that outcome (ok or closed).
|
||||
@@ -82,26 +87,58 @@ The plain `task.join` and the I/O intrinsics (`net.accept`, reads on streams) ar
|
||||
| Point | Observes cancellation |
|
||||
|---|---|
|
||||
| `task.check_cancel` | always when observable |
|
||||
| `task.join` (terminator form), `channel.send`, `channel.recv`, `select`, `mutex.lock` | only when it would otherwise wait (§2.1 step 2) |
|
||||
| `task.join.cancel`, `channel.send`, `channel.recv`, `select`, `mutex.lock` | only when it would otherwise wait (§2.1 step 2) |
|
||||
|
||||
### 2.3 Instructions
|
||||
|
||||
| Instruction | Operands → result | Effect | Verifier | Runtime |
|
||||
|---|---|---|---|---|
|
||||
| `task.check_cancel, ok -> @ok, cancelled -> @c` (terminator) | — ; both targets have no parameters | — | — | `@c` if cancellation is observable, else `@ok`; a scheduling point |
|
||||
| `task.join %t, ok -> @ok, cancelled -> @c` (terminator) | `task<T>`; `@ok(T)`, `@c()` | — | on the `ok` edge as plain `task.join` (consumes `%t`, ends its loans); on the `cancelled` edge `%t` and its loans stay live | waiting operation; ready when the task has returned |
|
||||
| `task.join.cancel %t, ok -> @ok, cancelled -> @c` (terminator) | `task<T>` (consumed on both edges); `@ok(T)`, `@c(task<T>)` | `task.wait` | `ok` edge: as `task.join` — the task is joined and its loans end. `cancelled` edge: the same task comes back as the parameter of `@c`; it is not joined, and its loans stay `LOANED`, now 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 |
|
||||
|
||||
`shield` is linear, function-bound (as `scope` in spec 04 §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`, like other linear state). Together with the innermost-first rule this makes shields properly nested regions. The depth is dynamic per task, so a function called inside a shield runs shielded.
|
||||
|
||||
The cancelled edge of the waiting `task.join` lets a parent react to its own cancellation, for example by forwarding it with `task.cancel %t` and then joining. Downward propagation is not automatic: changing `task.cancel` would change the published `tasks` feature.
|
||||
`task.join.cancel` is a new instruction, not a form of `task.join`. Its loan state machine extends spec 04 §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 (§2.1 step 1). The usual reaction on `cancelled` forwards the request and then waits:
|
||||
|
||||
```text
|
||||
cancelled(%t2: task<u64>):
|
||||
task.cancel %t2
|
||||
%r = task.join %t2
|
||||
```
|
||||
|
||||
Downward propagation is not automatic: changing `task.cancel` would change the published `tasks` feature.
|
||||
|
||||
**Shield invariant.** `task.shield.begin` pushes a token; `task.shield.end` accepts only the innermost live token:
|
||||
|
||||
```text
|
||||
%a = task.shield.begin
|
||||
%b = task.shield.begin
|
||||
task.shield.end %a ; E4305
|
||||
task.shield.end %b ; ok
|
||||
task.shield.end %a ; ok
|
||||
```
|
||||
|
||||
Every shield token is ended before `ret` (`E3002`). A trap needs no cleanup.
|
||||
|
||||
### 2.4 Effect `task.wait`
|
||||
|
||||
The new effect `task.wait` (no capability) is required by `channel.send`, `channel.recv`, `select` and `mutex.lock`. It is checked like every effect (spec 05 §9; `E3101`). It exists so that a function's public contract says whether it may wait for another task, which §4.4 needs.
|
||||
The new effect `task.wait` (no capability) says in a function's public contract that it may wait for another task. §4.4 needs that.
|
||||
|
||||
Joins do not need it. A function can join only tasks it spawned itself, because handles are function-bound, and spawning already requires `task.spawn`.
|
||||
**Gated requirement.** In a module declaring `cancellation`, every operation that may suspend requires `task.wait`: `task.join` (from `tasks`), `task.join.cancel`, `channel.send`, `channel.recv`, `select` and `mutex.lock`. It is checked like every effect (spec 05 §9; `E3101`).
|
||||
|
||||
A module that does not declare `cancellation` is unaffected. A published `tasks` module whose function joins without declaring `task.wait` stays valid with the same run record. Only modules that opt into the new feature get the extended effect semantics, and `channels` and `sync` imply it. This is an addition under spec 07 §8.7, because the stricter rule is attached to a feature that older modules do not declare.
|
||||
|
||||
A separate verifier-level "may suspend" analysis was rejected: it would add a second transitive property next to effects.
|
||||
|
||||
## 3. Channels (feature `channels`)
|
||||
|
||||
@@ -124,32 +161,41 @@ Linear element types such as `own<T>`, `file<…>` and `stream` are allowed. Mov
|
||||
|
||||
### 3.2 Instructions
|
||||
|
||||
All endpoint operations take a `mut` borrow of the endpoint. Only the close operations consume it.
|
||||
`send` and `recv` take a `mut` borrow of the endpoint, `dup` takes a `ref`, and the close operations consume it.
|
||||
|
||||
| Instruction | Operands → targets | Effect | Verifier | Runtime |
|
||||
|---|---|---|---|---|
|
||||
| `channel.make T, %n, %mem, ok -> @ok, err -> @err` (terminator) | `%n: usize` capacity, `%mem: cap<mem.alloc>`; `@ok(sender<T>, receiver<T>)`, `@err(alloc_error)` | `mem.alloc` | element rules of §3.1 | charges `%n × size(T)` bytes to `%mem` (spec 03 §19); a capacity of 0 creates a rendezvous channel. Never waits |
|
||||
| `%tx2 = channel.sender.dup %txb` | `mut<sender<T>>` → `sender<T>` | — | — | one more sender of the same channel |
|
||||
| `channel.send %txb, %v, ok -> @ok, closed -> @closed, cancelled -> @c` (terminator) | `mut<sender<T>>`, `T`; targets without parameters | `task.wait` | `%v` is consumed on the `ok` edge only. On `closed` and `cancelled` it is still live and owned by the caller | waiting operation (§3.3) |
|
||||
| `%tx2 = channel.sender.dup %txr` | `ref<sender<T>>` → `sender<T>` | — | — | a new, separately linear sender of the same channel; `open_senders += 1`; the original stays live |
|
||||
| `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; on `ok` it is gone | waiting operation (§3.3) |
|
||||
| `channel.recv %rxb, ok -> @ok, closed -> @closed, cancelled -> @c` (terminator) | `mut<receiver<T>>`; `@ok(T)` | `task.wait` | — | waiting operation (§3.3) |
|
||||
| `channel.close_sender %tx` | `sender<T>` (consumed) | — | — | if it was the last sender, a drained receiver now sees `closed`; a scheduling point |
|
||||
| `channel.close_sender %tx` | `sender<T>` (consumed) | — | — | `open_senders -= 1`; at 0 no new value can ever enter; a scheduling point |
|
||||
| `channel.close_receiver %rx, ok -> @ok, pending -> @pending` (terminator) | `receiver<T>` (consumed on both edges); `@pending(receiver<T>)` | — | — | §3.4; a scheduling point |
|
||||
| `select …` (terminator) | §3.5 | `task.wait` | §3.5 | §3.5 |
|
||||
|
||||
### 3.3 Ready conditions
|
||||
### 3.3 Channel states and ready conditions
|
||||
|
||||
Values are delivered in FIFO order.
|
||||
A channel has a FIFO buffer, a count `open_senders` of live senders and a receiver state:
|
||||
|
||||
| State | Meaning |
|
||||
|---|---|
|
||||
| `OPEN` | the receiver is open and `open_senders > 0`: sends are accepted |
|
||||
| `RECEIVER_CLOSING` | the receiver has started closing (§3.4): 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: `recv` reports `closed`, `close_receiver` takes `ok` |
|
||||
| `CLOSED` | the receiver has been consumed; sends report `closed` |
|
||||
|
||||
New values stop arriving in exactly two ways: the last sender closes, or the receiver starts closing. In both cases values already buffered are received, never destroyed: the runtime never silently drops an owned value.
|
||||
|
||||
| Operation | `ok`-ready | `closed`-ready |
|
||||
|---|---|---|
|
||||
| send, capacity `n > 0` | fewer than `n` values buffered | the receiver is closed or closing |
|
||||
| send, capacity 0 | the receiver is waiting in `channel.recv` or in a `select` with a receive arm on this channel | the receiver is closed or closing |
|
||||
| recv, capacity `n > 0` | a value is buffered | the buffer is empty and every sender is closed, or the receiver is closing |
|
||||
| recv, capacity 0 | a sender is waiting in `channel.send` or in a `select` with a send arm on this channel | as above |
|
||||
| send, capacity `n > 0` | `OPEN` and fewer than `n` values buffered | `RECEIVER_CLOSING` or `CLOSED` |
|
||||
| send, capacity 0 | `OPEN` and the receiver is waiting 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 is waiting in `channel.send` or in a `select` with a send arm on this channel | `DRAINED` |
|
||||
|
||||
`closed` takes precedence for a send: a send to a closed receiver is `closed` even if there is space. For a receive, buffered values take precedence: `closed` is reported only after the channel is drained. This is the RFC's answer to "last sender closed → recv after the queue is drained → closed". It is a concurrency state, not an `io_error`.
|
||||
|
||||
On a rendezvous channel, a sender waiting in `channel.send` holds its value until a receiver takes it. A sender that leaves on `cancelled` or `closed` keeps the value. Waiting rendezvous senders are matched in the order they began to wait. When a send completes against a waiting `select`, that `select` resumes on the matched arm without a draw.
|
||||
On a rendezvous channel, a sender waiting in `channel.send` holds its value until a receiver takes it. A sender that leaves on `cancelled` or `closed` gets the value back as a block parameter. Waiting rendezvous senders are matched in the order they began to wait. When a send completes against a waiting `select`, that `select` resumes on the matched arm without a draw.
|
||||
|
||||
A **synchronization edge** runs from a successful send to the receive that obtains the value (spec 04 §12). In the cooperative interpreter this is immediate; native backends must provide acquire/release ordering.
|
||||
|
||||
@@ -157,12 +203,12 @@ A **synchronization edge** runs from a successful send to the receive that obtai
|
||||
|
||||
`channel.close_receiver` never drops a value:
|
||||
|
||||
- if the buffer is empty, the receiver is consumed and the channel is closed for sending. Waiting and future sends end `closed`. The program continues at `@ok`;
|
||||
- otherwise the channel enters the **closing** state. Sends end `closed` from now on, and waiting rendezvous senders also end `closed`. The receiver comes back on `@pending`. The program drains it with `channel.recv` until `closed` (buffered values first) and then closes it again, which now takes `@ok`.
|
||||
- if the buffer is empty, the receiver is consumed and the channel becomes `CLOSED`. Waiting and future sends end `closed`. The program continues at `@ok`;
|
||||
- otherwise the channel enters `RECEIVER_CLOSING`. Sends end `closed` from now on, and waiting rendezvous senders also end `closed`. The receiver comes back on `@pending`. The program drains it with `channel.recv` until `closed` (buffered values first) and then closes it again, which now takes `@ok`.
|
||||
|
||||
Because the closing state admits no new values, the drain loop terminates. A receiver can therefore stop early without leaking linear values, and the verifier needs no knowledge of the buffer contents.
|
||||
|
||||
The channel's memory is returned to the `mem.alloc` budget when the receiver and the last sender have both been closed, at whichever of those closes comes last. That is an explicit instruction, so there is no hidden destructor.
|
||||
The channel's memory is returned to the `mem.alloc` budget when the channel is `CLOSED` and `open_senders = 0`, at whichever of those closes comes last. That is an explicit instruction, so there is no hidden destructor.
|
||||
|
||||
### 3.5 `select`
|
||||
|
||||
@@ -177,13 +223,16 @@ select [recv %rxb1, ok -> @a, closed -> @a_done],
|
||||
- **Arms** are channel operations, nothing else: `[recv %rxb, ok -> @x, closed -> @y]` with `@x(T)`, and `[send %txb, %v, ok -> @x, closed -> @y]`. A `select` has at least one arm and exactly one `cancelled` target. The arm targets have the parameters of the corresponding `channel.recv`/`channel.send` targets.
|
||||
- **Readiness.** An arm is ready when its operation is `ok`-ready or `closed`-ready (§3.3). The `select` as a whole is a waiting operation (§2.1) whose ready condition is "at least one arm is ready".
|
||||
- **Choice.** If exactly one arm is ready, it is taken. If `k ≥ 2` arms are ready, the scheduler draws `x = next()` from the schedule generator and takes the ready arm number `x mod k`, in textual order. The draw counts as a scheduler choice. It is not "first in text", so schedule exploration covers alternative ready branches. With `schedules: N`, task scheduling and arm selection vary together, from one stream.
|
||||
- **Ownership along edges.** A value in a send arm is consumed only on that arm's `ok` edge. On every other edge of the `select` it is still live. The same value may therefore appear in several send arms:
|
||||
- **Ownership along edges.** Let `V` be the distinct values named by the 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` that were not sent on that edge, in the order of `V`. So ownership is visible in the CFG; nothing is restored implicitly:
|
||||
|
||||
```text
|
||||
select [send %tx1, %x, ok -> @to1, closed -> @c1], [send %tx2, %x, ok -> @to2, closed -> @c2], cancelled -> @c
|
||||
; V = (%x)
|
||||
; @to1() @to2() %x was sent
|
||||
; @c1(%x) @c2(%x) @c(%x) %x comes back
|
||||
```
|
||||
|
||||
This is the branch-edge ownership rule (spec 06 §6.3) applied per arm. The verifier applies the transfer function of the chosen arm to each edge.
|
||||
The same value may appear in several send arms, because only one arm runs. This is the branch-edge ownership rule (spec 06 §6.3) applied per arm. A plain `channel.send` is the one-arm case.
|
||||
- **Borrows.** Arms may name the same endpoint borrow more than once. Only one arm runs.
|
||||
|
||||
## 4. Mutex (feature `sync`)
|
||||
@@ -219,19 +268,18 @@ A trap terminates the whole execution (spec 04 §33.2), so there is no state whe
|
||||
|
||||
On every path where a `guard<T>` is live, the function MUST NOT execute:
|
||||
|
||||
- a waiting operation (§2.1), including `mutex.lock`;
|
||||
- `task.spawn`, or `task.join` in either form;
|
||||
- an instruction that requires the effect `task.wait` or `task.spawn` (in a `sync` module these are every suspending operation of §2.4, `task.join` included, and `task.spawn`);
|
||||
- a `call` (or an indirect call) of a function whose declared effects include `task.wait` or `task.spawn`.
|
||||
|
||||
Violations are `E4304` (`sync.wait_while_locked`), naming the guard and the instruction.
|
||||
|
||||
Consequences, by construction:
|
||||
|
||||
- a task holds at most one guard at a time, since `mutex.lock` is itself a waiting operation;
|
||||
- a task holds at most one guard at a time, since `mutex.lock` requires `task.wait`: `lock A; lock B` is `E4304`, and the classic lock-order deadlock cannot be expressed in safe AIR 0;
|
||||
- the holder of a lock never waits for another task. Every wait in `mutex.lock` ends when the holder runs to its `mutex.unlock`, unless the holder runs forever (fuel bounds that) or traps (which ends the run). **Mutexes alone cannot deadlock**, and no lock-ordering discipline is needed;
|
||||
- a critical section may call ordinary functions and pass them the `mut<T>` from `mutex.borrow`. Their effect sets say whether they may wait.
|
||||
|
||||
The check reads only the caller's guard state and the callees' declared effects, so it stays intraprocedural (spec 04 §33 invariant 6). `sync` depends on `effects` through `cancellation`, so callee effect sets are always available. This is stricter than common runtimes. A future feature may relax it if evaluation shows the need. It is deliberately not reserved now.
|
||||
The check reads only the caller's guard state and the callees' declared effects, so it stays intraprocedural (spec 04 §33 invariant 6). `sync` depends on `cancellation`, so callee effect sets are always available and `task.join` carries `task.wait`. This is stricter than common runtimes. A future feature may relax it if evaluation shows the need. It is deliberately not reserved now.
|
||||
|
||||
## 5. Scheduler and run record (resolves Q-013)
|
||||
|
||||
@@ -250,7 +298,9 @@ effects [task.wait]
|
||||
entry:
|
||||
%txb = borrow.mut %tx
|
||||
%v = const.u64 7
|
||||
channel.send %txb, %v, ok -> @done, closed -> @done, cancelled -> @done
|
||||
channel.send %txb, %v, ok -> @done, closed -> @back, cancelled -> @back
|
||||
back(%w: u64): ; the value comes back; u64 needs no cleanup
|
||||
br @done
|
||||
done:
|
||||
borrow.end %txb
|
||||
channel.close_sender %tx
|
||||
@@ -294,6 +344,8 @@ locked(%g: guard<u64>):
|
||||
|
||||
```text
|
||||
select [send %a, %job, ok -> @sent_a, closed -> @a_gone], [send %b, %job, ok -> @sent_b, closed -> @b_gone], cancelled -> @stop
|
||||
; @sent_a() @sent_b() — %job went to one worker
|
||||
; @a_gone(%j: own<@Job>) @b_gone(%j: own<@Job>) @stop(%j: own<@Job>) — the job comes back
|
||||
```
|
||||
|
||||
## Rejected examples
|
||||
@@ -312,7 +364,7 @@ locked(%g: guard<u64>):
|
||||
```text
|
||||
channel.send %txb, %v, ok -> @sent, closed -> @closed, cancelled -> @c
|
||||
sent:
|
||||
%w = struct.get @Msg.id, %v ; E2001: %v was moved on the ok edge
|
||||
%w = own.take %v ; E2001: %v was consumed by the send
|
||||
```
|
||||
|
||||
```text
|
||||
@@ -337,11 +389,11 @@ New codes:
|
||||
Existing codes used:
|
||||
|
||||
- `E4001` and `E1001` for element rules;
|
||||
- `E2001` and `E2002` for moved send values;
|
||||
- `E2001` and `E2002` for values consumed by `send`/`select`;
|
||||
- `E2102` for guard and access borrows;
|
||||
- `E3002` for leaked endpoints, guards and shields, and for differing shield stacks at a join;
|
||||
- `E3101` for `task.wait`;
|
||||
- `E0006` for `rwlock`, `channel_transfer` and `guard_transfer`.
|
||||
- `E0006` for `rwlock`, `channel_transfer`, `receiver_dup` and `guard_transfer`.
|
||||
|
||||
No new trap codes. No new environment outcomes: deadlocks remain `blocked`.
|
||||
|
||||
@@ -352,16 +404,22 @@ On acceptance:
|
||||
- spec 04: new §37 (waiting and cancellation), §38 (channels), §39 (select), §40 (mutex), §41 (critical-section rule); §14–§16 point to them; §11 marks RwLock as deferred;
|
||||
- spec 02 §6 / spec 04 §35: Send/Share rows for the new types;
|
||||
- spec 05: effect `task.wait`;
|
||||
- spec 07 §8: types and the `select` syntax; spec 07a: contract rows;
|
||||
- spec 07 §8.11: types and the `select` syntax; spec 07a: contract rows;
|
||||
- spec 08: verifier stages (shield stack, guard state, E4304);
|
||||
- spec 11 §8: choice points, record;
|
||||
- `reference/air-text.ebnf`, `air-features.md`, `effects.md`, `error-codes.md`;
|
||||
- `reference/air-features.md`, `effects.md`, `error-codes.md`;
|
||||
- `docs/agent/bundle.txt`;
|
||||
- Q-013 marked resolved.
|
||||
|
||||
## Compatibility and migration
|
||||
|
||||
Additive under spec 07 §8.7: three new features, new types, instructions, one effect and two diagnostic codes. `tasks` and every published feature keep their meaning. The plain `task.join` is not a cancellation point, and `task.cancelled` ignores shields (it reads the raw flag). The only change to the registry beyond additions is the dependency lists of the reserved `channels` and `sync` (decision D1). No module can use a reserved feature today, so no accepted or rejected module changes status.
|
||||
Additive under spec 07 §8.7: three new features, new types, instructions, one effect and two diagnostic codes. `tasks` and every published feature keep their meaning:
|
||||
|
||||
- the plain `task.join` is not a cancellation point;
|
||||
- `task.cancelled` ignores shields (it reads the raw flag);
|
||||
- `task.join` requires `task.wait` only in modules declaring `cancellation`, which no existing module does.
|
||||
|
||||
The dependency lists of the reserved `channels` and `sync` change under the general rule of §1 (D1). No module can use a reserved feature, so no accepted or rejected module changes status.
|
||||
|
||||
## Agent complexity impact
|
||||
|
||||
@@ -379,7 +437,7 @@ Additive under spec 07 §8.7: three new features, new types, instructions, one e
|
||||
- per-edge consumption for `channel.send` and `select`, which reuses the existing per-edge linear machinery (as for `fs.close`).
|
||||
- **New syntax:**
|
||||
- 3 features, 5 types, 1 effect;
|
||||
- 13 instructions: `task.check_cancel`, waiting `task.join`, `task.shield.begin`/`end`, `channel.make`/`sender.dup`/`send`/`recv`/`close_sender`/`close_receiver`, `select`, `mutex.make`/`take`/`lock`/`borrow`/`unlock` (counting `select` once).
|
||||
- 17 instructions: `task.check_cancel`, `task.join.cancel`, `task.shield.begin`/`end`, `channel.make`/`sender.dup`/`send`/`recv`/`close_sender`/`close_receiver`, `select`, `mutex.make`/`take`/`lock`/`borrow`/`unlock` (counting `select` once).
|
||||
- **Interactions:** loans (sharing a `ref<mutex>`), effects (`task.wait`, the E4304 call rule), linearity (endpoints, guards, shields), heap budgets (channel buffers), the scheduler (choices).
|
||||
- **Repairability:**
|
||||
- each outcome is an edge the agent must route, so a forgotten case is a CFG error, not a runtime surprise;
|
||||
@@ -398,15 +456,14 @@ Additive under spec 07 §8.7: three new features, new types, instructions, one e
|
||||
- **`select` picks the first ready arm in text**: reproducible, but schedule exploration would never see the other branch. Rejected for a seeded draw.
|
||||
- **`select` over arbitrary expressions**: rejected; arms are operations with fixed transfer functions.
|
||||
- **Cancellation as a trap or a universal `TaskError`**: rejected; outcomes are edges.
|
||||
- **Automatic downward cancellation propagation**: would change the published `task.cancel`. Instead the waiting `task.join` lets a parent forward cancellation explicitly.
|
||||
- **Automatic downward cancellation propagation**: would change the published `task.cancel`. Instead `task.join.cancel` lets a parent forward cancellation explicitly.
|
||||
- **A cancellable form of `task.join`** (same opcode with edges): a new instruction is clearer and keeps the published opcode's contract untouched.
|
||||
- **Restoring a sent value implicitly on `closed`/`cancelled`** (the same SSA value stays live): ownership would not be visible in the CFG; rejected for block parameters.
|
||||
- **`task.wait` required by `task.join` unconditionally**: would make published `tasks` modules fail with `E3101`; rejected for the gated rule (§2.4).
|
||||
- **Shield as a lexical block or a separate task**: AIR has no lexical blocks; a linear token with innermost-first ending gives proper nesting with existing machinery.
|
||||
- **`RwLock` now**: read and write guards, fairness, starvation and several concurrent guards add little expressive power over a mutex, ownership partitioning and channels. Deferred (reserved `rwlock`).
|
||||
- **Channel capacity in the type, buffer in frame storage**: avoids the heap but makes endpoints borrows, which cannot be moved into tasks or returned. Rejected for linear endpoints with an explicit `mem.alloc` charge.
|
||||
|
||||
## Unresolved questions
|
||||
|
||||
Decisions requested from the owner:
|
||||
|
||||
- **D1 — dependencies of the reserved `channels` and `sync`.** The registry says a reserved feature's name and dependencies are fixed, but `[ownership]` cannot state the semantics, and §1 needs `cancellation`/`heap` and `cancellation`. No module is affected, since reserved features are unusable. Proposal: allow the change this once, recorded in DD-014's log.
|
||||
- **D2 — waiting `task.join`.** Proposal: a new terminator form in `cancellation`, with no automatic propagation. Alternative: no cancellable join at all; parents must poll `task.check_cancel` between joins.
|
||||
- **D3 — `channel.sender.dup` in format 0.** Proposal: yes. Several producers into one receiver is the common fan-in, and "last sender closed" needs it to be meaningful. Alternative: one sender per channel; fan-in uses one channel per producer and `select`.
|
||||
None. Owner decisions: D1 general rule for reserved dependencies; D2 `task.join.cancel` as a new instruction returning the handle on `cancelled`; D3 `channel.sender.dup` from `ref<sender<T>>`, no receiver duplication (`receiver_dup` reserved). Deferred: `RwLock`, several receivers, atomics.
|
||||
Reference in new issue
Block a user