ThreadPool.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. using System;
  13. using System.Collections;
  14. using System.Globalization;
  15. using System.Runtime.CompilerServices;
  16. namespace System.Threading {
  17. public sealed class ThreadPool {
  18. private ThreadPool ()
  19. {
  20. /* nothing to do */
  21. }
  22. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  23. static extern bool BindHandleInternal (IntPtr osHandle);
  24. public static bool BindHandle (IntPtr osHandle)
  25. {
  26. return BindHandleInternal (osHandle);
  27. }
  28. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  29. public static extern void GetAvailableThreads (out int workerThreads, out int completionPortThreads);
  30. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  31. public static extern void GetMaxThreads (out int workerThreads, out int completionPortThreads);
  32. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  33. public static extern void GetMinThreads (out int workerThreads, out int completionPortThreads);
  34. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  35. public static extern void SetMinThreads (int workerThreads, int completionPortThreads);
  36. public static bool QueueUserWorkItem (WaitCallback callback)
  37. {
  38. IAsyncResult ar = callback.BeginInvoke (null, null, null);
  39. if (ar == null)
  40. return false;
  41. return true;
  42. }
  43. public static bool QueueUserWorkItem (WaitCallback callback, object state)
  44. {
  45. IAsyncResult ar = callback.BeginInvoke (state, null, null);
  46. if (ar == null)
  47. return false;
  48. return true;
  49. }
  50. public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
  51. WaitOrTimerCallback callBack,
  52. object state,
  53. int millisecondsTimeOutInterval,
  54. bool executeOnlyOnce)
  55. {
  56. return RegisterWaitForSingleObject (waitObject, callBack, state,
  57. (long) millisecondsTimeOutInterval, executeOnlyOnce);
  58. }
  59. public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
  60. WaitOrTimerCallback callBack,
  61. object state,
  62. long millisecondsTimeOutInterval,
  63. bool executeOnlyOnce)
  64. {
  65. if (millisecondsTimeOutInterval < -1)
  66. throw new ArgumentOutOfRangeException ("timeout", "timeout < -1");
  67. if (millisecondsTimeOutInterval > Int32.MaxValue)
  68. throw new NotSupportedException ("Timeout is too big. Maximum is Int32.MaxValue");
  69. TimeSpan timeout = new TimeSpan (0, 0, 0, 0, (int) millisecondsTimeOutInterval);
  70. RegisteredWaitHandle waiter = new RegisteredWaitHandle (waitObject, callBack, state,
  71. timeout, executeOnlyOnce);
  72. QueueUserWorkItem (new WaitCallback (waiter.Wait), null);
  73. return waiter;
  74. }
  75. public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
  76. WaitOrTimerCallback callBack,
  77. object state,
  78. TimeSpan timeout,
  79. bool executeOnlyOnce)
  80. {
  81. return RegisterWaitForSingleObject (waitObject, callBack, state,
  82. (long) timeout.TotalMilliseconds, executeOnlyOnce);
  83. }
  84. [CLSCompliant(false)]
  85. public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
  86. WaitOrTimerCallback callBack,
  87. object state,
  88. uint millisecondsTimeOutInterval,
  89. bool executeOnlyOnce)
  90. {
  91. return RegisterWaitForSingleObject (waitObject, callBack, state,
  92. (long) millisecondsTimeOutInterval, executeOnlyOnce);
  93. }
  94. public static bool UnsafeQueueUserWorkItem (WaitCallback callback, object state)
  95. {
  96. IAsyncResult ar = callback.BeginInvoke (state, null, null);
  97. if (ar == null)
  98. return false;
  99. return true;
  100. }
  101. [MonoTODO]
  102. public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
  103. WaitOrTimerCallback callBack, object state, int millisecondsTimeOutInterval,
  104. bool executeOnlyOnce)
  105. {
  106. throw new NotImplementedException ();
  107. }
  108. [MonoTODO]
  109. public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
  110. WaitOrTimerCallback callBack, object state, long millisecondsTimeOutInterval,
  111. bool executeOnlyOnce)
  112. {
  113. throw new NotImplementedException ();
  114. }
  115. [MonoTODO]
  116. public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
  117. WaitOrTimerCallback callBack, object state, TimeSpan timeout,
  118. bool executeOnlyOnce)
  119. {
  120. throw new NotImplementedException ();
  121. }
  122. [MonoTODO]
  123. [CLSCompliant (false)]
  124. public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
  125. WaitOrTimerCallback callBack, object state, uint millisecondsTimeOutInterval,
  126. bool executeOnlyOnce)
  127. {
  128. throw new NotImplementedException ();
  129. }
  130. }
  131. }