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
42 [StructLayout (LayoutKind.Sequential)]
43 public abstract partial class WaitHandle
44 : MarshalByRefObject, IDisposable
46 protected static readonly IntPtr InvalidHandle = (IntPtr) (-1);
48 static int WaitMultiple(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext, bool WaitAll)
50 int release_last = -1;
55 SynchronizationAttribute.ExitContext ();
58 for (int i = 0; i < waitHandles.Length; ++i) {
61 /* we have to put it in a finally block, to avoid having a ThreadAbortException
62 * between the return from DangerousAddRef and the assignement to release_last */
64 waitHandles [i].SafeWaitHandle.DangerousAddRef (ref release);
70 return WaitAll_internal (waitHandles, millisecondsTimeout);
72 return WaitAny_internal (waitHandles, millisecondsTimeout);
74 for (int i = release_last; i >= 0; --i) {
75 waitHandles [i].SafeWaitHandle.DangerousRelease ();
80 SynchronizationAttribute.EnterContext ();
85 [MethodImplAttribute(MethodImplOptions.InternalCall)]
86 private static extern int WaitAll_internal(WaitHandle[] handles, int ms);
88 [MethodImplAttribute(MethodImplOptions.InternalCall)]
89 private static extern int WaitAny_internal(WaitHandle[] handles, int ms);
91 static int WaitOneNative (SafeHandle waitableSafeHandle, uint millisecondsTimeout, bool hasThreadAffinity, bool exitContext)
97 SynchronizationAttribute.ExitContext ();
100 waitableSafeHandle.DangerousAddRef (ref release);
102 return WaitOne_internal (waitableSafeHandle.DangerousGetHandle (), (int) millisecondsTimeout);
105 waitableSafeHandle.DangerousRelease ();
107 #if !DISABLE_REMOTING
109 SynchronizationAttribute.EnterContext ();
114 [MethodImplAttribute(MethodImplOptions.InternalCall)]
115 static extern int WaitOne_internal(IntPtr handle, int ms);
117 static int SignalAndWaitOne (SafeWaitHandle waitHandleToSignal,SafeWaitHandle waitHandleToWaitOn, int millisecondsTimeout, bool hasThreadAffinity, bool exitContext)
119 bool releaseHandleToSignal = false, releaseHandleToWaitOn = false;
121 waitHandleToSignal.DangerousAddRef (ref releaseHandleToSignal);
122 waitHandleToWaitOn.DangerousAddRef (ref releaseHandleToWaitOn);
124 return SignalAndWait_Internal (waitHandleToSignal.DangerousGetHandle (), waitHandleToWaitOn.DangerousGetHandle (), millisecondsTimeout);
126 if (releaseHandleToSignal)
127 waitHandleToSignal.DangerousRelease ();
128 if (releaseHandleToWaitOn)
129 waitHandleToWaitOn.DangerousRelease ();
133 [MethodImplAttribute(MethodImplOptions.InternalCall)]
134 static extern int SignalAndWait_Internal (IntPtr toSignal, IntPtr toWaitOn, int ms);