Simplify Parallel.For execution.
[mono.git] / mcs / class / corlib / System.Threading / AtomicBoolean.cs
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 namespace System.Threading
28 {
29         internal struct AtomicBooleanValue
30         {
31                 int flag;
32                 const int UnSet = 0;
33                 const int Set = 1;
34
35                 public bool CompareAndExchange (bool expected, bool newVal)
36                 {
37                         int newTemp = newVal ? Set : UnSet;
38                         int expectedTemp = expected ? Set : UnSet;
39
40                         return Interlocked.CompareExchange (ref flag, newTemp, expectedTemp) == expectedTemp;
41                 }
42
43                 public static AtomicBooleanValue FromValue (bool value)
44                 {
45                         AtomicBooleanValue temp = new AtomicBooleanValue ();
46                         temp.Value = value;
47
48                         return temp;
49                 }
50
51                 public bool TrySet ()
52                 {
53                         return !Exchange (true);
54                 }
55
56                 public bool TryRelaxedSet ()
57                 {
58                         return flag == UnSet && !Exchange (true);
59                 }
60
61                 public bool Exchange (bool newVal)
62                 {
63                         int newTemp = newVal ? Set : UnSet;
64                         return Interlocked.Exchange (ref flag, newTemp) == Set;
65                 }
66
67                 public bool Value {
68                         get {
69                                 return flag == Set;
70                         }
71                         set {
72                                 Exchange (value);
73                         }
74                 }
75
76                 public bool Equals (AtomicBooleanValue rhs)
77                 {
78                         return this.flag == rhs.flag;
79                 }
80
81                 public override bool Equals (object rhs)
82                 {
83                         return rhs is AtomicBooleanValue ? Equals ((AtomicBooleanValue)rhs) : false;
84                 }
85
86                 public override int GetHashCode ()
87                 {
88                         return flag.GetHashCode ();
89                 }
90
91                 public static explicit operator bool (AtomicBooleanValue rhs)
92                 {
93                         return rhs.Value;
94                 }
95
96                 public static implicit operator AtomicBooleanValue (bool rhs)
97                 {
98                         return AtomicBooleanValue.FromValue (rhs);
99                 }
100         }
101
102         internal class AtomicBoolean
103         {
104                 int flag;
105                 const int UnSet = 0;
106                 const int Set = 1;
107
108                 public bool CompareAndExchange (bool expected, bool newVal)
109                 {
110                         int newTemp = newVal ? Set : UnSet;
111                         int expectedTemp = expected ? Set : UnSet;
112
113                         return Interlocked.CompareExchange (ref flag, newTemp, expectedTemp) == expectedTemp;
114                 }
115
116                 public static AtomicBoolean FromValue (bool value)
117                 {
118                         AtomicBoolean temp = new AtomicBoolean ();
119                         temp.Value = value;
120
121                         return temp;
122                 }
123
124                 public bool TrySet ()
125                 {
126                         return !Exchange (true);
127                 }
128
129                 public bool TryRelaxedSet ()
130                 {
131                         return flag == UnSet && !Exchange (true);
132                 }
133
134                 public bool Exchange (bool newVal)
135                 {
136                         int newTemp = newVal ? Set : UnSet;
137                         return Interlocked.Exchange (ref flag, newTemp) == Set;
138                 }
139
140                 public bool Value {
141                         get {
142                                 return flag == Set;
143                         }
144                         set {
145                                 Exchange (value);
146                         }
147                 }
148
149                 public bool Equals (AtomicBoolean rhs)
150                 {
151                         return this.flag == rhs.flag;
152                 }
153
154                 public override bool Equals (object rhs)
155                 {
156                         return rhs is AtomicBoolean ? Equals ((AtomicBoolean)rhs) : false;
157                 }
158
159                 public override int GetHashCode ()
160                 {
161                         return flag.GetHashCode ();
162                 }
163
164                 public static explicit operator bool (AtomicBoolean rhs)
165                 {
166                         return rhs.Value;
167                 }
168
169                 public static implicit operator AtomicBoolean (bool rhs)
170                 {
171                         return AtomicBoolean.FromValue (rhs);
172                 }
173         }
174 }