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

Stop counting work items from ThreadPoolTypedWorkItemQueue for ThreadPool.CompletedWorkItemCount #106854

Merged
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 @@ -1393,7 +1393,11 @@ void IThreadPoolWorkItem.Execute()
currentThread.ResetThreadPoolThread();
}

ThreadInt64PersistentCounter.Add(tl.threadLocalCompletionCountObject!, completedCount);
eduardo-vp marked this conversation as resolved.
Show resolved Hide resolved
// Discount a work item here to avoid counting most of the queue processing work items
if (completedCount > 1)
{
ThreadInt64PersistentCounter.Add(tl.threadLocalCompletionCountObject!, completedCount - 1);
}
}
}

Expand Down
27 changes: 27 additions & 0 deletions src/libraries/System.Threading.ThreadPool/tests/ThreadPoolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,33 @@ static async Task RunAsyncIOTest()
}, ioCompletionPortCount.ToString()).Dispose();
}


[ConditionalFact(nameof(IsThreadingAndRemoteExecutorSupported))]
[PlatformSpecific(TestPlatforms.Windows)]
public static unsafe void ThreadPoolCompletedWorkItemCountTest()
{
// Run in a separate process to test in a clean thread pool environment such that we don't count external work items
RemoteExecutor.Invoke(() =>
{
using var manualResetEvent = new ManualResetEventSlim(false);

var overlapped = new Overlapped();
NativeOverlapped* nativeOverlapped = overlapped.Pack((errorCode, numBytes, innerNativeOverlapped) =>
{
Overlapped.Free(innerNativeOverlapped);
manualResetEvent.Set();
}, null);

ThreadPool.UnsafeQueueNativeOverlapped(nativeOverlapped);
manualResetEvent.Wait();

// Allow work item(s) to be marked as completed during this time, should be only one
ThreadTestHelpers.WaitForCondition(() => ThreadPool.CompletedWorkItemCount == 1);
Thread.Sleep(50);
Assert.Equal(1, ThreadPool.CompletedWorkItemCount);
}).Dispose();
}

public static bool IsThreadingAndRemoteExecutorSupported =>
PlatformDetection.IsThreadingSupported && RemoteExecutor.IsSupported;

Expand Down
Loading