Skip to content

Commit

Permalink
Merge branch 'development' of github.com:Washi1337/AsmResolver into d…
Browse files Browse the repository at this point in the history
…evelopment
  • Loading branch information
Washi1337 committed Oct 18, 2022
2 parents 54c65ee + f76f4f2 commit 44ba488
Showing 1 changed file with 37 additions and 16 deletions.
53 changes: 37 additions & 16 deletions src/AsmResolver/Collections/LazyList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;

namespace AsmResolver.Collections
{
Expand All @@ -12,6 +13,7 @@ namespace AsmResolver.Collections
[DebuggerDisplay("Count = {" + nameof(Count) + "}")]
public abstract class LazyList<TItem> : IList<TItem>
{
private readonly ReaderWriterLockSlim _lock = new(LockRecursionPolicy.NoRecursion);
private readonly List<TItem> _items;

/// <summary>
Expand Down Expand Up @@ -41,8 +43,11 @@ public TItem this[int index]
}
set
{
EnsureIsInitialized();
OnSetItem(index, value);
lock (_items)
{
EnsureIsInitialized();
OnSetItem(index, value);
}
}
}

Expand Down Expand Up @@ -104,7 +109,7 @@ private void EnsureIsInitialized()
{
if (!IsInitialized)
{
lock (this)
lock (_items)
{
if (!IsInitialized)
{
Expand All @@ -128,8 +133,11 @@ private void EnsureIsInitialized()
/// <inheritdoc />
public void Clear()
{
OnClearItems();
IsInitialized = true;
lock (_items)
{
OnClearItems();
IsInitialized = true;
}
}

/// <inheritdoc />
Expand All @@ -149,11 +157,15 @@ public void CopyTo(TItem[] array, int arrayIndex)
/// <inheritdoc />
public bool Remove(TItem item)
{
EnsureIsInitialized();
int index = Items.IndexOf(item);
if (index == -1)
return false;
OnRemoveItem(index);
lock (_items)
{
EnsureIsInitialized();
int index = Items.IndexOf(item);
if (index == -1)
return false;
OnRemoveItem(index);
}

return true;
}

Expand All @@ -167,8 +179,11 @@ public int IndexOf(TItem item)
/// <inheritdoc />
public void Insert(int index, TItem item)
{
EnsureIsInitialized();
OnInsertItem(index, item);
lock (_items)
{
EnsureIsInitialized();
OnInsertItem(index, item);
}
}

/// <summary>
Expand All @@ -178,15 +193,21 @@ public void Insert(int index, TItem item)
/// <param name="items">The items to insert.</param>
private void InsertRange(int index, IEnumerable<TItem> items)
{
EnsureIsInitialized();
OnInsertRange(index, items);
lock (_items)
{
EnsureIsInitialized();
OnInsertRange(index, items);
}
}

/// <inheritdoc />
public void RemoveAt(int index)
{
EnsureIsInitialized();
OnRemoveItem(index);
lock (_items)
{
EnsureIsInitialized();
OnRemoveItem(index);
}
}

/// <summary>
Expand Down

0 comments on commit 44ba488

Please sign in to comment.