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

Commit

Permalink
Use same Task for Task.CompletedTask and ATMB.Completed
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams committed Jun 5, 2019
1 parent d7e49ef commit d888c56
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 9 deletions.
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 via generic.

/// <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 @@ -1487,8 +1487,10 @@ bool IAsyncResult.CompletedSynchronously
/// </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

0 comments on commit d888c56

Please sign in to comment.