comparison Implab/Parallels/AsyncPool.cs @ 192:f1da3afc3521 release v2.1

Слияние с v2
author cin
date Fri, 22 Apr 2016 13:10:34 +0300
parents 706fccb85524
children
comparison
equal deleted inserted replaced
71:1714fd8678ef 192:f1da3afc3521
1 using Implab.Diagnostics; 1 using Implab.Diagnostics;
2 using System; 2 using System;
3 using System.Threading; 3 using System.Threading;
4 using System.Linq;
4 5
5 namespace Implab.Parallels { 6 namespace Implab.Parallels {
6 /// <summary> 7 /// <summary>
7 /// Класс для распаралеливания задач. 8 /// Класс для распаралеливания задач.
8 /// </summary> 9 /// </summary>
12 /// </remarks> 13 /// </remarks>
13 public static class AsyncPool { 14 public static class AsyncPool {
14 15
15 public static IPromise<T> Invoke<T>(Func<T> func) { 16 public static IPromise<T> Invoke<T>(Func<T> func) {
16 var p = new Promise<T>(); 17 var p = new Promise<T>();
17 var caller = TraceContext.Snapshot(); 18 var caller = TraceContext.Instance.CurrentOperation;
18 19
19 ThreadPool.QueueUserWorkItem(param => { 20 ThreadPool.QueueUserWorkItem(param => {
20 TraceContext.Fork(caller); 21 TraceContext.Instance.EnterLogicalOperation(caller,false);
21 try { 22 try {
22 p.Resolve(func()); 23 p.Resolve(func());
23 } catch(Exception e) { 24 } catch(Exception e) {
24 p.Reject(e); 25 p.Reject(e);
25 } 26 } finally {
27 TraceContext.Instance.Leave();
28 }
26 }); 29 });
27 30
28 return p; 31 return p;
29 } 32 }
30 33
31 public static IPromise<T> InvokeNewThread<T>(Func<T> func) { 34 public static IPromise<T> Invoke<T>(Func<ICancellationToken, T> func) {
35 var p = new Promise<T>();
36 var caller = TraceContext.Instance.CurrentOperation;
37
38 ThreadPool.QueueUserWorkItem(param => {
39 TraceContext.Instance.EnterLogicalOperation(caller,false);
40 try {
41 p.Resolve(func(p));
42 } catch(Exception e) {
43 p.Reject(e);
44 } finally {
45 TraceContext.Instance.Leave();
46 }
47 });
48
49 return p;
50 }
51
52 public static IPromise<T> RunThread<T>(Func<T> func) {
32 var p = new Promise<T>(); 53 var p = new Promise<T>();
33 54
34 var caller = TraceContext.Snapshot(); 55 var caller = TraceContext.Instance.CurrentOperation;
35 56
36 var worker = new Thread(() => { 57 var worker = new Thread(() => {
37 TraceContext.Fork(caller); 58 TraceContext.Instance.EnterLogicalOperation(caller,false);
38 try { 59 try {
39 p.Resolve(func()); 60 p.Resolve(func());
40 } catch (Exception e) { 61 } catch (Exception e) {
41 p.Reject(e); 62 p.Reject(e);
63 } finally {
64 TraceContext.Instance.Leave();
65 }
66 });
67 worker.IsBackground = true;
68 worker.Start();
69
70 return p;
71 }
72
73 public static IPromise<T> RunThread<T>(Func<ICancellationToken, T> func) {
74 var p = new Promise<T>();
75
76 var caller = TraceContext.Instance.CurrentOperation;
77
78 var worker = new Thread(() => {
79 TraceContext.Instance.EnterLogicalOperation(caller,false);
80 try {
81 p.Resolve(func(p));
82 } catch (Exception e) {
83 p.Reject(e);
84 } finally {
85 TraceContext.Instance.Leave();
42 } 86 }
43 }); 87 });
44 worker.IsBackground = true; 88 worker.IsBackground = true;
45 worker.Start(); 89 worker.Start();
46 90
47 return p; 91 return p;
48 } 92 }
49 93
50 94
51 public static IPromise InvokeNewThread(Action func) { 95 public static IPromise RunThread(Action func) {
52 var p = new Promise<object>(); 96 var p = new Promise();
53 97
54 var caller = TraceContext.Snapshot(); 98 var caller = TraceContext.Instance.CurrentOperation;
55 99
56 var worker = new Thread(() => { 100 var worker = new Thread(() => {
57 TraceContext.Fork(caller); 101 TraceContext.Instance.EnterLogicalOperation(caller,false);
58 try { 102 try {
59 func(); 103 func();
60 p.Resolve(); 104 p.Resolve();
61 } catch (Exception e) { 105 } catch (Exception e) {
62 p.Reject(e); 106 p.Reject(e);
107 } finally {
108 TraceContext.Instance.Leave();
63 } 109 }
64 }); 110 });
65 worker.IsBackground = true; 111 worker.IsBackground = true;
66 worker.Start(); 112 worker.Start();
67 113
68 return p; 114 return p;
69 } 115 }
116
117 public static IPromise RunThread(Action<ICancellationToken> func) {
118 var p = new Promise();
119
120 var caller = TraceContext.Instance.CurrentOperation;
121
122 var worker = new Thread(() => {
123 TraceContext.Instance.EnterLogicalOperation(caller,false);
124 try {
125 func(p);
126 p.Resolve();
127 } catch (Exception e) {
128 p.Reject(e);
129 } finally {
130 TraceContext.Instance.Leave();
131 }
132 });
133 worker.IsBackground = true;
134 worker.Start();
135
136 return p;
137 }
138
139 public static IPromise[] RunThread(params Action[] func) {
140 return func.Select(f => RunThread(f)).ToArray();
141 }
142
143 public static IPromise[] RunThread(params Action<ICancellationToken>[] func) {
144 return func.Select(f => RunThread(f)).ToArray();
145 }
146
147 public static IPromise<T>[] RunThread<T>(params Func<T>[] func) {
148 return func.Select(f => RunThread(f)).ToArray();
149 }
150
151 public static IPromise<T>[] RunThread<T>(params Func<ICancellationToken, T>[] func) {
152 return func.Select(f => RunThread(f)).ToArray();
153 }
70 } 154 }
71 } 155 }