Repository navigation
Expand file tree
/
Copy pathParallelWorkSet.cs
More file actions
210 lines (175 loc) · 6.82 KB
/
Copy pathParallelWorkSet.cs
File metadata and controls
210 lines (175 loc) · 6.82 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
#nullable disable
namespace Microsoft.Build.Graph
{
/// <summary>
/// Provides deduping of expensive work by a key, or modeling of a set of deduped work that
/// can be awaited as a unit. Completed results are kept in the collection for reuse.
/// </summary>
internal class ParallelWorkSet<TKey, TResult>
{
private readonly CancellationToken _cancellationToken;
/// <summary>
/// Number of workers to process work items.
/// </summary>
private readonly int _degreeOfParallelism;
private readonly ConcurrentDictionary<TKey, Lazy<TResult>> _inProgressOrCompletedWork;
private bool _isSchedulingCompleted;
private long _pendingCount;
private readonly ConcurrentQueue<Lazy<TResult>> _queue =
new ConcurrentQueue<Lazy<TResult>>();
private readonly SemaphoreSlim _semaphore;
private readonly List<Task> _tasks;
private readonly List<Exception> _exceptions = new List<Exception>(0);
/// <summary>
/// Retrieves all completed work items.
/// </summary>
internal Dictionary<TKey, TResult> CompletedWork
{
get
{
var completedWork = new Dictionary<TKey, TResult>(_inProgressOrCompletedWork.Count);
foreach (KeyValuePair<TKey, Lazy<TResult>> kvp in _inProgressOrCompletedWork)
{
Lazy<TResult> workItem = kvp.Value;
if (workItem.IsValueCreated)
{
completedWork[kvp.Key] = workItem.Value;
}
}
if (_exceptions.Count > 0)
{
throw new AggregateException(_exceptions);
}
return completedWork;
}
}
/// <summary>
/// Checks if the work set has been marked as completed.
/// </summary>
internal bool IsCompleted
{
get => Volatile.Read(ref _isSchedulingCompleted);
private set => Volatile.Write(ref _isSchedulingCompleted, value);
}
internal ParallelWorkSet(int degreeOfParallelism, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
{
if (degreeOfParallelism < 0)
{
throw new ArgumentException("Degree of parallelism must be a positive integer.");
}
_cancellationToken = cancellationToken;
_degreeOfParallelism = degreeOfParallelism;
_inProgressOrCompletedWork = new ConcurrentDictionary<TKey, Lazy<TResult>>(comparer);
// Semaphore count is 0 to ensure that all the tasks are blocked unless new data is scheduled.
_semaphore = new SemaphoreSlim(0, int.MaxValue);
_tasks = new List<Task>(degreeOfParallelism);
for (int i = 0; i < degreeOfParallelism; i++)
{
_tasks.Add(CreateProcessorItemTask());
}
}
/// <summary>
/// Enqueues a work item to the work set.
/// </summary>
/// <param name="key"></param>
/// <param name="workFunc"></param>
internal void AddWork(TKey key, Func<TResult> workFunc)
{
if (IsCompleted)
{
throw new InvalidOperationException("Cannot add new work after work set is marked as completed.");
}
var workItem = new Lazy<TResult>(workFunc);
if (!_inProgressOrCompletedWork.TryAdd(key, workItem))
{
return;
}
Interlocked.Increment(ref _pendingCount);
// NOTE: Enqueue MUST happen before releasing the semaphore
// to ensure WaitAsync below never returns when there is not
// a corresponding item in the queue to be dequeued. The only
// exception is on completion of all items.
_queue.Enqueue(workItem);
_semaphore.Release();
}
/// <summary>
/// Assists processing items until all the items added to the queue are processed, completes the work set, and
/// propagates any exceptions thrown by workers.
/// </summary>
internal void WaitForAllWorkAndComplete()
{
if (IsCompleted)
{
return;
}
while (!_cancellationToken.IsCancellationRequested && Interlocked.Read(ref _pendingCount) > 0)
{
ExecuteWorkItem();
}
IsCompleted = true;
// Release one thread that will release all the threads when all the elements are processed.
_semaphore.Release();
Task.WaitAll(_tasks.ToArray());
if (_exceptions.Count > 0)
{
throw new AggregateException(_exceptions);
}
}
private Task CreateProcessorItemTask()
{
return Task.Run(
async () =>
{
bool shouldStopAllWorkers = false;
while (!shouldStopAllWorkers)
{
await _semaphore.WaitAsync(_cancellationToken);
try
{
ExecuteWorkItem();
}
finally
{
shouldStopAllWorkers = Interlocked.Read(ref _pendingCount) == 0 && IsCompleted;
if (shouldStopAllWorkers)
{
// Ensure all tasks are unblocked and can gracefully
// finish since there are at most degreeOfParallelism - 1 tasks
// waiting at this point
_semaphore.Release(_degreeOfParallelism);
}
}
}
},
_cancellationToken);
}
private void ExecuteWorkItem()
{
if (_queue.TryDequeue(out Lazy<TResult> workItem))
{
try
{
_ = workItem.Value;
}
catch (Exception ex)
{
lock (_exceptions)
{
_exceptions.Add(ex);
}
}
finally
{
Interlocked.Decrement(ref _pendingCount);
}
}
}
}
}