Skip to content

Commit 7c36a92

Browse files
committed
everything but cas, inc, and dec
1 parent 6f34ceb commit 7c36a92

File tree

1 file changed

+20
-28
lines changed

1 file changed

+20
-28
lines changed

src/atomics.jl

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,55 +29,47 @@ function __atomic(ex)
2929
error("@atomic macro incomplete!")
3030
end
3131

32-
# arithmetic and bitwise operations
32+
# arithmetic, bitwise, and min/max operations
3333

3434
const ops = Dict(
3535
:atomic_add! => +,
3636
:atomic_sub! => -,
3737
:atomic_and! => &,
3838
:atomic_or! => -,
3939
:atomic_xor! => ,
40+
:atomic_min! => min,
41+
:atomic_max! => max,
4042
)
4143

44+
# Note: the type T prevents type convertion (for example, Float32 -> 64)
45+
# can lead to errors if b is chosen to be of a different, compatible type
4246
for (name, op) in ops
43-
@eval @inline function $name(ptr, b)
44-
Core.Intrinsics.atomic_pointermodify(ptr, $op, b, :monotonic)
47+
@eval @inline function $name(ptr::Ptr{T}, b::T) where T
48+
Core.Intrinsics.atomic_pointermodify(ptr::Ptr{T}, $op, b::T, :monotonic)
4549
end
4650
end
4751

48-
# Not sure what this one is...
49-
function atomic_cas!(ptr, b)
50-
Core.Intrinsics.atomic_pointermodify(ptr, &, b, :monotonic)
52+
# TODO: Not sure what this one is...
53+
function atomic_cas!(ptr::Ptr{T}, old::T, new::T) where T
54+
Core.Intrinsics.atomic_pointerswap(ptr::Ptr{T}, x::T, :monotonic)
5155
end
5256

53-
# TODO
54-
function atomic_dec!(ptr, b)
55-
Core.Intrinsics.atomic_pointermodify(ptr, &, b, :monotonic)
57+
# TODO: Not sure what this one is...
58+
function atomic_dec!(ptr::Ptr{T}, b::T) where T
59+
Core.Intrinsics.atomic_pointermodify(ptr, -, 1, :monotonic)
5660
end
5761

58-
# TODO
59-
function atomic_inc!(ptr, b)
60-
Core.Intrinsics.atomic_pointermodify(ptr, &, b, :monotonic)
62+
# TODO: Not sure what this one is...
63+
function atomic_inc!(ptr::Ptr{T}, b::T) where T
64+
Core.Intrinsics.atomic_pointermodify(ptr, +, 1, :monotonic)
6165
end
6266

63-
# TODO
64-
function atomic_max!(ptr, b)
65-
Core.Intrinsics.atomic_pointermodify(ptr, &, b, :monotonic)
67+
function atomic_xchg!(ptr::Ptr{T}, b::T) where T
68+
Core.Intrinsics.atomic_pointerswap(ptr::Ptr{T}, x::T, :monotonic)
6669
end
6770

68-
# TODO
69-
function atomic_min!(ptr, b)
70-
Core.Intrinsics.atomic_pointermodify(ptr, &, b, :monotonic)
71-
end
72-
73-
# TODO
74-
function atomic_op!(ptr, b)
75-
Core.Intrinsics.atomic_pointermodify(ptr, &, b, :monotonic)
76-
end
77-
78-
# TODO
79-
function atomic_xchg!(ptr, b)
80-
Core.Intrinsics.atomic_pointermodify(ptr, &, b, :monotonic)
71+
function atomic_op!(ptr::Ptr{T}, op, b::T) where T
72+
Core.Intrinsics.atomic_pointermodify(ptr::Ptr{T}, op, b::T, :monotonic)
8173
end
8274

8375
###

0 commit comments

Comments
 (0)