2006-07-04 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / corlib / System.Threading / WaitHandle.cs
old mode 100755 (executable)
new mode 100644 (file)
index 85e1b9b..cc80b77
@@ -6,10 +6,7 @@
 //     Gonzalo Paniagua Javier (gonzalo@ximian.com
 //
 // (C) 2002,2003 Ximian, Inc.  (http://www.ximian.com)
-//
-
-//
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
+using System;
+using System.Reflection;
 using System.Runtime.CompilerServices;
 using System.Runtime.Remoting.Contexts;
+using System.Security.Permissions;
 
 namespace System.Threading
 {
@@ -41,7 +41,7 @@ namespace System.Threading
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                private static extern bool WaitAll_internal(WaitHandle[] handles, int ms, bool exitContext);
                
-               static void CheckArray (WaitHandle [] handles)
+               static void CheckArray (WaitHandle [] handles, bool waitAll)
                {
                        if (handles == null)
                                throw new ArgumentNullException ("waitHandles");
@@ -50,6 +50,11 @@ namespace System.Threading
                        if (length > 64)
                                throw new NotSupportedException ("Too many handles");
 
+                       if (waitAll && length > 1 &&
+                           (Thread.CurrentThread.ApartmentState == ApartmentState.STA ||
+                            Assembly.GetEntryAssembly ().EntryPoint.GetCustomAttributes (typeof (STAThreadAttribute), false).Length == 1))
+                               throw new NotSupportedException ("WaitAll for multiple handles is not allowed on an STA thread.");
+                       
                        foreach (WaitHandle w in handles) {
                                if (w == null)
                                        throw new ArgumentNullException ("waitHandles", "null handle");
@@ -61,13 +66,13 @@ namespace System.Threading
                
                public static bool WaitAll(WaitHandle[] waitHandles)
                {
-                       CheckArray (waitHandles);
+                       CheckArray (waitHandles, true);
                        return(WaitAll_internal(waitHandles, Timeout.Infinite, false));
                }
 
                public static bool WaitAll(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext)
                {
-                       CheckArray (waitHandles);
+                       CheckArray (waitHandles, true);
                        try {
                                if (exitContext) SynchronizationAttribute.ExitContext ();
                                return(WaitAll_internal(waitHandles, millisecondsTimeout, false));
@@ -81,7 +86,7 @@ namespace System.Threading
                                           TimeSpan timeout,
                                           bool exitContext)
                {
-                       CheckArray (waitHandles);
+                       CheckArray (waitHandles, true);
                        long ms = (long) timeout.TotalMilliseconds;
                        
                        if (ms < -1 || ms > Int32.MaxValue)
@@ -102,7 +107,7 @@ namespace System.Threading
                // LAMESPEC: Doesn't specify how to signal failures
                public static int WaitAny(WaitHandle[] waitHandles)
                {
-                       CheckArray (waitHandles);
+                       CheckArray (waitHandles, false);
                        return(WaitAny_internal(waitHandles, Timeout.Infinite, false));
                }
 
@@ -110,7 +115,7 @@ namespace System.Threading
                                          int millisecondsTimeout,
                                          bool exitContext)
                {
-                       CheckArray (waitHandles);
+                       CheckArray (waitHandles, false);
                        try {
                                if (exitContext) SynchronizationAttribute.ExitContext ();
                                return(WaitAny_internal(waitHandles, millisecondsTimeout, exitContext));
@@ -123,7 +128,7 @@ namespace System.Threading
                public static int WaitAny(WaitHandle[] waitHandles,
                                          TimeSpan timeout, bool exitContext)
                {
-                       CheckArray (waitHandles);
+                       CheckArray (waitHandles, false);
                        long ms = (long) timeout.TotalMilliseconds;
                        
                        if (ms < -1 || ms > Int32.MaxValue)
@@ -152,6 +157,8 @@ namespace System.Threading
                                return(os_handle);
                        }
                                
+                       [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
+                       [SecurityPermission (SecurityAction.InheritanceDemand, UnmanagedCode = true)]
                        set {
                                os_handle=value;
                        }
@@ -207,7 +214,7 @@ namespace System.Threading
 
                protected static readonly IntPtr InvalidHandle = IntPtr.Zero;
 
-               private bool disposed = false;
+               bool disposed = false;
 
                void IDisposable.Dispose() {
                        Dispose(true);