Skip to content
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
70 changes: 70 additions & 0 deletions src/CenterEdge.Async/AsyncHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ public static void RunSync(Func<Task> task)
/// </remarks>
public static void RunSync<TState>(Func<TState, Task> task, TState state)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(task);
#else
if (task is null)
{
ThrowHelper.ThrowArgumentNullException(nameof(task));
return; // unreachable, but helps static analysis
}
#endif

var oldContext = SynchronizationContext.Current;

if (IsDeadlockSafe(oldContext))
Expand Down Expand Up @@ -101,6 +111,16 @@ public static void RunSync<TState>(Func<TState, Task> task, TState state)
[OverloadResolutionPriority(-1)]
public static void RunSync(Func<ValueTask> task)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(task);
#else
if (task is null)
{
ThrowHelper.ThrowArgumentNullException(nameof(task));
return; // unreachable, but helps static analysis
}
#endif

RunSync(static state => state.Invoke(), task);
}

Expand All @@ -116,6 +136,16 @@ public static void RunSync(Func<ValueTask> task)
[OverloadResolutionPriority(-1)]
public static void RunSync<TState>(Func<TState, ValueTask> task, TState state)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(task);
#else
if (task is null)
{
ThrowHelper.ThrowArgumentNullException(nameof(task));
return; // unreachable, but helps static analysis
}
#endif

var oldContext = SynchronizationContext.Current;

if (IsDeadlockSafe(oldContext))
Expand Down Expand Up @@ -161,6 +191,16 @@ public static void RunSync<TState>(Func<TState, ValueTask> task, TState state)
/// </remarks>
public static T RunSync<T>(Func<Task<T>> task)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(task);
#else
if (task is null)
{
ThrowHelper.ThrowArgumentNullException(nameof(task));
return default!; // unreachable, but helps static analysis
}
#endif

return RunSync(static state => state.Invoke(), task);
}

Expand All @@ -176,6 +216,16 @@ public static T RunSync<T>(Func<Task<T>> task)
/// </remarks>
public static T RunSync<T, TState>(Func<TState, Task<T>> task, TState state)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(task);
#else
if (task is null)
{
ThrowHelper.ThrowArgumentNullException(nameof(task));
return default!; // unreachable, but helps static analysis
}
#endif

var oldContext = SynchronizationContext.Current;

if (IsDeadlockSafe(oldContext))
Expand Down Expand Up @@ -219,6 +269,16 @@ public static T RunSync<T, TState>(Func<TState, Task<T>> task, TState state)
[OverloadResolutionPriority(-1)]
public static T RunSync<T>(Func<ValueTask<T>> task)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(task);
#else
if (task is null)
{
ThrowHelper.ThrowArgumentNullException(nameof(task));
return default!; // unreachable, but helps static analysis
}
#endif

return RunSync(static state => state.Invoke(), task);
}

Expand All @@ -235,6 +295,16 @@ public static T RunSync<T>(Func<ValueTask<T>> task)
[OverloadResolutionPriority(-1)]
public static T RunSync<T, TState>(Func<TState, ValueTask<T>> task, TState state)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(task);
#else
if (task is null)
{
ThrowHelper.ThrowArgumentNullException(nameof(task));
return default!; // unreachable, but helps static analysis
}
#endif

var oldContext = SynchronizationContext.Current;

if (IsDeadlockSafe(oldContext))
Expand Down
4 changes: 4 additions & 0 deletions src/CenterEdge.Async/CenterEdge.Async.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.6.3" />
</ItemGroup>

<ItemGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">
<Compile Remove="ThrowHelper.cs" />
</ItemGroup>

<ItemGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))">
<Compile Remove="OverloadResolutionPriorityAttribute.cs" />
</ItemGroup>
Expand Down
10 changes: 10 additions & 0 deletions src/CenterEdge.Async/ExclusiveSynchronizationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ public override void Send(SendOrPostCallback d, object? state)

public override void Post(SendOrPostCallback d, object? state)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(d);
#else
if (d is null)
{
ThrowHelper.ThrowArgumentNullException(nameof(d));
Comment thread
brantburnett marked this conversation as resolved.
return; // unreachable, but helps static analysis
}
#endif

try
{
_items.Add(new WorkItem(d, state));
Expand Down
9 changes: 9 additions & 0 deletions src/CenterEdge.Async/ThrowHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace CenterEdge.Async;

internal class ThrowHelper
{
public static void ThrowArgumentNullException(string paramName) =>
throw new ArgumentNullException(paramName);
}
Loading