AtomicReference
Provides atomic operations for the reference type.
using System;
using System.Runtime.CompilerServices;
using System.Threading;
namespace DotNext.Threading
{
public static class AtomicReference
{
private sealed class Operations<T> : Atomic<T> where T : class
{
internal static readonly Operations<T> Instance = new Operations<T>();
private Operations()
{
}
internal override bool CompareAndSet(ref T value, T expected, T update)
{
return Interlocked.CompareExchange<T>(ref value, update, expected) == expected;
}
private protected override T VolatileRead(ref T value)
{
return Volatile.Read<T>(ref value);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool CompareAndSet<T>(ref T value, T expected, T update) where T : class
{
return ((Atomic<T>)Operations<T>.Instance).CompareAndSet(ref value, expected, update);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T AccumulateAndGet<T>(ref T value, T x, Func<T, T, T> accumulator) where T : class
{
return ((Atomic<T>)Operations<T>.Instance).Accumulate(ref value, x, accumulator).NewValue;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T GetAndAccumulate<T>(ref T value, T x, Func<T, T, T> accumulator) where T : class
{
return ((Atomic<T>)Operations<T>.Instance).Accumulate(ref value, x, accumulator).OldValue;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T UpdateAndGet<T>(ref T value, Func<T, T> updater) where T : class
{
return ((Atomic<T>)Operations<T>.Instance).Update(ref value, updater).NewValue;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T GetAndUpdate<T>(ref T value, Func<T, T> updater) where T : class
{
return ((Atomic<T>)Operations<T>.Instance).Update(ref value, updater).OldValue;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T VolatileRead<T>(this T[] array, long index) where T : class
{
return Volatile.Read(ref array[index]);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void VolatileWrite<T>(this T[] array, long index, T element) where T : class
{
Volatile.Write(ref array[index], element);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool CompareAndSet<T>(this T[] array, long index, T expected, T update) where T : class
{
return CompareAndSet(ref array[index], expected, update);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T CompareExchange<T>(this T[] array, long index, T update, T comparand) where T : class
{
return Interlocked.CompareExchange(ref array[index], update, comparand);
}
public static T GetAndSet<T>(this T[] array, long index, T update) where T : class
{
return Interlocked.Exchange(ref array[index], update);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T SetAndGet<T>(this T[] array, long index, T update) where T : class
{
array.VolatileWrite(index, update);
return update;
}
public static T AccumulateAndGet<T>(this T[] array, long index, T x, Func<T, T, T> accumulator) where T : class
{
return AccumulateAndGet(ref array[index], x, accumulator);
}
public static T GetAndAccumulate<T>(this T[] array, long index, T x, Func<T, T, T> accumulator) where T : class
{
return GetAndAccumulate(ref array[index], x, accumulator);
}
public static T UpdateAndGet<T>(this T[] array, long index, Func<T, T> updater) where T : class
{
return UpdateAndGet(ref array[index], updater);
}
public static T GetAndUpdate<T>(this T[] array, long index, Func<T, T> updater) where T : class
{
return GetAndUpdate(ref array[index], updater);
}
}
}