@@ -1,333 +1,333 | |||
|
1 | 1 | using System; |
|
2 | 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; |
|
3 | 3 | using System.Reflection; |
|
4 | 4 | using System.Threading; |
|
5 | 5 | using Implab.Parallels; |
|
6 | 6 | |
|
7 | 7 | namespace Implab.Test { |
|
8 | 8 | [TestClass] |
|
9 | 9 | public class AsyncTests { |
|
10 | 10 | [TestMethod] |
|
11 | 11 | public void ResolveTest() { |
|
12 | 12 | int res = -1; |
|
13 | 13 | var p = new Promise<int>(); |
|
14 | 14 | p.Then(x => res = x); |
|
15 | 15 | p.Resolve(100); |
|
16 | 16 | |
|
17 |
Assert.AreEqual( |
|
|
17 | Assert.AreEqual(100, res); | |
|
18 | 18 | } |
|
19 | 19 | |
|
20 | 20 | [TestMethod] |
|
21 | 21 | public void RejectTest() { |
|
22 | 22 | int res = -1; |
|
23 | 23 | Exception err = null; |
|
24 | 24 | |
|
25 | 25 | var p = new Promise<int>(); |
|
26 | 26 | p.Then(x => res = x, e => err = e); |
|
27 | 27 | p.Reject(new ApplicationException("error")); |
|
28 | 28 | |
|
29 | 29 | Assert.AreEqual(res, -1); |
|
30 | 30 | Assert.AreEqual(err.Message, "error"); |
|
31 | 31 | |
|
32 | 32 | } |
|
33 | 33 | |
|
34 | 34 | [TestMethod] |
|
35 | 35 | public void JoinSuccessTest() { |
|
36 | 36 | var p = new Promise<int>(); |
|
37 | 37 | p.Resolve(100); |
|
38 | 38 | Assert.AreEqual(p.Join(), 100); |
|
39 | 39 | } |
|
40 | 40 | |
|
41 | 41 | [TestMethod] |
|
42 | 42 | public void JoinFailTest() { |
|
43 | 43 | var p = new Promise<int>(); |
|
44 | 44 | p.Reject(new ApplicationException("failed")); |
|
45 | 45 | |
|
46 | 46 | try { |
|
47 | 47 | p.Join(); |
|
48 | 48 | throw new ApplicationException("WRONG!"); |
|
49 | 49 | } catch (TargetInvocationException err) { |
|
50 | 50 | Assert.AreEqual(err.InnerException.Message, "failed"); |
|
51 | 51 | } catch { |
|
52 | 52 | Assert.Fail("Got wrong excaption"); |
|
53 | 53 | } |
|
54 | 54 | } |
|
55 | 55 | |
|
56 | 56 | [TestMethod] |
|
57 | 57 | public void MapTest() { |
|
58 | 58 | var p = new Promise<int>(); |
|
59 | 59 | |
|
60 | 60 | var p2 = p.Map(x => x.ToString()); |
|
61 | 61 | p.Resolve(100); |
|
62 | 62 | |
|
63 | 63 | Assert.AreEqual(p2.Join(), "100"); |
|
64 | 64 | } |
|
65 | 65 | |
|
66 | 66 | [TestMethod] |
|
67 | 67 | public void FixErrorTest() { |
|
68 | 68 | var p = new Promise<int>(); |
|
69 | 69 | |
|
70 | 70 | var p2 = p.Error(e => 101); |
|
71 | 71 | |
|
72 | 72 | p.Reject(new Exception()); |
|
73 | 73 | |
|
74 | 74 | Assert.AreEqual(p2.Join(), 101); |
|
75 | 75 | } |
|
76 | 76 | |
|
77 | 77 | [TestMethod] |
|
78 | 78 | public void ChainTest() { |
|
79 | 79 | var p1 = new Promise<int>(); |
|
80 | 80 | |
|
81 | 81 | var p3 = p1.Chain(x => { |
|
82 | 82 | var p2 = new Promise<string>(); |
|
83 | 83 | p2.Resolve(x.ToString()); |
|
84 | 84 | return p2; |
|
85 | 85 | }); |
|
86 | 86 | |
|
87 | 87 | p1.Resolve(100); |
|
88 | 88 | |
|
89 | 89 | Assert.AreEqual(p3.Join(), "100"); |
|
90 | 90 | } |
|
91 | 91 | |
|
92 | 92 | [TestMethod] |
|
93 | 93 | public void PoolTest() { |
|
94 | 94 | var pid = Thread.CurrentThread.ManagedThreadId; |
|
95 | 95 | var p = AsyncPool.Invoke(() => Thread.CurrentThread.ManagedThreadId); |
|
96 | 96 | |
|
97 | 97 | Assert.AreNotEqual(pid, p.Join()); |
|
98 | 98 | } |
|
99 | 99 | |
|
100 | 100 | [TestMethod] |
|
101 | 101 | public void WorkerPoolSizeTest() { |
|
102 | 102 | var pool = new WorkerPool(5, 10, 0); |
|
103 | 103 | |
|
104 | 104 | Assert.AreEqual(5, pool.ThreadCount); |
|
105 | 105 | |
|
106 | 106 | pool.Invoke(() => { Thread.Sleep(1000000); return 10; }); |
|
107 | 107 | pool.Invoke(() => { Thread.Sleep(1000000); return 10; }); |
|
108 | 108 | pool.Invoke(() => { Thread.Sleep(1000000); return 10; }); |
|
109 | 109 | |
|
110 | 110 | Assert.AreEqual(5, pool.ThreadCount); |
|
111 | 111 | |
|
112 | 112 | for (int i = 0; i < 100; i++) |
|
113 | 113 | pool.Invoke(() => { Thread.Sleep(1000000); return 10; }); |
|
114 | 114 | Thread.Sleep(100); |
|
115 | 115 | Assert.AreEqual(10, pool.ThreadCount); |
|
116 | 116 | |
|
117 | 117 | pool.Dispose(); |
|
118 | 118 | } |
|
119 | 119 | |
|
120 | 120 | [TestMethod] |
|
121 | 121 | public void WorkerPoolCorrectTest() { |
|
122 | 122 | var pool = new WorkerPool(0,1000,100); |
|
123 | 123 | |
|
124 | 124 | int iterations = 1000; |
|
125 | 125 | int pending = iterations; |
|
126 | 126 | var stop = new ManualResetEvent(false); |
|
127 | 127 | |
|
128 | 128 | var count = 0; |
|
129 | 129 | for (int i = 0; i < iterations; i++) { |
|
130 | 130 | pool |
|
131 | 131 | .Invoke(() => 1) |
|
132 | 132 | .Then(x => Interlocked.Add(ref count, x)) |
|
133 | 133 | .Then(x => Math.Log10(x)) |
|
134 | 134 | .Anyway(() => { |
|
135 | 135 | Interlocked.Decrement(ref pending); |
|
136 | 136 | if (pending == 0) |
|
137 | 137 | stop.Set(); |
|
138 | 138 | }); |
|
139 | 139 | } |
|
140 | 140 | |
|
141 | 141 | stop.WaitOne(); |
|
142 | 142 | |
|
143 | 143 | Assert.AreEqual(iterations, count); |
|
144 | 144 | Console.WriteLine("Max threads: {0}", pool.MaxRunningThreads); |
|
145 | 145 | pool.Dispose(); |
|
146 | 146 | |
|
147 | 147 | } |
|
148 | 148 | |
|
149 | 149 | [TestMethod] |
|
150 | 150 | public void WorkerPoolDisposeTest() { |
|
151 | 151 | var pool = new WorkerPool(5, 20); |
|
152 | 152 | Assert.AreEqual(5, pool.ThreadCount); |
|
153 | 153 | pool.Dispose(); |
|
154 | 154 | Thread.Sleep(100); |
|
155 | 155 | Assert.AreEqual(0, pool.ThreadCount); |
|
156 | 156 | pool.Dispose(); |
|
157 | 157 | } |
|
158 | 158 | |
|
159 | 159 | [TestMethod] |
|
160 | 160 | public void MTQueueTest() { |
|
161 | 161 | var queue = new MTQueue<int>(); |
|
162 | 162 | int res; |
|
163 | 163 | |
|
164 | 164 | queue.Enqueue(10); |
|
165 | 165 | Assert.IsTrue(queue.TryDequeue(out res)); |
|
166 | 166 | Assert.AreEqual(10, res); |
|
167 | 167 | Assert.IsFalse(queue.TryDequeue(out res)); |
|
168 | 168 | |
|
169 | 169 | for (int i = 0; i < 1000; i++) |
|
170 | 170 | queue.Enqueue(i); |
|
171 | 171 | |
|
172 | 172 | for (int i = 0; i < 1000; i++) { |
|
173 | 173 | queue.TryDequeue(out res); |
|
174 | 174 | Assert.AreEqual(i, res); |
|
175 | 175 | } |
|
176 | 176 | |
|
177 | 177 | int writers = 0; |
|
178 | 178 | int readers = 0; |
|
179 | 179 | var stop = new ManualResetEvent(false); |
|
180 | 180 | int total = 0; |
|
181 | 181 | |
|
182 | 182 | int itemsPerWriter = 1000; |
|
183 | 183 | int writersCount = 3; |
|
184 | 184 | |
|
185 | 185 | for (int i = 0; i < writersCount; i++) { |
|
186 | 186 | Interlocked.Increment(ref writers); |
|
187 | 187 | var wn = i; |
|
188 | 188 | AsyncPool |
|
189 | 189 | .InvokeNewThread(() => { |
|
190 | 190 | for (int ii = 0; ii < itemsPerWriter; ii++) { |
|
191 | 191 | queue.Enqueue(1); |
|
192 | 192 | } |
|
193 | 193 | return 1; |
|
194 | 194 | }) |
|
195 | 195 | .Anyway(() => Interlocked.Decrement(ref writers)); |
|
196 | 196 | } |
|
197 | 197 | |
|
198 | 198 | for (int i = 0; i < 10; i++) { |
|
199 | 199 | Interlocked.Increment(ref readers); |
|
200 | 200 | var wn = i; |
|
201 | 201 | AsyncPool |
|
202 | 202 | .InvokeNewThread(() => { |
|
203 | 203 | int t; |
|
204 | 204 | do { |
|
205 | 205 | while (queue.TryDequeue(out t)) |
|
206 | 206 | Interlocked.Add(ref total, t); |
|
207 | 207 | } while (writers > 0); |
|
208 | 208 | return 1; |
|
209 | 209 | }) |
|
210 | 210 | .Anyway(() => { |
|
211 | 211 | Interlocked.Decrement(ref readers); |
|
212 | 212 | if (readers == 0) |
|
213 | 213 | stop.Set(); |
|
214 | 214 | }); |
|
215 | 215 | } |
|
216 | 216 | |
|
217 | 217 | stop.WaitOne(); |
|
218 | 218 | |
|
219 | 219 | Assert.AreEqual(itemsPerWriter * writersCount, total); |
|
220 | 220 | } |
|
221 | 221 | |
|
222 | 222 | [TestMethod] |
|
223 | 223 | public void ParallelMapTest() { |
|
224 | 224 | |
|
225 | 225 | int count = 100000; |
|
226 | 226 | |
|
227 | 227 | double[] args = new double[count]; |
|
228 | 228 | var rand = new Random(); |
|
229 | 229 | |
|
230 | 230 | for (int i = 0; i < count; i++) |
|
231 | 231 | args[i] = rand.NextDouble(); |
|
232 | 232 | |
|
233 | 233 | var t = Environment.TickCount; |
|
234 | 234 | var res = args.ParallelMap(x => Math.Sin(x*x), 4).Join(); |
|
235 | 235 | |
|
236 | 236 | Console.WriteLine("Map complete in {0} ms", Environment.TickCount - t); |
|
237 | 237 | |
|
238 | 238 | t = Environment.TickCount; |
|
239 | 239 | for (int i = 0; i < count; i++) |
|
240 | 240 | Assert.AreEqual(Math.Sin(args[i] * args[i]), res[i]); |
|
241 | 241 | Console.WriteLine("Verified in {0} ms", Environment.TickCount - t); |
|
242 | 242 | } |
|
243 | 243 | |
|
244 | 244 | [TestMethod] |
|
245 | 245 | public void ChainedMapTest() { |
|
246 | 246 | |
|
247 |
using (var pool = new WorkerPool( |
|
|
247 | using (var pool = new WorkerPool(4,4,0)) { | |
|
248 | 248 | int count = 10000; |
|
249 | 249 | |
|
250 | 250 | double[] args = new double[count]; |
|
251 | 251 | var rand = new Random(); |
|
252 | 252 | |
|
253 | 253 | for (int i = 0; i < count; i++) |
|
254 | 254 | args[i] = rand.NextDouble(); |
|
255 | 255 | |
|
256 | 256 | var t = Environment.TickCount; |
|
257 | 257 | var res = args |
|
258 | 258 | .ChainedMap( |
|
259 | 259 | x => pool.Invoke( |
|
260 | 260 | () => Math.Sin(x * x) |
|
261 | 261 | ), |
|
262 | 262 | 4 |
|
263 | 263 | ) |
|
264 | 264 | .Join(); |
|
265 | 265 | |
|
266 | 266 | Console.WriteLine("Map complete in {0} ms", Environment.TickCount - t); |
|
267 | 267 | |
|
268 | 268 | t = Environment.TickCount; |
|
269 | 269 | for (int i = 0; i < count; i++) |
|
270 | 270 | Assert.AreEqual(Math.Sin(args[i] * args[i]), res[i]); |
|
271 | 271 | Console.WriteLine("Verified in {0} ms", Environment.TickCount - t); |
|
272 | 272 | Console.WriteLine("Max workers: {0}", pool.MaxRunningThreads); |
|
273 | 273 | } |
|
274 | 274 | } |
|
275 | 275 | |
|
276 | 276 | [TestMethod] |
|
277 | 277 | public void ParallelForEachTest() { |
|
278 | 278 | |
|
279 | 279 | int count = 100000; |
|
280 | 280 | |
|
281 | 281 | int[] args = new int[count]; |
|
282 | 282 | var rand = new Random(); |
|
283 | 283 | |
|
284 | 284 | for (int i = 0; i < count; i++) |
|
285 | 285 | args[i] = (int)(rand.NextDouble() * 100); |
|
286 | 286 | |
|
287 | 287 | int result = 0; |
|
288 | 288 | |
|
289 | 289 | var t = Environment.TickCount; |
|
290 | 290 | args.ParallelForEach(x => Interlocked.Add(ref result, x), 4).Join(); |
|
291 | 291 | |
|
292 | 292 | Console.WriteLine("Iteration complete in {0} ms, result: {1}", Environment.TickCount - t, result); |
|
293 | 293 | |
|
294 | 294 | int result2 = 0; |
|
295 | 295 | |
|
296 | 296 | t = Environment.TickCount; |
|
297 | 297 | for (int i = 0; i < count; i++) |
|
298 | 298 | result2 += args[i]; |
|
299 | 299 | Assert.AreEqual(result2, result); |
|
300 | 300 | Console.WriteLine("Verified in {0} ms", Environment.TickCount - t); |
|
301 | 301 | } |
|
302 | 302 | |
|
303 | 303 | [TestMethod] |
|
304 | 304 | public void ComplexCase1Test() { |
|
305 | 305 | var flags = new bool[3]; |
|
306 | 306 | |
|
307 | 307 | // op1 (aync 200ms) => op2 (async 200ms) => op3 (sync map) |
|
308 | 308 | |
|
309 | 309 | var p = PromiseHelper |
|
310 | 310 | .Sleep(200, "Alan") |
|
311 | 311 | .Cancelled(() => flags[0] = true) |
|
312 | 312 | .Chain(x => |
|
313 | 313 | PromiseHelper |
|
314 | 314 | .Sleep(200, "Hi, " + x) |
|
315 | 315 | .Map(y => y) |
|
316 | 316 | .Cancelled(() => flags[1] = true) |
|
317 | 317 | ) |
|
318 | 318 | .Cancelled(() => flags[2] = true); |
|
319 | 319 | Thread.Sleep(300); |
|
320 | 320 | p.Cancel(); |
|
321 | 321 | try { |
|
322 | 322 | Assert.AreEqual(p.Join(), "Hi, Alan"); |
|
323 | 323 | Assert.Fail("Shouldn't get here"); |
|
324 | 324 | } catch (OperationCanceledException) { |
|
325 | 325 | } |
|
326 | 326 | |
|
327 | 327 | Assert.IsFalse(flags[0]); |
|
328 | 328 | Assert.IsTrue(flags[1]); |
|
329 | 329 | Assert.IsTrue(flags[2]); |
|
330 | 330 | } |
|
331 | 331 | } |
|
332 | 332 | } |
|
333 | 333 |
|
1 | NO CONTENT: modified file, binary diff hidden |
@@ -1,33 +1,20 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Linq; |
|
4 | 4 | using System.Text; |
|
5 | 5 | |
|
6 | 6 | namespace Implab |
|
7 | 7 | { |
|
8 | 8 | public interface IPromise: ICancellable |
|
9 | 9 | { |
|
10 | 10 | /// <summary> |
|
11 | 11 | /// Check whereather the promise has no more than one dependent promise. |
|
12 | 12 | /// </summary> |
|
13 | 13 | bool IsExclusive |
|
14 | 14 | { |
|
15 | 15 | get; |
|
16 | 16 | } |
|
17 | 17 | |
|
18 | /// <summary> | |
|
19 | /// The current state of the promise. | |
|
20 | /// </summary> | |
|
21 | PromiseState State | |
|
22 | { | |
|
23 | get; | |
|
24 | } | |
|
25 | 18 | |
|
26 | /// <summary> | |
|
27 | /// Registers handler for the case when the promise is cencelled. If the promise already cancelled the | |
|
28 | /// handler will be invoked immediatelly. | |
|
29 | /// </summary> | |
|
30 | /// <param name="handler">The handler</param> | |
|
31 | void HandleCancelled(Action handler); | |
|
32 | 19 | } |
|
33 | 20 | } |
@@ -1,171 +1,171 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Diagnostics; |
|
4 | 4 | using System.Linq; |
|
5 | 5 | using System.Text; |
|
6 | 6 | using System.Threading; |
|
7 | 7 | |
|
8 | 8 | namespace Implab.Parallels { |
|
9 | 9 | public static class ArrayTraits { |
|
10 | 10 | class ArrayIterator<TSrc> : DispatchPool<int> { |
|
11 | 11 | readonly Action<TSrc> m_action; |
|
12 | 12 | readonly TSrc[] m_source; |
|
13 | 13 | readonly Promise<int> m_promise = new Promise<int>(); |
|
14 | 14 | |
|
15 | 15 | int m_pending; |
|
16 | 16 | int m_next; |
|
17 | 17 | |
|
18 | 18 | public ArrayIterator(TSrc[] source, Action<TSrc> action, int threads) |
|
19 | 19 | : base(threads) { |
|
20 | 20 | |
|
21 | 21 | Debug.Assert(source != null); |
|
22 | 22 | Debug.Assert(action != null); |
|
23 | 23 | |
|
24 | 24 | m_next = 0; |
|
25 | 25 | m_source = source; |
|
26 | 26 | m_pending = source.Length; |
|
27 | 27 | m_action = action; |
|
28 | 28 | |
|
29 | 29 | m_promise.Anyway(() => Dispose()); |
|
30 | 30 | m_promise.Cancelled(() => Dispose()); |
|
31 | 31 | |
|
32 | 32 | InitPool(); |
|
33 | 33 | } |
|
34 | 34 | |
|
35 | 35 | public Promise<int> Promise { |
|
36 | 36 | get { |
|
37 | 37 | return m_promise; |
|
38 | 38 | } |
|
39 | 39 | } |
|
40 | 40 | |
|
41 | 41 | protected override bool TryDequeue(out int unit) { |
|
42 | 42 | unit = Interlocked.Increment(ref m_next) - 1; |
|
43 | 43 | return unit >= m_source.Length ? false : true; |
|
44 | 44 | } |
|
45 | 45 | |
|
46 | 46 | protected override void InvokeUnit(int unit) { |
|
47 | 47 | try { |
|
48 | 48 | m_action(m_source[unit]); |
|
49 | 49 | var pending = Interlocked.Decrement(ref m_pending); |
|
50 | 50 | if (pending == 0) |
|
51 | 51 | m_promise.Resolve(m_source.Length); |
|
52 | 52 | } catch (Exception e) { |
|
53 | 53 | m_promise.Reject(e); |
|
54 | 54 | } |
|
55 | 55 | } |
|
56 | 56 | } |
|
57 | 57 | |
|
58 | 58 | class ArrayMapper<TSrc, TDst>: DispatchPool<int> { |
|
59 | 59 | readonly Func<TSrc, TDst> m_transform; |
|
60 | 60 | readonly TSrc[] m_source; |
|
61 | 61 | readonly TDst[] m_dest; |
|
62 | 62 | readonly Promise<TDst[]> m_promise = new Promise<TDst[]>(); |
|
63 | 63 | |
|
64 | 64 | int m_pending; |
|
65 | 65 | int m_next; |
|
66 | 66 | |
|
67 | 67 | public ArrayMapper(TSrc[] source, Func<TSrc, TDst> transform, int threads) |
|
68 | 68 | : base(threads) { |
|
69 | 69 | |
|
70 | 70 | Debug.Assert (source != null); |
|
71 | 71 | Debug.Assert( transform != null); |
|
72 | 72 | |
|
73 | 73 | m_next = 0; |
|
74 | 74 | m_source = source; |
|
75 | 75 | m_dest = new TDst[source.Length]; |
|
76 | 76 | m_pending = source.Length; |
|
77 | 77 | m_transform = transform; |
|
78 | 78 | |
|
79 | 79 | m_promise.Anyway(() => Dispose()); |
|
80 | 80 | m_promise.Cancelled(() => Dispose()); |
|
81 | 81 | |
|
82 | 82 | InitPool(); |
|
83 | 83 | } |
|
84 | 84 | |
|
85 | 85 | public Promise<TDst[]> Promise { |
|
86 | 86 | get { |
|
87 | 87 | return m_promise; |
|
88 | 88 | } |
|
89 | 89 | } |
|
90 | 90 | |
|
91 | 91 | protected override bool TryDequeue(out int unit) { |
|
92 | 92 | unit = Interlocked.Increment(ref m_next) - 1; |
|
93 | 93 | return unit >= m_source.Length ? false : true; |
|
94 | 94 | } |
|
95 | 95 | |
|
96 | 96 | protected override void InvokeUnit(int unit) { |
|
97 | 97 | try { |
|
98 | 98 | m_dest[unit] = m_transform(m_source[unit]); |
|
99 | 99 | var pending = Interlocked.Decrement(ref m_pending); |
|
100 | 100 | if (pending == 0) |
|
101 | 101 | m_promise.Resolve(m_dest); |
|
102 | 102 | } catch (Exception e) { |
|
103 | 103 | m_promise.Reject(e); |
|
104 | 104 | } |
|
105 | 105 | } |
|
106 | 106 | } |
|
107 | 107 | |
|
108 | 108 | public static Promise<TDst[]> ParallelMap<TSrc, TDst> (this TSrc[] source, Func<TSrc,TDst> transform, int threads) { |
|
109 | 109 | if (source == null) |
|
110 | 110 | throw new ArgumentNullException("source"); |
|
111 | 111 | if (transform == null) |
|
112 | 112 | throw new ArgumentNullException("transform"); |
|
113 | 113 | |
|
114 | 114 | var mapper = new ArrayMapper<TSrc, TDst>(source, transform, threads); |
|
115 | 115 | return mapper.Promise; |
|
116 | 116 | } |
|
117 | 117 | |
|
118 | 118 | public static Promise<int> ParallelForEach<TSrc>(this TSrc[] source, Action<TSrc> action, int threads) { |
|
119 | 119 | if (source == null) |
|
120 | 120 | throw new ArgumentNullException("source"); |
|
121 | 121 | if (action == null) |
|
122 | 122 | throw new ArgumentNullException("action"); |
|
123 | 123 | |
|
124 | 124 | var iter = new ArrayIterator<TSrc>(source, action, threads); |
|
125 | 125 | return iter.Promise; |
|
126 | 126 | } |
|
127 | 127 | |
|
128 | 128 | public static Promise<TDst[]> ChainedMap<TSrc, TDst>(this TSrc[] source, ChainedOperation<TSrc, TDst> transform, int threads) { |
|
129 | 129 | if (source == null) |
|
130 | 130 | throw new ArgumentNullException("source"); |
|
131 | 131 | if (transform == null) |
|
132 | 132 | throw new ArgumentNullException("transform"); |
|
133 | 133 | if (threads <= 0) |
|
134 | 134 | throw new ArgumentOutOfRangeException("Threads number must be greater then zero"); |
|
135 | 135 | |
|
136 | 136 | var promise = new Promise<TDst[]>(); |
|
137 | 137 | var res = new TDst[source.Length]; |
|
138 | 138 | var pending = source.Length; |
|
139 | 139 | var semaphore = new Semaphore(threads, threads); |
|
140 | 140 | |
|
141 | 141 | AsyncPool.InvokeNewThread(() => { |
|
142 | 142 | for (int i = 0; i < source.Length; i++) { |
|
143 |
if(promise. |
|
|
143 | if(promise.IsResolved) | |
|
144 | 144 | break; // stop processing in case of error or cancellation |
|
145 | 145 | var idx = i; |
|
146 | 146 | semaphore.WaitOne(); |
|
147 | 147 | try { |
|
148 | 148 | var p1 = transform(source[i]); |
|
149 | 149 | p1.Anyway(() => semaphore.Release()); |
|
150 | 150 | p1.Cancelled(() => semaphore.Release()); |
|
151 | 151 | p1.Then( |
|
152 | 152 | x => { |
|
153 | 153 | res[idx] = x; |
|
154 | 154 | var left = Interlocked.Decrement(ref pending); |
|
155 | 155 | if (left == 0) |
|
156 | 156 | promise.Resolve(res); |
|
157 | 157 | }, |
|
158 | 158 | e => promise.Reject(e) |
|
159 | 159 | ); |
|
160 | 160 | |
|
161 | 161 | } catch (Exception e) { |
|
162 | 162 | promise.Reject(e); |
|
163 | 163 | } |
|
164 | 164 | } |
|
165 | 165 | return 0; |
|
166 | 166 | }); |
|
167 | 167 | |
|
168 | 168 | return promise.Anyway(() => semaphore.Dispose()); |
|
169 | 169 | } |
|
170 | 170 | } |
|
171 | 171 | } |
@@ -1,74 +1,75 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Linq; |
|
4 | 4 | using System.Text; |
|
5 | 5 | using System.Threading; |
|
6 | 6 | |
|
7 | 7 | namespace Implab.Parallels { |
|
8 | 8 | public class MTQueue<T> { |
|
9 | 9 | class Node { |
|
10 | 10 | public Node(T value) { |
|
11 | 11 | this.value = value; |
|
12 | 12 | } |
|
13 | 13 | public readonly T value; |
|
14 | 14 | public Node next; |
|
15 | 15 | } |
|
16 | 16 | |
|
17 | 17 | Node m_first; |
|
18 | 18 | Node m_last; |
|
19 | 19 | |
|
20 | 20 | public void Enqueue(T value) { |
|
21 | 21 | var last = m_last; |
|
22 | 22 | var next = new Node(value); |
|
23 | 23 | |
|
24 | 24 | while (last != Interlocked.CompareExchange(ref m_last, next, last)) |
|
25 | 25 | last = m_last; |
|
26 | 26 | |
|
27 | 27 | if (last != null) |
|
28 | 28 | last.next = next; |
|
29 | 29 | else |
|
30 | 30 | m_first = next; |
|
31 | 31 | } |
|
32 | 32 | |
|
33 | 33 | public bool TryDequeue(out T value) { |
|
34 | 34 | Node first; |
|
35 | 35 | Node next = null; |
|
36 | 36 | value = default(T); |
|
37 | 37 | |
|
38 | 38 | do { |
|
39 | 39 | first = m_first; |
|
40 | 40 | if (first == null) |
|
41 | 41 | return false; |
|
42 | 42 | next = first.next; |
|
43 | 43 | if (next == null) { |
|
44 | 44 | // this is the last element, |
|
45 | // then try to update tail | |
|
45 | // then try to update the tail | |
|
46 | 46 | if (first != Interlocked.CompareExchange(ref m_last, null, first)) { |
|
47 |
// this is |
|
|
47 | // this is a ace condition | |
|
48 | 48 | if (m_last == null) |
|
49 | // the queue is empty | |
|
49 | 50 | return false; |
|
50 |
// tail has been changed, tha |
|
|
51 | // tail has been changed, than we need to restart | |
|
51 | 52 | continue; |
|
52 | 53 | } |
|
53 | 54 | |
|
54 | 55 | // tail succesfully updated and first.next will never be changed |
|
55 | 56 | // other readers will fail due to inconsistency m_last != m_fist, but m_first.next == null |
|
56 | 57 | // but the writer may update the m_first since the m_last is null |
|
57 | 58 | |
|
58 | 59 | // so we need to fix inconsistency by setting m_first to null, but if it already has been |
|
59 | 60 | // updated by a writer then we should just give up |
|
60 | 61 | Interlocked.CompareExchange(ref m_first, null, first); |
|
61 | 62 | break; |
|
62 | 63 | |
|
63 | 64 | } else { |
|
64 | 65 | if (first == Interlocked.CompareExchange(ref m_first, next, first)) |
|
65 | 66 | // head succesfully updated |
|
66 | 67 | break; |
|
67 | 68 | } |
|
68 | 69 | } while (true); |
|
69 | 70 | |
|
70 | 71 | value = first.value; |
|
71 | 72 | return true; |
|
72 | 73 | } |
|
73 | 74 | } |
|
74 | 75 | } |
@@ -1,549 +1,564 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Reflection; |
|
4 | 4 | using System.Diagnostics; |
|
5 | 5 | using System.Threading; |
|
6 | using Implab.Parallels; | |
|
6 | 7 | |
|
7 | 8 | namespace Implab { |
|
8 | 9 | |
|
9 | 10 | public delegate void ErrorHandler(Exception e); |
|
10 | 11 | public delegate T ErrorHandler<out T>(Exception e); |
|
11 | 12 | public delegate void ResultHandler<in T>(T result); |
|
12 | 13 | public delegate TNew ResultMapper<in TSrc, out TNew>(TSrc result); |
|
13 | 14 | public delegate Promise<TNew> ChainedOperation<in TSrc, TNew>(TSrc result); |
|
14 | 15 | |
|
15 | 16 | /// <summary> |
|
16 | 17 | /// ΠΠ»Π°ΡΡ Π΄Π»Ρ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠ³ΠΎ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ². Π’Π°ΠΊ Π½Π°Π·ΡΠ²Π°Π΅ΠΌΠΎΠ΅ "ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅". |
|
17 | 18 | /// </summary> |
|
18 | 19 | /// <typeparam name="T">Π’ΠΈΠΏ ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌΠΎΠ³ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°</typeparam> |
|
19 | 20 | /// <remarks> |
|
20 | 21 | /// <para>Π‘Π΅ΡΠ²ΠΈΡ ΠΏΡΠΈ ΠΎΠ±ΡΠ°ΡΠ΅Π½ΠΈΠΈ ΠΊ Π΅Π³ΠΎ ΠΌΠ΅ΡΠΎΠ΄Ρ Π΄Π°Π΅Ρ ΠΎΠ±Π΅ΡΠ°ΠΈΠ½ΠΈΠ΅ ΠΎ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ, |
|
21 | 22 | /// ΠΊΠ»ΠΈΠ΅Π½Ρ ΠΏΠΎΠ»ΡΡΠΈΠ² ΡΠ°ΠΊΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΡΡΡΠ°Π½ΠΎΠ²ΠΈΡΡ ΡΡΠ΄ ΠΎΠ±ΡΠ°ΡΠ½ΡΡ Π²ΡΠ·ΠΎΠ²ΠΎ Π΄Π»Ρ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ |
|
22 | 23 | /// ΡΠΎΠ±ΡΡΠΈΠΉ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ, ΡΠΎΠ΅ΡΡΡ Π·Π°Π²Π΅ΡΡΠ΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΈ ΠΏΡΠ΅Π΄ΠΎΡΡΠ°Π²Π»Π΅Π½ΠΈΠΈ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ².</para> |
|
23 | 24 | /// <para> |
|
24 | 25 | /// ΠΠ±Π΅ΡΠ΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΊΠ°ΠΊ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ, ΡΠ°ΠΊ ΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ Ρ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ. ΠΠ»Ρ ΠΏΠΎΠ΄ΠΏΠΈΡΠΊΠΈ Π½Π° |
|
25 | 26 | /// Π΄Π°Π½Π½ΡΠ΅ ΡΠΎΠ±ΡΡΠΈΡ ΠΊΠ»ΠΈΠ΅Π½Ρ Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΠΌΠ΅ΡΠΎΠ΄Ρ <c>Then</c>. |
|
26 | 27 | /// </para> |
|
27 | 28 | /// <para> |
|
28 | 29 | /// Π‘Π΅ΡΠ²ΠΈΡ, Π² ΡΠ²ΠΎΡ ΠΎΡΠ΅ΡΠ΅Π΄Ρ, ΠΏΠΎ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΡ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ (Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ Ρ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ), |
|
29 | 30 | /// ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅Ρ ΠΌΠ΅ΡΠΎΠ΄Ρ <c>Resolve</c> Π»ΠΈΠ±ΠΎ <c>Reject</c> Π΄Π»Ρ ΠΎΠΏΠΎΠ²Π΅ΡΠ΅Π½ΠΈΡ ΠΊΠ»ΠΈΠ΅ΡΠ½Π° ΠΎ |
|
30 | 31 | /// Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. |
|
31 | 32 | /// </para> |
|
32 | 33 | /// <para> |
|
33 | 34 | /// ΠΡΠ»ΠΈ ΡΠ΅ΡΠ²Π΅Ρ ΡΡΠΏΠ΅Π» Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π΅ΡΠ΅ Π΄ΠΎ ΡΠΎΠ³ΠΎ, ΠΊΠ°ΠΊ ΠΊΠ»ΠΈΠ΅Π½Ρ Π½Π° Π½Π΅Π³ΠΎ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π»ΡΡ, |
|
34 | 35 | /// ΡΠΎ Π² ΠΌΠΎΠΌΠ΅Π½Ρ ΠΏΠΎΠ΄ΠΏΠΈΡΠΊΠΈ ΠΊΠ»ΠΈΠ΅Π½ΡΠ° Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ ΡΠΎΠΎΡΠ²Π΅ΡΡΠ²ΡΡΡΠΈΠ΅ ΡΠΎΠ±ΡΡΠΈΡ Π² ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΌ |
|
35 | 36 | /// ΡΠ΅ΠΆΠΈΠΌΠ΅ ΠΈ ΠΊΠ»ΠΈΠ΅Π½Ρ Π±ΡΠ΄Π΅Ρ ΠΎΠΏΠΎΠ²Π΅ΡΠ΅Π½ Π² Π»ΡΠ±ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅. ΠΠ½Π°ΡΠ΅, ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ Π΄ΠΎΠ±Π°Π²Π»ΡΡΡΡΡ Π² |
|
36 | 37 | /// ΡΠΏΠΈΡΠΎΠΊ Π² ΠΏΠΎΡΡΠ΄ΠΊΠ΅ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π½ΠΈΡ ΠΈ Π² ΡΡΠΎΠΌ ΠΆΠ΅ ΠΏΠΎΡΡΠ΄ΠΊΠ΅ ΠΎΠ½ΠΈ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ |
|
37 | 38 | /// ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. |
|
38 | 39 | /// </para> |
|
39 | 40 | /// <para> |
|
40 | 41 | /// ΠΠ±ΡΠ°Π±Π°ΡΡΠ²Π°Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²ΡΠ²Π°ΡΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ Π»ΠΈΠ±ΠΎ ΠΈΠ½ΠΈΡΠΈΠΈΡΠΎΠ²Π°ΡΡ |
|
41 | 42 | /// ΡΠ²ΡΠ·Π°Π½Π½ΡΠ΅ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΡΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ, ΠΊΠΎΡΠΎΡΡΠ΅ ΡΠ°ΠΊΠΆΠ΅ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. ΠΠ»Ρ ΡΡΠΎΠ³ΠΎ ΡΠ»Π΅Π΄ΡΠ΅Ρ |
|
42 | 43 | /// ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²ΡΡΡΡΡ ΡΠΎΡΠΌΡ ΠΌΠ΅ΡΠΎΠ΄Π΅ <c>Then</c>. |
|
43 | 44 | /// </para> |
|
44 | 45 | /// <para> |
|
45 | 46 | /// Π’Π°ΠΊΠΆΠ΅ Ρ ΠΎΡΠΎΡΠΈΠΌ ΠΏΡΠ°Π²ΠΈΠ»ΠΎΠΌ ΡΠ²Π»ΡΠ΅ΡΡΡ ΡΠΎ, ΡΡΠΎ <c>Resolve</c> ΠΈ <c>Reject</c> Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²ΡΠ·ΡΠ²Π°ΡΡ |
|
46 | 47 | /// ΡΠΎΠ»ΡΠΊΠΎ ΠΈΠ½ΠΈΡΠΈΠ°ΡΠΎΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈΠ½Π°ΡΠ΅ ΠΌΠΎΠ³ΡΡ Π²ΠΎΠ·Π½ΠΈΠΊΠ½ΡΡΡ ΠΏΡΠΎΡΠΈΠ²ΠΎΡΠ΅ΡΠΈΡ. |
|
47 | 48 | /// </para> |
|
48 | 49 | /// </remarks> |
|
49 | 50 | public class Promise<T> : IPromise { |
|
50 | 51 | |
|
51 |
struct |
|
|
52 | struct HandlerDescriptor { | |
|
52 | 53 | public ResultHandler<T> resultHandler; |
|
53 | 54 | public ErrorHandler errorHandler; |
|
55 | public Action cancellHandler; | |
|
56 | ||
|
57 | public void Resolve(T result) { | |
|
58 | if (resultHandler != null) | |
|
59 | try { | |
|
60 | resultHandler(result); | |
|
61 | } catch (Exception e) { | |
|
62 | Reject(e); | |
|
54 | 63 | } |
|
64 | } | |
|
65 | ||
|
66 | public void Reject(Exception err) { | |
|
67 | if (errorHandler != null) | |
|
68 | try { | |
|
69 | errorHandler(err); | |
|
70 | } catch { | |
|
71 | } | |
|
72 | } | |
|
73 | ||
|
74 | public void Cancel() { | |
|
75 | if (cancellHandler != null) | |
|
76 | try { | |
|
77 | cancellHandler(); | |
|
78 | } catch { | |
|
79 | } | |
|
80 | } | |
|
81 | } | |
|
82 | ||
|
83 | const int UnresolvedSate = 0; | |
|
84 | const int TransitionalState = 1; | |
|
85 | const int ResolvedState = 2; | |
|
86 | const int RejectedState = 3; | |
|
87 | const int CancelledState = 4; | |
|
55 | 88 | |
|
56 | 89 | readonly IPromise m_parent; |
|
57 | ||
|
58 | LinkedList<ResultHandlerInfo> m_resultHandlers = new LinkedList<ResultHandlerInfo>(); | |
|
59 | LinkedList<Action> m_cancelHandlers = new LinkedList<Action>(); | |
|
90 | readonly bool m_cancellable; | |
|
60 | 91 | |
|
61 | readonly object m_lock = new Object(); | |
|
62 | readonly bool m_cancellable; | |
|
63 | 92 | int m_childrenCount = 0; |
|
64 | ||
|
65 | PromiseState m_state; | |
|
93 | int m_state; | |
|
66 | 94 | T m_result; |
|
67 | 95 | Exception m_error; |
|
68 | 96 | |
|
97 | readonly MTQueue<HandlerDescriptor> m_handlers = new MTQueue<HandlerDescriptor>(); | |
|
98 | ||
|
69 | 99 | public Promise() { |
|
70 | 100 | m_cancellable = true; |
|
71 | 101 | } |
|
72 | 102 | |
|
73 | 103 | public Promise(IPromise parent, bool cancellable) { |
|
74 | 104 | m_cancellable = cancellable; |
|
75 | 105 | m_parent = parent; |
|
76 | if (parent != null) | |
|
77 | parent.HandleCancelled(InternalCancel); | |
|
78 | 106 | } |
|
79 | 107 | |
|
80 | 108 | void InternalCancel() { |
|
81 | 109 | // don't try to cancel parent :) |
|
82 | 110 | Cancel(false); |
|
83 | 111 | } |
|
84 | 112 | |
|
113 | bool BeginTransit() { | |
|
114 | return UnresolvedSate == Interlocked.CompareExchange(ref m_state, TransitionalState, UnresolvedSate); | |
|
115 | } | |
|
116 | ||
|
117 | void CompleteTransit(int state) { | |
|
118 | if (TransitionalState != Interlocked.CompareExchange(ref m_state, state, TransitionalState)) | |
|
119 | throw new InvalidOperationException("Can't complete transition when the object isn't in the transitional state"); | |
|
120 | } | |
|
121 | ||
|
122 | public bool IsResolved { | |
|
123 | get { | |
|
124 | return m_state > 1; | |
|
125 | } | |
|
126 | } | |
|
127 | ||
|
128 | public bool IsCancelled { | |
|
129 | get { | |
|
130 | return m_state == CancelledState; | |
|
131 | } | |
|
132 | } | |
|
133 | ||
|
85 | 134 | /// <summary> |
|
86 | 135 | /// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠΎΠΎΠ±ΡΠ°Ρ ΠΎΠ± ΡΡΠΏΠ΅ΡΠ½ΠΎΠΌ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ. |
|
87 | 136 | /// </summary> |
|
88 | 137 | /// <param name="result">Π Π΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ.</param> |
|
89 | 138 | /// <exception cref="InvalidOperationException">ΠΠ°Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ</exception> |
|
90 | 139 | public void Resolve(T result) { |
|
91 | lock (m_lock) { | |
|
92 | if (m_state == PromiseState.Cancelled) | |
|
93 | return; | |
|
94 | if (m_state != PromiseState.Unresolved) | |
|
140 | if (BeginTransit()) { | |
|
141 | m_result = result; | |
|
142 | CompleteTransit(ResolvedState); | |
|
143 | OnStateChanged(); | |
|
144 | } else if (m_state != CancelledState) | |
|
95 | 145 |
|
|
96 | m_result = result; | |
|
97 | m_state = PromiseState.Resolved; | |
|
98 | } | |
|
99 | ||
|
100 | OnStateChanged(); | |
|
101 | 146 | } |
|
102 | 147 | |
|
103 | 148 | /// <summary> |
|
104 | 149 | /// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠΎΠΎΠ±ΡΠ°Ρ ΠΎΠ± ΠΎΡΠΈΠ±ΠΊΠ΅ |
|
105 | 150 | /// </summary> |
|
106 | 151 | /// <remarks> |
|
107 | 152 | /// ΠΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π΄ΠΎΠ»ΠΆΠ½ΠΎ ΡΠ°Π±ΠΎΡΠ°ΡΡ Π² ΠΌΠ½ΠΎΠ³ΠΎΠΏΡΠΎΡΠ½ΠΎΠΉ ΡΡΠ΅Π΄Π΅, ΠΏΡΠΈ Π΅Π³ΠΎ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΡΡΠ°Π·Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΠΎΡΠΎΠΊΠΎΠ² |
|
108 | 153 | /// ΠΌΠΎΠ³Ρ Π²Π΅ΡΠ½ΡΡΡ ΠΎΡΠΈΠ±ΠΊΡ, ΠΏΡΠΈ ΡΡΠΎΠΌ ΡΠΎΠ»ΡΠΊΠΎ ΠΏΠ΅ΡΠ²Π°Ρ Π±ΡΠ΄Π΅Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½Π° Π² ΠΊΠ°ΡΠ΅ΡΡΠ²Π΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°, ΠΎΡΡΠ°Π»ΡΠ½ΡΠ΅ |
|
109 | 154 | /// Π±ΡΠ΄ΡΡ ΠΏΡΠΎΠΈΠ³Π½ΠΎΡΠΈΡΠΎΠ²Π°Π½Ρ. |
|
110 | 155 | /// </remarks> |
|
111 | 156 | /// <param name="error">ΠΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ</param> |
|
112 | 157 | /// <exception cref="InvalidOperationException">ΠΠ°Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ</exception> |
|
113 | 158 | public void Reject(Exception error) { |
|
114 | lock (m_lock) { | |
|
115 | if (m_state == PromiseState.Cancelled || m_state == PromiseState.Rejected) | |
|
116 | return; | |
|
117 | if (m_state != PromiseState.Unresolved) | |
|
159 | if (BeginTransit()) { | |
|
160 | m_error = error; | |
|
161 | CompleteTransit(RejectedState); | |
|
162 | OnStateChanged(); | |
|
163 | } else if (m_state == ResolvedState) | |
|
118 | 164 |
|
|
119 | m_error = error; | |
|
120 | m_state = PromiseState.Rejected; | |
|
121 | } | |
|
122 | ||
|
123 | OnStateChanged(); | |
|
124 | 165 | } |
|
125 | 166 | |
|
126 | 167 | /// <summary> |
|
127 | 168 | /// ΠΡΠΌΠ΅Π½ΡΠ΅Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, Π΅ΡΠ»ΠΈ ΡΡΠΎ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ. |
|
128 | 169 | /// </summary> |
|
129 | 170 | /// <returns><c>true</c> ΠΠΏΠ΅ΡΠ°ΡΠΈΡ Π±ΡΠ»Π° ΠΎΡΠΌΠ΅Π½Π΅Π½Π°, ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ Π½Π΅ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ.<c>false</c> ΠΎΡΠΌΠ΅Π½Π° Π½Π΅ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½Π°, ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΈ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ ΠΎΡΡΠ°Π±ΠΎΡΠ°Π»ΠΈ.</returns> |
|
130 | 171 | public bool Cancel() { |
|
131 | 172 | return Cancel(true); |
|
132 | 173 | } |
|
133 | 174 | |
|
134 | 175 | /// <summary> |
|
135 | 176 | /// Adds new handlers to this promise. |
|
136 | 177 | /// </summary> |
|
137 | 178 | /// <param name="success">The handler of the successfully completed operation. |
|
138 | 179 | /// This handler will recieve an operation result as a parameter.</param> |
|
139 | 180 | /// <param name="error">Handles an exception that may occur during the operation.</param> |
|
140 | 181 | /// <returns>The new promise chained to this one.</returns> |
|
141 | 182 | public Promise<T> Then(ResultHandler<T> success, ErrorHandler error) { |
|
142 | 183 | if (success == null && error == null) |
|
143 | 184 | return this; |
|
144 | 185 | |
|
145 | 186 | var medium = new Promise<T>(this, true); |
|
146 | 187 | |
|
147 | var handlerInfo = new ResultHandlerInfo(); | |
|
148 | ||
|
188 | ResultHandler<T> resultHandler; | |
|
149 | 189 | if (success != null) |
|
150 |
|
|
|
190 | resultHandler = x => { | |
|
151 | 191 | success(x); |
|
152 | 192 | medium.Resolve(x); |
|
153 | 193 | }; |
|
154 | 194 | else |
|
155 |
|
|
|
195 | resultHandler = medium.Resolve; | |
|
156 | 196 | |
|
197 | ErrorHandler errorHandler; | |
|
157 | 198 | if (error != null) |
|
158 |
|
|
|
199 | errorHandler = x => { | |
|
159 | 200 | try { |
|
160 | 201 | error(x); |
|
161 | 202 | } catch { } |
|
162 | 203 | medium.Reject(x); |
|
163 | 204 | }; |
|
164 | 205 | else |
|
165 |
|
|
|
206 | errorHandler = medium.Reject; | |
|
166 | 207 | |
|
167 | AddHandler(handlerInfo); | |
|
208 | AddHandler(resultHandler, errorHandler, medium.InternalCancel); | |
|
168 | 209 | |
|
169 | 210 | return medium; |
|
170 | 211 | } |
|
171 | 212 | |
|
172 | 213 | /// <summary> |
|
173 | 214 | /// Adds new handlers to this promise. |
|
174 | 215 | /// </summary> |
|
175 | 216 | /// <param name="success">The handler of the successfully completed operation. |
|
176 | 217 | /// This handler will recieve an operation result as a parameter.</param> |
|
177 | 218 | /// <param name="error">Handles an exception that may occur during the operation and returns the value which will be used as the result of the operation.</param> |
|
178 | 219 | /// <returns>The new promise chained to this one.</returns> |
|
179 | 220 | public Promise<T> Then(ResultHandler<T> success, ErrorHandler<T> error) { |
|
180 | 221 | if (success == null && error == null) |
|
181 | 222 | return this; |
|
182 | 223 | |
|
183 | 224 | var medium = new Promise<T>(this, true); |
|
184 | 225 | |
|
185 | var handlerInfo = new ResultHandlerInfo(); | |
|
226 | ResultHandler<T> resultHandler; | |
|
227 | ErrorHandler errorHandler; | |
|
186 | 228 | |
|
187 | 229 | if (success != null) |
|
188 |
|
|
|
230 | resultHandler = x => { | |
|
189 | 231 | success(x); |
|
190 | 232 | medium.Resolve(x); |
|
191 | 233 | }; |
|
192 | 234 | else |
|
193 |
|
|
|
235 | resultHandler = medium.Resolve; | |
|
194 | 236 | |
|
195 | 237 | if (error != null) |
|
196 |
|
|
|
238 | errorHandler = x => { | |
|
197 | 239 | try { |
|
198 | 240 | medium.Resolve(error(x)); |
|
199 | 241 | } catch { } |
|
200 | 242 | medium.Reject(x); |
|
201 | 243 | }; |
|
202 | 244 | else |
|
203 |
|
|
|
245 | errorHandler = medium.Reject; | |
|
204 | 246 | |
|
205 | AddHandler(handlerInfo); | |
|
247 | AddHandler(resultHandler, errorHandler, medium.InternalCancel); | |
|
206 | 248 | |
|
207 | 249 | return medium; |
|
208 | 250 | } |
|
209 | 251 | |
|
210 | 252 | |
|
211 | 253 | public Promise<T> Then(ResultHandler<T> success) { |
|
212 | 254 | if (success == null) |
|
213 | 255 | return this; |
|
214 | 256 | |
|
215 | 257 | var medium = new Promise<T>(this, true); |
|
216 | 258 | |
|
217 | var handlerInfo = new ResultHandlerInfo(); | |
|
259 | ResultHandler<T> resultHandler; | |
|
218 | 260 | |
|
219 | 261 | if (success != null) |
|
220 |
|
|
|
262 | resultHandler = x => { | |
|
221 | 263 | success(x); |
|
222 | 264 | medium.Resolve(x); |
|
223 | 265 | }; |
|
224 | 266 | else |
|
225 |
|
|
|
267 | resultHandler = medium.Resolve; | |
|
226 | 268 | |
|
227 | handlerInfo.errorHandler = medium.Reject; | |
|
228 | ||
|
229 | AddHandler(handlerInfo); | |
|
269 | AddHandler(resultHandler, medium.Reject, medium.InternalCancel); | |
|
230 | 270 | |
|
231 | 271 | return medium; |
|
232 | 272 | } |
|
233 | 273 | |
|
234 | 274 | public Promise<T> Error(ErrorHandler error) { |
|
235 | 275 | return Then(null, error); |
|
236 | 276 | } |
|
237 | 277 | |
|
238 | 278 | /// <summary> |
|
239 | 279 | /// Handles error and allows to keep the promise. |
|
240 | 280 | /// </summary> |
|
241 | 281 | /// <remarks> |
|
242 | 282 | /// If the specified handler throws an exception, this exception will be used to reject the promise. |
|
243 | 283 | /// </remarks> |
|
244 | 284 | /// <param name="handler">The error handler which returns the result of the promise.</param> |
|
245 | 285 | /// <returns>New promise.</returns> |
|
246 | 286 | public Promise<T> Error(ErrorHandler<T> handler) { |
|
247 | 287 | if (handler == null) |
|
248 | 288 | return this; |
|
249 | 289 | |
|
250 | 290 | var medium = new Promise<T>(this, true); |
|
251 | 291 | |
|
252 |
AddHandler( |
|
|
253 |
|
|
|
292 | AddHandler( | |
|
293 | null, | |
|
294 | e => { | |
|
254 | 295 | try { |
|
255 | 296 | medium.Resolve(handler(e)); |
|
256 | 297 | } catch (Exception e2) { |
|
257 | 298 | medium.Reject(e2); |
|
258 | 299 | } |
|
259 | } | |
|
260 | }); | |
|
300 | }, | |
|
301 | medium.InternalCancel | |
|
302 | ); | |
|
261 | 303 | |
|
262 | 304 | return medium; |
|
263 | 305 | } |
|
264 | 306 | |
|
265 | 307 | public Promise<T> Anyway(Action handler) { |
|
266 | 308 | if (handler == null) |
|
267 | 309 | return this; |
|
268 | 310 | |
|
269 | 311 | var medium = new Promise<T>(); |
|
270 | 312 | |
|
271 |
AddHandler( |
|
|
272 |
|
|
|
313 | AddHandler( | |
|
314 | x => { | |
|
273 | 315 | // to avoid handler being called multiple times we handle exception by ourselfs |
|
274 | 316 | try { |
|
275 | 317 | handler(); |
|
276 | 318 | medium.Resolve(x); |
|
277 | 319 | } catch (Exception e) { |
|
278 | 320 | medium.Reject(e); |
|
279 | 321 | } |
|
280 | 322 | }, |
|
281 | errorHandler = x => { | |
|
323 | ||
|
324 | e => { | |
|
282 | 325 | try { |
|
283 | 326 | handler(); |
|
284 | 327 | } catch { } |
|
285 |
medium.Reject( |
|
|
286 | } | |
|
287 | }); | |
|
328 | medium.Reject(e); | |
|
329 | }, | |
|
330 | ||
|
331 | medium.InternalCancel | |
|
332 | ); | |
|
288 | 333 | |
|
289 | 334 | return medium; |
|
290 | 335 | } |
|
291 | 336 | |
|
292 | 337 | /// <summary> |
|
293 | 338 | /// ΠΠΎΠ·Π²ΠΎΠ»ΡΠ΅Ρ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°ΡΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΊ Π½ΠΎΠ²ΠΎΠΌΡ ΡΠΈΠΏΡ. |
|
294 | 339 | /// </summary> |
|
295 | 340 | /// <typeparam name="TNew">ΠΠΎΠ²ΡΠΉ ΡΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°.</typeparam> |
|
296 | 341 | /// <param name="mapper">ΠΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΠΊ Π½ΠΎΠ²ΠΎΠΌΡ ΡΠΈΠΏΡ.</param> |
|
297 | 342 | /// <param name="error">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ. ΠΠ°Π½Π½ΡΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΏΠΎΠ»ΡΡΠΈΡ |
|
298 | 343 | /// ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param> |
|
299 | 344 | /// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΈΡΡ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ.</returns> |
|
300 | 345 | public Promise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper, ErrorHandler error) { |
|
301 | 346 | if (mapper == null) |
|
302 | 347 | throw new ArgumentNullException("mapper"); |
|
303 | 348 | |
|
304 | 349 | // ΡΠΎΠ·Π΄Π°Π΅ΠΌ ΠΏΡΠΈΡΠ΅ΠΏΠ»Π΅Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ |
|
305 | 350 | var chained = new Promise<TNew>(); |
|
306 | 351 | |
|
307 | AddHandler(new ResultHandlerInfo() { | |
|
308 | resultHandler = result => chained.Resolve(mapper(result)), | |
|
309 | errorHandler = delegate(Exception e) { | |
|
352 | ResultHandler<T> resultHandler = result => chained.Resolve(mapper(result)); | |
|
353 | ErrorHandler errorHandler = delegate(Exception e) { | |
|
310 | 354 |
|
|
311 | 355 |
|
|
312 | 356 |
|
|
313 | 357 |
|
|
314 | 358 |
|
|
315 | 359 |
|
|
316 |
|
|
|
317 | }); | |
|
360 | }; | |
|
361 | ||
|
362 | ||
|
363 | AddHandler( | |
|
364 | resultHandler, | |
|
365 | errorHandler, | |
|
366 | chained.InternalCancel | |
|
367 | ); | |
|
318 | 368 | |
|
319 | 369 | return chained; |
|
320 | 370 | } |
|
321 | 371 | |
|
322 | 372 | public Promise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper) { |
|
323 | 373 | return Map(mapper, null); |
|
324 | 374 | } |
|
325 | 375 | |
|
326 | 376 | /// <summary> |
|
327 | 377 | /// Π‘ΡΠ΅ΠΏΠ»ΡΠ΅Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ Π°ΡΠ½Ρ ΡΠΎΠ½Π½ΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ. Π£ΠΊΠ°Π·Π°Π½Π½Π°Ρ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Π±ΡΠ΄Π΅Ρ Π²ΡΠ·Π²Π°Π½Π° ΠΏΠΎΡΠ»Π΅ |
|
328 | 378 | /// Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΡΠ΅ΠΊΡΡΠ΅ΠΉ, Π° ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΡΠ΅ΠΊΡΡΠ΅ΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ Π΄Π»Ρ ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·Π°ΡΠΈΠΈ |
|
329 | 379 | /// Π½ΠΎΠ²ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ. |
|
330 | 380 | /// </summary> |
|
331 | 381 | /// <typeparam name="TNew">Π’ΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</typeparam> |
|
332 | 382 | /// <param name="chained">ΠΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΊΠΎΡΠΎΡΠ°Ρ Π΄ΠΎΠ»ΠΆΠ½Π° Π±ΡΠ΄Π΅Ρ Π½Π°ΡΠ°ΡΡΡΡ ΠΏΠΎΡΠ»Π΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΡΠ΅ΠΊΡΡΠ΅ΠΉ.</param> |
|
333 | 383 | /// <param name="error">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ. ΠΠ°Π½Π½ΡΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΏΠΎΠ»ΡΡΠΈΡ |
|
334 | 384 | /// ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΡΠ΅ΠΊΡΠ΅ΡΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param> |
|
335 | 385 | /// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΠΎ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΡ ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ Π°ΡΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</returns> |
|
336 | 386 | public Promise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained, ErrorHandler error) { |
|
337 | 387 | |
|
338 | 388 | // ΠΏΡΠΎΠ±Π»Π΅ΠΌΠ° Π² ΡΠΎΠΌ, ΡΡΠΎ Π½Π° ΠΌΠΎΠΌΠ΅Π½Ρ ΡΠ²ΡΠ·ΡΠ²Π°Π½ΠΈΡ Π΅ΡΠ΅ Π½Π΅ Π½Π°ΡΠ°ΡΠ° Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΏΠΎΡΡΠΎΠΌΡ Π½ΡΠΆΠ½ΠΎ |
|
339 | 389 | // ΡΠΎΠ·Π΄Π°ΡΡ ΠΏΠΎΡΡΠ΅Π΄Π½ΠΈΠΊΠ°, ΠΊ ΠΊΠΎΡΠΎΡΠΎΠΌΡ Π±ΡΠ΄ΡΡ ΠΏΠΎΠ΄Π²ΡΠ·ΡΠ²Π°ΡΡΡΡ ΡΠ»Π΅Π΄ΡΡΡΠΈΠ΅ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ. |
|
340 | 390 | // ΠΊΠΎΠ³Π΄Π° Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π° ΡΠ΅Π°Π»ΡΠ½Π°Ρ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΎΠ½Π° ΠΎΠ±ΡΠ°ΡΠΈΡΡΡΡ ΠΊ ΠΏΠΎΡΡΠ΅Π΄Π½ΠΈΠΊΡ, ΡΡΠΎΠ±Ρ |
|
341 | 391 | // ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΡΠ΅ΡΠ΅Π· Π½Π΅Π³ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ ΡΠ°Π±ΠΎΡΡ. |
|
342 | 392 | var medium = new Promise<TNew>(this, true); |
|
343 | 393 | |
|
344 | AddHandler(new ResultHandlerInfo { | |
|
345 | resultHandler = delegate(T result) { | |
|
346 | if (medium.State == PromiseState.Cancelled) | |
|
394 | ResultHandler<T> resultHandler = delegate(T result) { | |
|
395 | if (medium.IsCancelled) | |
|
347 | 396 |
|
|
348 | 397 | |
|
349 | 398 |
|
|
350 | 399 | |
|
351 | 400 |
|
|
352 | 401 |
|
|
353 | 402 |
|
|
354 | 403 |
|
|
355 | 404 |
|
|
356 | 405 |
|
|
357 |
|
|
|
358 | errorHandler = delegate(Exception e) { | |
|
406 | }; | |
|
407 | ||
|
408 | ErrorHandler errorHandler = delegate(Exception e) { | |
|
359 | 409 |
|
|
360 | 410 |
|
|
361 | 411 |
|
|
362 | 412 |
|
|
363 |
|
|
|
364 | }); | |
|
413 | }; | |
|
414 | ||
|
415 | AddHandler( | |
|
416 | resultHandler, | |
|
417 | errorHandler, | |
|
418 | medium.InternalCancel | |
|
419 | ); | |
|
365 | 420 | |
|
366 | 421 | return medium; |
|
367 | 422 | } |
|
368 | 423 | |
|
369 | 424 | public Promise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained) { |
|
370 | 425 | return Chain(chained, null); |
|
371 | 426 | } |
|
372 | 427 | |
|
373 | 428 | public Promise<T> Cancelled(Action handler) { |
|
374 | if (handler == null) | |
|
375 | return this; | |
|
376 | lock (m_lock) { | |
|
377 | if (m_state == PromiseState.Unresolved) | |
|
378 | m_cancelHandlers.AddLast(handler); | |
|
379 | else if (m_state == PromiseState.Cancelled) | |
|
380 | handler(); | |
|
381 | } | |
|
429 | AddHandler(null, null, handler); | |
|
382 | 430 | return this; |
|
383 | 431 | } |
|
384 | 432 | |
|
385 |
public |
|
|
386 |
|
|
|
433 | public Promise<T> Finally(Action handler) { | |
|
434 | if (handler == null) | |
|
435 | throw new ArgumentNullException("handler"); | |
|
436 | AddHandler( | |
|
437 | x => handler(), | |
|
438 | e => handler(), | |
|
439 | handler | |
|
440 | ); | |
|
441 | return this; | |
|
387 | 442 | } |
|
388 | 443 | |
|
389 | 444 | /// <summary> |
|
390 | 445 | /// ΠΠΎΠΆΠΈΠ΄Π°Π΅ΡΡΡ ΠΎΡΠ»ΠΎΠΆΠ΅Π½Π½ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈ Π² ΡΠ»ΡΡΠ°Π΅ ΡΡΠΏΠ΅Ρ Π°, Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅Ρ |
|
391 | 446 | /// Π΅Π³ΠΎ, ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ, Π² ΠΏΡΠΎΡΠΈΠ²Π½ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ Π±ΡΠΎΡΠ°Π΅Ρ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅. |
|
392 | 447 | /// </summary> |
|
393 | 448 | /// <remarks> |
|
394 | 449 | /// <para> |
|
395 | 450 | /// ΠΡΠ»ΠΈ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ Π±ΡΠ»ΠΎ ΠΏΡΠ΅ΡΠ²Π°Π½ΠΎ ΠΏΠΎ ΡΠ°ΠΉΠΌΠ°ΡΡΡ, ΡΡΠΎ Π½Π΅ Π·Π½Π°ΡΠΈΡ, |
|
396 | 451 | /// ΡΡΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ»ΠΎ ΠΎΡΠΌΠ΅Π½Π΅Π½ΠΎ ΠΈΠ»ΠΈ ΡΡΠΎ-ΡΠΎ Π² ΡΡΠΎΠΌ ΡΠΎΠ΄Π΅, ΡΡΠΎ ΡΠΎΠ»ΡΠΊΠΎ |
|
397 | 452 | /// ΠΎΠ·Π½Π°ΡΠ°Π΅Ρ, ΡΡΠΎ ΠΌΡ Π΅Π³ΠΎ Π½Π΅ Π΄ΠΎΠΆΠ΄Π°Π»ΠΈΡΡ, ΠΎΠ΄Π½Π°ΠΊΠΎ Π²ΡΠ΅ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΠ΅ |
|
398 | 453 | /// ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ, ΠΊΠ°ΠΊ Π±ΡΠ»ΠΈ ΡΠ°ΠΊ ΠΎΡΡΠ°Π»ΠΈΡΡ ΠΈ ΠΎΠ½ΠΈ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ, ΠΊΠΎΠ³Π΄Π° |
|
399 | 454 | /// ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ. |
|
400 | 455 | /// </para> |
|
401 | 456 | /// <para> |
|
402 | 457 | /// Π’Π°ΠΊΠΎΠ΅ ΠΏΠΎΠ²Π΅Π΄Π΅Π½ΠΈΠ΅ Π²ΠΏΠΎΠ»Π½Π΅ ΠΎΠΏΡΠ°Π²Π΄Π°Π½ΠΎ ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΡΠ°ΠΉΠΌΠ°ΡΡ ΠΌΠΎΠΆΠ΅Ρ ΠΈΡΡΠ΅ΡΡ |
|
403 | 458 | /// Π² ΡΠΎΡ ΠΌΠΎΠΌΠ΅Π½Ρ, ΠΊΠΎΠ³Π΄Π° Π½Π°ΡΠ°Π»Π°ΡΡ ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΠ° ΡΠ΅ΠΏΠΎΡΠΊΠΈ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΎΠ², ΠΈ |
|
404 | 459 | /// ΠΊ ΡΠΎΠΌΡ ΠΆΠ΅ ΡΠ΅ΠΊΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΡΡΠΎΡΡΡ Π² ΡΠ΅ΠΏΠΎΡΠΊΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ ΠΈ Π΅Π³ΠΎ |
|
405 | 460 | /// ΠΎΡΠΊΠ»ΠΎΠ½Π΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΠΏΡΠΈΠ²Π΅ΡΡΠΈ ΠΊ Π½Π΅ΠΏΡΠΎΠ³Π½ΠΎΠ·ΠΈΡΡΠ΅ΠΌΠΎΠΌΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ. |
|
406 | 461 | /// </para> |
|
407 | 462 | /// </remarks> |
|
408 | 463 | /// <param name="timeout">ΠΡΠ΅ΠΌΡ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΡ</param> |
|
409 | 464 | /// <returns>Π Π΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ</returns> |
|
410 | 465 | public T Join(int timeout) { |
|
411 | 466 | var evt = new ManualResetEvent(false); |
|
412 | 467 | Anyway(() => evt.Set()); |
|
413 | 468 | Cancelled(() => evt.Set()); |
|
414 | 469 | |
|
415 | 470 | if (!evt.WaitOne(timeout, true)) |
|
416 | 471 | throw new TimeoutException(); |
|
417 | 472 | |
|
418 |
switch ( |
|
|
419 |
case |
|
|
473 | switch (m_state) { | |
|
474 | case ResolvedState: | |
|
420 | 475 | return m_result; |
|
421 |
case |
|
|
476 | case CancelledState: | |
|
422 | 477 | throw new OperationCanceledException(); |
|
423 |
case |
|
|
478 | case RejectedState: | |
|
424 | 479 | throw new TargetInvocationException(m_error); |
|
425 | 480 | default: |
|
426 |
throw new ApplicationException(String.Format("Invalid promise state {0}", |
|
|
481 | throw new ApplicationException(String.Format("Invalid promise state {0}", m_state)); | |
|
427 | 482 | } |
|
428 | 483 | } |
|
429 | 484 | |
|
430 | 485 | public T Join() { |
|
431 | 486 | return Join(Timeout.Infinite); |
|
432 | 487 | } |
|
433 | 488 | |
|
434 |
void AddHandler(ResultHandler |
|
|
435 | bool invokeRequired = false; | |
|
489 | void AddHandler(ResultHandler<T> success, ErrorHandler error, Action cancel) { | |
|
490 | Interlocked.Increment(ref m_childrenCount); | |
|
491 | ||
|
492 | HandlerDescriptor handler = new HandlerDescriptor { | |
|
493 | resultHandler = success, | |
|
494 | errorHandler = error, | |
|
495 | cancellHandler = cancel | |
|
496 | }; | |
|
436 | 497 | |
|
437 | lock (m_lock) { | |
|
438 | m_childrenCount++; | |
|
439 | if (m_state == PromiseState.Unresolved) { | |
|
440 | m_resultHandlers.AddLast(handler); | |
|
441 | } else | |
|
442 | invokeRequired = true; | |
|
443 | } | |
|
498 | bool queued; | |
|
444 | 499 | |
|
445 | // ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ Π½Π΅ Π΄ΠΎΠ»ΠΆΠ½Ρ Π±Π»ΠΎΠΊΠΈΡΠΎΠ²Π°ΡΡ ΡΠ°ΠΌ ΠΎΠ±ΡΠ΅ΠΊΡ | |
|
446 | if (invokeRequired) | |
|
500 | if (!IsResolved) { | |
|
501 | m_handlers.Enqueue(handler); | |
|
502 | queued = true; | |
|
503 | } else { | |
|
504 | // the promise is in resolved state, just invoke the handled with minimum overhead | |
|
505 | queued = false; | |
|
447 | 506 | InvokeHandler(handler); |
|
448 | 507 | } |
|
449 | 508 | |
|
450 | void InvokeHandler(ResultHandlerInfo handler) { | |
|
509 | if (queued && IsResolved && m_handlers.TryDequeue(out handler)) | |
|
510 | // if the promise have been resolved while we was adding handler to the queue | |
|
511 | // we can't guarantee that someone is still processing it | |
|
512 | // therefore we will fetch a handler from the queue and execute it | |
|
513 | // note that fetched handler may be not the one we have added | |
|
514 | InvokeHandler(handler); | |
|
515 | ||
|
516 | } | |
|
517 | ||
|
518 | void InvokeHandler(HandlerDescriptor handler) { | |
|
451 | 519 | switch (m_state) { |
|
452 |
case |
|
|
453 | try { | |
|
454 | if (handler.resultHandler != null) | |
|
455 | handler.resultHandler(m_result); | |
|
456 | } catch (Exception e) { | |
|
457 | try { | |
|
458 | if (handler.errorHandler != null) | |
|
459 | handler.errorHandler(e); | |
|
460 | } catch { } | |
|
461 | } | |
|
520 | case ResolvedState: | |
|
521 | handler.Resolve(m_result); | |
|
462 | 522 | break; |
|
463 |
case |
|
|
464 |
|
|
|
465 | if (handler.errorHandler != null) | |
|
466 | handler.errorHandler(m_error); | |
|
467 |
|
|
|
523 | case RejectedState: | |
|
524 | handler.Reject(m_error); | |
|
525 | break; | |
|
526 | case CancelledState: | |
|
527 | handler.Cancel(); | |
|
468 | 528 | break; |
|
469 | 529 | default: |
|
470 | 530 | // do nothing |
|
471 | 531 | return; |
|
472 | 532 | } |
|
473 | 533 | } |
|
474 | 534 | |
|
475 | 535 | protected virtual void OnStateChanged() { |
|
476 | switch (m_state) { | |
|
477 | case PromiseState.Resolved: | |
|
478 | foreach (var resultHandlerInfo in m_resultHandlers) | |
|
479 | try { | |
|
480 | if (resultHandlerInfo.resultHandler != null) | |
|
481 | resultHandlerInfo.resultHandler(m_result); | |
|
482 | } catch (Exception e) { | |
|
483 | try { | |
|
484 | if (resultHandlerInfo.errorHandler != null) | |
|
485 | resultHandlerInfo.errorHandler(e); | |
|
486 | } catch { } | |
|
487 | } | |
|
488 | break; | |
|
489 | case PromiseState.Cancelled: | |
|
490 | foreach (var cancelHandler in m_cancelHandlers) | |
|
491 | cancelHandler(); | |
|
492 | break; | |
|
493 | case PromiseState.Rejected: | |
|
494 | foreach (var resultHandlerInfo in m_resultHandlers) | |
|
495 | try { | |
|
496 | if (resultHandlerInfo.errorHandler != null) | |
|
497 | resultHandlerInfo.errorHandler(m_error); | |
|
498 | } catch { } | |
|
499 | break; | |
|
500 | default: | |
|
501 | throw new InvalidOperationException(String.Format("Promise entered an invalid state {0}", m_state)); | |
|
502 | } | |
|
503 | ||
|
504 | m_resultHandlers = null; | |
|
505 | m_cancelHandlers = null; | |
|
536 | HandlerDescriptor handler; | |
|
537 | while (m_handlers.TryDequeue(out handler)) | |
|
538 | InvokeHandler(handler); | |
|
506 | 539 | } |
|
507 | 540 | |
|
508 | 541 | |
|
509 | 542 | |
|
510 | 543 | public bool IsExclusive { |
|
511 | 544 | get { |
|
512 | lock (m_lock) { | |
|
513 | 545 |
|
|
514 | 546 |
|
|
515 | 547 |
|
|
516 | } | |
|
517 | ||
|
518 | public PromiseState State { | |
|
519 | get { | |
|
520 | lock (m_lock) { | |
|
521 | return m_state; | |
|
522 | } | |
|
523 | } | |
|
524 | } | |
|
525 | 548 | |
|
526 | 549 | protected bool Cancel(bool dependencies) { |
|
527 | bool result; | |
|
528 | ||
|
529 | lock (m_lock) { | |
|
530 | if (m_state == PromiseState.Unresolved) { | |
|
531 | m_state = PromiseState.Cancelled; | |
|
532 | result = true; | |
|
533 | } else { | |
|
534 | result = false; | |
|
535 | } | |
|
536 | } | |
|
537 | ||
|
538 | if (result) | |
|
550 | if (BeginTransit()) { | |
|
551 | CompleteTransit(CancelledState); | |
|
539 | 552 | OnStateChanged(); |
|
540 | 553 | |
|
541 |
if (dependencies && m_parent != null && m_parent.IsExclusive) |
|
|
554 | if (dependencies && m_parent != null && m_parent.IsExclusive) | |
|
542 | 555 | m_parent.Cancel(); |
|
556 | ||
|
557 | return true; | |
|
558 | } else { | |
|
559 | return false; | |
|
543 | 560 | } |
|
544 | ||
|
545 | return result; | |
|
546 | 561 | } |
|
547 | 562 | |
|
548 | 563 | } |
|
549 | 564 | } |
General Comments 0
You need to be logged in to leave comments.
Login now