Skip to content

Commit ac553a7

Browse files
committed
adding rough inc / dec functions
1 parent b7b3b38 commit ac553a7

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/atomics.jl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,26 @@ for (name, op) in ops
4949
end
5050
end
5151

52-
# TODO: Not sure what this one is...
5352
function atomic_cas!(ptr::Ptr{T}, old::T, new::T) where T
5453
Core.Intrinsics.atomic_pointerreplace(ptr::Ptr{T}, old, new, :release_acquire, :monotonic)
5554
end
5655

57-
# TODO: Not sure what this one is...
56+
# Implementation of: (((old == 0) | (old > b)) ? b : (old-1)), returns old
5857
function atomic_dec!(ptr::Ptr{T}, b::T) where T
59-
Core.Intrinsics.atomic_pointermodify(ptr, -, 1, :monotonic)
58+
if (unsafe_load(ptr) == 0 | unsafe_load(ptr) > b)
59+
Core.Intrinsics.atomic_pointerswap(ptr, b, :monotonic)
60+
else
61+
Core.Intrinsics.atomic_pointermodify(ptr, -, 1, :monotonic)
62+
end
6063
end
6164

62-
# TODO: Not sure what this one is...
65+
# implementation of: ((old >= b) ? 0 : (old+1)), returns old
6366
function atomic_inc!(ptr::Ptr{T}, b::T) where T
64-
Core.Intrinsics.atomic_pointermodify(ptr, +, 1, :monotonic)
67+
if unsafe_load(ptr) >= b
68+
Core.Intrinsics.atomic_pointerswap(ptr, 0, :monotonic)
69+
else
70+
Core.Intrinsics.atomic_pointermodify(ptr, +, 1, :monotonic)
71+
end
6572
end
6673

6774
function atomic_xchg!(ptr::Ptr{T}, b::T) where T

0 commit comments

Comments
 (0)