using System; using System.Threading; namespace Swan.Threading { /// /// Defines an atomic generic Enum. /// /// The type of enum. public sealed class AtomicEnum where T : struct, IConvertible { private long _backingValue; /// /// Initializes a new instance of the class. /// /// The initial value. /// T must be an enumerated type. public AtomicEnum(T initialValue) { if (!Enum.IsDefined(typeof(T), initialValue)) throw new ArgumentException("T must be an enumerated type"); Value = initialValue; } /// /// Gets or sets the value. /// public T Value { get => (T)Enum.ToObject(typeof(T), BackingValue); set => BackingValue = Convert.ToInt64(value); } private long BackingValue { get => Interlocked.Read(ref _backingValue); set => Interlocked.Exchange(ref _backingValue, value); } } }