/
JoinableTaskCollection.cs
292 lines (257 loc) · 12.4 KB
/
JoinableTaskCollection.cs
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// 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.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
/// <summary>
/// A collection of incomplete <see cref="JoinableTask" /> objects.
/// </summary>
/// <remarks>
/// Any completed <see cref="JoinableTask" /> is automatically removed from the collection.
/// </remarks>
[DebuggerDisplay("JoinableTaskCollection: {displayName ?? \"(anonymous)\"}")]
public class JoinableTaskCollection : IJoinableTaskDependent, IEnumerable<JoinableTask>
{
/// <summary>
/// A value indicating whether joinable tasks are only removed when completed or removed as many times as they were added.
/// </summary>
private readonly bool refCountAddedJobs;
/// <summary>
/// A human-readable name that may appear in hang reports.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string? displayName;
/// <summary>
/// The <see cref="JoinableTaskDependencyGraph.JoinableTaskDependentData"/> to track dependencies between tasks.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private JoinableTaskDependencyGraph.JoinableTaskDependentData dependentData;
/// <summary>
/// An event that is set when the collection is empty (lazily initialized).
/// </summary>
private AsyncManualResetEvent? emptyEvent;
/// <summary>
/// Initializes a new instance of the <see cref="JoinableTaskCollection"/> class.
/// </summary>
/// <param name="context">The <see cref="JoinableTaskContext"/> instance to which this collection applies.</param>
/// <param name="refCountAddedJobs">
/// <c>true</c> if JoinableTask instances added to the collection multiple times should remain in the collection until they are
/// either removed the same number of times or until they are completed;
/// <c>false</c> causes the first Remove call for a JoinableTask to remove it from this collection regardless
/// how many times it had been added.</param>
public JoinableTaskCollection(JoinableTaskContext context, bool refCountAddedJobs = false)
{
Requires.NotNull(context, nameof(context));
this.Context = context;
this.refCountAddedJobs = refCountAddedJobs;
}
/// <summary>
/// Gets the <see cref="JoinableTaskContext"/> to which this collection belongs.
/// </summary>
public JoinableTaskContext Context { get; }
/// <summary>
/// Gets or sets a human-readable name that may appear in hang reports.
/// </summary>
/// <remarks>
/// This property should *not* be set to a value that may disclose
/// personally identifiable information or other confidential data
/// since this value may be included in hang reports sent to a third party.
/// </remarks>
public string? DisplayName
{
get { return this.displayName; }
set { this.displayName = value; }
}
/// <summary>
/// Gets JoinableTaskContext for <see cref="JoinableTaskDependencyGraph.JoinableTaskDependentData"/> to access locks.
/// </summary>
JoinableTaskContext IJoinableTaskDependent.JoinableTaskContext => this.Context;
/// <summary>
/// Gets a value indicating whether we need count reference for child dependent nodes.
/// </summary>
bool IJoinableTaskDependent.NeedRefCountChildDependencies => this.refCountAddedJobs;
ref JoinableTaskDependencyGraph.JoinableTaskDependentData IJoinableTaskDependent.GetJoinableTaskDependentData() => ref this.dependentData;
/// <summary>
/// Adds the specified <see cref="JoinableTask" /> to this collection.
/// </summary>
/// <param name="joinableTask">The <see cref="JoinableTask" /> to add to the collection.</param>
/// <remarks>
/// As the collection only stores *incomplete* <see cref="JoinableTask" /> instances,
/// if the <paramref name="joinableTask" /> is already completed, it will not be added to the collection and this method will simply return.
/// Any <see cref="JoinableTask" /> instances added to the collection will be automatically removed upon completion.
/// </remarks>
public void Add(JoinableTask joinableTask)
{
Requires.NotNull(joinableTask, nameof(joinableTask));
if (joinableTask.Factory.Context != this.Context)
{
Requires.Argument(false, "joinableTask", Strings.JoinableTaskContextAndCollectionMismatch);
}
JoinableTaskDependencyGraph.AddDependency(this, joinableTask);
}
/// <summary>
/// Removes the specified <see cref="JoinableTask" /> from this collection,
/// or decrements the ref count if this collection tracks that.
/// </summary>
/// <param name="joinableTask">The <see cref="JoinableTask" /> to remove.</param>
/// <remarks>
/// Completed <see cref="JoinableTask" /> instances are automatically removed from the collection.
/// Calling this method to remove them is not necessary.
/// </remarks>
public void Remove(JoinableTask joinableTask)
{
Requires.NotNull(joinableTask, nameof(joinableTask));
JoinableTaskDependencyGraph.RemoveDependency(this, joinableTask);
}
/// <summary>
/// Shares access to the main thread that the caller's JoinableTask may have (if any) with all
/// JoinableTask instances in this collection until the returned value is disposed.
/// </summary>
/// <returns>A value to dispose of to revert the join.</returns>
/// <remarks>
/// Calling this method when the caller is not executing within a JoinableTask safely no-ops.
/// </remarks>
public JoinRelease Join()
{
JoinableTask? ambientJob = this.Context.AmbientTask;
if (ambientJob is null)
{
// The caller isn't running in the context of a joinable task, so there is nothing to join with this collection.
return default(JoinRelease);
}
return JoinableTaskDependencyGraph.AddDependency(ambientJob, this);
}
/// <summary>
/// Joins the caller's context to this collection till the collection is empty.
/// </summary>
/// <returns>A task that completes when this collection is empty.</returns>
public Task JoinTillEmptyAsync() => this.JoinTillEmptyAsync(CancellationToken.None);
/// <summary>
/// Joins the caller's context to this collection till the collection is empty.
/// </summary>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>A task that completes when this collection is empty, or is canceled when <paramref name="cancellationToken"/> is canceled.</returns>
public async Task JoinTillEmptyAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
if (this.emptyEvent is null)
{
// We need a read lock to protect against the emptiness of this collection changing
// while we're setting the initial set state of the new event.
using (this.Context.NoMessagePumpSynchronizationContext.Apply())
{
lock (this.Context.SyncContextLock)
{
if (this.emptyEvent is null)
{
this.emptyEvent = new AsyncManualResetEvent(JoinableTaskDependencyGraph.HasNoChildDependentNode(this));
}
}
}
}
using (this.Join())
{
await this.emptyEvent.WaitAsync(cancellationToken).ConfigureAwaitRunInline();
}
}
/// <summary>
/// Checks whether the specified joinable task is a member of this collection.
/// </summary>
public bool Contains(JoinableTask joinableTask)
{
Requires.NotNull(joinableTask, nameof(joinableTask));
using (this.Context.NoMessagePumpSynchronizationContext.Apply())
{
lock (this.Context.SyncContextLock)
{
return JoinableTaskDependencyGraph.HasDirectDependency(this, joinableTask);
}
}
}
/// <summary>
/// Enumerates the tasks in this collection.
/// </summary>
public IEnumerator<JoinableTask> GetEnumerator()
{
using (this.Context.NoMessagePumpSynchronizationContext.Apply())
{
var joinables = new List<JoinableTask>();
lock (this.Context.SyncContextLock)
{
foreach (IJoinableTaskDependent? item in JoinableTaskDependencyGraph.GetDirectDependentNodes(this))
{
if (item is JoinableTask joinableTask)
{
joinables.Add(joinableTask);
}
}
}
return joinables.GetEnumerator();
}
}
/// <summary>
/// Enumerates the tasks in this collection.
/// </summary>
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
void IJoinableTaskDependent.OnAddedToDependency(IJoinableTaskDependent parent)
{
}
void IJoinableTaskDependent.OnRemovedFromDependency(IJoinableTaskDependent parentNode)
{
}
void IJoinableTaskDependent.OnDependencyAdded(IJoinableTaskDependent joinChild)
{
if (this.emptyEvent is object && joinChild is JoinableTask)
{
this.emptyEvent.Reset();
}
}
void IJoinableTaskDependent.OnDependencyRemoved(IJoinableTaskDependent joinChild)
{
if (this.emptyEvent is object && JoinableTaskDependencyGraph.HasNoChildDependentNode(this))
{
this.emptyEvent.Set();
}
}
/// <summary>
/// A value whose disposal cancels a <see cref="Join"/> operation.
/// </summary>
public struct JoinRelease : IDisposable
{
private IJoinableTaskDependent? parentDependencyNode;
private IJoinableTaskDependent? childDependencyNode;
/// <summary>
/// Initializes a new instance of the <see cref="JoinRelease"/> struct.
/// </summary>
/// <param name="parentDependencyNode">The Main thread controlling SingleThreadSynchronizationContext to use to accelerate execution of Main thread bound work.</param>
/// <param name="childDependencyNode">The instance that created this value.</param>
internal JoinRelease(IJoinableTaskDependent parentDependencyNode, IJoinableTaskDependent childDependencyNode)
{
Requires.NotNull(parentDependencyNode, nameof(parentDependencyNode));
Requires.NotNull(childDependencyNode, nameof(childDependencyNode));
this.parentDependencyNode = parentDependencyNode;
this.childDependencyNode = childDependencyNode;
}
/// <summary>
/// Cancels the <see cref="Join"/> operation.
/// </summary>
public void Dispose()
{
if (this.parentDependencyNode is object)
{
RoslynDebug.Assert(this.childDependencyNode is object, $"{nameof(this.childDependencyNode)} can only be null when {nameof(this.parentDependencyNode)} is null.");
JoinableTaskDependencyGraph.RemoveDependency(this.parentDependencyNode, this.childDependencyNode);
this.parentDependencyNode = null;
}
this.childDependencyNode = null;
}
}
}
}