RemotingThreadPool.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #if TARGET_JVM
  45. volatile
  46. #endif
  47. bool stopped = false;
  48. static object globalLock = new object ();
  49. static RemotingThreadPool sharedPool;
  50. public static RemotingThreadPool GetSharedPool ()
  51. {
  52. lock (globalLock) {
  53. if (sharedPool == null)
  54. sharedPool = new RemotingThreadPool ();
  55. sharedPool.poolUsers++;
  56. }
  57. return sharedPool;
  58. }
  59. public void Free ()
  60. {
  61. lock (globalLock) {
  62. if (--poolUsers > 0)
  63. return;
  64. lock (workItems) {
  65. stopped = true;
  66. threadDone.Set ();
  67. workItems.Clear ();
  68. foreach (Thread t in runningThreads)
  69. #if !TARGET_JVM
  70. t.Abort ();
  71. #else
  72. t.Interrupt();
  73. #endif
  74. runningThreads.Clear ();
  75. }
  76. if (this == sharedPool)
  77. sharedPool = null;
  78. }
  79. }
  80. public bool RunThread (ThreadStart threadStart)
  81. {
  82. lock (workItems) {
  83. if (stopped)
  84. throw new RemotingException ("Server channel stopped.");
  85. if (freeThreads > 0) {
  86. freeThreads--;
  87. workItems.Enqueue (threadStart);
  88. Monitor.Pulse (workItems);
  89. return true;
  90. } else if (runningThreads.Count < MinThreads) {
  91. workItems.Enqueue (threadStart);
  92. StartPoolThread ();
  93. return true;
  94. }
  95. }
  96. // Try again some ms later, and if there are still no free threads,
  97. // then create a new one
  98. threadDone.WaitOne (PoolGrowDelay, false);
  99. lock (workItems) {
  100. if (stopped)
  101. throw new RemotingException ("Server channel stopped.");
  102. if (freeThreads > 0) {
  103. freeThreads--;
  104. workItems.Enqueue (threadStart);
  105. Monitor.Pulse (workItems);
  106. } else {
  107. if (runningThreads.Count >= ThreadLimit)
  108. return false;
  109. workItems.Enqueue (threadStart);
  110. StartPoolThread ();
  111. }
  112. }
  113. return true;
  114. }
  115. void StartPoolThread ()
  116. {
  117. Thread thread = new Thread (new ThreadStart (PoolThread));
  118. runningThreads.Add (thread);
  119. thread.IsBackground = true;
  120. thread.Start ();
  121. }
  122. void PoolThread ()
  123. {
  124. #if !TARGET_JVM
  125. while (true) {
  126. #else
  127. while (!stopped)
  128. {
  129. #endif
  130. ThreadStart work = null;
  131. do {
  132. lock (workItems) {
  133. if (workItems.Count > 0)
  134. work = (ThreadStart) workItems.Dequeue ();
  135. else {
  136. freeThreads ++;
  137. threadDone.Set ();
  138. if (!Monitor.Wait (workItems, ThreadWaitTime)) {
  139. // Maybe it timed out when the work was being queued.
  140. if (workItems.Count > 0) {
  141. work = (ThreadStart) workItems.Dequeue ();
  142. } else {
  143. freeThreads --;
  144. if (freeThreads == 0) threadDone.Reset ();
  145. runningThreads.Remove (Thread.CurrentThread);
  146. return;
  147. }
  148. }
  149. }
  150. }
  151. } while (work == null);
  152. try {
  153. work ();
  154. }
  155. catch (Exception ex)
  156. {
  157. #if DEBUG
  158. Console.WriteLine("The exception was caught during RemotingThreadPool.PoolThread - work: {0}, {1}", ex.GetType(), ex.Message);
  159. #endif
  160. }
  161. }
  162. }
  163. }
  164. }