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

Use tighter locking for Node list enumerator #7070

Merged
merged 3 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ public struct BondedItemsEnumerator : IEnumerator<NodeBucketItem>, IEnumerable<N
public BondedItemsEnumerator(NodeBucket nodeBucket)
{
_nodeBucket = nodeBucket;
lock (_nodeBucket._nodeBucketLock)
{
_currentNode = nodeBucket._items.Last;
}
_referenceTime = DateTime.UtcNow;
Monitor.Enter(_nodeBucket._nodeBucketLock);
_currentNode = nodeBucket._items.Last;
Current = null!;
}

Expand All @@ -63,13 +65,16 @@ public BondedItemsEnumerator(NodeBucket nodeBucket)

public bool MoveNext()
{
while (_currentNode is not null)
lock (_nodeBucket._nodeBucketLock)
{
Current = _currentNode.Value;
_currentNode = _currentNode.Previous;
if (Current.IsBonded(_referenceTime))
while (_currentNode is not null)
{
return true;
Current = _currentNode.Value;
_currentNode = _currentNode.Previous;
if (Current.IsBonded(_referenceTime))
{
return true;
}
}
}

Expand All @@ -81,11 +86,6 @@ public bool MoveNext()

public void Dispose()
{
if (_nodeBucket is not null)
{
Monitor.Exit(_nodeBucket._nodeBucketLock);
}
_nodeBucket = null!;
}
public BondedItemsEnumerator GetEnumerator() => this;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,40 +105,33 @@ public ClosestNodesEnumerator(NodeBucket[] buckets, int bucketSize)

public bool MoveNext()
{
try
while (_count < _bucketSize)
{
while (_count < _bucketSize)
if (!_enumeratorSet || !_itemEnumerator.MoveNext())
{
if (!_enumeratorSet || !_itemEnumerator.MoveNext())
_itemEnumerator.Dispose();
_bucketIndex++;
if (_bucketIndex >= _buckets.Length)
{
_itemEnumerator.Dispose();
_bucketIndex++;
if (_bucketIndex >= _buckets.Length)
{
return false;
}

_itemEnumerator = _buckets[_bucketIndex].BondedItems.GetEnumerator();
_enumeratorSet = true;
continue;
return false;
}

Current = _itemEnumerator.Current.Node!;
_count++;
return true;
_itemEnumerator = _buckets[_bucketIndex].BondedItems.GetEnumerator();
_enumeratorSet = true;
continue;
}
}
finally
{
_itemEnumerator.Dispose();

Current = _itemEnumerator.Current.Node!;
_count++;
return true;
}

return false;
}

void IEnumerator.Reset() => throw new NotSupportedException();

public void Dispose() { }
public void Dispose() => _itemEnumerator.Dispose();

public ClosestNodesEnumerator GetEnumerator() => this;

Expand Down