@@ -1,779 +1,852 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Reflection; |
|
3 | 3 | using System.Threading; |
|
4 | 4 | using Implab.Parallels; |
|
5 | 5 | |
|
6 | 6 | #if MONO |
|
7 | 7 | |
|
8 | 8 | using NUnit.Framework; |
|
9 | 9 | using TestClassAttribute = NUnit.Framework.TestFixtureAttribute; |
|
10 | 10 | using TestMethod = NUnit.Framework.TestAttribute; |
|
11 | 11 | |
|
12 | 12 | #else |
|
13 | 13 | |
|
14 | 14 | using Microsoft.VisualStudio.TestTools.UnitTesting; |
|
15 | 15 | |
|
16 | 16 | #endif |
|
17 | 17 | |
|
18 | 18 | namespace Implab.Test { |
|
19 | 19 | [TestClass] |
|
20 | 20 | public class AsyncTests { |
|
21 | 21 | [TestMethod] |
|
22 | 22 | public void ResolveTest() { |
|
23 | 23 | int res = -1; |
|
24 | 24 | var p = new Promise<int>(); |
|
25 | 25 | p.Then(x => res = x); |
|
26 | 26 | p.Resolve(100); |
|
27 | 27 | |
|
28 | 28 | Assert.AreEqual(100, res); |
|
29 | 29 | } |
|
30 | 30 | |
|
31 | 31 | [TestMethod] |
|
32 | 32 | public void RejectTest() { |
|
33 | 33 | int res = -1; |
|
34 | 34 | Exception err = null; |
|
35 | 35 | |
|
36 | 36 | var p = new Promise<int>(); |
|
37 | 37 | p.Then( |
|
38 | 38 | x => res = x, |
|
39 | 39 | e => { |
|
40 | 40 | err = e; |
|
41 | 41 | return -2; |
|
42 | 42 | } |
|
43 | 43 | ); |
|
44 | 44 | p.Reject(new ApplicationException("error")); |
|
45 | 45 | |
|
46 | 46 | Assert.AreEqual(res, -1); |
|
47 | 47 | Assert.AreEqual(err.Message, "error"); |
|
48 | 48 | |
|
49 | 49 | } |
|
50 | 50 | |
|
51 | 51 | [TestMethod] |
|
52 | 52 | public void CancelExceptionTest() { |
|
53 | 53 | var p = new Promise<bool>(); |
|
54 | 54 | p.Cancel(); |
|
55 | 55 | |
|
56 | 56 | var p2 = p.Cancelled(() => { |
|
57 | 57 | throw new ApplicationException("CANCELLED"); |
|
58 | 58 | }); |
|
59 | 59 | |
|
60 | 60 | try { |
|
61 | 61 | p2.Join(); |
|
62 | 62 | Assert.Fail(); |
|
63 | 63 | } catch (ApplicationException err) { |
|
64 | 64 | Assert.AreEqual("CANCELLED", err.InnerException.Message); |
|
65 | 65 | } |
|
66 | 66 | |
|
67 | 67 | } |
|
68 | 68 | |
|
69 | 69 | [TestMethod] |
|
70 | 70 | public void ContinueOnCancelTest() { |
|
71 | 71 | var p = new Promise<bool>(); |
|
72 | 72 | p.Cancel(); |
|
73 | 73 | |
|
74 | 74 | var p2 = p |
|
75 | 75 | .Cancelled<bool>(() => { |
|
76 | 76 | throw new ApplicationException("CANCELLED"); |
|
77 | 77 | }) |
|
78 | 78 | .Error(e => true); |
|
79 | 79 | |
|
80 | 80 | Assert.AreEqual(true, p2.Join()); |
|
81 | 81 | } |
|
82 | 82 | |
|
83 | 83 | [TestMethod] |
|
84 | 84 | public void JoinSuccessTest() { |
|
85 | 85 | var p = new Promise<int>(); |
|
86 | 86 | p.Resolve(100); |
|
87 | 87 | Assert.AreEqual(p.Join(), 100); |
|
88 | 88 | } |
|
89 | 89 | |
|
90 | 90 | [TestMethod] |
|
91 | 91 | public void JoinFailTest() { |
|
92 | 92 | var p = new Promise<int>(); |
|
93 | 93 | p.Reject(new ApplicationException("failed")); |
|
94 | 94 | |
|
95 | 95 | try { |
|
96 | 96 | p.Join(); |
|
97 | 97 | throw new ApplicationException("WRONG!"); |
|
98 | 98 | } catch (TargetInvocationException err) { |
|
99 | 99 | Assert.AreEqual(err.InnerException.Message, "failed"); |
|
100 | 100 | } catch { |
|
101 | 101 | Assert.Fail("Got wrong excaption"); |
|
102 | 102 | } |
|
103 | 103 | } |
|
104 | 104 | |
|
105 | 105 | [TestMethod] |
|
106 | 106 | public void MapTest() { |
|
107 | 107 | var p = new Promise<int>(); |
|
108 | 108 | |
|
109 | 109 | var p2 = p.Then(x => x.ToString()); |
|
110 | 110 | p.Resolve(100); |
|
111 | 111 | |
|
112 | 112 | Assert.AreEqual(p2.Join(), "100"); |
|
113 | 113 | } |
|
114 | 114 | |
|
115 | 115 | [TestMethod] |
|
116 | 116 | public void FixErrorTest() { |
|
117 | 117 | var p = new Promise<int>(); |
|
118 | 118 | |
|
119 | 119 | var p2 = p.Error(e => 101); |
|
120 | 120 | |
|
121 | 121 | p.Reject(new Exception()); |
|
122 | 122 | |
|
123 | 123 | Assert.AreEqual(p2.Join(), 101); |
|
124 | 124 | } |
|
125 | 125 | |
|
126 | 126 | [TestMethod] |
|
127 | 127 | public void ChainTest() { |
|
128 | 128 | var p1 = new Promise<int>(); |
|
129 | 129 | |
|
130 | 130 | var p3 = p1.Chain(x => { |
|
131 | 131 | var p2 = new Promise<string>(); |
|
132 | 132 | p2.Resolve(x.ToString()); |
|
133 | 133 | return p2; |
|
134 | 134 | }); |
|
135 | 135 | |
|
136 | 136 | p1.Resolve(100); |
|
137 | 137 | |
|
138 | 138 | Assert.AreEqual(p3.Join(), "100"); |
|
139 | 139 | } |
|
140 | 140 | |
|
141 | 141 | [TestMethod] |
|
142 | 142 | public void ChainFailTest() { |
|
143 | 143 | var p1 = new Promise<int>(); |
|
144 | 144 | |
|
145 | 145 | var p3 = p1.Chain(x => { |
|
146 | 146 | var p2 = new Promise<string>(); |
|
147 | 147 | p2.Reject(new Exception("DIE!!!")); |
|
148 | 148 | return p2; |
|
149 | 149 | }); |
|
150 | 150 | |
|
151 | 151 | p1.Resolve(100); |
|
152 | 152 | |
|
153 | 153 | Assert.IsTrue(p3.IsResolved); |
|
154 | 154 | } |
|
155 | 155 | |
|
156 | 156 | [TestMethod] |
|
157 | 157 | public void PoolTest() { |
|
158 | 158 | var pid = Thread.CurrentThread.ManagedThreadId; |
|
159 | 159 | var p = AsyncPool.Invoke(() => Thread.CurrentThread.ManagedThreadId); |
|
160 | 160 | |
|
161 | 161 | Assert.AreNotEqual(pid, p.Join()); |
|
162 | 162 | } |
|
163 | 163 | |
|
164 | 164 | [TestMethod] |
|
165 | 165 | public void WorkerPoolSizeTest() { |
|
166 | 166 | var pool = new WorkerPool(5, 10, 1); |
|
167 | 167 | |
|
168 | 168 | Assert.AreEqual(5, pool.PoolSize); |
|
169 | 169 | |
|
170 | 170 | pool.Invoke(() => { Thread.Sleep(100000000); return 10; }); |
|
171 | 171 | pool.Invoke(() => { Thread.Sleep(100000000); return 10; }); |
|
172 | 172 | pool.Invoke(() => { Thread.Sleep(100000000); return 10; }); |
|
173 | 173 | |
|
174 | 174 | Assert.AreEqual(5, pool.PoolSize); |
|
175 | 175 | |
|
176 | 176 | for (int i = 0; i < 100; i++) |
|
177 | 177 | pool.Invoke(() => { Thread.Sleep(100000000); return 10; }); |
|
178 | 178 | Thread.Sleep(200); |
|
179 | 179 | Assert.AreEqual(10, pool.PoolSize); |
|
180 | 180 | |
|
181 | 181 | pool.Dispose(); |
|
182 | 182 | } |
|
183 | 183 | |
|
184 | 184 | [TestMethod] |
|
185 | 185 | public void WorkerPoolCorrectTest() { |
|
186 | 186 | var pool = new WorkerPool(0,1000,100); |
|
187 | 187 | |
|
188 | 188 | const int iterations = 1000; |
|
189 | 189 | int pending = iterations; |
|
190 | 190 | var stop = new ManualResetEvent(false); |
|
191 | 191 | |
|
192 | 192 | var count = 0; |
|
193 | 193 | for (int i = 0; i < iterations; i++) { |
|
194 | 194 | pool |
|
195 | 195 | .Invoke(() => 1) |
|
196 | 196 | .Then(x => Interlocked.Add(ref count, x)) |
|
197 | 197 | .Then(x => Math.Log10(x)) |
|
198 | 198 | .On(() => { |
|
199 | 199 | Interlocked.Decrement(ref pending); |
|
200 | 200 | if (pending == 0) |
|
201 | 201 | stop.Set(); |
|
202 | 202 | }, PromiseEventType.All); |
|
203 | 203 | } |
|
204 | 204 | |
|
205 | 205 | stop.WaitOne(); |
|
206 | 206 | |
|
207 | 207 | Assert.AreEqual(iterations, count); |
|
208 | 208 | Console.WriteLine("Max threads: {0}", pool.MaxRunningThreads); |
|
209 | 209 | pool.Dispose(); |
|
210 | 210 | |
|
211 | 211 | } |
|
212 | 212 | |
|
213 | 213 | [TestMethod] |
|
214 | 214 | public void WorkerPoolDisposeTest() { |
|
215 | 215 | var pool = new WorkerPool(5, 20); |
|
216 | 216 | Assert.AreEqual(5, pool.PoolSize); |
|
217 | 217 | pool.Dispose(); |
|
218 | 218 | Thread.Sleep(500); |
|
219 | 219 | Assert.AreEqual(0, pool.PoolSize); |
|
220 | 220 | pool.Dispose(); |
|
221 | 221 | } |
|
222 | 222 | |
|
223 | 223 | [TestMethod] |
|
224 | 224 | public void MTQueueTest() { |
|
225 | 225 | var queue = new MTQueue<int>(); |
|
226 | 226 | int res; |
|
227 | 227 | |
|
228 | 228 | queue.Enqueue(10); |
|
229 | 229 | Assert.IsTrue(queue.TryDequeue(out res)); |
|
230 | 230 | Assert.AreEqual(10, res); |
|
231 | 231 | Assert.IsFalse(queue.TryDequeue(out res)); |
|
232 | 232 | |
|
233 | 233 | for (int i = 0; i < 1000; i++) |
|
234 | 234 | queue.Enqueue(i); |
|
235 | 235 | |
|
236 | 236 | for (int i = 0; i < 1000; i++) { |
|
237 | 237 | queue.TryDequeue(out res); |
|
238 | 238 | Assert.AreEqual(i, res); |
|
239 | 239 | } |
|
240 | 240 | |
|
241 | 241 | int writers = 0; |
|
242 | 242 | int readers = 0; |
|
243 | 243 | var stop = new ManualResetEvent(false); |
|
244 | 244 | int total = 0; |
|
245 | 245 | |
|
246 | 246 | const int itemsPerWriter = 10000; |
|
247 | 247 | const int writersCount = 10; |
|
248 | 248 | |
|
249 | 249 | for (int i = 0; i < writersCount; i++) { |
|
250 | 250 | Interlocked.Increment(ref writers); |
|
251 | 251 | AsyncPool |
|
252 | 252 | .RunThread(() => { |
|
253 | 253 | for (int ii = 0; ii < itemsPerWriter; ii++) { |
|
254 | 254 | queue.Enqueue(1); |
|
255 | 255 | } |
|
256 | 256 | return 1; |
|
257 | 257 | }) |
|
258 | 258 | .On(() => Interlocked.Decrement(ref writers), PromiseEventType.All); |
|
259 | 259 | } |
|
260 | 260 | |
|
261 | 261 | for (int i = 0; i < 10; i++) { |
|
262 | 262 | Interlocked.Increment(ref readers); |
|
263 | 263 | AsyncPool |
|
264 | 264 | .RunThread(() => { |
|
265 | 265 | int t; |
|
266 | 266 | do { |
|
267 | 267 | while (queue.TryDequeue(out t)) |
|
268 | 268 | Interlocked.Add(ref total, t); |
|
269 | 269 | } while (writers > 0); |
|
270 | 270 | return 1; |
|
271 | 271 | }) |
|
272 | 272 | .On(() => { |
|
273 | 273 | Interlocked.Decrement(ref readers); |
|
274 | 274 | if (readers == 0) |
|
275 | 275 | stop.Set(); |
|
276 | 276 | }, PromiseEventType.All); |
|
277 | 277 | } |
|
278 | 278 | |
|
279 | 279 | stop.WaitOne(); |
|
280 | 280 | |
|
281 | 281 | Assert.AreEqual(100000, total); |
|
282 | 282 | } |
|
283 | 283 | |
|
284 | 284 | [TestMethod] |
|
285 | 285 | public void AsyncQueueTest() { |
|
286 | 286 | var queue = new AsyncQueue<int>(); |
|
287 | 287 | int res; |
|
288 | 288 | |
|
289 | 289 | queue.Enqueue(10); |
|
290 | 290 | Assert.IsTrue(queue.TryDequeue(out res)); |
|
291 | 291 | Assert.AreEqual(10, res); |
|
292 | 292 | Assert.IsFalse(queue.TryDequeue(out res)); |
|
293 | 293 | |
|
294 | 294 | for (int i = 0; i < 1000; i++) |
|
295 | 295 | queue.Enqueue(i); |
|
296 | 296 | |
|
297 | 297 | for (int i = 0; i < 1000; i++) { |
|
298 | 298 | queue.TryDequeue(out res); |
|
299 | 299 | Assert.AreEqual(i, res); |
|
300 | 300 | } |
|
301 | 301 | |
|
302 | 302 | const int count = 10000000; |
|
303 | 303 | |
|
304 | 304 | int res1 = 0, res2 = 0; |
|
305 | 305 | var t1 = Environment.TickCount; |
|
306 | 306 | |
|
307 | 307 | AsyncPool.RunThread( |
|
308 | 308 | () => { |
|
309 | 309 | for (var i = 0; i < count; i++) |
|
310 | 310 | queue.Enqueue(1); |
|
311 | 311 | Console.WriteLine("done writer #1: {0} ms", Environment.TickCount - t1); |
|
312 | 312 | }, |
|
313 | 313 | () => { |
|
314 | 314 | for (var i = 0; i < count; i++) |
|
315 | 315 | queue.Enqueue(2); |
|
316 | 316 | Console.WriteLine("done writer #2: {0} ms", Environment.TickCount - t1); |
|
317 | 317 | }, |
|
318 | 318 | () => { |
|
319 | 319 | int temp; |
|
320 | 320 | int i = 0; |
|
321 | 321 | while (i < count) |
|
322 | 322 | if (queue.TryDequeue(out temp)) { |
|
323 | 323 | i++; |
|
324 | 324 | res1 += temp; |
|
325 | 325 | } |
|
326 | 326 | Console.WriteLine("done reader #1: {0} ms", Environment.TickCount - t1); |
|
327 | 327 | }, |
|
328 | 328 | () => { |
|
329 | 329 | int temp; |
|
330 | 330 | int i = 0; |
|
331 | 331 | while (i < count) |
|
332 | 332 | if (queue.TryDequeue(out temp)) { |
|
333 | 333 | i++; |
|
334 | 334 | res2 += temp; |
|
335 | 335 | } |
|
336 | 336 | Console.WriteLine("done reader #2: {0} ms", Environment.TickCount - t1); |
|
337 | 337 | } |
|
338 | 338 | ) |
|
339 | 339 | .Bundle() |
|
340 | 340 | .Join(); |
|
341 | 341 | |
|
342 | 342 | Assert.AreEqual(count * 3, res1 + res2); |
|
343 | 343 | |
|
344 | 344 | Console.WriteLine( |
|
345 | 345 | "done: {0} ms, summ#1: {1}, summ#2: {2}, total: {3}, count: {4}", |
|
346 | 346 | Environment.TickCount - t1, |
|
347 | 347 | res1, |
|
348 | 348 | res2, |
|
349 | 349 | res1 + res2, |
|
350 | 350 | count |
|
351 | 351 | ); |
|
352 | 352 | } |
|
353 | 353 | |
|
354 | 354 | [TestMethod] |
|
355 | 355 | public void AsyncQueueBatchTest() { |
|
356 | 356 | var queue = new AsyncQueue<int>(); |
|
357 | 357 | |
|
358 | 358 | const int wBatch = 29; |
|
359 | 359 | const int wCount = 400000; |
|
360 | 360 | const int total = wBatch * wCount * 2; |
|
361 | 361 | const int summ = wBatch * wCount * 3; |
|
362 | 362 | |
|
363 | 363 | int r1 = 0, r2 = 0; |
|
364 | 364 | const int rBatch = 111; |
|
365 | 365 | int read = 0; |
|
366 | 366 | |
|
367 | 367 | var t1 = Environment.TickCount; |
|
368 | 368 | |
|
369 | 369 | AsyncPool.RunThread( |
|
370 | 370 | () => { |
|
371 | 371 | var buffer = new int[wBatch]; |
|
372 | 372 | for(int i = 0; i<wBatch; i++) |
|
373 | 373 | buffer[i] = 1; |
|
374 | 374 | |
|
375 | 375 | for(int i =0; i < wCount; i++) |
|
376 | 376 | queue.EnqueueRange(buffer,0,wBatch); |
|
377 | 377 | Console.WriteLine("done writer #1: {0} ms", Environment.TickCount - t1); |
|
378 | 378 | }, |
|
379 | 379 | () => { |
|
380 | 380 | var buffer = new int[wBatch]; |
|
381 | 381 | for(int i = 0; i<wBatch; i++) |
|
382 | 382 | buffer[i] = 2; |
|
383 | 383 | |
|
384 | 384 | for(int i =0; i < wCount; i++) |
|
385 | 385 | queue.EnqueueRange(buffer,0,wBatch); |
|
386 | 386 | Console.WriteLine("done writer #2: {0} ms", Environment.TickCount - t1); |
|
387 | 387 | }, |
|
388 | 388 | () => { |
|
389 | 389 | var buffer = new int[rBatch]; |
|
390 | 390 | |
|
391 | 391 | while(read < total) { |
|
392 | 392 | int actual; |
|
393 | 393 | if (queue.TryDequeueRange(buffer,0,rBatch,out actual)) { |
|
394 | 394 | for(int i=0; i< actual; i++) |
|
395 | 395 | r1 += buffer[i]; |
|
396 | 396 | Interlocked.Add(ref read, actual); |
|
397 | 397 | } |
|
398 | 398 | } |
|
399 | 399 | |
|
400 | 400 | Console.WriteLine("done reader #1: {0} ms", Environment.TickCount - t1); |
|
401 | 401 | }, |
|
402 | 402 | () => { |
|
403 | 403 | var buffer = new int[rBatch]; |
|
404 | 404 | |
|
405 | 405 | while(read < total) { |
|
406 | 406 | int actual; |
|
407 | 407 | if (queue.TryDequeueRange(buffer,0,rBatch,out actual)) { |
|
408 | 408 | for(int i=0; i< actual; i++) |
|
409 | 409 | r2 += buffer[i]; |
|
410 | 410 | Interlocked.Add(ref read, actual); |
|
411 | 411 | } |
|
412 | 412 | } |
|
413 | 413 | |
|
414 | 414 | Console.WriteLine("done reader #2: {0} ms", Environment.TickCount - t1); |
|
415 | 415 | } |
|
416 | 416 | ) |
|
417 | 417 | .Bundle() |
|
418 | 418 | .Join(); |
|
419 | 419 | |
|
420 | 420 | Assert.AreEqual(summ , r1 + r2); |
|
421 | 421 | |
|
422 | 422 | Console.WriteLine( |
|
423 | 423 | "done: {0} ms, summ#1: {1}, summ#2: {2}, total: {3}, count: {4}", |
|
424 | 424 | Environment.TickCount - t1, |
|
425 | 425 | r1, |
|
426 | 426 | r2, |
|
427 | 427 | r1 + r2, |
|
428 | 428 | total |
|
429 | 429 | ); |
|
430 | 430 | } |
|
431 | 431 | |
|
432 | 432 | [TestMethod] |
|
433 | 433 | public void AsyncQueueChunkDequeueTest() { |
|
434 | 434 | var queue = new AsyncQueue<int>(); |
|
435 | 435 | |
|
436 | 436 | const int wBatch = 31; |
|
437 | 437 | const int wCount = 200000; |
|
438 | 438 | const int total = wBatch * wCount * 3; |
|
439 | 439 | const int summ = wBatch * wCount * 6; |
|
440 | 440 | |
|
441 | 441 | int r1 = 0, r2 = 0; |
|
442 | 442 | const int rBatch = 1024; |
|
443 | 443 | int read = 0; |
|
444 | 444 | |
|
445 | 445 | var t1 = Environment.TickCount; |
|
446 | 446 | |
|
447 | 447 | AsyncPool.RunThread( |
|
448 | 448 | () => { |
|
449 | 449 | var buffer = new int[wBatch]; |
|
450 | 450 | for(int i = 0; i<wBatch; i++) |
|
451 | 451 | buffer[i] = 1; |
|
452 | 452 | |
|
453 | 453 | for(int i =0; i < wCount; i++) |
|
454 | 454 | queue.EnqueueRange(buffer,0,wBatch); |
|
455 | 455 | Console.WriteLine("done writer #1: {0} ms", Environment.TickCount - t1); |
|
456 | 456 | }, |
|
457 | 457 | () => { |
|
458 | 458 | var buffer = new int[wBatch]; |
|
459 | 459 | for(int i = 0; i<wBatch; i++) |
|
460 | 460 | buffer[i] = 2; |
|
461 | 461 | |
|
462 | 462 | for(int i =0; i < wCount; i++) |
|
463 | 463 | queue.EnqueueRange(buffer,0,wBatch); |
|
464 | 464 | Console.WriteLine("done writer #2: {0} ms", Environment.TickCount - t1); |
|
465 | 465 | }, |
|
466 | 466 | () => { |
|
467 | 467 | var buffer = new int[wBatch]; |
|
468 | 468 | for(int i = 0; i<wBatch; i++) |
|
469 | 469 | buffer[i] = 3; |
|
470 | 470 | |
|
471 | 471 | for(int i =0; i < wCount; i++) |
|
472 | 472 | queue.EnqueueRange(buffer,0,wBatch); |
|
473 | 473 | Console.WriteLine("done writer #3: {0} ms", Environment.TickCount - t1); |
|
474 | 474 | }, |
|
475 | 475 | () => { |
|
476 | 476 | var buffer = new int[rBatch]; |
|
477 | 477 | int count = 1; |
|
478 | 478 | double avgchunk = 0; |
|
479 | 479 | while(read < total) { |
|
480 | 480 | int actual; |
|
481 | 481 | if (queue.TryDequeueChunk(buffer,0,rBatch,out actual)) { |
|
482 | 482 | for(int i=0; i< actual; i++) |
|
483 | 483 | r2 += buffer[i]; |
|
484 | 484 | Interlocked.Add(ref read, actual); |
|
485 | 485 | avgchunk = avgchunk*(count-1)/count + actual/(double)count; |
|
486 | 486 | count ++; |
|
487 | 487 | } |
|
488 | 488 | } |
|
489 | 489 | |
|
490 | 490 | Console.WriteLine("done reader #2: {0} ms, avg chunk size: {1}", Environment.TickCount - t1, avgchunk); |
|
491 | 491 | } |
|
492 | 492 | ) |
|
493 | 493 | .Bundle() |
|
494 | 494 | .Join(); |
|
495 | 495 | |
|
496 | 496 | Assert.AreEqual(summ , r1 + r2); |
|
497 | 497 | |
|
498 | 498 | Console.WriteLine( |
|
499 | 499 | "done: {0} ms, summ#1: {1}, summ#2: {2}, total: {3}, count: {4}", |
|
500 | 500 | Environment.TickCount - t1, |
|
501 | 501 | r1, |
|
502 | 502 | r2, |
|
503 | 503 | r1 + r2, |
|
504 | 504 | total |
|
505 | 505 | ); |
|
506 | 506 | } |
|
507 | 507 | |
|
508 | 508 | [TestMethod] |
|
509 | 509 | public void AsyncQueueDrainTest() { |
|
510 | 510 | var queue = new AsyncQueue<int>(); |
|
511 | 511 | |
|
512 | 512 | const int wBatch = 11; |
|
513 | 513 | const int wCount = 200000; |
|
514 | 514 | const int total = wBatch * wCount * 3; |
|
515 | 515 | const int summ = wBatch * wCount * 3; |
|
516 | 516 | |
|
517 | 517 | int r1 = 0, r2 = 0; |
|
518 | 518 | const int rBatch = 11; |
|
519 | 519 | int read = 0; |
|
520 | 520 | |
|
521 | 521 | var t1 = Environment.TickCount; |
|
522 | 522 | |
|
523 | 523 | AsyncPool.RunThread( |
|
524 | 524 | () => { |
|
525 | 525 | var buffer = new int[wBatch]; |
|
526 | 526 | for(int i = 0; i<wBatch; i++) |
|
527 | 527 | buffer[i] = 1; |
|
528 | 528 | |
|
529 | 529 | for(int i =0; i < wCount; i++) |
|
530 | 530 | queue.EnqueueRange(buffer,0,wBatch); |
|
531 | 531 | Console.WriteLine("done writer #1: {0} ms", Environment.TickCount - t1); |
|
532 | 532 | }, |
|
533 | 533 | () => { |
|
534 | 534 | for(int i =0; i < wCount * wBatch; i++) |
|
535 | 535 | queue.Enqueue(1); |
|
536 | 536 | Console.WriteLine("done writer #2: {0} ms", Environment.TickCount - t1); |
|
537 | 537 | }, |
|
538 | 538 | () => { |
|
539 | 539 | var buffer = new int[wBatch]; |
|
540 | 540 | for(int i = 0; i<wBatch; i++) |
|
541 | 541 | buffer[i] = 1; |
|
542 | 542 | |
|
543 | 543 | for(int i =0; i < wCount; i++) |
|
544 | 544 | queue.EnqueueRange(buffer,0,wBatch); |
|
545 | 545 | Console.WriteLine("done writer #3: {0} ms", Environment.TickCount - t1); |
|
546 | 546 | }, |
|
547 | 547 | /*() => { |
|
548 | 548 | int temp; |
|
549 | 549 | int count = 0; |
|
550 | 550 | while (read < total) |
|
551 | 551 | if (queue.TryDequeue(out temp)) { |
|
552 | 552 | count++; |
|
553 | 553 | r1 += temp; |
|
554 | 554 | Interlocked.Increment(ref read); |
|
555 | 555 | } |
|
556 | 556 | Console.WriteLine("done reader #1: {0} ms, {1} count", Environment.TickCount - t1, count); |
|
557 | 557 | },*/ |
|
558 | 558 | /*() => { |
|
559 | 559 | var buffer = new int[rBatch]; |
|
560 | 560 | var count = 0; |
|
561 | 561 | while(read < total) { |
|
562 | 562 | int actual; |
|
563 | 563 | if (queue.TryDequeueRange(buffer,0,rBatch,out actual)) { |
|
564 | 564 | for(int i=0; i< actual; i++) |
|
565 | 565 | r1 += buffer[i]; |
|
566 | 566 | Interlocked.Add(ref read, actual); |
|
567 | 567 | count += actual; |
|
568 | 568 | } |
|
569 | 569 | } |
|
570 | 570 | |
|
571 | 571 | Console.WriteLine("done reader #1: {0} ms, {1} items", Environment.TickCount - t1, count); |
|
572 | 572 | },*/ |
|
573 | 573 | () => { |
|
574 | 574 | var count = 0; |
|
575 | 575 | while(read < total) { |
|
576 | 576 | var buffer = queue.Drain(); |
|
577 | 577 | for(int i=0; i< buffer.Length; i++) |
|
578 | 578 | r1 += buffer[i]; |
|
579 | 579 | Interlocked.Add(ref read, buffer.Length); |
|
580 | 580 | count += buffer.Length; |
|
581 | 581 | } |
|
582 | 582 | Console.WriteLine("done reader #1: {0} ms, {1} items", Environment.TickCount - t1, count); |
|
583 | 583 | }, |
|
584 | 584 | () => { |
|
585 | 585 | var count = 0; |
|
586 | 586 | while(read < total) { |
|
587 | 587 | var buffer = queue.Drain(); |
|
588 | 588 | for(int i=0; i< buffer.Length; i++) |
|
589 | 589 | r2 += buffer[i]; |
|
590 | 590 | Interlocked.Add(ref read, buffer.Length); |
|
591 | 591 | count += buffer.Length; |
|
592 | 592 | } |
|
593 | 593 | Console.WriteLine("done reader #2: {0} ms, {1} items", Environment.TickCount - t1, count); |
|
594 | 594 | } |
|
595 | 595 | ) |
|
596 | 596 | .Bundle() |
|
597 | 597 | .Join(); |
|
598 | 598 | |
|
599 | 599 | Assert.AreEqual(summ , r1 + r2); |
|
600 | 600 | |
|
601 | 601 | Console.WriteLine( |
|
602 | 602 | "done: {0} ms, summ#1: {1}, summ#2: {2}, total: {3}, count: {4}", |
|
603 | 603 | Environment.TickCount - t1, |
|
604 | 604 | r1, |
|
605 | 605 | r2, |
|
606 | 606 | r1 + r2, |
|
607 | 607 | total |
|
608 | 608 | ); |
|
609 | 609 | } |
|
610 | 610 | |
|
611 | 611 | [TestMethod] |
|
612 | 612 | public void ParallelMapTest() { |
|
613 | 613 | |
|
614 | 614 | const int count = 100000; |
|
615 | 615 | |
|
616 | 616 | var args = new double[count]; |
|
617 | 617 | var rand = new Random(); |
|
618 | 618 | |
|
619 | 619 | for (int i = 0; i < count; i++) |
|
620 | 620 | args[i] = rand.NextDouble(); |
|
621 | 621 | |
|
622 | 622 | var t = Environment.TickCount; |
|
623 | 623 | var res = args.ParallelMap(x => Math.Sin(x*x), 4).Join(); |
|
624 | 624 | |
|
625 | 625 | Console.WriteLine("Map complete in {0} ms", Environment.TickCount - t); |
|
626 | 626 | |
|
627 | 627 | t = Environment.TickCount; |
|
628 | 628 | for (int i = 0; i < count; i++) |
|
629 | 629 | Assert.AreEqual(Math.Sin(args[i] * args[i]), res[i]); |
|
630 | 630 | Console.WriteLine("Verified in {0} ms", Environment.TickCount - t); |
|
631 | 631 | } |
|
632 | 632 | |
|
633 | 633 | [TestMethod] |
|
634 | 634 | public void ChainedMapTest() { |
|
635 | 635 | |
|
636 | 636 | using (var pool = new WorkerPool()) { |
|
637 | 637 | const int count = 10000; |
|
638 | 638 | |
|
639 | 639 | var args = new double[count]; |
|
640 | 640 | var rand = new Random(); |
|
641 | 641 | |
|
642 | 642 | for (int i = 0; i < count; i++) |
|
643 | 643 | args[i] = rand.NextDouble(); |
|
644 | 644 | |
|
645 | 645 | var t = Environment.TickCount; |
|
646 | 646 | var res = args |
|
647 | 647 | .ChainedMap( |
|
648 | 648 | // Analysis disable once AccessToDisposedClosure |
|
649 | 649 | x => pool.Invoke( |
|
650 | 650 | () => Math.Sin(x * x) |
|
651 | 651 | ), |
|
652 | 652 | 4 |
|
653 | 653 | ) |
|
654 | 654 | .Join(); |
|
655 | 655 | |
|
656 | 656 | Console.WriteLine("Map complete in {0} ms", Environment.TickCount - t); |
|
657 | 657 | |
|
658 | 658 | t = Environment.TickCount; |
|
659 | 659 | for (int i = 0; i < count; i++) |
|
660 | 660 | Assert.AreEqual(Math.Sin(args[i] * args[i]), res[i]); |
|
661 | 661 | Console.WriteLine("Verified in {0} ms", Environment.TickCount - t); |
|
662 | 662 | Console.WriteLine("Max workers: {0}", pool.MaxRunningThreads); |
|
663 | 663 | } |
|
664 | 664 | } |
|
665 | 665 | |
|
666 | 666 | [TestMethod] |
|
667 | 667 | public void ParallelForEachTest() { |
|
668 | 668 | |
|
669 | 669 | const int count = 100000; |
|
670 | 670 | |
|
671 | 671 | var args = new int[count]; |
|
672 | 672 | var rand = new Random(); |
|
673 | 673 | |
|
674 | 674 | for (int i = 0; i < count; i++) |
|
675 | 675 | args[i] = (int)(rand.NextDouble() * 100); |
|
676 | 676 | |
|
677 | 677 | int result = 0; |
|
678 | 678 | |
|
679 | 679 | var t = Environment.TickCount; |
|
680 | 680 | args.ParallelForEach(x => Interlocked.Add(ref result, x), 4).Join(); |
|
681 | 681 | |
|
682 | 682 | Console.WriteLine("Iteration complete in {0} ms, result: {1}", Environment.TickCount - t, result); |
|
683 | 683 | |
|
684 | 684 | int result2 = 0; |
|
685 | 685 | |
|
686 | 686 | t = Environment.TickCount; |
|
687 | 687 | for (int i = 0; i < count; i++) |
|
688 | 688 | result2 += args[i]; |
|
689 | 689 | Assert.AreEqual(result2, result); |
|
690 | 690 | Console.WriteLine("Verified in {0} ms", Environment.TickCount - t); |
|
691 | 691 | } |
|
692 | 692 | |
|
693 | 693 | [TestMethod] |
|
694 | 694 | public void ComplexCase1Test() { |
|
695 | 695 | var flags = new bool[3]; |
|
696 | 696 | |
|
697 | 697 | // op1 (aync 200ms) => op2 (async 200ms) => op3 (sync map) |
|
698 | 698 | |
|
699 | 699 | var step1 = PromiseHelper |
|
700 | 700 | .Sleep(200, "Alan") |
|
701 | 701 | .On(() => flags[0] = true, PromiseEventType.Cancelled); |
|
702 | 702 | var p = step1 |
|
703 | 703 | .Chain(x => |
|
704 | 704 | PromiseHelper |
|
705 | 705 | .Sleep(200, "Hi, " + x) |
|
706 | 706 | .Then(y => y) |
|
707 | 707 | .On(() => flags[1] = true, PromiseEventType.Cancelled) |
|
708 | 708 | ) |
|
709 | 709 | .On(() => flags[2] = true, PromiseEventType.Cancelled); |
|
710 | 710 | step1.Join(); |
|
711 | 711 | p.Cancel(); |
|
712 | 712 | try { |
|
713 | 713 | Assert.AreEqual(p.Join(), "Hi, Alan"); |
|
714 | 714 | Assert.Fail("Shouldn't get here"); |
|
715 | 715 | } catch (OperationCanceledException) { |
|
716 | 716 | } |
|
717 | 717 | |
|
718 | 718 | Assert.IsFalse(flags[0]); |
|
719 | 719 | Assert.IsTrue(flags[1]); |
|
720 | 720 | Assert.IsTrue(flags[2]); |
|
721 | 721 | } |
|
722 | 722 | |
|
723 | 723 | [TestMethod] |
|
724 | 724 | public void ChainedCancel1Test() { |
|
725 | 725 | // ΠΏΡΠΈ ΠΎΡΠΌΠ΅Π½Π΅ ΡΡΠ΅ΠΏΠ»Π΅Π½Π½ΠΎΠΉ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ Π²ΡΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π΄ΠΎΠ»ΠΆΠ½ΠΎ |
|
726 | 726 | // Π·Π°Π²Π΅ΡΡΠ°ΡΡΡΡ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ OperationCanceledException |
|
727 | 727 | var p = PromiseHelper |
|
728 | 728 | .Sleep(1, "Hi, HAL!") |
|
729 | 729 | .Then(x => { |
|
730 | 730 | // Π·Π°ΠΏΡΡΠΊΠ°Π΅ΠΌ Π΄Π²Π΅ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΡΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ |
|
731 | 731 | var result = PromiseHelper.Sleep(1000, "HEM ENABLED!!!"); |
|
732 | 732 | // Π²ΡΠΎΡΠ°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ ΠΎΡΠΌΠ΅Π½ΡΠ΅Ρ ΠΏΠ΅ΡΠ²ΡΡ Π΄ΠΎ Π·Π°Π²Π΅ΡΡΠ΅Π½ΠΈΡ |
|
733 | 733 | PromiseHelper |
|
734 | 734 | .Sleep(100, "HAL, STOP!") |
|
735 | 735 | .Then(result.Cancel); |
|
736 | 736 | return result; |
|
737 | 737 | }); |
|
738 | 738 | try { |
|
739 | 739 | p.Join(); |
|
740 | 740 | } catch (TargetInvocationException err) { |
|
741 | 741 | Assert.IsTrue(err.InnerException is OperationCanceledException); |
|
742 | 742 | } |
|
743 | 743 | } |
|
744 | 744 | |
|
745 | 745 | [TestMethod] |
|
746 | 746 | public void ChainedCancel2Test() { |
|
747 | 747 | // ΠΏΡΠΈ ΠΎΡΠΌΠ΅Π½Π΅ ΡΠ΅ΠΏΠΎΡΠΊΠΈ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ, Π²Π»ΠΎΠΆΠ΅Π½Π½ΡΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΡΠ°ΠΊΠΆΠ΅ Π΄ΠΎΠ»ΠΆΠ½Ρ ΠΎΡΠΌΠ΅Π½ΡΡΡΡΡ |
|
748 | 748 | var pSurvive = new Promise<bool>(); |
|
749 | 749 | var hemStarted = new ManualResetEvent(false); |
|
750 | 750 | var p = PromiseHelper |
|
751 | 751 | .Sleep(1, "Hi, HAL!") |
|
752 | 752 | .Chain(x => { |
|
753 | 753 | hemStarted.Set(); |
|
754 | 754 | // Π·Π°ΠΏΡΡΠΊΠ°Π΅ΠΌ Π΄Π²Π΅ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΡΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ |
|
755 | 755 | var result = PromiseHelper |
|
756 | 756 | .Sleep(100000000, "HEM ENABLED!!!") |
|
757 | 757 | .Then(s => { |
|
758 | 758 | pSurvive.Resolve(false); |
|
759 | 759 | return s; |
|
760 | 760 | }); |
|
761 | 761 | |
|
762 | 762 | result |
|
763 | 763 | .Cancelled(() => pSurvive.Resolve(true)); |
|
764 | 764 | |
|
765 | 765 | return result; |
|
766 | 766 | }); |
|
767 | 767 | |
|
768 | 768 | hemStarted.WaitOne(); |
|
769 | 769 | p.Cancel(); |
|
770 | 770 | |
|
771 | 771 | try { |
|
772 | 772 | p.Join(); |
|
773 | 773 | } catch (OperationCanceledException) { |
|
774 | 774 | Assert.IsTrue(pSurvive.Join()); |
|
775 | 775 | } |
|
776 | 776 | } |
|
777 | ||
|
778 | [TestMethod] | |
|
779 | public void SharedLockTest() { | |
|
780 | var l = new SharedLock(); | |
|
781 | int shared = 0; | |
|
782 | int exclusive = 0; | |
|
783 | var s1 = new Signal(); | |
|
784 | var log = new AsyncQueue<string>(); | |
|
785 | ||
|
786 | try { | |
|
787 | AsyncPool.RunThread( | |
|
788 | () => { | |
|
789 | log.Enqueue("Reader #1 started"); | |
|
790 | try { | |
|
791 | l.LockShared(); | |
|
792 | log.Enqueue("Reader #1 lock got"); | |
|
793 | if (Interlocked.Increment(ref shared) == 2) | |
|
794 | s1.Set(); | |
|
795 | s1.Wait(); | |
|
796 | log.Enqueue("Reader #1 finished"); | |
|
797 | Interlocked.Decrement(ref shared); | |
|
798 | } finally { | |
|
799 | l.Release(); | |
|
800 | log.Enqueue("Reader #1 lock released"); | |
|
801 | } | |
|
802 | }, | |
|
803 | () => { | |
|
804 | log.Enqueue("Reader #2 started"); | |
|
805 | ||
|
806 | try { | |
|
807 | l.LockShared(); | |
|
808 | log.Enqueue("Reader #2 lock got"); | |
|
809 | ||
|
810 | if (Interlocked.Increment(ref shared) == 2) | |
|
811 | s1.Set(); | |
|
812 | s1.Wait(); | |
|
813 | log.Enqueue("Reader #2 upgrading to writer"); | |
|
814 | Interlocked.Decrement(ref shared); | |
|
815 | l.Upgrade(); | |
|
816 | log.Enqueue("Reader #2 upgraded"); | |
|
817 | ||
|
818 | Assert.AreEqual(1, Interlocked.Increment(ref exclusive)); | |
|
819 | Assert.AreEqual(0, shared); | |
|
820 | log.Enqueue("Reader #2 finished"); | |
|
821 | Interlocked.Decrement(ref exclusive); | |
|
822 | } finally { | |
|
823 | l.Release(); | |
|
824 | log.Enqueue("Reader #2 lock released"); | |
|
825 | } | |
|
826 | }, | |
|
827 | () => { | |
|
828 | log.Enqueue("Writer #1 started"); | |
|
829 | try { | |
|
830 | l.LockExclusive(); | |
|
831 | log.Enqueue("Writer #1 got the lock"); | |
|
832 | Assert.AreEqual(1, Interlocked.Increment(ref exclusive)); | |
|
833 | Interlocked.Decrement(ref exclusive); | |
|
834 | log.Enqueue("Writer #1 is finished"); | |
|
835 | } finally { | |
|
836 | l.Release(); | |
|
837 | log.Enqueue("Writer #1 lock released"); | |
|
838 | } | |
|
839 | } | |
|
840 | ).Bundle().Join(1000); | |
|
841 | log.Enqueue("Done"); | |
|
842 | } catch(Exception error) { | |
|
843 | log.Enqueue(error.Message); | |
|
844 | throw; | |
|
845 | } finally { | |
|
846 | foreach (var m in log) | |
|
847 | Console.WriteLine(m); | |
|
848 | } | |
|
849 | } | |
|
777 | 850 | } |
|
778 | 851 | } |
|
779 | 852 |
@@ -1,76 +1,202 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Threading; |
|
3 | 3 | using System.Diagnostics; |
|
4 | 4 | |
|
5 | 5 | namespace Implab.Parallels { |
|
6 | 6 | /// <summary> |
|
7 | 7 | /// Implements a lightweight mechanism to aquire a shared or an exclusive lock. |
|
8 | 8 | /// </summary> |
|
9 | 9 | public class SharedLock { |
|
10 | 10 | readonly object m_lock = new object(); |
|
11 | // the count of locks currently acquired by clients | |
|
11 | 12 | int m_locks; |
|
13 | // the count of pending requests for upgrade | |
|
14 | int m_upgrades; | |
|
12 | 15 | bool m_exclusive; |
|
13 | 16 | |
|
14 | 17 | public bool LockExclusive(int timeout) { |
|
15 | 18 | lock (m_lock) { |
|
16 | if (m_locks > 0 && !Monitor.Wait(m_lock, timeout)) | |
|
17 | return false; | |
|
19 | var dt = timeout; | |
|
20 | if (m_locks > m_upgrades) { | |
|
21 | var t1 = Environment.TickCount; | |
|
22 | do { | |
|
23 | if (!Monitor.Wait(m_lock, timeout)) | |
|
24 | return false; | |
|
25 | ||
|
26 | if (m_locks == m_upgrades) | |
|
27 | break; | |
|
28 | ||
|
29 | if (timeout > 0) { | |
|
30 | dt = timeout - Environment.TickCount + t1; | |
|
31 | if (dt < 0) | |
|
32 | return false; | |
|
33 | } | |
|
34 | } while(true); | |
|
35 | } | |
|
18 | 36 | m_exclusive = true; |
|
19 |
m_locks |
|
|
37 | m_locks ++; | |
|
20 | 38 | return true; |
|
21 | 39 | } |
|
22 | 40 | } |
|
23 | 41 | |
|
24 | 42 | public void LockExclusive() { |
|
25 | LockExclusive(-1); | |
|
43 | lock (m_lock) { | |
|
44 | ||
|
45 | while (m_locks > m_upgrades) | |
|
46 | Monitor.Wait(m_lock); | |
|
47 | ||
|
48 | m_exclusive = true; | |
|
49 | m_locks ++; | |
|
50 | } | |
|
26 | 51 | } |
|
27 | 52 | |
|
53 | /// <summary> | |
|
54 | /// Acquires a shared lock. | |
|
55 | /// </summary> | |
|
56 | /// <returns><c>true</c>, if the shared lock was acquired, <c>false</c> if the specified timeout was expired.</returns> | |
|
57 | /// <param name="timeout">Timeout.</param> | |
|
28 | 58 | public bool LockShared(int timeout) { |
|
29 | 59 | lock (m_lock) { |
|
30 | 60 | if (!m_exclusive) { |
|
31 | 61 | m_locks++; |
|
32 | 62 | return true; |
|
33 | 63 | } |
|
34 | 64 | |
|
35 |
if (m_locks == |
|
|
65 | if (m_locks == m_upgrades) { | |
|
36 | 66 | m_exclusive = false; |
|
37 | 67 | m_locks = 1; |
|
38 | 68 | return true; |
|
39 | 69 | } |
|
40 | ||
|
41 | if (Monitor.Wait(m_lock, timeout)) { | |
|
42 | Debug.Assert(m_locks == 0); | |
|
43 |
|
|
|
70 | ||
|
71 | var t1 = Environment.TickCount; | |
|
72 | var dt = timeout; | |
|
73 | do { | |
|
74 | if (!Monitor.Wait(m_lock, dt)) | |
|
75 | return false; | |
|
76 | ||
|
77 | if (m_locks == m_upgrades || !m_exclusive) | |
|
78 | break; | |
|
79 | ||
|
80 | if (timeout >= 0) { | |
|
81 | dt = timeout - Environment.TickCount + t1; | |
|
82 | if (dt < 0) | |
|
83 | return false; | |
|
84 | } | |
|
85 | } while(true); | |
|
86 | ||
|
87 | m_locks ++; | |
|
88 | m_exclusive = false; | |
|
89 | return true; | |
|
90 | } | |
|
91 | } | |
|
92 | ||
|
93 | /// <summary> | |
|
94 | /// Acquires the shared lock. | |
|
95 | /// </summary> | |
|
96 | public void LockShared() { | |
|
97 | lock (m_lock) { | |
|
98 | if (!m_exclusive) { | |
|
99 | m_locks++; | |
|
100 | } else if (m_locks == m_upgrades) { | |
|
44 | 101 | m_exclusive = false; |
|
45 |
|
|
|
102 | m_locks++; | |
|
103 | } else { | |
|
104 | while (m_exclusive && m_locks > m_upgrades) | |
|
105 | Monitor.Wait(m_lock); | |
|
106 | ||
|
107 | m_locks++; | |
|
108 | m_exclusive = false; | |
|
46 | 109 | } |
|
47 | return false; | |
|
48 | 110 | } |
|
49 | 111 | } |
|
50 | 112 | |
|
51 | public void LockShared() { | |
|
52 | LockShared(-1); | |
|
53 | } | |
|
54 | ||
|
55 |
public void |
|
|
113 | /// <summary> | |
|
114 | /// Upgrades the current lock to exclusive level. | |
|
115 | /// </summary> | |
|
116 | /// <remarks>If the current lock is exclusive already the method does nothing.</remarks> | |
|
117 | public void Upgrade() { | |
|
56 | 118 | lock (m_lock) { |
|
57 |
if (m_exclusive |
|
|
58 | throw new InvalidOperationException(); | |
|
59 |
m_locks |
|
|
60 | if (m_locks == 0) | |
|
61 | Monitor.PulseAll(m_lock); | |
|
119 | if (!m_exclusive) { | |
|
120 | ||
|
121 | if (m_locks <= m_upgrades) | |
|
122 | throw new InvalidOperationException(); | |
|
123 | ||
|
124 | if (m_locks - m_upgrades == 1) { | |
|
125 | m_exclusive = true; | |
|
126 | } else { | |
|
127 | m_upgrades++; | |
|
128 | ||
|
129 | while (m_locks > m_upgrades) | |
|
130 | Monitor.Wait(m_lock); | |
|
131 | ||
|
132 | m_upgrades--; | |
|
133 | m_exclusive = true; | |
|
134 | } | |
|
135 | } | |
|
62 | 136 | } |
|
63 | 137 | } |
|
64 | 138 | |
|
65 | public void ReleaseExclusive() { | |
|
139 | /// <summary> | |
|
140 | /// Upgrades the current lock to exclusive level. | |
|
141 | /// </summary> | |
|
142 | /// <param name="timeout">Timeout.</param> | |
|
143 | /// <returns><c>true</c> if the current lock was updated, <c>false</c> the specified timeout was expired.</returns> | |
|
144 | /// <remarks>If the current lock is exclusive already the method does nothing.</remarks> | |
|
145 | public bool Upgrade(int timeout) { | |
|
66 | 146 | lock (m_lock) { |
|
67 |
if ( |
|
|
147 | if (m_exclusive) | |
|
148 | return true; | |
|
149 | if (m_locks <= m_upgrades) | |
|
68 | 150 | throw new InvalidOperationException(); |
|
69 | m_locks = 0; | |
|
70 | Monitor.PulseAll(m_lock); | |
|
151 | ||
|
152 | if (m_locks - m_upgrades == 1) { | |
|
153 | m_exclusive = true; | |
|
154 | } else { | |
|
155 | var t1 = Environment.TickCount; | |
|
156 | var dt = timeout; | |
|
157 | m_upgrades++; | |
|
158 | do { | |
|
159 | if (!Monitor.Wait(m_lock, dt)) { | |
|
160 | m_upgrades--; | |
|
161 | return false; | |
|
162 | } | |
|
163 | ||
|
164 | // we may get there but the shared lock already aquired | |
|
165 | if (m_locks == m_upgrades) | |
|
166 | break; | |
|
167 | ||
|
168 | if (timeout >= 0) { | |
|
169 | dt = timeout - Environment.TickCount + t1; | |
|
170 | if (dt < 0) { | |
|
171 | m_upgrades--; | |
|
172 | return false; | |
|
173 | } | |
|
174 | } | |
|
175 | } while(true); | |
|
176 | m_upgrades--; | |
|
177 | m_exclusive = true; | |
|
178 | } | |
|
179 | return true; | |
|
71 | 180 | } |
|
72 | 181 | } |
|
73 | 182 | |
|
183 | /// <summary> | |
|
184 | /// Downgrades this lock to shared level. | |
|
185 | /// </summary> | |
|
186 | public void Downgrade() { | |
|
187 | lock (m_lock) | |
|
188 | m_exclusive = false; | |
|
189 | } | |
|
190 | ||
|
191 | /// <summary> | |
|
192 | /// Releases the current lock. | |
|
193 | /// </summary> | |
|
194 | public void Release() { | |
|
195 | lock (m_lock) | |
|
196 | // if no more running threads left | |
|
197 | if (--m_locks == m_upgrades) | |
|
198 | Monitor.PulseAll(m_lock); | |
|
199 | } | |
|
74 | 200 | } |
|
75 | 201 | } |
|
76 | 202 |
@@ -1,187 +1,192 | |||
|
1 | 1 | using System.Threading; |
|
2 | 2 | using System; |
|
3 | 3 | using Implab.Diagnostics; |
|
4 | 4 | using System.Collections.Generic; |
|
5 | 5 | |
|
6 | 6 | |
|
7 | 7 | #if NET_4_5 |
|
8 | 8 | using System.Threading.Tasks; |
|
9 | 9 | #endif |
|
10 | 10 | |
|
11 | 11 | namespace Implab { |
|
12 | 12 | public static class PromiseExtensions { |
|
13 | 13 | public static IPromise<T> DispatchToCurrentContext<T>(this IPromise<T> that) { |
|
14 | 14 | Safe.ArgumentNotNull(that, "that"); |
|
15 | 15 | var context = SynchronizationContext.Current; |
|
16 | 16 | if (context == null) |
|
17 | 17 | return that; |
|
18 | 18 | |
|
19 | 19 | var p = new SyncContextPromise<T>(context); |
|
20 | 20 | p.On(that.Cancel, PromiseEventType.Cancelled); |
|
21 | 21 | |
|
22 | 22 | that.On( |
|
23 | 23 | p.Resolve, |
|
24 | 24 | p.Reject, |
|
25 | 25 | p.Cancel |
|
26 | 26 | ); |
|
27 | 27 | return p; |
|
28 | 28 | } |
|
29 | 29 | |
|
30 | 30 | public static IPromise<T> DispatchToContext<T>(this IPromise<T> that, SynchronizationContext context) { |
|
31 | 31 | Safe.ArgumentNotNull(that, "that"); |
|
32 | 32 | Safe.ArgumentNotNull(context, "context"); |
|
33 | 33 | |
|
34 | 34 | var p = new SyncContextPromise<T>(context); |
|
35 | 35 | p.On(that.Cancel, PromiseEventType.Cancelled); |
|
36 | 36 | |
|
37 | 37 | |
|
38 | 38 | that.On( |
|
39 | 39 | p.Resolve, |
|
40 | 40 | p.Reject, |
|
41 | 41 | p.Cancel |
|
42 | 42 | ); |
|
43 | 43 | return p; |
|
44 | 44 | } |
|
45 | 45 | |
|
46 | 46 | /// <summary> |
|
47 | 47 | /// Ensures the dispatched. |
|
48 | 48 | /// </summary> |
|
49 | 49 | /// <returns>The dispatched.</returns> |
|
50 | 50 | /// <param name="that">That.</param> |
|
51 | 51 | /// <param name="head">Head.</param> |
|
52 | 52 | /// <param name="cleanup">Cleanup.</param> |
|
53 | 53 | /// <typeparam name="TPromise">The 1st type parameter.</typeparam> |
|
54 | 54 | /// <typeparam name="T">The 2nd type parameter.</typeparam> |
|
55 | 55 | public static TPromise EnsureDispatched<TPromise,T>(this TPromise that, IPromise<T> head, Action<T> cleanup) where TPromise : IPromise{ |
|
56 | 56 | Safe.ArgumentNotNull(that, "that"); |
|
57 | 57 | Safe.ArgumentNotNull(head, "head"); |
|
58 | 58 | |
|
59 | 59 | that.On(null,null,() => head.On(cleanup)); |
|
60 | 60 | |
|
61 | 61 | return that; |
|
62 | 62 | } |
|
63 | 63 | |
|
64 | 64 | public static AsyncCallback AsyncCallback<T>(this Promise<T> that, Func<IAsyncResult,T> callback) { |
|
65 | 65 | Safe.ArgumentNotNull(that, "that"); |
|
66 | 66 | Safe.ArgumentNotNull(callback, "callback"); |
|
67 | 67 | var op = TraceContext.Instance.CurrentOperation; |
|
68 | 68 | return ar => { |
|
69 | 69 | TraceContext.Instance.EnterLogicalOperation(op,false); |
|
70 | 70 | try { |
|
71 | 71 | that.Resolve(callback(ar)); |
|
72 | 72 | } catch (Exception err) { |
|
73 | 73 | that.Reject(err); |
|
74 | 74 | } finally { |
|
75 | 75 | TraceContext.Instance.Leave(); |
|
76 | 76 | } |
|
77 | 77 | }; |
|
78 | 78 | } |
|
79 | 79 | |
|
80 | 80 | static void CancelCallback(object cookie) { |
|
81 | 81 | ((ICancellable)cookie).Cancel(); |
|
82 | 82 | } |
|
83 | 83 | |
|
84 | 84 | /// <summary> |
|
85 | 85 | /// Cancells promise after the specified timeout is elapsed. |
|
86 | 86 | /// </summary> |
|
87 | 87 | /// <param name="that">The promise to cancel on timeout.</param> |
|
88 | 88 | /// <param name="milliseconds">The timeout in milliseconds.</param> |
|
89 | 89 | /// <typeparam name="TPromise">The 1st type parameter.</typeparam> |
|
90 | 90 | public static TPromise Timeout<TPromise>(this TPromise that, int milliseconds) where TPromise : IPromise { |
|
91 | 91 | Safe.ArgumentNotNull(that, "that"); |
|
92 | 92 | var timer = new Timer(CancelCallback, that, milliseconds, -1); |
|
93 | 93 | that.On(timer.Dispose, PromiseEventType.All); |
|
94 | 94 | return that; |
|
95 | 95 | } |
|
96 | 96 | |
|
97 | 97 | public static IPromise Bundle(this ICollection<IPromise> that) { |
|
98 | 98 | Safe.ArgumentNotNull(that, "that"); |
|
99 | 99 | |
|
100 | 100 | int count = that.Count; |
|
101 | 101 | int errors = 0; |
|
102 | 102 | var medium = new Promise(); |
|
103 | 103 | |
|
104 | if (count == 0) { | |
|
105 | medium.Resolve(); | |
|
106 | return medium; | |
|
107 | } | |
|
108 | ||
|
104 | 109 | medium.On(() => { |
|
105 | 110 | foreach(var p2 in that) |
|
106 | 111 | p2.Cancel(); |
|
107 | 112 | }, PromiseEventType.ErrorOrCancel); |
|
108 | 113 | |
|
109 | 114 | foreach (var p in that) |
|
110 | 115 | p.On( |
|
111 | 116 | () => { |
|
112 | 117 | if (Interlocked.Decrement(ref count) == 0) |
|
113 | 118 | medium.Resolve(); |
|
114 | 119 | }, |
|
115 | 120 | error => { |
|
116 | 121 | if (Interlocked.Increment(ref errors) == 1) |
|
117 | 122 | medium.Reject( |
|
118 | 123 | new Exception("The dependency promise is failed", error) |
|
119 | 124 | ); |
|
120 | 125 | }, |
|
121 | 126 | () => { |
|
122 | 127 | if (Interlocked.Increment(ref errors) == 1) |
|
123 | 128 | medium.Reject( |
|
124 | 129 | new Exception("The dependency promise is cancelled") |
|
125 | 130 | ); |
|
126 | 131 | } |
|
127 | 132 | ); |
|
128 | 133 | |
|
129 | 134 | return medium; |
|
130 | 135 | } |
|
131 | 136 | |
|
132 | 137 | public static IPromise<T[]> Bundle<T>(this ICollection<IPromise<T>> that) { |
|
133 | 138 | Safe.ArgumentNotNull(that, "that"); |
|
134 | 139 | |
|
135 | 140 | int count = that.Count; |
|
136 | 141 | int errors = 0; |
|
137 | 142 | var medium = new Promise<T[]>(); |
|
138 | 143 | var results = new T[that.Count]; |
|
139 | 144 | |
|
140 | 145 | medium.On(() => { |
|
141 | 146 | foreach(var p2 in that) |
|
142 | 147 | p2.Cancel(); |
|
143 | 148 | }, PromiseEventType.ErrorOrCancel); |
|
144 | 149 | |
|
145 | 150 | int i = 0; |
|
146 | 151 | foreach (var p in that) { |
|
147 | 152 | var idx = i; |
|
148 | 153 | p.On( |
|
149 | 154 | x => { |
|
150 | 155 | results[idx] = x; |
|
151 | 156 | if (Interlocked.Decrement(ref count) == 0) |
|
152 | 157 | medium.Resolve(results); |
|
153 | 158 | }, |
|
154 | 159 | error => { |
|
155 | 160 | if (Interlocked.Increment(ref errors) == 1) |
|
156 | 161 | medium.Reject( |
|
157 | 162 | new Exception("The dependency promise is failed", error) |
|
158 | 163 | ); |
|
159 | 164 | }, |
|
160 | 165 | () => { |
|
161 | 166 | if (Interlocked.Increment(ref errors) == 1) |
|
162 | 167 | medium.Reject( |
|
163 | 168 | new Exception("The dependency promise is cancelled") |
|
164 | 169 | ); |
|
165 | 170 | } |
|
166 | 171 | ); |
|
167 | 172 | i++; |
|
168 | 173 | } |
|
169 | 174 | |
|
170 | 175 | return medium; |
|
171 | 176 | } |
|
172 | 177 | |
|
173 | 178 | #if NET_4_5 |
|
174 | 179 | |
|
175 | 180 | public static Task<T> GetTask<T>(this IPromise<T> that) { |
|
176 | 181 | Safe.ArgumentNotNull(that, "that"); |
|
177 | 182 | var tcs = new TaskCompletionSource<T>(); |
|
178 | 183 | |
|
179 | 184 | that.On(tcs.SetResult, tcs.SetException, tcs.SetCanceled); |
|
180 | 185 | |
|
181 | 186 | return tcs.Task; |
|
182 | 187 | } |
|
183 | 188 | |
|
184 | 189 | #endif |
|
185 | 190 | } |
|
186 | 191 | } |
|
187 | 192 |
@@ -1,34 +1,93 | |||
|
1 | 1 | using System; |
|
2 | 2 | using Implab.Diagnostics; |
|
3 | 3 | using Implab.Parallels; |
|
4 | 4 | using Implab; |
|
5 | 5 | using System.Collections.Generic; |
|
6 | 6 | using System.Collections.Concurrent; |
|
7 | using System.Threading; | |
|
7 | 8 | |
|
8 | 9 | namespace MonoPlay { |
|
9 | 10 | class MainClass { |
|
10 | 11 | public static void Main(string[] args) { |
|
11 | 12 | if (args == null) |
|
12 | 13 | throw new ArgumentNullException("args"); |
|
13 | 14 | |
|
14 | const int count = 10000000; | |
|
15 | ||
|
16 | 15 | var t1 = Environment.TickCount; |
|
17 | 16 | |
|
18 | for (int i = 0; i < count; i++) { | |
|
19 | var p = new Promise<int>(); | |
|
17 | const int reads = 100000; | |
|
18 | const int writes = 1000; | |
|
19 | const int readThreads = 8; | |
|
20 | const int writeThreads = 0; | |
|
21 | ||
|
22 | var l = new SharedLock(); | |
|
23 | var st = new HashSet<int>(); | |
|
20 | 24 | |
|
21 | p.On(x => {}).On(x => {}); | |
|
25 | Action reader1 = () => { | |
|
26 | for (int i =0; i < reads; i++) { | |
|
27 | try { | |
|
28 | l.LockShared(); | |
|
29 | st.Contains(i % 1000); | |
|
30 | Thread.Sleep(0); | |
|
31 | } finally { | |
|
32 | l.Release(); | |
|
33 | } | |
|
34 | } | |
|
35 | }; | |
|
36 | ||
|
37 | Action reader2 = () => { | |
|
38 | for(var i = 0; i < reads; i++) | |
|
39 | lock(st) { | |
|
40 | st.Contains(i % 1000); | |
|
41 | Thread.Sleep(0); | |
|
42 | } | |
|
43 | }; | |
|
22 | 44 | |
|
23 | p.Resolve(i); | |
|
45 | Action writer1 = () => { | |
|
46 | var rnd = new Random(Environment.TickCount); | |
|
47 | for (int i = 0; i < writes; i++) { | |
|
48 | try { | |
|
49 | l.LockExclusive(); | |
|
50 | st.Add(rnd.Next(1000)); | |
|
51 | //Thread.Sleep(1); | |
|
52 | } finally { | |
|
53 | l.Release(); | |
|
54 | } | |
|
55 | } | |
|
56 | }; | |
|
24 | 57 | |
|
25 | } | |
|
58 | Action writer2 = () => { | |
|
59 | var rnd = new Random(Environment.TickCount); | |
|
60 | for (int i = 0; i < writes; i++) { | |
|
61 | lock (st) { | |
|
62 | st.Add(rnd.Next(1000)); | |
|
63 | //Thread.Sleep(1); | |
|
64 | } | |
|
65 | } | |
|
66 | }; | |
|
67 | ||
|
68 | ||
|
26 | 69 | |
|
27 | ||
|
70 | var readers = new IPromise[readThreads]; | |
|
71 | for (int i = 0; i < readThreads; i++) | |
|
72 | readers[i] = AsyncPool.RunThread(reader1); | |
|
73 | ||
|
74 | var writers = new IPromise[writeThreads]; | |
|
75 | for (int i = 0; i < writeThreads; i++) | |
|
76 | writers[i] = AsyncPool.RunThread(writer1); | |
|
77 | ||
|
78 | ||
|
79 | new [] { | |
|
80 | readers.Bundle().On(() => Console.WriteLine("readers complete in {0} ms", Environment.TickCount - t1)), | |
|
81 | writers.Bundle().On(() => Console.WriteLine("writers complete in {0} ms", Environment.TickCount - t1)) | |
|
82 | }.Bundle().Join(); | |
|
83 | ||
|
84 | ||
|
28 | 85 | |
|
29 | 86 | var t2 = Environment.TickCount; |
|
30 | 87 | Console.WriteLine("done: {0} ms, {1:.00} Mb, {2} GC", t2 - t1, GC.GetTotalMemory(false) / (1024*1024), GC.CollectionCount(0) ); |
|
31 | 88 | |
|
32 | 89 | } |
|
90 | ||
|
91 | ||
|
33 | 92 | } |
|
34 | 93 | } |
General Comments 0
You need to be logged in to leave comments.
Login now