ThreadPool.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // System.Threading.ThreadPool
  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.Security.Permissions;
  36. namespace System.Threading {
  37. #if NET_2_0
  38. public static class ThreadPool {
  39. #else
  40. public sealed class ThreadPool {
  41. private ThreadPool ()
  42. {
  43. /* nothing to do */
  44. }
  45. #endif
  46. public static bool BindHandle (IntPtr osHandle)
  47. {
  48. return true;
  49. }
  50. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  51. public static extern void GetAvailableThreads (out int workerThreads, out int completionPortThreads);
  52. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  53. public static extern void GetMaxThreads (out int workerThreads, out int completionPortThreads);
  54. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  55. public static extern void GetMinThreads (out int workerThreads, out int completionPortThreads);
  56. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  57. [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
  58. public static extern bool SetMinThreads (int workerThreads, int completionPortThreads);
  59. public static bool QueueUserWorkItem (WaitCallback callback)
  60. {
  61. IAsyncResult ar = callback.BeginInvoke (null, null, null);
  62. if (ar == null)
  63. return false;
  64. return true;
  65. }
  66. public static bool QueueUserWorkItem (WaitCallback callback, object state)
  67. {
  68. IAsyncResult ar = callback.BeginInvoke (state, null, null);
  69. if (ar == null)
  70. return false;
  71. return true;
  72. }
  73. public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
  74. WaitOrTimerCallback callBack,
  75. object state,
  76. int millisecondsTimeOutInterval,
  77. bool executeOnlyOnce)
  78. {
  79. return RegisterWaitForSingleObject (waitObject, callBack, state,
  80. (long) millisecondsTimeOutInterval, executeOnlyOnce);
  81. }
  82. public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
  83. WaitOrTimerCallback callBack,
  84. object state,
  85. long millisecondsTimeOutInterval,
  86. bool executeOnlyOnce)
  87. {
  88. if (millisecondsTimeOutInterval < -1)
  89. throw new ArgumentOutOfRangeException ("timeout", "timeout < -1");
  90. if (millisecondsTimeOutInterval > Int32.MaxValue)
  91. throw new NotSupportedException ("Timeout is too big. Maximum is Int32.MaxValue");
  92. TimeSpan timeout = new TimeSpan (0, 0, 0, 0, (int) millisecondsTimeOutInterval);
  93. RegisteredWaitHandle waiter = new RegisteredWaitHandle (waitObject, callBack, state,
  94. timeout, executeOnlyOnce);
  95. QueueUserWorkItem (new WaitCallback (waiter.Wait), null);
  96. return waiter;
  97. }
  98. public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
  99. WaitOrTimerCallback callBack,
  100. object state,
  101. TimeSpan timeout,
  102. bool executeOnlyOnce)
  103. {
  104. return RegisterWaitForSingleObject (waitObject, callBack, state,
  105. (long) timeout.TotalMilliseconds, executeOnlyOnce);
  106. }
  107. [CLSCompliant(false)]
  108. public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
  109. WaitOrTimerCallback callBack,
  110. object state,
  111. uint millisecondsTimeOutInterval,
  112. bool executeOnlyOnce)
  113. {
  114. return RegisterWaitForSingleObject (waitObject, callBack, state,
  115. (long) millisecondsTimeOutInterval, executeOnlyOnce);
  116. }
  117. [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
  118. public static bool UnsafeQueueUserWorkItem (WaitCallback callback, object state)
  119. {
  120. // no stack propagation here (that's why it's unsafe and requires extra security permissions)
  121. IAsyncResult ar = null;
  122. try {
  123. if (!ExecutionContext.IsFlowSuppressed ())
  124. ExecutionContext.SuppressFlow (); // on current thread only
  125. ar = callback.BeginInvoke (state, null, null);
  126. }
  127. finally {
  128. if (ExecutionContext.IsFlowSuppressed ())
  129. ExecutionContext.RestoreFlow ();
  130. }
  131. return (ar != null);
  132. }
  133. [MonoTODO]
  134. [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
  135. public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
  136. WaitOrTimerCallback callBack, object state, int millisecondsTimeOutInterval,
  137. bool executeOnlyOnce)
  138. {
  139. throw new NotImplementedException ();
  140. }
  141. [MonoTODO]
  142. [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
  143. public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
  144. WaitOrTimerCallback callBack, object state, long millisecondsTimeOutInterval,
  145. bool executeOnlyOnce)
  146. {
  147. throw new NotImplementedException ();
  148. }
  149. [MonoTODO]
  150. [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
  151. public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
  152. WaitOrTimerCallback callBack, object state, TimeSpan timeout,
  153. bool executeOnlyOnce)
  154. {
  155. throw new NotImplementedException ();
  156. }
  157. [MonoTODO]
  158. [CLSCompliant (false)]
  159. [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
  160. public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
  161. WaitOrTimerCallback callBack, object state, uint millisecondsTimeOutInterval,
  162. bool executeOnlyOnce)
  163. {
  164. throw new NotImplementedException ();
  165. }
  166. }
  167. }