Skip to content

Commit 9f9d22a

Browse files
authored
Merge pull request #130 from abstratovcm/fix-trylookup-thread
fix: add thread safety to TryLookup by locking _storeLock
2 parents 4a08183 + 1e756b9 commit 9f9d22a

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/Mzinga/Core/FixedCache.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,23 @@ private void StoreInternal(TKey key, TEntry newEntry)
114114

115115
public bool TryLookup(TKey key, [NotNullWhen(returnValue: true)] out TEntry? entry)
116116
{
117-
if (_dict.TryGetValue(key, out FixedCacheEntry<TKey, TEntry>? wrappedEntry))
117+
lock (_storeLock)
118118
{
119-
entry = wrappedEntry.Entry;
119+
if (_dict.TryGetValue(key, out FixedCacheEntry<TKey, TEntry>? wrappedEntry))
120+
{
121+
entry = wrappedEntry.Entry;
120122
#if DEBUG
121-
Metrics.Hit();
123+
Metrics.Hit();
122124
#endif
123-
return true;
124-
}
125+
return true;
126+
}
125127
#if DEBUG
126-
Metrics.Miss();
128+
Metrics.Miss();
127129
#endif
128130

129-
entry = default;
130-
return false;
131+
entry = default;
132+
return false;
133+
}
131134
}
132135

133136
public void Clear()

0 commit comments

Comments
 (0)