ThreadPool.cs 6.2 KB

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