2 // System.Threading.ThreadPool.cs
6 // Dick Porter (dick@ximian.com)
7 // Maurer Dietmar (dietmar@ximian.com)
9 // (C) Ximian, Inc. http://www.ximian.com
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
33 using System.Globalization;
34 using System.Runtime.CompilerServices;
35 using System.Runtime.Remoting.Messaging;
36 using System.Runtime.InteropServices;
37 using System.Security.Permissions;
39 namespace System.Threading {
41 public static class ThreadPool {
42 [Obsolete("This method is obsolete, use BindHandle(SafeHandle) instead")]
43 public static bool BindHandle (IntPtr osHandle)
48 public static bool BindHandle (SafeHandle osHandle)
51 throw new ArgumentNullException ("osHandle");
57 [MethodImplAttribute(MethodImplOptions.InternalCall)]
58 public static extern void GetAvailableThreads (out int workerThreads, out int completionPortThreads);
60 [MethodImplAttribute(MethodImplOptions.InternalCall)]
61 public static extern void GetMaxThreads (out int workerThreads, out int completionPortThreads);
63 [MethodImplAttribute(MethodImplOptions.InternalCall)]
64 public static extern void GetMinThreads (out int workerThreads, out int completionPortThreads);
66 [MonoTODO("The min number of completion port threads is not evaluated.")]
67 [MethodImplAttribute(MethodImplOptions.InternalCall)]
68 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
69 public static extern bool SetMinThreads (int workerThreads, int completionPortThreads);
71 [MethodImplAttribute(MethodImplOptions.InternalCall)]
72 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
73 public static extern bool SetMaxThreads (int workerThreads, int completionPortThreads);
75 public static bool QueueUserWorkItem (WaitCallback callBack)
77 return QueueUserWorkItem (callBack, null);
80 public static bool QueueUserWorkItem (WaitCallback callBack, object state)
83 throw new ArgumentNullException ("callBack");
85 if (callBack.IsTransparentProxy ()) {
86 IAsyncResult ar = callBack.BeginInvoke (state, null, null);
90 AsyncResult ares = new AsyncResult (callBack, state, true);
96 [MethodImplAttribute(MethodImplOptions.InternalCall)]
97 static extern void pool_queue (AsyncResult ares);
99 // TODO: It should be interface interface only to avoid extra allocation
100 internal static void QueueWorkItem (WaitCallback callBack, object state)
102 pool_queue (new AsyncResult (callBack, state, false));
105 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
106 WaitOrTimerCallback callBack,
108 int millisecondsTimeOutInterval,
109 bool executeOnlyOnce)
111 return RegisterWaitForSingleObject (waitObject, callBack, state,
112 (long) millisecondsTimeOutInterval, executeOnlyOnce);
115 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
116 WaitOrTimerCallback callBack,
118 long millisecondsTimeOutInterval,
119 bool executeOnlyOnce)
121 if (waitObject == null)
122 throw new ArgumentNullException ("waitObject");
124 if (callBack == null)
125 throw new ArgumentNullException ("callBack");
127 if (millisecondsTimeOutInterval < -1)
128 throw new ArgumentOutOfRangeException ("timeout", "timeout < -1");
130 if (millisecondsTimeOutInterval > Int32.MaxValue)
131 throw new NotSupportedException ("Timeout is too big. Maximum is Int32.MaxValue");
133 TimeSpan timeout = new TimeSpan (0, 0, 0, 0, (int) millisecondsTimeOutInterval);
135 RegisteredWaitHandle waiter = new RegisteredWaitHandle (waitObject, callBack, state,
136 timeout, executeOnlyOnce);
137 QueueUserWorkItem (new WaitCallback (waiter.Wait), null);
141 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
142 WaitOrTimerCallback callBack,
145 bool executeOnlyOnce)
147 return RegisterWaitForSingleObject (waitObject, callBack, state,
148 (long) timeout.TotalMilliseconds, executeOnlyOnce);
152 [CLSCompliant(false)]
153 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
154 WaitOrTimerCallback callBack,
156 uint millisecondsTimeOutInterval,
157 bool executeOnlyOnce)
159 return RegisterWaitForSingleObject (waitObject, callBack, state,
160 (long) millisecondsTimeOutInterval, executeOnlyOnce);
163 [CLSCompliant (false)]
164 unsafe public static bool UnsafeQueueNativeOverlapped (NativeOverlapped *overlapped)
166 throw new NotImplementedException ();
169 #if !NET_2_1 || MOBILE
171 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
172 public static bool UnsafeQueueUserWorkItem (WaitCallback callBack, object state)
174 if (callBack == null)
175 throw new ArgumentNullException ("callBack");
177 // no stack propagation here (that's why it's unsafe and requires extra security permissions)
178 if (!callBack.IsTransparentProxy ()) {
179 AsyncResult ares = new AsyncResult (callBack, state, false);
184 if (!ExecutionContext.IsFlowSuppressed ())
185 ExecutionContext.SuppressFlow (); // on current thread only
186 IAsyncResult ar = callBack.BeginInvoke (state, null, null);
190 if (ExecutionContext.IsFlowSuppressed ())
191 ExecutionContext.RestoreFlow ();
196 [MonoTODO("Not implemented")]
197 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
198 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
199 WaitOrTimerCallback callBack, object state, int millisecondsTimeOutInterval,
200 bool executeOnlyOnce)
202 throw new NotImplementedException ();
205 [MonoTODO("Not implemented")]
206 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
207 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
208 WaitOrTimerCallback callBack, object state, long millisecondsTimeOutInterval,
209 bool executeOnlyOnce)
211 throw new NotImplementedException ();
214 [MonoTODO("Not implemented")]
215 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
216 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
217 WaitOrTimerCallback callBack, object state, TimeSpan timeout,
218 bool executeOnlyOnce)
220 throw new NotImplementedException ();
223 [MonoTODO("Not implemented")]
224 [CLSCompliant (false)]
225 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
226 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
227 WaitOrTimerCallback callBack, object state, uint millisecondsTimeOutInterval,
228 bool executeOnlyOnce)
230 throw new NotImplementedException ();