annotate Implab/Parallels/WorkerPool.cs @ 36:313f708a50e9 diagnostics

improved log concept
author cin
date Tue, 15 Apr 2014 02:00:09 +0400
parents 2880242f987a
children fe33f4e02ad5
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
36
313f708a50e9 improved log concept
cin
parents: 35
diff changeset
45 var caller = TraceContext.Current;
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() {
36
313f708a50e9 improved log concept
cin
parents: 35
diff changeset
48 TraceLog.Transfer(caller);
13
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
49 try {
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
50 promise.Resolve(task());
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
51 } catch (Exception e) {
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
52 promise.Reject(e);
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
53 }
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
54 });
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
55
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
56 return promise;
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
57 }
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
58
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
59 protected void EnqueueTask(Action unit) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
60 Debug.Assert(unit != null);
16
cin
parents: 15
diff changeset
61 var len = Interlocked.Increment(ref m_queueLength);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
62 m_queue.Enqueue(unit);
16
cin
parents: 15
diff changeset
63
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
64 if (len > m_threshold*ActiveThreads)
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
65 GrowPool();
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
66 }
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
67
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
68 protected override bool TryDequeue(out Action unit) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
69 if (m_queue.TryDequeue(out unit)) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
70 Interlocked.Decrement(ref m_queueLength);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
71 return true;
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
72 }
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
73 return false;
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
74 }
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
75
34
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
76 protected override bool Suspend() {
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
77 // This override solves race condition
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
78 // WORKER CLIENT
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
79 // ---------------------------------------
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
80 // TryDeque == false
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
81 // Enqueue(unit), queueLen++
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
82 // GrowPool? == NO
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
83 // ActiveThreads--
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
84 // Suspend
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
85 // queueLength > 0
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
86 // continue
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
87 if (m_queueLength > 0)
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
88 return true;
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
89 return base.Suspend();
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
90 }
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 21
diff changeset
91
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
92 protected override void InvokeUnit(Action unit) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
93 unit();
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
94 }
16
cin
parents: 15
diff changeset
95
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
96 }
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
97 }