Skip to content

Commit

Permalink
consistency (#430)
Browse files Browse the repository at this point in the history
  • Loading branch information
bitfaster authored Oct 18, 2023
1 parent 6c73818 commit b68c40b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 21 deletions.
11 changes: 5 additions & 6 deletions BitFaster.Caching/Atomic/AsyncAtomicFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,19 @@ public V ValueIfCreated

private async ValueTask<V> CreateValueAsync<TFactory>(K key, TFactory valueFactory) where TFactory : struct, IAsyncValueFactory<K, V>
{
var init = initializer;
var init = Volatile.Read(ref initializer);

if (init != null)
{
value = await init.CreateValueAsync(key, valueFactory).ConfigureAwait(false);
initializer = null;
Volatile.Write(ref initializer, null);
}

return value;
}

private class Initializer
{
private readonly object syncLock = new();
private bool isInitialized;
private Task<V> valueTask;

Expand Down Expand Up @@ -145,12 +144,12 @@ private Task<V> DoubleCheck(Task<V> value)
return valueTask;
}

lock (syncLock)
lock (this)
{
if (!Volatile.Read(ref isInitialized))
if (!isInitialized)
{
valueTask = value;
Volatile.Write(ref isInitialized, true);
isInitialized = true;
}
}

Expand Down
4 changes: 2 additions & 2 deletions BitFaster.Caching/Atomic/AtomicFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,13 @@ public V CreateValue<TFactory>(K key, TFactory valueFactory) where TFactory : st
{
lock (this)
{
if (Volatile.Read(ref isInitialized))
if (isInitialized)
{
return value;
}

value = valueFactory.Create(key);
Volatile.Write(ref isInitialized, true);
isInitialized = true;
return value;
}
}
Expand Down
11 changes: 5 additions & 6 deletions BitFaster.Caching/Atomic/ScopedAsyncAtomicFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ public bool TryCreateLifetime(out Lifetime<V> lifetime)

private async ValueTask InitializeScopeAsync<TFactory>(K key, TFactory valueFactory) where TFactory : struct, IAsyncValueFactory<K, Scoped<V>>
{
var init = initializer;
var init = Volatile.Read(ref initializer);

if (init != null)
{
scope = await init.CreateScopeAsync(key, valueFactory).ConfigureAwait(false);
initializer = null;
Volatile.Write(ref initializer, null);
}
}

Expand All @@ -144,7 +144,6 @@ public void Dispose()

private class Initializer
{
private readonly object syncLock = new();
private bool isTaskInitialized;
private bool isTaskCompleted;
private bool isDisposeRequested;
Expand Down Expand Up @@ -191,12 +190,12 @@ private Task<Scoped<V>> DoubleCheck(Task<Scoped<V>> value)
return task;
}

lock (syncLock)
lock (this)
{
if (!Volatile.Read(ref isTaskInitialized))
if (!isTaskInitialized)
{
task = value;
Volatile.Write(ref isTaskInitialized, true);
isTaskInitialized = true;
}
}

Expand Down
13 changes: 6 additions & 7 deletions BitFaster.Caching/Atomic/ScopedAtomicFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,31 +158,30 @@ public void Dispose()

private class Initializer
{
private readonly object syncLock = new();
private bool isInitialized;
private Scoped<V> value;

public Scoped<V> CreateScope<TFactory>(K key, TFactory valueFactory) where TFactory : struct, IValueFactory<K, Scoped<V>>
{
lock (syncLock)
lock (this)
{
if (Volatile.Read(ref isInitialized))
if (isInitialized)
{
return value;
}

value = valueFactory.Create(key);
Volatile.Write(ref isInitialized, true);
isInitialized = true;

return value;
}
}

public Scoped<V> TryCreateDisposedScope()
{
lock (syncLock)
lock (this)
{
if (Volatile.Read(ref isInitialized))
if (isInitialized)
{
return value;
}
Expand All @@ -191,7 +190,7 @@ public Scoped<V> TryCreateDisposedScope()
var temp = new Scoped<V>(default);
temp.Dispose();
value = temp;
Volatile.Write(ref isInitialized, true);
isInitialized = true;

return value;
}
Expand Down

0 comments on commit b68c40b

Please sign in to comment.