Skip to content

Commit

Permalink
Remove lock object of lazy variable, and use 'this' instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
Washi1337 committed Sep 17, 2022
1 parent d10cf47 commit 16367c1
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/AsmResolver/LazyVariable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ namespace AsmResolver
/// Represents a variable that can be lazily initialized and/or assigned a new value.
/// </summary>
/// <typeparam name="T">The type of the values that the variable stores.</typeparam>
/// <remarks>
/// For performance reasons, this class locks on itself for thread synchronization. Therefore, consumers
/// should not lock instances of this class as a lock object to avoid dead-locks.
/// </remarks>
public class LazyVariable<T>
{
private T? _value;
private readonly Func<T?>? _getValue;
private readonly object _lockObject = new();

/// <summary>
/// Creates a new lazy variable and initialize it with a constant.
Expand Down Expand Up @@ -55,7 +58,7 @@ public T Value
}
set
{
lock (_lockObject)
lock (this)
{
_value = value;
IsInitialized = true;
Expand All @@ -65,7 +68,7 @@ public T Value

private void InitializeValue()
{
lock (_lockObject)
lock (this)
{
if (!IsInitialized)
{
Expand Down

0 comments on commit 16367c1

Please sign in to comment.