2
0

AtomicBoolean.cs 3.9 KB

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