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 : MarshalByRefObject, IDisposable
45 [MethodImplAttribute(MethodImplOptions.InternalCall)]
46 private static extern bool WaitAll_internal(WaitHandle[] handles, int ms, bool exitContext);
48 static void CheckArray (WaitHandle [] handles, bool waitAll)
51 throw new ArgumentNullException ("waitHandles");
53 int length = handles.Length;
55 throw new NotSupportedException ("Too many handles");
59 // Although we should thrown an exception if this is an STA thread,
60 // Mono does not know anything about STA threads, and just makes
61 // things like Paint.NET not even possible to work.
63 // See bug #78455 for the bug this is supposed to fix.
65 if (waitAll && length > 1 && IsSTAThread)
66 throw new NotSupportedException ("WaitAll for multiple handles is not allowed on an STA thread.");
68 foreach (WaitHandle w in handles) {
70 throw new ArgumentNullException ("waitHandles", "null handle");
72 if (w.safe_wait_handle == null)
73 throw new ArgumentException ("null element found", "waitHandle");
78 // usage of property is commented - see above
79 static bool IsSTAThread {
81 bool isSTA = Thread.CurrentThread.ApartmentState ==
84 // FIXME: remove this check after Thread.ApartmentState
85 // has been properly implemented.
87 Assembly asm = Assembly.GetEntryAssembly ();
89 isSTA = asm.EntryPoint.GetCustomAttributes (typeof (STAThreadAttribute), false).Length > 0;
96 public static bool WaitAll(WaitHandle[] waitHandles)
98 CheckArray (waitHandles, true);
99 return(WaitAll_internal(waitHandles, Timeout.Infinite, false));
102 public static bool WaitAll(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext)
104 CheckArray (waitHandles, true);
105 // check negative - except for -1 (which is Timeout.Infinite)
106 if (millisecondsTimeout < Timeout.Infinite)
107 throw new ArgumentOutOfRangeException ("millisecondsTimeout");
110 if (exitContext) SynchronizationAttribute.ExitContext ();
111 return(WaitAll_internal(waitHandles, millisecondsTimeout, false));
114 if (exitContext) SynchronizationAttribute.EnterContext ();
118 public static bool WaitAll(WaitHandle[] waitHandles,
122 CheckArray (waitHandles, true);
123 long ms = (long) timeout.TotalMilliseconds;
125 if (ms < -1 || ms > Int32.MaxValue)
126 throw new ArgumentOutOfRangeException ("timeout");
129 if (exitContext) SynchronizationAttribute.ExitContext ();
130 return (WaitAll_internal (waitHandles, (int) ms, exitContext));
133 if (exitContext) SynchronizationAttribute.EnterContext ();
137 [MethodImplAttribute(MethodImplOptions.InternalCall)]
138 private static extern int WaitAny_internal(WaitHandle[] handles, int ms, bool exitContext);
140 // LAMESPEC: Doesn't specify how to signal failures
141 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
142 public static int WaitAny(WaitHandle[] waitHandles)
144 CheckArray (waitHandles, false);
145 return(WaitAny_internal(waitHandles, Timeout.Infinite, false));
148 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
149 public static int WaitAny(WaitHandle[] waitHandles,
150 int millisecondsTimeout,
153 CheckArray (waitHandles, false);
154 // check negative - except for -1 (which is Timeout.Infinite)
155 if (millisecondsTimeout < Timeout.Infinite)
156 throw new ArgumentOutOfRangeException ("millisecondsTimeout");
159 if (exitContext) SynchronizationAttribute.ExitContext ();
160 return(WaitAny_internal(waitHandles, millisecondsTimeout, exitContext));
163 if (exitContext) SynchronizationAttribute.EnterContext ();
167 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
168 public static int WaitAny(WaitHandle[] waitHandles, TimeSpan timeout)
170 return WaitAny (waitHandles, timeout, false);
173 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
174 public static int WaitAny(WaitHandle[] waitHandles, int millisecondsTimeout)
176 return WaitAny (waitHandles, millisecondsTimeout, false);
179 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
180 public static int WaitAny(WaitHandle[] waitHandles,
181 TimeSpan timeout, bool exitContext)
183 CheckArray (waitHandles, false);
184 long ms = (long) timeout.TotalMilliseconds;
186 if (ms < -1 || ms > Int32.MaxValue)
187 throw new ArgumentOutOfRangeException ("timeout");
190 if (exitContext) SynchronizationAttribute.ExitContext ();
191 return (WaitAny_internal(waitHandles, (int) ms, exitContext));
194 if (exitContext) SynchronizationAttribute.EnterContext ();
198 protected WaitHandle()
203 public virtual void Close() {
205 GC.SuppressFinalize (this);
208 public const int WaitTimeout = 258;
211 // In 2.0 we use SafeWaitHandles instead of IntPtrs
213 SafeWaitHandle safe_wait_handle;
215 [Obsolete ("In the profiles > 2.x, use SafeHandle instead of Handle")]
216 public virtual IntPtr Handle {
218 return safe_wait_handle.DangerousGetHandle ();
221 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
222 [SecurityPermission (SecurityAction.InheritanceDemand, UnmanagedCode = true)]
224 if (value == InvalidHandle)
225 safe_wait_handle = new SafeWaitHandle (InvalidHandle, false);
227 safe_wait_handle = new SafeWaitHandle (value, true);
231 [MethodImplAttribute(MethodImplOptions.InternalCall)]
232 private extern bool WaitOne_internal(IntPtr handle, int ms, bool exitContext);
234 protected virtual void Dispose (bool explicitDisposing)
240 // This is only the case if the handle was never properly initialized
241 // most likely a bug in the derived class
243 if (safe_wait_handle == null)
247 if (safe_wait_handle != null)
248 safe_wait_handle.Dispose ();
253 public SafeWaitHandle SafeWaitHandle {
254 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
256 return safe_wait_handle;
259 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
262 safe_wait_handle = new SafeWaitHandle (InvalidHandle, false);
264 safe_wait_handle = value;
268 public static bool SignalAndWait (WaitHandle toSignal,
271 return SignalAndWait (toSignal, toWaitOn, -1, false);
274 public static bool SignalAndWait (WaitHandle toSignal,
276 int millisecondsTimeout,
279 if (toSignal == null)
280 throw new ArgumentNullException ("toSignal");
281 if (toWaitOn == null)
282 throw new ArgumentNullException ("toWaitOn");
284 if (millisecondsTimeout < -1)
285 throw new ArgumentOutOfRangeException ("millisecondsTimeout");
287 return SignalAndWait_Internal (toSignal.Handle, toWaitOn.Handle, millisecondsTimeout, exitContext);
290 public static bool SignalAndWait (WaitHandle toSignal,
295 double ms = timeout.TotalMilliseconds;
296 if (ms > Int32.MaxValue)
297 throw new ArgumentOutOfRangeException ("timeout");
299 return SignalAndWait (toSignal, toWaitOn, Convert.ToInt32 (ms), false);
302 [MethodImplAttribute(MethodImplOptions.InternalCall)]
303 static extern bool SignalAndWait_Internal (IntPtr toSignal, IntPtr toWaitOn, int ms, bool exitContext);
305 public virtual bool WaitOne()
308 bool release = false;
310 safe_wait_handle.DangerousAddRef (ref release);
311 return (WaitOne_internal(safe_wait_handle.DangerousGetHandle (), Timeout.Infinite, false));
314 safe_wait_handle.DangerousRelease ();
318 public virtual bool WaitOne(int millisecondsTimeout, bool exitContext)
321 // check negative - except for -1 (which is Timeout.Infinite)
322 if (millisecondsTimeout < Timeout.Infinite)
323 throw new ArgumentOutOfRangeException ("millisecondsTimeout");
325 bool release = false;
328 SynchronizationAttribute.ExitContext ();
329 safe_wait_handle.DangerousAddRef (ref release);
330 return (WaitOne_internal(safe_wait_handle.DangerousGetHandle (), millisecondsTimeout, exitContext));
333 SynchronizationAttribute.EnterContext ();
335 safe_wait_handle.DangerousRelease ();
339 public virtual bool WaitOne (int millisecondsTimeout)
341 return WaitOne (millisecondsTimeout, false);
344 public virtual bool WaitOne (TimeSpan timeout)
346 return WaitOne (timeout, false);
349 public virtual bool WaitOne(TimeSpan timeout, bool exitContext)
352 long ms = (long) timeout.TotalMilliseconds;
353 if (ms < -1 || ms > Int32.MaxValue)
354 throw new ArgumentOutOfRangeException ("timeout");
356 bool release = false;
359 SynchronizationAttribute.ExitContext ();
360 safe_wait_handle.DangerousAddRef (ref release);
361 return (WaitOne_internal(safe_wait_handle.DangerousGetHandle (), (int) ms, exitContext));
365 SynchronizationAttribute.EnterContext ();
367 safe_wait_handle.DangerousRelease ();
371 internal void CheckDisposed ()
373 if (disposed || safe_wait_handle == null)
374 throw new ObjectDisposedException (GetType ().FullName);
377 public static bool WaitAll(WaitHandle[] waitHandles, int millisecondsTimeout)
379 return WaitAll (waitHandles, millisecondsTimeout, false);
382 public static bool WaitAll(WaitHandle[] waitHandles, TimeSpan timeout)
384 return WaitAll (waitHandles, timeout, false);
387 protected static readonly IntPtr InvalidHandle = (IntPtr) (-1);
388 bool disposed = false;
390 void IDisposable.Dispose() {
392 // Take yourself off the Finalization queue
393 GC.SuppressFinalize(this);