[w32handle] Unify WaitHandle.Wait{One,Any,All} icalls (#5051)
[mono.git] / mcs / class / corlib / System.Threading / WaitHandle.cs
1 //
2 // System.Threading.WaitHandle.cs
3 //
4 // Author:
5 //      Dick Porter (dick@ximian.com)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com
7 //
8 // (C) 2002,2003 Ximian, Inc.   (http://www.ximian.com)
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 using System;
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;
39
40 namespace System.Threading
41 {
42         [StructLayout (LayoutKind.Sequential)]
43         public abstract partial class WaitHandle
44                 : MarshalByRefObject, IDisposable
45         {
46                 protected static readonly IntPtr InvalidHandle = (IntPtr) (-1);
47
48                 internal const int MaxWaitHandles = 64;
49
50                 static int WaitMultiple(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext, bool WaitAll)
51                 {
52                         if (waitHandles.Length > MaxWaitHandles)
53                                 return WAIT_FAILED;
54
55                         int release_last = -1;
56
57                         try {
58 #if !DISABLE_REMOTING
59                                 if (exitContext)
60                                         SynchronizationAttribute.ExitContext ();
61 #endif
62
63                                 for (int i = 0; i < waitHandles.Length; ++i) {
64                                         try {} finally {
65                                                 /* we have to put it in a finally block, to avoid having a ThreadAbortException
66                                                  * between the return from DangerousAddRef and the assignement to release_last */
67                                                 bool release = false;
68                                                 waitHandles [i].SafeWaitHandle.DangerousAddRef (ref release);
69                                                 release_last = i;
70                                         }
71                                 }
72
73                                 unsafe {
74                                         IntPtr* handles = stackalloc IntPtr[waitHandles.Length];
75
76                                         for (int i = 0; i < waitHandles.Length; ++i)
77                                                 handles[i] = waitHandles[i].SafeWaitHandle.DangerousGetHandle ();
78
79                                         return Wait_internal(handles, waitHandles.Length, WaitAll, millisecondsTimeout);
80                                 }
81                         } finally {
82                                 for (int i = release_last; i >= 0; --i) {
83                                         waitHandles [i].SafeWaitHandle.DangerousRelease ();
84                                 }
85
86 #if !DISABLE_REMOTING
87                                 if (exitContext)
88                                         SynchronizationAttribute.EnterContext ();
89 #endif
90                         }
91                 }
92
93                 static int WaitOneNative (SafeHandle waitableSafeHandle, uint millisecondsTimeout, bool hasThreadAffinity, bool exitContext)
94                 {
95                         bool release = false;
96                         try {
97 #if !DISABLE_REMOTING
98                                 if (exitContext)
99                                         SynchronizationAttribute.ExitContext ();
100 #endif
101
102                                 waitableSafeHandle.DangerousAddRef (ref release);
103
104                                 unsafe {
105                                         IntPtr handle = waitableSafeHandle.DangerousGetHandle();
106                                         return Wait_internal(&handle, 1, false, (int)millisecondsTimeout);
107                                 }
108                         } finally {
109                                 if (release)
110                                         waitableSafeHandle.DangerousRelease ();
111
112 #if !DISABLE_REMOTING
113                                 if (exitContext)
114                                         SynchronizationAttribute.EnterContext ();
115 #endif
116                         }
117                 }
118
119                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
120                 unsafe static extern int Wait_internal(IntPtr* handles, int numHandles, bool waitAll, int ms);
121
122                 static int SignalAndWaitOne (SafeWaitHandle waitHandleToSignal,SafeWaitHandle waitHandleToWaitOn, int millisecondsTimeout, bool hasThreadAffinity,  bool exitContext)
123                 {
124                         bool releaseHandleToSignal = false, releaseHandleToWaitOn = false;
125                         try {
126                                 waitHandleToSignal.DangerousAddRef (ref releaseHandleToSignal);
127                                 waitHandleToWaitOn.DangerousAddRef (ref releaseHandleToWaitOn);
128
129                                 return SignalAndWait_Internal (waitHandleToSignal.DangerousGetHandle (), waitHandleToWaitOn.DangerousGetHandle (), millisecondsTimeout);
130                         } finally {
131                                 if (releaseHandleToSignal)
132                                         waitHandleToSignal.DangerousRelease ();
133                                 if (releaseHandleToWaitOn)
134                                         waitHandleToWaitOn.DangerousRelease ();
135                         }
136                 }
137
138                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
139                 static extern int SignalAndWait_Internal (IntPtr toSignal, IntPtr toWaitOn, int ms);
140         }
141 }