diff --git a/Implab/Parallels/ArrayTraits.cs b/Implab/Parallels/ArrayTraits.cs --- a/Implab/Parallels/ArrayTraits.cs +++ b/Implab/Parallels/ArrayTraits.cs @@ -163,16 +163,18 @@ namespace Implab.Parallels { 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 @@ namespace Implab.Parallels { if (left == 0) promise.Resolve(res); }, - e => promise.Reject(e) + promise.Reject ); } catch (Exception e) { diff --git a/Implab/Parallels/DispatchPool.cs b/Implab/Parallels/DispatchPool.cs --- a/Implab/Parallels/DispatchPool.cs +++ b/Implab/Parallels/DispatchPool.cs @@ -69,7 +69,7 @@ namespace Implab.Parallels { protected abstract bool TryDequeue(out TUnit unit); - private bool Dequeue(out TUnit unit, int timeout) { + bool Dequeue(out TUnit unit, int timeout) { int ts = Environment.TickCount; if (TryDequeue(out unit)) return true;