4 // Marek Safar <marek.safar@gmail.com>
6 // Copyright (c) 2008 Jérémie "Garuma" Laval
7 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
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:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
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
31 namespace System.Threading
33 [System.Diagnostics.DebuggerDisplayAttribute ("Initial Count={InitialCount}, Current Count={CurrentCount}")]
34 public class CountdownEvent : IDisposable
38 ManualResetEventSlim evt;
40 public CountdownEvent (int initialCount)
43 throw new ArgumentOutOfRangeException ("initialCount");
45 evt = new ManualResetEventSlim (initialCount == 0);
46 this.initial = this.initialCount = initialCount;
49 public int CurrentCount {
55 public int InitialCount {
63 return initialCount == 0;
67 public WaitHandle WaitHandle {
69 return evt.WaitHandle;
78 public bool Signal (int signalCount)
81 throw new ArgumentOutOfRangeException ("signalCount");
86 if (!ApplyOperation (-signalCount, out newValue))
87 throw new InvalidOperationException ("The event is already set");
97 public void AddCount ()
102 public void AddCount (int signalCount)
104 if (!TryAddCount (signalCount))
105 throw new InvalidOperationException ("The event is already signaled and cannot be incremented");
108 public bool TryAddCount ()
110 return TryAddCount (1);
113 public bool TryAddCount (int signalCount)
115 if (signalCount <= 0)
116 throw new ArgumentOutOfRangeException ("signalCount");
121 return ApplyOperation (signalCount, out temp);
124 bool ApplyOperation (int num, out int newValue)
129 oldCount = initialCount;
135 newValue = oldCount + num;
139 } while (Interlocked.CompareExchange (ref initialCount, newValue, oldCount) != oldCount);
149 public void Wait (CancellationToken cancellationToken)
151 evt.Wait (cancellationToken);
154 public bool Wait (int millisecondsTimeout)
156 return evt.Wait (millisecondsTimeout);
159 public bool Wait(TimeSpan timeout)
161 return evt.Wait (timeout);
164 public bool Wait (int millisecondsTimeout, CancellationToken cancellationToken)
166 return evt.Wait (millisecondsTimeout, cancellationToken);
169 public bool Wait(TimeSpan timeout, CancellationToken cancellationToken)
171 return evt.Wait (timeout, cancellationToken);
179 public void Reset (int count)
182 throw new ArgumentOutOfRangeException ("count");
186 initialCount = initial = count;
193 public void Dispose ()
198 protected virtual void Dispose (bool disposing)
204 void CheckDisposed ()
206 if (evt.disposed.Value)
207 throw new ObjectDisposedException ("CountdownEvent");