Skip to content
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

Dedup Task.WhenAll non-generic and generic implementations #88154

Merged
merged 2 commits into from
Jul 5, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -1626,15 +1626,19 @@ public Task<TResult> ContinueWhenAll<TAntecedentResult>(Task<TAntecedentResult>[
if (continuationFunction != null)
{
return starter.ContinueWith(
GenericDelegateCache<TAntecedentResult, TResult>.CWAllFuncDelegate,
static (starter, continuationFunction) => ((Func<Task<TAntecedentResult>[], TResult>)continuationFunction!)(starter.Result),
continuationFunction, scheduler, cancellationToken, continuationOptions);
}
else
{
Debug.Assert(continuationAction != null);

return starter.ContinueWith(
GenericDelegateCache<TAntecedentResult, TResult>.CWAllActionDelegate,
static (starter, continuationAction) =>
{
((Action<Task<TAntecedentResult>[]>)continuationAction!)(starter.Result);
return default(TResult)!;
},
continuationAction, scheduler, cancellationToken, continuationOptions);
}
}
Expand Down Expand Up @@ -2026,7 +2030,7 @@ public Task<TResult> ContinueWhenAny<TAntecedentResult>(Task<TAntecedentResult>[
if (scheduler == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.scheduler);

// Call common ContinueWhenAny setup logic, extract starter
Task<Task> starter = TaskFactory.CommonCWAnyLogic(tasks);
Task<Task<TAntecedentResult>> starter = TaskFactory.CommonCWAnyLogic(tasks);

// Bail early if cancellation has been requested.
if (cancellationToken.IsCancellationRequested
Expand All @@ -2040,64 +2044,20 @@ public Task<TResult> ContinueWhenAny<TAntecedentResult>(Task<TAntecedentResult>[
if (continuationFunction != null)
{
return starter.ContinueWith(
GenericDelegateCache<TAntecedentResult, TResult>.CWAnyFuncDelegate,
static (starter, continuationFunction) => ((Func<Task<TAntecedentResult>, TResult>)continuationFunction!)(starter.Result),
continuationFunction, scheduler, cancellationToken, continuationOptions);
}
else
{
Debug.Assert(continuationAction != null);
return starter.ContinueWith(
GenericDelegateCache<TAntecedentResult, TResult>.CWAnyActionDelegate,
static (starter, continuationAction) =>
{
((Action<Task<TAntecedentResult>>)continuationAction!)(starter.Result);
return default(TResult)!;
},
continuationAction, scheduler, cancellationToken, continuationOptions);
}
}
}

// For the ContinueWhenAnyImpl/ContinueWhenAllImpl methods that are generic on TAntecedentResult,
// the compiler won't cache the internal ContinueWith delegate because it is generic on both
// TAntecedentResult and TResult. The GenericDelegateCache serves as a cache for those delegates.
internal static class GenericDelegateCache<TAntecedentResult, TResult>
{
// ContinueWith delegate for TaskFactory<TResult>.ContinueWhenAnyImpl<TAntecedentResult>(non-null continuationFunction)
internal static Func<Task<Task>, object?, TResult> CWAnyFuncDelegate =
static (Task<Task> wrappedWinner, object? state) =>
{
Debug.Assert(state is Func<Task<TAntecedentResult>, TResult>);
var func = (Func<Task<TAntecedentResult>, TResult>)state;
var arg = (Task<TAntecedentResult>)wrappedWinner.Result;
return func(arg);
};

// ContinueWith delegate for TaskFactory<TResult>.ContinueWhenAnyImpl<TAntecedentResult>(non-null continuationAction)
internal static Func<Task<Task>, object?, TResult> CWAnyActionDelegate =
static (Task<Task> wrappedWinner, object? state) =>
{
Debug.Assert(state is Action<Task<TAntecedentResult>>);
var action = (Action<Task<TAntecedentResult>>)state;
var arg = (Task<TAntecedentResult>)wrappedWinner.Result;
action(arg);
return default!;
};

// ContinueWith delegate for TaskFactory<TResult>.ContinueWhenAllImpl<TAntecedentResult>(non-null continuationFunction)
internal static Func<Task<Task<TAntecedentResult>[]>, object?, TResult> CWAllFuncDelegate =
static (Task<Task<TAntecedentResult>[]> wrappedAntecedents, object? state) =>
{
wrappedAntecedents.NotifyDebuggerOfWaitCompletionIfNecessary();
Debug.Assert(state is Func<Task<TAntecedentResult>[], TResult>);
var func = (Func<Task<TAntecedentResult>[], TResult>)state;
return func(wrappedAntecedents.Result);
};

// ContinueWith delegate for TaskFactory<TResult>.ContinueWhenAllImpl<TAntecedentResult>(non-null continuationAction)
internal static Func<Task<Task<TAntecedentResult>[]>, object?, TResult> CWAllActionDelegate =
static (Task<Task<TAntecedentResult>[]> wrappedAntecedents, object? state) =>
{
wrappedAntecedents.NotifyDebuggerOfWaitCompletionIfNecessary();
Debug.Assert(state is Action<Task<TAntecedentResult>[]>);
var action = (Action<Task<TAntecedentResult>[]>)state;
action(wrappedAntecedents.Result);
return default!;
};
}
}
Loading
Loading