Skip to content

Commit

Permalink
Add Channel.CreateUnboundedPrioritized (dotnet#100550)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored and matouskozak committed Apr 30, 2024
1 parent d02647a commit a90e4b7
Show file tree
Hide file tree
Showing 11 changed files with 587 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@

<ItemGroup>
<Compile Include="System.Threading.Channels.cs" />
<Compile Include="System.Threading.Channels.netcoreapp.cs" Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'netstandard2.1'))"/>
<Compile Include="System.Threading.Channels.netstandard21.cs" Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'netstandard2.1'))"/>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'">
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime/ref/System.Runtime.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
<Compile Include="System.Threading.Channels.netcoreapp.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
<PackageReference Include="System.Threading.Tasks.Extensions" Version="$(SystemThreadingTasksExtensionsVersion)" />
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Bcl.AsyncInterfaces\ref\Microsoft.Bcl.AsyncInterfaces.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
// Changes to this file must follow the https://aka.ms/api-review process.
// ------------------------------------------------------------------------------

using System.Collections.Generic;

namespace System.Threading.Channels
{
public partial class ChannelClosedException : System.InvalidOperationException
public partial class Channel
{
public static System.Threading.Channels.Channel<T> CreateUnboundedPrioritized<T>() { throw null; }
public static System.Threading.Channels.Channel<T> CreateUnboundedPrioritized<T>(System.Threading.Channels.UnboundedPrioritizedChannelOptions<T> options) { throw null; }
}
public sealed partial class UnboundedPrioritizedChannelOptions<T> : System.Threading.Channels.ChannelOptions
{
#if NET8_0_OR_GREATER
[System.ObsoleteAttribute("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId = "SYSLIB0051", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
#endif
protected ChannelClosedException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
public System.Collections.Generic.IComparer<T>? Comparer { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// ------------------------------------------------------------------------------
// Changes to this file must follow the https://aka.ms/api-review process.
// ------------------------------------------------------------------------------

namespace System.Threading.Channels
{
public partial class ChannelClosedException : System.InvalidOperationException
{
#if NET8_0_OR_GREATER
[System.ObsoleteAttribute("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId = "SYSLIB0051", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
#endif
protected ChannelClosedException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,10 @@ System.Threading.Channel&lt;T&gt;</PackageDescription>
<ItemGroup>
<Compile Include="System\VoidResult.cs" />
<Compile Include="System\Threading\Channels\AsyncOperation.cs" />
<Compile Include="System\Threading\Channels\AsyncOperation.netcoreapp.cs"
Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'" />
<Compile Include="System\Threading\Channels\AsyncOperation.netstandard.cs"
Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'" />
<Compile Include="System\Threading\Channels\BoundedChannel.cs" />
<Compile Include="System\Threading\Channels\BoundedChannelFullMode.cs" />
<Compile Include="System\Threading\Channels\Channel.cs" />
<Compile Include="System\Threading\Channels\ChannelClosedException.cs" />
<Compile Include="System\Threading\Channels\ChannelClosedException.netcoreapp.cs"
Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'netstandard2.1'))" />
<Compile Include="System\Threading\Channels\ChannelOptions.cs" />
<Compile Include="System\Threading\Channels\ChannelReader.cs" />
<Compile Include="System\Threading\Channels\ChannelUtilities.cs" />
Expand All @@ -32,21 +26,25 @@ System.Threading.Channel&lt;T&gt;</PackageDescription>
<Compile Include="System\Threading\Channels\Channel_2.cs" />
<Compile Include="System\Threading\Channels\IDebugEnumerator.cs" />
<Compile Include="System\Threading\Channels\SingleConsumerUnboundedChannel.cs" />
<Compile Include="System\Threading\Channels\TaskCompletionSource.cs"
Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'"/>
<Compile Include="System\Threading\Channels\UnboundedChannel.cs" />
<Compile Include="$(CommonPath)Internal\Padding.cs"
Link="Common\Internal\Padding.cs" />
<Compile Include="$(CommonPath)System\Collections\Concurrent\IProducerConsumerQueue.cs"
Link="Common\System\Collections\Concurrent\IProducerConsumerQueue.cs" />
<Compile Include="$(CommonPath)System\Collections\Concurrent\MultiProducerMultiConsumerQueue.cs"
Link="Common\System\Collections\Concurrent\MultiProducerMultiConsumerQueue.cs" />
<Compile Include="$(CommonPath)System\Collections\Concurrent\SingleProducerSingleConsumerQueue.cs"
Link="Common\System\Collections\Concurrent\SingleProducerSingleConsumerQueue.cs" />
<Compile Include="$(CommonPath)System\Collections\Generic\Deque.cs"
Link="Common\System\Collections\Generic\Deque.cs" />
<Compile Include="$(CommonPath)System\Obsoletions.cs"
Link="Common\System\Obsoletions.cs" />
<Compile Include="$(CommonPath)Internal\Padding.cs" Link="Common\Internal\Padding.cs" />
<Compile Include="$(CommonPath)System\Collections\Concurrent\IProducerConsumerQueue.cs" Link="Common\System\Collections\Concurrent\IProducerConsumerQueue.cs" />
<Compile Include="$(CommonPath)System\Collections\Concurrent\MultiProducerMultiConsumerQueue.cs" Link="Common\System\Collections\Concurrent\MultiProducerMultiConsumerQueue.cs" />
<Compile Include="$(CommonPath)System\Collections\Concurrent\SingleProducerSingleConsumerQueue.cs" Link="Common\System\Collections\Concurrent\SingleProducerSingleConsumerQueue.cs" />
<Compile Include="$(CommonPath)System\Collections\Generic\Deque.cs" Link="Common\System\Collections\Generic\Deque.cs" />
<Compile Include="$(CommonPath)System\Obsoletions.cs" Link="Common\System\Obsoletions.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
<Compile Include="System\Threading\Channels\AsyncOperation.netstandard.cs" />
<Compile Include="System\Threading\Channels\TaskCompletionSource.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
<Compile Include="System\Threading\Channels\AsyncOperation.netcoreapp.cs" />
<Compile Include="System\Threading\Channels\Channel.netcoreapp.cs" />
<Compile Include="System\Threading\Channels\ChannelOptions.netcoreapp.cs" />
<Compile Include="System\Threading\Channels\UnboundedPriorityChannel.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'">
Expand All @@ -57,6 +55,10 @@ System.Threading.Channel&lt;T&gt;</PackageDescription>
<Reference Include="System.Threading.ThreadPool" />
</ItemGroup>

<ItemGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'netstandard2.1'))">
<Compile Include="System\Threading\Channels\ChannelClosedException.netcoreapp.cs" />
</ItemGroup>

<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'netstandard2.1'))">
<PackageReference Include="System.Threading.Tasks.Extensions" Version="$(SystemThreadingTasksExtensionsVersion)" />
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Bcl.AsyncInterfaces\src\Microsoft.Bcl.AsyncInterfaces.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace System.Threading.Channels
{
/// <summary>Provides static methods for creating channels.</summary>
public static class Channel
public static partial class Channel
{
/// <summary>Creates an unbounded channel usable by any number of readers and writers concurrently.</summary>
/// <returns>The created channel.</returns>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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;

namespace System.Threading.Channels
{
/// <summary>Provides static methods for creating channels.</summary>
public static partial class Channel
{
/// <summary>Creates an unbounded prioritized channel usable by any number of readers and writers concurrently.</summary>
/// <returns>The created channel.</returns>
/// <remarks>
/// <see cref="Comparer{T}.Default"/> is used to determine priority of elements.
/// The next item read from the channel will be the element available in the channel with the lowest priority value.
/// </remarks>
public static Channel<T> CreateUnboundedPrioritized<T>() =>
new UnboundedPrioritizedChannel<T>(runContinuationsAsynchronously: true, comparer: null);

/// <summary>Creates an unbounded prioritized channel subject to the provided options.</summary>
/// <typeparam name="T">Specifies the type of data in the channel.</typeparam>
/// <param name="options">Options that guide the behavior of the channel.</param>
/// <returns>The created channel.</returns>
/// <remarks>
/// The supplied <paramref name="options"/>' <see cref="UnboundedPrioritizedChannelOptions{T}.Comparer"/> is used to determine priority of elements,
/// or <see cref="Comparer{T}.Default"/> if the provided comparer is null.
/// The next item read from the channel will be the element available in the channel with the lowest priority value.
/// </remarks>
public static Channel<T> CreateUnboundedPrioritized<T>(UnboundedPrioritizedChannelOptions<T> options)
{
ArgumentNullException.ThrowIfNull(options);

return new UnboundedPrioritizedChannel<T>(!options.AllowSynchronousContinuations, options.Comparer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public abstract class ChannelOptions
public bool AllowSynchronousContinuations { get; set; }
}

/// <summary>Provides options that control the behavior of <see cref="BoundedChannel{T}"/> instances.</summary>
/// <summary>Provides options that control the behavior of instances created by <see cref="M:Channel.CreateBounded"/>.</summary>
public sealed class BoundedChannelOptions : ChannelOptions
{
/// <summary>The maximum number of items the bounded channel may store.</summary>
Expand Down Expand Up @@ -94,7 +94,7 @@ public BoundedChannelFullMode FullMode
}
}

/// <summary>Provides options that control the behavior of <see cref="UnboundedChannel{T}"/> instances.</summary>
/// <summary>Provides options that control the behavior of instances created by <see cref="M:Channel.CreateUnbounded"/>.</summary>
public sealed class UnboundedChannelOptions : ChannelOptions
{
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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;

namespace System.Threading.Channels
{
/// <summary>Provides options that control the behavior of instances created by <see cref="M:Channel.CreateUnboundedPrioritized"/>.</summary>
public sealed class UnboundedPrioritizedChannelOptions<T> : ChannelOptions
{
/// <summary>Gets or sets the comparer used by the channel to prioritize elements.</summary>
public IComparer<T>? Comparer { get; set; }
}
}
Loading

0 comments on commit a90e4b7

Please sign in to comment.