AtomicBoolean.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 class AtomicBoolean
  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 AtomicBoolean FromValue (bool value)
  39. {
  40. AtomicBoolean temp = new AtomicBoolean ();
  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 (AtomicBoolean rhs)
  66. {
  67. return this.flag == rhs.flag;
  68. }
  69. public override bool Equals (object rhs)
  70. {
  71. return rhs is AtomicBoolean ? Equals ((AtomicBoolean)rhs) : false;
  72. }
  73. public override int GetHashCode ()
  74. {
  75. return flag.GetHashCode ();
  76. }
  77. public static explicit operator bool (AtomicBoolean rhs)
  78. {
  79. return rhs.Value;
  80. }
  81. public static implicit operator AtomicBoolean (bool rhs)
  82. {
  83. return AtomicBoolean.FromValue (rhs);
  84. }
  85. }
  86. }