Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Share CompletedTask between Task and AsyncTaskMethodBuilder #25423

Closed
Closed
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 @@ -191,13 +191,6 @@ private void NotifySynchronizationContextOfCompletion()
/// </remarks>
public struct AsyncTaskMethodBuilder
{
/// <summary>A cached VoidTaskResult task used for builders that complete synchronously.</summary>
#if PROJECTN
private static readonly Task<VoidTaskResult> s_cachedCompleted = AsyncTaskCache.CreateCacheableTask<VoidTaskResult>(default(VoidTaskResult));
#else
private static readonly Task<VoidTaskResult> s_cachedCompleted = AsyncTaskMethodBuilder<VoidTaskResult>.s_defaultResultTask;
#endif

/// <summary>The generic builder object to which this non-generic instance delegates.</summary>
private AsyncTaskMethodBuilder<VoidTaskResult> m_builder; // mutable struct: must not be readonly. Debugger depends on the exact name of this field.

Expand Down Expand Up @@ -270,7 +263,7 @@ public Task Task
/// </summary>
/// <exception cref="System.InvalidOperationException">The builder is not initialized.</exception>
/// <exception cref="System.InvalidOperationException">The task has already completed.</exception>
public void SetResult() => m_builder.SetResult(s_cachedCompleted); // Using s_cachedCompleted is faster than using s_defaultResultTask.
public void SetResult() => m_builder.SetResult(Task.s_cachedCompleted); // Using s_cachedCompleted is faster than using s_defaultResultTask.

/// <summary>
/// Completes the <see cref="System.Threading.Tasks.Task"/> in the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1483,8 +1483,11 @@ internal TaskScheduler? ExecutingTaskScheduler
/// </remarks>
public static TaskFactory Factory { get; } = new TaskFactory();

// Is a Task{VoidTaskResult} so it can be shared with AsyncTaskMethodBuilder
internal static readonly Task<VoidTaskResult> s_cachedCompleted = new Task<VoidTaskResult>(false, default, (TaskCreationOptions)InternalTaskOptions.DoNotDispose, default);

/// <summary>Gets a task that's already been completed successfully.</summary>
public static Task CompletedTask { get; } = new Task(false, (TaskCreationOptions)InternalTaskOptions.DoNotDispose, default);
public static Task CompletedTask => s_cachedCompleted;

/// <summary>
/// Provides an event that can be used to wait for completion.
Expand Down