Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Build/BackEnd/Components/RequestBuilder/RequestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,7 @@ private sealed class DedicatedThreadsTaskScheduler : TaskScheduler
{
private readonly BlockingCollection<Task> _tasks = new BlockingCollection<Task>();
private int _availableThreads = 0;
private int _threadCount = 0;

protected override void QueueTask(Task task)
{
Expand Down Expand Up @@ -1538,6 +1539,7 @@ private void RequestThread()

private void InjectThread()
{
int currentThreadId = Interlocked.Increment(ref _threadCount);
var thread = new Thread(() =>
{
foreach (Task t in _tasks.GetConsumingEnumerable())
Expand All @@ -1547,6 +1549,7 @@ private void InjectThread()
}
});
thread.IsBackground = true;
thread.Name = $"MSBuild RequestBuilder Task Scheduler Thread {currentThreadId}";
thread.Start();
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/Shared/AwaitExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ private class OneSTAThreadPerTaskScheduler : TaskScheduler
/// </summary>
private readonly ConcurrentQueue<Task> _queuedTasks = new ConcurrentQueue<Task>();

/// <summary>
/// Counter for generating unique thread IDs.
/// </summary>
private int _threadCount = 0;

/// <summary>
/// Returns the list of queued tasks.
/// </summary>
Expand All @@ -172,6 +177,7 @@ protected override void QueueTask(Task task)
{
_queuedTasks.Enqueue(task);

int currentThreadId = Interlocked.Increment(ref _threadCount);
ParameterizedThreadStart threadStart = new ParameterizedThreadStart((_) =>
{
Task t;
Expand All @@ -182,6 +188,7 @@ protected override void QueueTask(Task task)
});

Thread thread = new Thread(threadStart);
thread.Name = $"MSBuild STA Task Scheduler Thread {currentThreadId}";
#if FEATURE_APARTMENT_STATE
thread.SetApartmentState(ApartmentState.STA);
#endif
Expand Down