Merged pull request #58 from XTZGZoReX/master.
[mono.git] / mcs / class / corlib / System.Threading / CountdownEvent.cs
index c12c85ed67c632f7ae45f44c10e0d45630cdf851..59c282ea567cd48aad06ab1008655f6b554be0b2 100644 (file)
 //
 //
 
-using System;
+#if NET_4_0 || MOBILE
 
-#if NET_4_0 || BOOTSTRAP_NET_4_0
+using System;
 
 namespace System.Threading
-{      
+{
+       [System.Diagnostics.DebuggerDisplayAttribute ("Initial Count={InitialCount}, Current Count={CurrentCount}")]
        public class CountdownEvent : IDisposable
        {
-               int count;
-               readonly int initial;
-               ManualResetEvent evt = new ManualResetEvent (false);
+               int initialCount;
+               int initial;
+               ManualResetEventSlim evt = new ManualResetEventSlim (false);
                
-               public CountdownEvent (int count)
+               public CountdownEvent (int initialCount)
                {
-                       if (count < 0)
-                               throw new ArgumentOutOfRangeException ("count is negative");
-                       this.initial = this.count = count;
+                       if (initialCount < 0)
+                               throw new ArgumentOutOfRangeException ("initialCount is negative");
+                       this.initial = this.initialCount = initialCount;
                }
                
                public bool Signal ()
@@ -46,18 +47,18 @@ namespace System.Threading
                        return Signal (1);
                }
                
-               public bool Signal (int num)
+               public bool Signal (int signalCount)
                {
-                       if (num <= 0)
-                               throw new ArgumentOutOfRangeException ("num");
+                       if (signalCount <= 0)
+                               throw new ArgumentOutOfRangeException ("signalCount");
                        
                        Action<int> check = delegate (int value) {
                                if (value < 0)
-                               throw new InvalidOperationException ("the specified count is larger that CurrentCount");
+                               throw new InvalidOperationException ("the specified initialCount is larger that CurrentCount");
                        };
                        
                        int newValue;
-                       if (!ApplyOperation (-num, check, out newValue))
+                       if (!ApplyOperation (-signalCount, check, out newValue))
                                throw new InvalidOperationException ("The event is already set");
                        
                        if (newValue == 0) {
@@ -73,12 +74,12 @@ namespace System.Threading
                        AddCount (1);
                }
                
-               public void AddCount (int num)
+               public void AddCount (int signalCount)
                {
-                       if (num < 0)
-                               throw new ArgumentOutOfRangeException ("num");
+                       if (signalCount < 0)
+                               throw new ArgumentOutOfRangeException ("signalCount");
                        
-                       if (!TryAddCount (num))
+                       if (!TryAddCount (signalCount))
                                throw new InvalidOperationException ("The event is already set");
                }
                
@@ -87,12 +88,12 @@ namespace System.Threading
                        return TryAddCount (1);
                }
                
-               public bool TryAddCount (int num)
+               public bool TryAddCount (int signalCount)
                {       
-                       if (num < 0)
-                               throw new ArgumentOutOfRangeException ("num");
+                       if (signalCount < 0)
+                               throw new ArgumentOutOfRangeException ("signalCount");
                        
-                       return ApplyOperation (num, null);
+                       return ApplyOperation (signalCount, null);
                }
                
                bool ApplyOperation (int num, Action<int> doCheck)
@@ -107,7 +108,7 @@ namespace System.Threading
                        newValue = 0;
                        
                        do {
-                               oldCount = count;
+                               oldCount = initialCount;
                                if (oldCount == 0)
                                        return false;
                                
@@ -115,93 +116,55 @@ namespace System.Threading
                                
                                if (doCheck != null)
                                        doCheck (newValue);
-                       } while (Interlocked.CompareExchange (ref count, newValue, oldCount) != oldCount);
+                       } while (Interlocked.CompareExchange (ref initialCount, newValue, oldCount) != oldCount);
                        
                        return true;
                }
                
                public void Wait ()
                {
-                       SpinWait wait = new SpinWait ();
-                       while (!IsSet) {
-                               wait.SpinOnce ();
-                       }
+                       evt.Wait ();
                }
                
-               public void Wait (CancellationToken token)
+               public void Wait (CancellationToken cancellationToken)
                {
-                       Wait (() => token.IsCancellationRequested);
+                       evt.Wait (cancellationToken);
                }
                
-               public bool Wait (int timeoutMilli)
+               public bool Wait (int millisecondsTimeout)
                {
-                       if (timeoutMilli == -1) {
-                               Wait ();
-                               return true;
-                       }
-                       
-                       Watch sw = Watch.StartNew ();
-                       long timeout = (long)timeoutMilli;
-                       
-                       bool result = Wait (() => sw.ElapsedMilliseconds > timeout);
-                       sw.Stop ();
-                       
-                       return result;
+                       return evt.Wait (millisecondsTimeout);
                }
                
-               public bool Wait(TimeSpan span)
+               public bool Wait(TimeSpan timeout)
                {
-                       return Wait ((int)span.TotalMilliseconds);
+                       return evt.Wait (timeout);
                }
                
-               public bool Wait (int timeoutMilli, CancellationToken token)
+               public bool Wait (int millisecondsTimeout, CancellationToken cancellationToken)
                {
-                       if (timeoutMilli == -1) {
-                               Wait ();
-                               return true;
-                       }
-                       
-                       Watch sw = Watch.StartNew ();
-                       long timeout = (long)timeoutMilli;
-                       
-                       bool result = Wait (() => sw.ElapsedMilliseconds > timeout || token.IsCancellationRequested);
-                       sw.Stop ();
-                       
-                       return result;
+                       return evt.Wait (millisecondsTimeout, cancellationToken);
                }
                
-               public bool Wait(TimeSpan span, CancellationToken token)
+               public bool Wait(TimeSpan timeout, CancellationToken cancellationToken)
                {
-                       return Wait ((int)span.TotalMilliseconds, token);
+                       return evt.Wait (timeout, cancellationToken);
                }
-               
-               bool Wait (Func<bool> waitPredicate)
-               {
-                       SpinWait wait = new SpinWait ();
-                       
-                       while (!IsSet) {
-                               if (waitPredicate ())
-                                       return false;
-                               wait.SpinOnce ();
-                       }
-                       
-                       return true;
-               }
-               
+
                public void Reset ()
                {
                        Reset (initial);
                }
                
-               public void Reset (int value)
+               public void Reset (int count)
                {
                        evt.Reset ();
-                       Interlocked.Exchange (ref count, value);
+                       initialCount = initial = count;
                }
                
                public int CurrentCount {
                        get {
-                               return count;
+                               return initialCount;
                        }
                }
                
@@ -213,13 +176,13 @@ namespace System.Threading
                        
                public bool IsSet {
                        get {
-                               return count == 0;
+                               return initialCount == 0;
                        }
                }
                
                public WaitHandle WaitHandle {
                        get {
-                               return evt;
+                               return evt.WaitHandle;
                        }
                }
 
@@ -230,7 +193,7 @@ namespace System.Threading
                        
                }
                
-               protected virtual void Dispose (bool managedRes)
+               protected virtual void Dispose (bool disposing)
                {
                        
                }