Skip to content

Commit 0ad7491

Browse files
authored
Fix Fix load and store for sizeof(T) == 0 (#16)
1 parent 0aaaa37 commit 0ad7491

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "UnsafeAtomicsLLVM"
22
uuid = "d80eeb9a-aca5-4d75-85e5-170c8b632249"
33
authors = ["Takafumi Arakaki <aka.tkf@gmail.com> and contributors"]
4-
version = "0.2.1"
4+
version = "0.2.2"
55

66
[deps]
77
LLVM = "929cbde3-209d-540e-8aea-75f648917ca0"

src/atomics.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,9 @@ end
191191
if sizeof(T) == 0
192192
# Mimicking what `Core.Intrinsics.atomic_pointerset` generates.
193193
# See: https://github.com/JuliaLang/julia/blob/v1.7.2/src/cgutils.cpp#L1570-L1572
194-
is_stronger_than_monotonic(order) || return :ptr
195194
return quote
196-
Core.Intrinsics.fence($(QuoteNode(order)))
195+
is_stronger_than_monotonic(_valueof(order)) || return :ptr
196+
Core.Intrinsics.atomic_fence(_valueof(order))
197197
ptr
198198
end
199199
end

test/runtests.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@ function check_default_ordering(T::Type)
1010
xs = T[rand(T), rand(T)]
1111
x1 = rand(T)
1212
x2 = rand(T)
13+
check_default_ordering(xs, x1, x2)
14+
end
15+
16+
function check_default_ordering(xs::AbstractArray{T}, x1::T, x2::T) where {T}
1317
@debug "xs=$(repr(xs)) x1=$(repr(x1)) x2=$(repr(x2))"
1418

1519
ptr = llvmptr(xs, 1)
1620
GC.@preserve xs begin
1721
@test UnsafeAtomics.load(ptr) === xs[1]
1822
UnsafeAtomics.store!(ptr, x1)
1923
@test xs[1] === x1
24+
sizeof(T) == 0 && return # CAS hangs on zero sized data...
2025
desired = (old = x1, success = true)
2126
@test UnsafeAtomics.cas!(ptr, x1, x2) === (old = x1, success = true)
2227
@test xs[1] === x2
@@ -37,6 +42,10 @@ function test_explicit_ordering(T::Type = UInt)
3742
xs = T[rand(T), rand(T)]
3843
x1 = rand(T)
3944
x2 = rand(T)
45+
test_explicit_ordering(xs, x1, x2)
46+
end
47+
48+
function test_explicit_ordering(xs::AbstractArray{T}, x1::T, x2::T) where {T}
4049
@debug "xs=$(repr(xs)) x1=$(repr(x1)) x2=$(repr(x2))"
4150

4251
ptr = llvmptr(xs, 1)
@@ -45,6 +54,7 @@ function test_explicit_ordering(T::Type = UInt)
4554
@test UnsafeAtomics.load(ptr, acquire) === xs[1]
4655
UnsafeAtomics.store!(ptr, x1, release)
4756
@test xs[1] === x1
57+
sizeof(T) == 0 && return # CAS hangs on zero sized data...
4858
desired = (old = x1, success = true)
4959
@test UnsafeAtomics.cas!(ptr, x1, x2, acq_rel, acquire) === desired
5060
@test xs[1] === x2
@@ -79,4 +89,10 @@ end
7989
check_default_ordering(T)
8090
test_explicit_ordering(T)
8191
end
92+
93+
@testset "Zero-sized types" begin
94+
@test sizeof(Nothing) == 0
95+
check_default_ordering([nothing, nothing], nothing, nothing)
96+
test_explicit_ordering([nothing, nothing], nothing, nothing)
97+
end
8298
end

0 commit comments

Comments
 (0)