DotNext by Roman Sakno

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

 AtomicBoolean

public struct AtomicBoolean : IEquatable<bool>, ISerializable, IAtomicWrapper<int, bool>
Represents atomic boolean.
using System; using System.Runtime.CompilerServices; using System.Runtime.Serialization; namespace DotNext.Threading { [Serializable] public struct AtomicBoolean : IEquatable<bool>, ISerializable, IAtomicWrapper<int, bool> { private const string ValueSerData = "value"; private const int True = 1; private const int False = 0; private int value; Atomic<int> IAtomicWrapper<int, bool>.Atomic { get { return AtomicInt32.Atomic; } } public bool Value { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { return ref value.VolatileRead() == 1; } [MethodImpl(MethodImplOptions.AggressiveInlining)] set { ref this.value.VolatileWrite(value ? 1 : 0); } } public AtomicBoolean(bool value) { this.value = (value ? 1 : 0); } private AtomicBoolean(SerializationInfo info, StreamingContext context) { value = (int)info.GetValue("value", typeof(int)); } bool IAtomicWrapper<int, bool>.Convert(int value) { return value == 1; } int IAtomicWrapper<int, bool>.Convert(bool value) { if (!value) return 0; return 1; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool CompareExchange(bool update, bool expected) { return Atomic<int, bool, AtomicBoolean>.CompareExchange(ref this, ref value, update, expected); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool CompareAndSet(bool expected, bool update) { return Atomic<int, bool, AtomicBoolean>.CompareAndSet(ref this, ref value, expected, update); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool FalseToTrue() { return CompareAndSet(false, true); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TrueToFalse() { return CompareAndSet(true, false); } private static bool Negate(bool value) { return !value; } public bool NegateAndGet() { return UpdateAndGet(Negate); } public bool GetAndNegate() { return GetAndUpdate(Negate); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool GetAndSet(bool update) { return Atomic<int, bool, AtomicBoolean>.GetAndSet(ref this, ref value, update); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool SetAndGet(bool update) { Value = update; return update; } public bool AccumulateAndGet(bool x, Func<bool, bool, bool> accumulator) { return Atomic<int, bool, AtomicBoolean>.Accumulate(ref this, ref value, x, accumulator).NewValue; } public bool GetAndAccumulate(bool x, Func<bool, bool, bool> accumulator) { return Atomic<int, bool, AtomicBoolean>.Accumulate(ref this, ref value, x, accumulator).OldValue; } public bool UpdateAndGet(Func<bool, bool> updater) { return Atomic<int, bool, AtomicBoolean>.Update(ref this, ref value, updater).NewValue; } public bool GetAndUpdate(Func<bool, bool> updater) { return Atomic<int, bool, AtomicBoolean>.Update(ref this, ref value, updater).OldValue; } public bool Equals(bool other) { return ref value.VolatileRead() == (other ? 1 : 0); } public override int GetHashCode() { return ref value.VolatileRead(); } public override bool Equals(object other) { if (other is bool) { bool other2 = (bool)other; return Equals(other2); } if (other is AtomicBoolean) { AtomicBoolean atomicBoolean = (AtomicBoolean)other; return ref atomicBoolean.value.VolatileRead() == ref value.VolatileRead(); } return false; } public override string ToString() { if (value != 1) return bool.FalseString; return bool.TrueString; } void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("value", value); } } }