From 3f6cead0db00aabb7fbd1d9a48c28b341396666e Mon Sep 17 00:00:00 2001 From: Sam Cook Date: Tue, 28 Apr 2015 18:38:08 +0100 Subject: [PATCH] Lock/unlock/extend instances in parallel --- RedLock/RedisLock.cs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/RedLock/RedisLock.cs b/RedLock/RedisLock.cs index bac70bb..a048509 100644 --- a/RedLock/RedisLock.cs +++ b/RedLock/RedisLock.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Net; using System.Threading; +using System.Threading.Tasks; using RedLock.Logging; using RedLock.Util; using StackExchange.Redis; @@ -215,14 +216,14 @@ private int Lock() { var locksAcquired = 0; - foreach (var cache in redisCaches) + Parallel.ForEach(redisCaches, cache => { if (LockInstance(cache)) { - locksAcquired++; + Interlocked.Increment(ref locksAcquired); } - } - + }); + return locksAcquired; } @@ -230,23 +231,20 @@ private int Extend() { var locksExtended = 0; - foreach (var cache in redisCaches) + Parallel.ForEach(redisCaches, cache => { if (ExtendInstance(cache)) { - locksExtended++; + Interlocked.Increment(ref locksExtended); } - } + }); return locksExtended; } private void Unlock() { - foreach (var cache in redisCaches) - { - UnlockInstance(cache); - } + Parallel.ForEach(redisCaches, cache => UnlockInstance(cache)); IsAcquired = false; }