DotNext by .NET Foundation and Contributors

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

 AtomicUInt32

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