1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
5
using System.Threading;
7
namespace ICSharpCode.Profiler.Interprocess
10
/// A boolean that starts 'false' and can be atomically set/reset.
12
public struct AtomicBoolean : IEquatable<AtomicBoolean>
17
/// Gets/Sets the value.
21
return Thread.VolatileRead(ref val) != 0;
24
Thread.VolatileWrite(ref val, value ? 1 : 0);
29
/// Sets the value to true.
31
/// <returns>True if the value was successfully set from false to true,
32
/// false if the value already was true.</returns>
35
return Interlocked.Exchange(ref val, 1) == 0;
39
/// Sets the value to false.
41
/// <returns>True if the value was successfully set from true to false,
42
/// false if the value already was false.</returns>
45
return Interlocked.Exchange(ref val, 0) != 0;
49
public override int GetHashCode()
51
return this.Value.GetHashCode();
55
public override bool Equals(object obj)
57
return (obj is AtomicBoolean) && Equals((AtomicBoolean)obj);
61
/// Tests the boolean for equality.
63
public bool Equals(AtomicBoolean other)
65
return this.Value == other.Value;
69
/// Tests the boolean for equality.
71
public static bool operator ==(AtomicBoolean left, AtomicBoolean right)
73
return left.Value == right.Value;
77
/// Tests the boolean for inequality.
79
public static bool operator !=(AtomicBoolean left, AtomicBoolean right)
81
return left.Value != right.Value;