2 // System.Threading.WaitHandle.cs
5 // Dick Porter (dick@ximian.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com
8 // (C) 2002,2003 Ximian, Inc. (http://www.ximian.com)
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Reflection;
33 using System.Runtime.CompilerServices;
34 using System.Runtime.Remoting.Contexts;
35 using System.Security.Permissions;
36 using System.Runtime.InteropServices;
37 using Microsoft.Win32.SafeHandles;
38 using System.Runtime.ConstrainedExecution;
40 namespace System.Threading
43 [StructLayout (LayoutKind.Sequential)]
44 public abstract class WaitHandle
48 : MarshalByRefObject, IDisposable
51 [MethodImplAttribute(MethodImplOptions.InternalCall)]
52 private static extern bool WaitAll_internal(WaitHandle[] handles, int ms, bool exitContext);
54 static void CheckArray (WaitHandle [] handles, bool waitAll)
57 throw new ArgumentNullException ("waitHandles");
59 int length = handles.Length;
61 throw new NotSupportedException ("Too many handles");
63 if (handles.Length == 0) {
64 // MS throws different exceptions from the different methods.
66 throw new ArgumentNullException ("waitHandles");
68 throw new ArgumentException ();
73 // Although we should thrown an exception if this is an STA thread,
74 // Mono does not know anything about STA threads, and just makes
75 // things like Paint.NET not even possible to work.
77 // See bug #78455 for the bug this is supposed to fix.
79 if (waitAll && length > 1 && IsSTAThread)
80 throw new NotSupportedException ("WaitAll for multiple handles is not allowed on an STA thread.");
82 foreach (WaitHandle w in handles) {
84 throw new ArgumentNullException ("waitHandles", "null handle");
86 if (w.safe_wait_handle == null)
87 throw new ArgumentException ("null element found", "waitHandle");
92 // usage of property is commented - see above
93 static bool IsSTAThread {
95 bool isSTA = Thread.CurrentThread.ApartmentState ==
98 // FIXME: remove this check after Thread.ApartmentState
99 // has been properly implemented.
101 Assembly asm = Assembly.GetEntryAssembly ();
103 isSTA = asm.EntryPoint.GetCustomAttributes (typeof (STAThreadAttribute), false).Length > 0;
110 public static bool WaitAll(WaitHandle[] waitHandles)
112 CheckArray (waitHandles, true);
113 return(WaitAll_internal(waitHandles, Timeout.Infinite, false));
116 public static bool WaitAll(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext)
118 CheckArray (waitHandles, true);
119 // check negative - except for -1 (which is Timeout.Infinite)
120 if (millisecondsTimeout < Timeout.Infinite)
121 throw new ArgumentOutOfRangeException ("millisecondsTimeout");
126 throw new NotSupportedException ("exitContext == true is not supported");
128 SynchronizationAttribute.ExitContext ();
131 return(WaitAll_internal(waitHandles, millisecondsTimeout, false));
134 if (exitContext) SynchronizationAttribute.EnterContext ();
138 public static bool WaitAll(WaitHandle[] waitHandles,
142 CheckArray (waitHandles, true);
143 long ms = (long) timeout.TotalMilliseconds;
145 if (ms < -1 || ms > Int32.MaxValue)
146 throw new ArgumentOutOfRangeException ("timeout");
151 throw new NotSupportedException ("exitContext == true is not supported");
153 SynchronizationAttribute.ExitContext ();
156 return (WaitAll_internal (waitHandles, (int) ms, exitContext));
159 if (exitContext) SynchronizationAttribute.EnterContext ();
163 [MethodImplAttribute(MethodImplOptions.InternalCall)]
164 private static extern int WaitAny_internal(WaitHandle[] handles, int ms, bool exitContext);
166 // LAMESPEC: Doesn't specify how to signal failures
167 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
168 public static int WaitAny(WaitHandle[] waitHandles)
170 CheckArray (waitHandles, false);
171 return(WaitAny_internal(waitHandles, Timeout.Infinite, false));
174 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
175 public static int WaitAny(WaitHandle[] waitHandles,
176 int millisecondsTimeout,
179 CheckArray (waitHandles, false);
180 // check negative - except for -1 (which is Timeout.Infinite)
181 if (millisecondsTimeout < Timeout.Infinite)
182 throw new ArgumentOutOfRangeException ("millisecondsTimeout");
187 throw new NotSupportedException ("exitContext == true is not supported");
189 SynchronizationAttribute.ExitContext ();
192 return(WaitAny_internal(waitHandles, millisecondsTimeout, exitContext));
195 if (exitContext) SynchronizationAttribute.EnterContext ();
199 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
200 public static int WaitAny(WaitHandle[] waitHandles, TimeSpan timeout)
202 return WaitAny (waitHandles, timeout, false);
205 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
206 public static int WaitAny(WaitHandle[] waitHandles, int millisecondsTimeout)
208 return WaitAny (waitHandles, millisecondsTimeout, false);
211 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
212 public static int WaitAny(WaitHandle[] waitHandles,
213 TimeSpan timeout, bool exitContext)
215 CheckArray (waitHandles, false);
216 long ms = (long) timeout.TotalMilliseconds;
218 if (ms < -1 || ms > Int32.MaxValue)
219 throw new ArgumentOutOfRangeException ("timeout");
224 throw new NotSupportedException ("exitContext == true is not supported");
226 SynchronizationAttribute.ExitContext ();
229 return (WaitAny_internal(waitHandles, (int) ms, exitContext));
232 if (exitContext) SynchronizationAttribute.EnterContext ();
236 protected WaitHandle()
241 public virtual void Close ()
246 #if NET_4_0 || MOBILE || MOONLIGHT
247 public void Dispose ()
249 void IDisposable.Dispose ()
255 public const int WaitTimeout = 258;
258 // In 2.0 we use SafeWaitHandles instead of IntPtrs
260 SafeWaitHandle safe_wait_handle;
262 [Obsolete ("In the profiles > 2.x, use SafeHandle instead of Handle")]
263 public virtual IntPtr Handle {
265 return safe_wait_handle.DangerousGetHandle ();
268 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
269 [SecurityPermission (SecurityAction.InheritanceDemand, UnmanagedCode = true)]
271 if (value == InvalidHandle)
272 safe_wait_handle = new SafeWaitHandle (InvalidHandle, false);
274 safe_wait_handle = new SafeWaitHandle (value, true);
278 [MethodImplAttribute(MethodImplOptions.InternalCall)]
279 private extern bool WaitOne_internal(IntPtr handle, int ms, bool exitContext);
281 protected virtual void Dispose (bool explicitDisposing)
286 // This is only the case if the handle was never properly initialized
287 // most likely a bug in the derived class
289 if (safe_wait_handle == null)
297 if (safe_wait_handle != null)
298 safe_wait_handle.Dispose ();
303 public SafeWaitHandle SafeWaitHandle {
304 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
306 return safe_wait_handle;
309 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
312 safe_wait_handle = new SafeWaitHandle (InvalidHandle, false);
314 safe_wait_handle = value;
318 public static bool SignalAndWait (WaitHandle toSignal,
321 return SignalAndWait (toSignal, toWaitOn, -1, false);
324 public static bool SignalAndWait (WaitHandle toSignal,
326 int millisecondsTimeout,
329 if (toSignal == null)
330 throw new ArgumentNullException ("toSignal");
331 if (toWaitOn == null)
332 throw new ArgumentNullException ("toWaitOn");
334 if (millisecondsTimeout < -1)
335 throw new ArgumentOutOfRangeException ("millisecondsTimeout");
337 return SignalAndWait_Internal (toSignal.Handle, toWaitOn.Handle, millisecondsTimeout, exitContext);
340 public static bool SignalAndWait (WaitHandle toSignal,
345 double ms = timeout.TotalMilliseconds;
346 if (ms > Int32.MaxValue)
347 throw new ArgumentOutOfRangeException ("timeout");
349 return SignalAndWait (toSignal, toWaitOn, Convert.ToInt32 (ms), false);
352 [MethodImplAttribute(MethodImplOptions.InternalCall)]
353 static extern bool SignalAndWait_Internal (IntPtr toSignal, IntPtr toWaitOn, int ms, bool exitContext);
355 public virtual bool WaitOne()
358 bool release = false;
360 safe_wait_handle.DangerousAddRef (ref release);
361 return (WaitOne_internal(safe_wait_handle.DangerousGetHandle (), Timeout.Infinite, false));
364 safe_wait_handle.DangerousRelease ();
368 public virtual bool WaitOne(int millisecondsTimeout, bool exitContext)
371 // check negative - except for -1 (which is Timeout.Infinite)
372 if (millisecondsTimeout < Timeout.Infinite)
373 throw new ArgumentOutOfRangeException ("millisecondsTimeout");
375 bool release = false;
379 throw new NotSupportedException ("exitContext == true is not supported");
381 SynchronizationAttribute.ExitContext ();
384 safe_wait_handle.DangerousAddRef (ref release);
385 return (WaitOne_internal(safe_wait_handle.DangerousGetHandle (), millisecondsTimeout, exitContext));
388 SynchronizationAttribute.EnterContext ();
390 safe_wait_handle.DangerousRelease ();
394 public virtual bool WaitOne (int millisecondsTimeout)
396 return WaitOne (millisecondsTimeout, false);
399 public virtual bool WaitOne (TimeSpan timeout)
401 return WaitOne (timeout, false);
404 public virtual bool WaitOne(TimeSpan timeout, bool exitContext)
407 long ms = (long) timeout.TotalMilliseconds;
408 if (ms < -1 || ms > Int32.MaxValue)
409 throw new ArgumentOutOfRangeException ("timeout");
411 bool release = false;
415 throw new NotSupportedException ("exitContext == true is not supported");
417 SynchronizationAttribute.ExitContext ();
420 safe_wait_handle.DangerousAddRef (ref release);
421 return (WaitOne_internal(safe_wait_handle.DangerousGetHandle (), (int) ms, exitContext));
425 SynchronizationAttribute.EnterContext ();
427 safe_wait_handle.DangerousRelease ();
431 internal void CheckDisposed ()
433 if (disposed || safe_wait_handle == null)
434 throw new ObjectDisposedException (GetType ().FullName);
437 public static bool WaitAll(WaitHandle[] waitHandles, int millisecondsTimeout)
439 return WaitAll (waitHandles, millisecondsTimeout, false);
442 public static bool WaitAll(WaitHandle[] waitHandles, TimeSpan timeout)
444 return WaitAll (waitHandles, timeout, false);
447 protected static readonly IntPtr InvalidHandle = (IntPtr) (-1);
448 bool disposed = false;