Merge pull request #1458 from BrzVlad/feature-fat-cas
[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         [ComVisible (true)]
43         [StructLayout (LayoutKind.Sequential)]
44         public abstract class WaitHandle
45                 : MarshalByRefObject, IDisposable
46         {
47                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
48                 private static extern bool WaitAll_internal(WaitHandle[] handles, int ms, bool exitContext);
49                 
50                 static void CheckArray (WaitHandle [] handles, bool waitAll)
51                 {
52                         if (handles == null)
53                                 throw new ArgumentNullException ("waitHandles");
54
55                         int length = handles.Length;
56                         if (length > 64)
57                                 throw new NotSupportedException ("Too many handles");
58
59                         if (handles.Length == 0) {
60                                 // MS throws different exceptions from the different methods.
61                                 if (waitAll)
62                                         throw new ArgumentNullException ("waitHandles");
63                                 else
64                                         throw new ArgumentException ();
65                         }
66
67 #if false
68                         //
69                         // Although we should thrown an exception if this is an STA thread,
70                         // Mono does not know anything about STA threads, and just makes
71                         // things like Paint.NET not even possible to work.
72                         //
73                         // See bug #78455 for the bug this is supposed to fix. 
74                         // 
75                         if (waitAll && length > 1 && IsSTAThread)
76                                 throw new NotSupportedException ("WaitAll for multiple handles is not allowed on an STA thread.");
77 #endif
78                         foreach (WaitHandle w in handles) {
79                                 if (w == null)
80                                         throw new ArgumentNullException ("waitHandles", "null handle");
81
82                                 if (w.safe_wait_handle == null)
83                                         throw new ArgumentException ("null element found", "waitHandle");
84
85                         }
86                 }
87 #if false
88                 // usage of property is commented - see above
89                 static bool IsSTAThread {
90                         get {
91                                 bool isSTA = Thread.CurrentThread.ApartmentState ==
92                                         ApartmentState.STA;
93
94                                 // FIXME: remove this check after Thread.ApartmentState
95                                 // has been properly implemented.
96                                 if (!isSTA) {
97                                         Assembly asm = Assembly.GetEntryAssembly ();
98                                         if (asm != null)
99                                                 isSTA = asm.EntryPoint.GetCustomAttributes (typeof (STAThreadAttribute), false).Length > 0;
100                                 }
101
102                                 return isSTA;
103                         }
104                 }
105 #endif
106                 public static bool WaitAll(WaitHandle[] waitHandles)
107                 {
108                         CheckArray (waitHandles, true);
109                         return(WaitAll_internal(waitHandles, Timeout.Infinite, false));
110                 }
111
112                 public static bool WaitAll(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext)
113                 {
114                         CheckArray (waitHandles, true);
115                         // check negative - except for -1 (which is Timeout.Infinite)
116                         if (millisecondsTimeout < Timeout.Infinite)
117                                 throw new ArgumentOutOfRangeException ("millisecondsTimeout");
118
119                         try {
120                                 if (exitContext) {
121 #if MONOTOUCH
122                                         throw new NotSupportedException ("exitContext == true is not supported");
123 #else
124                                         SynchronizationAttribute.ExitContext ();
125 #endif
126                                 }
127                                 return(WaitAll_internal(waitHandles, millisecondsTimeout, false));
128                         }
129                         finally {
130                                 if (exitContext) SynchronizationAttribute.EnterContext ();
131                         }
132                 }
133
134                 public static bool WaitAll(WaitHandle[] waitHandles,
135                                            TimeSpan timeout,
136                                            bool exitContext)
137                 {
138                         CheckArray (waitHandles, true);
139                         long ms = (long) timeout.TotalMilliseconds;
140                         
141                         if (ms < -1 || ms > Int32.MaxValue)
142                                 throw new ArgumentOutOfRangeException ("timeout");
143
144                         try {
145                                 if (exitContext) {
146 #if MONOTOUCH
147                                         throw new NotSupportedException ("exitContext == true is not supported");
148 #else
149                                         SynchronizationAttribute.ExitContext ();
150 #endif
151                                 }
152                                 return (WaitAll_internal (waitHandles, (int) ms, exitContext));
153                         }
154                         finally {
155                                 if (exitContext) SynchronizationAttribute.EnterContext ();
156                         }
157                 }
158
159                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
160                 private static extern int WaitAny_internal(WaitHandle[] handles, int ms, bool exitContext);
161
162                 // LAMESPEC: Doesn't specify how to signal failures
163                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
164                 public static int WaitAny(WaitHandle[] waitHandles)
165                 {
166                         CheckArray (waitHandles, false);
167                         return(WaitAny_internal(waitHandles, Timeout.Infinite, false));
168                 }
169
170                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
171                 public static int WaitAny(WaitHandle[] waitHandles,
172                                           int millisecondsTimeout,
173                                           bool exitContext)
174                 {
175                         CheckArray (waitHandles, false);
176                         // check negative - except for -1 (which is Timeout.Infinite)
177                         if (millisecondsTimeout < Timeout.Infinite)
178                                 throw new ArgumentOutOfRangeException ("millisecondsTimeout");
179
180                         try {
181                                 if (exitContext) {
182 #if MONOTOUCH
183                                         throw new NotSupportedException ("exitContext == true is not supported");
184 #else
185                                         SynchronizationAttribute.ExitContext ();
186 #endif
187                                 }
188                                 return(WaitAny_internal(waitHandles, millisecondsTimeout, exitContext));
189                         }
190                         finally {
191                                 if (exitContext) SynchronizationAttribute.EnterContext ();
192                         }
193                 }
194
195                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
196                 public static int WaitAny(WaitHandle[] waitHandles, TimeSpan timeout)
197                 {
198                         return WaitAny (waitHandles, timeout, false);
199                 }
200
201                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
202                 public static int WaitAny(WaitHandle[] waitHandles, int millisecondsTimeout)
203                 {
204                         return WaitAny (waitHandles, millisecondsTimeout, false);
205                 }
206
207                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
208                 public static int WaitAny(WaitHandle[] waitHandles,
209                                           TimeSpan timeout, bool exitContext)
210                 {
211                         CheckArray (waitHandles, false);
212                         long ms = (long) timeout.TotalMilliseconds;
213                         
214                         if (ms < -1 || ms > Int32.MaxValue)
215                                 throw new ArgumentOutOfRangeException ("timeout");
216
217                         try {
218                                 if (exitContext) {
219 #if MONOTOUCH
220                                         throw new NotSupportedException ("exitContext == true is not supported");
221 #else
222                                         SynchronizationAttribute.ExitContext ();
223 #endif
224                                 }
225                                 return (WaitAny_internal(waitHandles, (int) ms, exitContext));
226                         }
227                         finally {
228                                 if (exitContext) SynchronizationAttribute.EnterContext ();
229                         }
230                 }
231
232                 protected WaitHandle()
233                 {
234                         // FIXME
235                 }
236
237                 public virtual void Close ()
238                 {
239                         Dispose(true);
240                 }
241
242                 public void Dispose ()
243                 {
244                         Close ();
245                 }
246
247                 public const int WaitTimeout = 258;
248
249                 //
250                 // In 2.0 we use SafeWaitHandles instead of IntPtrs
251                 //
252                 SafeWaitHandle safe_wait_handle;
253
254                 [Obsolete ("In the profiles > 2.x, use SafeHandle instead of Handle")]
255                 public virtual IntPtr Handle {
256                         get {
257                                 return safe_wait_handle.DangerousGetHandle ();
258                         }
259
260                         [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
261                         [SecurityPermission (SecurityAction.InheritanceDemand, UnmanagedCode = true)]
262                         set {
263                                 if (value == InvalidHandle)
264                                         safe_wait_handle = new SafeWaitHandle (InvalidHandle, false);
265                                 else
266                                         safe_wait_handle = new SafeWaitHandle (value, true);
267                         }
268                 }
269                 
270                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
271                 private extern bool WaitOne_internal(IntPtr handle, int ms, bool exitContext);
272
273                 protected virtual void Dispose (bool explicitDisposing)
274                 {
275                         if (!disposed){
276
277                                 //
278                                 // This is only the case if the handle was never properly initialized
279                                 // most likely a bug in the derived class
280                                 //
281                                 if (safe_wait_handle == null)
282                                         return;
283
284                                 lock (this){
285                                         if (disposed)
286                                                 return;
287
288                                         disposed = true;
289                                         if (safe_wait_handle != null)
290                                                 safe_wait_handle.Dispose ();
291                                 }
292                         }
293                 }
294
295                 public SafeWaitHandle SafeWaitHandle {
296                         [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
297                         get {
298                                 return safe_wait_handle;
299                         }
300
301                         [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
302                         set {
303                                 if (value == null)
304                                         safe_wait_handle = new SafeWaitHandle (InvalidHandle, false);
305                                 else
306                                         safe_wait_handle = value;
307                         }
308                 }
309
310                 public static bool SignalAndWait (WaitHandle toSignal,
311                                                   WaitHandle toWaitOn)
312                 {
313                         return SignalAndWait (toSignal, toWaitOn, -1, false);
314                 }
315                 
316                 public static bool SignalAndWait (WaitHandle toSignal,
317                                                   WaitHandle toWaitOn,
318                                                   int millisecondsTimeout,
319                                                   bool exitContext)
320                 {
321                         if (toSignal == null)
322                                 throw new ArgumentNullException ("toSignal");
323                         if (toWaitOn == null)
324                                 throw new ArgumentNullException ("toWaitOn");
325
326                         if (millisecondsTimeout < -1)
327                                 throw new ArgumentOutOfRangeException ("millisecondsTimeout");
328
329                         return SignalAndWait_Internal (toSignal.Handle, toWaitOn.Handle, millisecondsTimeout, exitContext);
330                 }
331                 
332                 public static bool SignalAndWait (WaitHandle toSignal,
333                                                   WaitHandle toWaitOn,
334                                                   TimeSpan timeout,
335                                                   bool exitContext)
336                 {
337                         double ms = timeout.TotalMilliseconds;
338                         if (ms > Int32.MaxValue)
339                                 throw new ArgumentOutOfRangeException ("timeout");
340
341                         return SignalAndWait (toSignal, toWaitOn, Convert.ToInt32 (ms), false);
342                 }
343
344                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
345                 static extern bool SignalAndWait_Internal (IntPtr toSignal, IntPtr toWaitOn, int ms, bool exitContext);
346
347                 public virtual bool WaitOne()
348                 {
349                         CheckDisposed ();
350                         bool release = false;
351                         try {
352                                 safe_wait_handle.DangerousAddRef (ref release);
353                                 return (WaitOne_internal(safe_wait_handle.DangerousGetHandle (), Timeout.Infinite, false));
354                         } finally {
355                                 if (release)
356                                         safe_wait_handle.DangerousRelease ();
357                         }
358                 }
359
360                 public virtual bool WaitOne(int millisecondsTimeout, bool exitContext)
361                 {
362                         CheckDisposed ();
363                         // check negative - except for -1 (which is Timeout.Infinite)
364                         if (millisecondsTimeout < Timeout.Infinite)
365                                 throw new ArgumentOutOfRangeException ("millisecondsTimeout");
366
367                         bool release = false;
368                         try {
369                                 if (exitContext) {
370 #if !MONOTOUCH
371                                         SynchronizationAttribute.ExitContext ();
372 #endif
373                                 }
374                                 safe_wait_handle.DangerousAddRef (ref release);
375                                 return (WaitOne_internal(safe_wait_handle.DangerousGetHandle (), millisecondsTimeout, exitContext));
376                         } finally {
377 #if !MONOTOUCH
378                                 if (exitContext)
379                                         SynchronizationAttribute.EnterContext ();
380 #endif
381                                 if (release)
382                                         safe_wait_handle.DangerousRelease ();
383                         }
384                 }
385
386                 public virtual bool WaitOne (int millisecondsTimeout)
387                 {
388                         return WaitOne (millisecondsTimeout, false);
389                 }
390
391                 public virtual bool WaitOne (TimeSpan timeout)
392                 {
393                         return WaitOne (timeout, false);
394                 }
395
396                 public virtual bool WaitOne(TimeSpan timeout, bool exitContext)
397                 {
398                         CheckDisposed ();
399                         long ms = (long) timeout.TotalMilliseconds;
400                         if (ms < -1 || ms > Int32.MaxValue)
401                                 throw new ArgumentOutOfRangeException ("timeout");
402
403                         bool release = false;
404                         try {
405                                 if (exitContext) {
406 #if !MONOTOUCH
407                                         SynchronizationAttribute.ExitContext ();
408 #endif
409                                 }
410                                 safe_wait_handle.DangerousAddRef (ref release);
411                                 return (WaitOne_internal(safe_wait_handle.DangerousGetHandle (), (int) ms, exitContext));
412                         }
413                         finally {
414 #if !MONOTOUCH
415                                 if (exitContext)
416                                         SynchronizationAttribute.EnterContext ();
417 #endif
418                                 if (release)
419                                         safe_wait_handle.DangerousRelease ();
420                         }
421                 }
422
423                 internal void CheckDisposed ()
424                 {
425                         if (disposed || safe_wait_handle == null)
426                                 throw new ObjectDisposedException (GetType ().FullName);
427                 }
428
429                 public static bool WaitAll(WaitHandle[] waitHandles, int millisecondsTimeout)
430                 {
431                         return WaitAll (waitHandles, millisecondsTimeout, false);
432                 }
433
434                 public static bool WaitAll(WaitHandle[] waitHandles, TimeSpan timeout)
435                 {
436                         return WaitAll (waitHandles, timeout, false);
437                 }
438                 
439                 protected static readonly IntPtr InvalidHandle = (IntPtr) (-1);
440                 bool disposed = false;
441         }
442 }