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;
38 using System.Runtime.InteropServices;
39 using Microsoft.Win32.SafeHandles;
40 using System.Runtime.ConstrainedExecution;
43 namespace System.Threading
48 public abstract class WaitHandle : MarshalByRefObject, IDisposable
50 [MethodImplAttribute(MethodImplOptions.InternalCall)]
51 private static extern bool WaitAll_internal(WaitHandle[] handles, int ms, bool exitContext);
53 static void CheckArray (WaitHandle [] handles, bool waitAll)
56 throw new ArgumentNullException ("waitHandles");
58 int length = handles.Length;
60 throw new NotSupportedException ("Too many handles");
64 // Although we should thrown an exception if this is an STA thread,
65 // Mono does not know anything about STA threads, and just makes
66 // things like Paint.NET not even possible to work.
68 // See bug #78455 for the bug this is supposed to fix.
70 if (waitAll && length > 1 && IsSTAThread)
71 throw new NotSupportedException ("WaitAll for multiple handles is not allowed on an STA thread.");
73 foreach (WaitHandle w in handles) {
75 throw new ArgumentNullException ("waitHandles", "null handle");
78 if (w.safe_wait_handle == null)
79 throw new ArgumentException ("null element found", "waitHandle");
81 if (w.os_handle == InvalidHandle)
82 throw new ArgumentException ("null element found", "waitHandle");
87 // usage of property is commented - see above
88 static bool IsSTAThread {
90 bool isSTA = Thread.CurrentThread.ApartmentState ==
93 // FIXME: remove this check after Thread.ApartmentState
94 // has been properly implemented.
96 Assembly asm = Assembly.GetEntryAssembly ();
98 isSTA = asm.EntryPoint.GetCustomAttributes (typeof (STAThreadAttribute), false).Length > 0;
105 public static bool WaitAll(WaitHandle[] waitHandles)
107 CheckArray (waitHandles, true);
108 return(WaitAll_internal(waitHandles, Timeout.Infinite, false));
111 public static bool WaitAll(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext)
113 CheckArray (waitHandles, true);
115 if (exitContext) SynchronizationAttribute.ExitContext ();
116 return(WaitAll_internal(waitHandles, millisecondsTimeout, false));
119 if (exitContext) SynchronizationAttribute.EnterContext ();
123 public static bool WaitAll(WaitHandle[] waitHandles,
127 CheckArray (waitHandles, true);
128 long ms = (long) timeout.TotalMilliseconds;
130 if (ms < -1 || ms > Int32.MaxValue)
131 throw new ArgumentOutOfRangeException ("timeout");
134 if (exitContext) SynchronizationAttribute.ExitContext ();
135 return (WaitAll_internal (waitHandles, (int) ms, exitContext));
138 if (exitContext) SynchronizationAttribute.EnterContext ();
142 [MethodImplAttribute(MethodImplOptions.InternalCall)]
143 private static extern int WaitAny_internal(WaitHandle[] handles, int ms, bool exitContext);
145 // LAMESPEC: Doesn't specify how to signal failures
147 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
149 public static int WaitAny(WaitHandle[] waitHandles)
151 CheckArray (waitHandles, false);
152 return(WaitAny_internal(waitHandles, Timeout.Infinite, false));
156 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
158 public static int WaitAny(WaitHandle[] waitHandles,
159 int millisecondsTimeout,
162 CheckArray (waitHandles, false);
164 if (exitContext) SynchronizationAttribute.ExitContext ();
165 return(WaitAny_internal(waitHandles, millisecondsTimeout, exitContext));
168 if (exitContext) SynchronizationAttribute.EnterContext ();
173 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
174 public static int WaitAny(WaitHandle[] waitHandles, TimeSpan timeout)
176 return WaitAny (waitHandles, timeout, false);
179 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
180 public static int WaitAny(WaitHandle[] waitHandles, int millisecondsTimeout)
182 return WaitAny (waitHandles, millisecondsTimeout, false);
186 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
188 public static int WaitAny(WaitHandle[] waitHandles,
189 TimeSpan timeout, bool exitContext)
191 CheckArray (waitHandles, false);
192 long ms = (long) timeout.TotalMilliseconds;
194 if (ms < -1 || ms > Int32.MaxValue)
195 throw new ArgumentOutOfRangeException ("timeout");
198 if (exitContext) SynchronizationAttribute.ExitContext ();
199 return (WaitAny_internal(waitHandles, (int) ms, exitContext));
202 if (exitContext) SynchronizationAttribute.EnterContext ();
215 public virtual void Close() {
217 GC.SuppressFinalize (this);
220 public const int WaitTimeout = 258;
224 // In 2.0 we use SafeWaitHandles instead of IntPtrs
226 SafeWaitHandle safe_wait_handle;
228 [Obsolete ("In the profiles > 2.x, use SafeHandle instead of Handle")]
229 public virtual IntPtr Handle {
231 return safe_wait_handle.DangerousGetHandle ();
234 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
235 [SecurityPermission (SecurityAction.InheritanceDemand, UnmanagedCode = true)]
237 if (value == InvalidHandle)
238 safe_wait_handle = new SafeWaitHandle (InvalidHandle, false);
240 safe_wait_handle = new SafeWaitHandle (value, true);
244 [MethodImplAttribute(MethodImplOptions.InternalCall)]
245 private extern bool WaitOne_internal(IntPtr handle, int ms, bool exitContext);
247 protected virtual void Dispose (bool explicitDisposing)
253 // This is only the case if the handle was never properly initialized
254 // most likely a bug in the derived class
256 if (safe_wait_handle == null)
260 if (safe_wait_handle != null)
261 safe_wait_handle.Dispose ();
266 public SafeWaitHandle SafeWaitHandle {
267 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
269 return safe_wait_handle;
272 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
275 safe_wait_handle = new SafeWaitHandle (InvalidHandle, false);
277 safe_wait_handle = value;
281 public static bool SignalAndWait (WaitHandle toSignal,
284 return SignalAndWait (toSignal, toWaitOn, -1, false);
287 public static bool SignalAndWait (WaitHandle toSignal,
289 int millisecondsTimeout,
292 if (toSignal == null)
293 throw new ArgumentNullException ("toSignal");
294 if (toWaitOn == null)
295 throw new ArgumentNullException ("toWaitOn");
297 if (millisecondsTimeout < -1)
298 throw new ArgumentOutOfRangeException ("millisecondsTimeout");
300 return SignalAndWait_Internal (toSignal.Handle, toWaitOn.Handle, millisecondsTimeout, exitContext);
303 public static bool SignalAndWait (WaitHandle toSignal,
308 double ms = timeout.TotalMilliseconds;
309 if (ms > Int32.MaxValue)
310 throw new ArgumentOutOfRangeException ("timeout");
312 return SignalAndWait (toSignal, toWaitOn, Convert.ToInt32 (ms), false);
315 [MethodImplAttribute(MethodImplOptions.InternalCall)]
316 static extern bool SignalAndWait_Internal (IntPtr toSignal, IntPtr toWaitOn, int ms, bool exitContext);
318 public virtual bool WaitOne()
321 bool release = false;
323 safe_wait_handle.DangerousAddRef (ref release);
324 return (WaitOne_internal(safe_wait_handle.DangerousGetHandle (), Timeout.Infinite, false));
327 safe_wait_handle.DangerousRelease ();
331 public virtual bool WaitOne(int millisecondsTimeout, bool exitContext)
334 bool release = false;
337 SynchronizationAttribute.ExitContext ();
338 safe_wait_handle.DangerousAddRef (ref release);
339 return (WaitOne_internal(safe_wait_handle.DangerousGetHandle (), millisecondsTimeout, exitContext));
342 SynchronizationAttribute.EnterContext ();
344 safe_wait_handle.DangerousRelease ();
348 public virtual bool WaitOne (int millisecondsTimeout)
350 return WaitOne (millisecondsTimeout, false);
353 public virtual bool WaitOne (TimeSpan timeout)
355 return WaitOne (timeout, false);
358 public virtual bool WaitOne(TimeSpan timeout, bool exitContext)
361 long ms = (long) timeout.TotalMilliseconds;
362 if (ms < -1 || ms > Int32.MaxValue)
363 throw new ArgumentOutOfRangeException ("timeout");
365 bool release = false;
368 SynchronizationAttribute.ExitContext ();
369 safe_wait_handle.DangerousAddRef (ref release);
370 return (WaitOne_internal(safe_wait_handle.DangerousGetHandle (), (int) ms, exitContext));
374 SynchronizationAttribute.EnterContext ();
376 safe_wait_handle.DangerousRelease ();
380 internal void CheckDisposed ()
382 if (disposed || safe_wait_handle == null)
383 throw new ObjectDisposedException (GetType ().FullName);
386 public static bool WaitAll(WaitHandle[] waitHandles, int millisecondsTimeout)
388 CheckArray (waitHandles, true);
389 return WaitAll_internal (waitHandles, millisecondsTimeout, false);
392 public static bool WaitAll(WaitHandle[] waitHandles, TimeSpan timeout)
394 CheckArray (waitHandles, true);
395 long ms = (long) timeout.TotalMilliseconds;
397 if (ms < -1 || ms > Int32.MaxValue)
398 throw new ArgumentOutOfRangeException ("timeout");
400 return (WaitAll_internal (waitHandles, (int) ms, false));
404 private IntPtr os_handle = InvalidHandle;
406 public virtual IntPtr Handle {
411 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
412 [SecurityPermission (SecurityAction.InheritanceDemand, UnmanagedCode = true)]
418 internal void CheckDisposed ()
420 if (disposed || os_handle == InvalidHandle)
421 throw new ObjectDisposedException (GetType ().FullName);
424 [MethodImplAttribute(MethodImplOptions.InternalCall)]
425 private extern bool WaitOne_internal(IntPtr handle, int ms, bool exitContext);
427 protected virtual void Dispose(bool explicitDisposing) {
428 // Check to see if Dispose has already been called.
431 if (os_handle == InvalidHandle)
435 if (os_handle != InvalidHandle) {
436 NativeEventCalls.CloseEvent_internal (os_handle);
437 os_handle = InvalidHandle;
443 public virtual bool WaitOne()
446 return(WaitOne_internal(os_handle, Timeout.Infinite, false));
449 public virtual bool WaitOne(int millisecondsTimeout, bool exitContext)
453 if (exitContext) SynchronizationAttribute.ExitContext ();
454 return(WaitOne_internal(os_handle, millisecondsTimeout, exitContext));
457 if (exitContext) SynchronizationAttribute.EnterContext ();
461 public virtual bool WaitOne(TimeSpan timeout, bool exitContext)
464 long ms = (long) timeout.TotalMilliseconds;
465 if (ms < -1 || ms > Int32.MaxValue)
466 throw new ArgumentOutOfRangeException ("timeout");
469 if (exitContext) SynchronizationAttribute.ExitContext ();
470 return (WaitOne_internal(os_handle, (int) ms, exitContext));
473 if (exitContext) SynchronizationAttribute.EnterContext ();
478 protected static readonly IntPtr InvalidHandle = (IntPtr) (-1);
479 bool disposed = false;
481 void IDisposable.Dispose() {
483 // Take yourself off the Finalization queue
484 GC.SuppressFinalize(this);