ThreadPool.cs 7.7 KB

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