diff Implab/Parallels/ArrayTraits.cs @ 89:ce0171cacec4 v2

improved performance of a chained map operation
author cin
date Wed, 08 Oct 2014 02:19:45 +0400
parents 4f20870d0816
children 4c0e5ef99986
line wrap: on
line diff
--- a/Implab/Parallels/ArrayTraits.cs	Tue Oct 07 10:58:38 2014 +0400
+++ b/Implab/Parallels/ArrayTraits.cs	Wed Oct 08 02:19:45 2014 +0400
@@ -163,16 +163,18 @@
                         break; // stop processing in case of error or cancellation
                     var idx = i;
 
-                    lock(locker) {
-                        while(slots == 0)
-                            Monitor.Wait(locker);
-                        slots--;
+                    if (Interlocked.Decrement(ref slots) < 0) {
+                        lock(locker) {
+                            while(slots < 0)
+                                Monitor.Wait(locker);
+                        }
                     }
+
                     try {
                         transform(source[i])
                             .Anyway(() => {
-                                lock(locker) {
-                                    slots ++;
+                                Interlocked.Increment(ref slots);
+                                lock (locker) {
                                     Monitor.Pulse(locker);
                                 }
                             })
@@ -183,7 +185,7 @@
                                     if (left == 0)
                                         promise.Resolve(res);
                                 },
-                                e => promise.Reject(e)
+                                promise.Reject
                             );
 
                     } catch (Exception e) {