-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detriplicate internal SingleProducerSingleConsumerQueue (#76932)
System.Private.CoreLib, System.Threading.Tasks.Dataflow, and System.Threading.Channels have all ended up with their own copy of SingleProducerSingleConsumerQueue and its associated helpers. This consolidates them down to a single shared copy. There's no functional change here, just deleting duplicates and moving things around.
- Loading branch information
1 parent
0c0102d
commit 70fb135
Showing
12 changed files
with
221 additions
and
923 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/libraries/Common/src/System/Collections/Concurrent/IProducerConsumerQueue.cs
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace System.Collections.Concurrent | ||
{ | ||
/// <summary>Represents a producer/consumer queue.</summary> | ||
/// <typeparam name="T">Specifies the type of data contained in the queue.</typeparam> | ||
internal interface IProducerConsumerQueue<T> : IEnumerable<T> | ||
{ | ||
/// <summary>Enqueues an item into the queue.</summary> | ||
/// <param name="item">The item to enqueue.</param> | ||
/// <remarks>This method is meant to be thread-safe subject to the particular nature of the implementation.</remarks> | ||
void Enqueue(T item); | ||
|
||
/// <summary>Attempts to dequeue an item from the queue.</summary> | ||
/// <param name="result">The dequeued item.</param> | ||
/// <returns>true if an item could be dequeued; otherwise, false.</returns> | ||
/// <remarks>This method is meant to be thread-safe subject to the particular nature of the implementation.</remarks> | ||
bool TryDequeue([MaybeNullWhen(false)] out T result); | ||
|
||
/// <summary>Gets whether the collection is currently empty.</summary> | ||
/// <remarks>This method may or may not be thread-safe.</remarks> | ||
bool IsEmpty { get; } | ||
|
||
/// <summary>Gets the number of items in the collection.</summary> | ||
/// <remarks>In many implementations, this method will not be thread-safe.</remarks> | ||
int Count { get; } | ||
|
||
/// <summary>A thread-safe way to get the number of items in the collection. May synchronize access by locking the provided synchronization object.</summary> | ||
/// <param name="syncObj">The sync object used to lock</param> | ||
/// <returns>The collection count</returns> | ||
int GetCountSafe(object syncObj); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/libraries/Common/src/System/Collections/Concurrent/MultiProducerMultiConsumerQueue.cs
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace System.Collections.Concurrent | ||
{ | ||
/// <summary> | ||
/// Provides a producer/consumer queue safe to be used by any number of producers and consumers concurrently. | ||
/// </summary> | ||
/// <typeparam name="T">Specifies the type of data contained in the queue.</typeparam> | ||
[DebuggerDisplay("Count = {Count}")] | ||
internal sealed class MultiProducerMultiConsumerQueue<T> : ConcurrentQueue<T>, IProducerConsumerQueue<T> | ||
{ | ||
/// <summary>Enqueues an item into the queue.</summary> | ||
/// <param name="item">The item to enqueue.</param> | ||
void IProducerConsumerQueue<T>.Enqueue(T item) { base.Enqueue(item); } | ||
|
||
/// <summary>Attempts to dequeue an item from the queue.</summary> | ||
/// <param name="result">The dequeued item.</param> | ||
/// <returns>true if an item could be dequeued; otherwise, false.</returns> | ||
bool IProducerConsumerQueue<T>.TryDequeue([MaybeNullWhen(false)] out T result) { return base.TryDequeue(out result); } | ||
|
||
/// <summary>Gets whether the collection is currently empty.</summary> | ||
bool IProducerConsumerQueue<T>.IsEmpty => base.IsEmpty; | ||
|
||
/// <summary>Gets the number of items in the collection.</summary> | ||
int IProducerConsumerQueue<T>.Count => base.Count; | ||
|
||
/// <summary>A thread-safe way to get the number of items in the collection. May synchronize access by locking the provided synchronization object.</summary> | ||
/// <remarks>ConcurrentQueue.Count is thread safe, no need to acquire the lock.</remarks> | ||
int IProducerConsumerQueue<T>.GetCountSafe(object syncObj) => base.Count; | ||
} | ||
} |
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
Oops, something went wrong.