RemotingThreadPool.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // System.Runtime.Remoting.Channels.RemotingThreadPool.cs
  3. //
  4. // Author: Lluis Sanchez Gual ([email protected])
  5. //
  6. // 2005 (C) Copyright, Novell, Inc.
  7. //
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Threading;
  31. namespace System.Runtime.Remoting.Channels
  32. {
  33. internal class RemotingThreadPool
  34. {
  35. const int ThreadLimit = 1000;
  36. const int ThreadWaitTime = 20000; //ms
  37. const int PoolGrowDelay = 500; // ms
  38. const int MinThreads = 3;
  39. int freeThreads;
  40. int poolUsers;
  41. Queue workItems = new Queue ();
  42. AutoResetEvent threadDone = new AutoResetEvent (false);
  43. ArrayList runningThreads = new ArrayList ();
  44. bool stopped = false;
  45. static object globalLock = new object ();
  46. static RemotingThreadPool sharedPool;
  47. public static RemotingThreadPool GetSharedPool ()
  48. {
  49. lock (globalLock) {
  50. if (sharedPool == null)
  51. sharedPool = new RemotingThreadPool ();
  52. sharedPool.poolUsers++;
  53. }
  54. return sharedPool;
  55. }
  56. public void Free ()
  57. {
  58. lock (globalLock) {
  59. if (--poolUsers > 0)
  60. return;
  61. lock (workItems) {
  62. stopped = true;
  63. threadDone.Set ();
  64. workItems.Clear ();
  65. foreach (Thread t in runningThreads)
  66. t.Abort ();
  67. runningThreads.Clear ();
  68. }
  69. if (this == sharedPool)
  70. sharedPool = null;
  71. }
  72. }
  73. public bool RunThread (ThreadStart threadStart)
  74. {
  75. lock (workItems) {
  76. if (stopped)
  77. throw new RemotingException ("Server channel stopped.");
  78. if (freeThreads > 0) {
  79. freeThreads--;
  80. workItems.Enqueue (threadStart);
  81. Monitor.Pulse (workItems);
  82. return true;
  83. } else if (runningThreads.Count < MinThreads) {
  84. workItems.Enqueue (threadStart);
  85. StartPoolThread ();
  86. return true;
  87. }
  88. }
  89. // Try again some ms later, and if there are still no free threads,
  90. // then create a new one
  91. threadDone.WaitOne (PoolGrowDelay, false);
  92. lock (workItems) {
  93. if (stopped)
  94. throw new RemotingException ("Server channel stopped.");
  95. if (freeThreads > 0) {
  96. freeThreads--;
  97. workItems.Enqueue (threadStart);
  98. Monitor.Pulse (workItems);
  99. } else {
  100. if (runningThreads.Count >= ThreadLimit)
  101. return false;
  102. workItems.Enqueue (threadStart);
  103. StartPoolThread ();
  104. }
  105. }
  106. return true;
  107. }
  108. void StartPoolThread ()
  109. {
  110. Thread thread = new Thread (new ThreadStart (PoolThread));
  111. runningThreads.Add (thread);
  112. thread.IsBackground = true;
  113. thread.Start ();
  114. }
  115. void PoolThread ()
  116. {
  117. while (true) {
  118. ThreadStart work = null;
  119. do {
  120. lock (workItems) {
  121. if (workItems.Count > 0)
  122. work = (ThreadStart) workItems.Dequeue ();
  123. else {
  124. freeThreads ++;
  125. threadDone.Set ();
  126. if (!Monitor.Wait (workItems, ThreadWaitTime)) {
  127. // Maybe it timed out when the work was being queued.
  128. if (workItems.Count > 0) {
  129. work = (ThreadStart) workItems.Dequeue ();
  130. } else {
  131. freeThreads --;
  132. if (freeThreads == 0) threadDone.Reset ();
  133. runningThreads.Remove (Thread.CurrentThread);
  134. return;
  135. }
  136. }
  137. }
  138. }
  139. } while (work == null);
  140. try {
  141. work ();
  142. } catch {
  143. // Can't do anything with the exception
  144. }
  145. }
  146. }
  147. }
  148. }