annotate Implab/Parallels/DispatchPool.cs @ 19:e3935fdf59a2 promises

Promise is rewritten to use interlocked operations instead of locks
author cin
date Sun, 10 Nov 2013 00:21:33 +0400
parents 7cd4a843b4e4
children 1c3b3d518480
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;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
12 int m_runningThreads = 0;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
13 int m_maxRunningThreads = 0;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
14 int m_suspended = 0;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
15 int m_exitRequired = 0;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
16 AutoResetEvent m_hasTasks = new AutoResetEvent(false);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
17
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
18 protected DispatchPool(int min, int max) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
19 if (min < 0)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
20 throw new ArgumentOutOfRangeException("min");
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
21 if (max <= 0)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
22 throw new ArgumentOutOfRangeException("max");
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
23
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
24 if (min > max)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
25 min = max;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
26 m_minThreads = min;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
27 m_maxThreads = max;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
28 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
29
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
30 protected DispatchPool(int threads)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
31 : this(threads, threads) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
32 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
33
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
34 protected DispatchPool() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
35 int maxThreads, maxCP;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
36 ThreadPool.GetMaxThreads(out maxThreads, out maxCP);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
37
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
38 m_minThreads = 0;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
39 m_maxThreads = maxThreads;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
40 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
41
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
42 protected void InitPool() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
43 for (int i = 0; i < m_minThreads; i++)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
44 StartWorker();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
45 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
46
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
47 public int ThreadCount {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
48 get {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
49 return m_runningThreads;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
50 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
51 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
52
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
53 public int MaxRunningThreads {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
54 get {
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 {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
61 return m_exitRequired != 0;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
62 }
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
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
65 protected abstract bool TryDequeue(out TUnit unit);
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
66
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
67 protected virtual bool ExtendPool() {
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
68 if (m_suspended > 0) {
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
69 m_hasTasks.Set();
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
70 return true;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
71 } else
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
72 return StartWorker();
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
73 }
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
74
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
75 /// <summary>
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
76 /// Запускает либо новый поток, если раньше не было ни одного потока, либо устанавливает событие пробуждение одного спящего потока
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
77 /// </summary>
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
78 protected void WakePool() {
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
79 m_hasTasks.Set(); // wake sleeping thread;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
80
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
81 if (AllocateThreadSlot(1)) {
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
82 var worker = new Thread(this.Worker);
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
83 worker.IsBackground = true;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
84 worker.Start();
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
85 }
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
86 }
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
87
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
88 protected virtual void Suspend() {
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
89 m_hasTasks.WaitOne();
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
90 }
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
91
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 {
16
cin
parents: 15
diff changeset
98 current = m_runningThreads;
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
99 if (current >= m_maxThreads || m_exitRequired != 0)
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;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
102 } while (current != Interlocked.CompareExchange(ref m_runningThreads, current + 1, current));
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) {
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
110 if (desired - 1 != Interlocked.CompareExchange(ref m_runningThreads, desired, desired - 1))
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
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
122 do {
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
123 current = m_runningThreads;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
124 if (current <= m_minThreads && m_exitRequired == 0)
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
125 // the thread is reserved
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
126 return false;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
127 } while (current != Interlocked.CompareExchange(ref m_runningThreads, current - 1, current));
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
128
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
129 last = (current == 1);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
130
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
131 return true;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
132 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
133
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
134 /// <summary>
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
135 /// releases thread slot unconditionally, used during cleanup
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
136 /// </summary>
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
137 /// <returns>true - no more threads left</returns>
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
138 bool ReleaseThreadSlotAnyway() {
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
139 var left = Interlocked.Decrement(ref m_runningThreads);
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
140 return left == 0;
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
141 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
142
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
143 void UpdateMaxThreads(int count) {
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
144 int max;
16
cin
parents: 15
diff changeset
145 do {
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
146 max = m_maxRunningThreads;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
147 if (max >= count)
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
148 break;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
149 } while(max != Interlocked.CompareExchange(ref m_maxRunningThreads, count, max));
16
cin
parents: 15
diff changeset
150 }
cin
parents: 15
diff changeset
151
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
152 #endregion
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
153
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
154 bool StartWorker() {
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
155 if (AllocateThreadSlot()) {
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
156 // slot successfully allocated
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
157 var worker = new Thread(this.Worker);
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
158 worker.IsBackground = true;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
159 worker.Start();
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
160
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
161 return true;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
162 } else {
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
163 return false;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
164 }
16
cin
parents: 15
diff changeset
165 }
cin
parents: 15
diff changeset
166
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
167 bool FetchTask(out TUnit unit) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
168 do {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
169 // exit if requested
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
170 if (m_exitRequired != 0) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
171 // release the thread slot
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
172 if (ReleaseThreadSlotAnyway()) // it was the last worker
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
173 m_hasTasks.Dispose();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
174 else
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
175 m_hasTasks.Set(); // wake next worker
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
176 unit = default(TUnit);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
177 return false;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
178 }
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 // fetch task
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
181 if (TryDequeue(out unit)) {
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
182 ExtendPool();
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
183 return true;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
184 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
185
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
186 //no tasks left, exit if the thread is no longer needed
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
187 bool last;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
188 if (ReleaseThreadSlot(out last)) {
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
189 if (last && m_hasTasks.WaitOne(0)) {
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
190 if (AllocateThreadSlot(1))
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
191 continue; // spin again...
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
192 else
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
193 // we failed to reallocate slot for this thread
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
194 // therefore we need to release the event
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
195 m_hasTasks.Set();
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
196 }
16
cin
parents: 15
diff changeset
197
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
198 return false;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
199 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
200
16
cin
parents: 15
diff changeset
201 // entering suspend state
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
202 Interlocked.Increment(ref m_suspended);
16
cin
parents: 15
diff changeset
203 // keep this thread and wait
cin
parents: 15
diff changeset
204 Suspend();
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
205 Interlocked.Decrement(ref m_suspended);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
206 } while (true);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
207 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
208
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
209 protected abstract void InvokeUnit(TUnit unit);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
210
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
211 void Worker() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
212 TUnit unit;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
213 while (FetchTask(out unit))
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
214 InvokeUnit(unit);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
215 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
216
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
217 protected virtual void Dispose(bool disposing) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
218 if (disposing) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
219 if (m_exitRequired == 0) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
220 if (Interlocked.CompareExchange(ref m_exitRequired, 1, 0) != 0)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
221 return;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
222
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
223 // wake sleeping threads
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
224 m_hasTasks.Set();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
225 GC.SuppressFinalize(this);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
226 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
227 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
228 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
229
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
230 public void Dispose() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
231 Dispose(true);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
232 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
233
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
234 ~DispatchPool() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
235 Dispose(false);
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 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
238 }