ManualResetEventSlim.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #if NET_4_0
  2. // ManuelResetEventSlim.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. using System;
  26. using System.Diagnostics;
  27. namespace System.Threading
  28. {
  29. public class ManualResetEventSlim : IDisposable
  30. {
  31. const int isSet = 1;
  32. const int isNotSet = 0;
  33. const int defaultSpinCount = 20;
  34. int state;
  35. readonly int spinCount;
  36. ManualResetEvent handle;
  37. public ManualResetEventSlim () : this(false, defaultSpinCount)
  38. {
  39. }
  40. public ManualResetEventSlim (bool initState) : this (initState, defaultSpinCount)
  41. {
  42. }
  43. public ManualResetEventSlim (bool initState, int spinCount)
  44. {
  45. if (spinCount < 0)
  46. throw new ArgumentOutOfRangeException ("spinCount is less than 0", "spinCount");
  47. this.state = initState ? isSet : isNotSet;
  48. this.spinCount = spinCount;
  49. this.handle = new ManualResetEvent (initState);
  50. }
  51. public bool IsSet {
  52. get {
  53. return state == isSet;
  54. }
  55. }
  56. public int SpinCount {
  57. get {
  58. return spinCount;
  59. }
  60. }
  61. public void Reset ()
  62. {
  63. Interlocked.Exchange (ref state, isNotSet);
  64. }
  65. public void Set ()
  66. {
  67. Interlocked.Exchange (ref state, isSet);
  68. }
  69. public void Wait ()
  70. {
  71. Wait (CancellationToken.None);
  72. }
  73. public bool Wait (int millisecondsTimeout)
  74. {
  75. return Wait (millisecondsTimeout, CancellationToken.None);
  76. }
  77. public bool Wait (TimeSpan ts)
  78. {
  79. return Wait ((int)ts.TotalMilliseconds, CancellationToken.None);
  80. }
  81. public void Wait (CancellationToken token)
  82. {
  83. Wait (-1, token);
  84. }
  85. public bool Wait (int millisecondsTimeout, CancellationToken token)
  86. {
  87. if (millisecondsTimeout < -1)
  88. throw new ArgumentOutOfRangeException ("millisecondsTimeout",
  89. "millisecondsTimeout is a negative number other than -1");
  90. Watch s = Watch.StartNew ();
  91. SpinWait sw = new SpinWait ();
  92. int count = 0;
  93. while (state == isNotSet) {
  94. if (token.IsCancellationRequested)
  95. return false;
  96. if (millisecondsTimeout > -1 && s.ElapsedMilliseconds > millisecondsTimeout)
  97. return false;
  98. if (count < spinCount) {
  99. sw.SpinOnce ();
  100. count++;
  101. } else {
  102. Thread.Sleep (0);
  103. }
  104. }
  105. return true;
  106. }
  107. public bool Wait (TimeSpan ts, CancellationToken token)
  108. {
  109. return Wait ((int)ts.TotalMilliseconds, token);
  110. }
  111. public WaitHandle WaitHandle {
  112. get {
  113. return handle;
  114. }
  115. }
  116. #region IDisposable implementation
  117. public void Dispose ()
  118. {
  119. Dispose(true);
  120. }
  121. protected virtual void Dispose(bool managedRes)
  122. {
  123. }
  124. #endregion
  125. }
  126. }
  127. #endif