annotate Implab/Parallels/WorkerPool.cs @ 119:2573b562e328 v2

Promises rewritten, added improved version of AsyncQueue
author cin
date Sun, 11 Jan 2015 19:13:02 +0300
parents 4c0e5ef99986
children 471f596b2603
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
1 using System;
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
2 using System.Threading;
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
3 using System.Diagnostics;
35
2880242f987a initial log capabilities
cin
parents: 34
diff changeset
4 using Implab.Diagnostics;
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
5
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
6 namespace Implab.Parallels {
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
7 public class WorkerPool : DispatchPool<Action> {
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
8
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 92
diff changeset
9 AsyncQueue<Action> m_queue = new AsyncQueue<Action>();
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
10 int m_queueLength = 0;
16
cin
parents: 15
diff changeset
11 readonly int m_threshold = 1;
13
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
12
16
cin
parents: 15
diff changeset
13 public WorkerPool(int minThreads, int maxThreads, int threshold)
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
14 : base(minThreads, maxThreads) {
16
cin
parents: 15
diff changeset
15 m_threshold = threshold;
cin
parents: 15
diff changeset
16 InitPool();
cin
parents: 15
diff changeset
17 }
cin
parents: 15
diff changeset
18
cin
parents: 15
diff changeset
19 public WorkerPool(int minThreads, int maxThreads) :
cin
parents: 15
diff changeset
20 base(minThreads, maxThreads) {
cin
parents: 15
diff changeset
21 InitPool();
13
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
22 }
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
23
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
24 public WorkerPool(int threads)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
25 : base(threads) {
16
cin
parents: 15
diff changeset
26 InitPool();
13
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
27 }
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
28
92
4c0e5ef99986 rewritten tracing
cin
parents: 81
diff changeset
29 public WorkerPool() {
16
cin
parents: 15
diff changeset
30 InitPool();
13
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
31 }
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
32
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
33 public Promise<T> Invoke<T>(Func<T> task) {
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
34 if (task == null)
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
35 throw new ArgumentNullException("task");
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
36 if (IsDisposed)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
37 throw new ObjectDisposedException(ToString());
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
38
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
39 var promise = new Promise<T>();
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
40
92
4c0e5ef99986 rewritten tracing
cin
parents: 81
diff changeset
41 var lop = TraceContext.Instance.CurrentOperation;
35
2880242f987a initial log capabilities
cin
parents: 34
diff changeset
42
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
43 EnqueueTask(delegate() {
92
4c0e5ef99986 rewritten tracing
cin
parents: 81
diff changeset
44 TraceContext.Instance.EnterLogicalOperation(lop, false);
4c0e5ef99986 rewritten tracing
cin
parents: 81
diff changeset
45 try {
4c0e5ef99986 rewritten tracing
cin
parents: 81
diff changeset
46 promise.Resolve(task());
4c0e5ef99986 rewritten tracing
cin
parents: 81
diff changeset
47 } catch (Exception e) {
4c0e5ef99986 rewritten tracing
cin
parents: 81
diff changeset
48 promise.Reject(e);
4c0e5ef99986 rewritten tracing
cin
parents: 81
diff changeset
49 } finally {
4c0e5ef99986 rewritten tracing
cin
parents: 81
diff changeset
50 TraceContext.Instance.Leave();
4c0e5ef99986 rewritten tracing
cin
parents: 81
diff changeset
51 }
13
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
52 });
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
53
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
54 return promise;
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
55 }
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
56
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
57 protected void EnqueueTask(Action unit) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
58 Debug.Assert(unit != null);
16
cin
parents: 15
diff changeset
59 var len = Interlocked.Increment(ref m_queueLength);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
60 m_queue.Enqueue(unit);
16
cin
parents: 15
diff changeset
61
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
62 if (len > m_threshold * PoolSize) {
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
63 StartWorker();
80
4f20870d0816 added memory barriers
cin
parents: 40
diff changeset
64 }
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
65
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
66 SignalThread();
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
67 }
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
68
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
69 protected override bool TryDequeue(out Action unit) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
70 if (m_queue.TryDequeue(out unit)) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
71 Interlocked.Decrement(ref m_queueLength);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
72 return true;
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
73 }
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
74 return false;
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
75 }
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
76
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
77 protected override void InvokeUnit(Action unit) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
78 unit();
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
79 }
16
cin
parents: 15
diff changeset
80
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
81 }
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
82 }