forked from microsoft/vs-threading
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncSemaphore.cs
More file actions
361 lines (314 loc) · 15 KB
/
Copy pathAsyncSemaphore.cs
File metadata and controls
361 lines (314 loc) · 15 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// 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.Threading;
using System.Threading.Tasks;
/// <summary>
/// An asynchronous <see cref="SemaphoreSlim"/> like class with more convenient release syntax.
/// </summary>
/// <remarks>
/// <para>This semaphore guarantees FIFO ordering.</para>
/// <para>
/// This object does *not* need to be disposed of, as it does not hold unmanaged resources.
/// Disposing this object has no effect on current users of the semaphore, and they are allowed to release their hold on the semaphore without exception.
/// An <see cref="ObjectDisposedException"/> is thrown back at anyone asking to or waiting to enter the semaphore after <see cref="Dispose()"/> is called.
/// </para>
/// </remarks>
public class AsyncSemaphore : IDisposable
{
/// <summary>
/// A task that is faulted with an <see cref="ObjectDisposedException"/>.
/// </summary>
private static readonly Task<Releaser> DisposedReleaserTask = TplExtensions.FaultedTask<Releaser>(new ObjectDisposedException(typeof(AsyncSemaphore).FullName));
/// <summary>
/// A task that is canceled without a specific token.
/// </summary>
private static readonly Task<Releaser> CanceledReleaser = Task.FromCanceled<Releaser>(new CancellationToken(true));
/// <summary>
/// A task to return for any uncontested request for the lock.
/// </summary>
private readonly Task<Releaser> uncontestedReleaser;
/// <summary>
/// The sync object to lock on for mutable field access.
/// </summary>
private readonly object syncObject = new object();
/// <summary>
/// A queue of operations waiting to enter the semaphore.
/// </summary>
private readonly LinkedList<WaiterInfo> waiters = new LinkedList<WaiterInfo>();
/// <summary>
/// A pool of recycled nodes.
/// </summary>
private readonly Stack<LinkedListNode<WaiterInfo?>> nodePool = new Stack<LinkedListNode<WaiterInfo?>>();
/// <summary>
/// A value indicating whether this instance has been disposed.
/// </summary>
private bool disposed;
/// <summary>
/// Initializes a new instance of the <see cref="AsyncSemaphore"/> class.
/// </summary>
/// <param name="initialCount">The initial number of requests for the semaphore that can be granted concurrently.</param>
public AsyncSemaphore(int initialCount)
{
this.CurrentCount = initialCount;
this.uncontestedReleaser = Task.FromResult(new Releaser(this));
}
/// <summary>
/// Gets the number of openings that remain in the semaphore.
/// </summary>
public int CurrentCount { get; private set; }
/// <summary>
/// Requests access to the lock.
/// </summary>
/// <param name="cancellationToken">A token whose cancellation signals lost interest in the lock.</param>
/// <returns>
/// A task whose result is a releaser that should be disposed to release the lock.
/// This task may be canceled if <paramref name="cancellationToken"/> is signaled.
/// </returns>
/// <exception cref="OperationCanceledException">Thrown when <paramref name="cancellationToken"/> is canceled before semaphore access is granted.</exception>
/// <exception cref="ObjectDisposedException">Thrown when this semaphore is disposed before semaphore access is granted.</exception>
public Task<Releaser> EnterAsync(CancellationToken cancellationToken = default) => this.EnterAsync(Timeout.InfiniteTimeSpan, cancellationToken);
/// <summary>
/// Requests access to the lock.
/// </summary>
/// <param name="timeout">A timeout for waiting for the lock.</param>
/// <param name="cancellationToken">A token whose cancellation signals lost interest in the lock.</param>
/// <returns>
/// A task whose result is a releaser that should be disposed to release the lock.
/// This task may be canceled if <paramref name="cancellationToken"/> is signaled or <paramref name="timeout"/> expires.
/// </returns>
/// <exception cref="OperationCanceledException">Thrown when <paramref name="cancellationToken"/> is canceled or the <paramref name="timeout"/> expires before semaphore access is granted.</exception>
/// <exception cref="ObjectDisposedException">Thrown when this semaphore is disposed before semaphore access is granted.</exception>
public Task<Releaser> EnterAsync(TimeSpan timeout, CancellationToken cancellationToken = default)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<Releaser>(cancellationToken);
}
lock (this.syncObject)
{
if (this.disposed)
{
return DisposedReleaserTask;
}
if (this.CurrentCount > 0)
{
this.CurrentCount--;
return this.uncontestedReleaser;
}
else if (timeout == TimeSpan.Zero)
{
return CanceledReleaser;
}
else
{
WaiterInfo info = new WaiterInfo(this, cancellationToken);
LinkedListNode<WaiterInfo>? node = this.GetNode(info);
// Careful: consider that if the token was cancelled just now (after we checked it on entry to this method)
// or the timeout expires,
// then this Register method may *inline* the handler we give it, reversing the apparent order of execution with respect to
// the code that follows this Register call.
info.CancellationTokenRegistration = cancellationToken.Register(s => CancellationHandler(s), info);
if (timeout != Timeout.InfiniteTimeSpan)
{
info.TimerTokenSource = new Timer(s => CancellationHandler(s), info, checked((int)timeout.TotalMilliseconds), Timeout.Infinite);
}
// Only add to the queue if cancellation hasn't already happened.
if (!info.Trigger.Task.IsCanceled)
{
this.waiters.AddLast(node);
info.Node = node;
}
else
{
// Make sure we don't leak the Timer if cancellation happened before we created it.
info.Cleanup();
// Also recycle the unused node.
this.RecycleNode(node);
}
return info.Trigger.Task;
}
}
}
/// <summary>
/// Requests access to the lock.
/// </summary>
/// <param name="timeout">A timeout for waiting for the lock (in milliseconds).</param>
/// <param name="cancellationToken">A token whose cancellation signals lost interest in the lock.</param>
/// <returns>A task whose result is a releaser that should be disposed to release the lock.</returns>
/// <exception cref="OperationCanceledException">Thrown when <paramref name="cancellationToken"/> is canceled or the <paramref name="timeout"/> expires before semaphore access is granted.</exception>
/// <exception cref="ObjectDisposedException">Thrown when this semaphore is disposed before semaphore access is granted.</exception>
public Task<Releaser> EnterAsync(int timeout, CancellationToken cancellationToken = default) => this.EnterAsync(TimeSpan.FromMilliseconds(timeout), cancellationToken);
/// <summary>
/// Faults all pending semaphore waiters with <see cref="ObjectDisposedException"/>
/// and rejects all subsequent attempts to enter the semaphore with the same exception.
/// </summary>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Disposes managed and unmanaged resources held by this instance.
/// </summary>
/// <param name="disposing"><c>true</c> if <see cref="Dispose()"/> was called; <c>false</c> if the object is being finalized.</param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
List<WaiterInfo>? waitersCopy = null;
lock (this.syncObject)
{
this.disposed = true;
if (this.waiters.Count > 0)
{
waitersCopy = new List<WaiterInfo>(this.waiters.Count);
while (this.waiters.First is { } head)
{
head.Value.Trigger.TrySetException(new ObjectDisposedException(this.GetType().FullName));
waitersCopy.Add(head.Value);
this.waiters.RemoveFirst();
head.Value.Node = null;
}
}
this.nodePool.Clear();
}
if (waitersCopy is object)
{
foreach (WaiterInfo? waitInfo in waitersCopy)
{
waitInfo.Cleanup();
}
}
}
}
private static void CancellationHandler(object? state)
{
var waiterInfo = (WaiterInfo)state!;
// The party that manages to complete or cancel the task is responsible to remove it from the queue.
if (waiterInfo.Trigger.TrySetCanceled(waiterInfo.CancellationToken.IsCancellationRequested ? waiterInfo.CancellationToken : new CancellationToken(true)))
{
// If the node is in the queue, remove it.
// It might not have been added yet if cancellation was already requested by the time we called Register.
lock (waiterInfo.Owner.syncObject)
{
if (waiterInfo.Node is { } node)
{
waiterInfo.Owner.waiters.Remove(node);
waiterInfo.Owner.RecycleNode(node);
}
}
}
// Clear registration and references.
waiterInfo.Cleanup();
}
private void Release()
{
WaiterInfo? info = null;
lock (this.syncObject)
{
if (this.CurrentCount++ == 0)
{
// We loop because the First node may have been canceled.
while (this.waiters.First is { } head)
{
// Remove the head of the queue.
this.waiters.RemoveFirst();
info = head.Value;
this.RecycleNode(head);
if (info.Trigger.TrySetResult(new Releaser(this)))
{
// We successfully let someone enter the semaphore.
this.CurrentCount--;
// We've filled the one slot available in the semaphore. Stop looking for more.
break;
}
}
}
}
// Release memory related to cancellation handling.
info?.Cleanup();
}
private void RecycleNode(LinkedListNode<WaiterInfo> node)
{
Assumes.True(Monitor.IsEntered(this.syncObject));
node.Value.Node = null;
if (this.nodePool.Count < 10)
{
LinkedListNode<WaiterInfo?> nullableNode = node!;
nullableNode.Value = null;
this.nodePool.Push(nullableNode);
}
}
private LinkedListNode<WaiterInfo> GetNode(WaiterInfo info)
{
Assumes.True(Monitor.IsEntered(this.syncObject));
if (this.nodePool.Count > 0)
{
LinkedListNode<WaiterInfo?>? node = this.nodePool.Pop();
node.Value = info;
return node!;
}
return new LinkedListNode<WaiterInfo>(info);
}
/// <summary>
/// A value whose disposal triggers the release of a lock.
/// </summary>
public readonly struct Releaser : IDisposable
{
/// <summary>
/// The lock instance to release.
/// </summary>
private readonly AsyncSemaphore? toRelease;
/// <summary>
/// Initializes a new instance of the <see cref="Releaser"/> struct.
/// </summary>
/// <param name="toRelease">The lock instance to release on.</param>
internal Releaser(AsyncSemaphore toRelease)
{
this.toRelease = toRelease;
}
/// <summary>
/// Releases the lock.
/// </summary>
public void Dispose()
{
if (this.toRelease is object)
{
this.toRelease.Release();
}
}
}
private class WaiterInfo
{
internal WaiterInfo(AsyncSemaphore owner, CancellationToken cancellationToken)
{
this.Owner = owner;
this.CancellationToken = cancellationToken;
}
internal LinkedListNode<WaiterInfo>? Node { get; set; }
internal AsyncSemaphore Owner { get; }
internal TaskCompletionSource<Releaser> Trigger { get; } = new TaskCompletionSource<Releaser>(TaskCreationOptions.RunContinuationsAsynchronously);
internal CancellationToken CancellationToken { get; }
internal CancellationTokenRegistration CancellationTokenRegistration { private get; set; }
internal IDisposable? TimerTokenSource { private get; set; }
internal void Cleanup()
{
CancellationTokenRegistration cancellationTokenRegistration;
IDisposable? timerTokenSource;
lock (this)
{
cancellationTokenRegistration = this.CancellationTokenRegistration;
this.CancellationTokenRegistration = default;
timerTokenSource = this.TimerTokenSource;
this.TimerTokenSource = null;
}
cancellationTokenRegistration.Dispose();
timerTokenSource?.Dispose();
}
}
}
}