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.Collections.Generic;
34 using System.Globalization;
35 using System.Runtime.CompilerServices;
36 using System.Runtime.Remoting.Messaging;
37 using System.Runtime.InteropServices;
38 using System.Security;
39 using System.Security.Permissions;
41 namespace System.Threading {
43 public static class ThreadPool {
45 [Obsolete("This method is obsolete, use BindHandle(SafeHandle) instead")]
46 public static bool BindHandle (IntPtr osHandle)
48 if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
49 return Microsoft.ThreadPool.BindHandle (osHandle);
54 public static bool BindHandle (SafeHandle osHandle)
56 if (Microsoft.ThreadPool.UseMicrosoftThreadPool) {
57 return Microsoft.ThreadPool.BindHandle (osHandle);
60 throw new ArgumentNullException ("osHandle");
66 public static void GetAvailableThreads (out int workerThreads, out int completionPortThreads)
68 if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
69 Microsoft.ThreadPool.GetAvailableThreads (out workerThreads, out completionPortThreads);
71 GetAvailableThreads_internal (out workerThreads, out completionPortThreads);
74 [MethodImplAttribute(MethodImplOptions.InternalCall)]
75 static extern void GetAvailableThreads_internal (out int workerThreads, out int completionPortThreads);
77 public static void GetMaxThreads (out int workerThreads, out int completionPortThreads)
79 if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
80 Microsoft.ThreadPool.GetMaxThreads (out workerThreads, out completionPortThreads);
82 GetMaxThreads_internal (out workerThreads, out completionPortThreads);
85 [MethodImplAttribute(MethodImplOptions.InternalCall)]
86 static extern void GetMaxThreads_internal (out int workerThreads, out int completionPortThreads);
88 public static void GetMinThreads (out int workerThreads, out int completionPortThreads)
90 if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
91 Microsoft.ThreadPool.GetMinThreads (out workerThreads, out completionPortThreads);
93 GetMinThreads_internal (out workerThreads, out completionPortThreads);
96 [MethodImplAttribute(MethodImplOptions.InternalCall)]
97 static extern void GetMinThreads_internal (out int workerThreads, out int completionPortThreads);
99 [MonoTODO("The min number of completion port threads is not evaluated.")]
100 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
101 public static bool SetMinThreads (int workerThreads, int completionPortThreads)
103 if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
104 return Microsoft.ThreadPool.SetMinThreads (workerThreads, completionPortThreads);
106 return SetMinThreads_internal (workerThreads, completionPortThreads);
109 [MethodImplAttribute(MethodImplOptions.InternalCall)]
110 static extern bool SetMinThreads_internal (int workerThreads, int completionPortThreads);
112 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
113 public static bool SetMaxThreads (int workerThreads, int completionPortThreads)
115 if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
116 return Microsoft.ThreadPool.SetMaxThreads (workerThreads, completionPortThreads);
118 return SetMaxThreads_internal (workerThreads, completionPortThreads);
121 [MethodImplAttribute(MethodImplOptions.InternalCall)]
122 static extern bool SetMaxThreads_internal (int workerThreads, int completionPortThreads);
124 public static bool QueueUserWorkItem (WaitCallback callBack)
126 if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
127 return Microsoft.ThreadPool.QueueUserWorkItem (callBack, null);
129 return QueueUserWorkItem (callBack, null);
132 public static bool QueueUserWorkItem (WaitCallback callBack, object state)
134 if (Microsoft.ThreadPool.UseMicrosoftThreadPool) {
135 return Microsoft.ThreadPool.QueueUserWorkItem (callBack, state);
137 if (callBack == null)
138 throw new ArgumentNullException ("callBack");
140 if (callBack.IsTransparentProxy ()) {
141 IAsyncResult ar = callBack.BeginInvoke (state, null, null);
145 AsyncResult ares = new AsyncResult (callBack, state, true);
152 [MethodImplAttribute(MethodImplOptions.InternalCall)]
153 static extern void pool_queue (AsyncResult ares);
155 // TODO: It should be interface interface only to avoid extra allocation
156 internal static void QueueWorkItem (WaitCallback callBack, object state)
158 if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
159 Microsoft.ThreadPool.QueueUserWorkItem (callBack, state);
161 pool_queue (new AsyncResult (callBack, state, false));
164 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
165 WaitOrTimerCallback callBack,
167 int millisecondsTimeOutInterval,
168 bool executeOnlyOnce)
170 if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
171 return Microsoft.ThreadPool.RegisterWaitForSingleObject (waitObject, callBack, state, millisecondsTimeOutInterval, executeOnlyOnce);
173 return RegisterWaitForSingleObject (waitObject, callBack, state, (long) millisecondsTimeOutInterval, executeOnlyOnce);
176 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
177 WaitOrTimerCallback callBack,
179 long millisecondsTimeOutInterval,
180 bool executeOnlyOnce)
182 if (Microsoft.ThreadPool.UseMicrosoftThreadPool) {
183 return Microsoft.ThreadPool.RegisterWaitForSingleObject (waitObject, callBack, state, millisecondsTimeOutInterval, executeOnlyOnce);
185 if (waitObject == null)
186 throw new ArgumentNullException ("waitObject");
188 if (callBack == null)
189 throw new ArgumentNullException ("callBack");
191 if (millisecondsTimeOutInterval < -1)
192 throw new ArgumentOutOfRangeException ("timeout", "timeout < -1");
194 if (millisecondsTimeOutInterval > Int32.MaxValue)
195 throw new NotSupportedException ("Timeout is too big. Maximum is Int32.MaxValue");
197 TimeSpan timeout = new TimeSpan (0, 0, 0, 0, (int) millisecondsTimeOutInterval);
199 RegisteredWaitHandle waiter = new RegisteredWaitHandle (waitObject, callBack, state,
200 timeout, executeOnlyOnce);
201 QueueUserWorkItem (new WaitCallback (waiter.Wait), null);
206 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
207 WaitOrTimerCallback callBack,
210 bool executeOnlyOnce)
212 if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
213 return Microsoft.ThreadPool.RegisterWaitForSingleObject (waitObject, callBack, state, timeout, executeOnlyOnce);
215 return RegisterWaitForSingleObject (waitObject, callBack, state, (long) timeout.TotalMilliseconds, executeOnlyOnce);
219 [CLSCompliant(false)]
220 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
221 WaitOrTimerCallback callBack,
223 uint millisecondsTimeOutInterval,
224 bool executeOnlyOnce)
226 if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
227 return Microsoft.ThreadPool.RegisterWaitForSingleObject (waitObject, callBack, state, millisecondsTimeOutInterval, executeOnlyOnce);
229 return RegisterWaitForSingleObject (waitObject, callBack, state, (long) millisecondsTimeOutInterval, executeOnlyOnce);
232 [CLSCompliant (false)]
233 unsafe public static bool UnsafeQueueNativeOverlapped (NativeOverlapped *overlapped)
235 if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
236 return Microsoft.ThreadPool.UnsafeQueueNativeOverlapped (overlapped);
238 throw new NotImplementedException ();
241 #if !NET_2_1 || MOBILE
243 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
244 public static bool UnsafeQueueUserWorkItem (WaitCallback callBack, object state)
246 if (Microsoft.ThreadPool.UseMicrosoftThreadPool) {
247 return Microsoft.ThreadPool.UnsafeQueueUserWorkItem (callBack, state);
249 if (callBack == null)
250 throw new ArgumentNullException ("callBack");
252 // no stack propagation here (that's why it's unsafe and requires extra security permissions)
253 if (!callBack.IsTransparentProxy ()) {
254 AsyncResult ares = new AsyncResult (callBack, state, false);
259 if (!ExecutionContext.IsFlowSuppressed ())
260 ExecutionContext.SuppressFlow (); // on current thread only
261 IAsyncResult ar = callBack.BeginInvoke (state, null, null);
265 if (ExecutionContext.IsFlowSuppressed ())
266 ExecutionContext.RestoreFlow ();
272 [MonoTODO("Not implemented")]
273 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
274 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
275 WaitOrTimerCallback callBack, object state, int millisecondsTimeOutInterval,
276 bool executeOnlyOnce)
278 if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
279 return Microsoft.ThreadPool.UnsafeRegisterWaitForSingleObject (waitObject, callBack, state, millisecondsTimeOutInterval, executeOnlyOnce);
281 throw new NotImplementedException ();
284 [MonoTODO("Not implemented")]
285 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
286 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
287 WaitOrTimerCallback callBack, object state, long millisecondsTimeOutInterval,
288 bool executeOnlyOnce)
290 if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
291 return Microsoft.ThreadPool.UnsafeRegisterWaitForSingleObject (waitObject, callBack, state, millisecondsTimeOutInterval, executeOnlyOnce);
293 throw new NotImplementedException ();
296 [MonoTODO("Not implemented")]
297 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
298 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
299 WaitOrTimerCallback callBack, object state, TimeSpan timeout,
300 bool executeOnlyOnce)
302 if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
303 return Microsoft.ThreadPool.UnsafeRegisterWaitForSingleObject (waitObject, callBack, state, timeout, executeOnlyOnce);
305 throw new NotImplementedException ();
308 [MonoTODO("Not implemented")]
309 [CLSCompliant (false)]
310 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
311 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
312 WaitOrTimerCallback callBack, object state, uint millisecondsTimeOutInterval,
313 bool executeOnlyOnce)
315 if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
316 return Microsoft.ThreadPool.UnsafeRegisterWaitForSingleObject (waitObject, callBack, state, millisecondsTimeOutInterval, executeOnlyOnce);
318 throw new NotImplementedException ();
323 #region ReferenceSources
324 // Extracted from ../../../../external/referencesource/mscorlib/system/threading/threadpool.cs
325 internal static void UnsafeQueueCustomWorkItem(IThreadPoolWorkItem workItem, bool forceGlobal)
327 if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
328 Microsoft.ThreadPool.UnsafeQueueCustomWorkItem (workItem, forceGlobal);
330 QueueWorkItem ((obj) => ((IThreadPoolWorkItem)obj).ExecuteWorkItem (), workItem);
333 internal static IEnumerable<IThreadPoolWorkItem> GetQueuedWorkItems()
335 if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
336 return Microsoft.ThreadPool.GetQueuedWorkItems ();
338 return new IThreadPoolWorkItem [0];
341 internal static bool TryPopCustomWorkItem(IThreadPoolWorkItem workItem)
343 if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
344 return Microsoft.ThreadPool.TryPopCustomWorkItem (workItem);
349 internal static void NotifyWorkItemProgress()
351 if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
352 Microsoft.ThreadPool.NotifyWorkItemProgress ();