|
| 1 | +# Atomic operations with Complex number support via integer reinterpretation |
| 2 | + |
1 | 3 | @inline function Atomix.get(ref, order) |
2 | 4 | ptr = Atomix.pointer(ref) |
3 | 5 | root = Atomix.gcroot(ref) |
4 | 6 | GC.@preserve root begin |
5 | | - UnsafeAtomics.load(ptr, order) |
| 7 | + _atomic_load(ptr, order) |
6 | 8 | end |
7 | 9 | end |
8 | 10 |
|
|
11 | 13 | ptr = Atomix.pointer(ref) |
12 | 14 | root = Atomix.gcroot(ref) |
13 | 15 | GC.@preserve root begin |
14 | | - UnsafeAtomics.store!(ptr, v, order) |
| 16 | + _atomic_store!(ptr, v, order) |
15 | 17 | end |
16 | 18 | end |
17 | 19 |
|
|
21 | 23 | ptr = Atomix.pointer(ref) |
22 | 24 | root = Atomix.gcroot(ref) |
23 | 25 | GC.@preserve root begin |
24 | | - UnsafeAtomics.cas!(ptr, expected, desired, success_ordering, failure_ordering) |
| 26 | + _atomic_cas!(ptr, expected, desired, success_ordering, failure_ordering) |
25 | 27 | end |
26 | 28 | end |
27 | 29 |
|
|
30 | 32 | ptr = Atomix.pointer(ref) |
31 | 33 | root = Atomix.gcroot(ref) |
32 | 34 | GC.@preserve root begin |
33 | | - UnsafeAtomics.modify!(ptr, op, x, ord) |
| 35 | + _atomic_modify!(ptr, op, x, ord) |
34 | 36 | end |
35 | 37 | end |
36 | 38 |
|
| 39 | +# Generic atomic operations - dispatch on type |
| 40 | +_atomic_load(ptr::Ptr{T}, order) where {T} = |
| 41 | + _with_int_repr(UnsafeAtomics.load, ptr, order) |
| 42 | + |
| 43 | +_atomic_store!(ptr::Ptr{T}, val::T, order) where {T} = |
| 44 | + _with_int_repr(UnsafeAtomics.store!, ptr, val, order) |
| 45 | + |
| 46 | +_atomic_cas!(ptr::Ptr{T}, expected::T, desired::T, success_order, failure_order) where {T} = |
| 47 | + _with_int_repr(UnsafeAtomics.cas!, ptr, expected, desired, success_order, failure_order) |
| 48 | + |
| 49 | +# Multiple dispatch for modify! - native atomics for non-Complex types |
| 50 | +function _atomic_modify!(ptr::Ptr{T}, op::OP, x::T, ord) where {T,OP} |
| 51 | + UnsafeAtomics.modify!(ptr, op, x, ord) |
| 52 | +end |
| 53 | + |
| 54 | +# CAS loop fallback for Complex types (no native atomic modify!) |
| 55 | +function _atomic_modify!(ptr::Ptr{Complex{T}}, op::OP, x::Complex{T}, ord) where {T,OP} |
| 56 | + old = _atomic_load(ptr, ord) |
| 57 | + while true |
| 58 | + new = op(old, x) |
| 59 | + result = _atomic_cas!(ptr, old, new, ord, ord) |
| 60 | + result.success && return (old => new) |
| 61 | + old = result.old |
| 62 | + end |
| 63 | +end |
| 64 | + |
| 65 | +# Helper: apply atomic operation with integer reinterpretation for Complex types |
| 66 | +function _with_int_repr(f, ptr::Ptr{Complex{T}}, args...) where {T} |
| 67 | + IntType = _int_type_for_complex(T) |
| 68 | + int_ptr = reinterpret(Ptr{IntType}, ptr) |
| 69 | + result = f(int_ptr, _to_int.(IntType, args)...) |
| 70 | + return _from_int(Complex{T}, result) |
| 71 | +end |
| 72 | + |
| 73 | +_with_int_repr(f, ptr::Ptr{T}, args...) where {T} = f(ptr, args...) |
| 74 | + |
| 75 | +# Integer type mapping for Complex types |
| 76 | +_int_type_for_complex(::Type{Float32}) = UInt64 |
| 77 | +_int_type_for_complex(::Type{Float64}) = UInt128 |
| 78 | + |
| 79 | +# Convert to/from integer representation |
| 80 | +_to_int(::Type{I}, x::Complex{T}) where {I,T} = reinterpret(I, x) |
| 81 | +_to_int(::Type{I}, x) where {I} = x |
| 82 | + |
| 83 | +_from_int(::Type{Complex{T}}, x::Integer) where {T} = reinterpret(Complex{T}, x) |
| 84 | +_from_int(::Type{Complex{T}}, result::NamedTuple) where {T} = |
| 85 | + (old = reinterpret(Complex{T}, result.old), success = result.success) |
| 86 | +_from_int(::Type{T}, x) where {T} = x |
| 87 | + |
37 | 88 | Atomix.asstorable(ref, v) = convert(eltype(ref), v) |
0 commit comments