AtomicBoolean.cs 4.0 KB

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