forked from microsoft/vs-threading
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncCountdownEvent.cs
More file actions
114 lines (106 loc) · 3.72 KB
/
Copy pathAsyncCountdownEvent.cs
File metadata and controls
114 lines (106 loc) · 3.72 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
// 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.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
/// <summary>
/// An asynchronous style countdown event.
/// </summary>
public class AsyncCountdownEvent
{
/// <summary>
/// The manual reset event we use to signal all awaiters.
/// </summary>
private readonly AsyncManualResetEvent manualEvent;
/// <summary>
/// The remaining number of signals required before we can unblock waiters.
/// </summary>
private int remainingCount;
/// <summary>
/// Initializes a new instance of the <see cref="AsyncCountdownEvent"/> class.
/// </summary>
/// <param name="initialCount">The number of signals required to unblock awaiters.</param>
public AsyncCountdownEvent(int initialCount)
{
Requires.Range(initialCount >= 0, "initialCount");
this.manualEvent = new AsyncManualResetEvent(initialCount == 0);
this.remainingCount = initialCount;
}
/// <summary>
/// Returns an awaitable that executes the continuation when the countdown reaches zero.
/// </summary>
/// <returns>An awaitable.</returns>
public Task WaitAsync()
{
return this.manualEvent.WaitAsync();
}
/// <summary>
/// Decrements the counter by one.
/// </summary>
/// <returns>
/// A task that completes when the signal has been set if this call causes the count to reach zero.
/// If the count is not zero, a completed task is returned.
/// </returns>
/// <remarks>
/// <para>
/// On .NET versions prior to 4.6:
/// This method may return before the signal set has propagated.
/// 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 Signal() instead."), EditorBrowsable(EditorBrowsableState.Never)]
public async Task SignalAsync()
{
int newCount = Interlocked.Decrement(ref this.remainingCount);
if (newCount == 0)
{
await this.manualEvent.SetAsync().ConfigureAwait(false);
}
else if (newCount < 0)
{
throw new InvalidOperationException();
}
}
/// <summary>
/// Decrements the counter by one.
/// </summary>
public void Signal()
{
int newCount = Interlocked.Decrement(ref this.remainingCount);
if (newCount == 0)
{
this.manualEvent.Set();
}
else if (newCount < 0)
{
throw new InvalidOperationException();
}
}
/// <summary>
/// Decrements the counter by one and returns an awaitable that executes the continuation when the countdown reaches zero.
/// </summary>
/// <returns>An awaitable.</returns>
public Task SignalAndWaitAsync()
{
try
{
this.Signal();
return this.WaitAsync();
}
catch (Exception ex)
{
return Task.FromException(ex);
}
}
}
}