annotate Implab/Parallels/AsyncPool.cs @ 94:a43745f81f10 v2

minor fixes
author cin
date Thu, 23 Oct 2014 17:50:09 +0400
parents 4c0e5ef99986
children 2573b562e328
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>();
92
4c0e5ef99986 rewritten tracing
cin
parents: 66
diff changeset
17 var caller = TraceContext.Instance.CurrentOperation;
25
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
18
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
19 ThreadPool.QueueUserWorkItem(param => {
92
4c0e5ef99986 rewritten tracing
cin
parents: 66
diff changeset
20 TraceContext.Instance.EnterLogicalOperation(caller,false);
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);
92
4c0e5ef99986 rewritten tracing
cin
parents: 66
diff changeset
25 } finally {
4c0e5ef99986 rewritten tracing
cin
parents: 66
diff changeset
26 TraceContext.Instance.Leave();
4c0e5ef99986 rewritten tracing
cin
parents: 66
diff changeset
27 }
25
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
28 });
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
29
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
30 return p;
14
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
31 }
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
32
45
d10034588e38 initial work on interactive logger
cin
parents: 40
diff changeset
33 public static IPromise<T> InvokeNewThread<T>(Func<T> func) {
14
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
34 var p = new Promise<T>();
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
35
92
4c0e5ef99986 rewritten tracing
cin
parents: 66
diff changeset
36 var caller = TraceContext.Instance.CurrentOperation;
35
2880242f987a initial log capabilities
cin
parents: 25
diff changeset
37
14
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
38 var worker = new Thread(() => {
92
4c0e5ef99986 rewritten tracing
cin
parents: 66
diff changeset
39 TraceContext.Instance.EnterLogicalOperation(caller,false);
14
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
40 try {
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
41 p.Resolve(func());
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
42 } catch (Exception e) {
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
43 p.Reject(e);
92
4c0e5ef99986 rewritten tracing
cin
parents: 66
diff changeset
44 } finally {
4c0e5ef99986 rewritten tracing
cin
parents: 66
diff changeset
45 TraceContext.Instance.Leave();
14
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
46 }
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
47 });
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
48 worker.IsBackground = true;
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
49 worker.Start();
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
50
e943453e5039 Implemented interllocked queue
cin
parents: 11
diff changeset
51 return p;
25
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
52 }
48
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
53
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
54
66
790e8a997d30 Refactoring
cin
parents: 48
diff changeset
55 public static IPromise InvokeNewThread(Action func) {
48
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
56 var p = new Promise<object>();
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
57
92
4c0e5ef99986 rewritten tracing
cin
parents: 66
diff changeset
58 var caller = TraceContext.Instance.CurrentOperation;
48
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
59
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
60 var worker = new Thread(() => {
92
4c0e5ef99986 rewritten tracing
cin
parents: 66
diff changeset
61 TraceContext.Instance.EnterLogicalOperation(caller,false);
48
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
62 try {
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
63 func();
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
64 p.Resolve();
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
65 } catch (Exception e) {
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
66 p.Reject(e);
92
4c0e5ef99986 rewritten tracing
cin
parents: 66
diff changeset
67 } finally {
4c0e5ef99986 rewritten tracing
cin
parents: 66
diff changeset
68 TraceContext.Instance.Leave();
48
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
69 }
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
70 });
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
71 worker.IsBackground = true;
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
72 worker.Start();
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
73
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
74 return p;
d9d794b61bb9 Interactive tracing
cin
parents: 45
diff changeset
75 }
25
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
76 }
9bf5b23650c9 refactoring
cin
parents: 16
diff changeset
77 }