4 // Copyright (c) 2008 Jérémie "Garuma" Laval
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:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
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
28 namespace System.Threading
30 public class CountdownEvent : IDisposable
34 ManualResetEvent evt = new ManualResetEvent (false);
36 public CountdownEvent (int count)
39 throw new ArgumentOutOfRangeException ("count is negative");
40 this.initial = this.count = count;
48 public bool Signal (int num)
51 throw new ArgumentOutOfRangeException ("num");
53 Action<int> check = delegate (int value) {
55 throw new InvalidOperationException ("the specified count is larger that CurrentCount");
59 if (!ApplyOperation (-num, check, out newValue))
60 throw new InvalidOperationException ("The event is already set");
70 public void AddCount ()
75 public void AddCount (int num)
78 throw new ArgumentOutOfRangeException ("num");
80 if (!TryAddCount (num))
81 throw new InvalidOperationException ("The event is already set");
84 public bool TryAddCount ()
86 return TryAddCount (1);
89 public bool TryAddCount (int num)
92 throw new ArgumentOutOfRangeException ("num");
94 Action<int> check = delegate (int value) { };
96 return ApplyOperation (num, check);
99 bool ApplyOperation (int num, Action<int> doCheck)
102 return ApplyOperation (num, doCheck, out temp);
105 bool ApplyOperation (int num, Action<int> doCheck, out int newValue)
115 newValue = oldCount + num;
119 } while (Interlocked.CompareExchange (ref count, newValue, oldCount) != oldCount);
126 SpinWait wait = new SpinWait ();
132 public void Wait (CancellationToken token)
134 Wait (() => token.IsCancellationRequested);
137 public bool Wait (int timeoutMilli)
139 if (timeoutMilli == -1) {
144 Watch sw = Watch.StartNew ();
145 long timeout = (long)timeoutMilli;
147 bool result = Wait (() => sw.ElapsedMilliseconds > timeout);
153 public bool Wait(TimeSpan span)
155 return Wait ((int)span.TotalMilliseconds);
158 public bool Wait (int timeoutMilli, CancellationToken token)
160 if (timeoutMilli == -1) {
165 Watch sw = Watch.StartNew ();
166 long timeout = (long)timeoutMilli;
168 bool result = Wait (() => sw.ElapsedMilliseconds > timeout || token.IsCancellationRequested);
174 public bool Wait(TimeSpan span, CancellationToken token)
176 return Wait ((int)span.TotalMilliseconds, token);
179 bool Wait (Func<bool> waitPredicate)
181 SpinWait wait = new SpinWait ();
184 if (waitPredicate ())
197 public void Reset (int value)
200 Interlocked.Exchange (ref count, value);
203 public int CurrentCount {
209 public int InitialCount {
221 public WaitHandle WaitHandle {
227 #region IDisposable implementation
229 public void Dispose ()
234 protected virtual void Dispose (bool managedRes)