CountdownEvent.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // CountdownEvent.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. namespace System.Threading
  27. {
  28. public class CountdownEvent : IDisposable
  29. {
  30. int count;
  31. readonly int initial;
  32. ManualResetEventSlim evt = new ManualResetEventSlim (false);
  33. public CountdownEvent (int count)
  34. {
  35. if (count < 0)
  36. throw new ArgumentOutOfRangeException ("count is negative");
  37. this.initial = this.count = count;
  38. }
  39. public bool Signal ()
  40. {
  41. return Signal (1);
  42. }
  43. public bool Signal (int num)
  44. {
  45. if (num <= 0)
  46. throw new ArgumentOutOfRangeException ("num");
  47. Action<int> check = delegate (int value) {
  48. if (value < 0)
  49. throw new InvalidOperationException ("the specified count is larger that CurrentCount");
  50. };
  51. int newValue;
  52. if (!ApplyOperation (-num, check, out newValue))
  53. throw new InvalidOperationException ("The event is already set");
  54. if (newValue == 0) {
  55. evt.Set ();
  56. return true;
  57. }
  58. return false;
  59. }
  60. public void AddCount ()
  61. {
  62. AddCount (1);
  63. }
  64. public void AddCount (int num)
  65. {
  66. if (num < 0)
  67. throw new ArgumentOutOfRangeException ("num");
  68. if (!TryAddCount (num))
  69. throw new InvalidOperationException ("The event is already set");
  70. }
  71. public bool TryAddCount ()
  72. {
  73. return TryAddCount (1);
  74. }
  75. public bool TryAddCount (int num)
  76. {
  77. if (num < 0)
  78. throw new ArgumentOutOfRangeException ("num");
  79. return ApplyOperation (num, null);
  80. }
  81. bool ApplyOperation (int num, Action<int> doCheck)
  82. {
  83. int temp;
  84. return ApplyOperation (num, doCheck, out temp);
  85. }
  86. bool ApplyOperation (int num, Action<int> doCheck, out int newValue)
  87. {
  88. int oldCount;
  89. newValue = 0;
  90. do {
  91. oldCount = count;
  92. if (oldCount == 0)
  93. return false;
  94. newValue = oldCount + num;
  95. if (doCheck != null)
  96. doCheck (newValue);
  97. } while (Interlocked.CompareExchange (ref count, newValue, oldCount) != oldCount);
  98. return true;
  99. }
  100. public void Wait ()
  101. {
  102. evt.Wait ();
  103. }
  104. public void Wait (CancellationToken token)
  105. {
  106. evt.Wait (token);
  107. }
  108. public bool Wait (int timeoutMilli)
  109. {
  110. return evt.Wait (timeoutMilli);
  111. }
  112. public bool Wait(TimeSpan span)
  113. {
  114. return evt.Wait (span);
  115. }
  116. public bool Wait (int timeoutMilli, CancellationToken token)
  117. {
  118. return evt.Wait (timeoutMilli, token);
  119. }
  120. public bool Wait(TimeSpan span, CancellationToken token)
  121. {
  122. return evt.Wait (span, token);
  123. }
  124. public void Reset ()
  125. {
  126. Reset (initial);
  127. }
  128. public void Reset (int value)
  129. {
  130. evt.Reset ();
  131. Interlocked.Exchange (ref count, value);
  132. }
  133. public int CurrentCount {
  134. get {
  135. return count;
  136. }
  137. }
  138. public int InitialCount {
  139. get {
  140. return initial;
  141. }
  142. }
  143. public bool IsSet {
  144. get {
  145. return count == 0;
  146. }
  147. }
  148. public WaitHandle WaitHandle {
  149. get {
  150. return evt.WaitHandle;
  151. }
  152. }
  153. #region IDisposable implementation
  154. public void Dispose ()
  155. {
  156. }
  157. protected virtual void Dispose (bool managedRes)
  158. {
  159. }
  160. #endregion
  161. }
  162. }
  163. #endif