annotate Implab/Parallels/WorkerPool.cs @ 44:e5ec543feee3

Рефакторинг, журналирование
author cin
date Wed, 16 Apr 2014 19:02:58 +0400
parents fe33f4e02ad5
children 4f20870d0816
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.Collections.Generic;
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
3 using System.Linq;
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
4 using System.Text;
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
5 using System.Threading;
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
6 using System.Diagnostics;
35
2880242f987a initial log capabilities
cin
parents: 34
diff changeset
7 using Implab.Diagnostics;
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
8
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
9 namespace Implab.Parallels {
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
10 public class WorkerPool : DispatchPool<Action> {
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
11
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
12 MTQueue<Action> m_queue = new MTQueue<Action>();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
13 int m_queueLength = 0;
16
cin
parents: 15
diff changeset
14 readonly int m_threshold = 1;
13
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
15
16
cin
parents: 15
diff changeset
16 public WorkerPool(int minThreads, int maxThreads, int threshold)
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
17 : base(minThreads, maxThreads) {
16
cin
parents: 15
diff changeset
18 m_threshold = threshold;
cin
parents: 15
diff changeset
19 InitPool();
cin
parents: 15
diff changeset
20 }
cin
parents: 15
diff changeset
21
cin
parents: 15
diff changeset
22 public WorkerPool(int minThreads, int maxThreads) :
cin
parents: 15
diff changeset
23 base(minThreads, maxThreads) {
cin
parents: 15
diff changeset
24 InitPool();
13
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
25 }
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
26
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
27 public WorkerPool(int threads)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
28 : base(threads) {
16
cin
parents: 15
diff changeset
29 InitPool();
13
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
30 }
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
31
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
32 public WorkerPool()
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
33 : base() {
16
cin
parents: 15
diff changeset
34 InitPool();
13
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
35 }
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
36
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
37 public Promise<T> Invoke<T>(Func<T> task) {
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
38 if (task == null)
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
39 throw new ArgumentNullException("task");
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
40 if (IsDisposed)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
41 throw new ObjectDisposedException(ToString());
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
42
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
43 var promise = new Promise<T>();
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
44
40
fe33f4e02ad5 improved tracing
cin
parents: 36
diff changeset
45 var caller = TraceContext.Snapshot();
35
2880242f987a initial log capabilities
cin
parents: 34
diff changeset
46
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
47 EnqueueTask(delegate() {
40
fe33f4e02ad5 improved tracing
cin
parents: 36
diff changeset
48 caller.Invoke(delegate() {
fe33f4e02ad5 improved tracing
cin
parents: 36
diff changeset
49 try {
fe33f4e02ad5 improved tracing
cin
parents: 36
diff changeset
50 promise.Resolve(task());
fe33f4e02ad5 improved tracing
cin
parents: 36
diff changeset
51 } catch (Exception e) {
fe33f4e02ad5 improved tracing
cin
parents: 36
diff changeset
52 promise.Reject(e);
fe33f4e02ad5 improved tracing
cin
parents: 36
diff changeset
53 }
fe33f4e02ad5 improved tracing
cin
parents: 36
diff changeset
54 });
13
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
55 });
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
56
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
57 return promise;
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
58 }
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
59
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
60 protected void EnqueueTask(Action unit) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
61 Debug.Assert(unit != null);
16
cin
parents: 15
diff changeset
62 var len = Interlocked.Increment(ref m_queueLength);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
63 m_queue.Enqueue(unit);
16
cin
parents: 15
diff changeset
64
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
65 if (len > m_threshold*ActiveThreads)
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
66 GrowPool();
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
34
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
77 protected override bool Suspend() {
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
78 // This override solves race condition
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
79 // WORKER CLIENT
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
80 // ---------------------------------------
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
81 // TryDeque == false
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
82 // Enqueue(unit), queueLen++
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
83 // GrowPool? == NO
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
84 // ActiveThreads--
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
85 // Suspend
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
86 // queueLength > 0
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
87 // continue
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
88 if (m_queueLength > 0)
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
89 return true;
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
90 return base.Suspend();
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
91 }
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
92
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
93 protected override void InvokeUnit(Action unit) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
94 unit();
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
95 }
16
cin
parents: 15
diff changeset
96
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
97 }
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
98 }