forked from microsoft/vs-threading
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncManualResetEvent.cs
More file actions
243 lines (222 loc) · 9.62 KB
/
Copy pathAsyncManualResetEvent.cs
File metadata and controls
243 lines (222 loc) · 9.62 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
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
// 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.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
/// <summary>
/// A flavor of <see cref="ManualResetEvent"/> that can be asynchronously awaited on.
/// </summary>
[DebuggerDisplay("Signaled: {IsSet}")]
public class AsyncManualResetEvent
{
/// <summary>
/// Whether the task completion source should allow executing continuations synchronously.
/// </summary>
private readonly bool allowInliningAwaiters;
/// <summary>
/// The object to lock when accessing fields.
/// </summary>
private readonly object syncObject = new object();
/// <summary>
/// The source of the task to return from <see cref="WaitAsync()"/>.
/// </summary>
/// <devremarks>
/// This should not need the volatile modifier because it is
/// always accessed within a lock.
/// </devremarks>
private TaskCompletionSourceWithoutInlining<EmptyStruct> taskCompletionSource;
/// <summary>
/// A flag indicating whether the event is signaled.
/// When this is set to true, it's possible that
/// <see cref="taskCompletionSource"/>.Task.IsCompleted is still false
/// if the completion has been scheduled asynchronously.
/// Thus, this field should be the definitive answer as to whether
/// the event is signaled because it is synchronously updated.
/// </summary>
/// <devremarks>
/// This should not need the volatile modifier because it is
/// always accessed within a lock.
/// </devremarks>
private bool isSet;
/// <summary>
/// Initializes a new instance of the <see cref="AsyncManualResetEvent"/> class.
/// </summary>
/// <param name="initialState">A value indicating whether the event should be initially signaled.</param>
/// <param name="allowInliningAwaiters">
/// A value indicating whether to allow <see cref="WaitAsync()"/> callers' continuations to execute
/// on the thread that calls <see cref="SetAsync()"/> before the call returns.
/// <see cref="SetAsync()"/> callers should not hold private locks if this value is <c>true</c> to avoid deadlocks.
/// When <c>false</c>, the task returned from <see cref="WaitAsync()"/> may not have fully transitioned to
/// its completed state by the time <see cref="SetAsync()"/> returns to its caller.
/// </param>
public AsyncManualResetEvent(bool initialState = false, bool allowInliningAwaiters = false)
{
this.allowInliningAwaiters = allowInliningAwaiters;
this.taskCompletionSource = this.CreateTaskSource();
this.isSet = initialState;
if (initialState)
{
this.taskCompletionSource.SetResult(EmptyStruct.Instance);
}
}
/// <summary>
/// Gets a value indicating whether the event is currently in a signaled state.
/// </summary>
public bool IsSet
{
get
{
lock (this.syncObject)
{
return this.isSet;
}
}
}
/// <summary>
/// Returns a task that will be completed when this event is set.
/// </summary>
public Task WaitAsync()
{
lock (this.syncObject)
{
return this.taskCompletionSource.Task;
}
}
/// <summary>
/// Returns a task that will be completed when this event is set.
/// </summary>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>A task that completes when the event is set, or cancels with the <paramref name="cancellationToken"/>.</returns>
public Task WaitAsync(CancellationToken cancellationToken) => this.WaitAsync().WithCancellation(cancellationToken);
/// <summary>
/// Sets this event to unblock callers of <see cref="WaitAsync()"/>.
/// </summary>
/// <returns>A task that completes when the signal has been set.</returns>
/// <remarks>
/// <para>
/// On .NET versions prior to 4.6:
/// This method may return before the signal set has propagated (so <see cref="IsSet"/> may return <c>false</c> for a bit more if called immediately).
/// The returned task completes when the signal has definitely been set.
/// </para>
/// <para>
/// On .NET 4.6 and later:
/// This method is not asynchronous. The returned Task is always completed.
/// </para>
/// </remarks>
[Obsolete("Use Set() instead."), EditorBrowsable(EditorBrowsableState.Never)]
public Task SetAsync()
{
TaskCompletionSourceWithoutInlining<EmptyStruct>? tcs = null;
bool transitionRequired = false;
lock (this.syncObject)
{
transitionRequired = !this.isSet;
tcs = this.taskCompletionSource;
this.isSet = true;
}
// Snap the Task that is exposed to the outside so we return that one.
// Once we complete the TaskCompletionSourceWithoutInlinining's task,
// the Task property will return the inner Task.
// SetAsync should return the same Task that WaitAsync callers would have observed previously.
Task result = tcs.Task;
if (transitionRequired)
{
tcs.TrySetResult(default(EmptyStruct));
}
return result;
}
/// <summary>
/// Sets this event to unblock callers of <see cref="WaitAsync()"/>.
/// </summary>
public void Set()
{
#pragma warning disable CS0618 // Type or member is obsolete
this.SetAsync();
#pragma warning restore CS0618 // Type or member is obsolete
}
/// <summary>
/// Resets this event to a state that will block callers of <see cref="WaitAsync()"/>.
/// </summary>
public void Reset()
{
lock (this.syncObject)
{
if (this.isSet)
{
this.taskCompletionSource = this.CreateTaskSource();
this.isSet = false;
}
}
}
/// <summary>
/// Sets and immediately resets this event, allowing all current waiters to unblock.
/// </summary>
/// <returns>A task that completes when the signal has been set.</returns>
/// <remarks>
/// <para>
/// On .NET versions prior to 4.6:
/// This method may return before the signal set has propagated (so <see cref="IsSet"/> may return <c>false</c> for a bit more if called immediately).
/// The returned task completes when the signal has definitely been set.
/// </para>
/// <para>
/// On .NET 4.6 and later:
/// This method is not asynchronous. The returned Task is always completed.
/// </para>
/// </remarks>
[Obsolete("Use PulseAll() instead."), EditorBrowsable(EditorBrowsableState.Never)]
public Task PulseAllAsync()
{
TaskCompletionSourceWithoutInlining<EmptyStruct>? tcs = null;
lock (this.syncObject)
{
// Atomically replace the completion source with a new, uncompleted source
// while capturing the previous one so we can complete it.
// This ensures that we don't leave a gap in time where WaitAsync() will
// continue to return completed Tasks due to a Pulse method which should
// execute instantaneously.
tcs = this.taskCompletionSource;
this.taskCompletionSource = this.CreateTaskSource();
this.isSet = false;
}
// Snap the Task that is exposed to the outside so we return that one.
// Once we complete the TaskCompletionSourceWithoutInlinining's task,
// the Task property will return the inner Task.
// PulseAllAsync should return the same Task that WaitAsync callers would have observed previously.
Task result = tcs.Task;
tcs.TrySetResult(default(EmptyStruct));
return result;
}
/// <summary>
/// Sets and immediately resets this event, allowing all current waiters to unblock.
/// </summary>
public void PulseAll()
{
#pragma warning disable CS0618 // Type or member is obsolete
this.PulseAllAsync();
#pragma warning restore CS0618 // Type or member is obsolete
}
/// <summary>
/// Gets an awaiter that completes when this event is signaled.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public TaskAwaiter GetAwaiter()
{
return this.WaitAsync().GetAwaiter();
}
/// <summary>
/// Creates a new TaskCompletionSource to represent an unset event.
/// </summary>
private TaskCompletionSourceWithoutInlining<EmptyStruct> CreateTaskSource()
{
return new TaskCompletionSourceWithoutInlining<EmptyStruct>(this.allowInliningAwaiters);
}
}
}