Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion BitFaster.Caching/Atomic/AsyncAtomicFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ private async ValueTask<V> CreateValueAsync<TFactory>(K key, TFactory valueFacto

private class Initializer
{
private readonly Lock locker = LockFactory.Create();
private bool isInitialized;
private Task<V>? valueTask;

Expand Down Expand Up @@ -178,7 +179,7 @@ private Task<V> DoubleCheck(Task<V> value)
return valueTask!;
}

lock (this)
lock (locker)
{
if (!isInitialized)
{
Expand Down
3 changes: 2 additions & 1 deletion BitFaster.Caching/Atomic/AtomicFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,14 @@ public override int GetHashCode()
#pragma warning disable CA2002 // Do not lock on objects with weak identity
private class Initializer
{
private readonly Lock locker = LockFactory.Create();
private bool isInitialized;
private V? value;
private ExceptionDispatchInfo? exceptionDispatch;

public V CreateValue<TFactory>(K key, TFactory valueFactory) where TFactory : struct, IValueFactory<K, V>
{
lock (this)
lock (locker)
{
if (isInitialized)
{
Expand Down
3 changes: 2 additions & 1 deletion BitFaster.Caching/Atomic/ScopedAsyncAtomicFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public void Dispose()

private class Initializer
{
private readonly Lock locker = LockFactory.Create();
private bool isTaskInitialized;
private bool isTaskCompleted;
private bool isDisposeRequested;
Expand Down Expand Up @@ -197,7 +198,7 @@ private Task<Scoped<V>> DoubleCheck(Task<Scoped<V>> value)
return task!;
}

lock (this)
lock (locker)
{
if (!isTaskInitialized)
{
Expand Down
5 changes: 3 additions & 2 deletions BitFaster.Caching/Atomic/ScopedAtomicFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,13 @@ public void Dispose()
#pragma warning disable CA2002 // Do not lock on objects with weak identity
private class Initializer
{
private readonly Lock locker = LockFactory.Create();
private bool isInitialized;
private Scoped<V>? value;

public Scoped<V> CreateScope<TFactory>(K key, TFactory valueFactory) where TFactory : struct, IValueFactory<K, Scoped<V>>
{
lock (this)
lock (locker)
{
if (isInitialized)
{
Expand All @@ -183,7 +184,7 @@ public Scoped<V> CreateScope<TFactory>(K key, TFactory valueFactory) where TFact

public Scoped<V> TryCreateDisposedScope()
{
lock (this)
lock (locker)
{
if (isInitialized)
{
Expand Down
9 changes: 8 additions & 1 deletion BitFaster.Caching/BitFaster.Caching.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
</ItemGroup>
<PackageReference Include="Backport.System.Threading.Lock" Version="3.1.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>analyzers</IncludeAssets>
</PackageReference>
<Using Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))" Alias="Lock" Include="System.Threading.Lock" />
<Using Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))" Alias="Lock" Include="Backport.System.Threading.Lock" />
<Using Alias="LockFactory" Include="Backport.System.Threading.LockFactory" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
Expand Down
11 changes: 6 additions & 5 deletions BitFaster.Caching/Lru/ClassicLru.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public sealed class ClassicLru<K, V> : ICacheExt<K, V>, IAsyncCacheExt<K, V>, IB
private readonly int capacity;
private readonly ConcurrentDictionary<K, LinkedListNode<LruItem>> dictionary;
private readonly LinkedList<LruItem> linkedList = new();
private readonly Lock locker = LockFactory.Create();

private readonly CacheMetrics metrics = new();
private readonly CachePolicy policy;
Expand Down Expand Up @@ -120,7 +121,7 @@ private bool TryAdd(K key, V value)
{
LinkedListNode<LruItem>? first = null;

lock (this.linkedList)
lock (this.locker)
{
if (linkedList.Count >= capacity)
{
Expand Down Expand Up @@ -303,7 +304,7 @@ private void OnRemove(LinkedListNode<LruItem> node)
// the List & Dictionary. Now thread A will try to move x to the end of the list.
if (node.List != null)
{
lock (this.linkedList)
lock (this.locker)
{
if (node.List != null)
{
Expand Down Expand Up @@ -350,7 +351,7 @@ public void AddOrUpdate(K key, V value)
{
LinkedListNode<LruItem>? first = null;

lock (this.linkedList)
lock (this.locker)
{
if (linkedList.Count >= capacity)
{
Expand Down Expand Up @@ -410,7 +411,7 @@ public void Trim(int itemCount)
{
LinkedListNode<LruItem>? first = null;

lock (this.linkedList)
lock (this.locker)
{
if (linkedList.Count > 0)
{
Expand Down Expand Up @@ -440,7 +441,7 @@ private void LockAndMoveToEnd(LinkedListNode<LruItem> node)
return;
}

lock (this.linkedList)
lock (this.locker)
{
if (node.List == null)
{
Expand Down
Loading