DotNext by .NET Foundation and Contributors

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

 AtomicInt32

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