-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Track the actual item count in BlockingCollection #133637
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
Open
aw0lid
wants to merge
1
commit into
dotnet:main
Choose a base branch
from
aw0lid:fix/blockingcollection-iscompleted-cancellation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+52
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,6 +45,7 @@ namespace System.Collections.Concurrent | |
| public class BlockingCollection<T> : IEnumerable<T>, ICollection, IDisposable, IReadOnlyCollection<T> | ||
| { | ||
| private IProducerConsumerCollection<T> _collection; | ||
| private int _actualCount; | ||
| private int _boundedCapacity; | ||
| private const int NON_BOUNDED = -1; | ||
| private SemaphoreSlim? _freeNodes; | ||
|
|
@@ -92,7 +93,7 @@ public bool IsCompleted | |
| get | ||
| { | ||
| CheckDisposed(); | ||
| return (IsAddingCompleted && (_occupiedNodes.CurrentCount == 0)); | ||
| return (IsAddingCompleted && (Volatile.Read(ref _actualCount) == 0)); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -105,7 +106,7 @@ public int Count | |
| get | ||
| { | ||
| CheckDisposed(); | ||
| return _occupiedNodes.CurrentCount; | ||
| return Volatile.Read(ref _actualCount); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -212,6 +213,7 @@ private void Initialize(IProducerConsumerCollection<T> collection, int boundedCa | |
| Debug.Assert(boundedCapacity > 0 || boundedCapacity == NON_BOUNDED); | ||
|
|
||
| _collection = collection; | ||
| _actualCount = collectionCount; | ||
| _boundedCapacity = boundedCapacity; | ||
| _isDisposed = false; | ||
| _consumersCancellationTokenSource = new CancellationTokenSource(); | ||
|
|
@@ -480,9 +482,18 @@ private bool TryAddWithNoTimeValidation(T item, int millisecondsTimeout, Cancell | |
| finally | ||
| { | ||
| if (addingSucceeded) | ||
| { | ||
| int current; | ||
| do | ||
| { | ||
| current = Volatile.Read(ref _actualCount); | ||
| } | ||
| while (Interlocked.CompareExchange(ref _actualCount, current + 1, current) != current); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| //After adding an element to the underlying storage, signal to the consumers | ||
| //waiting on _occupiedNodes that there is a new item added ready to be consumed. | ||
| _occupiedNodes.Release(); | ||
| } | ||
| else | ||
| //TryAdd did not result in increasing the size of the underlying store and hence we need | ||
| //to increment back the count of the _freeNodes semaphore. | ||
|
|
@@ -706,6 +717,13 @@ private bool TryTakeWithNoTimeValidation([MaybeNullWhen(false)] out T item, int | |
| // removeFaulted implies !removeSucceeded, but the reverse is not true. | ||
| if (removeSucceeded) | ||
| { | ||
| int current; | ||
| do | ||
| { | ||
| current = Volatile.Read(ref _actualCount); | ||
| } | ||
| while (Interlocked.CompareExchange(ref _actualCount, current - 1, current) != current); | ||
|
|
||
| if (_freeNodes != null) | ||
| { | ||
| Debug.Assert(_boundedCapacity != NON_BOUNDED); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can drop the volatile read, because below the interlocked operation provides the barrier.