annotate Implab/Parallels/WorkerPool.cs @ 81:2c5631b43c7d v2

dispatch pool rewritten
author cin
date Fri, 26 Sep 2014 20:44:01 +0400
parents 4f20870d0816
children 4c0e5ef99986
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;
80
4f20870d0816 added memory barriers
cin
parents: 40
diff changeset
15 int m_workers = 0;
13
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
16
16
cin
parents: 15
diff changeset
17 public WorkerPool(int minThreads, int maxThreads, int threshold)
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
18 : base(minThreads, maxThreads) {
16
cin
parents: 15
diff changeset
19 m_threshold = threshold;
80
4f20870d0816 added memory barriers
cin
parents: 40
diff changeset
20 m_workers = minThreads;
16
cin
parents: 15
diff changeset
21 InitPool();
cin
parents: 15
diff changeset
22 }
cin
parents: 15
diff changeset
23
cin
parents: 15
diff changeset
24 public WorkerPool(int minThreads, int maxThreads) :
cin
parents: 15
diff changeset
25 base(minThreads, maxThreads) {
80
4f20870d0816 added memory barriers
cin
parents: 40
diff changeset
26 m_workers = minThreads;
16
cin
parents: 15
diff changeset
27 InitPool();
13
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
28 }
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
29
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
30 public WorkerPool(int threads)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
31 : base(threads) {
80
4f20870d0816 added memory barriers
cin
parents: 40
diff changeset
32 m_workers = threads;
16
cin
parents: 15
diff changeset
33 InitPool();
13
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
34 }
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
35
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
36 public WorkerPool()
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
37 : base() {
16
cin
parents: 15
diff changeset
38 InitPool();
13
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
39 }
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
40
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
41 public Promise<T> Invoke<T>(Func<T> task) {
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
42 if (task == null)
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
43 throw new ArgumentNullException("task");
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
44 if (IsDisposed)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
45 throw new ObjectDisposedException(ToString());
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
46
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
47 var promise = new Promise<T>();
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
48
40
fe33f4e02ad5 improved tracing
cin
parents: 36
diff changeset
49 var caller = TraceContext.Snapshot();
35
2880242f987a initial log capabilities
cin
parents: 34
diff changeset
50
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
51 EnqueueTask(delegate() {
40
fe33f4e02ad5 improved tracing
cin
parents: 36
diff changeset
52 caller.Invoke(delegate() {
fe33f4e02ad5 improved tracing
cin
parents: 36
diff changeset
53 try {
fe33f4e02ad5 improved tracing
cin
parents: 36
diff changeset
54 promise.Resolve(task());
fe33f4e02ad5 improved tracing
cin
parents: 36
diff changeset
55 } catch (Exception e) {
fe33f4e02ad5 improved tracing
cin
parents: 36
diff changeset
56 promise.Reject(e);
fe33f4e02ad5 improved tracing
cin
parents: 36
diff changeset
57 }
fe33f4e02ad5 improved tracing
cin
parents: 36
diff changeset
58 });
13
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 12
diff changeset
59 });
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
60
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
61 return promise;
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
62 }
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
63
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
64 protected void EnqueueTask(Action unit) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
65 Debug.Assert(unit != null);
16
cin
parents: 15
diff changeset
66 var len = Interlocked.Increment(ref m_queueLength);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
67 m_queue.Enqueue(unit);
16
cin
parents: 15
diff changeset
68
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
69 if (len > m_threshold * PoolSize) {
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
70 StartWorker();
80
4f20870d0816 added memory barriers
cin
parents: 40
diff changeset
71 }
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
72
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
73 SignalThread();
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
74 }
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
75
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
76 protected override bool TryDequeue(out Action unit) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
77 if (m_queue.TryDequeue(out unit)) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
78 Interlocked.Decrement(ref m_queueLength);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
79 return true;
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
80 }
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
81 return false;
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
82 }
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
83
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
84 protected override void InvokeUnit(Action unit) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 13
diff changeset
85 unit();
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
86 }
16
cin
parents: 15
diff changeset
87
12
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
88 }
eb418ba8275b refactoring, added WorkerPool
cin
parents:
diff changeset
89 }