New tests.
[mono.git] / mcs / class / corlib / System.Threading / CountdownEvent.cs
index 935ccf03ff52fca619c31163ad35eb9448f5209b..c12c85ed67c632f7ae45f44c10e0d45630cdf851 100644 (file)
@@ -1,4 +1,3 @@
-#if NET_4_0
 // CountdownEvent.cs
 //
 // Copyright (c) 2008 Jérémie "Garuma" Laval
 
 using System;
 
+#if NET_4_0 || BOOTSTRAP_NET_4_0
+
 namespace System.Threading
 {      
        public class CountdownEvent : IDisposable
        {
                int count;
                readonly int initial;
-               bool isCanceled;
                ManualResetEvent evt = new ManualResetEvent (false);
                
                public CountdownEvent (int count)
@@ -48,21 +48,19 @@ namespace System.Threading
                
                public bool Signal (int num)
                {
-                       if (num < 0)
+                       if (num <= 0)
                                throw new ArgumentOutOfRangeException ("num");
                        
                        Action<int> check = delegate (int value) {
                                if (value < 0)
                                throw new InvalidOperationException ("the specified count is larger that CurrentCount");
-                               if (IsCanceled)
-                               throw new OperationCanceledException ();
                        };
                        
                        int newValue;
                        if (!ApplyOperation (-num, check, out newValue))
                                throw new InvalidOperationException ("The event is already set");
                        
-                       if (newValue <= 0) {
+                       if (newValue == 0) {
                                evt.Set ();
                                return true;
                        }
@@ -94,12 +92,7 @@ namespace System.Threading
                        if (num < 0)
                                throw new ArgumentOutOfRangeException ("num");
                        
-                       Action<int> check = delegate (int value) {
-                               if (IsCanceled)
-                               throw new OperationCanceledException ();
-                       };
-                       
-                       return ApplyOperation (num, check);
+                       return ApplyOperation (num, null);
                }
                
                bool ApplyOperation (int num, Action<int> doCheck)
@@ -114,14 +107,14 @@ namespace System.Threading
                        newValue = 0;
                        
                        do {
-                               if (IsSet)
+                               oldCount = count;
+                               if (oldCount == 0)
                                        return false;
                                
-                               oldCount = count;
                                newValue = oldCount + num;
                                
-                               doCheck (newValue);
-                               
+                               if (doCheck != null)
+                                       doCheck (newValue);
                        } while (Interlocked.CompareExchange (ref count, newValue, oldCount) != oldCount);
                        
                        return true;
@@ -135,6 +128,11 @@ namespace System.Threading
                        }
                }
                
+               public void Wait (CancellationToken token)
+               {
+                       Wait (() => token.IsCancellationRequested);
+               }
+               
                public bool Wait (int timeoutMilli)
                {
                        if (timeoutMilli == -1) {
@@ -215,7 +213,7 @@ namespace System.Threading
                        
                public bool IsSet {
                        get {
-                               return count <= 0;
+                               return count == 0;
                        }
                }
                
@@ -231,25 +229,12 @@ namespace System.Threading
                {
                        
                }
-               #endregion 
-               
-               
-               #region ISupportsCancellation implementation 
                
-               public void Cancel ()
+               protected virtual void Dispose (bool managedRes)
                {
-                       Interlocked.Exchange (ref count, 0);
-                       isCanceled = true;
-               }
-               
-               public bool IsCanceled {
-                       get {
-                               return isCanceled;
-                       }
+                       
                }
-               
-               #endregion 
-               
+               #endregion      
        }
 }
 #endif