27 #if INSIDE_MONO_PARALLEL 30 namespace Mono.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 AotInterlocked.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 AotInterlocked.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 AotInterlocked.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 AotInterlocked.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);