forked from microsoft/vs-threading
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskCompletionSourceWithoutInlining`1.cs
More file actions
64 lines (59 loc) · 3.05 KB
/
Copy pathTaskCompletionSourceWithoutInlining`1.cs
File metadata and controls
64 lines (59 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.VisualStudio.Threading
{
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
/// <summary>
/// A <see cref="TaskCompletionSource{TResult}"/>-derivative that
/// does not inline continuations if so configured.
/// </summary>
/// <typeparam name="T">The type of the task's resulting value.</typeparam>
internal class TaskCompletionSourceWithoutInlining<T> : TaskCompletionSource<T>
{
/// <summary>
/// The Task that we expose to others that may not inline continuations.
/// </summary>
private readonly Task<T> exposedTask;
/// <summary>
/// Initializes a new instance of the <see cref="TaskCompletionSourceWithoutInlining{T}"/> class.
/// </summary>
/// <param name="allowInliningContinuations">
/// <c>true</c> to allow continuations to be inlined; otherwise <c>false</c>.
/// </param>
/// <param name="options">
/// TaskCreationOptions to pass on to the base constructor.
/// </param>
/// <param name="state">The state to set on the Task.</param>
internal TaskCompletionSourceWithoutInlining(bool allowInliningContinuations, TaskCreationOptions options = TaskCreationOptions.None, object? state = null)
: base(state, AdjustFlags(options, allowInliningContinuations))
{
this.exposedTask = base.Task;
}
/// <summary>
/// Gets the <see cref="Task"/> that may never complete inline with completion of this <see cref="TaskCompletionSource{TResult}"/>.
/// </summary>
/// <devremarks>
/// Return the base.Task if it is already completed since inlining continuations
/// on the completer is no longer a concern. Also, when we are not inlining continuations,
/// this.exposedTask completes slightly later than base.Task, and callers expect
/// the Task we return to be complete as soon as they call TrySetResult.
/// </devremarks>
internal new Task<T> Task => base.Task.IsCompleted ? base.Task : this.exposedTask;
/// <summary>
/// Modifies the specified flags to include RunContinuationsAsynchronously
/// if wanted by the caller and supported by the platform.
/// </summary>
/// <param name="options">The base options supplied by the caller.</param>
/// <param name="allowInliningContinuations"><c>true</c> to allow inlining continuations.</param>
/// <returns>The possibly modified flags.</returns>
private static TaskCreationOptions AdjustFlags(TaskCreationOptions options, bool allowInliningContinuations)
{
return allowInliningContinuations
? (options & ~TaskCreationOptions.RunContinuationsAsynchronously)
: (options | TaskCreationOptions.RunContinuationsAsynchronously);
}
}
}