ManualResetEventSlim.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // ManuelResetEventSlim.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. #if NET_4_0 || BOOTSTRAP_NET_4_0
  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 = 10;
  34. const int deepSleepTime = 20;
  35. int state;
  36. readonly int spinCount;
  37. ManualResetEvent handle;
  38. public ManualResetEventSlim () : this (false, defaultSpinCount)
  39. {
  40. }
  41. public ManualResetEventSlim (bool initState) : this (initState, defaultSpinCount)
  42. {
  43. }
  44. public ManualResetEventSlim (bool initState, int spinCount)
  45. {
  46. if (spinCount < 0)
  47. throw new ArgumentOutOfRangeException ("spinCount is less than 0", "spinCount");
  48. this.state = initState ? isSet : isNotSet;
  49. this.spinCount = spinCount;
  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. if (handle != null)
  65. handle.Reset ();
  66. }
  67. public void Set ()
  68. {
  69. Interlocked.Exchange (ref state, isSet);
  70. if (handle != null)
  71. handle.Set ();
  72. }
  73. public void Wait ()
  74. {
  75. Wait (CancellationToken.None);
  76. }
  77. public bool Wait (int millisecondsTimeout)
  78. {
  79. return Wait (millisecondsTimeout, CancellationToken.None);
  80. }
  81. public bool Wait (TimeSpan ts)
  82. {
  83. return Wait ((int)ts.TotalMilliseconds, CancellationToken.None);
  84. }
  85. public void Wait (CancellationToken token)
  86. {
  87. Wait (-1, token);
  88. }
  89. public bool Wait (int millisecondsTimeout, CancellationToken token)
  90. {
  91. if (millisecondsTimeout < -1)
  92. throw new ArgumentOutOfRangeException ("millisecondsTimeout",
  93. "millisecondsTimeout is a negative number other than -1");
  94. Watch s = Watch.StartNew ();
  95. SpinWait sw = new SpinWait ();
  96. while (state == isNotSet) {
  97. token.ThrowIfCancellationRequested ();
  98. if (millisecondsTimeout > -1 && s.ElapsedMilliseconds > millisecondsTimeout)
  99. return false;
  100. if (sw.Count < spinCount)
  101. sw.SpinOnce ();
  102. else
  103. if (WaitHandle.WaitOne (Math.Min (Math.Max (millisecondsTimeout - (int)s.ElapsedMilliseconds, 1), deepSleepTime)))
  104. return true;
  105. }
  106. return true;
  107. }
  108. public bool Wait (TimeSpan ts, CancellationToken token)
  109. {
  110. return Wait ((int)ts.TotalMilliseconds, token);
  111. }
  112. public WaitHandle WaitHandle {
  113. get {
  114. if (handle != null)
  115. return handle;
  116. return LazyInitializer.EnsureInitialized (ref handle,
  117. () => new ManualResetEvent (state == isSet ? true : false));
  118. }
  119. }
  120. #region IDisposable implementation
  121. public void Dispose ()
  122. {
  123. Dispose(true);
  124. }
  125. protected virtual void Dispose(bool managedRes)
  126. {
  127. }
  128. #endregion
  129. }
  130. }
  131. #endif