Skip to content

Commit

Permalink
Fix Fix load and store for sizeof(T) == 0 (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
vchuravy authored Nov 30, 2024
1 parent 0aaaa37 commit 0ad7491
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "UnsafeAtomicsLLVM"
uuid = "d80eeb9a-aca5-4d75-85e5-170c8b632249"
authors = ["Takafumi Arakaki <[email protected]> and contributors"]
version = "0.2.1"
version = "0.2.2"

[deps]
LLVM = "929cbde3-209d-540e-8aea-75f648917ca0"
Expand Down
4 changes: 2 additions & 2 deletions src/atomics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ 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)))
is_stronger_than_monotonic(_valueof(order)) || return :ptr
Core.Intrinsics.atomic_fence(_valueof(order))
ptr
end
end
Expand Down
16 changes: 16 additions & 0 deletions test/runtests.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 @@ -79,4 +89,10 @@ end
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

2 comments on commit 0ad7491

@vchuravy
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/120438

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.2 -m "<description of version>" 0ad7491d5b312ba4d3a84a50e2ec63146db8e162
git push origin v0.2.2

Please sign in to comment.