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

Modify thread pool thread counting to be a bit more defensive #70478

Merged
merged 2 commits into from
Jun 9, 2022
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 @@ -31,61 +31,52 @@ private void SetInt16Value(short value, byte shift) =>
/// </summary>
public short NumProcessingWork
{
get => GetInt16Value(NumProcessingWorkShift);
get
{
short value = GetInt16Value(NumProcessingWorkShift);
Debug.Assert(value >= 0);
return value;
}
set
{
Debug.Assert(value >= 0);
SetInt16Value(value, NumProcessingWorkShift);
SetInt16Value(Math.Max((short)0, value), NumProcessingWorkShift);
}
}

public void SubtractNumProcessingWork(short value)
{
Debug.Assert(value >= 0);
Debug.Assert(value <= NumProcessingWork);

_data -= (ulong)(ushort)value << NumProcessingWorkShift;
}

public void InterlockedDecrementNumProcessingWork()
{
Debug.Assert(NumProcessingWorkShift == 0);

ThreadCounts counts = new ThreadCounts(Interlocked.Decrement(ref _data));
Debug.Assert(counts.NumProcessingWork >= 0);
}

/// <summary>
/// Number of thread pool threads that currently exist.
/// </summary>
public short NumExistingThreads
{
get => GetInt16Value(NumExistingThreadsShift);
get
{
short value = GetInt16Value(NumExistingThreadsShift);
Debug.Assert(value >= 0);
return value;
}
set
{
Debug.Assert(value >= 0);
SetInt16Value(value, NumExistingThreadsShift);
SetInt16Value(Math.Max((short)0, value), NumExistingThreadsShift);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be changed to ushort to avoid the Math.Max in multiple places, or is that change going to be too disruptive?

Copy link
Member Author

@kouvel kouvel Jun 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing to ushort wouldn't prevent an underflow, a check would still need to ensure that it does not cross zero and wrap around. It may have other undesirable effects in odd cases, probably not worth it.

}
}

public void SubtractNumExistingThreads(short value)
{
Debug.Assert(value >= 0);
Debug.Assert(value <= NumExistingThreads);

_data -= (ulong)(ushort)value << NumExistingThreadsShift;
}

/// <summary>
/// Max possible thread pool threads we want to have.
/// </summary>
public short NumThreadsGoal
{
get => GetInt16Value(NumThreadsGoalShift);
get
{
short value = GetInt16Value(NumThreadsGoalShift);
Debug.Assert(value > 0);
return value;
}
set
{
Debug.Assert(value > 0);
SetInt16Value(value, NumThreadsGoalShift);
SetInt16Value(Math.Max((short)1, value), NumThreadsGoalShift);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,14 @@ private static void WorkerThreadStart()
// Since this thread is currently registered as an existing thread, if more work comes in meanwhile,
// this thread would be expected to satisfy the new work. Ensure that NumExistingThreads is not
// decreased below NumProcessingWork, as that would be indicative of such a case.
short numExistingThreads = counts.NumExistingThreads;
if (numExistingThreads <= counts.NumProcessingWork)
if (counts.NumExistingThreads <= counts.NumProcessingWork)
{
// In this case, enough work came in that this thread should not time out and should go back to work.
break;
}

ThreadCounts newCounts = counts;
newCounts.SubtractNumExistingThreads(1);
short newNumExistingThreads = (short)(numExistingThreads - 1);
short newNumExistingThreads = --newCounts.NumExistingThreads;
short newNumThreadsGoal =
Math.Max(
threadPoolInstance.MinThreadsGoal,
Expand Down Expand Up @@ -173,7 +171,23 @@ private static void WorkerThreadStart()
/// </summary>
private static void RemoveWorkingWorker(PortableThreadPool threadPoolInstance)
{
threadPoolInstance._separated.counts.InterlockedDecrementNumProcessingWork();
// A compare-exchange loop is used instead of Interlocked.Decrement or Interlocked.Add to defensively prevent
// NumProcessingWork from underflowing. See the setter for NumProcessingWork.
ThreadCounts counts = threadPoolInstance._separated.counts;
kouvel marked this conversation as resolved.
Show resolved Hide resolved
while (true)
{
ThreadCounts newCounts = counts;
newCounts.NumProcessingWork--;

ThreadCounts countsBeforeUpdate =
threadPoolInstance._separated.counts.InterlockedCompareExchange(newCounts, counts);
if (countsBeforeUpdate == counts)
{
break;
}

counts = countsBeforeUpdate;
}

// It's possible that we decided we had thread requests just before a request came in,
// but reduced the worker count *after* the request came in. In this case, we might
Expand Down Expand Up @@ -235,8 +249,8 @@ internal static void MaybeAddWorkingWorker(PortableThreadPool threadPoolInstance
while (true)
{
ThreadCounts newCounts = counts;
newCounts.SubtractNumProcessingWork((short)toCreate);
newCounts.SubtractNumExistingThreads((short)toCreate);
newCounts.NumProcessingWork -= (short)toCreate;
newCounts.NumExistingThreads -= (short)toCreate;

ThreadCounts oldCounts = threadPoolInstance._separated.counts.InterlockedCompareExchange(newCounts, counts);
if (oldCounts == counts)
Expand Down Expand Up @@ -273,7 +287,7 @@ internal static bool ShouldStopProcessingWorkNow(PortableThreadPool threadPoolIn
}

ThreadCounts newCounts = counts;
newCounts.SubtractNumProcessingWork(1);
newCounts.NumProcessingWork--;

ThreadCounts oldCounts = threadPoolInstance._separated.counts.InterlockedCompareExchange(newCounts, counts);

Expand Down