ThreadPool.cs 6.4 KB

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