15
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3 using System.Linq;
|
|
4 using System.Text;
|
|
5 using System.Threading;
|
|
6 using System.Diagnostics;
|
|
7
|
|
8 namespace Implab.Parallels {
|
|
9 public abstract class DispatchPool<TUnit> : IDisposable {
|
|
10 readonly int m_minThreads;
|
|
11 readonly int m_maxThreads;
|
20
|
12 int m_createdThreads = 0;
|
|
13 int m_activeThreads = 0;
|
|
14 int m_sleepingThreads = 0;
|
15
|
15 int m_maxRunningThreads = 0;
|
|
16 int m_exitRequired = 0;
|
20
|
17 int m_releaseTimeout = 100; // timeout while the working thread will wait for the new tasks before exit
|
15
|
18 AutoResetEvent m_hasTasks = new AutoResetEvent(false);
|
|
19
|
|
20 protected DispatchPool(int min, int max) {
|
|
21 if (min < 0)
|
|
22 throw new ArgumentOutOfRangeException("min");
|
|
23 if (max <= 0)
|
|
24 throw new ArgumentOutOfRangeException("max");
|
|
25
|
|
26 if (min > max)
|
|
27 min = max;
|
|
28 m_minThreads = min;
|
|
29 m_maxThreads = max;
|
|
30 }
|
|
31
|
|
32 protected DispatchPool(int threads)
|
|
33 : this(threads, threads) {
|
|
34 }
|
|
35
|
|
36 protected DispatchPool() {
|
|
37 int maxThreads, maxCP;
|
|
38 ThreadPool.GetMaxThreads(out maxThreads, out maxCP);
|
|
39
|
|
40 m_minThreads = 0;
|
|
41 m_maxThreads = maxThreads;
|
|
42 }
|
|
43
|
|
44 protected void InitPool() {
|
|
45 for (int i = 0; i < m_minThreads; i++)
|
|
46 StartWorker();
|
|
47 }
|
|
48
|
20
|
49 public int PoolSize {
|
15
|
50 get {
|
20
|
51 return m_createdThreads;
|
|
52 }
|
|
53 }
|
|
54
|
|
55 public int ActiveThreads {
|
|
56 get {
|
|
57 return m_activeThreads;
|
15
|
58 }
|
|
59 }
|
|
60
|
|
61 public int MaxRunningThreads {
|
|
62 get {
|
|
63 return m_maxRunningThreads;
|
|
64 }
|
|
65 }
|
|
66
|
|
67 protected bool IsDisposed {
|
|
68 get {
|
|
69 return m_exitRequired != 0;
|
|
70 }
|
|
71 }
|
|
72
|
17
|
73 protected abstract bool TryDequeue(out TUnit unit);
|
|
74
|
|
75 protected virtual bool ExtendPool() {
|
20
|
76 if (m_sleepingThreads == 0)
|
|
77 // no sleeping workers are available
|
|
78 // try create one
|
|
79 return StartWorker();
|
|
80 else {
|
|
81 // we can get here a race condition when several threads asks to extend pool
|
|
82 // and some sleaping threads are exited due timeout but they are still counted as sleeping
|
|
83 // in that case all of this threads could exit except one
|
|
84 WakePool();
|
17
|
85 return true;
|
20
|
86 }
|
|
87
|
17
|
88 }
|
|
89
|
|
90 /// <summary>
|
|
91 /// Запускает либо новый поток, если раньше не было ни одного потока, либо устанавливает событие пробуждение одного спящего потока
|
|
92 /// </summary>
|
|
93 protected void WakePool() {
|
|
94 m_hasTasks.Set(); // wake sleeping thread;
|
|
95
|
|
96 if (AllocateThreadSlot(1)) {
|
20
|
97 // if there were no threads in the pool
|
17
|
98 var worker = new Thread(this.Worker);
|
|
99 worker.IsBackground = true;
|
|
100 worker.Start();
|
|
101 }
|
|
102 }
|
|
103
|
20
|
104 bool Sleep(int timeout) {
|
|
105 Interlocked.Increment(ref m_sleepingThreads);
|
|
106 var result = m_hasTasks.WaitOne(timeout);
|
|
107 Interlocked.Decrement(ref m_sleepingThreads);
|
|
108 return result;
|
|
109 }
|
|
110
|
|
111 protected virtual bool Suspend() {
|
|
112 //no tasks left, exit if the thread is no longer needed
|
|
113 bool last;
|
|
114 bool requestExit;
|
|
115
|
|
116 if (m_releaseTimeout > 0)
|
|
117 requestExit = !Sleep(m_releaseTimeout);
|
|
118 else
|
|
119 requestExit = true;
|
|
120
|
|
121
|
|
122 if (requestExit && ReleaseThreadSlot(out last)) {
|
|
123 // in case at the moment the last thread was being released
|
|
124 // a new task was added to the queue, we need to try
|
|
125 // to revoke the thread to avoid the situation when the task is left unprocessed
|
|
126 if (last && m_hasTasks.WaitOne(0)) {
|
|
127 if (AllocateThreadSlot(1))
|
|
128 return true; // spin again...
|
|
129 else
|
|
130 // we failed to reallocate the first slot for this thread
|
|
131 // therefore we need to release the event
|
|
132 m_hasTasks.Set();
|
|
133 }
|
|
134
|
|
135 return false;
|
|
136 }
|
|
137
|
|
138 Sleep(-1);
|
|
139
|
|
140 return true;
|
17
|
141 }
|
|
142
|
|
143 #region thread slots traits
|
|
144
|
|
145 bool AllocateThreadSlot() {
|
16
|
146 int current;
|
15
|
147 // use spins to allocate slot for the new thread
|
|
148 do {
|
20
|
149 current = m_createdThreads;
|
15
|
150 if (current >= m_maxThreads || m_exitRequired != 0)
|
|
151 // no more slots left or the pool has been disposed
|
|
152 return false;
|
20
|
153 } while (current != Interlocked.CompareExchange(ref m_createdThreads, current + 1, current));
|
15
|
154
|
17
|
155 UpdateMaxThreads(current + 1);
|
|
156
|
|
157 return true;
|
|
158 }
|
15
|
159
|
17
|
160 bool AllocateThreadSlot(int desired) {
|
20
|
161 if (desired - 1 != Interlocked.CompareExchange(ref m_createdThreads, desired, desired - 1))
|
17
|
162 return false;
|
|
163
|
|
164 UpdateMaxThreads(desired);
|
15
|
165
|
17
|
166 return true;
|
|
167 }
|
|
168
|
|
169 bool ReleaseThreadSlot(out bool last) {
|
|
170 last = false;
|
|
171 int current;
|
|
172 // use spins to release slot for the new thread
|
|
173 do {
|
20
|
174 current = m_createdThreads;
|
17
|
175 if (current <= m_minThreads && m_exitRequired == 0)
|
|
176 // the thread is reserved
|
|
177 return false;
|
20
|
178 } while (current != Interlocked.CompareExchange(ref m_createdThreads, current - 1, current));
|
17
|
179
|
|
180 last = (current == 1);
|
15
|
181
|
|
182 return true;
|
|
183 }
|
|
184
|
17
|
185 /// <summary>
|
|
186 /// releases thread slot unconditionally, used during cleanup
|
|
187 /// </summary>
|
|
188 /// <returns>true - no more threads left</returns>
|
|
189 bool ReleaseThreadSlotAnyway() {
|
20
|
190 var left = Interlocked.Decrement(ref m_createdThreads);
|
17
|
191 return left == 0;
|
15
|
192 }
|
|
193
|
17
|
194 void UpdateMaxThreads(int count) {
|
|
195 int max;
|
16
|
196 do {
|
17
|
197 max = m_maxRunningThreads;
|
|
198 if (max >= count)
|
|
199 break;
|
|
200 } while(max != Interlocked.CompareExchange(ref m_maxRunningThreads, count, max));
|
16
|
201 }
|
|
202
|
17
|
203 #endregion
|
|
204
|
|
205 bool StartWorker() {
|
|
206 if (AllocateThreadSlot()) {
|
|
207 // slot successfully allocated
|
|
208 var worker = new Thread(this.Worker);
|
|
209 worker.IsBackground = true;
|
|
210 worker.Start();
|
|
211
|
|
212 return true;
|
|
213 } else {
|
|
214 return false;
|
|
215 }
|
16
|
216 }
|
|
217
|
15
|
218 bool FetchTask(out TUnit unit) {
|
|
219 do {
|
|
220 // exit if requested
|
|
221 if (m_exitRequired != 0) {
|
|
222 // release the thread slot
|
20
|
223 Interlocked.Decrement(ref m_activeThreads);
|
17
|
224 if (ReleaseThreadSlotAnyway()) // it was the last worker
|
15
|
225 m_hasTasks.Dispose();
|
|
226 else
|
17
|
227 m_hasTasks.Set(); // wake next worker
|
15
|
228 unit = default(TUnit);
|
|
229 return false;
|
|
230 }
|
|
231
|
|
232 // fetch task
|
|
233 if (TryDequeue(out unit)) {
|
17
|
234 ExtendPool();
|
15
|
235 return true;
|
|
236 }
|
|
237
|
20
|
238 Interlocked.Decrement(ref m_activeThreads);
|
15
|
239
|
16
|
240 // entering suspend state
|
|
241 // keep this thread and wait
|
20
|
242 if (!Suspend())
|
|
243 return false;
|
|
244
|
|
245 Interlocked.Increment(ref m_activeThreads);
|
15
|
246 } while (true);
|
|
247 }
|
|
248
|
|
249 protected abstract void InvokeUnit(TUnit unit);
|
|
250
|
|
251 void Worker() {
|
|
252 TUnit unit;
|
20
|
253 Interlocked.Increment(ref m_activeThreads);
|
15
|
254 while (FetchTask(out unit))
|
|
255 InvokeUnit(unit);
|
|
256 }
|
|
257
|
|
258 protected virtual void Dispose(bool disposing) {
|
|
259 if (disposing) {
|
|
260 if (m_exitRequired == 0) {
|
|
261 if (Interlocked.CompareExchange(ref m_exitRequired, 1, 0) != 0)
|
|
262 return;
|
|
263
|
|
264 // wake sleeping threads
|
|
265 m_hasTasks.Set();
|
|
266 GC.SuppressFinalize(this);
|
|
267 }
|
|
268 }
|
|
269 }
|
|
270
|
|
271 public void Dispose() {
|
|
272 Dispose(true);
|
|
273 }
|
|
274
|
|
275 ~DispatchPool() {
|
|
276 Dispose(false);
|
|
277 }
|
|
278 }
|
|
279 }
|