This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Fix semantics of ArrayMemoryPool #27615
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Runtime.InteropServices; | ||
using System.Threading; | ||
|
||
#if !netstandard | ||
using Internal.Runtime.CompilerServices; | ||
#else | ||
|
@@ -21,13 +23,14 @@ private sealed class ArrayMemoryPoolBuffer : OwnedMemory<T> | |
public ArrayMemoryPoolBuffer(int size) | ||
{ | ||
_array = ArrayPool<T>.Shared.Rent(size); | ||
_refCount = 1; | ||
} | ||
|
||
public sealed override int Length => _array.Length; | ||
|
||
public sealed override bool IsDisposed => _array == null; | ||
|
||
protected sealed override bool IsRetained => _refCount > 0; | ||
protected sealed override bool IsRetained => Volatile.Read(ref _refCount) > 0; | ||
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. @stephentoub, does it need to be interlocked read? 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.
No, it's only 32-bit, so there's no risk of tearing and Interlocked.Read isn't needed. Volatile.Read may not even be necessary depending on what it's used for, but it's unlikely to hurt and so might as well be used. 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. Ah, I missed that it's an int. |
||
|
||
public sealed override Span<T> Span | ||
{ | ||
|
@@ -79,22 +82,30 @@ public sealed override MemoryHandle Pin(int byteOffset = 0) | |
|
||
public sealed override void Retain() | ||
{ | ||
if (IsDisposed) | ||
ThrowHelper.ThrowObjectDisposedException_ArrayMemoryPoolBuffer(); | ||
|
||
_refCount++; | ||
while (true) | ||
{ | ||
int currentCount = Volatile.Read(ref _refCount); | ||
if (currentCount <= 0) ThrowHelper.ThrowObjectDisposedException_ArrayMemoryPoolBuffer(); | ||
if (Interlocked.CompareExchange(ref _refCount, currentCount + 1, currentCount) == currentCount) break; | ||
} | ||
} | ||
|
||
public sealed override bool Release() | ||
{ | ||
if (IsDisposed) | ||
ThrowHelper.ThrowObjectDisposedException_ArrayMemoryPoolBuffer(); | ||
|
||
int newRefCount = --_refCount; | ||
if (newRefCount < 0) | ||
ThrowHelper.ThrowInvalidOperationException(); | ||
|
||
return newRefCount != 0; | ||
while (true) | ||
{ | ||
int currentCount = Volatile.Read(ref _refCount); | ||
if (currentCount <= 0) ThrowHelper.ThrowObjectDisposedException_ArrayMemoryPoolBuffer(); | ||
if (Interlocked.CompareExchange(ref _refCount, currentCount - 1, currentCount) == currentCount) | ||
{ | ||
if (currentCount == 1) | ||
{ | ||
Dispose(); | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains 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
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.
nit: add new line