Skip to content

Commit

Permalink
Remove Lock from SimpleThreadSafeDictionary in benefit of ReaderWrite…
Browse files Browse the repository at this point in the history
…rLockSlim

castleproject#565
  • Loading branch information
generik0 committed Nov 25, 2020
1 parent e9de148 commit 6203294
Showing 1 changed file with 41 additions and 14 deletions.
55 changes: 41 additions & 14 deletions src/Castle.Windsor/Core/Internal/SimpleThreadSafeDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace Castle.Core.Internal
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

/// <summary>
/// Simple type for thread safe adding/reading to/from keyed store. The difference between this and built in concurrent dictionary is that in this case adding is happening under a lock so never more than one thread will be adding at a time.
Expand All @@ -26,14 +27,19 @@ namespace Castle.Core.Internal
public class SimpleThreadSafeDictionary<TKey, TValue>
{
private readonly Dictionary<TKey, TValue> inner = new Dictionary<TKey, TValue>();
private readonly Lock @lock = Lock.Create();
private readonly ReaderWriterLockSlim @lock = new ReaderWriterLockSlim();

public bool Contains(TKey key)
{
using (@lock.ForReading())
@lock.EnterReadLock();
try
{
return inner.ContainsKey(key);
}
finally
{
@lock.ExitReadLock();
}
}

/// <summary>
Expand All @@ -42,48 +48,69 @@ public bool Contains(TKey key)
/// <returns> </returns>
public TValue[] EjectAllValues()
{
using (@lock.ForWriting())
@lock.EnterWriteLock();
try
{
var values = inner.Values.ToArray();
inner.Clear();
return values;
}
finally
{
@lock.ExitWriteLock();
}
}

public TValue GetOrAdd(TKey key, Func<TKey, TValue> factory)
{
using (var token = @lock.ForReadingUpgradeable())

@lock.EnterUpgradeableReadLock();
try
{
TValue value;
if (inner.TryGetValue(key, out value))
if (inner.TryGetValue(key, out var value))
{
return value;
}
// We can safely allow reads from other threads while preparing new value, since
// only 1 thread can hold upgradable read lock (even write requests will wait on it).
// Also this helps to prevent downstream deadlocks due to factory method call
var newValue = factory(key);
token.Upgrade();
if (inner.TryGetValue(key, out value))
@lock.EnterWriteLock();
try
{
if (inner.TryGetValue(key, out value))
{
return value;
}
value = newValue;
inner.Add(key, value);
return value;
}
value = newValue;
inner.Add(key, value);
return value;
finally
{
@lock.ExitWriteLock();
}
}
finally
{
@lock.EnterUpgradeableReadLock();
}
}

public TValue GetOrThrow(TKey key)
{
using (@lock.ForReading())
@lock.EnterReadLock();
try
{
TValue value;
if (inner.TryGetValue(key, out value))
if (inner.TryGetValue(key, out var value))
{
return value;
}
}
finally
{
@lock.ExitReadLock();
}
throw new ArgumentException(string.Format("Item for key {0} was not found.", key));
}
}
Expand Down

0 comments on commit 6203294

Please sign in to comment.