New tests.
[mono.git] / mcs / class / corlib / System.Threading / AtomicBoolean.cs
1 #if NET_4_0 || BOOTSTRAP_NET_4_0
2 // AtomicBoolean.cs
3 //
4 // Copyright (c) 2008 Jérémie "Garuma" Laval
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 // THE SOFTWARE.
23 //
24 //
25
26 using System;
27
28 namespace System.Threading
29 {
30         internal class AtomicBoolean
31         {
32                 int flag;
33                 const int UnSet = 0;
34                 const int Set = 1;
35                 
36                 public bool CompareAndExchange (bool expected, bool newVal)
37                 {
38                         int newTemp = newVal ? Set : UnSet;
39                         int expectedTemp = expected ? Set : UnSet;
40                         
41                         return Interlocked.CompareExchange (ref flag, newTemp, expectedTemp) == expectedTemp;
42                 }
43                 
44                 public static AtomicBoolean FromValue (bool value)
45                 {
46                         AtomicBoolean temp = new AtomicBoolean ();
47                         temp.Value = value;
48                         
49                         return temp;
50                 }
51                 
52                 public bool TrySet ()
53                 {
54                         return !Exchange (true);
55                 }
56                 
57                 public bool Exchange (bool newVal)
58                 {
59                         int newTemp = newVal ? Set : UnSet;
60                         return Interlocked.Exchange (ref flag, newTemp) == Set;
61                 }
62                 
63                 public bool Value {
64                         get {
65                                 return flag == Set;
66                         } 
67                         set {
68                                 Exchange (value);
69                         }
70                 }
71                 
72                 public bool Equals (AtomicBoolean rhs)
73                 {
74                         return this.flag == rhs.flag;
75                 }
76                 
77                 public override bool Equals (object rhs)
78                 {
79                         return rhs is AtomicBoolean ? Equals ((AtomicBoolean)rhs) : false;
80                 }
81                 
82                 public override int GetHashCode ()
83                 {
84                         return flag.GetHashCode ();
85                 }
86                 
87                 public static explicit operator bool (AtomicBoolean rhs)
88                 {
89                         return rhs.Value;
90                 }
91                 
92                 public static implicit operator AtomicBoolean (bool rhs)
93                 {
94                         return AtomicBoolean.FromValue (rhs);
95                 }
96         }
97 }
98 #endif