annotate Implab/Parallels/DispatchPool.cs @ 105:4d308952fd5e v2

minor fixes
author cin
date Mon, 10 Nov 2014 10:17:54 +0300
parents dc4942d09e74
children f803565868a4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
1 using System;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
2 using System.Threading;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
3
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
4 namespace Implab.Parallels {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
5 public abstract class DispatchPool<TUnit> : IDisposable {
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
6 readonly int m_minThreadsLimit;
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
7 readonly int m_maxThreadsLimit;
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
8 readonly int m_releaseTimeout = 1000; // the timeout while the working thread will wait for the new tasks before exit
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
9
93
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
10 int m_threads; // the current size of the pool
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
11 int m_maxRunningThreads; // the meximum reached size of the pool
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
12 int m_exit; // the pool is going to shutdown, all unused workers are released
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
13
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
14 readonly object m_signal = new object(); // used to pulse waiting threads
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
16 protected DispatchPool(int min, int max) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
17 if (min < 0)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
18 throw new ArgumentOutOfRangeException("min");
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
19 if (max <= 0)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
20 throw new ArgumentOutOfRangeException("max");
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
21
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
22 if (min > max)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
23 min = max;
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
24 m_minThreadsLimit = min;
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
25 m_maxThreadsLimit = max;
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
26 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
27
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
28 protected DispatchPool(int threads)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
29 : this(threads, threads) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
30 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
31
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
32 protected DispatchPool() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
33 int maxThreads, maxCP;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
34 ThreadPool.GetMaxThreads(out maxThreads, out maxCP);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
35
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
36 m_minThreadsLimit = 0;
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
37 m_maxThreadsLimit = maxThreads;
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
38 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
39
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
40 protected void InitPool() {
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
41 for (int i = 0; i < m_minThreadsLimit; i++)
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
42 StartWorker();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
43 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
44
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
45 public int PoolSize {
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
46 get {
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
47 Thread.MemoryBarrier();
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
48 return m_threads;
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
49 }
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
50 }
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
51
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
52 public int MaxRunningThreads {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
53 get {
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
54 Thread.MemoryBarrier();
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
55 return m_maxRunningThreads;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
56 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
57 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
58
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
59 protected bool IsDisposed {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
60 get {
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
61 Thread.MemoryBarrier();
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
62 return m_exit == 1;
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
63 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
64 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
65
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
66 protected abstract bool TryDequeue(out TUnit unit);
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
67
89
ce0171cacec4 improved performance of a chained map operation
cin
parents: 81
diff changeset
68 bool Dequeue(out TUnit unit, int timeout) {
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
69 int ts = Environment.TickCount;
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
70 if (TryDequeue(out unit))
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
71 return true;
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
72 lock (m_signal) {
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
73 while (!TryDequeue(out unit) && m_exit == 0)
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
74 if(!Monitor.Wait(m_signal, Math.Max(0, ts + timeout - Environment.TickCount))) {
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
75 // timeout
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
76 return false;
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
77 }
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
78 // queue item or terminate
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
79 Monitor.Pulse(m_signal);
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
80 if (m_exit == 1)
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
81 return false;
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
82 }
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
83 return true;
22
5a35900264f5 implemented nonblocking wake singnals processing
cin
parents: 21
diff changeset
84 }
5a35900264f5 implemented nonblocking wake singnals processing
cin
parents: 21
diff changeset
85
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
86 protected void SignalThread() {
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
87 lock (m_signal) {
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
88 Monitor.Pulse(m_signal);
30
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 24
diff changeset
89 }
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 24
diff changeset
90 }
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 24
diff changeset
91
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
92 #region thread slots traits
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
93
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
94 bool AllocateThreadSlot() {
16
cin
parents: 15
diff changeset
95 int current;
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
96 // use spins to allocate slot for the new thread
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
97 do {
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
98 current = m_threads;
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
99 if (current >= m_maxThreadsLimit || m_exit == 1)
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
100 // no more slots left or the pool has been disposed
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
101 return false;
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
102 } while (current != Interlocked.CompareExchange(ref m_threads, current + 1, current));
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
103
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
104 UpdateMaxThreads(current + 1);
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
105
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
106 return true;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
107 }
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
108
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
109 bool AllocateThreadSlot(int desired) {
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
110 if (desired - 1 != Interlocked.CompareExchange(ref m_threads, desired, desired - 1))
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
111 return false;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
112
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
113 UpdateMaxThreads(desired);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
114
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
115 return true;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
116 }
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
117
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
118 bool ReleaseThreadSlot(out bool last) {
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
119 last = false;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
120 int current;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
121 // use spins to release slot for the new thread
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
122 Thread.MemoryBarrier();
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
123 do {
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
124 current = m_threads;
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
125 if (current <= m_minThreadsLimit && m_exit == 0)
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
126 // the thread is reserved
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
127 return false;
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
128 } while (current != Interlocked.CompareExchange(ref m_threads, current - 1, current));
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
129
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
130 last = (current == 1);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
131
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
132 return true;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
133 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
134
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
135 void UpdateMaxThreads(int count) {
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
136 int max;
16
cin
parents: 15
diff changeset
137 do {
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
138 max = m_maxRunningThreads;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
139 if (max >= count)
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
140 break;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
141 } while(max != Interlocked.CompareExchange(ref m_maxRunningThreads, count, max));
16
cin
parents: 15
diff changeset
142 }
cin
parents: 15
diff changeset
143
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
144 #endregion
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
145
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
146 protected bool StartWorker() {
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
147 if (AllocateThreadSlot()) {
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
148 // slot successfully allocated
92
4c0e5ef99986 rewritten tracing
cin
parents: 89
diff changeset
149 var worker = new Thread(Worker);
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
150 worker.IsBackground = true;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
151 worker.Start();
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
152
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
153 return true;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
154 }
92
4c0e5ef99986 rewritten tracing
cin
parents: 89
diff changeset
155 return false;
16
cin
parents: 15
diff changeset
156 }
cin
parents: 15
diff changeset
157
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
158 protected abstract void InvokeUnit(TUnit unit);
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
159
41
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 34
diff changeset
160 protected virtual void Worker() {
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
161 TUnit unit;
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
162 bool last;
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
163 do {
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
164 while (Dequeue(out unit, m_releaseTimeout)) {
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
165 InvokeUnit(unit);
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
166 }
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
167 if(!ReleaseThreadSlot(out last))
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
168 continue;
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
169 // queue may be not empty
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
170 if (last && TryDequeue(out unit)) {
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
171 InvokeUnit(unit);
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
172 if (AllocateThreadSlot(1))
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
173 continue;
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
174 // we can safely exit since pool is alive
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
175 }
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
176 break;
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
177 } while(true);
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
178 }
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
179
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
180
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
181 protected virtual void Dispose(bool disposing) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
182 if (disposing) {
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
183 if (0 == Interlocked.CompareExchange(ref m_exit, 1, 0)) { // implies memory barrier
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
184 // wake sleeping threads
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
185 SignalThread();
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
186 GC.SuppressFinalize(this);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
187 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
188 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
189 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
190
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
191 public void Dispose() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
192 Dispose(true);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
193 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
194
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
195 ~DispatchPool() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
196 Dispose(false);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
197 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
198 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
199 }