Merge pull request #1718 from madewokherd/sgenthreadcleanup
[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                 static int WaitMultiple(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext, bool WaitAll)
49                 {
50 #if MONOTOUCH
51                         if (exitContext)
52                                 throw new NotSupportedException ("exitContext == true is not supported");
53 #endif
54
55                         int release_last = -1;
56
57                         try {
58                                 if (exitContext)
59                                         SynchronizationAttribute.ExitContext ();
60
61                                 for (int i = 0; i < waitHandles.Length; ++i) {
62                                         try {
63                                         } finally {
64                                                 /* we have to put it in a finally block, to avoid having a ThreadAbortException
65                                                  * between the return from DangerousAddRef and the assignement to release_last */
66                                                 bool release = false;
67                                                 waitHandles [i].SafeWaitHandle.DangerousAddRef (ref release);
68                                                 release_last = i;
69                                         }
70                                 }
71
72                                 if (WaitAll)
73                                         return WaitAll_internal (waitHandles, millisecondsTimeout, exitContext);
74                                 else
75                                         return WaitAny_internal (waitHandles, millisecondsTimeout, exitContext);
76                         } finally {
77                                 for (int i = release_last; i >= 0; --i) {
78                                         waitHandles [i].SafeWaitHandle.DangerousRelease ();
79                                 }
80
81                                 if (exitContext)
82                                         SynchronizationAttribute.EnterContext ();
83                         }
84                 }
85
86                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
87                 private static extern int WaitAll_internal(WaitHandle[] handles, int ms, bool exitContext);
88
89                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
90                 private static extern int WaitAny_internal(WaitHandle[] handles, int ms, bool exitContext);
91
92                 static int WaitOneNative (SafeHandle waitableSafeHandle, uint millisecondsTimeout, bool hasThreadAffinity, bool exitContext)
93                 {
94 #if MONOTOUCH
95                         if (exitContext)
96                                 throw new NotSupportedException ("exitContext == true is not supported");
97 #endif
98
99                         bool release = false;
100                         try {
101                                 if (exitContext)
102                                         SynchronizationAttribute.ExitContext ();
103
104                                 waitableSafeHandle.DangerousAddRef (ref release);
105
106                                 return WaitOne_internal (waitableSafeHandle.DangerousGetHandle (), (int) millisecondsTimeout, exitContext);
107                         } finally {
108                                 if (release)
109                                         waitableSafeHandle.DangerousRelease ();
110
111                                 if (exitContext)
112                                         SynchronizationAttribute.EnterContext ();
113                         }
114                 }
115
116                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
117                 static extern int WaitOne_internal(IntPtr handle, int ms, bool exitContext);
118
119                 static int SignalAndWaitOne (SafeWaitHandle waitHandleToSignal,SafeWaitHandle waitHandleToWaitOn, int millisecondsTimeout, bool hasThreadAffinity,  bool exitContext)
120                 {
121                         bool releaseHandleToSignal = false, releaseHandleToWaitOn = false;
122                         try {
123                                 waitHandleToSignal.DangerousAddRef (ref releaseHandleToSignal);
124                                 waitHandleToWaitOn.DangerousAddRef (ref releaseHandleToWaitOn);
125
126                                 return SignalAndWait_Internal (waitHandleToSignal.DangerousGetHandle (), waitHandleToWaitOn.DangerousGetHandle (), millisecondsTimeout, exitContext);
127                         } finally {
128                                 if (releaseHandleToSignal)
129                                         waitHandleToSignal.DangerousRelease ();
130                                 if (releaseHandleToWaitOn)
131                                         waitHandleToWaitOn.DangerousRelease ();
132                         }
133                 }
134
135                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
136                 static extern int SignalAndWait_Internal (IntPtr toSignal, IntPtr toWaitOn, int ms, bool exitContext);
137         }
138 }