DotNext by .NET Foundation and Contributors

<PackageReference Include="DotNext" Version="4.15.1" />

 AtomicSingle

public static class AtomicSingle
Various atomic operations for Single data type accessible as extension methods.
using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; namespace DotNext.Threading { public static class AtomicSingle { [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float VolatileRead([In] [IsReadOnly] ref float value) { return Volatile.Read(ref Unsafe.AsRef(ref value)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void VolatileWrite(ref float value, float newValue) { Volatile.Write(ref value, newValue); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float IncrementAndGet(ref float value) { return Update(ref value, default(Increment)).NewValue; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float DecrementAndGet(ref float value) { return Update(ref value, default(Decrement)).NewValue; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float AddAndGet(ref float value, float operand) { return Accumulate(ref value, operand, default(Adder)).NewValue; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float GetAndAdd(ref float value, float operand) { return Accumulate(ref value, operand, default(Adder)).OldValue; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static bool Equals(float x, float y) { return BitConverter.SingleToInt32Bits(x) == BitConverter.SingleToInt32Bits(y); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool CompareAndSet(ref float value, float expected, float update) { return Equals(Interlocked.CompareExchange(ref value, update, expected), expected); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float GetAndSet(ref float value, float update) { return Interlocked.Exchange(ref value, update); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float SetAndGet(ref float value, float update) { ref value.VolatileWrite(update); return update; } private static (float OldValue, float NewValue) Update<TUpdater>(ref float value, TUpdater updater) where TUpdater : struct, ISupplier<float, float> { float num = Volatile.Read(ref value); float num3; float num2; do { num3 = ((ISupplier<float, float>)updater).Invoke(num2 = num); } while (!Equals(num = Interlocked.CompareExchange(ref value, num3, num2), num2)); return (num2, num3); } private static (float OldValue, float NewValue) Accumulate<TAccumulator>(ref float value, float x, TAccumulator accumulator) where TAccumulator : struct, ISupplier<float, float, float> { float num = Volatile.Read(ref value); float num3; float num2; do { num3 = ((ISupplier<float, float, float>)accumulator).Invoke(num2 = num, x); } while (!Equals(num = Interlocked.CompareExchange(ref value, num3, num2), num2)); return (num2, num3); } [MethodImpl(MethodImplOptions.AggressiveInlining)] [System.Runtime.CompilerServices.NullableContext(1)] public static float AccumulateAndGet(ref float value, float x, Func<float, float, float> accumulator) { return Accumulate(ref value, x, (DelegatingSupplier<float, float, float>)accumulator).NewValue; } [MethodImpl(MethodImplOptions.AggressiveInlining)] [CLSCompliant(false)] public static float AccumulateAndGet(ref float value, float x, IntPtr accumulator) { return Accumulate(ref value, x, (Supplier<float, float, float>)(long)accumulator).NewValue; } [MethodImpl(MethodImplOptions.AggressiveInlining)] [System.Runtime.CompilerServices.NullableContext(1)] public static float GetAndAccumulate(ref float value, float x, Func<float, float, float> accumulator) { return Accumulate(ref value, x, (DelegatingSupplier<float, float, float>)accumulator).OldValue; } [MethodImpl(MethodImplOptions.AggressiveInlining)] [CLSCompliant(false)] public static float GetAndAccumulate(ref float value, float x, IntPtr accumulator) { return Accumulate(ref value, x, (Supplier<float, float, float>)(long)accumulator).OldValue; } [MethodImpl(MethodImplOptions.AggressiveInlining)] [System.Runtime.CompilerServices.NullableContext(1)] public static float UpdateAndGet(ref float value, Func<float, float> updater) { return Update(ref value, (DelegatingSupplier<float, float>)updater).NewValue; } [MethodImpl(MethodImplOptions.AggressiveInlining)] [CLSCompliant(false)] public static float UpdateAndGet(ref float value, IntPtr updater) { return Update(ref value, (Supplier<float, float>)(long)updater).NewValue; } [MethodImpl(MethodImplOptions.AggressiveInlining)] [System.Runtime.CompilerServices.NullableContext(1)] public static float GetAndUpdate(ref float value, Func<float, float> updater) { return Update(ref value, (DelegatingSupplier<float, float>)updater).OldValue; } [MethodImpl(MethodImplOptions.AggressiveInlining)] [CLSCompliant(false)] public static float GetAndUpdate(ref float value, IntPtr updater) { return Update(ref value, (Supplier<float, float>)(long)updater).OldValue; } } }