Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ext/UnsafeAtomicsLLVM/atomics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,10 @@ end
if sizeof(T) == 0
# Mimicking what `Core.Intrinsics.atomic_pointerset` generates.
# See: https://github.com/JuliaLang/julia/blob/v1.7.2/src/cgutils.cpp#L1570-L1572
is_stronger_than_monotonic(order) || return :ptr
return quote
Core.Intrinsics.fence($(QuoteNode(order)))
ptr
is_stronger_than_monotonic(_valueof(order)) || return ptr
Core.Intrinsics.atomic_fence(_valueof(order))
return ptr
end
end
llvm_order = _valueof(llvm_from_julia_ordering(order()))
Expand Down
17 changes: 17 additions & 0 deletions test/UnsafeAtomicsLLVM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ function check_default_ordering(T::Type)
xs = T[rand(T), rand(T)]
x1 = rand(T)
x2 = rand(T)
check_default_ordering(xs, x1, x2)
end

function check_default_ordering(xs::AbstractArray{T}, x1::T, x2::T) where T
@debug "xs=$(repr(xs)) x1=$(repr(x1)) x2=$(repr(x2))"

ptr = llvmptr(xs, 1)
GC.@preserve xs begin
@test UnsafeAtomics.load(ptr) === xs[1]
UnsafeAtomics.store!(ptr, x1)
@test xs[1] === x1
sizeof(T) == 0 && return # CAS hangs on zero sized data...
desired = (old = x1, success = true)
@test UnsafeAtomics.cas!(ptr, x1, x2) === (old = x1, success = true)
@test xs[1] === x2
Expand All @@ -37,6 +42,10 @@ function test_explicit_ordering(T::Type = UInt)
xs = T[rand(T), rand(T)]
x1 = rand(T)
x2 = rand(T)
test_explicit_ordering(xs, x1, x2)
end

function test_explicit_ordering(xs::AbstractArray{T}, x1::T, x2::T) where T
@debug "xs=$(repr(xs)) x1=$(repr(x1)) x2=$(repr(x2))"

ptr = llvmptr(xs, 1)
Expand All @@ -45,6 +54,7 @@ function test_explicit_ordering(T::Type = UInt)
@test UnsafeAtomics.load(ptr, acquire) === xs[1]
UnsafeAtomics.store!(ptr, x1, release)
@test xs[1] === x1
sizeof(T) == 0 && return # CAS hangs on zero sized data...
desired = (old = x1, success = true)
@test UnsafeAtomics.cas!(ptr, x1, x2, acq_rel, acquire) === desired
@test xs[1] === x2
Expand Down Expand Up @@ -74,9 +84,16 @@ function test_explicit_ordering(T::Type = UInt)
end
end


@testset "UnsafeAtomicsLLVM" begin
@testset for T in inttypes
check_default_ordering(T)
test_explicit_ordering(T)
end

@testset "Zero-sized types" begin
@test sizeof(Nothing) == 0
check_default_ordering([nothing, nothing], nothing, nothing)
test_explicit_ordering([nothing, nothing], nothing, nothing)
end
end