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:
1 parent
ce9ef03bc0
commit
4242132b2a
70 files changed
+2630
-80
No files matched your search
@@ -0,0 +1,27 @@
|
||||
; A strong compare-exchange returns the old value and whether it exchanged; a non-seq_cst one needs atomic.weak.
|
||||
; expect: run
|
||||
; result: 14
|
||||
; phase: 5
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.atomic_cmpxchg
|
||||
air 0
|
||||
lang 0.1
|
||||
features [atomics, effects, ownership]
|
||||
|
||||
fn @main() -> u64
|
||||
effects [atomic, atomic.weak]
|
||||
entry:
|
||||
%five = const.u64 5
|
||||
%c = atomic.new %five
|
||||
%r = borrow.ref %c
|
||||
%seven = const.u64 7
|
||||
%old1, %ok1 = atomic.cmpxchg.seq_cst.seq_cst %r, %five, %seven
|
||||
assert %ok1
|
||||
%old2, %ok2 = atomic.cmpxchg.acq_rel.acquire %r, %five, %five
|
||||
%no = const.bool true
|
||||
%failed = xor.bool %ok2, %no
|
||||
assert %failed
|
||||
borrow.end %r
|
||||
%v = atomic.take %c
|
||||
%sum = add.checked.u64 %v, %old2
|
||||
ret %sum
|
||||
@@ -0,0 +1,36 @@
|
||||
; Two tasks add to one cell with relaxed add.wrap: atomicity makes the total exact on every schedule.
|
||||
; expect: run
|
||||
; schedules: 30
|
||||
; quantum: 1
|
||||
; result: 6
|
||||
; phase: 5
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.atomic_counter
|
||||
air 0
|
||||
lang 0.1
|
||||
features [atomics, cancellation, effects, ownership, tasks]
|
||||
|
||||
fn @bump(%c: ref<atomic<u64>>) -> u64
|
||||
effects [atomic, atomic.weak]
|
||||
entry:
|
||||
%one = const.u64 1
|
||||
%a = atomic.add.wrap.relaxed %c, %one
|
||||
%b = atomic.add.wrap.relaxed %c, %one
|
||||
%d = atomic.add.wrap.relaxed %c, %one
|
||||
ret %d
|
||||
|
||||
fn @main() -> u64
|
||||
effects [atomic, atomic.weak, task.spawn, task.wait]
|
||||
entry:
|
||||
%z = const.u64 0
|
||||
%cell = atomic.new %z
|
||||
%r = borrow.ref %cell
|
||||
%s = task.scope.begin
|
||||
%t1 = task.spawn %s, @bump(%r)
|
||||
%t2 = task.spawn %s, @bump(%r)
|
||||
%x1 = task.join %t1
|
||||
%x2 = task.join %t2
|
||||
task.scope.end %s
|
||||
borrow.end %r
|
||||
%total = atomic.take %cell
|
||||
ret %total
|
||||
@@ -0,0 +1,48 @@
|
||||
; Release/acquire publication: whenever the reader sees the flag, it sees the data (never 1 with 0).
|
||||
; expect: run
|
||||
; schedules: 30
|
||||
; quantum: 1
|
||||
; result: 1
|
||||
; phase: 5
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.atomic_publication
|
||||
air 0
|
||||
lang 0.1
|
||||
features [atomics, cancellation, effects, ownership, tasks]
|
||||
|
||||
fn @writer(%d: ref<atomic<u64>>, %f: ref<atomic<bool>>) -> u64
|
||||
effects [atomic, atomic.weak]
|
||||
entry:
|
||||
%v = const.u64 42
|
||||
atomic.store.relaxed %d, %v
|
||||
%t = const.bool true
|
||||
atomic.store.release %f, %t
|
||||
ret %v
|
||||
|
||||
fn @main() -> u64
|
||||
effects [atomic, atomic.weak, task.spawn, task.wait]
|
||||
entry:
|
||||
%z = const.u64 0
|
||||
%no = const.bool false
|
||||
%data = atomic.new %z
|
||||
%flag = atomic.new %no
|
||||
%rd = borrow.ref %data
|
||||
%rf = borrow.ref %flag
|
||||
%s = task.scope.begin
|
||||
%t = task.spawn %s, @writer(%rd, %rf)
|
||||
%seen = atomic.load.acquire %rf
|
||||
%val = atomic.load.relaxed %rd
|
||||
%w = task.join %t
|
||||
task.scope.end %s
|
||||
borrow.end %rd
|
||||
borrow.end %rf
|
||||
%dv = atomic.take %data
|
||||
%fv = atomic.take %flag
|
||||
cbr %seen, @published(), @done()
|
||||
published:
|
||||
%ok = eq.u64 %val, %w
|
||||
assert %ok
|
||||
br @done()
|
||||
done:
|
||||
%one = const.u64 1
|
||||
ret %one
|
||||
@@ -0,0 +1,44 @@
|
||||
; Every read-modify-write returns the old value; add.wrap and sub.wrap are modular; bool cells take xchg, and, or, xor.
|
||||
; expect: run
|
||||
; result: 10
|
||||
; phase: 5
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.atomic_rmw_ops
|
||||
air 0
|
||||
lang 0.1
|
||||
features [atomics, effects, ownership]
|
||||
|
||||
fn @main() -> u8
|
||||
effects [atomic]
|
||||
entry:
|
||||
%z = const.u8 0
|
||||
%c = atomic.new %z
|
||||
%r = borrow.ref %c
|
||||
%one = const.u8 1
|
||||
%o1 = atomic.sub.wrap.seq_cst %r, %one
|
||||
%v6 = const.u8 6
|
||||
%o2 = atomic.xchg.seq_cst %r, %v6
|
||||
%v3 = const.u8 3
|
||||
%o3 = atomic.and.seq_cst %r, %v3
|
||||
%v8 = const.u8 8
|
||||
%o4 = atomic.or.seq_cst %r, %v8
|
||||
%o5 = atomic.xor.seq_cst %r, %v3
|
||||
%o6 = atomic.add.wrap.seq_cst %r, %one
|
||||
borrow.end %r
|
||||
%final = atomic.take %c
|
||||
%e1 = const.u8 255
|
||||
%k1 = eq.u8 %o2, %e1
|
||||
assert %k1
|
||||
%e2 = const.u8 2
|
||||
%k2 = eq.u8 %o4, %e2
|
||||
assert %k2
|
||||
%t = const.bool true
|
||||
%b = atomic.new %t
|
||||
%rb = borrow.ref %b
|
||||
%ob = atomic.xor.seq_cst %rb, %t
|
||||
borrow.end %rb
|
||||
%fb = atomic.take %b
|
||||
assert %ob
|
||||
%nf = xor.bool %fb, %t
|
||||
assert %nf
|
||||
ret %final
|
||||
Reference in new issue
Block a user