AtomicBoolean.cs
Go to the documentation of this file.
1 // AtomicBoolean.cs
2 //
3 // Copyright (c) 2008 Jérémie "Garuma" Laval
4 //
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:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
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
21 // THE SOFTWARE.
22 //
23 //
24 
25 using System;
26 
27 #if INSIDE_MONO_PARALLEL
28 using System.Threading;
29 
30 namespace Mono.Threading
31 #else
32 namespace System.Threading
33 #endif
34 {
35 #if INSIDE_MONO_PARALLEL
36  public
37 #endif
38  struct AtomicBooleanValue
39  {
40  int flag;
41  const int UnSet = 0;
42  const int Set = 1;
43 
44  public bool CompareAndExchange (bool expected, bool newVal)
45  {
46  int newTemp = newVal ? Set : UnSet;
47  int expectedTemp = expected ? Set : UnSet;
48 
49  return AotInterlocked.CompareExchange (ref flag, newTemp, expectedTemp) == expectedTemp;
50  }
51 
52  public static AtomicBooleanValue FromValue (bool value)
53  {
54  AtomicBooleanValue temp = new AtomicBooleanValue ();
55  temp.Value = value;
56 
57  return temp;
58  }
59 
60  public bool TrySet ()
61  {
62  return !Exchange (true);
63  }
64 
65  public bool TryRelaxedSet ()
66  {
67  return flag == UnSet && !Exchange (true);
68  }
69 
70  public bool Exchange (bool newVal)
71  {
72  int newTemp = newVal ? Set : UnSet;
73  return AotInterlocked.Exchange (ref flag, newTemp) == Set;
74  }
75 
76  public bool Value {
77  get {
78  return flag == Set;
79  }
80  set {
81  Exchange (value);
82  }
83  }
84 
85  public bool Equals (AtomicBooleanValue rhs)
86  {
87  return this.flag == rhs.flag;
88  }
89 
90  public override bool Equals (object rhs)
91  {
92  return rhs is AtomicBooleanValue ? Equals ((AtomicBooleanValue)rhs) : false;
93  }
94 
95  public override int GetHashCode ()
96  {
97  return flag.GetHashCode ();
98  }
99 
100  public static explicit operator bool (AtomicBooleanValue rhs)
101  {
102  return rhs.Value;
103  }
104 
105  public static implicit operator AtomicBooleanValue (bool rhs)
106  {
107  return AtomicBooleanValue.FromValue (rhs);
108  }
109  }
110 
111 #if INSIDE_MONO_PARALLEL
112  public
113 #endif
114  class AtomicBoolean
115  {
116  int flag;
117  const int UnSet = 0;
118  const int Set = 1;
119 
120  public bool CompareAndExchange (bool expected, bool newVal)
121  {
122  int newTemp = newVal ? Set : UnSet;
123  int expectedTemp = expected ? Set : UnSet;
124 
125  return AotInterlocked.CompareExchange (ref flag, newTemp, expectedTemp) == expectedTemp;
126  }
127 
128  public static AtomicBoolean FromValue (bool value)
129  {
130  AtomicBoolean temp = new AtomicBoolean ();
131  temp.Value = value;
132 
133  return temp;
134  }
135 
136  public bool TrySet ()
137  {
138  return !Exchange (true);
139  }
140 
141  public bool TryRelaxedSet ()
142  {
143  return flag == UnSet && !Exchange (true);
144  }
145 
146  public bool Exchange (bool newVal)
147  {
148  int newTemp = newVal ? Set : UnSet;
149  return AotInterlocked.Exchange (ref flag, newTemp) == Set;
150  }
151 
152  public bool Value {
153  get {
154  return flag == Set;
155  }
156  set {
157  Exchange (value);
158  }
159  }
160 
161  public bool Equals (AtomicBoolean rhs)
162  {
163  return this.flag == rhs.flag;
164  }
165 
166  public override bool Equals (object rhs)
167  {
168  return rhs is AtomicBoolean ? Equals ((AtomicBoolean)rhs) : false;
169  }
170 
171  public override int GetHashCode ()
172  {
173  return flag.GetHashCode ();
174  }
175 
176  public static explicit operator bool (AtomicBoolean rhs)
177  {
178  return rhs.Value;
179  }
180 
181  public static implicit operator AtomicBoolean (bool rhs)
182  {
183  return AtomicBoolean.FromValue (rhs);
184  }
185  }
186 }