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