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(ref int value)
{
return value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void VolatileWrite(ref int value, int newValue)
{
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 Add(ref int value, int operand)
{
return Interlocked.Add(ref value, operand);
}
[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(ref int value, [In] [System.Runtime.CompilerServices.IsReadOnly] ref ValueFunc<int, int> updater)
{
int num2;
int num;
do {
num2 = updater.Invoke(num = ref value.VolatileRead());
} while (!ref value.CompareAndSet(num, num2));
return (num, num2);
}
private static (int OldValue, int NewValue) Accumulate(ref int value, int x, [In] [System.Runtime.CompilerServices.IsReadOnly] ref ValueFunc<int, int, int> accumulator)
{
int num2;
int num;
do {
num2 = accumulator.Invoke(num = ref value.VolatileRead(), x);
} while (!ref value.CompareAndSet(num, num2));
return (num, num2);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int AccumulateAndGet(ref int value, int x, Func<int, int, int> accumulator)
{
ValueFunc<int, int, int> accumulator2 = new ValueFunc<int, int, int>(accumulator, true);
return ref value.AccumulateAndGet(x, ref accumulator2);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int AccumulateAndGet(ref int value, int x, [In] [System.Runtime.CompilerServices.IsReadOnly] ref ValueFunc<int, int, int> accumulator)
{
return Accumulate(ref value, x, ref accumulator).NewValue;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetAndAccumulate(ref int value, int x, Func<int, int, int> accumulator)
{
ValueFunc<int, int, int> accumulator2 = new ValueFunc<int, int, int>(accumulator, true);
return ref value.GetAndAccumulate(x, ref accumulator2);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetAndAccumulate(ref int value, int x, [In] [System.Runtime.CompilerServices.IsReadOnly] ref ValueFunc<int, int, int> accumulator)
{
return Accumulate(ref value, x, ref accumulator).OldValue;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int UpdateAndGet(ref int value, Func<int, int> updater)
{
ValueFunc<int, int> updater2 = new ValueFunc<int, int>(updater, true);
return ref value.UpdateAndGet(ref updater2);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int UpdateAndGet(ref int value, [In] [System.Runtime.CompilerServices.IsReadOnly] ref ValueFunc<int, int> updater)
{
return Update(ref value, ref updater).NewValue;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetAndUpdate(ref int value, Func<int, int> updater)
{
ValueFunc<int, int> updater2 = new ValueFunc<int, int>(updater, true);
return ref value.GetAndUpdate(ref updater2);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetAndUpdate(ref int value, [In] [System.Runtime.CompilerServices.IsReadOnly] ref ValueFunc<int, int> updater)
{
return Update(ref value, ref updater).OldValue;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int VolatileRead(this int[] array, long index)
{
return ref array[index].VolatileRead();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void VolatileWrite(this int[] array, long index, int value)
{
ref array[index].VolatileWrite(value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int IncrementAndGet(this int[] array, long index)
{
return ref array[index].IncrementAndGet();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int DecrementAndGet(this int[] array, long index)
{
return ref array[index].DecrementAndGet();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int CompareExchange(this int[] array, long index, int update, int comparand)
{
return Interlocked.CompareExchange(ref array[index], update, comparand);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool CompareAndSet(this int[] array, long index, int expected, int update)
{
return ref array[index].CompareAndSet(expected, update);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Add(this int[] array, long index, int operand)
{
return ref array[index].Add(operand);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetAndSet(this int[] array, long index, int update)
{
return ref array[index].GetAndSet(update);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int SetAndGet(this int[] array, long index, int update)
{
array.VolatileWrite(index, update);
return update;
}
public static int AccumulateAndGet(this int[] array, long index, int x, Func<int, int, int> accumulator)
{
ValueFunc<int, int, int> accumulator2 = new ValueFunc<int, int, int>(accumulator, true);
return array.AccumulateAndGet(index, x, ref accumulator2);
}
public static int AccumulateAndGet(this int[] array, long index, int x, [In] [System.Runtime.CompilerServices.IsReadOnly] ref ValueFunc<int, int, int> accumulator)
{
return ref array[index].AccumulateAndGet(x, ref accumulator);
}
public static int GetAndAccumulate(this int[] array, long index, int x, Func<int, int, int> accumulator)
{
ValueFunc<int, int, int> accumulator2 = new ValueFunc<int, int, int>(accumulator, true);
return array.GetAndAccumulate(index, x, ref accumulator2);
}
public static int GetAndAccumulate(this int[] array, long index, int x, [In] [System.Runtime.CompilerServices.IsReadOnly] ref ValueFunc<int, int, int> accumulator)
{
return ref array[index].GetAndAccumulate(x, ref accumulator);
}
public static int UpdateAndGet(this int[] array, long index, Func<int, int> updater)
{
ValueFunc<int, int> updater2 = new ValueFunc<int, int>(updater, true);
return array.UpdateAndGet(index, ref updater2);
}
public static int UpdateAndGet(this int[] array, long index, [In] [System.Runtime.CompilerServices.IsReadOnly] ref ValueFunc<int, int> updater)
{
return ref array[index].UpdateAndGet(ref updater);
}
public static int GetAndUpdate(this int[] array, long index, Func<int, int> updater)
{
ValueFunc<int, int> updater2 = new ValueFunc<int, int>(updater, true);
return array.GetAndUpdate(index, ref updater2);
}
public static int GetAndUpdate(this int[] array, long index, [In] [System.Runtime.CompilerServices.IsReadOnly] ref ValueFunc<int, int> updater)
{
return ref array[index].GetAndUpdate(ref updater);
}
}
}