annotate Implab/Parallels/DispatchPool.cs @ 20:1c3b3d518480 promises

refactoring, sync
author cin
date Tue, 12 Nov 2013 02:27:22 +0400
parents 7cd4a843b4e4
children 6a56df4ec59e
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.Collections.Generic;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
3 using System.Linq;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
4 using System.Text;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
5 using System.Threading;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
6 using System.Diagnostics;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
7
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
8 namespace Implab.Parallels {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
9 public abstract class DispatchPool<TUnit> : IDisposable {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
10 readonly int m_minThreads;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
11 readonly int m_maxThreads;
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
12 int m_createdThreads = 0;
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
13 int m_activeThreads = 0;
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
14 int m_sleepingThreads = 0;
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
15 int m_maxRunningThreads = 0;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
16 int m_exitRequired = 0;
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
17 int m_releaseTimeout = 100; // timeout while the working thread will wait for the new tasks before exit
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
18 AutoResetEvent m_hasTasks = new AutoResetEvent(false);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
19
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
20 protected DispatchPool(int min, int max) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
21 if (min < 0)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
22 throw new ArgumentOutOfRangeException("min");
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
23 if (max <= 0)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
24 throw new ArgumentOutOfRangeException("max");
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
25
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
26 if (min > max)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
27 min = max;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
28 m_minThreads = min;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
29 m_maxThreads = max;
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(int threads)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
33 : this(threads, threads) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
34 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
35
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
36 protected DispatchPool() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
37 int maxThreads, maxCP;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
38 ThreadPool.GetMaxThreads(out maxThreads, out maxCP);
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 m_minThreads = 0;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
41 m_maxThreads = maxThreads;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
42 }
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 protected void InitPool() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
45 for (int i = 0; i < m_minThreads; i++)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
46 StartWorker();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
47 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
48
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
49 public int PoolSize {
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
50 get {
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
51 return m_createdThreads;
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
52 }
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
53 }
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
54
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
55 public int ActiveThreads {
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
56 get {
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
57 return m_activeThreads;
15
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 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
60
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
61 public int MaxRunningThreads {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
62 get {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
63 return m_maxRunningThreads;
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 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
66
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
67 protected bool IsDisposed {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
68 get {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
69 return m_exitRequired != 0;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
70 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
71 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
72
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
73 protected abstract bool TryDequeue(out TUnit unit);
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
74
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
75 protected virtual bool ExtendPool() {
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
76 if (m_sleepingThreads == 0)
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
77 // no sleeping workers are available
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
78 // try create one
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
79 return StartWorker();
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
80 else {
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
81 // we can get here a race condition when several threads asks to extend pool
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
82 // and some sleaping threads are exited due timeout but they are still counted as sleeping
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
83 // in that case all of this threads could exit except one
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
84 WakePool();
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
85 return true;
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
86 }
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
87
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
88 }
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
89
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
90 /// <summary>
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
91 /// Запускает либо новый поток, если раньше не было ни одного потока, либо устанавливает событие пробуждение одного спящего потока
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
92 /// </summary>
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
93 protected void WakePool() {
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
94 m_hasTasks.Set(); // wake sleeping thread;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
95
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
96 if (AllocateThreadSlot(1)) {
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
97 // if there were no threads in the pool
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
98 var worker = new Thread(this.Worker);
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
99 worker.IsBackground = true;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
100 worker.Start();
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
101 }
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
102 }
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
103
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
104 bool Sleep(int timeout) {
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
105 Interlocked.Increment(ref m_sleepingThreads);
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
106 var result = m_hasTasks.WaitOne(timeout);
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
107 Interlocked.Decrement(ref m_sleepingThreads);
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
108 return result;
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
109 }
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
110
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
111 protected virtual bool Suspend() {
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
112 //no tasks left, exit if the thread is no longer needed
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
113 bool last;
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
114 bool requestExit;
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
115
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
116 if (m_releaseTimeout > 0)
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
117 requestExit = !Sleep(m_releaseTimeout);
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
118 else
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
119 requestExit = true;
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
120
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
121
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
122 if (requestExit && ReleaseThreadSlot(out last)) {
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
123 // in case at the moment the last thread was being released
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
124 // a new task was added to the queue, we need to try
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
125 // to revoke the thread to avoid the situation when the task is left unprocessed
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
126 if (last && m_hasTasks.WaitOne(0)) {
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
127 if (AllocateThreadSlot(1))
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
128 return true; // spin again...
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
129 else
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
130 // we failed to reallocate the first slot for this thread
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
131 // therefore we need to release the event
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
132 m_hasTasks.Set();
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
133 }
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
134
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
135 return false;
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
136 }
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
137
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
138 Sleep(-1);
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
139
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
140 return true;
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
141 }
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
142
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
143 #region thread slots traits
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
144
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
145 bool AllocateThreadSlot() {
16
cin
parents: 15
diff changeset
146 int current;
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
147 // use spins to allocate slot for the new thread
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
148 do {
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
149 current = m_createdThreads;
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
150 if (current >= m_maxThreads || m_exitRequired != 0)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
151 // no more slots left or the pool has been disposed
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
152 return false;
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
153 } while (current != Interlocked.CompareExchange(ref m_createdThreads, current + 1, current));
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
154
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
155 UpdateMaxThreads(current + 1);
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
156
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
157 return true;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
158 }
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
159
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
160 bool AllocateThreadSlot(int desired) {
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
161 if (desired - 1 != Interlocked.CompareExchange(ref m_createdThreads, desired, desired - 1))
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
162 return false;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
163
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
164 UpdateMaxThreads(desired);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
165
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
166 return true;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
167 }
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
168
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
169 bool ReleaseThreadSlot(out bool last) {
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
170 last = false;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
171 int current;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
172 // use spins to release slot for the new thread
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
173 do {
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
174 current = m_createdThreads;
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
175 if (current <= m_minThreads && m_exitRequired == 0)
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
176 // the thread is reserved
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
177 return false;
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
178 } while (current != Interlocked.CompareExchange(ref m_createdThreads, current - 1, current));
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
179
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
180 last = (current == 1);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
181
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
182 return true;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
183 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
184
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
185 /// <summary>
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
186 /// releases thread slot unconditionally, used during cleanup
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
187 /// </summary>
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
188 /// <returns>true - no more threads left</returns>
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
189 bool ReleaseThreadSlotAnyway() {
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
190 var left = Interlocked.Decrement(ref m_createdThreads);
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
191 return left == 0;
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
192 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
193
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
194 void UpdateMaxThreads(int count) {
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
195 int max;
16
cin
parents: 15
diff changeset
196 do {
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
197 max = m_maxRunningThreads;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
198 if (max >= count)
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
199 break;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
200 } while(max != Interlocked.CompareExchange(ref m_maxRunningThreads, count, max));
16
cin
parents: 15
diff changeset
201 }
cin
parents: 15
diff changeset
202
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
203 #endregion
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
204
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
205 bool StartWorker() {
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
206 if (AllocateThreadSlot()) {
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
207 // slot successfully allocated
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
208 var worker = new Thread(this.Worker);
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
209 worker.IsBackground = true;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
210 worker.Start();
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
211
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
212 return true;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
213 } else {
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
214 return false;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
215 }
16
cin
parents: 15
diff changeset
216 }
cin
parents: 15
diff changeset
217
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
218 bool FetchTask(out TUnit unit) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
219 do {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
220 // exit if requested
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
221 if (m_exitRequired != 0) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
222 // release the thread slot
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
223 Interlocked.Decrement(ref m_activeThreads);
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
224 if (ReleaseThreadSlotAnyway()) // it was the last worker
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
225 m_hasTasks.Dispose();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
226 else
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
227 m_hasTasks.Set(); // wake next worker
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
228 unit = default(TUnit);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
229 return false;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
230 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
231
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
232 // fetch task
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
233 if (TryDequeue(out unit)) {
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
234 ExtendPool();
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
235 return true;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
236 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
237
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
238 Interlocked.Decrement(ref m_activeThreads);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
239
16
cin
parents: 15
diff changeset
240 // entering suspend state
cin
parents: 15
diff changeset
241 // keep this thread and wait
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
242 if (!Suspend())
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
243 return false;
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
244
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
245 Interlocked.Increment(ref m_activeThreads);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
246 } while (true);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
247 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
248
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
249 protected abstract void InvokeUnit(TUnit unit);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
250
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
251 void Worker() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
252 TUnit unit;
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
253 Interlocked.Increment(ref m_activeThreads);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
254 while (FetchTask(out unit))
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
255 InvokeUnit(unit);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
256 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
257
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
258 protected virtual void Dispose(bool disposing) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
259 if (disposing) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
260 if (m_exitRequired == 0) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
261 if (Interlocked.CompareExchange(ref m_exitRequired, 1, 0) != 0)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
262 return;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
263
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
264 // wake sleeping threads
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
265 m_hasTasks.Set();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
266 GC.SuppressFinalize(this);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
267 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
268 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
269 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
270
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
271 public void Dispose() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
272 Dispose(true);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
273 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
274
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
275 ~DispatchPool() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
276 Dispose(false);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
277 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
278 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
279 }