3 // Copyright (c) 2008 Jérémie "Garuma" Laval
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 namespace System.Threading
29 internal struct AtomicBooleanValue
35 public bool CompareAndExchange (bool expected, bool newVal)
37 int newTemp = newVal ? Set : UnSet;
38 int expectedTemp = expected ? Set : UnSet;
40 return Interlocked.CompareExchange (ref flag, newTemp, expectedTemp) == expectedTemp;
43 public static AtomicBooleanValue FromValue (bool value)
45 AtomicBooleanValue temp = new AtomicBooleanValue ();
53 return !Exchange (true);
56 public bool TryRelaxedSet ()
58 return flag == UnSet && !Exchange (true);
61 public bool Exchange (bool newVal)
63 int newTemp = newVal ? Set : UnSet;
64 return Interlocked.Exchange (ref flag, newTemp) == Set;
76 public bool Equals (AtomicBooleanValue rhs)
78 return this.flag == rhs.flag;
81 public override bool Equals (object rhs)
83 return rhs is AtomicBooleanValue ? Equals ((AtomicBooleanValue)rhs) : false;
86 public override int GetHashCode ()
88 return flag.GetHashCode ();
91 public static explicit operator bool (AtomicBooleanValue rhs)
96 public static implicit operator AtomicBooleanValue (bool rhs)
98 return AtomicBooleanValue.FromValue (rhs);
102 internal class AtomicBoolean
108 public bool CompareAndExchange (bool expected, bool newVal)
110 int newTemp = newVal ? Set : UnSet;
111 int expectedTemp = expected ? Set : UnSet;
113 return Interlocked.CompareExchange (ref flag, newTemp, expectedTemp) == expectedTemp;
116 public static AtomicBoolean FromValue (bool value)
118 AtomicBoolean temp = new AtomicBoolean ();
124 public bool TrySet ()
126 return !Exchange (true);
129 public bool TryRelaxedSet ()
131 return flag == UnSet && !Exchange (true);
134 public bool Exchange (bool newVal)
136 int newTemp = newVal ? Set : UnSet;
137 return Interlocked.Exchange (ref flag, newTemp) == Set;
149 public bool Equals (AtomicBoolean rhs)
151 return this.flag == rhs.flag;
154 public override bool Equals (object rhs)
156 return rhs is AtomicBoolean ? Equals ((AtomicBoolean)rhs) : false;
159 public override int GetHashCode ()
161 return flag.GetHashCode ();
164 public static explicit operator bool (AtomicBoolean rhs)
169 public static implicit operator AtomicBoolean (bool rhs)
171 return AtomicBoolean.FromValue (rhs);