(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / corlib / System.Threading / WaitHandle.cs
blob85e1b9b4f70cde47e813dff924838a7c3f6f0b8d
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 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Runtime.CompilerServices;
35 using System.Runtime.Remoting.Contexts;
37 namespace System.Threading
39 public abstract class WaitHandle : MarshalByRefObject, IDisposable
41 [MethodImplAttribute(MethodImplOptions.InternalCall)]
42 private static extern bool WaitAll_internal(WaitHandle[] handles, int ms, bool exitContext);
44 static void CheckArray (WaitHandle [] handles)
46 if (handles == null)
47 throw new ArgumentNullException ("waitHandles");
49 int length = handles.Length;
50 if (length > 64)
51 throw new NotSupportedException ("Too many handles");
53 foreach (WaitHandle w in handles) {
54 if (w == null)
55 throw new ArgumentNullException ("waitHandles", "null handle");
57 if (w.os_handle == InvalidHandle)
58 throw new ArgumentException ("null element found", "waitHandle");
62 public static bool WaitAll(WaitHandle[] waitHandles)
64 CheckArray (waitHandles);
65 return(WaitAll_internal(waitHandles, Timeout.Infinite, false));
68 public static bool WaitAll(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext)
70 CheckArray (waitHandles);
71 try {
72 if (exitContext) SynchronizationAttribute.ExitContext ();
73 return(WaitAll_internal(waitHandles, millisecondsTimeout, false));
75 finally {
76 if (exitContext) SynchronizationAttribute.EnterContext ();
80 public static bool WaitAll(WaitHandle[] waitHandles,
81 TimeSpan timeout,
82 bool exitContext)
84 CheckArray (waitHandles);
85 long ms = (long) timeout.TotalMilliseconds;
87 if (ms < -1 || ms > Int32.MaxValue)
88 throw new ArgumentOutOfRangeException ("timeout");
90 try {
91 if (exitContext) SynchronizationAttribute.ExitContext ();
92 return (WaitAll_internal (waitHandles, (int) ms, exitContext));
94 finally {
95 if (exitContext) SynchronizationAttribute.EnterContext ();
99 [MethodImplAttribute(MethodImplOptions.InternalCall)]
100 private static extern int WaitAny_internal(WaitHandle[] handles, int ms, bool exitContext);
102 // LAMESPEC: Doesn't specify how to signal failures
103 public static int WaitAny(WaitHandle[] waitHandles)
105 CheckArray (waitHandles);
106 return(WaitAny_internal(waitHandles, Timeout.Infinite, false));
109 public static int WaitAny(WaitHandle[] waitHandles,
110 int millisecondsTimeout,
111 bool exitContext)
113 CheckArray (waitHandles);
114 try {
115 if (exitContext) SynchronizationAttribute.ExitContext ();
116 return(WaitAny_internal(waitHandles, millisecondsTimeout, exitContext));
118 finally {
119 if (exitContext) SynchronizationAttribute.EnterContext ();
123 public static int WaitAny(WaitHandle[] waitHandles,
124 TimeSpan timeout, bool exitContext)
126 CheckArray (waitHandles);
127 long ms = (long) timeout.TotalMilliseconds;
129 if (ms < -1 || ms > Int32.MaxValue)
130 throw new ArgumentOutOfRangeException ("timeout");
132 try {
133 if (exitContext) SynchronizationAttribute.ExitContext ();
134 return (WaitAny_internal(waitHandles, (int) ms, exitContext));
136 finally {
137 if (exitContext) SynchronizationAttribute.EnterContext ();
141 [MonoTODO]
142 public WaitHandle() {
143 // FIXME
146 public const int WaitTimeout = 258;
148 private IntPtr os_handle = InvalidHandle;
150 public virtual IntPtr Handle {
151 get {
152 return(os_handle);
155 set {
156 os_handle=value;
160 public virtual void Close() {
161 Dispose(true);
162 GC.SuppressFinalize (this);
165 internal void CheckDisposed ()
167 if (disposed || os_handle == InvalidHandle)
168 throw new ObjectDisposedException (GetType ().FullName);
171 [MethodImplAttribute(MethodImplOptions.InternalCall)]
172 private extern bool WaitOne_internal(IntPtr handle, int ms, bool exitContext);
174 public virtual bool WaitOne()
176 CheckDisposed ();
177 return(WaitOne_internal(os_handle, Timeout.Infinite, false));
180 public virtual bool WaitOne(int millisecondsTimeout, bool exitContext)
182 CheckDisposed ();
183 try {
184 if (exitContext) SynchronizationAttribute.ExitContext ();
185 return(WaitOne_internal(os_handle, millisecondsTimeout, exitContext));
187 finally {
188 if (exitContext) SynchronizationAttribute.EnterContext ();
192 public virtual bool WaitOne(TimeSpan timeout, bool exitContext)
194 CheckDisposed ();
195 long ms = (long) timeout.TotalMilliseconds;
196 if (ms < -1 || ms > Int32.MaxValue)
197 throw new ArgumentOutOfRangeException ("timeout");
199 try {
200 if (exitContext) SynchronizationAttribute.ExitContext ();
201 return (WaitOne_internal(os_handle, (int) ms, exitContext));
203 finally {
204 if (exitContext) SynchronizationAttribute.EnterContext ();
208 protected static readonly IntPtr InvalidHandle = IntPtr.Zero;
210 private bool disposed = false;
212 void IDisposable.Dispose() {
213 Dispose(true);
214 // Take yourself off the Finalization queue
215 GC.SuppressFinalize(this);
218 protected virtual void Dispose(bool explicitDisposing) {
219 // Check to see if Dispose has already been called.
220 if (!disposed) {
221 disposed=true;
222 if (os_handle == InvalidHandle)
223 return;
225 lock (this) {
226 if (os_handle != InvalidHandle) {
227 NativeEventCalls.CloseEvent_internal (os_handle);
228 os_handle = InvalidHandle;
234 ~WaitHandle() {
235 Dispose(false);