Merge branch 'master' of github.com:mono/mono
[mono.git] / mcs / class / corlib / System.Threading / WaitHandle.cs
index ca831ab2ec150a59354600c909b8692fe9595256..0c4daaf09ad5d71e60c3dca93d76f7f104140117 100644 (file)
@@ -33,18 +33,13 @@ 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
        {
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
@@ -59,6 +54,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 +77,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,6 +110,10 @@ 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 ();
                                return(WaitAll_internal(waitHandles, millisecondsTimeout, false));
@@ -143,23 +146,23 @@ 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 ();
                                return(WaitAny_internal(waitHandles, millisecondsTimeout, exitContext));
@@ -169,9 +172,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)
                {
@@ -190,13 +203,8 @@ namespace System.Threading
                        }
                }
 
-               [MonoTODO]
-#if NET_2_0
-               protected
-#else
-               public
-#endif
-               WaitHandle() {
+               protected WaitHandle()
+               {
                        // FIXME
                }
 
@@ -205,9 +213,17 @@ namespace System.Threading
                        GC.SuppressFinalize (this);
                }
 
+#if NET_4_0
+               public void Dispose ()
+#else          
+               void IDisposable.Dispose ()
+#endif
+               {
+                       Close ();
+               }
+
                public const int WaitTimeout = 258;
 
-#if NET_2_0
                //
                // In 2.0 we use SafeWaitHandles instead of IntPtrs
                //
@@ -266,31 +282,43 @@ namespace System.Threading
                        }
                }
 
-               [MonoTODO]
                public static bool SignalAndWait (WaitHandle toSignal,
                                                  WaitHandle toWaitOn)
                {
-                       throw new NotImplementedException ();
+                       return SignalAndWait (toSignal, toWaitOn, -1, false);
                }
                
-               [MonoTODO]
                public static bool SignalAndWait (WaitHandle toSignal,
                                                  WaitHandle toWaitOn,
                                                  int millisecondsTimeout,
                                                  bool exitContext)
                {
-                       throw new NotImplementedException ();
+                       if (toSignal == null)
+                               throw new ArgumentNullException ("toSignal");
+                       if (toWaitOn == null)
+                               throw new ArgumentNullException ("toWaitOn");
+
+                       if (millisecondsTimeout < -1)
+                               throw new ArgumentOutOfRangeException ("millisecondsTimeout");
+
+                       return SignalAndWait_Internal (toSignal.Handle, toWaitOn.Handle, millisecondsTimeout, exitContext);
                }
                
-               [MonoTODO]
                public static bool SignalAndWait (WaitHandle toSignal,
                                                  WaitHandle toWaitOn,
                                                  TimeSpan timeout,
                                                  bool exitContext)
                {
-                       throw new NotImplementedException ();
+                       double ms = timeout.TotalMilliseconds;
+                       if (ms > Int32.MaxValue)
+                               throw new ArgumentOutOfRangeException ("timeout");
+
+                       return SignalAndWait (toSignal, toWaitOn, Convert.ToInt32 (ms), false);
                }
 
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               static extern bool SignalAndWait_Internal (IntPtr toSignal, IntPtr toWaitOn, int ms, bool exitContext);
+
                public virtual bool WaitOne()
                {
                        CheckDisposed ();
@@ -307,6 +335,10 @@ 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)
@@ -326,6 +358,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 ();
@@ -353,90 +390,20 @@ namespace System.Threading
                        if (disposed || safe_wait_handle == null)
                                throw new ObjectDisposedException (GetType ().FullName);
                }
-#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;
-                                       }
-                               }
-                       }
-               }
-               
-               public virtual bool WaitOne()
+               public static bool WaitAll(WaitHandle[] waitHandles, int millisecondsTimeout)
                {
-                       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 ();
-                       }
+                       return WaitAll (waitHandles, millisecondsTimeout, false);
                }
 
-               public virtual bool WaitOne(TimeSpan timeout, bool exitContext)
+               public static bool WaitAll(WaitHandle[] waitHandles, TimeSpan timeout)
                {
-                       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 ();
-                       }
+                       return WaitAll (waitHandles, timeout, false);
                }
-#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);
                }