ThreadPool.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. //
  2. // System.Threading.ThreadPool.cs
  3. //
  4. // Author:
  5. // Patrik Torstensson
  6. // Dick Porter ([email protected])
  7. // Maurer Dietmar ([email protected])
  8. //
  9. // (C) Ximian, Inc. http://www.ximian.com
  10. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  11. //
  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:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  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.
  30. //
  31. using System.Collections;
  32. using System.Collections.Generic;
  33. using System.Globalization;
  34. using System.Runtime.CompilerServices;
  35. using System.Runtime.Remoting.Messaging;
  36. using System.Runtime.InteropServices;
  37. using System.Security;
  38. using System.Security.Permissions;
  39. namespace System.Threading {
  40. public static class ThreadPool {
  41. [Obsolete("This method is obsolete, use BindHandle(SafeHandle) instead")]
  42. public static bool BindHandle (IntPtr osHandle)
  43. {
  44. if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
  45. return Microsoft.ThreadPool.BindHandle (osHandle);
  46. else
  47. return true;
  48. }
  49. public static bool BindHandle (SafeHandle osHandle)
  50. {
  51. if (Microsoft.ThreadPool.UseMicrosoftThreadPool) {
  52. return Microsoft.ThreadPool.BindHandle (osHandle);
  53. } else {
  54. if (osHandle == null)
  55. throw new ArgumentNullException ("osHandle");
  56. return true;
  57. }
  58. }
  59. public static void GetAvailableThreads (out int workerThreads, out int completionPortThreads)
  60. {
  61. if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
  62. Microsoft.ThreadPool.GetAvailableThreads (out workerThreads, out completionPortThreads);
  63. else
  64. GetAvailableThreads_internal (out workerThreads, out completionPortThreads);
  65. }
  66. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  67. static extern void GetAvailableThreads_internal (out int workerThreads, out int completionPortThreads);
  68. public static void GetMaxThreads (out int workerThreads, out int completionPortThreads)
  69. {
  70. if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
  71. Microsoft.ThreadPool.GetMaxThreads (out workerThreads, out completionPortThreads);
  72. else
  73. GetMaxThreads_internal (out workerThreads, out completionPortThreads);
  74. }
  75. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  76. static extern void GetMaxThreads_internal (out int workerThreads, out int completionPortThreads);
  77. public static void GetMinThreads (out int workerThreads, out int completionPortThreads)
  78. {
  79. if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
  80. Microsoft.ThreadPool.GetMinThreads (out workerThreads, out completionPortThreads);
  81. else
  82. GetMinThreads_internal (out workerThreads, out completionPortThreads);
  83. }
  84. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  85. static extern void GetMinThreads_internal (out int workerThreads, out int completionPortThreads);
  86. [MonoTODO("The min number of completion port threads is not evaluated.")]
  87. [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
  88. public static bool SetMinThreads (int workerThreads, int completionPortThreads)
  89. {
  90. if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
  91. return Microsoft.ThreadPool.SetMinThreads (workerThreads, completionPortThreads);
  92. else
  93. return SetMinThreads_internal (workerThreads, completionPortThreads);
  94. }
  95. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  96. static extern bool SetMinThreads_internal (int workerThreads, int completionPortThreads);
  97. [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
  98. public static bool SetMaxThreads (int workerThreads, int completionPortThreads)
  99. {
  100. if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
  101. return Microsoft.ThreadPool.SetMaxThreads (workerThreads, completionPortThreads);
  102. else
  103. return SetMaxThreads_internal (workerThreads, completionPortThreads);
  104. }
  105. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  106. static extern bool SetMaxThreads_internal (int workerThreads, int completionPortThreads);
  107. public static bool QueueUserWorkItem (WaitCallback callBack)
  108. {
  109. if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
  110. return Microsoft.ThreadPool.QueueUserWorkItem (callBack, null);
  111. else
  112. return QueueUserWorkItem (callBack, null);
  113. }
  114. public static bool QueueUserWorkItem (WaitCallback callBack, object state)
  115. {
  116. if (Microsoft.ThreadPool.UseMicrosoftThreadPool) {
  117. return Microsoft.ThreadPool.QueueUserWorkItem (callBack, state);
  118. } else {
  119. if (callBack == null)
  120. throw new ArgumentNullException ("callBack");
  121. if (callBack.IsTransparentProxy ()) {
  122. IAsyncResult ar = callBack.BeginInvoke (state, null, null);
  123. if (ar == null)
  124. return false;
  125. } else {
  126. AsyncResult ares = new AsyncResult (callBack, state, true);
  127. pool_queue (ares);
  128. }
  129. return true;
  130. }
  131. }
  132. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  133. static extern void pool_queue (AsyncResult ares);
  134. // TODO: It should be interface interface only to avoid extra allocation
  135. internal static void QueueWorkItem (WaitCallback callBack, object state)
  136. {
  137. if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
  138. Microsoft.ThreadPool.QueueUserWorkItem (callBack, state);
  139. else
  140. pool_queue (new AsyncResult (callBack, state, false));
  141. }
  142. public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
  143. WaitOrTimerCallback callBack,
  144. object state,
  145. int millisecondsTimeOutInterval,
  146. bool executeOnlyOnce)
  147. {
  148. if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
  149. return Microsoft.ThreadPool.RegisterWaitForSingleObject (waitObject, callBack, state, millisecondsTimeOutInterval, executeOnlyOnce);
  150. else
  151. return RegisterWaitForSingleObject (waitObject, callBack, state, (long) millisecondsTimeOutInterval, executeOnlyOnce);
  152. }
  153. public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
  154. WaitOrTimerCallback callBack,
  155. object state,
  156. long millisecondsTimeOutInterval,
  157. bool executeOnlyOnce)
  158. {
  159. if (Microsoft.ThreadPool.UseMicrosoftThreadPool) {
  160. return Microsoft.ThreadPool.RegisterWaitForSingleObject (waitObject, callBack, state, millisecondsTimeOutInterval, executeOnlyOnce);
  161. } else {
  162. if (waitObject == null)
  163. throw new ArgumentNullException ("waitObject");
  164. if (callBack == null)
  165. throw new ArgumentNullException ("callBack");
  166. if (millisecondsTimeOutInterval < -1)
  167. throw new ArgumentOutOfRangeException ("timeout", "timeout < -1");
  168. if (millisecondsTimeOutInterval > Int32.MaxValue)
  169. throw new NotSupportedException ("Timeout is too big. Maximum is Int32.MaxValue");
  170. TimeSpan timeout = new TimeSpan (0, 0, 0, 0, (int) millisecondsTimeOutInterval);
  171. RegisteredWaitHandle waiter = new RegisteredWaitHandle (waitObject, callBack, state,
  172. timeout, executeOnlyOnce);
  173. QueueUserWorkItem (new WaitCallback (waiter.Wait), null);
  174. return waiter;
  175. }
  176. }
  177. public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
  178. WaitOrTimerCallback callBack,
  179. object state,
  180. TimeSpan timeout,
  181. bool executeOnlyOnce)
  182. {
  183. if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
  184. return Microsoft.ThreadPool.RegisterWaitForSingleObject (waitObject, callBack, state, timeout, executeOnlyOnce);
  185. else
  186. return RegisterWaitForSingleObject (waitObject, callBack, state, (long) timeout.TotalMilliseconds, executeOnlyOnce);
  187. }
  188. [CLSCompliant(false)]
  189. public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
  190. WaitOrTimerCallback callBack,
  191. object state,
  192. uint millisecondsTimeOutInterval,
  193. bool executeOnlyOnce)
  194. {
  195. if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
  196. return Microsoft.ThreadPool.RegisterWaitForSingleObject (waitObject, callBack, state, millisecondsTimeOutInterval, executeOnlyOnce);
  197. else
  198. return RegisterWaitForSingleObject (waitObject, callBack, state, (long) millisecondsTimeOutInterval, executeOnlyOnce);
  199. }
  200. [CLSCompliant (false)]
  201. unsafe public static bool UnsafeQueueNativeOverlapped (NativeOverlapped *overlapped)
  202. {
  203. if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
  204. return Microsoft.ThreadPool.UnsafeQueueNativeOverlapped (overlapped);
  205. else
  206. throw new NotImplementedException ();
  207. }
  208. #if !NET_2_1 || MOBILE
  209. [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
  210. public static bool UnsafeQueueUserWorkItem (WaitCallback callBack, object state)
  211. {
  212. if (Microsoft.ThreadPool.UseMicrosoftThreadPool) {
  213. return Microsoft.ThreadPool.UnsafeQueueUserWorkItem (callBack, state);
  214. } else {
  215. if (callBack == null)
  216. throw new ArgumentNullException ("callBack");
  217. // no stack propagation here (that's why it's unsafe and requires extra security permissions)
  218. if (!callBack.IsTransparentProxy ()) {
  219. AsyncResult ares = new AsyncResult (callBack, state, false);
  220. pool_queue (ares);
  221. return true;
  222. }
  223. try {
  224. if (!ExecutionContext.IsFlowSuppressed ())
  225. ExecutionContext.SuppressFlow (); // on current thread only
  226. IAsyncResult ar = callBack.BeginInvoke (state, null, null);
  227. if (ar == null)
  228. return false;
  229. } finally {
  230. if (ExecutionContext.IsFlowSuppressed ())
  231. ExecutionContext.RestoreFlow ();
  232. }
  233. return true;
  234. }
  235. }
  236. [MonoTODO("Not implemented")]
  237. [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
  238. public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
  239. WaitOrTimerCallback callBack, object state, int millisecondsTimeOutInterval,
  240. bool executeOnlyOnce)
  241. {
  242. if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
  243. return Microsoft.ThreadPool.UnsafeRegisterWaitForSingleObject (waitObject, callBack, state, millisecondsTimeOutInterval, executeOnlyOnce);
  244. else
  245. throw new NotImplementedException ();
  246. }
  247. [MonoTODO("Not implemented")]
  248. [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
  249. public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
  250. WaitOrTimerCallback callBack, object state, long millisecondsTimeOutInterval,
  251. bool executeOnlyOnce)
  252. {
  253. if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
  254. return Microsoft.ThreadPool.UnsafeRegisterWaitForSingleObject (waitObject, callBack, state, millisecondsTimeOutInterval, executeOnlyOnce);
  255. else
  256. throw new NotImplementedException ();
  257. }
  258. [MonoTODO("Not implemented")]
  259. [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
  260. public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
  261. WaitOrTimerCallback callBack, object state, TimeSpan timeout,
  262. bool executeOnlyOnce)
  263. {
  264. if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
  265. return Microsoft.ThreadPool.UnsafeRegisterWaitForSingleObject (waitObject, callBack, state, timeout, executeOnlyOnce);
  266. else
  267. throw new NotImplementedException ();
  268. }
  269. [MonoTODO("Not implemented")]
  270. [CLSCompliant (false)]
  271. [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
  272. public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
  273. WaitOrTimerCallback callBack, object state, uint millisecondsTimeOutInterval,
  274. bool executeOnlyOnce)
  275. {
  276. if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
  277. return Microsoft.ThreadPool.UnsafeRegisterWaitForSingleObject (waitObject, callBack, state, millisecondsTimeOutInterval, executeOnlyOnce);
  278. else
  279. throw new NotImplementedException ();
  280. }
  281. #endif
  282. #region ReferenceSources
  283. // Extracted from ../../../../external/referencesource/mscorlib/system/threading/threadpool.cs
  284. internal static void UnsafeQueueCustomWorkItem(IThreadPoolWorkItem workItem, bool forceGlobal)
  285. {
  286. if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
  287. Microsoft.ThreadPool.UnsafeQueueCustomWorkItem (workItem, forceGlobal);
  288. else
  289. QueueWorkItem ((obj) => ((IThreadPoolWorkItem)obj).ExecuteWorkItem (), workItem);
  290. }
  291. internal static IEnumerable<IThreadPoolWorkItem> GetQueuedWorkItems()
  292. {
  293. if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
  294. return Microsoft.ThreadPool.GetQueuedWorkItems ();
  295. else
  296. return new IThreadPoolWorkItem [0];
  297. }
  298. internal static bool TryPopCustomWorkItem(IThreadPoolWorkItem workItem)
  299. {
  300. if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
  301. return Microsoft.ThreadPool.TryPopCustomWorkItem (workItem);
  302. else
  303. return false;
  304. }
  305. internal static void NotifyWorkItemProgress()
  306. {
  307. if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
  308. Microsoft.ThreadPool.NotifyWorkItemProgress ();
  309. }
  310. #endregion
  311. }
  312. }