Barrier.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //
  2. // Barrier.cs
  3. //
  4. // Author:
  5. // Jérémie "Garuma" Laval <[email protected]>
  6. //
  7. // Copyright (c) 2009 Jérémie "Garuma" Laval
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. #if NET_4_0 || BOOTSTRAP_NET_4_0
  27. using System;
  28. namespace System.Threading
  29. {
  30. public class Barrier : IDisposable
  31. {
  32. const int MaxParticipants = 32767;
  33. Action<Barrier> postPhaseAction;
  34. int participants;
  35. bool cleaned;
  36. CountdownEvent cntd;
  37. ManualResetEventSlim postPhaseEvt = new ManualResetEventSlim ();
  38. long phase;
  39. public Barrier (int participants) : this (participants, null)
  40. {
  41. }
  42. public Barrier (int participants, Action<Barrier> postPhaseAction)
  43. {
  44. if (participants < 0 || participants > MaxParticipants)
  45. throw new ArgumentOutOfRangeException ("participants");
  46. this.participants = participants;
  47. this.postPhaseAction = postPhaseAction;
  48. InitCountdownEvent ();
  49. }
  50. public void Dispose ()
  51. {
  52. Dispose (true);
  53. }
  54. protected virtual void Dispose (bool disposing)
  55. {
  56. if (disposing){
  57. if (cntd != null){
  58. cntd.Dispose ();
  59. cntd = null;
  60. }
  61. postPhaseAction = null;
  62. cleaned = true;
  63. }
  64. }
  65. void InitCountdownEvent ()
  66. {
  67. postPhaseEvt = new ManualResetEventSlim (false);
  68. cntd = new CountdownEvent (participants);
  69. }
  70. public long AddParticipant ()
  71. {
  72. return AddParticipants (1);
  73. }
  74. static Exception GetDisposed ()
  75. {
  76. return new ObjectDisposedException ("Barrier");
  77. }
  78. public long AddParticipants (int participantCount)
  79. {
  80. if (cleaned)
  81. throw GetDisposed ();
  82. if (participantCount < 0)
  83. throw new InvalidOperationException ();
  84. // Basically, we try to add ourselves and return
  85. // the phase. If the call return false, we repeatdly try
  86. // to add ourselves for the next phase
  87. do {
  88. if (cntd.TryAddCount (participantCount)) {
  89. Interlocked.Add (ref participants, participantCount);
  90. return phase;
  91. }
  92. } while (true);
  93. }
  94. public void RemoveParticipant ()
  95. {
  96. RemoveParticipants (1);
  97. }
  98. public void RemoveParticipants (int participantCount)
  99. {
  100. if (cleaned)
  101. throw GetDisposed ();
  102. if (participantCount < 0)
  103. throw new ArgumentOutOfRangeException ("participantCount");
  104. if (cntd.Signal (participantCount))
  105. PostPhaseAction (postPhaseEvt);
  106. Interlocked.Add (ref participants, -participantCount);
  107. }
  108. public void SignalAndWait ()
  109. {
  110. if (cleaned)
  111. throw GetDisposed ();
  112. SignalAndWait ((c) => { c.Wait (); return true; });
  113. }
  114. public void SignalAndWait (CancellationToken token)
  115. {
  116. if (cleaned)
  117. throw GetDisposed ();
  118. SignalAndWait ((c) => { c.Wait (token); return true; });
  119. }
  120. public bool SignalAndWait (int millisecondTimeout)
  121. {
  122. if (cleaned)
  123. throw GetDisposed ();
  124. return SignalAndWait ((c) => c.Wait (millisecondTimeout));
  125. }
  126. public bool SignalAndWait (TimeSpan ts)
  127. {
  128. if (cleaned)
  129. throw GetDisposed ();
  130. return SignalAndWait ((c) => c.Wait (ts));
  131. }
  132. public bool SignalAndWait (int millisecondTimeout, CancellationToken token)
  133. {
  134. if (cleaned)
  135. throw GetDisposed ();
  136. return SignalAndWait ((c) => c.Wait (millisecondTimeout, token));
  137. }
  138. public bool SignalAndWait (TimeSpan ts, CancellationToken token)
  139. {
  140. if (cleaned)
  141. throw GetDisposed ();
  142. return SignalAndWait ((c) => c.Wait (ts, token));
  143. }
  144. bool SignalAndWait (Func<CountdownEvent, bool> associate)
  145. {
  146. bool result;
  147. CountdownEvent temp = cntd;
  148. ManualResetEventSlim evt = postPhaseEvt;
  149. if (!temp.Signal ()) {
  150. result = Wait (associate, temp, evt);
  151. } else {
  152. result = true;
  153. PostPhaseAction (evt);
  154. }
  155. return result;
  156. }
  157. bool Wait (Func<CountdownEvent, bool> associate, CountdownEvent temp, ManualResetEventSlim evt)
  158. {
  159. if (!associate (temp))
  160. return false;
  161. evt.Wait ();
  162. return true;
  163. }
  164. void PostPhaseAction (ManualResetEventSlim evt)
  165. {
  166. if (postPhaseAction != null) {
  167. try {
  168. postPhaseAction (this);
  169. } catch (Exception e) {
  170. throw new BarrierPostPhaseException (e);
  171. }
  172. }
  173. InitCountdownEvent ();
  174. phase++;
  175. evt.Set ();
  176. }
  177. public long CurrentPhaseNumber {
  178. get {
  179. return phase;
  180. }
  181. }
  182. public int ParticipantCount {
  183. get {
  184. return participants;
  185. }
  186. }
  187. }
  188. }
  189. #endif