DotNext by Roman Sakno

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

 AtomicIntPtr

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