AtomicInt32
Various atomic operations for Int32 data type
accessible as extension methods.
using System;
using System.Runtime.CompilerServices;
using System.Threading;
namespace DotNext.Threading
{
public static class AtomicInt32
{
private sealed class Operations : Atomic<int>
{
internal Operations()
{
}
internal override int Exchange(ref int value, int update)
{
return Interlocked.Exchange(ref value, update);
}
internal override int CompareExchange(ref int value, int update, int expected)
{
return Interlocked.CompareExchange(ref value, update, expected);
}
internal override int VolatileRead(ref int value)
{
return Volatile.Read(ref value);
}
private protected override bool Equals(int x, int y)
{
return x == y;
}
}
internal static readonly Atomic<int> Atomic = new Operations();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int VolatileRead(ref int value)
{
return Volatile.Read(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 Atomic.CompareAndSet(ref value, expected, update);
}
[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)
{
Volatile.Write(ref value, update);
return update;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int AccumulateAndGet(ref int value, int x, Func<int, int, int> accumulator)
{
return Atomic.Accumulate(ref value, x, accumulator).NewValue;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetAndAccumulate(ref int value, int x, Func<int, int, int> accumulator)
{
return Atomic.Accumulate(ref value, x, accumulator).OldValue;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int UpdateAndGet(ref int value, Func<int, int> updater)
{
return Atomic.Update(ref value, updater).NewValue;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetAndUpdate(ref int value, Func<int, int> updater)
{
return Atomic.Update(ref value, 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)
{
return ref array[index].AccumulateAndGet(x, accumulator);
}
public static int GetAndAccumulate(this int[] array, long index, int x, Func<int, int, int> accumulator)
{
return ref array[index].GetAndAccumulate(x, accumulator);
}
public static int UpdateAndGet(this int[] array, long index, Func<int, int> updater)
{
return ref array[index].UpdateAndGet(updater);
}
public static int GetAndUpdate(this int[] array, long index, Func<int, int> updater)
{
return ref array[index].GetAndUpdate(updater);
}
}
}