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,19 @@
|
||||
; add.wrap is defined for integers only (spec 04 §24.2).
|
||||
; expect: reject E1001
|
||||
; phase: 5
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.add_bool
|
||||
air 0
|
||||
lang 0.1
|
||||
features [atomics, effects, ownership]
|
||||
|
||||
fn @main() -> bool
|
||||
effects [atomic]
|
||||
entry:
|
||||
%t = const.bool true
|
||||
%c = atomic.new %t
|
||||
%r = borrow.ref %c
|
||||
%x = atomic.add.wrap.seq_cst %r, %t
|
||||
borrow.end %r
|
||||
%v = atomic.take %c
|
||||
ret %v
|
||||
@@ -0,0 +1,21 @@
|
||||
; A failed compare-exchange is a read: no release part (spec 04 §24.1).
|
||||
; expect: reject E4201
|
||||
; phase: 5
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.cmpxchg_failure_release
|
||||
air 0
|
||||
lang 0.1
|
||||
features [atomics, effects, ownership]
|
||||
|
||||
fn @main() -> u64
|
||||
effects [atomic, atomic.weak]
|
||||
entry:
|
||||
%z = const.u64 0
|
||||
%c = atomic.new %z
|
||||
%r = borrow.ref %c
|
||||
%one = const.u64 1
|
||||
%o, %ok = atomic.cmpxchg.acq_rel.acq_rel %r, %z, %one
|
||||
borrow.end %r
|
||||
%v = atomic.take %c
|
||||
%k = const.u64 0
|
||||
ret %k
|
||||
@@ -0,0 +1,21 @@
|
||||
; A compare-exchange failure ordering is never stronger than its success ordering (spec 04 §24.1).
|
||||
; expect: reject E4201
|
||||
; phase: 5
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.cmpxchg_failure_stronger
|
||||
air 0
|
||||
lang 0.1
|
||||
features [atomics, effects, ownership]
|
||||
|
||||
fn @main() -> u64
|
||||
effects [atomic, atomic.weak]
|
||||
entry:
|
||||
%z = const.u64 0
|
||||
%c = atomic.new %z
|
||||
%r = borrow.ref %c
|
||||
%one = const.u64 1
|
||||
%o, %ok = atomic.cmpxchg.relaxed.acquire %r, %z, %one
|
||||
borrow.end %r
|
||||
%v = atomic.take %c
|
||||
%k = const.u64 0
|
||||
ret %k
|
||||
@@ -0,0 +1,20 @@
|
||||
; There is no relaxed fence (spec 04 §27).
|
||||
; expect: reject E4201
|
||||
; phase: 5
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.fence_relaxed
|
||||
air 0
|
||||
lang 0.1
|
||||
features [atomics, effects, ownership]
|
||||
|
||||
fn @main() -> u64
|
||||
effects [atomic, atomic.weak]
|
||||
entry:
|
||||
%z = const.u64 0
|
||||
%c = atomic.new %z
|
||||
%r = borrow.ref %c
|
||||
atomic.fence.relaxed
|
||||
borrow.end %r
|
||||
%v = atomic.take %c
|
||||
%k = const.u64 0
|
||||
ret %k
|
||||
@@ -0,0 +1,20 @@
|
||||
; An atomic cell is not IslandSafe: a frozen graph is deeply immutable (spec 02 §6).
|
||||
; expect: reject E2111
|
||||
; phase: 5
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.island_content
|
||||
air 0
|
||||
lang 0.1
|
||||
features [atomics, capabilities, effects, heap, islands, ownership, resources]
|
||||
|
||||
fn @main(%heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc]
|
||||
entry:
|
||||
%z = const.u64 0
|
||||
%c = atomic.new %z
|
||||
island.create %c, %heap, ok -> @made, err -> @oom
|
||||
made(%i: island<atomic<u64>>):
|
||||
island.destroy %i
|
||||
ret %z
|
||||
oom(%e: alloc_error):
|
||||
trap
|
||||
@@ -0,0 +1,15 @@
|
||||
; An atomic cell is linear: it is taken, moved or returned (spec 04 §23).
|
||||
; expect: reject E3002
|
||||
; phase: 5
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.leak
|
||||
air 0
|
||||
lang 0.1
|
||||
features [atomics, effects, ownership]
|
||||
|
||||
fn @main() -> u64
|
||||
effects []
|
||||
entry:
|
||||
%z = const.u64 0
|
||||
%c = atomic.new %z
|
||||
ret %z
|
||||
@@ -0,0 +1,20 @@
|
||||
; A load cannot be release (spec 04 §24).
|
||||
; expect: reject E4201
|
||||
; phase: 5
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.load_release
|
||||
air 0
|
||||
lang 0.1
|
||||
features [atomics, effects, ownership]
|
||||
|
||||
fn @main() -> u64
|
||||
effects [atomic, atomic.weak]
|
||||
entry:
|
||||
%z = const.u64 0
|
||||
%c = atomic.new %z
|
||||
%r = borrow.ref %c
|
||||
%x = atomic.load.release %r
|
||||
borrow.end %r
|
||||
%v = atomic.take %c
|
||||
%k = const.u64 0
|
||||
ret %k
|
||||
@@ -0,0 +1,20 @@
|
||||
; Every atomic memory operation needs the effect atomic (spec 04 §25).
|
||||
; expect: reject E3101
|
||||
; phase: 5
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.missing_atomic_effect
|
||||
air 0
|
||||
lang 0.1
|
||||
features [atomics, effects, ownership]
|
||||
|
||||
fn @main() -> u64
|
||||
effects []
|
||||
entry:
|
||||
%z = const.u64 0
|
||||
%c = atomic.new %z
|
||||
%r = borrow.ref %c
|
||||
%x = atomic.load.seq_cst %r
|
||||
borrow.end %r
|
||||
%v = atomic.take %c
|
||||
%k = const.u64 0
|
||||
ret %k
|
||||
@@ -0,0 +1,20 @@
|
||||
; A non-seq_cst access needs atomic.weak besides atomic (spec 04 §25).
|
||||
; expect: reject E3101
|
||||
; phase: 5
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.missing_weak_effect
|
||||
air 0
|
||||
lang 0.1
|
||||
features [atomics, effects, ownership]
|
||||
|
||||
fn @main() -> u64
|
||||
effects [atomic]
|
||||
entry:
|
||||
%z = const.u64 0
|
||||
%c = atomic.new %z
|
||||
%r = borrow.ref %c
|
||||
%x = atomic.load.acquire %r
|
||||
borrow.end %r
|
||||
%v = atomic.take %c
|
||||
%k = const.u64 0
|
||||
ret %k
|
||||
@@ -0,0 +1,21 @@
|
||||
; Atomic operations take ref<atomic<T>>, never mut (spec 04 §23).
|
||||
; expect: reject E1001
|
||||
; phase: 5
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.mut_operand
|
||||
air 0
|
||||
lang 0.1
|
||||
features [atomics, effects, ownership]
|
||||
|
||||
fn @peek(%c: mut<atomic<u64>>) -> u64
|
||||
effects [atomic]
|
||||
entry:
|
||||
%x = atomic.load.seq_cst %c
|
||||
%k = const.u64 0
|
||||
ret %k
|
||||
|
||||
fn @main() -> u64
|
||||
effects []
|
||||
entry:
|
||||
%k = const.u64 0
|
||||
ret %k
|
||||
@@ -0,0 +1,20 @@
|
||||
; atomic<T> needs the feature atomics in the header (spec 07 §8.1).
|
||||
; expect: reject E0006
|
||||
; phase: 5
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.not_declared
|
||||
air 0
|
||||
lang 0.1
|
||||
features [effects, ownership]
|
||||
|
||||
fn @peek(%c: ref<atomic<u64>>) -> u64
|
||||
effects []
|
||||
entry:
|
||||
%k = const.u64 0
|
||||
ret %k
|
||||
|
||||
fn @main() -> u64
|
||||
effects []
|
||||
entry:
|
||||
%k = const.u64 0
|
||||
ret %k
|
||||
@@ -0,0 +1,20 @@
|
||||
; An atomic cell is read only by atomic operations (spec 04 §28).
|
||||
; expect: reject E4202
|
||||
; phase: 5
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.ordinary_load
|
||||
air 0
|
||||
lang 0.1
|
||||
features [atomics, effects, ownership]
|
||||
|
||||
fn @main() -> u64
|
||||
effects [atomic, atomic.weak]
|
||||
entry:
|
||||
%z = const.u64 0
|
||||
%c = atomic.new %z
|
||||
%r = borrow.ref %c
|
||||
%x = load %r
|
||||
borrow.end %r
|
||||
%v = atomic.take %c
|
||||
%k = const.u64 0
|
||||
ret %k
|
||||
@@ -0,0 +1,21 @@
|
||||
; A store cannot be acquire (spec 04 §24).
|
||||
; expect: reject E4201
|
||||
; phase: 5
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.store_acquire
|
||||
air 0
|
||||
lang 0.1
|
||||
features [atomics, effects, ownership]
|
||||
|
||||
fn @main() -> u64
|
||||
effects [atomic, atomic.weak]
|
||||
entry:
|
||||
%z = const.u64 0
|
||||
%c = atomic.new %z
|
||||
%r = borrow.ref %c
|
||||
%one = const.u64 1
|
||||
atomic.store.acquire %r, %one
|
||||
borrow.end %r
|
||||
%v = atomic.take %c
|
||||
%k = const.u64 0
|
||||
ret %k
|
||||
@@ -0,0 +1,16 @@
|
||||
; atomic<T> is linear: as a struct field it needs linear_aggregates (spec 04 §23).
|
||||
; expect: reject E0006
|
||||
; phase: 5
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.struct_field
|
||||
air 0
|
||||
lang 0.1
|
||||
features [atomics, effects, ownership]
|
||||
|
||||
type @S = struct { n: atomic<u64> }
|
||||
|
||||
fn @main() -> u64
|
||||
effects []
|
||||
entry:
|
||||
%k = const.u64 0
|
||||
ret %k
|
||||
@@ -0,0 +1,19 @@
|
||||
; atomic.take needs every borrow of the cell ended (spec 04 §28).
|
||||
; expect: reject E2101
|
||||
; phase: 5
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.take_while_borrowed
|
||||
air 0
|
||||
lang 0.1
|
||||
features [atomics, effects, ownership]
|
||||
|
||||
fn @main() -> u64
|
||||
effects [atomic]
|
||||
entry:
|
||||
%z = const.u64 0
|
||||
%c = atomic.new %z
|
||||
%r = borrow.ref %c
|
||||
%v = atomic.take %c
|
||||
%x = atomic.load.seq_cst %r
|
||||
borrow.end %r
|
||||
ret %v
|
||||
@@ -0,0 +1,28 @@
|
||||
; atomic.take while the cell is lent to a live task is a race (spec 04 §28).
|
||||
; expect: reject E4101
|
||||
; phase: 5
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.take_while_lent
|
||||
air 0
|
||||
lang 0.1
|
||||
features [atomics, cancellation, effects, ownership, tasks]
|
||||
|
||||
fn @reader(%c: ref<atomic<u64>>) -> u64
|
||||
effects [atomic]
|
||||
entry:
|
||||
%x = atomic.load.seq_cst %c
|
||||
ret %x
|
||||
|
||||
fn @main() -> u64
|
||||
effects [atomic, task.spawn, task.wait]
|
||||
entry:
|
||||
%z = const.u64 0
|
||||
%c = atomic.new %z
|
||||
%r = borrow.ref %c
|
||||
%s = task.scope.begin
|
||||
%t = task.spawn %s, @reader(%r)
|
||||
%v = atomic.take %c
|
||||
%x = task.join %t
|
||||
task.scope.end %s
|
||||
borrow.end %r
|
||||
ret %v
|
||||
@@ -0,0 +1,20 @@
|
||||
; atomic<T> holds bool or an integer of at most 64 bits (spec 04 §23).
|
||||
; expect: reject E1001
|
||||
; phase: 5
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.u128_cell
|
||||
air 0
|
||||
lang 0.1
|
||||
features [atomics, effects, ownership]
|
||||
|
||||
fn @main() -> u64
|
||||
effects [atomic]
|
||||
entry:
|
||||
%z = const.u128 0
|
||||
%c = atomic.new %z
|
||||
%r = borrow.ref %c
|
||||
%x = atomic.load.seq_cst %r
|
||||
borrow.end %r
|
||||
%v = atomic.take %c
|
||||
%k = const.u64 0
|
||||
ret %k
|
||||
@@ -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
|
||||
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user