3 // Copyright (c) 2008 Jérémie "Garuma" Laval
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:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
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
27 #if NET_4_0 || BOOTSTRAP_NET_4_0
29 namespace System.Threading
31 public class CountdownEvent : IDisposable
35 ManualResetEvent evt = new ManualResetEvent (false);
37 public CountdownEvent (int count)
40 throw new ArgumentOutOfRangeException ("count is negative");
41 this.initial = this.count = count;
49 public bool Signal (int num)
52 throw new ArgumentOutOfRangeException ("num");
54 Action<int> check = delegate (int value) {
56 throw new InvalidOperationException ("the specified count is larger that CurrentCount");
60 if (!ApplyOperation (-num, check, out newValue))
61 throw new InvalidOperationException ("The event is already set");
71 public void AddCount ()
76 public void AddCount (int num)
79 throw new ArgumentOutOfRangeException ("num");
81 if (!TryAddCount (num))
82 throw new InvalidOperationException ("The event is already set");
85 public bool TryAddCount ()
87 return TryAddCount (1);
90 public bool TryAddCount (int num)
93 throw new ArgumentOutOfRangeException ("num");
95 return ApplyOperation (num, null);
98 bool ApplyOperation (int num, Action<int> doCheck)
101 return ApplyOperation (num, doCheck, out temp);
104 bool ApplyOperation (int num, Action<int> doCheck, out int newValue)
114 newValue = oldCount + num;
118 } while (Interlocked.CompareExchange (ref count, newValue, oldCount) != oldCount);
125 SpinWait wait = new SpinWait ();
131 public void Wait (CancellationToken token)
133 Wait (() => token.IsCancellationRequested);
136 public bool Wait (int timeoutMilli)
138 if (timeoutMilli == -1) {
143 Watch sw = Watch.StartNew ();
144 long timeout = (long)timeoutMilli;
146 bool result = Wait (() => sw.ElapsedMilliseconds > timeout);
152 public bool Wait(TimeSpan span)
154 return Wait ((int)span.TotalMilliseconds);
157 public bool Wait (int timeoutMilli, CancellationToken token)
159 if (timeoutMilli == -1) {
164 Watch sw = Watch.StartNew ();
165 long timeout = (long)timeoutMilli;
167 bool result = Wait (() => sw.ElapsedMilliseconds > timeout || token.IsCancellationRequested);
173 public bool Wait(TimeSpan span, CancellationToken token)
175 return Wait ((int)span.TotalMilliseconds, token);
178 bool Wait (Func<bool> waitPredicate)
180 SpinWait wait = new SpinWait ();
183 if (waitPredicate ())
196 public void Reset (int value)
199 Interlocked.Exchange (ref count, value);
202 public int CurrentCount {
208 public int InitialCount {
220 public WaitHandle WaitHandle {
226 #region IDisposable implementation
228 public void Dispose ()
233 protected virtual void Dispose (bool managedRes)