Skip to content

Commit b68c40b

Browse files
authored
consistency (#430)
1 parent 6c73818 commit b68c40b

File tree

4 files changed

+18
-21
lines changed

4 files changed

+18
-21
lines changed

BitFaster.Caching/Atomic/AsyncAtomicFactory.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,19 @@ public V ValueIfCreated
9494

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

9999
if (init != null)
100100
{
101101
value = await init.CreateValueAsync(key, valueFactory).ConfigureAwait(false);
102-
initializer = null;
102+
Volatile.Write(ref initializer, null);
103103
}
104104

105105
return value;
106106
}
107107

108108
private class Initializer
109109
{
110-
private readonly object syncLock = new();
111110
private bool isInitialized;
112111
private Task<V> valueTask;
113112

@@ -145,12 +144,12 @@ private Task<V> DoubleCheck(Task<V> value)
145144
return valueTask;
146145
}
147146

148-
lock (syncLock)
147+
lock (this)
149148
{
150-
if (!Volatile.Read(ref isInitialized))
149+
if (!isInitialized)
151150
{
152151
valueTask = value;
153-
Volatile.Write(ref isInitialized, true);
152+
isInitialized = true;
154153
}
155154
}
156155

BitFaster.Caching/Atomic/AtomicFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,13 @@ public V CreateValue<TFactory>(K key, TFactory valueFactory) where TFactory : st
142142
{
143143
lock (this)
144144
{
145-
if (Volatile.Read(ref isInitialized))
145+
if (isInitialized)
146146
{
147147
return value;
148148
}
149149

150150
value = valueFactory.Create(key);
151-
Volatile.Write(ref isInitialized, true);
151+
isInitialized = true;
152152
return value;
153153
}
154154
}

BitFaster.Caching/Atomic/ScopedAsyncAtomicFactory.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ public bool TryCreateLifetime(out Lifetime<V> lifetime)
114114

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

119119
if (init != null)
120120
{
121121
scope = await init.CreateScopeAsync(key, valueFactory).ConfigureAwait(false);
122-
initializer = null;
122+
Volatile.Write(ref initializer, null);
123123
}
124124
}
125125

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

145145
private class Initializer
146146
{
147-
private readonly object syncLock = new();
148147
private bool isTaskInitialized;
149148
private bool isTaskCompleted;
150149
private bool isDisposeRequested;
@@ -191,12 +190,12 @@ private Task<Scoped<V>> DoubleCheck(Task<Scoped<V>> value)
191190
return task;
192191
}
193192

194-
lock (syncLock)
193+
lock (this)
195194
{
196-
if (!Volatile.Read(ref isTaskInitialized))
195+
if (!isTaskInitialized)
197196
{
198197
task = value;
199-
Volatile.Write(ref isTaskInitialized, true);
198+
isTaskInitialized = true;
200199
}
201200
}
202201

BitFaster.Caching/Atomic/ScopedAtomicFactory.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,31 +158,30 @@ public void Dispose()
158158

159159
private class Initializer
160160
{
161-
private readonly object syncLock = new();
162161
private bool isInitialized;
163162
private Scoped<V> value;
164163

165164
public Scoped<V> CreateScope<TFactory>(K key, TFactory valueFactory) where TFactory : struct, IValueFactory<K, Scoped<V>>
166165
{
167-
lock (syncLock)
166+
lock (this)
168167
{
169-
if (Volatile.Read(ref isInitialized))
168+
if (isInitialized)
170169
{
171170
return value;
172171
}
173172

174173
value = valueFactory.Create(key);
175-
Volatile.Write(ref isInitialized, true);
174+
isInitialized = true;
176175

177176
return value;
178177
}
179178
}
180179

181180
public Scoped<V> TryCreateDisposedScope()
182181
{
183-
lock (syncLock)
182+
lock (this)
184183
{
185-
if (Volatile.Read(ref isInitialized))
184+
if (isInitialized)
186185
{
187186
return value;
188187
}
@@ -191,7 +190,7 @@ public Scoped<V> TryCreateDisposedScope()
191190
var temp = new Scoped<V>(default);
192191
temp.Dispose();
193192
value = temp;
194-
Volatile.Write(ref isInitialized, true);
193+
isInitialized = true;
195194

196195
return value;
197196
}

0 commit comments

Comments
 (0)