Skip to content

Commit fbf6be5

Browse files
VSadovCopilotjkotas
authored
A few fixes in the threadpool semaphore. Unify Windows/Unix implementation of LIFO policy. (#123921)
Re: #123159 Changes: - Correctly handle `Backoff.Exponential(0)`. Embarrassing bug. To get exponentially growing random spin count for an iteration we generate pseudorandom `uint` and do `>> (32 - attempt)`. Since C# masks the shift operand with 31, when `attempt==0` we end up not shifting at all, and the first iteration gets a large random spin count. That caused many noisy results and interestingly some improvements (in scenarios that benefit from very long spins). - Unified implementation of LIFO policy with lightweight minimal implementation of LIFO waiting. Once we are done spinning, we block threads and when workers are needed again wake them in LIFO order. Unix WaitSubsystem is pretty heavy for these needs. It supports Interruptible waits, waiting on multiple objects, etc... None of that is interesting here. Most calls into the subsystem take a global process-wide lock which can contend under load with other uses, or a worker-waking threads may contend with the workers going to sleep, etc... Windows used an opaque `GetQueuedCompletionStatus` for the side effect of releasing threads in LIFO order when completion is posted, with unknown overheads and interactions, even though typically it is more efficient than Unix WaitSubsystem. The portable implementation seems to be faster than either of the platform-specific ones. (measured by disabling spinning and running a few latency-sensitive benchmarks). The portable implementation is also easier to reason about and to debug anomalies. - Adaptive spinning in the threadpool based on estimates of CPU core availability. Spinning in threadpool is very tricky and spinning benefits differ greatly between scenarios. For some scenarios the longer the spin the better. But there are scenarios that benefit when the threadpool releases cores quickly once it sees no work. No preset fixed spin count is going to be good for everything. Adaptive approach appears to be necessary to improve some scenarios without regressing many others. We can further improve the heuristic, if there are more ideas. --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: Jan Kotas <[email protected]>
1 parent 67eb8c0 commit fbf6be5

21 files changed

Lines changed: 683 additions & 195 deletions

‎docs/coding-guidelines/interop-guidelines.md‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ internal static partial class Interop
3434
...
3535
internal static partial class Interop
3636
{
37-
internal static partial class mincore { ... }
37+
internal static partial class Mincore { ... }
3838
}
3939
```
4040
- With few exceptions, the only methods that should be defined in these interop types are DllImports.
@@ -49,7 +49,9 @@ internal static partial class Interop
4949
```
5050
\Common\src\Interop
5151
\Windows
52-
\mincore
52+
\Kernel32
53+
... interop files
54+
\Mincore
5355
... interop files
5456
\Unix
5557
\libc
@@ -71,8 +73,8 @@ As shown above, platforms may be additive, in that an assembly may use functiona
7173
\libc
7274
\Interop.strerror.cs
7375
\Windows
74-
\mincore
75-
\Interop.OutputDebugString.cs
76+
\Mincore
77+
\Interop.WaitOnAddress.cs <-- Also contains WakeByAddressSingle
7678
```
7779

7880
- If structs/constants will be used on their own without an associated DllImport, or if they may be used with multiple DllImports not in the same file, they should be declared in a separate file.
@@ -81,9 +83,9 @@ As shown above, platforms may be additive, in that an assembly may use functiona
8183
```
8284
\Common\src\Interop
8385
\Windows
84-
\mincore
85-
\Interop.DuplicateHandle_SafeTokenHandle.cs
86-
\Interop.DuplicateHandle_IntPtr.cs
86+
\Kernel32
87+
\Interop.DuplicateHandle_SafeFileHandle.cs
88+
\Interop.DuplicateHandle_SafePipeHandle.cs
8789
```
8890

8991
- The library names used per-platform are stored in internal constants in the Interop class in a private Libraries class in a per-platform file named Interop.Libraries.cs. These constants are then used for all DllImports to that library, rather than having the string duplicated each time, e.g.
@@ -94,12 +96,9 @@ internal static partial class Interop // contents of Common\src\Interop\Windows\
9496
private static class Libraries
9597
{
9698
internal const string Kernel32 = "kernel32.dll";
99+
internal const string OleAut32 = "oleaut32.dll";
97100
internal const string Localization = "api-ms-win-core-localization-l1-2-0.dll";
98-
internal const string Handle = "api-ms-win-core-handle-l1-1-0.dll";
99-
internal const string ProcessThreads = "api-ms-win-core-processthreads-l1-1-0.dll";
100-
internal const string File = "api-ms-win-core-file-l1-1-0.dll";
101-
internal const string NamedPipe = "api-ms-win-core-namedpipe-l1-1-0.dll";
102-
internal const string IO = "api-ms-win-core-io-l1-1-0.dll";
101+
internal const string Synch = "api-ms-win-core-synch-l1-2-0.dll";
103102
...
104103
}
105104
}

‎src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Windows.targets‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ The .NET Foundation licenses this file to you under the MIT license.
8282
<SdkNativeLibrary Include="user32.lib" />
8383
<SdkNativeLibrary Include="version.lib" />
8484
<SdkNativeLibrary Include="ws2_32.lib" />
85+
<SdkNativeLibrary Include="Synchronization.lib" />
8586
</ItemGroup>
8687

8788
<ItemGroup>

‎src/coreclr/nativeaot/BuildIntegration/WindowsAPIs.txt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2340,6 +2340,12 @@ ws2_32!WSCUpdateProvider
23402340
ws2_32!WSCWriteNameSpaceOrder
23412341
ws2_32!WSCWriteProviderOrder
23422342

2343+
#
2344+
# Synchronization.lib
2345+
#
2346+
api-ms-win-core-synch-l1-2-0!WaitOnAddress
2347+
api-ms-win-core-synch-l1-2-0!WakeByAddressSingle
2348+
23432349
#
23442350
# Include all memory allocation APIs from ucrt to ensure that all of them use allocator
23452351
# from the same ucrt copy. Also include the frequently used basic memory manipulation APIs.

‎src/coreclr/tools/aot/ILCompiler/reproNative/reproNative.vcxproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
<NativeAotSourceRoot>$(CoreClrSourceRoot)nativeaot\</NativeAotSourceRoot>
4747
<ArtifactsRoot>$(CoreClrSourceRoot)..\..\artifacts\</ArtifactsRoot>
4848
<NativeRoot>$(CoreClrSourceRoot)..\native\</NativeRoot>
49-
<Win32SDKLibs>advapi32.lib;bcrypt.lib;crypt32.lib;iphlpapi.lib;kernel32.lib;mswsock.lib;ncrypt.lib;normaliz.lib;ntdll.lib;ole32.lib;oleaut32.lib;secur32.lib;user32.lib;version.lib;ws2_32.lib</Win32SDKLibs>
49+
<Win32SDKLibs>advapi32.lib;bcrypt.lib;crypt32.lib;iphlpapi.lib;kernel32.lib;mswsock.lib;ncrypt.lib;normaliz.lib;ntdll.lib;ole32.lib;oleaut32.lib;secur32.lib;user32.lib;version.lib;ws2_32.lib;Synchronization.lib</Win32SDKLibs>
5050
</PropertyGroup>
5151
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
5252
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
5+
using System.Runtime.InteropServices;
6+
7+
internal static partial class Interop
8+
{
9+
internal static partial class Sys
10+
{
11+
[LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_LowLevelFutex_WaitOnAddress")]
12+
internal static unsafe partial void LowLevelFutex_WaitOnAddress(int* address, int comparand);
13+
14+
[LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_LowLevelFutex_WaitOnAddressTimeout")]
15+
[return: MarshalAs(UnmanagedType.Bool)]
16+
internal static unsafe partial bool LowLevelFutex_WaitOnAddressTimeout(int* address, int comparand, int timeoutMilliseconds);
17+
18+
[LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_LowLevelFutex_WakeByAddressSingle")]
19+
internal static unsafe partial void LowLevelFutex_WakeByAddressSingle(int* address);
20+
}
21+
}

‎src/libraries/Common/src/Interop/Unix/System.Native/Interop.LowLevelMonitor.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ internal static partial class Sys
1717
[LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_LowLevelMonitor_Acquire")]
1818
internal static partial void LowLevelMonitor_Acquire(IntPtr monitor);
1919

20+
[SuppressGCTransition]
2021
[LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_LowLevelMonitor_Release")]
2122
internal static partial void LowLevelMonitor_Release(IntPtr monitor);
2223

‎src/libraries/Common/src/Interop/Windows/Interop.Libraries.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ internal static partial class Libraries
4848
internal const string Xolehlp = "xolehlp.dll";
4949
internal const string Comdlg32 = "comdlg32.dll";
5050
internal const string Gdiplus = "gdiplus.dll";
51-
internal const string Oleaut32 = "oleaut32.dll";
5251
internal const string Winspool = "winspool.drv";
52+
internal const string Synch = "api-ms-win-core-synch-l1-2-0.dll";
5353
}
5454
}

‎src/libraries/Common/src/Interop/Windows/Kernel32/Interop.CriticalSection.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ internal struct CRITICAL_SECTION
2525
[LibraryImport(Libraries.Kernel32)]
2626
internal static unsafe partial void EnterCriticalSection(CRITICAL_SECTION* lpCriticalSection);
2727

28+
[SuppressGCTransition]
2829
[LibraryImport(Libraries.Kernel32)]
2930
internal static unsafe partial void LeaveCriticalSection(CRITICAL_SECTION* lpCriticalSection);
3031

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
5+
using System.Runtime.InteropServices;
6+
7+
internal static partial class Interop
8+
{
9+
internal static partial class Mincore
10+
{
11+
[LibraryImport(Libraries.Synch, SetLastError = true)]
12+
internal static unsafe partial BOOL WaitOnAddress(void* Address, void* CompareAddress, nint AddressSize, int dwMilliseconds);
13+
14+
[LibraryImport(Libraries.Synch)]
15+
internal static unsafe partial void WakeByAddressSingle(void* Address);
16+
}
17+
}

‎src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,6 +1840,9 @@
18401840
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.CreateSymbolicLink.cs">
18411841
<Link>Common\Interop\Windows\Kernel32\Interop.CreateSymbolicLink.cs</Link>
18421842
</Compile>
1843+
<Compile Include="$(CommonPath)Interop\Windows\Mincore\Interop.WaitOnAddress.cs">
1844+
<Link>Common\Interop\Windows\Mincore\Interop.WaitOnAddress.cs</Link>
1845+
</Compile>
18431846
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.CriticalSection.cs">
18441847
<Link>Common\Interop\Windows\Kernel32\Interop.CriticalSection.cs</Link>
18451848
</Compile>
@@ -2471,6 +2474,9 @@
24712474
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.LowLevelMonitor.cs">
24722475
<Link>Common\Interop\Unix\System.Native\Interop.LowLevelMonitor.cs</Link>
24732476
</Compile>
2477+
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Futex.cs">
2478+
<Link>Common\Interop\Unix\System.Native\Interop.Futex.cs</Link>
2479+
</Compile>
24742480
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.LSeek.cs">
24752481
<Link>Common\Interop\Unix\System.Native\Interop.LSeek.cs</Link>
24762482
</Compile>
@@ -2825,8 +2831,9 @@
28252831
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\PortableThreadPool.Unix.cs" Condition="'$(TargetsUnix)' == 'true' or ('$(TargetsBrowser)' == 'true' and '$(FeatureWasmManagedThreads)' != 'true') or '$(TargetsWasi)' == 'true'" />
28262832
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\PortableThreadPool.Windows.cs" Condition="'$(TargetsWindows)' == 'true'" />
28272833
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\LowLevelLifoSemaphore.cs" />
2828-
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\LowLevelLifoSemaphore.Windows.cs" Condition="'$(TargetsWindows)' == 'true'" />
2829-
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\LowLevelLifoSemaphore.Unix.cs" Condition="'$(TargetsUnix)' == 'true' or '$(TargetsBrowser)' == 'true' or '$(TargetsWasi)' == 'true'" />
2834+
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\LowLevelThreadBlocker.cs" />
2835+
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\LowLevelFutex.Windows.cs" Condition="'$(TargetsWindows)' == 'true'" />
2836+
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\LowLevelFutex.Unix.cs" Condition="'$(TargetsUnix)' == 'true'" />
28302837
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\PreAllocatedOverlapped.Windows.cs" Condition="'$(TargetsWindows)' == 'true'" />
28312838
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\PreAllocatedOverlapped.Unix.cs" Condition="'$(TargetsUnix)' == 'true' or ('$(TargetsBrowser)' == 'true' and '$(FeatureWasmManagedThreads)' == 'true')" />
28322839
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\PreAllocatedOverlapped.Portable.cs" Condition="('$(TargetsBrowser)' != 'true' and '$(TargetsWasi)' != 'true') or '$(FeatureWasmManagedThreads)' == 'true'" />
@@ -2930,4 +2937,4 @@
29302937
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\Wasi\WasiPollWorld.wit.imports.wasi.io.v0_2_0.IPoll.cs" />
29312938
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\Wasi\WasiPollWorld.wit.imports.wasi.io.v0_2_0.PollInterop.cs" />
29322939
</ItemGroup>
2933-
</Project>
2940+
</Project>

0 commit comments

Comments
 (0)