|
| 1 | +module TestCore |
| 2 | + |
| 3 | +import UnsafeAtomicsLLVM |
| 4 | + |
| 5 | +using UnsafeAtomics: UnsafeAtomics, acquire, release, acq_rel |
| 6 | +using UnsafeAtomics.Internal: OP_RMW_TABLE, inttypes |
| 7 | +using Test |
| 8 | + |
| 9 | +llvmptr(xs::Array, i) = reinterpret(Core.LLVMPtr{eltype(xs),0}, pointer(xs, i)) |
| 10 | + |
| 11 | +function test_default_ordering() |
| 12 | + @testset for T in inttypes |
| 13 | + check_default_ordering(T) |
| 14 | + end |
| 15 | +end |
| 16 | + |
| 17 | +function check_default_ordering(T::Type) |
| 18 | + xs = T[rand(T), rand(T)] |
| 19 | + x1 = rand(T) |
| 20 | + x2 = rand(T) |
| 21 | + @debug "xs=$(repr(xs)) x1=$(repr(x1)) x2=$(repr(x2))" |
| 22 | + |
| 23 | + ptr = llvmptr(xs, 1) |
| 24 | + GC.@preserve xs begin |
| 25 | + @test UnsafeAtomics.load(ptr) === xs[1] |
| 26 | + UnsafeAtomics.store!(ptr, x1) |
| 27 | + @test xs[1] === x1 |
| 28 | + desired = (old = x1, success = true) |
| 29 | + @test UnsafeAtomics.cas!(ptr, x1, x2) === (old = x1, success = true) |
| 30 | + @test xs[1] === x2 |
| 31 | + @testset for (op, name) in OP_RMW_TABLE |
| 32 | + xs[1] = x1 |
| 33 | + @test UnsafeAtomics.modify!(ptr, op, x2) === (x1 => op(x1, x2)) |
| 34 | + @test xs[1] === op(x1, x2) |
| 35 | + |
| 36 | + rmw = getfield(UnsafeAtomics, Symbol(name, :!)) |
| 37 | + xs[1] = x1 |
| 38 | + @test rmw(ptr, x2) === x1 |
| 39 | + @test xs[1] === op(x1, x2) |
| 40 | + end |
| 41 | + end |
| 42 | +end |
| 43 | + |
| 44 | +function test_explicit_ordering(T::Type = UInt) |
| 45 | + xs = T[rand(T), rand(T)] |
| 46 | + x1 = rand(T) |
| 47 | + x2 = rand(T) |
| 48 | + @debug "xs=$(repr(xs)) x1=$(repr(x1)) x2=$(repr(x2))" |
| 49 | + |
| 50 | + ptr = llvmptr(xs, 1) |
| 51 | + GC.@preserve xs begin |
| 52 | + |
| 53 | + @test UnsafeAtomics.load(ptr, acquire) === xs[1] |
| 54 | + UnsafeAtomics.store!(ptr, x1, release) |
| 55 | + @test xs[1] === x1 |
| 56 | + desired = (old = x1, success = true) |
| 57 | + @test UnsafeAtomics.cas!(ptr, x1, x2, acq_rel, acquire) === desired |
| 58 | + @test xs[1] === x2 |
| 59 | + @testset for (op, name) in OP_RMW_TABLE |
| 60 | + xs[1] = x1 |
| 61 | + @test UnsafeAtomics.modify!(ptr, op, x2, acq_rel) === (x1 => op(x1, x2)) |
| 62 | + @test xs[1] === op(x1, x2) |
| 63 | + |
| 64 | + rmw = getfield(UnsafeAtomics, Symbol(name, :!)) |
| 65 | + xs[1] = x1 |
| 66 | + @test rmw(ptr, x2, acquire) === x1 |
| 67 | + @test xs[1] === op(x1, x2) |
| 68 | + end |
| 69 | + end |
| 70 | +end |
| 71 | + |
| 72 | +end # module |
0 commit comments