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 public abstract class WaitHandle
47 : 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");
62 if (handles.Length == 0) {
63 // MS throws different exceptions from the different methods.
65 throw new ArgumentNullException ("waitHandles");
67 throw new ArgumentException ();
72 // Although we should thrown an exception if this is an STA thread,
73 // Mono does not know anything about STA threads, and just makes
74 // things like Paint.NET not even possible to work.
76 // See bug #78455 for the bug this is supposed to fix.
78 if (waitAll && length > 1 && IsSTAThread)
79 throw new NotSupportedException ("WaitAll for multiple handles is not allowed on an STA thread.");
81 foreach (WaitHandle w in handles) {
83 throw new ArgumentNullException ("waitHandles", "null handle");
85 if (w.safe_wait_handle == null)
86 throw new ArgumentException ("null element found", "waitHandle");
91 // usage of property is commented - see above
92 static bool IsSTAThread {
94 bool isSTA = Thread.CurrentThread.ApartmentState ==
97 // FIXME: remove this check after Thread.ApartmentState
98 // has been properly implemented.
100 Assembly asm = Assembly.GetEntryAssembly ();
102 isSTA = asm.EntryPoint.GetCustomAttributes (typeof (STAThreadAttribute), false).Length > 0;
109 public static bool WaitAll(WaitHandle[] waitHandles)
111 CheckArray (waitHandles, true);
112 return(WaitAll_internal(waitHandles, Timeout.Infinite, false));
115 public static bool WaitAll(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext)
117 CheckArray (waitHandles, true);
118 // check negative - except for -1 (which is Timeout.Infinite)
119 if (millisecondsTimeout < Timeout.Infinite)
120 throw new ArgumentOutOfRangeException ("millisecondsTimeout");
125 throw new NotSupportedException ("exitContext == true is not supported");
127 SynchronizationAttribute.ExitContext ();
130 return(WaitAll_internal(waitHandles, millisecondsTimeout, false));
133 if (exitContext) SynchronizationAttribute.EnterContext ();
137 public static bool WaitAll(WaitHandle[] waitHandles,
141 CheckArray (waitHandles, true);
142 long ms = (long) timeout.TotalMilliseconds;
144 if (ms < -1 || ms > Int32.MaxValue)
145 throw new ArgumentOutOfRangeException ("timeout");
150 throw new NotSupportedException ("exitContext == true is not supported");
152 SynchronizationAttribute.ExitContext ();
155 return (WaitAll_internal (waitHandles, (int) ms, exitContext));
158 if (exitContext) SynchronizationAttribute.EnterContext ();
162 [MethodImplAttribute(MethodImplOptions.InternalCall)]
163 private static extern int WaitAny_internal(WaitHandle[] handles, int ms, bool exitContext);
165 // LAMESPEC: Doesn't specify how to signal failures
166 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
167 public static int WaitAny(WaitHandle[] waitHandles)
169 CheckArray (waitHandles, false);
170 return(WaitAny_internal(waitHandles, Timeout.Infinite, false));
173 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
174 public static int WaitAny(WaitHandle[] waitHandles,
175 int millisecondsTimeout,
178 CheckArray (waitHandles, false);
179 // check negative - except for -1 (which is Timeout.Infinite)
180 if (millisecondsTimeout < Timeout.Infinite)
181 throw new ArgumentOutOfRangeException ("millisecondsTimeout");
186 throw new NotSupportedException ("exitContext == true is not supported");
188 SynchronizationAttribute.ExitContext ();
191 return(WaitAny_internal(waitHandles, millisecondsTimeout, exitContext));
194 if (exitContext) SynchronizationAttribute.EnterContext ();
198 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
199 public static int WaitAny(WaitHandle[] waitHandles, TimeSpan timeout)
201 return WaitAny (waitHandles, timeout, false);
204 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
205 public static int WaitAny(WaitHandle[] waitHandles, int millisecondsTimeout)
207 return WaitAny (waitHandles, millisecondsTimeout, false);
210 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
211 public static int WaitAny(WaitHandle[] waitHandles,
212 TimeSpan timeout, bool exitContext)
214 CheckArray (waitHandles, false);
215 long ms = (long) timeout.TotalMilliseconds;
217 if (ms < -1 || ms > Int32.MaxValue)
218 throw new ArgumentOutOfRangeException ("timeout");
223 throw new NotSupportedException ("exitContext == true is not supported");
225 SynchronizationAttribute.ExitContext ();
228 return (WaitAny_internal(waitHandles, (int) ms, exitContext));
231 if (exitContext) SynchronizationAttribute.EnterContext ();
235 protected WaitHandle()
240 public virtual void Close ()
245 #if NET_4_0 || MOBILE || MOONLIGHT
246 public void Dispose ()
248 void IDisposable.Dispose ()
254 public const int WaitTimeout = 258;
257 // In 2.0 we use SafeWaitHandles instead of IntPtrs
259 SafeWaitHandle safe_wait_handle;
261 [Obsolete ("In the profiles > 2.x, use SafeHandle instead of Handle")]
262 public virtual IntPtr Handle {
264 return safe_wait_handle.DangerousGetHandle ();
267 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
268 [SecurityPermission (SecurityAction.InheritanceDemand, UnmanagedCode = true)]
270 if (value == InvalidHandle)
271 safe_wait_handle = new SafeWaitHandle (InvalidHandle, false);
273 safe_wait_handle = new SafeWaitHandle (value, true);
277 [MethodImplAttribute(MethodImplOptions.InternalCall)]
278 private extern bool WaitOne_internal(IntPtr handle, int ms, bool exitContext);
280 protected virtual void Dispose (bool explicitDisposing)
285 // This is only the case if the handle was never properly initialized
286 // most likely a bug in the derived class
288 if (safe_wait_handle == null)
296 if (safe_wait_handle != null)
297 safe_wait_handle.Dispose ();
302 public SafeWaitHandle SafeWaitHandle {
303 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
305 return safe_wait_handle;
308 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
311 safe_wait_handle = new SafeWaitHandle (InvalidHandle, false);
313 safe_wait_handle = value;
317 public static bool SignalAndWait (WaitHandle toSignal,
320 return SignalAndWait (toSignal, toWaitOn, -1, false);
323 public static bool SignalAndWait (WaitHandle toSignal,
325 int millisecondsTimeout,
328 if (toSignal == null)
329 throw new ArgumentNullException ("toSignal");
330 if (toWaitOn == null)
331 throw new ArgumentNullException ("toWaitOn");
333 if (millisecondsTimeout < -1)
334 throw new ArgumentOutOfRangeException ("millisecondsTimeout");
336 return SignalAndWait_Internal (toSignal.Handle, toWaitOn.Handle, millisecondsTimeout, exitContext);
339 public static bool SignalAndWait (WaitHandle toSignal,
344 double ms = timeout.TotalMilliseconds;
345 if (ms > Int32.MaxValue)
346 throw new ArgumentOutOfRangeException ("timeout");
348 return SignalAndWait (toSignal, toWaitOn, Convert.ToInt32 (ms), false);
351 [MethodImplAttribute(MethodImplOptions.InternalCall)]
352 static extern bool SignalAndWait_Internal (IntPtr toSignal, IntPtr toWaitOn, int ms, bool exitContext);
354 public virtual bool WaitOne()
357 bool release = false;
359 safe_wait_handle.DangerousAddRef (ref release);
360 return (WaitOne_internal(safe_wait_handle.DangerousGetHandle (), Timeout.Infinite, false));
363 safe_wait_handle.DangerousRelease ();
367 public virtual bool WaitOne(int millisecondsTimeout, bool exitContext)
370 // check negative - except for -1 (which is Timeout.Infinite)
371 if (millisecondsTimeout < Timeout.Infinite)
372 throw new ArgumentOutOfRangeException ("millisecondsTimeout");
374 bool release = false;
378 throw new NotSupportedException ("exitContext == true is not supported");
380 SynchronizationAttribute.ExitContext ();
383 safe_wait_handle.DangerousAddRef (ref release);
384 return (WaitOne_internal(safe_wait_handle.DangerousGetHandle (), millisecondsTimeout, exitContext));
387 SynchronizationAttribute.EnterContext ();
389 safe_wait_handle.DangerousRelease ();
393 public virtual bool WaitOne (int millisecondsTimeout)
395 return WaitOne (millisecondsTimeout, false);
398 public virtual bool WaitOne (TimeSpan timeout)
400 return WaitOne (timeout, false);
403 public virtual bool WaitOne(TimeSpan timeout, bool exitContext)
406 long ms = (long) timeout.TotalMilliseconds;
407 if (ms < -1 || ms > Int32.MaxValue)
408 throw new ArgumentOutOfRangeException ("timeout");
410 bool release = false;
414 throw new NotSupportedException ("exitContext == true is not supported");
416 SynchronizationAttribute.ExitContext ();
419 safe_wait_handle.DangerousAddRef (ref release);
420 return (WaitOne_internal(safe_wait_handle.DangerousGetHandle (), (int) ms, exitContext));
424 SynchronizationAttribute.EnterContext ();
426 safe_wait_handle.DangerousRelease ();
430 internal void CheckDisposed ()
432 if (disposed || safe_wait_handle == null)
433 throw new ObjectDisposedException (GetType ().FullName);
436 public static bool WaitAll(WaitHandle[] waitHandles, int millisecondsTimeout)
438 return WaitAll (waitHandles, millisecondsTimeout, false);
441 public static bool WaitAll(WaitHandle[] waitHandles, TimeSpan timeout)
443 return WaitAll (waitHandles, timeout, false);
446 protected static readonly IntPtr InvalidHandle = (IntPtr) (-1);
447 bool disposed = false;