ThreadPool.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using PaintDotNet.SystemLayer;
  2. using System;
  3. using System.Collections;
  4. using System.Threading;
  5. namespace PaintDotNet.Threading
  6. {
  7. /// <summary>
  8. /// Uses the .NET ThreadPool to do our own type of thread pool. The main difference
  9. /// here is that we limit our usage of the thread pool, and that we can also drain
  10. /// the threads we have ("fence"). The default maximum number of threads is
  11. /// Processor.LogicalCpuCount.
  12. /// </summary>
  13. public class ThreadPool
  14. {
  15. private static ThreadPool global = new ThreadPool(2 * Processor.LogicalCpuCount);
  16. public static ThreadPool Global
  17. {
  18. get
  19. {
  20. return global;
  21. }
  22. }
  23. private ArrayList exceptions = ArrayList.Synchronized(new ArrayList());
  24. private bool useFXTheadPool;
  25. public static int MinimumCount
  26. {
  27. get
  28. {
  29. return WaitableCounter.MinimumCount;
  30. }
  31. }
  32. public static int MaximumCount
  33. {
  34. get
  35. {
  36. return WaitableCounter.MaximumCount;
  37. }
  38. }
  39. public Exception[] Exceptions
  40. {
  41. get
  42. {
  43. return (Exception[])this.exceptions.ToArray(typeof(Exception));
  44. }
  45. }
  46. public void ClearExceptions()
  47. {
  48. exceptions.Clear();
  49. }
  50. public void DrainExceptions()
  51. {
  52. if (this.exceptions.Count > 0)
  53. {
  54. throw new WorkerThreadException("Worker thread threw an exception", (Exception)this.exceptions[0]);
  55. }
  56. ClearExceptions();
  57. }
  58. private WaitableCounter counter;
  59. public ThreadPool()
  60. : this(Processor.LogicalCpuCount)
  61. {
  62. }
  63. public ThreadPool(int maxThreads)
  64. : this(maxThreads, true)
  65. {
  66. }
  67. public ThreadPool(int maxThreads, bool useFXThreadPool)
  68. {
  69. if (maxThreads < MinimumCount || maxThreads > MaximumCount)
  70. {
  71. throw new ArgumentOutOfRangeException("maxThreads", "must be between " + MinimumCount.ToString() + " and " + MaximumCount.ToString() + " inclusive");
  72. }
  73. this.counter = new WaitableCounter(maxThreads);
  74. this.useFXTheadPool = useFXThreadPool;
  75. }
  76. /*
  77. private sealed class FunctionCallTrampoline
  78. {
  79. private Delegate theDelegate;
  80. private object[] parameters;
  81. public void WaitCallback(object ignored)
  82. {
  83. theDelegate.DynamicInvoke(this.parameters);
  84. }
  85. public FunctionCallTrampoline(Delegate theDelegate, object[] parameters)
  86. {
  87. this.theDelegate = theDelegate;
  88. this.parameters = parameters;
  89. }
  90. }
  91. public void QueueFunctionCall(Delegate theDelegate, params object[] parameters)
  92. {
  93. FunctionCallTrampoline fct = new FunctionCallTrampoline(theDelegate, parameters);
  94. QueueUserWorkItem(fct.WaitCallback, null);
  95. }
  96. */
  97. public void QueueUserWorkItem(WaitCallback callback)
  98. {
  99. QueueUserWorkItem(callback, null);
  100. }
  101. public void QueueUserWorkItem(WaitCallback callback, object state)
  102. {
  103. IDisposable token = counter.AcquireToken();
  104. ThreadWrapperContext twc = new ThreadWrapperContext(callback, state, token, this.exceptions);
  105. if (this.useFXTheadPool)
  106. {
  107. System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback(twc.ThreadWrapper), twc);
  108. }
  109. else
  110. {
  111. Thread thread = new Thread(new ThreadStart(twc.ThreadWrapper));
  112. thread.IsBackground = true;
  113. thread.Start();
  114. }
  115. }
  116. public bool IsDrained(uint msTimeout)
  117. {
  118. bool result = counter.IsEmpty(msTimeout);
  119. if (result)
  120. {
  121. Drain();
  122. }
  123. return result;
  124. }
  125. public bool IsDrained()
  126. {
  127. return IsDrained(0);
  128. }
  129. public void Drain()
  130. {
  131. counter.WaitForEmpty();
  132. DrainExceptions();
  133. }
  134. private sealed class ThreadWrapperContext
  135. {
  136. private WaitCallback callback;
  137. private object context;
  138. private IDisposable counterToken;
  139. private ArrayList exceptionsBucket;
  140. public ThreadWrapperContext(WaitCallback callback, object context,
  141. IDisposable counterToken, ArrayList exceptionsBucket)
  142. {
  143. this.callback = callback;
  144. this.context = context;
  145. this.counterToken = counterToken;
  146. this.exceptionsBucket = exceptionsBucket;
  147. }
  148. public void ThreadWrapper()
  149. {
  150. using (IDisposable token = this.counterToken)
  151. {
  152. try
  153. {
  154. this.callback(this.context);
  155. }
  156. catch (Exception ex)
  157. {
  158. this.exceptionsBucket.Add(ex);
  159. }
  160. }
  161. }
  162. public void ThreadWrapper(object state)
  163. {
  164. ThreadWrapper();
  165. }
  166. }
  167. }
  168. }