; Phase 5 reference program (hand-written AIR): atomic statistics. Three workers sum ; their parts of a lent array into a relaxed counter, raise a shared maximum with a ; compare-exchange loop, and count themselves done with an acq_rel add; the last one ; publishes the total, which it must see complete (release sequence of RMWs). ; expect: run ; schedules: 30 ; quantum: 3 ; result: 26091 ; phase: 5 ; spec: docs/spec/04-concurrency-and-atomics.md module test.atomic_stats air 0 lang 0.1 features [arrays, atomics, cancellation, effects, ownership, tasks] fn @work(%part: slice, %total: ref>, %max: ref>, %done: ref>, %report: ref>) -> u8 effects [atomic, atomic.weak] entry: %i0 = const.usize 0 %z = const.u64 0 br @scan(%i0, %z, %z) scan(%i: usize, %sum: u64, %hi: u64): %n = slice.len %part %end = eq.usize %i, %n cbr %end, @publish(%sum, %hi), @elem(%i, %sum, %hi) elem(%j: usize, %s: u64, %h: u64): %e = borrow.index %part, %j %x = load %e borrow.end %e %s2 = add.checked.u64 %s, %x %bigger = gt.u64 %x, %h cbr %bigger, @next(%j, %s2, %x), @next(%j, %s2, %h) next(%k: usize, %s3: u64, %h3: u64): %one = const.usize 1 %k2 = add.checked.usize %k, %one br @scan(%k2, %s3, %h3) publish(%psum: u64, %pmax: u64): %old = atomic.add.wrap.relaxed %total, %psum br @raise(%pmax) raise(%m: u64): %cur = atomic.load.relaxed %max %low = le.u64 %m, %cur cbr %low, @count(), @try(%m, %cur) try(%m2: u64, %seen: u64): %prev, %ok = atomic.cmpxchg.relaxed.relaxed %max, %seen, %m2 cbr %ok, @count(), @raise(%m2) count: %uno = const.u64 1 %f = atomic.add.wrap.acq_rel %done, %uno %two = const.u64 2 %last = eq.u64 %f, %two cbr %last, @report(), @finish() report: %t = atomic.load.relaxed %total atomic.store.release %report, %t br @finish() finish: %r = const.u8 0 ret %r fn @main() -> u64 effects [atomic, atomic.weak, task.spawn, task.wait] entry: %arr = local.alloc array local.zero %arr %w = borrow.mut %arr %a0 = const.u64 3 %a1 = const.u64 9 %a2 = const.u64 4 %a3 = const.u64 1 %a4 = const.u64 7 %a5 = const.u64 2 %i0 = const.usize 0 %i1 = const.usize 1 %i2 = const.usize 2 %i3 = const.usize 3 %i4 = const.usize 4 %i5 = const.usize 5 %e0 = borrow.index %w, %i0 store %e0, %a0 borrow.end %e0 %e1 = borrow.index %w, %i1 store %e1, %a1 borrow.end %e1 %e2 = borrow.index %w, %i2 store %e2, %a2 borrow.end %e2 %e3 = borrow.index %w, %i3 store %e3, %a3 borrow.end %e3 %e4 = borrow.index %w, %i4 store %e4, %a4 borrow.end %e4 %e5 = borrow.index %w, %i5 store %e5, %a5 borrow.end %e5 borrow.end %w %z = const.u64 0 %ctotal = atomic.new %z %cmax = atomic.new %z %cdone = atomic.new %z %creport = atomic.new %z %rtotal = borrow.ref %ctotal %rmax = borrow.ref %cmax %rdone = borrow.ref %cdone %rreport = borrow.ref %creport %ra = borrow.ref %arr %s = slice.make %ra %pa, %rest = slice.split %s, %i2 %pb, %pc = slice.split %rest, %i2 %sc = task.scope.begin %ta = task.spawn %sc, @work(%pa, %rtotal, %rmax, %rdone, %rreport) %tb = task.spawn %sc, @work(%pb, %rtotal, %rmax, %rdone, %rreport) %tc = task.spawn %sc, @work(%pc, %rtotal, %rmax, %rdone, %rreport) %xa = task.join %ta %xb = task.join %tb %xc = task.join %tc task.scope.end %sc borrow.end %pb borrow.end %pc borrow.end %pa borrow.end %rest borrow.end %s borrow.end %ra borrow.end %rtotal borrow.end %rmax borrow.end %rdone borrow.end %rreport %vtotal = atomic.take %ctotal %vmax = atomic.take %cmax %vdone = atomic.take %cdone %vreport = atomic.take %creport %k1000 = const.u64 1000 %k10 = const.u64 10 %p1 = mul.checked.u64 %vtotal, %k1000 %p2 = mul.checked.u64 %vmax, %k10 %p3 = add.checked.u64 %p1, %p2 %same = eq.u64 %vreport, %vtotal cbr %same, @ok(%p3), @no(%p3) ok(%acc: u64): %one = const.u64 1 %res = add.checked.u64 %acc, %one ret %res no(%acc2: u64): ret %acc2