Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change node filters to use a concurrent list instead of a dictionary #698

Merged
merged 5 commits into from
Jun 4, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions NBitcoin/Utils/ThreadSafeList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class ThreadSafeList<T> : IEnumerable<T>
private List<T> _Behaviors;
private object _lock = new object();

private List<T> _EnumeratorList = null;

public ThreadSafeList()
{
lock (_lock)
Expand All @@ -29,6 +31,7 @@ public IDisposable Add(T item)
lock (_lock)
{
_Behaviors.Add(item);
_EnumeratorList = null;
}
return new ActionDisposable(() =>
{
Expand All @@ -48,6 +51,7 @@ public bool Remove(T item)
lock (_lock)
{
removed = _Behaviors.Remove(item);
_EnumeratorList = null;
}

if (removed)
Expand Down Expand Up @@ -92,12 +96,14 @@ public void Remove<U>() where U : T

public IEnumerator<T> GetEnumerator()
{
List<T> list = null;
lock (_lock)
if (_EnumeratorList == null)
{
list = _Behaviors.ToList();
lock (_lock)
{
_EnumeratorList = _Behaviors.ToList();
}
}
return list?.GetEnumerator();
return _EnumeratorList?.GetEnumerator();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please capture enumeratorList in a lock variable so we are sure GetEnumerator never returns null.

}

#endregion
Expand Down