DotNext by Roman Sakno

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

 AtomicInt64

public static class AtomicInt64
using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; namespace DotNext.Threading { public static class AtomicInt64 { [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long VolatileRead(ref long value) { return Atomic.Read(ref value); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void VolatileWrite(ref long value, long newValue) { Atomic.Write(ref value, newValue); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long IncrementAndGet(ref long value) { return Interlocked.Increment(ref value); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long DecrementAndGet(ref long value) { return Interlocked.Decrement(ref value); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool CompareAndSet(ref long value, long expected, long update) { return Interlocked.CompareExchange(ref value, update, expected) == expected; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long Add(ref long value, long operand) { return Interlocked.Add(ref value, operand); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long GetAndSet(ref long value, long update) { return Interlocked.Exchange(ref value, update); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long SetAndGet(ref long value, long update) { ref value.VolatileWrite(update); return update; } private static (long OldValue, long NewValue) Update(ref long value, [In] [System.Runtime.CompilerServices.IsReadOnly] ref ValueFunc<long, long> updater) { long num2; long num; do { num2 = updater.Invoke(num = ref value.VolatileRead()); } while (!ref value.CompareAndSet(num, num2)); return (num, num2); } private static (long OldValue, long NewValue) Accumulate(ref long value, long x, [In] [System.Runtime.CompilerServices.IsReadOnly] ref ValueFunc<long, long, long> accumulator) { long num2; long num; do { num2 = accumulator.Invoke(num = ref value.VolatileRead(), x); } while (!ref value.CompareAndSet(num, num2)); return (num, num2); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long AccumulateAndGet(ref long value, long x, Func<long, long, long> accumulator) { ValueFunc<long, long, long> accumulator2 = new ValueFunc<long, long, long>(accumulator, true); return ref value.AccumulateAndGet(x, ref accumulator2); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long AccumulateAndGet(ref long value, long x, [In] [System.Runtime.CompilerServices.IsReadOnly] ref ValueFunc<long, long, long> accumulator) { return Accumulate(ref value, x, ref accumulator).NewValue; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long GetAndAccumulate(ref long value, long x, Func<long, long, long> accumulator) { ValueFunc<long, long, long> accumulator2 = new ValueFunc<long, long, long>(accumulator, true); return ref value.GetAndAccumulate(x, ref accumulator2); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long GetAndAccumulate(ref long value, long x, [In] [System.Runtime.CompilerServices.IsReadOnly] ref ValueFunc<long, long, long> accumulator) { return Accumulate(ref value, x, ref accumulator).OldValue; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long UpdateAndGet(ref long value, Func<long, long> updater) { ValueFunc<long, long> updater2 = new ValueFunc<long, long>(updater, true); return ref value.UpdateAndGet(ref updater2); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long UpdateAndGet(ref long value, [In] [System.Runtime.CompilerServices.IsReadOnly] ref ValueFunc<long, long> updater) { return Update(ref value, ref updater).NewValue; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long GetAndUpdate(ref long value, Func<long, long> updater) { ValueFunc<long, long> updater2 = new ValueFunc<long, long>(updater, true); return ref value.GetAndUpdate(ref updater2); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long GetAndUpdate(ref long value, [In] [System.Runtime.CompilerServices.IsReadOnly] ref ValueFunc<long, long> updater) { return Update(ref value, ref updater).OldValue; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long VolatileRead(this long[] array, long index) { return ref array[index].VolatileRead(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void VolatileWrite(this long[] array, long index, long value) { ref array[index].VolatileWrite(value); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long IncrementAndGet(this long[] array, long index) { return ref array[index].IncrementAndGet(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long DecrementAndGet(this long[] array, long index) { return ref array[index].DecrementAndGet(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long CompareExchange(this long[] array, long index, long update, long comparand) { return Interlocked.CompareExchange(ref array[index], update, comparand); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool CompareAndSet(this long[] array, long index, long expected, long update) { return ref array[index].CompareAndSet(expected, update); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long Add(this long[] array, long index, long operand) { return ref array[index].Add(operand); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long GetAndSet(this long[] array, long index, long update) { return ref array[index].GetAndSet(update); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long SetAndGet(this long[] array, long index, long update) { array.VolatileWrite(index, update); return update; } public static long AccumulateAndGet(this long[] array, long index, long x, Func<long, long, long> accumulator) { ValueFunc<long, long, long> accumulator2 = new ValueFunc<long, long, long>(accumulator, true); return array.AccumulateAndGet(index, x, ref accumulator2); } public static long AccumulateAndGet(this long[] array, long index, long x, [In] [System.Runtime.CompilerServices.IsReadOnly] ref ValueFunc<long, long, long> accumulator) { return ref array[index].AccumulateAndGet(x, ref accumulator); } public static long GetAndAccumulate(this long[] array, long index, long x, Func<long, long, long> accumulator) { ValueFunc<long, long, long> accumulator2 = new ValueFunc<long, long, long>(accumulator, true); return array.GetAndAccumulate(index, x, ref accumulator2); } public static long GetAndAccumulate(this long[] array, long index, long x, [In] [System.Runtime.CompilerServices.IsReadOnly] ref ValueFunc<long, long, long> accumulator) { return ref array[index].GetAndAccumulate(x, ref accumulator); } public static long UpdateAndGet(this long[] array, long index, Func<long, long> updater) { ValueFunc<long, long> updater2 = new ValueFunc<long, long>(updater, true); return array.UpdateAndGet(index, ref updater2); } public static long UpdateAndGet(this long[] array, long index, [In] [System.Runtime.CompilerServices.IsReadOnly] ref ValueFunc<long, long> updater) { return ref array[index].UpdateAndGet(ref updater); } public static long GetAndUpdate(this long[] array, long index, Func<long, long> updater) { ValueFunc<long, long> updater2 = new ValueFunc<long, long>(updater, true); return array.GetAndUpdate(index, ref updater2); } public static long GetAndUpdate(this long[] array, long index, [In] [System.Runtime.CompilerServices.IsReadOnly] ref ValueFunc<long, long> updater) { return ref array[index].GetAndUpdate(ref updater); } } }