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
Show file tree
Hide file tree
Changes from 4 commits
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
2 changes: 1 addition & 1 deletion NBitcoin/Protocol/Filters/NodeFiltersCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace NBitcoin.Protocol.Filters
{
public class NodeFiltersCollection : ThreadSafeCollection<INodeFilter>
public class NodeFiltersCollection : ThreadSafeList<INodeFilter>
{
public IDisposable Add(Action<IncomingMessage, Action> onReceiving, Action<Node, Payload, Action> onSending = null)
{
Expand Down
123 changes: 123 additions & 0 deletions NBitcoin/Utils/ThreadSafeList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NBitcoin
{
public class ThreadSafeList<T> : IEnumerable<T>
{
private List<T> _Behaviors;
private object _lock = new object();

private List<T> _EnumeratorList = null;

public ThreadSafeList()
{
lock (_lock)
_Behaviors = new List<T>();
}

/// <summary>
/// Add an item to the collection
/// </summary>
/// <param name="item"></param>
/// <returns>When disposed, the item is removed</returns>
public IDisposable Add(T item)
{
if (item == null)
throw new ArgumentNullException(nameof(item));
OnAdding(item);
lock (_lock)
{
_Behaviors.Add(item);
_EnumeratorList = null;
}
return new ActionDisposable(() =>
{
}, () => Remove(item));
}

protected virtual void OnAdding(T obj)
{
}
protected virtual void OnRemoved(T obj)
{
}

public bool Remove(T item)
{
bool removed = false;
lock (_lock)
{
removed = _Behaviors.Remove(item);
_EnumeratorList = null;
}

if (removed)
OnRemoved(item);
return removed;
}

public void Clear()
{
foreach (var behavior in this)
Remove(behavior);
}

public T FindOrCreate<U>() where U : T, new()
{
return FindOrCreate<U>(() => new U());
}
public U FindOrCreate<U>(Func<U> create) where U : T
{
var result = this.OfType<U>().FirstOrDefault();
if (result == null)
{
result = create();
Add(result);
}
return result;
}
public U Find<U>() where U : T
{
return this.OfType<U>().FirstOrDefault();
}

public void Remove<U>() where U : T
{
foreach (var b in this.OfType<U>())
{
Remove(b);
}
}

#region IEnumerable<T> Members

public IEnumerator<T> GetEnumerator()
{
IEnumerator<T> enumerator = null;

lock (_lock)
{
if (_EnumeratorList == null)
_EnumeratorList = _Behaviors.ToList();

enumerator = _EnumeratorList?.GetEnumerator();
}

return enumerator;
Copy link
Collaborator

@NicolasDorier NicolasDorier Jun 3, 2019

Choose a reason for hiding this comment

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

IEnumerator<T> enumerator = _EnumeratorList?.GetEnumerator();
if (enumerator == null)
{
   lock (_lock)
   {
       var behaviorsList = _Behaviors.ToList();
       _EnumeratorList = behaviorsList;
       enumerator =  behaviorsList.GetEnumerator();
   }
}
return enumerator;

So you lock only if really needed. And it is provable that it is impossible to get a null enumerator out of this function.

}

#endregion

#region IEnumerable Members

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

#endregion
}
}