Skip to content

Commit

Permalink
Don't use readonly SpinLock. Fixes #63 #64
Browse files Browse the repository at this point in the history
  • Loading branch information
JimBobSquarePants committed Aug 5, 2019
1 parent 5d381f0 commit c3af742
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 31 deletions.
10 changes: 5 additions & 5 deletions src/ImageSharp.Web/Caching/AsyncKeyLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal sealed class AsyncKeyLock
/// <summary>
/// SpinLock used to protect access to the Keys and Pool collections.
/// </summary>
private static readonly SpinLock SpinLock = new SpinLock(false);
private static SpinLock spinLock = new SpinLock(false);

/// <summary>
/// Maximum size of the doorman pool. If the pool is already full when releasing
Expand Down Expand Up @@ -76,7 +76,7 @@ private static Doorman GetDoorman(string key)
bool lockTaken = false;
try
{
SpinLock.Enter(ref lockTaken);
spinLock.Enter(ref lockTaken);

if (!Keys.TryGetValue(key, out doorman))
{
Expand All @@ -91,7 +91,7 @@ private static Doorman GetDoorman(string key)
{
if (lockTaken)
{
SpinLock.Exit();
spinLock.Exit();
}
}

Expand All @@ -109,7 +109,7 @@ private static void ReleaseDoorman(Doorman doorman)
bool lockTaken = false;
try
{
SpinLock.Enter(ref lockTaken);
spinLock.Enter(ref lockTaken);

if (--doorman.RefCount == 0)
{
Expand All @@ -125,7 +125,7 @@ private static void ReleaseDoorman(Doorman doorman)
{
if (lockTaken)
{
SpinLock.Exit();
spinLock.Exit();
}
}
}
Expand Down
17 changes: 5 additions & 12 deletions tests/ImageSharp.Web.Tests/Caching/AsyncKeyLockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

namespace SixLabors.ImageSharp.Web.Tests.Caching
{
[Collection(nameof(NonParallelFixture))]
public class AsyncKeyLockTests
{
private static readonly AsyncKeyLock AsyncLock = new AsyncKeyLock();
Expand All @@ -14,14 +13,7 @@ public class AsyncKeyLockTests
private const string AsyncKey2 = "ASYNC_KEY2";

[Fact]
public void AsyncLockActsAsDoorman()
{
// Run the two tests from a single test to see if we can stop tests freezing on Travis.
this.AsyncLockCanLockByKey();
this.AsyncLockAllowsDifferentKeysToRun();
}

private void AsyncLockCanLockByKey()
public async Task AsyncLockCanLockByKey()
{
bool zeroEntered = false;
bool entered = false;
Expand All @@ -46,11 +38,12 @@ private void AsyncLockCanLockByKey()
}
})).ToArray();

Task.WaitAll(tasks);
await Task.WhenAll(tasks);
Assert.Equal(5, index);
}

private void AsyncLockAllowsDifferentKeysToRun()
[Fact]
public async Task AsyncLockAllowsDifferentKeysToRun()
{
bool zeroEntered = false;
bool entered = false;
Expand All @@ -76,7 +69,7 @@ private void AsyncLockAllowsDifferentKeysToRun()

})).ToArray();

Task.WaitAll(tasks);
await Task.WhenAll(tasks);
Assert.Equal(5, index);
}
}
Expand Down
14 changes: 0 additions & 14 deletions tests/ImageSharp.Web.Tests/NonParallelFixture.cs

This file was deleted.

0 comments on commit c3af742

Please sign in to comment.