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

[7.0] Check for pending IO in the portable thread pool's worker threads #82246

Merged
merged 2 commits into from
Mar 9, 2023
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 @@ -73,5 +73,9 @@ internal enum ThreadPriority : int
[LibraryImport(Libraries.Kernel32)]
[return:MarshalAs(UnmanagedType.Bool)]
internal static partial bool SetThreadPriority(SafeWaitHandle hThread, int nPriority);

[LibraryImport(Libraries.Kernel32, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static partial bool GetThreadIOPendingFlag(nint hThread, out BOOL lpIOIsPending);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1763,6 +1763,9 @@
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.SystemTimeToFileTime.cs">
<Link>Common\Interop\Windows\Kernel32\Interop.SystemTimeToFileTime.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.Threading.cs">
<Link>Common\Interop\Windows\Kernel32\Interop.Threading.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.TimeZone.cs">
<Link>Common\Interop\Windows\Kernel32\Interop.TimeZone.cs</Link>
</Compile>
Expand Down Expand Up @@ -2380,8 +2383,8 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\PortableThreadPool.WaitThread.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\PortableThreadPool.WorkerThread.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\PortableThreadPool.WorkerTracking.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\PortableThreadPool.CpuUtilizationReader.Unix.cs" Condition="'$(TargetsUnix)' == 'true' or '$(TargetsBrowser)' == 'true'" />
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\PortableThreadPool.CpuUtilizationReader.Windows.cs" Condition="'$(TargetsWindows)' == 'true'" />
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\PortableThreadPool.Unix.cs" Condition="'$(TargetsUnix)' == 'true' or '$(TargetsBrowser)' == 'true'" />
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\PortableThreadPool.Windows.cs" Condition="'$(TargetsWindows)' == 'true'" />
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\LowLevelLifoSemaphore.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\LowLevelLifoSemaphore.Windows.cs" Condition="'$(TargetsWindows)' == 'true'" />
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\PreAllocatedOverlapped.cs" Condition="'$(TargetsBrowser)' != 'true' or '$(FeatureWasmThreads)' == 'true'" />
Expand Down Expand Up @@ -2417,9 +2420,6 @@
<ItemGroup Condition="'$(FeatureCoreCLR)' != 'true' and '$(TargetsWindows)' == 'true'">
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\Thread.Windows.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\WaitHandle.Windows.cs" />
<Compile Include="$(CommonPath)\Interop\Windows\Kernel32\Interop.Threading.cs">
<Link>Interop\Windows\Kernel32\Interop.Threading.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)System\IParsable.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ namespace System.Threading
{
internal sealed partial class PortableThreadPool
{
private static partial class WorkerThread
{
private static bool IsIOPending => false;
}

private struct CpuUtilizationReader
{
private Interop.Sys.ProcessCpuInformation _cpuInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ namespace System.Threading
{
internal sealed partial class PortableThreadPool
{
private static partial class WorkerThread
{
private static bool IsIOPending
{
get
{
bool success =
Interop.Kernel32.GetThreadIOPendingFlag(Interop.Kernel32.GetCurrentThread(), out Interop.BOOL isIOPending);
Debug.Assert(success);
return !success || isIOPending != Interop.BOOL.FALSE;
}
}
}

private struct CpuUtilizationReader
{
public long _idleTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal sealed partial class PortableThreadPool
/// <summary>
/// The worker thread infastructure for the CLR thread pool.
/// </summary>
private static class WorkerThread
private static partial class WorkerThread
{
private const int SemaphoreSpinCountDefaultBaseline = 70;
#if !TARGET_ARM64 && !TARGET_ARM && !TARGET_LOONGARCH64
Expand Down Expand Up @@ -115,6 +115,12 @@ private static void WorkerThreadStart()
}
}

// The thread cannot exit if it has IO pending, otherwise the IO may be canceled
if (IsIOPending)
{
continue;
}

threadAdjustmentLock.Acquire();
try
{
Expand Down