CountdownEvent.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #if NET_4_0 || BOOTSTRAP_NET_4_0
  2. // CountdownEvent.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. namespace System.Threading
  27. {
  28. public class CountdownEvent : IDisposable
  29. {
  30. int count;
  31. readonly int initial;
  32. ManualResetEvent evt = new ManualResetEvent (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. SpinWait wait = new SpinWait ();
  103. while (!IsSet) {
  104. wait.SpinOnce ();
  105. }
  106. }
  107. public void Wait (CancellationToken token)
  108. {
  109. Wait (() => token.IsCancellationRequested);
  110. }
  111. public bool Wait (int timeoutMilli)
  112. {
  113. if (timeoutMilli == -1) {
  114. Wait ();
  115. return true;
  116. }
  117. Watch sw = Watch.StartNew ();
  118. long timeout = (long)timeoutMilli;
  119. bool result = Wait (() => sw.ElapsedMilliseconds > timeout);
  120. sw.Stop ();
  121. return result;
  122. }
  123. public bool Wait(TimeSpan span)
  124. {
  125. return Wait ((int)span.TotalMilliseconds);
  126. }
  127. public bool Wait (int timeoutMilli, CancellationToken token)
  128. {
  129. if (timeoutMilli == -1) {
  130. Wait ();
  131. return true;
  132. }
  133. Watch sw = Watch.StartNew ();
  134. long timeout = (long)timeoutMilli;
  135. bool result = Wait (() => sw.ElapsedMilliseconds > timeout || token.IsCancellationRequested);
  136. sw.Stop ();
  137. return result;
  138. }
  139. public bool Wait(TimeSpan span, CancellationToken token)
  140. {
  141. return Wait ((int)span.TotalMilliseconds, token);
  142. }
  143. bool Wait (Func<bool> waitPredicate)
  144. {
  145. SpinWait wait = new SpinWait ();
  146. while (!IsSet) {
  147. if (waitPredicate ())
  148. return false;
  149. wait.SpinOnce ();
  150. }
  151. return true;
  152. }
  153. public void Reset ()
  154. {
  155. Reset (initial);
  156. }
  157. public void Reset (int value)
  158. {
  159. evt.Reset ();
  160. Interlocked.Exchange (ref count, value);
  161. }
  162. public int CurrentCount {
  163. get {
  164. return count;
  165. }
  166. }
  167. public int InitialCount {
  168. get {
  169. return initial;
  170. }
  171. }
  172. public bool IsSet {
  173. get {
  174. return count == 0;
  175. }
  176. }
  177. public WaitHandle WaitHandle {
  178. get {
  179. return evt;
  180. }
  181. }
  182. #region IDisposable implementation
  183. public void Dispose ()
  184. {
  185. }
  186. protected virtual void Dispose (bool managedRes)
  187. {
  188. }
  189. #endregion
  190. }
  191. }
  192. #endif