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 #if INSIDE_MONO_PARALLEL
28 using System.Threading;
30 namespace Mono.Threading
32 namespace System.Threading
35 #if INSIDE_MONO_PARALLEL
38 struct AtomicBooleanValue
44 public bool CompareAndExchange (bool expected, bool newVal)
46 int newTemp = newVal ? Set : UnSet;
47 int expectedTemp = expected ? Set : UnSet;
49 return Interlocked.CompareExchange (ref flag, newTemp, expectedTemp) == expectedTemp;
52 public static AtomicBooleanValue FromValue (bool value)
54 AtomicBooleanValue temp = new AtomicBooleanValue ();
62 return !Exchange (true);
65 public bool TryRelaxedSet ()
67 return flag == UnSet && !Exchange (true);
70 public bool Exchange (bool newVal)
72 int newTemp = newVal ? Set : UnSet;
73 return Interlocked.Exchange (ref flag, newTemp) == Set;
85 public bool Equals (AtomicBooleanValue rhs)
87 return this.flag == rhs.flag;
90 public override bool Equals (object rhs)
92 return rhs is AtomicBooleanValue ? Equals ((AtomicBooleanValue)rhs) : false;
95 public override int GetHashCode ()
97 return flag.GetHashCode ();
100 public static explicit operator bool (AtomicBooleanValue rhs)
105 public static implicit operator AtomicBooleanValue (bool rhs)
107 return AtomicBooleanValue.FromValue (rhs);
111 #if INSIDE_MONO_PARALLEL
120 public bool CompareAndExchange (bool expected, bool newVal)
122 int newTemp = newVal ? Set : UnSet;
123 int expectedTemp = expected ? Set : UnSet;
125 return Interlocked.CompareExchange (ref flag, newTemp, expectedTemp) == expectedTemp;
128 public static AtomicBoolean FromValue (bool value)
130 AtomicBoolean temp = new AtomicBoolean ();
136 public bool TrySet ()
138 return !Exchange (true);
141 public bool TryRelaxedSet ()
143 return flag == UnSet && !Exchange (true);
146 public bool Exchange (bool newVal)
148 int newTemp = newVal ? Set : UnSet;
149 return Interlocked.Exchange (ref flag, newTemp) == Set;
161 public bool Equals (AtomicBoolean rhs)
163 return this.flag == rhs.flag;
166 public override bool Equals (object rhs)
168 return rhs is AtomicBoolean ? Equals ((AtomicBoolean)rhs) : false;
171 public override int GetHashCode ()
173 return flag.GetHashCode ();
176 public static explicit operator bool (AtomicBoolean rhs)
181 public static implicit operator AtomicBoolean (bool rhs)
183 return AtomicBoolean.FromValue (rhs);