ThreadPool.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 Novell (http://www.novell.com)
  11. //
  12. //
  13. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  14. //
  15. // Permission is hereby granted, free of charge, to any person obtaining
  16. // a copy of this software and associated documentation files (the
  17. // "Software"), to deal in the Software without restriction, including
  18. // without limitation the rights to use, copy, modify, merge, publish,
  19. // distribute, sublicense, and/or sell copies of the Software, and to
  20. // permit persons to whom the Software is furnished to do so, subject to
  21. // the following conditions:
  22. //
  23. // The above copyright notice and this permission notice shall be
  24. // included in all copies or substantial portions of the Software.
  25. //
  26. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  30. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  31. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  32. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  33. //
  34. using System;
  35. using System.Collections;
  36. using System.Globalization;
  37. using System.Runtime.CompilerServices;
  38. namespace System.Threading {
  39. public sealed class ThreadPool {
  40. private ThreadPool ()
  41. {
  42. /* nothing to do */
  43. }
  44. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  45. static extern bool BindHandleInternal (IntPtr osHandle);
  46. public static bool BindHandle (IntPtr osHandle)
  47. {
  48. return BindHandleInternal (osHandle);
  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. public static extern bool SetMinThreads (int workerThreads, int completionPortThreads);
  58. public static bool QueueUserWorkItem (WaitCallback callback)
  59. {
  60. IAsyncResult ar = callback.BeginInvoke (null, null, null);
  61. if (ar == null)
  62. return false;
  63. return true;
  64. }
  65. public static bool QueueUserWorkItem (WaitCallback callback, object state)
  66. {
  67. IAsyncResult ar = callback.BeginInvoke (state, null, null);
  68. if (ar == null)
  69. return false;
  70. return true;
  71. }
  72. public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
  73. WaitOrTimerCallback callBack,
  74. object state,
  75. int millisecondsTimeOutInterval,
  76. bool executeOnlyOnce)
  77. {
  78. return RegisterWaitForSingleObject (waitObject, callBack, state,
  79. (long) millisecondsTimeOutInterval, executeOnlyOnce);
  80. }
  81. public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
  82. WaitOrTimerCallback callBack,
  83. object state,
  84. long millisecondsTimeOutInterval,
  85. bool executeOnlyOnce)
  86. {
  87. if (millisecondsTimeOutInterval < -1)
  88. throw new ArgumentOutOfRangeException ("timeout", "timeout < -1");
  89. if (millisecondsTimeOutInterval > Int32.MaxValue)
  90. throw new NotSupportedException ("Timeout is too big. Maximum is Int32.MaxValue");
  91. TimeSpan timeout = new TimeSpan (0, 0, 0, 0, (int) millisecondsTimeOutInterval);
  92. RegisteredWaitHandle waiter = new RegisteredWaitHandle (waitObject, callBack, state,
  93. timeout, executeOnlyOnce);
  94. QueueUserWorkItem (new WaitCallback (waiter.Wait), null);
  95. return waiter;
  96. }
  97. public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
  98. WaitOrTimerCallback callBack,
  99. object state,
  100. TimeSpan timeout,
  101. bool executeOnlyOnce)
  102. {
  103. return RegisterWaitForSingleObject (waitObject, callBack, state,
  104. (long) timeout.TotalMilliseconds, executeOnlyOnce);
  105. }
  106. [CLSCompliant(false)]
  107. public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
  108. WaitOrTimerCallback callBack,
  109. object state,
  110. uint millisecondsTimeOutInterval,
  111. bool executeOnlyOnce)
  112. {
  113. return RegisterWaitForSingleObject (waitObject, callBack, state,
  114. (long) millisecondsTimeOutInterval, executeOnlyOnce);
  115. }
  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. public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
  125. WaitOrTimerCallback callBack, object state, int millisecondsTimeOutInterval,
  126. bool executeOnlyOnce)
  127. {
  128. throw new NotImplementedException ();
  129. }
  130. [MonoTODO]
  131. public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
  132. WaitOrTimerCallback callBack, object state, long millisecondsTimeOutInterval,
  133. bool executeOnlyOnce)
  134. {
  135. throw new NotImplementedException ();
  136. }
  137. [MonoTODO]
  138. public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
  139. WaitOrTimerCallback callBack, object state, TimeSpan timeout,
  140. bool executeOnlyOnce)
  141. {
  142. throw new NotImplementedException ();
  143. }
  144. [MonoTODO]
  145. [CLSCompliant (false)]
  146. public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
  147. WaitOrTimerCallback callBack, object state, uint millisecondsTimeOutInterval,
  148. bool executeOnlyOnce)
  149. {
  150. throw new NotImplementedException ();
  151. }
  152. }
  153. }