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

Слияние с v2
author cin
date Fri, 22 Apr 2016 13:10:34 +0300
parents 706fccb85524
children
line wrap: on
line diff
--- a/Implab/Parallels/AsyncPool.cs	Wed Sep 03 18:34:02 2014 +0400
+++ b/Implab/Parallels/AsyncPool.cs	Fri Apr 22 13:10:34 2016 +0300
@@ -1,6 +1,7 @@
 using Implab.Diagnostics;
 using System;
 using System.Threading;
+using System.Linq;
 
 namespace Implab.Parallels {
 	/// <summary>
@@ -14,31 +15,74 @@
 
 		public static IPromise<T> Invoke<T>(Func<T> func) {
 			var p = new Promise<T>();
-            var caller = TraceContext.Snapshot();
+            var caller = TraceContext.Instance.CurrentOperation;
 
 			ThreadPool.QueueUserWorkItem(param => {
-                TraceContext.Fork(caller);
+                TraceContext.Instance.EnterLogicalOperation(caller,false);
 				try {
 					p.Resolve(func());
 				} catch(Exception e) {
 					p.Reject(e);
-				}
+                } finally {
+                    TraceContext.Instance.Leave();
+                }
 			});
 
 			return p;
 		}
 
-        public static IPromise<T> InvokeNewThread<T>(Func<T> func) {
+        public static IPromise<T> Invoke<T>(Func<ICancellationToken, T> func) {
+            var p = new Promise<T>();
+            var caller = TraceContext.Instance.CurrentOperation;
+
+            ThreadPool.QueueUserWorkItem(param => {
+                TraceContext.Instance.EnterLogicalOperation(caller,false);
+                try {
+                    p.Resolve(func(p));
+                } catch(Exception e) {
+                    p.Reject(e);
+                } finally {
+                    TraceContext.Instance.Leave();
+                }
+            });
+
+            return p;
+        }
+
+        public static IPromise<T> RunThread<T>(Func<T> func) {
             var p = new Promise<T>();
 
-            var caller = TraceContext.Snapshot();
+            var caller = TraceContext.Instance.CurrentOperation;
 
             var worker = new Thread(() => {
-                TraceContext.Fork(caller);
+                TraceContext.Instance.EnterLogicalOperation(caller,false);
                 try {
                     p.Resolve(func());
                 } catch (Exception e) {
                     p.Reject(e);
+                } finally {
+                    TraceContext.Instance.Leave();
+                }
+            });
+            worker.IsBackground = true;
+            worker.Start();
+
+            return p;
+        }
+
+        public static IPromise<T> RunThread<T>(Func<ICancellationToken, T> func) {
+            var p = new Promise<T>();
+
+            var caller = TraceContext.Instance.CurrentOperation;
+
+            var worker = new Thread(() => {
+                TraceContext.Instance.EnterLogicalOperation(caller,false);
+                try {
+                    p.Resolve(func(p));
+                } catch (Exception e) {
+                    p.Reject(e);
+                } finally {
+                    TraceContext.Instance.Leave();
                 }
             });
             worker.IsBackground = true;
@@ -48,18 +92,20 @@
         }
 
 
-        public static IPromise InvokeNewThread(Action func) {
-            var p = new Promise<object>();
+        public static IPromise RunThread(Action func) {
+            var p = new Promise();
 
-            var caller = TraceContext.Snapshot();
+            var caller = TraceContext.Instance.CurrentOperation;
 
             var worker = new Thread(() => {
-                TraceContext.Fork(caller);
+                TraceContext.Instance.EnterLogicalOperation(caller,false);
                 try {
                     func();
                     p.Resolve();
                 } catch (Exception e) {
                     p.Reject(e);
+                } finally {
+                    TraceContext.Instance.Leave();
                 }
             });
             worker.IsBackground = true;
@@ -67,5 +113,43 @@
 
             return p;
         }
+
+        public static IPromise RunThread(Action<ICancellationToken> func) {
+            var p = new Promise();
+
+            var caller = TraceContext.Instance.CurrentOperation;
+
+            var worker = new Thread(() => {
+                TraceContext.Instance.EnterLogicalOperation(caller,false);
+                try {
+                    func(p);
+                    p.Resolve();
+                } catch (Exception e) {
+                    p.Reject(e);
+                } finally {
+                    TraceContext.Instance.Leave();
+                }
+            });
+            worker.IsBackground = true;
+            worker.Start();
+
+            return p;
+        }
+
+        public static IPromise[] RunThread(params Action[] func) {
+            return func.Select(f => RunThread(f)).ToArray();
+        }
+
+        public static IPromise[] RunThread(params Action<ICancellationToken>[] func) {
+            return func.Select(f => RunThread(f)).ToArray();
+        }
+
+        public static IPromise<T>[] RunThread<T>(params Func<T>[] func) {
+            return func.Select(f => RunThread(f)).ToArray();
+        }
+
+        public static IPromise<T>[] RunThread<T>(params Func<ICancellationToken, T>[] func) {
+            return func.Select(f => RunThread(f)).ToArray();
+        }
 	}
 }