Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ThreadPool.SetMinThreads and SetMaxThreads to return false when they don't override the preconfigured value #57699

Merged
merged 5 commits into from
Aug 30, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ private struct CacheLineSeparated

private PortableThreadPool()
{
_minThreads = ForcedMinWorkerThreads > 0 ? ForcedMinWorkerThreads : (short)Environment.ProcessorCount;
_minThreads = HasForcedMinThreads ? ForcedMinWorkerThreads : (short)Environment.ProcessorCount;
if (_minThreads > MaxPossibleThreadCount)
{
_minThreads = MaxPossibleThreadCount;
}

_maxThreads = ForcedMaxWorkerThreads > 0 ? ForcedMaxWorkerThreads : DefaultMaxWorkerThreadCount;
_maxThreads = HasForcedMaxThreads ? ForcedMaxWorkerThreads : DefaultMaxWorkerThreadCount;
if (_maxThreads > MaxPossibleThreadCount)
{
_maxThreads = MaxPossibleThreadCount;
Expand All @@ -104,6 +104,11 @@ private PortableThreadPool()
_separated.counts.NumThreadsGoal = _minThreads;
}

private static bool HasForcedMinThreads =>
ForcedMinWorkerThreads > 0 && (ForcedMaxWorkerThreads <= 0 || ForcedMinWorkerThreads <= ForcedMaxWorkerThreads);
private static bool HasForcedMaxThreads =>
ForcedMaxWorkerThreads > 0 && (ForcedMinWorkerThreads <= 0 || ForcedMinWorkerThreads <= ForcedMaxWorkerThreads);

public bool SetMinThreads(int workerThreads, int ioCompletionThreads)
{
if (workerThreads < 0 || ioCompletionThreads < 0)
Expand All @@ -124,9 +129,9 @@ public bool SetMinThreads(int workerThreads, int ioCompletionThreads)

ThreadPool.SetMinIOCompletionThreads(ioCompletionThreads);

if (ForcedMinWorkerThreads != 0)
if (HasForcedMinThreads)
{
return true;
return workerThreads == ForcedMinWorkerThreads;
}

short newMinThreads = (short)Math.Max(1, Math.Min(workerThreads, MaxPossibleThreadCount));
Expand Down Expand Up @@ -184,9 +189,9 @@ public bool SetMaxThreads(int workerThreads, int ioCompletionThreads)

ThreadPool.SetMaxIOCompletionThreads(ioCompletionThreads);

if (ForcedMaxWorkerThreads != 0)
if (HasForcedMaxThreads)
{
return true;
return workerThreads == ForcedMaxWorkerThreads;
}

short newMaxThreads = (short)Math.Min(workerThreads, MaxPossibleThreadCount);
Expand Down
18 changes: 18 additions & 0 deletions src/libraries/System.Threading.ThreadPool/tests/ThreadPoolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,24 @@ public static void SetMinMaxThreadsTest_ChangedInDotNetCore()
VerifyMinThreads(minw, minc);
}
}).Dispose();

// Verify that SetMinThreads() and SetMaxThreads() return false when trying to set a different value from what is
// configured through config
var options = new RemoteInvokeOptions();
options.RuntimeConfigurationOptions["System.Threading.ThreadPool.MinThreads"] = "1";
options.RuntimeConfigurationOptions["System.Threading.ThreadPool.MaxThreads"] = "2";
RemoteExecutor.Invoke(() =>
{
int w, c;
ThreadPool.GetMinThreads(out w, out c);
Assert.Equal(1, w);
ThreadPool.GetMaxThreads(out w, out c);
Assert.Equal(2, w);
Assert.True(ThreadPool.SetMinThreads(1, 1));
Assert.True(ThreadPool.SetMaxThreads(2, 1));
Assert.False(ThreadPool.SetMinThreads(2, 1));
Assert.False(ThreadPool.SetMaxThreads(1, 1));
}, options).Dispose();
}

private static void VerifyMinThreads(int expectedMinw, int expectedMinc)
Expand Down