Mercurial > pub > ImplabNet
comparison Implab/Parallels/ArrayTraits.cs @ 24:ee04e1fa78da
fixed dispatch pool race condition
author | cin |
---|---|
date | Thu, 14 Nov 2013 01:15:07 +0400 |
parents | e3935fdf59a2 |
children | 9bf5b23650c9 |
comparison
equal
deleted
inserted
replaced
23:f0568ff069a5 | 24:ee04e1fa78da |
---|---|
165 return 0; | 165 return 0; |
166 }); | 166 }); |
167 | 167 |
168 return promise.Anyway(() => semaphore.Dispose()); | 168 return promise.Anyway(() => semaphore.Dispose()); |
169 } | 169 } |
170 | |
171 /* | |
172 this method is pretty fast, but it may cause a stack overflow if an element transformation is made faster then the next operation is | |
173 be chained, in this case the syncronous callback invocation will occur | |
174 | |
175 public static Promise<TDst[]> ChainedMap2<TSrc, TDst>(this TSrc[] source, ChainedOperation<TSrc, TDst> transform, int threads) { | |
176 if (source == null) | |
177 throw new ArgumentNullException("source"); | |
178 if (transform == null) | |
179 throw new ArgumentNullException("transform"); | |
180 if (threads <= 0) | |
181 throw new ArgumentOutOfRangeException("Threads number must be greater then zero"); | |
182 | |
183 var promise = new Promise<TDst[]>(); | |
184 var res = new TDst[source.Length]; | |
185 var index = -1; // we will start with increment | |
186 var len = source.Length; | |
187 var pending = len; | |
188 | |
189 Action<int> callback = null; | |
190 callback = (current) => { | |
191 if (current < len) { | |
192 transform(source[current]) | |
193 .Then( | |
194 x => { | |
195 res[current] = x; | |
196 if (Interlocked.Decrement(ref pending) == 0) | |
197 promise.Resolve(res); | |
198 else | |
199 callback(Interlocked.Increment(ref index)); | |
200 }, | |
201 e => promise.Reject(e) | |
202 ); | |
203 } | |
204 }; | |
205 | |
206 for (int i = 0; i < threads; i++) | |
207 callback(Interlocked.Increment(ref index)); | |
208 return promise; | |
209 } | |
210 */ | |
170 } | 211 } |
171 } | 212 } |