Files
dev 0879b9497b Phase 5: race detector (P5-6) and the atomic_stats reference program (P5-9b)
Dynamic data-race detector for ordinary memory: vector clocks are the
task-keyed part of the weak-memory views, so happens-before follows every
synchronization edge already modelled; per-cell shadows with path
granularity. Enabled for every conformance run test (433, no false
positive); a unit test shows an unverified racy program caught on every
schedule. Reference program atomic_stats in .aji and hand-written AIR by an
independent author (equivalent:, 30 schedule seeds). Author feedback:
07a clarifications; Q-025 (no array construction) logged.
2026-10-06 08:11:25 +03:00

86 lines
2.7 KiB
Rust

// Reference program (Phase 5): atomic statistics. Three workers each sum one third
// of a lent array into a relaxed counter and raise a shared maximum with a
// compare_exchange loop. Each then counts itself done with an acq_rel add.wrap; the
// last one to finish synchronizes with the others through that counter (a release
// sequence of read-modify-writes), so its relaxed read of the total sees every add,
// and it publishes the total as a report. Result: total * 1000 + max * 10 + (report == total).
// expect: run
// schedules: 30
// quantum: 3
// result: 26091
// equivalent: phase5/run/program_atomic_stats.air
// phase: 6
// spec: docs/spec/04-concurrency-and-atomics.md
module demo.atomic_stats
fn work(part: slice<u64>, total: ref<atomic<u64>>, max: ref<atomic<u64>>, done: ref<atomic<u64>>, report: ref<atomic<u64>>) -> u8
effects [atomic, atomic.weak]
{
var sum: u64 = 0;
var top: u64 = 0;
var i: usize = 0;
while i < part.len {
sum = sum + part[i];
if part[i] > top {
top = part[i];
}
i = i + 1;
}
let before = atomic.add.wrap(total, sum, relaxed);
loop {
let current = atomic.load(max, relaxed);
if top <= current {
break;
}
let (old, exchanged) = atomic.compare_exchange(max, current, top, relaxed, relaxed);
if exchanged {
break;
}
}
let finished = atomic.add.wrap(done, 1u64, acq_rel);
if finished == 2 {
let seen = atomic.load(total, relaxed);
atomic.store(report, seen, release);
}
0u8
}
fn main() -> u64
effects [atomic, atomic.weak, task.spawn, task.wait]
{
var data: [u64; 6] = [0u64; 6];
data[0] = 3;
data[1] = 9;
data[2] = 4;
data[3] = 1;
data[4] = 7;
data[5] = 2;
let total = atomic.new(0u64);
let max = atomic.new(0u64);
let done = atomic.new(0u64);
let report = atomic.new(0u64);
{
ref t = total;
ref m = max;
ref d = done;
ref r = report;
ref all = data;
let (first, rest) = all.split_at(2);
let (second, third) = rest.split_at(2);
scope s {
let a = task.spawn(s, work, first, t, m, d, r);
let b = task.spawn(s, work, second, t, m, d, r);
let c = task.spawn(s, work, third, t, m, d, r);
let ra = task.join(move a);
let rb = task.join(move b);
let rc = task.join(move c);
}
}
let sum = atomic.take(move total);
let top = atomic.take(move max);
let n = atomic.take(move done);
let published = atomic.take(move report);
let flag: u64 = if published == sum { 1 } else { 0 };
sum * 1000 + top * 10 + flag
}