[corlib] Fixes security tests failures
[mono.git] / mcs / class / corlib / System.Threading / WaitHandle.cs
index 75a1b207105e40bf59d12aae7a97245683f96116..5558efad52f46159fe85716d319e60902ec26eaa 100644 (file)
@@ -33,19 +33,16 @@ using System.Reflection;
 using System.Runtime.CompilerServices;
 using System.Runtime.Remoting.Contexts;
 using System.Security.Permissions;
-
-#if NET_2_0
 using System.Runtime.InteropServices;
 using Microsoft.Win32.SafeHandles;
 using System.Runtime.ConstrainedExecution;
-#endif
 
 namespace System.Threading
 {
-#if NET_2_0
        [ComVisible (true)]
-#endif
-       public abstract class WaitHandle : MarshalByRefObject, IDisposable
+       [StructLayout (LayoutKind.Sequential)]
+       public abstract class WaitHandle
+               : MarshalByRefObject, IDisposable
        {
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                private static extern bool WaitAll_internal(WaitHandle[] handles, int ms, bool exitContext);
@@ -59,6 +56,14 @@ namespace System.Threading
                        if (length > 64)
                                throw new NotSupportedException ("Too many handles");
 
+                       if (handles.Length == 0) {
+                               // MS throws different exceptions from the different methods.
+                               if (waitAll)
+                                       throw new ArgumentNullException ("waitHandles");
+                               else
+                                       throw new ArgumentException ();
+                       }
+
 #if false
                        //
                        // Although we should thrown an exception if this is an STA thread,
@@ -74,13 +79,9 @@ namespace System.Threading
                                if (w == null)
                                        throw new ArgumentNullException ("waitHandles", "null handle");
 
-#if NET_2_0
                                if (w.safe_wait_handle == null)
                                        throw new ArgumentException ("null element found", "waitHandle");
-#else
-                               if (w.os_handle == InvalidHandle)
-                                       throw new ArgumentException ("null element found", "waitHandle");
-#endif
+
                        }
                }
 #if false
@@ -111,8 +112,18 @@ namespace System.Threading
                public static bool WaitAll(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext)
                {
                        CheckArray (waitHandles, true);
+                       // check negative - except for -1 (which is Timeout.Infinite)
+                       if (millisecondsTimeout < Timeout.Infinite)
+                               throw new ArgumentOutOfRangeException ("millisecondsTimeout");
+
                        try {
-                               if (exitContext) SynchronizationAttribute.ExitContext ();
+                               if (exitContext) {
+#if MONOTOUCH
+                                       throw new NotSupportedException ("exitContext == true is not supported");
+#else
+                                       SynchronizationAttribute.ExitContext ();
+#endif
+                               }
                                return(WaitAll_internal(waitHandles, millisecondsTimeout, false));
                        }
                        finally {
@@ -131,7 +142,13 @@ namespace System.Threading
                                throw new ArgumentOutOfRangeException ("timeout");
 
                        try {
-                               if (exitContext) SynchronizationAttribute.ExitContext ();
+                               if (exitContext) {
+#if MONOTOUCH
+                                       throw new NotSupportedException ("exitContext == true is not supported");
+#else
+                                       SynchronizationAttribute.ExitContext ();
+#endif
+                               }
                                return (WaitAll_internal (waitHandles, (int) ms, exitContext));
                        }
                        finally {
@@ -143,25 +160,31 @@ namespace System.Threading
                private static extern int WaitAny_internal(WaitHandle[] handles, int ms, bool exitContext);
 
                // LAMESPEC: Doesn't specify how to signal failures
-#if NET_2_0
                [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
-#endif
                public static int WaitAny(WaitHandle[] waitHandles)
                {
                        CheckArray (waitHandles, false);
                        return(WaitAny_internal(waitHandles, Timeout.Infinite, false));
                }
 
-#if NET_2_0
                [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
-#endif
                public static int WaitAny(WaitHandle[] waitHandles,
                                          int millisecondsTimeout,
                                          bool exitContext)
                {
                        CheckArray (waitHandles, false);
+                       // check negative - except for -1 (which is Timeout.Infinite)
+                       if (millisecondsTimeout < Timeout.Infinite)
+                               throw new ArgumentOutOfRangeException ("millisecondsTimeout");
+
                        try {
-                               if (exitContext) SynchronizationAttribute.ExitContext ();
+                               if (exitContext) {
+#if MONOTOUCH
+                                       throw new NotSupportedException ("exitContext == true is not supported");
+#else
+                                       SynchronizationAttribute.ExitContext ();
+#endif
+                               }
                                return(WaitAny_internal(waitHandles, millisecondsTimeout, exitContext));
                        }
                        finally {
@@ -169,9 +192,19 @@ namespace System.Threading
                        }
                }
 
-#if NET_2_0
                [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
-#endif
+               public static int WaitAny(WaitHandle[] waitHandles, TimeSpan timeout)
+               {
+                       return WaitAny (waitHandles, timeout, false);
+               }
+
+               [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
+               public static int WaitAny(WaitHandle[] waitHandles, int millisecondsTimeout)
+               {
+                       return WaitAny (waitHandles, millisecondsTimeout, false);
+               }
+
+               [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
                public static int WaitAny(WaitHandle[] waitHandles,
                                          TimeSpan timeout, bool exitContext)
                {
@@ -182,7 +215,13 @@ namespace System.Threading
                                throw new ArgumentOutOfRangeException ("timeout");
 
                        try {
-                               if (exitContext) SynchronizationAttribute.ExitContext ();
+                               if (exitContext) {
+#if MONOTOUCH
+                                       throw new NotSupportedException ("exitContext == true is not supported");
+#else
+                                       SynchronizationAttribute.ExitContext ();
+#endif
+                               }
                                return (WaitAny_internal(waitHandles, (int) ms, exitContext));
                        }
                        finally {
@@ -190,24 +229,23 @@ namespace System.Threading
                        }
                }
 
-               [MonoTODO]
-#if NET_2_0
-               protected
-#else
-               public
-#endif
-               WaitHandle() {
+               protected WaitHandle()
+               {
                        // FIXME
                }
 
-               public virtual void Close() {
+               public virtual void Close ()
+               {
                        Dispose(true);
-                       GC.SuppressFinalize (this);
+               }
+
+               public void Dispose ()
+               {
+                       Close ();
                }
 
                public const int WaitTimeout = 258;
 
-#if NET_2_0
                //
                // In 2.0 we use SafeWaitHandles instead of IntPtrs
                //
@@ -235,7 +273,6 @@ namespace System.Threading
                protected virtual void Dispose (bool explicitDisposing)
                {
                        if (!disposed){
-                               disposed = true;
 
                                //
                                // This is only the case if the handle was never properly initialized
@@ -245,6 +282,10 @@ namespace System.Threading
                                        return;
 
                                lock (this){
+                                       if (disposed)
+                                               return;
+
+                                       disposed = true;
                                        if (safe_wait_handle != null)
                                                safe_wait_handle.Dispose ();
                                }
@@ -319,15 +360,24 @@ namespace System.Threading
                public virtual bool WaitOne(int millisecondsTimeout, bool exitContext)
                {
                        CheckDisposed ();
+                       // check negative - except for -1 (which is Timeout.Infinite)
+                       if (millisecondsTimeout < Timeout.Infinite)
+                               throw new ArgumentOutOfRangeException ("millisecondsTimeout");
+
                        bool release = false;
                        try {
-                               if (exitContext)
+                               if (exitContext) {
+#if !MONOTOUCH
                                        SynchronizationAttribute.ExitContext ();
+#endif
+                               }
                                safe_wait_handle.DangerousAddRef (ref release);
                                return (WaitOne_internal(safe_wait_handle.DangerousGetHandle (), millisecondsTimeout, exitContext));
                        } finally {
+#if !MONOTOUCH
                                if (exitContext)
                                        SynchronizationAttribute.EnterContext ();
+#endif
                                if (release)
                                        safe_wait_handle.DangerousRelease ();
                        }
@@ -338,6 +388,11 @@ namespace System.Threading
                        return WaitOne (millisecondsTimeout, false);
                }
 
+               public virtual bool WaitOne (TimeSpan timeout)
+               {
+                       return WaitOne (timeout, false);
+               }
+
                public virtual bool WaitOne(TimeSpan timeout, bool exitContext)
                {
                        CheckDisposed ();
@@ -347,14 +402,19 @@ namespace System.Threading
 
                        bool release = false;
                        try {
-                               if (exitContext)
+                               if (exitContext) {
+#if !MONOTOUCH
                                        SynchronizationAttribute.ExitContext ();
+#endif
+                               }
                                safe_wait_handle.DangerousAddRef (ref release);
                                return (WaitOne_internal(safe_wait_handle.DangerousGetHandle (), (int) ms, exitContext));
                        }
                        finally {
+#if !MONOTOUCH
                                if (exitContext)
                                        SynchronizationAttribute.EnterContext ();
+#endif
                                if (release)
                                        safe_wait_handle.DangerousRelease ();
                        }
@@ -368,107 +428,15 @@ namespace System.Threading
 
                public static bool WaitAll(WaitHandle[] waitHandles, int millisecondsTimeout)
                {
-                       CheckArray (waitHandles, true);
-                       return WaitAll_internal (waitHandles, millisecondsTimeout, false);
+                       return WaitAll (waitHandles, millisecondsTimeout, false);
                }
 
                public static bool WaitAll(WaitHandle[] waitHandles, TimeSpan timeout)
                {
-                       CheckArray (waitHandles, true);
-                       long ms = (long) timeout.TotalMilliseconds;
-                       
-                       if (ms < -1 || ms > Int32.MaxValue)
-                               throw new ArgumentOutOfRangeException ("timeout");
-
-                       return (WaitAll_internal (waitHandles, (int) ms, false));
-               }
-               
-#else
-               private IntPtr os_handle = InvalidHandle;
-               
-               public virtual IntPtr Handle {
-                       get {
-                               return(os_handle);
-                       }
-                               
-                       [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
-                       [SecurityPermission (SecurityAction.InheritanceDemand, UnmanagedCode = true)]
-                       set {
-                               os_handle=value;
-                       }
-               }
-
-               internal void CheckDisposed ()
-               {
-                       if (disposed || os_handle == InvalidHandle)
-                               throw new ObjectDisposedException (GetType ().FullName);
-               }
-               
-               [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               private extern bool WaitOne_internal(IntPtr handle, int ms, bool exitContext);
-
-               protected virtual void Dispose(bool explicitDisposing) {
-                       // Check to see if Dispose has already been called.
-                       if (!disposed) {
-                               disposed=true;
-                               if (os_handle == InvalidHandle)
-                                       return;
-
-                               lock (this) {
-                                       if (os_handle != InvalidHandle) {
-                                               NativeEventCalls.CloseEvent_internal (os_handle);
-                                               os_handle = InvalidHandle;
-                                       }
-                               }
-                       }
+                       return WaitAll (waitHandles, timeout, false);
                }
                
-               public virtual bool WaitOne()
-               {
-                       CheckDisposed ();
-                       return(WaitOne_internal(os_handle, Timeout.Infinite, false));
-               }
-
-               public virtual bool WaitOne(int millisecondsTimeout, bool exitContext)
-               {
-                       CheckDisposed ();
-                       try {
-                               if (exitContext) SynchronizationAttribute.ExitContext ();
-                               return(WaitOne_internal(os_handle, millisecondsTimeout, exitContext));
-                       }
-                       finally {
-                               if (exitContext) SynchronizationAttribute.EnterContext ();
-                       }
-               }
-
-               public virtual bool WaitOne(TimeSpan timeout, bool exitContext)
-               {
-                       CheckDisposed ();
-                       long ms = (long) timeout.TotalMilliseconds;
-                       if (ms < -1 || ms > Int32.MaxValue)
-                               throw new ArgumentOutOfRangeException ("timeout");
-
-                       try {
-                               if (exitContext) SynchronizationAttribute.ExitContext ();
-                               return (WaitOne_internal(os_handle, (int) ms, exitContext));
-                       }
-                       finally {
-                               if (exitContext) SynchronizationAttribute.EnterContext ();
-                       }
-               }
-#endif
-
                protected static readonly IntPtr InvalidHandle = (IntPtr) (-1);
                bool disposed = false;
-
-               void IDisposable.Dispose() {
-                       Dispose(true);
-                       // Take yourself off the Finalization queue
-                       GC.SuppressFinalize(this);
-               }
-               
-               ~WaitHandle() {
-                       Dispose(false);
-               }
        }
 }