Mercurial > pub > ImplabNet
diff Implab/Parallels/AsyncPool.cs @ 145:706fccb85524 v2
RC: cancellation support for promises + tests
author | cin |
---|---|
date | Sun, 08 Mar 2015 02:52:27 +0300 |
parents | a336cb13c6a9 |
children |
line wrap: on
line diff
--- a/Implab/Parallels/AsyncPool.cs Fri Mar 06 15:45:26 2015 +0300 +++ b/Implab/Parallels/AsyncPool.cs Sun Mar 08 02:52:27 2015 +0300 @@ -31,6 +31,24 @@ return p; } + 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>(); @@ -52,6 +70,27 @@ 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; + worker.Start(); + + return p; + } + public static IPromise RunThread(Action func) { var p = new Promise(); @@ -75,12 +114,42 @@ 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(); + } } }