~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/Analysis/Profiler/Controller/Interprocess/AtomicBoolean.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.Threading;
 
6
 
 
7
namespace ICSharpCode.Profiler.Interprocess
 
8
{
 
9
        /// <summary>
 
10
        /// A boolean that starts 'false' and can be atomically set/reset.
 
11
        /// </summary>
 
12
        public struct AtomicBoolean : IEquatable<AtomicBoolean>
 
13
        {
 
14
                int val;
 
15
                
 
16
                /// <summary>
 
17
                /// Gets/Sets the value.
 
18
                /// </summary>
 
19
                public bool Value {
 
20
                        get {
 
21
                                return Thread.VolatileRead(ref val) != 0;
 
22
                        }
 
23
                        set {
 
24
                                Thread.VolatileWrite(ref val, value ? 1 : 0);
 
25
                        }
 
26
                }
 
27
                
 
28
                /// <summary>
 
29
                /// Sets the value to true.
 
30
                /// </summary>
 
31
                /// <returns>True if the value was successfully set from false to true,
 
32
                /// false if the value already was true.</returns>
 
33
                public bool Set()
 
34
                {
 
35
                        return Interlocked.Exchange(ref val, 1) == 0;
 
36
                }
 
37
                
 
38
                /// <summary>
 
39
                /// Sets the value to false.
 
40
                /// </summary>
 
41
                /// <returns>True if the value was successfully set from true to false,
 
42
                /// false if the value already was false.</returns>
 
43
                public bool Reset()
 
44
                {
 
45
                        return Interlocked.Exchange(ref val, 0) != 0;
 
46
                }
 
47
                
 
48
                /// <inheritdoc/>
 
49
                public override int GetHashCode()
 
50
                {
 
51
                        return this.Value.GetHashCode();
 
52
                }
 
53
                
 
54
                /// <inheritdoc/>
 
55
                public override bool Equals(object obj)
 
56
                {
 
57
                        return (obj is AtomicBoolean) && Equals((AtomicBoolean)obj);
 
58
                }
 
59
                
 
60
                /// <summary>
 
61
                /// Tests the boolean for equality.
 
62
                /// </summary>
 
63
                public bool Equals(AtomicBoolean other)
 
64
                {
 
65
                        return this.Value == other.Value;
 
66
                }
 
67
                
 
68
                /// <summary>
 
69
                /// Tests the boolean for equality.
 
70
                /// </summary>
 
71
                public static bool operator ==(AtomicBoolean left, AtomicBoolean right)
 
72
                {
 
73
                        return left.Value == right.Value;
 
74
                }
 
75
                
 
76
                /// <summary>
 
77
                /// Tests the boolean for inequality.
 
78
                /// </summary>
 
79
                public static bool operator !=(AtomicBoolean left, AtomicBoolean right)
 
80
                {
 
81
                        return left.Value != right.Value;
 
82
                }
 
83
        }
 
84
}