annotate Implab/Parallels/AsyncPool.cs @ 48:d9d794b61bb9 interactive logger

Interactive tracing Improved working with tracing contexts
author cin
date Fri, 18 Apr 2014 12:34:45 +0400
parents d10034588e38
children 790e8a997d30
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35
2880242f987a initial log capabilities
cin
parents: 25
diff changeset
1 using Implab.Diagnostics;
25
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
2 using System;
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
3 using System.Threading;
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
4
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
5 namespace Implab.Parallels {
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
6 /// <summary>
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
7 /// Класс для распаралеливания задач.
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
8 /// </summary>
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
9 /// <remarks>
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
10 /// Используя данный класс и лямда выражения можно распараллелить
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
11 /// вычисления, для этого используется концепция обещаний.
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
12 /// </remarks>
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
13 public static class AsyncPool {
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
14
45
d10034588e38 initial work on interactive logger
cin
parents: 40
diff changeset
15 public static IPromise<T> Invoke<T>(Func<T> func) {
25
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
16 var p = new Promise<T>();
40
fe33f4e02ad5 improved tracing
cin
parents: 36
diff changeset
17 var caller = TraceContext.Snapshot();
25
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
18
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
19 ThreadPool.QueueUserWorkItem(param => {
48
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
20 TraceContext.Fork(caller);
25
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
21 try {
14
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
22 p.Resolve(func());
25
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
23 } catch(Exception e) {
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
24 p.Reject(e);
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
25 }
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
26 });
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
27
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
28 return p;
14
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
29 }
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
30
45
d10034588e38 initial work on interactive logger
cin
parents: 40
diff changeset
31 public static IPromise<T> InvokeNewThread<T>(Func<T> func) {
14
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
32 var p = new Promise<T>();
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
33
40
fe33f4e02ad5 improved tracing
cin
parents: 36
diff changeset
34 var caller = TraceContext.Snapshot();
35
2880242f987a initial log capabilities
cin
parents: 25
diff changeset
35
14
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
36 var worker = new Thread(() => {
48
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
37 TraceContext.Fork(caller);
14
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
38 try {
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
39 p.Resolve(func());
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
40 } catch (Exception e) {
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
41 p.Reject(e);
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
42 }
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
43 });
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
44 worker.IsBackground = true;
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
45 worker.Start();
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
46
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
47 return p;
25
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
48 }
48
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
49
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
50
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
51 public static IPromiseBase InvokeNewThread(Action func) {
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
52 var p = new Promise<object>();
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
53
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
54 var caller = TraceContext.Snapshot();
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
55
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
56 var worker = new Thread(() => {
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
57 TraceContext.Fork(caller);
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
58 try {
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
59 func();
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
60 p.Resolve();
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
61 } catch (Exception e) {
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
62 p.Reject(e);
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
63 }
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
64 });
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
65 worker.IsBackground = true;
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
66 worker.Start();
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
67
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
68 return p;
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
69 }
25
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
70 }
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
71 }