DotNext by .NET Foundation and Contributors

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

 AtomicEnum

public static class AtomicEnum
Provides basic atomic operations for arbitrary enum type.
using DotNext.Runtime; using System; using System.Runtime.CompilerServices; using System.Threading; namespace DotNext.Threading { public static class AtomicEnum { [MethodImpl(MethodImplOptions.AggressiveInlining)] public static TEnum VolatileRead<TEnum>(ref TEnum value) where TEnum : struct, Enum { if (Unsafe.SizeOf<TEnum>() != 8 || IntPtr.Size == 8) return <VolatileRead>g__ReadCore|0_0(ref value); return Intrinsics.ReinterpretCast<long, TEnum>(Volatile.Read(ref Unsafe.As<TEnum, long>(ref value))); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void VolatileWrite<TEnum>(ref TEnum value, TEnum newValue) where TEnum : struct, Enum { if (Unsafe.SizeOf<TEnum>() == 8 && IntPtr.Size != 8) Volatile.Write(ref Unsafe.As<TEnum, long>(ref value), Intrinsics.ReinterpretCast<TEnum, long>(newValue)); else <VolatileWrite>g__WriteCore|1_0(ref value, newValue); } } }