Phase 5: atomics through every layer (RFC 0015)

AIR: linear atomic<T> (AtomicScalar), atomic.new/take/load/store/RMW/
cmpxchg (two results)/fence with explicit orderings; feature atomics
published. Verifier: ordering tables (E4201), ordinary access (E4202),
effects [atomic] / [atomic, atomic.weak], linearity, take while lent
(E4101, also for mutex.take). Interpreter: timestamped message machine
(src/interp/weak.zig); views travel through spawn/join, channels, mutexes,
close and cancel; seq_cst reads the latest message and programs without
weak reads make no extra draw (existing records unchanged). aji explore:
exhaustive exploration that branches only before visible operations.
Litmus: 20/20 tests give exactly the oracle's sets; differential test
(1500 random programs) found no unsound outcome. .aji layer 10, Quick
Reference atomics, source coverage 67/67, audit report atomic_operations.
AIR bundle +5.9 %, QUICK.md +0.5k.
This commit is contained in:
dev committed 2026-10-06 08:05:01 +03:00
1 parent ce9ef03bc0
commit 4242132b2a
70 files changed
+2630 -80

No files matched your search

@@ -0,0 +1,23 @@
// compare_exchange binds the old value and whether it exchanged; the other operations return the old value.
// expect: run
// result: 28
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.compare_exchange
fn main() -> u64
effects [atomic, atomic.weak]
{
let c = atomic.new(5u64);
let (old, exchanged) = atomic.compare_exchange(ref c, 5u64, 9u64, acq_rel, acquire);
let (old2, again) = atomic.compare_exchange(ref c, 5u64, 1u64, seq_cst, relaxed);
let before = atomic.swap(ref c, 3u64, seq_cst);
let x = atomic.or(ref c, 4u64, seq_cst);
let y = atomic.and(ref c, 6u64, seq_cst);
let z = atomic.xor(ref c, 1u64, seq_cst);
let w = atomic.sub.wrap(ref c, 2u64, seq_cst);
atomic.fence(acq_rel);
let last = atomic.load(ref c, seq_cst);
let v = atomic.take(move c);
if exchanged and not again { old + old2 + before + v } else { 0 }
}
@@ -0,0 +1,35 @@
// Two tasks add to one cell with relaxed add.wrap; the total is exact on every schedule.
// expect: run
// schedules: 30
// quantum: 7
// result: 100
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.counter
fn bump(c: ref<atomic<u64>>) -> u8
effects [atomic, atomic.weak]
{
var i: u64 = 0;
while i < 50 {
let prev = atomic.add.wrap(c, 1u64, relaxed);
i = i + 1;
}
0u8
}
fn main() -> u64
effects [atomic, atomic.weak, task.spawn, task.wait]
{
let count = atomic.new(0u64);
{
ref c = count;
scope s {
let a = task.spawn(s, bump, c);
let b = task.spawn(s, bump, c);
let ra = task.join(move a);
let rb = task.join(move b);
}
}
atomic.take(move count)
}
@@ -0,0 +1,40 @@
// Release/acquire publication: a reader that sees the flag sees the data.
// expect: run
// schedules: 30
// quantum: 1
// result: 1
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.publication
fn publish(data: ref<atomic<u64>>, ready: ref<atomic<bool>>) -> u8
effects [atomic, atomic.weak]
{
atomic.store(data, 42u64, relaxed);
atomic.store(ready, true, release);
0u8
}
fn main() -> u64
effects [atomic, atomic.weak, task.spawn, task.wait]
{
let data = atomic.new(0u64);
let ready = atomic.new(false);
var ok: u64 = 1;
{
ref d = data;
ref r = ready;
scope s {
let p = task.spawn(s, publish, d, r);
if atomic.load(r, acquire) {
if atomic.load(d, relaxed) != 42 {
ok = 0;
}
}
let rp = task.join(move p);
}
}
let v = atomic.take(move data);
let f = atomic.take(move ready);
ok
}
@@ -0,0 +1,13 @@
// An atomic cell is not copied: `let b = a;` would be ambiguous, so the move is written.
// expect: reject E7003
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.copy_cell
fn main() -> u64
effects [atomic]
{
let a = atomic.new(1u64);
let b = a;
atomic.take(move b)
}
@@ -0,0 +1,13 @@
// compare_exchange has two results and is bound with `let (old, exchanged) = …`.
// expect: reject E7006
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.exchange_as_value
fn main() -> u64
effects [atomic]
{
let c = atomic.new(1u64);
let v = atomic.compare_exchange(ref c, 1u64, 2u64, seq_cst, seq_cst);
atomic.take(move c)
}
@@ -0,0 +1,14 @@
// The failure ordering of compare_exchange is never stronger than its success ordering.
// expect: reject E4201
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.exchange_failure_order
fn main() -> u64
effects [atomic, atomic.weak]
{
let c = atomic.new(1u64);
let (old, exchanged) = atomic.compare_exchange(ref c, 1u64, 2u64, relaxed, acquire);
let v = atomic.take(move c);
old + v
}
@@ -0,0 +1,14 @@
// A load cannot be release.
// expect: reject E4201
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.invalid_order
fn main() -> u64
effects [atomic, atomic.weak]
{
let c = atomic.new(1u64);
let v = atomic.load(ref c, release);
let t = atomic.take(move c);
v
}
@@ -0,0 +1,12 @@
// An atomic cell is linear: it is taken, moved or returned.
// expect: reject E3002
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.leak
fn main() -> u64
effects [atomic]
{
let c = atomic.new(1u64);
atomic.load(ref c, seq_cst)
}
@@ -0,0 +1,15 @@
// An atomic cell is borrowed with `ref`, never `mut`.
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.mut_borrow
fn main() -> u64
effects [atomic]
{
let c = atomic.new(1u64);
{
mut m = c;
}
atomic.take(move c)
}
@@ -0,0 +1,15 @@
// Orderings are words written in place, not values.
// expect: reject E7006
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.ordering_value
fn main() -> u64
effects [atomic]
{
let c = atomic.new(1u64);
let o = 1u64;
let v = atomic.load(ref c, o);
let t = atomic.take(move c);
v
}
@@ -0,0 +1,18 @@
// A cell is read only by atomic operations: `*r` is not one.
// expect: reject E4202
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.ordinary_read
fn main() -> u64
effects [atomic]
{
let c = atomic.new(1u64);
var v: u64 = 0;
{
ref r = c;
v = *r;
}
let t = atomic.take(move c);
v
}
@@ -0,0 +1,27 @@
// Taking a cell while its borrow is lent to a live task is a race.
// expect: reject E4101
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.take_while_lent
fn read(c: ref<atomic<u64>>) -> u64
effects [atomic]
{
atomic.load(c, seq_cst)
}
fn main() -> u64
effects [atomic, task.spawn, task.wait]
{
let c = atomic.new(1u64);
var v: u64 = 0;
{
ref r = c;
scope s {
let t = task.spawn(s, read, r);
v = atomic.take(move c);
let x = task.join(move t);
}
}
v
}
@@ -0,0 +1,14 @@
// A relaxed access needs atomic.weak besides atomic.
// expect: reject E3101
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.weak_effect
fn main() -> u64
effects [atomic]
{
let c = atomic.new(1u64);
let v = atomic.load(ref c, relaxed);
let t = atomic.take(move c);
v
}
@@ -0,0 +1,13 @@
// atomic<T> holds bool or an integer of at most 64 bits.
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.wide_cell
fn main() -> u64
effects [atomic]
{
let c = atomic.new(1u128);
let t = atomic.take(move c);
0
}
@@ -0,0 +1,14 @@
// Atomic operations take the cell through `ref`.
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.without_ref
fn main() -> u64
effects [atomic]
{
let c = atomic.new(1u64);
let v = atomic.load(c, seq_cst);
let t = atomic.take(move c);
v
}
@@ -0,0 +1,37 @@
// With seq_cst only, a function declares atomic and not atomic.weak: interleaving reasoning is exact.
// expect: run
// schedules: 30
// quantum: 1
// result: 1
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.sc_only
fn flip(x: ref<atomic<u64>>, y: ref<atomic<u64>>) -> u64
effects [atomic]
{
atomic.store(x, 1u64, seq_cst);
atomic.load(y, seq_cst)
}
fn main() -> u64
effects [atomic, task.spawn, task.wait]
{
let x = atomic.new(0u64);
let y = atomic.new(0u64);
var both: u64 = 0;
{
ref rx = x;
ref ry = y;
scope s {
let a = task.spawn(s, flip, rx, ry);
let b = task.spawn(s, flip, ry, rx);
let r0 = task.join(move a);
let r1 = task.join(move b);
both = r0 + r1;
}
}
let vx = atomic.take(move x);
let vy = atomic.take(move y);
if both == 0 { 0 } else { 1 }
}
@@ -0,0 +1,22 @@
// Quick Reference: atomics.
// expect: run
// result: 6
// phase: 6
// spec: docs/agent/quick-reference.md
module demo.qratomics
fn main() -> u64
effects [atomic, atomic.weak]
{
let count = atomic.new(0u64); // atomic<T>: bool or an integer up to 64 bits; linear
{
ref c = count; // every operation takes `ref`; lend `c` to tasks
let old = atomic.add.wrap(c, 1u64, relaxed); // swap, add.wrap, sub.wrap, and, or, xor return the old value
atomic.store(c, 5u64, release);
let v = atomic.load(c, acquire);
let (prev, exchanged) = atomic.compare_exchange(c, 5u64, 6u64, acq_rel, acquire);
atomic.fence(seq_cst);
}
let total = atomic.take(move count); // needs no live borrow of count
total
}