changeset 30:2fad2d1f4b03

small refactoring, cleanup.
author cin
date Mon, 07 Apr 2014 03:25:57 +0400
parents 768f7deeb55b
children dafaadca5b9f
files Implab.Fx/AnimationHelpers.cs Implab.Test/AsyncTests.cs Implab/Parallels/ArrayTraits.cs Implab/Parallels/DispatchPool.cs Implab/Promise.cs
diffstat 5 files changed, 26 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/Implab.Fx/AnimationHelpers.cs	Thu Mar 06 17:59:03 2014 +0400
+++ b/Implab.Fx/AnimationHelpers.cs	Mon Apr 07 03:25:57 2014 +0400
@@ -38,14 +38,14 @@
             return anim;
         }
 
-        public static Promise<T> CloseFadeOut<T>(this T ctl) where T : Form
+        public static IPromise<T> CloseFadeOut<T>(this T ctl) where T : Form
         {
             var anim = ctl.AnimateTransparency(0);
 
             return anim.Play().DispatchToControl(ctl).Then(frm => frm.Close());
         }
 
-        public static Promise<T> OverlayFadeIn<T>(this Form that, T overlay) where T : Form
+        public static IPromise<T> OverlayFadeIn<T>(this Form that, T overlay) where T : Form
         {
             if (that == null)
                 throw new ArgumentNullException("that");
--- a/Implab.Test/AsyncTests.cs	Thu Mar 06 17:59:03 2014 +0400
+++ b/Implab.Test/AsyncTests.cs	Mon Apr 07 03:25:57 2014 +0400
@@ -255,7 +255,7 @@
 
                 var t = Environment.TickCount;
                 var res = args
-                    .ChainedMap2(
+                    .ChainedMap(
                         x => pool.Invoke(
                             () => Math.Sin(x * x)
                         ),
--- a/Implab/Parallels/ArrayTraits.cs	Thu Mar 06 17:59:03 2014 +0400
+++ b/Implab/Parallels/ArrayTraits.cs	Mon Apr 07 03:25:57 2014 +0400
@@ -105,7 +105,7 @@
             }
         }
 
-        public static Promise<TDst[]> ParallelMap<TSrc, TDst> (this TSrc[] source, Func<TSrc,TDst> transform, int threads) {
+        public static IPromise<TDst[]> ParallelMap<TSrc, TDst> (this TSrc[] source, Func<TSrc,TDst> transform, int threads) {
             if (source == null)
                 throw new ArgumentNullException("source");
             if (transform == null)
@@ -115,7 +115,7 @@
             return mapper.Promise;
         }
 
-        public static Promise<int> ParallelForEach<TSrc>(this TSrc[] source, Action<TSrc> action, int threads) {
+        public static IPromise<int> ParallelForEach<TSrc>(this TSrc[] source, Action<TSrc> action, int threads) {
             if (source == null)
                 throw new ArgumentNullException("source");
             if (action == null)
@@ -136,6 +136,7 @@
             var promise = new Promise<TDst[]>();
             var res = new TDst[source.Length];
             var pending = source.Length;
+
             var semaphore = new Semaphore(threads, threads);
 
             AsyncPool.InvokeNewThread(() => {
--- a/Implab/Parallels/DispatchPool.cs	Thu Mar 06 17:59:03 2014 +0400
+++ b/Implab/Parallels/DispatchPool.cs	Mon Apr 07 03:25:57 2014 +0400
@@ -143,18 +143,24 @@
                 // anyway it may take some time for the thread to start
                 // we will ensure that at least one thread is running
 
-                if (AllocateThreadSlot(1)) {
-                    // if there were no threads in the pool
-                    var worker = new Thread(this.Worker);
-                    worker.IsBackground = true;
-                    worker.Start();
-                }
+                EnsurePoolIsAlive();
             } else {
                 // if there is no sleeping threads in the pool
-                if (!StartWorker())
-                    // we haven't started a new thread, but the current can be on the way and it can't process the queue
+                if (!StartWorker()) {
+                    // we haven't started a new thread, but the current can be on the way to terminate and it can't process the queue
                     // send it a signal to spin again
-                    SignalThread(); 
+                    SignalThread();
+                    EnsurePoolIsAlive();
+                }
+            }
+        }
+
+        private void EnsurePoolIsAlive() {
+            if (AllocateThreadSlot(1)) {
+                // if there were no threads in the pool
+                var worker = new Thread(this.Worker);
+                worker.IsBackground = true;
+                worker.Start();
             }
         }
 
@@ -177,12 +183,9 @@
                 // in case at the moment the last thread was being released
                 // a new task was added to the queue, we need to try
                 // to revoke the thread to avoid the situation when the task is left unprocessed
-                if (last && Sleep(0)) { // Sleep(0) will fetch pending task or will return false
-                    if (AllocateThreadSlot(1))
-                        return true; // spin again...
-                    else
-                        SignalThread(); // since Sleep(0) has fetched the signal we neet to reschedule it
-                    
+                if (last && FetchSignalOrWait(0)) { // FetchSignalOrWait(0) will fetch pending task or will return false
+                    SignalThread(); // since FetchSignalOrWait(0) has fetched the signal we need to reschedule it
+                    return AllocateThreadSlot(1); // ensure that at least one thread is alive
                 }
 
                 return false;
--- a/Implab/Promise.cs	Thu Mar 06 17:59:03 2014 +0400
+++ b/Implab/Promise.cs	Mon Apr 07 03:25:57 2014 +0400
@@ -11,7 +11,7 @@
     public delegate T ErrorHandler<out T>(Exception e);
     public delegate void ResultHandler<in T>(T result);
     public delegate TNew ResultMapper<in TSrc, out TNew>(TSrc result);
-    public delegate Promise<TNew> ChainedOperation<in TSrc,TNew>(TSrc result);
+    public delegate IPromise<TNew> ChainedOperation<in TSrc,TNew>(TSrc result);
 
     /// <summary>
     /// Класс для асинхронного получения результатов. Так называемое "обещание".
@@ -610,7 +610,7 @@
         /// <param name="promises">Список обещаний. Если список пустой, то результирующее обещание возвращается уже выполненным.</param>
         /// <returns>Обещание объединяющее в себе результат переданных обещаний.</returns>
         /// <exception cref="ArgumentNullException"><paramref name="promises"/> не может быть null</exception>
-        public static Promise<T[]> CreateComposite(IList<Promise<T>> promises) {
+        public static IPromise<T[]> CreateComposite(IList<IPromise<T>> promises) {
             if (promises == null)
                 throw new ArgumentNullException();