ThreadPool.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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.Globalization;
  33. using System.Runtime.CompilerServices;
  34. using System.Runtime.Remoting.Messaging;
  35. using System.Runtime.InteropServices;
  36. using System.Security.Permissions;
  37. namespace System.Threading {
  38. #if NET_2_0
  39. public static class ThreadPool {
  40. #else
  41. public sealed class ThreadPool {
  42. private ThreadPool ()
  43. {
  44. /* nothing to do */
  45. }
  46. #endif
  47. #if NET_2_0
  48. [Obsolete("This method is obsolete, use BindHandle(SafeHandle) instead")]
  49. #endif
  50. public static bool BindHandle (IntPtr osHandle)
  51. {
  52. return true;
  53. }
  54. #if NET_2_0
  55. public static bool BindHandle (SafeHandle osHandle)
  56. {
  57. if (osHandle == null)
  58. throw new ArgumentNullException ("osHandle");
  59. return true;
  60. }
  61. #endif
  62. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  63. public static extern void GetAvailableThreads (out int workerThreads, out int completionPortThreads);
  64. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  65. public static extern void GetMaxThreads (out int workerThreads, out int completionPortThreads);
  66. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  67. public static extern void GetMinThreads (out int workerThreads, out int completionPortThreads);
  68. [MonoTODO("The min number of completion port threads is not evaluated.")]
  69. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  70. [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
  71. public static extern bool SetMinThreads (int workerThreads, int completionPortThreads);
  72. #if NET_2_0
  73. [MonoTODO("The max number of threads cannot be decremented.")]
  74. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  75. [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
  76. public static extern bool SetMaxThreads (int workerThreads, int completionPortThreads);
  77. #endif
  78. public static bool QueueUserWorkItem (WaitCallback callBack)
  79. {
  80. return QueueUserWorkItem (callBack, null);
  81. }
  82. public static bool QueueUserWorkItem (WaitCallback callBack, object state)
  83. {
  84. callBack = MoonlightHandler (callBack);
  85. IAsyncResult ar = callBack.BeginInvoke (state, null, null);
  86. if (ar == null)
  87. return false;
  88. return true;
  89. }
  90. public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
  91. WaitOrTimerCallback callBack,
  92. object state,
  93. int millisecondsTimeOutInterval,
  94. bool executeOnlyOnce)
  95. {
  96. return RegisterWaitForSingleObject (waitObject, callBack, state,
  97. (long) millisecondsTimeOutInterval, executeOnlyOnce);
  98. }
  99. public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
  100. WaitOrTimerCallback callBack,
  101. object state,
  102. long millisecondsTimeOutInterval,
  103. bool executeOnlyOnce)
  104. {
  105. if (millisecondsTimeOutInterval < -1)
  106. throw new ArgumentOutOfRangeException ("timeout", "timeout < -1");
  107. if (millisecondsTimeOutInterval > Int32.MaxValue)
  108. throw new NotSupportedException ("Timeout is too big. Maximum is Int32.MaxValue");
  109. TimeSpan timeout = new TimeSpan (0, 0, 0, 0, (int) millisecondsTimeOutInterval);
  110. RegisteredWaitHandle waiter = new RegisteredWaitHandle (waitObject, callBack, state,
  111. timeout, executeOnlyOnce);
  112. QueueUserWorkItem (new WaitCallback (waiter.Wait), null);
  113. return waiter;
  114. }
  115. public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
  116. WaitOrTimerCallback callBack,
  117. object state,
  118. TimeSpan timeout,
  119. bool executeOnlyOnce)
  120. {
  121. return RegisterWaitForSingleObject (waitObject, callBack, state,
  122. (long) timeout.TotalMilliseconds, executeOnlyOnce);
  123. }
  124. [CLSCompliant(false)]
  125. public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
  126. WaitOrTimerCallback callBack,
  127. object state,
  128. uint millisecondsTimeOutInterval,
  129. bool executeOnlyOnce)
  130. {
  131. return RegisterWaitForSingleObject (waitObject, callBack, state,
  132. (long) millisecondsTimeOutInterval, executeOnlyOnce);
  133. }
  134. #if NET_2_0
  135. [CLSCompliant (false)]
  136. unsafe public static bool UnsafeQueueNativeOverlapped (NativeOverlapped *overlapped)
  137. {
  138. throw new NotImplementedException ();
  139. }
  140. #endif
  141. [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
  142. public static bool UnsafeQueueUserWorkItem (WaitCallback callBack, object state)
  143. {
  144. // no stack propagation here (that's why it's unsafe and requires extra security permissions)
  145. IAsyncResult ar = null;
  146. try {
  147. if (!ExecutionContext.IsFlowSuppressed ())
  148. ExecutionContext.SuppressFlow (); // on current thread only
  149. ar = callBack.BeginInvoke (state, null, null);
  150. }
  151. finally {
  152. if (ExecutionContext.IsFlowSuppressed ())
  153. ExecutionContext.RestoreFlow ();
  154. }
  155. return (ar != null);
  156. }
  157. [MonoTODO("Not implemented")]
  158. [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
  159. public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
  160. WaitOrTimerCallback callBack, object state, int millisecondsTimeOutInterval,
  161. bool executeOnlyOnce)
  162. {
  163. throw new NotImplementedException ();
  164. }
  165. [MonoTODO("Not implemented")]
  166. [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
  167. public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
  168. WaitOrTimerCallback callBack, object state, long millisecondsTimeOutInterval,
  169. bool executeOnlyOnce)
  170. {
  171. throw new NotImplementedException ();
  172. }
  173. [MonoTODO("Not implemented")]
  174. [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
  175. public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
  176. WaitOrTimerCallback callBack, object state, TimeSpan timeout,
  177. bool executeOnlyOnce)
  178. {
  179. throw new NotImplementedException ();
  180. }
  181. [MonoTODO("Not implemented")]
  182. [CLSCompliant (false)]
  183. [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
  184. public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
  185. WaitOrTimerCallback callBack, object state, uint millisecondsTimeOutInterval,
  186. bool executeOnlyOnce)
  187. {
  188. throw new NotImplementedException ();
  189. }
  190. static WaitCallback MoonlightHandler (WaitCallback callback)
  191. {
  192. #if NET_2_1
  193. return delegate (object o) {
  194. try {
  195. callback (o);
  196. }
  197. catch (Exception ex) {
  198. Thread.MoonlightUnhandledException (ex);
  199. }
  200. };
  201. #else
  202. return callback;
  203. #endif
  204. }
  205. }
  206. }