Skip to content

Commit

Permalink
Lock/unlock/extend instances in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
samcook committed Apr 28, 2015
1 parent b6cd78f commit 3f6cead
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions RedLock/RedisLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -215,38 +216,35 @@ 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;
}

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;
}
Expand Down

0 comments on commit 3f6cead

Please sign in to comment.