@@ -1,333 +1,333 | |||
|
1 | 1 | using System; |
|
2 | 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; |
|
3 | 3 | using System.Reflection; |
|
4 | 4 | using System.Threading; |
|
5 | 5 | using Implab.Parallels; |
|
6 | 6 | |
|
7 | 7 | namespace Implab.Test { |
|
8 | 8 | [TestClass] |
|
9 | 9 | public class AsyncTests { |
|
10 | 10 | [TestMethod] |
|
11 | 11 | public void ResolveTest() { |
|
12 | 12 | int res = -1; |
|
13 | 13 | var p = new Promise<int>(); |
|
14 | 14 | p.Then(x => res = x); |
|
15 | 15 | p.Resolve(100); |
|
16 | 16 | |
|
17 | 17 | Assert.AreEqual(100, res); |
|
18 | 18 | } |
|
19 | 19 | |
|
20 | 20 | [TestMethod] |
|
21 | 21 | public void RejectTest() { |
|
22 | 22 | int res = -1; |
|
23 | 23 | Exception err = null; |
|
24 | 24 | |
|
25 | 25 | var p = new Promise<int>(); |
|
26 | 26 | p.Then(x => res = x, e => err = e); |
|
27 | 27 | p.Reject(new ApplicationException("error")); |
|
28 | 28 | |
|
29 | 29 | Assert.AreEqual(res, -1); |
|
30 | 30 | Assert.AreEqual(err.Message, "error"); |
|
31 | 31 | |
|
32 | 32 | } |
|
33 | 33 | |
|
34 | 34 | [TestMethod] |
|
35 | 35 | public void JoinSuccessTest() { |
|
36 | 36 | var p = new Promise<int>(); |
|
37 | 37 | p.Resolve(100); |
|
38 | 38 | Assert.AreEqual(p.Join(), 100); |
|
39 | 39 | } |
|
40 | 40 | |
|
41 | 41 | [TestMethod] |
|
42 | 42 | public void JoinFailTest() { |
|
43 | 43 | var p = new Promise<int>(); |
|
44 | 44 | p.Reject(new ApplicationException("failed")); |
|
45 | 45 | |
|
46 | 46 | try { |
|
47 | 47 | p.Join(); |
|
48 | 48 | throw new ApplicationException("WRONG!"); |
|
49 | 49 | } catch (TargetInvocationException err) { |
|
50 | 50 | Assert.AreEqual(err.InnerException.Message, "failed"); |
|
51 | 51 | } catch { |
|
52 | 52 | Assert.Fail("Got wrong excaption"); |
|
53 | 53 | } |
|
54 | 54 | } |
|
55 | 55 | |
|
56 | 56 | [TestMethod] |
|
57 | 57 | public void MapTest() { |
|
58 | 58 | var p = new Promise<int>(); |
|
59 | 59 | |
|
60 | 60 | var p2 = p.Map(x => x.ToString()); |
|
61 | 61 | p.Resolve(100); |
|
62 | 62 | |
|
63 | 63 | Assert.AreEqual(p2.Join(), "100"); |
|
64 | 64 | } |
|
65 | 65 | |
|
66 | 66 | [TestMethod] |
|
67 | 67 | public void FixErrorTest() { |
|
68 | 68 | var p = new Promise<int>(); |
|
69 | 69 | |
|
70 | 70 | var p2 = p.Error(e => 101); |
|
71 | 71 | |
|
72 | 72 | p.Reject(new Exception()); |
|
73 | 73 | |
|
74 | 74 | Assert.AreEqual(p2.Join(), 101); |
|
75 | 75 | } |
|
76 | 76 | |
|
77 | 77 | [TestMethod] |
|
78 | 78 | public void ChainTest() { |
|
79 | 79 | var p1 = new Promise<int>(); |
|
80 | 80 | |
|
81 | 81 | var p3 = p1.Chain(x => { |
|
82 | 82 | var p2 = new Promise<string>(); |
|
83 | 83 | p2.Resolve(x.ToString()); |
|
84 | 84 | return p2; |
|
85 | 85 | }); |
|
86 | 86 | |
|
87 | 87 | p1.Resolve(100); |
|
88 | 88 | |
|
89 | 89 | Assert.AreEqual(p3.Join(), "100"); |
|
90 | 90 | } |
|
91 | 91 | |
|
92 | 92 | [TestMethod] |
|
93 | 93 | public void PoolTest() { |
|
94 | 94 | var pid = Thread.CurrentThread.ManagedThreadId; |
|
95 | 95 | var p = AsyncPool.Invoke(() => Thread.CurrentThread.ManagedThreadId); |
|
96 | 96 | |
|
97 | 97 | Assert.AreNotEqual(pid, p.Join()); |
|
98 | 98 | } |
|
99 | 99 | |
|
100 | 100 | [TestMethod] |
|
101 | 101 | public void WorkerPoolSizeTest() { |
|
102 | 102 | var pool = new WorkerPool(5, 10, 0); |
|
103 | 103 | |
|
104 |
Assert.AreEqual(5, pool. |
|
|
104 | Assert.AreEqual(5, pool.PoolSize); | |
|
105 | 105 | |
|
106 | 106 | pool.Invoke(() => { Thread.Sleep(1000000); return 10; }); |
|
107 | 107 | pool.Invoke(() => { Thread.Sleep(1000000); return 10; }); |
|
108 | 108 | pool.Invoke(() => { Thread.Sleep(1000000); return 10; }); |
|
109 | 109 | |
|
110 |
Assert.AreEqual(5, pool. |
|
|
110 | Assert.AreEqual(5, pool.PoolSize); | |
|
111 | 111 | |
|
112 | 112 | for (int i = 0; i < 100; i++) |
|
113 | 113 | pool.Invoke(() => { Thread.Sleep(1000000); return 10; }); |
|
114 | 114 | Thread.Sleep(100); |
|
115 |
Assert.AreEqual(10, pool. |
|
|
115 | Assert.AreEqual(10, pool.PoolSize); | |
|
116 | 116 | |
|
117 | 117 | pool.Dispose(); |
|
118 | 118 | } |
|
119 | 119 | |
|
120 | 120 | [TestMethod] |
|
121 | 121 | public void WorkerPoolCorrectTest() { |
|
122 | 122 | var pool = new WorkerPool(0,1000,100); |
|
123 | 123 | |
|
124 | 124 | int iterations = 1000; |
|
125 | 125 | int pending = iterations; |
|
126 | 126 | var stop = new ManualResetEvent(false); |
|
127 | 127 | |
|
128 | 128 | var count = 0; |
|
129 | 129 | for (int i = 0; i < iterations; i++) { |
|
130 | 130 | pool |
|
131 | 131 | .Invoke(() => 1) |
|
132 | 132 | .Then(x => Interlocked.Add(ref count, x)) |
|
133 | 133 | .Then(x => Math.Log10(x)) |
|
134 | 134 | .Anyway(() => { |
|
135 | 135 | Interlocked.Decrement(ref pending); |
|
136 | 136 | if (pending == 0) |
|
137 | 137 | stop.Set(); |
|
138 | 138 | }); |
|
139 | 139 | } |
|
140 | 140 | |
|
141 | 141 | stop.WaitOne(); |
|
142 | 142 | |
|
143 | 143 | Assert.AreEqual(iterations, count); |
|
144 | 144 | Console.WriteLine("Max threads: {0}", pool.MaxRunningThreads); |
|
145 | 145 | pool.Dispose(); |
|
146 | 146 | |
|
147 | 147 | } |
|
148 | 148 | |
|
149 | 149 | [TestMethod] |
|
150 | 150 | public void WorkerPoolDisposeTest() { |
|
151 | 151 | var pool = new WorkerPool(5, 20); |
|
152 |
Assert.AreEqual(5, pool. |
|
|
152 | Assert.AreEqual(5, pool.PoolSize); | |
|
153 | 153 | pool.Dispose(); |
|
154 |
Thread.Sleep( |
|
|
155 |
Assert.AreEqual(0, pool. |
|
|
154 | Thread.Sleep(200); | |
|
155 | Assert.AreEqual(0, pool.PoolSize); | |
|
156 | 156 | pool.Dispose(); |
|
157 | 157 | } |
|
158 | 158 | |
|
159 | 159 | [TestMethod] |
|
160 | 160 | public void MTQueueTest() { |
|
161 | 161 | var queue = new MTQueue<int>(); |
|
162 | 162 | int res; |
|
163 | 163 | |
|
164 | 164 | queue.Enqueue(10); |
|
165 | 165 | Assert.IsTrue(queue.TryDequeue(out res)); |
|
166 | 166 | Assert.AreEqual(10, res); |
|
167 | 167 | Assert.IsFalse(queue.TryDequeue(out res)); |
|
168 | 168 | |
|
169 | 169 | for (int i = 0; i < 1000; i++) |
|
170 | 170 | queue.Enqueue(i); |
|
171 | 171 | |
|
172 | 172 | for (int i = 0; i < 1000; i++) { |
|
173 | 173 | queue.TryDequeue(out res); |
|
174 | 174 | Assert.AreEqual(i, res); |
|
175 | 175 | } |
|
176 | 176 | |
|
177 | 177 | int writers = 0; |
|
178 | 178 | int readers = 0; |
|
179 | 179 | var stop = new ManualResetEvent(false); |
|
180 | 180 | int total = 0; |
|
181 | 181 | |
|
182 | 182 | int itemsPerWriter = 1000; |
|
183 | 183 | int writersCount = 3; |
|
184 | 184 | |
|
185 | 185 | for (int i = 0; i < writersCount; i++) { |
|
186 | 186 | Interlocked.Increment(ref writers); |
|
187 | 187 | var wn = i; |
|
188 | 188 | AsyncPool |
|
189 | 189 | .InvokeNewThread(() => { |
|
190 | 190 | for (int ii = 0; ii < itemsPerWriter; ii++) { |
|
191 | 191 | queue.Enqueue(1); |
|
192 | 192 | } |
|
193 | 193 | return 1; |
|
194 | 194 | }) |
|
195 | 195 | .Anyway(() => Interlocked.Decrement(ref writers)); |
|
196 | 196 | } |
|
197 | 197 | |
|
198 | 198 | for (int i = 0; i < 10; i++) { |
|
199 | 199 | Interlocked.Increment(ref readers); |
|
200 | 200 | var wn = i; |
|
201 | 201 | AsyncPool |
|
202 | 202 | .InvokeNewThread(() => { |
|
203 | 203 | int t; |
|
204 | 204 | do { |
|
205 | 205 | while (queue.TryDequeue(out t)) |
|
206 | 206 | Interlocked.Add(ref total, t); |
|
207 | 207 | } while (writers > 0); |
|
208 | 208 | return 1; |
|
209 | 209 | }) |
|
210 | 210 | .Anyway(() => { |
|
211 | 211 | Interlocked.Decrement(ref readers); |
|
212 | 212 | if (readers == 0) |
|
213 | 213 | stop.Set(); |
|
214 | 214 | }); |
|
215 | 215 | } |
|
216 | 216 | |
|
217 | 217 | stop.WaitOne(); |
|
218 | 218 | |
|
219 | 219 | Assert.AreEqual(itemsPerWriter * writersCount, total); |
|
220 | 220 | } |
|
221 | 221 | |
|
222 | 222 | [TestMethod] |
|
223 | 223 | public void ParallelMapTest() { |
|
224 | 224 | |
|
225 | 225 | int count = 100000; |
|
226 | 226 | |
|
227 | 227 | double[] args = new double[count]; |
|
228 | 228 | var rand = new Random(); |
|
229 | 229 | |
|
230 | 230 | for (int i = 0; i < count; i++) |
|
231 | 231 | args[i] = rand.NextDouble(); |
|
232 | 232 | |
|
233 | 233 | var t = Environment.TickCount; |
|
234 | 234 | var res = args.ParallelMap(x => Math.Sin(x*x), 4).Join(); |
|
235 | 235 | |
|
236 | 236 | Console.WriteLine("Map complete in {0} ms", Environment.TickCount - t); |
|
237 | 237 | |
|
238 | 238 | t = Environment.TickCount; |
|
239 | 239 | for (int i = 0; i < count; i++) |
|
240 | 240 | Assert.AreEqual(Math.Sin(args[i] * args[i]), res[i]); |
|
241 | 241 | Console.WriteLine("Verified in {0} ms", Environment.TickCount - t); |
|
242 | 242 | } |
|
243 | 243 | |
|
244 | 244 | [TestMethod] |
|
245 | 245 | public void ChainedMapTest() { |
|
246 | 246 | |
|
247 |
using (var pool = new WorkerPool( |
|
|
247 | using (var pool = new WorkerPool(0,100,0)) { | |
|
248 | 248 | int count = 10000; |
|
249 | 249 | |
|
250 | 250 | double[] args = new double[count]; |
|
251 | 251 | var rand = new Random(); |
|
252 | 252 | |
|
253 | 253 | for (int i = 0; i < count; i++) |
|
254 | 254 | args[i] = rand.NextDouble(); |
|
255 | 255 | |
|
256 | 256 | var t = Environment.TickCount; |
|
257 | 257 | var res = args |
|
258 | 258 | .ChainedMap( |
|
259 | 259 | x => pool.Invoke( |
|
260 | 260 | () => Math.Sin(x * x) |
|
261 | 261 | ), |
|
262 | 262 | 4 |
|
263 | 263 | ) |
|
264 | 264 | .Join(); |
|
265 | 265 | |
|
266 | 266 | Console.WriteLine("Map complete in {0} ms", Environment.TickCount - t); |
|
267 | 267 | |
|
268 | 268 | t = Environment.TickCount; |
|
269 | 269 | for (int i = 0; i < count; i++) |
|
270 | 270 | Assert.AreEqual(Math.Sin(args[i] * args[i]), res[i]); |
|
271 | 271 | Console.WriteLine("Verified in {0} ms", Environment.TickCount - t); |
|
272 | 272 | Console.WriteLine("Max workers: {0}", pool.MaxRunningThreads); |
|
273 | 273 | } |
|
274 | 274 | } |
|
275 | 275 | |
|
276 | 276 | [TestMethod] |
|
277 | 277 | public void ParallelForEachTest() { |
|
278 | 278 | |
|
279 | 279 | int count = 100000; |
|
280 | 280 | |
|
281 | 281 | int[] args = new int[count]; |
|
282 | 282 | var rand = new Random(); |
|
283 | 283 | |
|
284 | 284 | for (int i = 0; i < count; i++) |
|
285 | 285 | args[i] = (int)(rand.NextDouble() * 100); |
|
286 | 286 | |
|
287 | 287 | int result = 0; |
|
288 | 288 | |
|
289 | 289 | var t = Environment.TickCount; |
|
290 | 290 | args.ParallelForEach(x => Interlocked.Add(ref result, x), 4).Join(); |
|
291 | 291 | |
|
292 | 292 | Console.WriteLine("Iteration complete in {0} ms, result: {1}", Environment.TickCount - t, result); |
|
293 | 293 | |
|
294 | 294 | int result2 = 0; |
|
295 | 295 | |
|
296 | 296 | t = Environment.TickCount; |
|
297 | 297 | for (int i = 0; i < count; i++) |
|
298 | 298 | result2 += args[i]; |
|
299 | 299 | Assert.AreEqual(result2, result); |
|
300 | 300 | Console.WriteLine("Verified in {0} ms", Environment.TickCount - t); |
|
301 | 301 | } |
|
302 | 302 | |
|
303 | 303 | [TestMethod] |
|
304 | 304 | public void ComplexCase1Test() { |
|
305 | 305 | var flags = new bool[3]; |
|
306 | 306 | |
|
307 | 307 | // op1 (aync 200ms) => op2 (async 200ms) => op3 (sync map) |
|
308 | 308 | |
|
309 | 309 | var p = PromiseHelper |
|
310 | 310 | .Sleep(200, "Alan") |
|
311 | 311 | .Cancelled(() => flags[0] = true) |
|
312 | 312 | .Chain(x => |
|
313 | 313 | PromiseHelper |
|
314 | 314 | .Sleep(200, "Hi, " + x) |
|
315 | 315 | .Map(y => y) |
|
316 | 316 | .Cancelled(() => flags[1] = true) |
|
317 | 317 | ) |
|
318 | 318 | .Cancelled(() => flags[2] = true); |
|
319 | 319 | Thread.Sleep(300); |
|
320 | 320 | p.Cancel(); |
|
321 | 321 | try { |
|
322 | 322 | Assert.AreEqual(p.Join(), "Hi, Alan"); |
|
323 | 323 | Assert.Fail("Shouldn't get here"); |
|
324 | 324 | } catch (OperationCanceledException) { |
|
325 | 325 | } |
|
326 | 326 | |
|
327 | 327 | Assert.IsFalse(flags[0]); |
|
328 | 328 | Assert.IsTrue(flags[1]); |
|
329 | 329 | Assert.IsTrue(flags[2]); |
|
330 | 330 | } |
|
331 | 331 | } |
|
332 | 332 | } |
|
333 | 333 |
|
1 | NO CONTENT: modified file, binary diff hidden |
@@ -1,238 +1,279 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Linq; |
|
4 | 4 | using System.Text; |
|
5 | 5 | using System.Threading; |
|
6 | 6 | using System.Diagnostics; |
|
7 | 7 | |
|
8 | 8 | namespace Implab.Parallels { |
|
9 | 9 | public abstract class DispatchPool<TUnit> : IDisposable { |
|
10 | 10 | readonly int m_minThreads; |
|
11 | 11 | readonly int m_maxThreads; |
|
12 |
int m_ |
|
|
12 | int m_createdThreads = 0; | |
|
13 | int m_activeThreads = 0; | |
|
14 | int m_sleepingThreads = 0; | |
|
13 | 15 | int m_maxRunningThreads = 0; |
|
14 | int m_suspended = 0; | |
|
15 | 16 | int m_exitRequired = 0; |
|
17 | int m_releaseTimeout = 100; // timeout while the working thread will wait for the new tasks before exit | |
|
16 | 18 | AutoResetEvent m_hasTasks = new AutoResetEvent(false); |
|
17 | 19 | |
|
18 | 20 | protected DispatchPool(int min, int max) { |
|
19 | 21 | if (min < 0) |
|
20 | 22 | throw new ArgumentOutOfRangeException("min"); |
|
21 | 23 | if (max <= 0) |
|
22 | 24 | throw new ArgumentOutOfRangeException("max"); |
|
23 | 25 | |
|
24 | 26 | if (min > max) |
|
25 | 27 | min = max; |
|
26 | 28 | m_minThreads = min; |
|
27 | 29 | m_maxThreads = max; |
|
28 | 30 | } |
|
29 | 31 | |
|
30 | 32 | protected DispatchPool(int threads) |
|
31 | 33 | : this(threads, threads) { |
|
32 | 34 | } |
|
33 | 35 | |
|
34 | 36 | protected DispatchPool() { |
|
35 | 37 | int maxThreads, maxCP; |
|
36 | 38 | ThreadPool.GetMaxThreads(out maxThreads, out maxCP); |
|
37 | 39 | |
|
38 | 40 | m_minThreads = 0; |
|
39 | 41 | m_maxThreads = maxThreads; |
|
40 | 42 | } |
|
41 | 43 | |
|
42 | 44 | protected void InitPool() { |
|
43 | 45 | for (int i = 0; i < m_minThreads; i++) |
|
44 | 46 | StartWorker(); |
|
45 | 47 | } |
|
46 | 48 | |
|
47 |
public int |
|
|
49 | public int PoolSize { | |
|
48 | 50 | get { |
|
49 |
return m_ |
|
|
51 | return m_createdThreads; | |
|
52 | } | |
|
53 | } | |
|
54 | ||
|
55 | public int ActiveThreads { | |
|
56 | get { | |
|
57 | return m_activeThreads; | |
|
50 | 58 | } |
|
51 | 59 | } |
|
52 | 60 | |
|
53 | 61 | public int MaxRunningThreads { |
|
54 | 62 | get { |
|
55 | 63 | return m_maxRunningThreads; |
|
56 | 64 | } |
|
57 | 65 | } |
|
58 | 66 | |
|
59 | 67 | protected bool IsDisposed { |
|
60 | 68 | get { |
|
61 | 69 | return m_exitRequired != 0; |
|
62 | 70 | } |
|
63 | 71 | } |
|
64 | 72 | |
|
65 | 73 | protected abstract bool TryDequeue(out TUnit unit); |
|
66 | 74 | |
|
67 | 75 | protected virtual bool ExtendPool() { |
|
68 |
if (m_s |
|
|
69 | m_hasTasks.Set(); | |
|
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(); | |
|
70 | 85 | return true; |
|
71 |
} |
|
|
72 | return StartWorker(); | |
|
86 | } | |
|
87 | ||
|
73 | 88 | } |
|
74 | 89 | |
|
75 | 90 | /// <summary> |
|
76 | 91 | /// ΠΠ°ΠΏΡΡΠΊΠ°Π΅Ρ Π»ΠΈΠ±ΠΎ Π½ΠΎΠ²ΡΠΉ ΠΏΠΎΡΠΎΠΊ, Π΅ΡΠ»ΠΈ ΡΠ°Π½ΡΡΠ΅ Π½Π΅ Π±ΡΠ»ΠΎ Π½ΠΈ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΏΠΎΡΠΎΠΊΠ°, Π»ΠΈΠ±ΠΎ ΡΡΡΠ°Π½Π°Π²Π»ΠΈΠ²Π°Π΅Ρ ΡΠΎΠ±ΡΡΠΈΠ΅ ΠΏΡΠΎΠ±ΡΠΆΠ΄Π΅Π½ΠΈΠ΅ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΡΠΏΡΡΠ΅Π³ΠΎ ΠΏΠΎΡΠΎΠΊΠ° |
|
77 | 92 | /// </summary> |
|
78 | 93 | protected void WakePool() { |
|
79 | 94 | m_hasTasks.Set(); // wake sleeping thread; |
|
80 | 95 | |
|
81 | 96 | if (AllocateThreadSlot(1)) { |
|
97 | // if there were no threads in the pool | |
|
82 | 98 | var worker = new Thread(this.Worker); |
|
83 | 99 | worker.IsBackground = true; |
|
84 | 100 | worker.Start(); |
|
85 | 101 | } |
|
86 | 102 | } |
|
87 | 103 | |
|
88 | protected virtual void Suspend() { | |
|
89 | m_hasTasks.WaitOne(); | |
|
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; | |
|
90 | 141 | } |
|
91 | 142 | |
|
92 | 143 | #region thread slots traits |
|
93 | 144 | |
|
94 | 145 | bool AllocateThreadSlot() { |
|
95 | 146 | int current; |
|
96 | 147 | // use spins to allocate slot for the new thread |
|
97 | 148 | do { |
|
98 |
current = m_ |
|
|
149 | current = m_createdThreads; | |
|
99 | 150 | if (current >= m_maxThreads || m_exitRequired != 0) |
|
100 | 151 | // no more slots left or the pool has been disposed |
|
101 | 152 | return false; |
|
102 |
} while (current != Interlocked.CompareExchange(ref m_ |
|
|
153 | } while (current != Interlocked.CompareExchange(ref m_createdThreads, current + 1, current)); | |
|
103 | 154 | |
|
104 | 155 | UpdateMaxThreads(current + 1); |
|
105 | 156 | |
|
106 | 157 | return true; |
|
107 | 158 | } |
|
108 | 159 | |
|
109 | 160 | bool AllocateThreadSlot(int desired) { |
|
110 |
if (desired - 1 != Interlocked.CompareExchange(ref m_ |
|
|
161 | if (desired - 1 != Interlocked.CompareExchange(ref m_createdThreads, desired, desired - 1)) | |
|
111 | 162 | return false; |
|
112 | 163 | |
|
113 | 164 | UpdateMaxThreads(desired); |
|
114 | 165 | |
|
115 | 166 | return true; |
|
116 | 167 | } |
|
117 | 168 | |
|
118 | 169 | bool ReleaseThreadSlot(out bool last) { |
|
119 | 170 | last = false; |
|
120 | 171 | int current; |
|
121 | 172 | // use spins to release slot for the new thread |
|
122 | 173 | do { |
|
123 |
current = m_ |
|
|
174 | current = m_createdThreads; | |
|
124 | 175 | if (current <= m_minThreads && m_exitRequired == 0) |
|
125 | 176 | // the thread is reserved |
|
126 | 177 | return false; |
|
127 |
} while (current != Interlocked.CompareExchange(ref m_ |
|
|
178 | } while (current != Interlocked.CompareExchange(ref m_createdThreads, current - 1, current)); | |
|
128 | 179 | |
|
129 | 180 | last = (current == 1); |
|
130 | 181 | |
|
131 | 182 | return true; |
|
132 | 183 | } |
|
133 | 184 | |
|
134 | 185 | /// <summary> |
|
135 | 186 | /// releases thread slot unconditionally, used during cleanup |
|
136 | 187 | /// </summary> |
|
137 | 188 | /// <returns>true - no more threads left</returns> |
|
138 | 189 | bool ReleaseThreadSlotAnyway() { |
|
139 |
var left = Interlocked.Decrement(ref m_ |
|
|
190 | var left = Interlocked.Decrement(ref m_createdThreads); | |
|
140 | 191 | return left == 0; |
|
141 | 192 | } |
|
142 | 193 | |
|
143 | 194 | void UpdateMaxThreads(int count) { |
|
144 | 195 | int max; |
|
145 | 196 | do { |
|
146 | 197 | max = m_maxRunningThreads; |
|
147 | 198 | if (max >= count) |
|
148 | 199 | break; |
|
149 | 200 | } while(max != Interlocked.CompareExchange(ref m_maxRunningThreads, count, max)); |
|
150 | 201 | } |
|
151 | 202 | |
|
152 | 203 | #endregion |
|
153 | 204 | |
|
154 | 205 | bool StartWorker() { |
|
155 | 206 | if (AllocateThreadSlot()) { |
|
156 | 207 | // slot successfully allocated |
|
157 | 208 | var worker = new Thread(this.Worker); |
|
158 | 209 | worker.IsBackground = true; |
|
159 | 210 | worker.Start(); |
|
160 | 211 | |
|
161 | 212 | return true; |
|
162 | 213 | } else { |
|
163 | 214 | return false; |
|
164 | 215 | } |
|
165 | 216 | } |
|
166 | 217 | |
|
167 | 218 | bool FetchTask(out TUnit unit) { |
|
168 | 219 | do { |
|
169 | 220 | // exit if requested |
|
170 | 221 | if (m_exitRequired != 0) { |
|
171 | 222 | // release the thread slot |
|
223 | Interlocked.Decrement(ref m_activeThreads); | |
|
172 | 224 | if (ReleaseThreadSlotAnyway()) // it was the last worker |
|
173 | 225 | m_hasTasks.Dispose(); |
|
174 | 226 | else |
|
175 | 227 | m_hasTasks.Set(); // wake next worker |
|
176 | 228 | unit = default(TUnit); |
|
177 | 229 | return false; |
|
178 | 230 | } |
|
179 | 231 | |
|
180 | 232 | // fetch task |
|
181 | 233 | if (TryDequeue(out unit)) { |
|
182 | 234 | ExtendPool(); |
|
183 | 235 | return true; |
|
184 | 236 | } |
|
185 | 237 | |
|
186 | //no tasks left, exit if the thread is no longer needed | |
|
187 | bool last; | |
|
188 | if (ReleaseThreadSlot(out last)) { | |
|
189 | if (last && m_hasTasks.WaitOne(0)) { | |
|
190 | if (AllocateThreadSlot(1)) | |
|
191 | continue; // spin again... | |
|
192 | else | |
|
193 | // we failed to reallocate slot for this thread | |
|
194 | // therefore we need to release the event | |
|
195 | m_hasTasks.Set(); | |
|
196 | } | |
|
197 | ||
|
198 | return false; | |
|
199 | } | |
|
238 | Interlocked.Decrement(ref m_activeThreads); | |
|
200 | 239 | |
|
201 | 240 | // entering suspend state |
|
202 | Interlocked.Increment(ref m_suspended); | |
|
203 | 241 | // keep this thread and wait |
|
204 |
Suspend() |
|
|
205 | Interlocked.Decrement(ref m_suspended); | |
|
242 | if (!Suspend()) | |
|
243 | return false; | |
|
244 | ||
|
245 | Interlocked.Increment(ref m_activeThreads); | |
|
206 | 246 | } while (true); |
|
207 | 247 | } |
|
208 | 248 | |
|
209 | 249 | protected abstract void InvokeUnit(TUnit unit); |
|
210 | 250 | |
|
211 | 251 | void Worker() { |
|
212 | 252 | TUnit unit; |
|
253 | Interlocked.Increment(ref m_activeThreads); | |
|
213 | 254 | while (FetchTask(out unit)) |
|
214 | 255 | InvokeUnit(unit); |
|
215 | 256 | } |
|
216 | 257 | |
|
217 | 258 | protected virtual void Dispose(bool disposing) { |
|
218 | 259 | if (disposing) { |
|
219 | 260 | if (m_exitRequired == 0) { |
|
220 | 261 | if (Interlocked.CompareExchange(ref m_exitRequired, 1, 0) != 0) |
|
221 | 262 | return; |
|
222 | 263 | |
|
223 | 264 | // wake sleeping threads |
|
224 | 265 | m_hasTasks.Set(); |
|
225 | 266 | GC.SuppressFinalize(this); |
|
226 | 267 | } |
|
227 | 268 | } |
|
228 | 269 | } |
|
229 | 270 | |
|
230 | 271 | public void Dispose() { |
|
231 | 272 | Dispose(true); |
|
232 | 273 | } |
|
233 | 274 | |
|
234 | 275 | ~DispatchPool() { |
|
235 | 276 | Dispose(false); |
|
236 | 277 | } |
|
237 | 278 | } |
|
238 | 279 | } |
@@ -1,89 +1,90 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Linq; |
|
4 | 4 | using System.Text; |
|
5 | 5 | using System.Threading; |
|
6 | 6 | using System.Diagnostics; |
|
7 | 7 | |
|
8 | 8 | namespace Implab.Parallels { |
|
9 | 9 | public class WorkerPool : DispatchPool<Action> { |
|
10 | 10 | |
|
11 | 11 | MTQueue<Action> m_queue = new MTQueue<Action>(); |
|
12 | 12 | int m_queueLength = 0; |
|
13 | 13 | readonly int m_threshold = 1; |
|
14 | 14 | |
|
15 | 15 | public WorkerPool(int minThreads, int maxThreads, int threshold) |
|
16 | 16 | : base(minThreads, maxThreads) { |
|
17 | 17 | m_threshold = threshold; |
|
18 | 18 | InitPool(); |
|
19 | 19 | } |
|
20 | 20 | |
|
21 | 21 | public WorkerPool(int minThreads, int maxThreads) : |
|
22 | 22 | base(minThreads, maxThreads) { |
|
23 | 23 | InitPool(); |
|
24 | 24 | } |
|
25 | 25 | |
|
26 | 26 | public WorkerPool(int threads) |
|
27 | 27 | : base(threads) { |
|
28 | 28 | InitPool(); |
|
29 | 29 | } |
|
30 | 30 | |
|
31 | 31 | public WorkerPool() |
|
32 | 32 | : base() { |
|
33 | 33 | InitPool(); |
|
34 | 34 | } |
|
35 | 35 | |
|
36 | 36 | public Promise<T> Invoke<T>(Func<T> task) { |
|
37 | 37 | if (task == null) |
|
38 | 38 | throw new ArgumentNullException("task"); |
|
39 | 39 | if (IsDisposed) |
|
40 | 40 | throw new ObjectDisposedException(ToString()); |
|
41 | 41 | |
|
42 | 42 | var promise = new Promise<T>(); |
|
43 | 43 | |
|
44 | 44 | EnqueueTask(delegate() { |
|
45 | 45 | try { |
|
46 | 46 | promise.Resolve(task()); |
|
47 | 47 | } catch (Exception e) { |
|
48 | 48 | promise.Reject(e); |
|
49 | 49 | } |
|
50 | 50 | }); |
|
51 | 51 | |
|
52 | 52 | return promise; |
|
53 | 53 | } |
|
54 | 54 | |
|
55 | 55 | protected void EnqueueTask(Action unit) { |
|
56 | 56 | Debug.Assert(unit != null); |
|
57 | 57 | var len = Interlocked.Increment(ref m_queueLength); |
|
58 | 58 | m_queue.Enqueue(unit); |
|
59 | 59 | |
|
60 |
|
|
|
61 | WakePool(); | |
|
60 | ExtendPool(); | |
|
62 | 61 | } |
|
63 | 62 | |
|
64 | 63 | protected override bool ExtendPool() { |
|
65 |
if (m_queueLength <= m_threshold*Thread |
|
|
64 | if (m_queueLength <= m_threshold*ActiveThreads) | |
|
66 | 65 | // in this case we are in active thread and it request for additional workers |
|
67 | 66 | // satisfy it only when queue is longer than threshold |
|
68 | 67 | return false; |
|
69 | 68 | return base.ExtendPool(); |
|
70 | 69 | } |
|
71 | 70 | |
|
72 | 71 | protected override bool TryDequeue(out Action unit) { |
|
73 | 72 | if (m_queue.TryDequeue(out unit)) { |
|
74 | 73 | Interlocked.Decrement(ref m_queueLength); |
|
75 | 74 | return true; |
|
76 | 75 | } |
|
77 | 76 | return false; |
|
78 | 77 | } |
|
79 | 78 | |
|
80 | 79 | protected override void InvokeUnit(Action unit) { |
|
81 | 80 | unit(); |
|
82 | 81 | } |
|
83 | 82 | |
|
84 |
protected override |
|
|
83 | protected override bool Suspend() { | |
|
85 | 84 | if (m_queueLength == 0) |
|
86 | base.Suspend(); | |
|
85 | return base.Suspend(); | |
|
86 | else | |
|
87 | return true; // spin again without locks... | |
|
87 | 88 | } |
|
88 | 89 | } |
|
89 | 90 | } |
General Comments 0
You need to be logged in to leave comments.
Login now