Reapply "A few fixes in the threadpool semaphore. Unify Windows/Unix implementation of LIFO policy." (#125193)#125596
Reapply "A few fixes in the threadpool semaphore. Unify Windows/Unix implementation of LIFO policy." (#125193)#125596VSadov wants to merge 3 commits intodotnet:mainfrom
Conversation
…implementation of LIFO policy." (dotnet#125193) This reverts commit 51b1e92.
|
Tagging subscribers to this area: @agocke, @VSadov |
There was a problem hiding this comment.
Pull request overview
Reapplies and reworks the ThreadPool LIFO semaphore changes (previously reverted due to NuGet restore regressions) by unifying the blocking/wake implementation across Windows and Unix using OS compare-and-wait primitives (WaitOnAddress / futex) with a monitor fallback.
Changes:
- Adds low-level compare-and-wait interop for Windows (WaitOnAddress) and Linux (futex) and wires them through System.Native/CoreLib.
- Replaces the prior per-OS
LowLevelLifoSemaphoreimplementations with a unified managed implementation using a LIFO stack of per-thread blockers plus updated spin/backoff behavior. - Adjusts worker dispatch heuristics (missed-steal handling) and configuration plumbing (cooperative blocking env var alias).
Reviewed changes
Copilot reviewed 23 out of 23 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| src/native/libs/System.Native/pal_threading.h | Adds exported futex-related entrypoints to the System.Native PAL surface. |
| src/native/libs/System.Native/pal_threading.c | Implements Linux futex wait/wake wrappers; provides non-Linux stubs. |
| src/native/libs/System.Native/entrypoints.c | Registers the new futex entrypoints for managed interop. |
| src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolWorkQueue.cs | Adds a 1ms sleep before requesting workers when a steal was missed. |
| src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.WorkerThread.cs | Switches the worker wait to the new LowLevelLifoSemaphore.Wait(timeout, activeThreadCount) signature; removes old spin-limit wiring at the call site. |
| src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.Blocking.cs | Adds the DOTNET_ThreadPool_CooperativeBlocking env var alias for cooperative blocking. |
| src/libraries/System.Private.CoreLib/src/System/Threading/LowLevelThreadBlocker.cs | Introduces a portable blocker abstraction (futex/WaitOnAddress or LowLevelMonitor fallback). |
| src/libraries/System.Private.CoreLib/src/System/Threading/LowLevelLifoSemaphore.cs | Replaces OS-specific semaphore core with a unified managed LIFO implementation + updated spin heuristic and wake accounting. |
| src/libraries/System.Private.CoreLib/src/System/Threading/LowLevelLifoSemaphore.Windows.cs | Removes the prior Windows IOCP-based LIFO semaphore implementation. |
| src/libraries/System.Private.CoreLib/src/System/Threading/LowLevelLifoSemaphore.Unix.cs | Removes the prior Unix WaitSubsystem-based semaphore implementation. |
| src/libraries/System.Private.CoreLib/src/System/Threading/LowLevelFutex.Windows.cs | Adds Windows WaitOnAddress-based compare-and-wait wrapper. |
| src/libraries/System.Private.CoreLib/src/System/Threading/LowLevelFutex.Unix.cs | Adds Unix futex wrapper (currently Linux-only per comments). |
| src/libraries/System.Private.CoreLib/src/System/Threading/Backoff.cs | Updates exponential backoff to return spin count and reduces max backoff. |
| src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems | Wires new threading and interop files into CoreLib build (adds/removes Compile items). |
| src/libraries/Common/src/Interop/Windows/Mincore/Interop.WaitOnAddress.cs | Adds LibraryImport declarations for WaitOnAddress/WakeByAddressSingle. |
| src/libraries/Common/src/Interop/Windows/Kernel32/Interop.CriticalSection.cs | Adds SuppressGCTransition on LeaveCriticalSection. |
| src/libraries/Common/src/Interop/Windows/Interop.Libraries.cs | Adds the Synch API-set library constant for WaitOnAddress imports. |
| src/libraries/Common/src/Interop/Unix/System.Native/Interop.LowLevelMonitor.cs | Adds SuppressGCTransition on LowLevelMonitor_Release. |
| src/libraries/Common/src/Interop/Unix/System.Native/Interop.Futex.cs | Adds LibraryImport declarations for futex wait/wake entrypoints. |
| src/coreclr/tools/aot/ILCompiler/reproNative/reproNative.vcxproj | Adds Synchronization.lib to link set for NativeAOT repro project. |
| src/coreclr/nativeaot/BuildIntegration/WindowsAPIs.txt | Allows WaitOnAddress/WakeByAddressSingle through the NativeAOT Windows API allowlist. |
| src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Windows.targets | Adds Synchronization.lib to NativeAOT SDK library list. |
| docs/coding-guidelines/interop-guidelines.md | Updates interop guideline examples to match casing/structure and adds Synch library mention. |
You can also share your feedback on Copilot code review. Take the survey.
src/libraries/System.Private.CoreLib/src/System/Threading/LowLevelLifoSemaphore.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Threading/LowLevelLifoSemaphore.cs
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Threading/LowLevelLifoSemaphore.cs
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolWorkQueue.cs
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolWorkQueue.cs
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems
Outdated
Show resolved
Hide resolved
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Reapplies and extends the threadpool semaphore/LIFO-policy changes that were previously reverted due to NuGet restore performance regression, by unifying the Windows/Unix implementation around a shared managed LIFO waiter stack and adding low-level wait/wake primitives (Linux futex, Windows WaitOnAddress) plus supporting interop/AOT wiring.
Changes:
- Add Linux futex exports in System.Native and corresponding managed interop; add Windows WaitOnAddress interop and link inputs for NativeAOT.
- Replace platform-specific
LowLevelLifoSemaphoreimplementations with a unified managed implementation built onLowLevelThreadBlocker. - Adjust threadpool behavior around missed steals (including a brief delay) and tweak a blocking config switch plumbing.
Reviewed changes
Copilot reviewed 23 out of 23 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| src/native/libs/System.Native/pal_threading.h | Adds futex-related PALEXPORT declarations. |
| src/native/libs/System.Native/pal_threading.c | Implements Linux futex wait/wake syscalls (with non-Linux stubs). |
| src/native/libs/System.Native/entrypoints.c | Exposes futex entrypoints via DllImportEntry. |
| src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolWorkQueue.cs | Adds a delay before requesting a worker when missed steals occur. |
| src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.WorkerThread.cs | Switches semaphore construction/Wait signature; minor comment fix. |
| src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.Blocking.cs | Adds env-var name to cooperative blocking config lookup. |
| src/libraries/System.Private.CoreLib/src/System/Threading/LowLevelThreadBlocker.cs | Introduces a portable blocker using futex/WaitOnAddress or monitor fallback. |
| src/libraries/System.Private.CoreLib/src/System/Threading/LowLevelLifoSemaphore.cs | Reworks semaphore into a single managed implementation with LIFO waiter stack + spin heuristic. |
| src/libraries/System.Private.CoreLib/src/System/Threading/LowLevelLifoSemaphore.Windows.cs | Removes prior Windows IOCP-based implementation. |
| src/libraries/System.Private.CoreLib/src/System/Threading/LowLevelLifoSemaphore.Unix.cs | Removes prior Unix WaitSubsystem-based implementation. |
| src/libraries/System.Private.CoreLib/src/System/Threading/LowLevelFutex.Windows.cs | Adds Windows WaitOnAddress/WakeByAddressSingle wrapper. |
| src/libraries/System.Private.CoreLib/src/System/Threading/LowLevelFutex.Unix.cs | Adds Linux futex wrapper (Linux-only). |
| src/libraries/System.Private.CoreLib/src/System/Threading/Backoff.cs | Changes exponential backoff to return spin count and reduces cap. |
| src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems | Wires new threading/interop files into CoreLib build and removes old semaphore OS-specific files. |
| src/libraries/Common/src/Interop/Windows/Mincore/Interop.WaitOnAddress.cs | Adds LibraryImport for WaitOnAddress/WakeByAddressSingle. |
| src/libraries/Common/src/Interop/Windows/Kernel32/Interop.CriticalSection.cs | Adds SuppressGCTransition to LeaveCriticalSection. |
| src/libraries/Common/src/Interop/Windows/Interop.Libraries.cs | Adds Libraries.Synch constant for the synch api-set. |
| src/libraries/Common/src/Interop/Unix/System.Native/Interop.LowLevelMonitor.cs | Adds SuppressGCTransition to LowLevelMonitor_Release. |
| src/libraries/Common/src/Interop/Unix/System.Native/Interop.Futex.cs | Adds LibraryImport declarations for System.Native futex exports. |
| src/coreclr/tools/aot/ILCompiler/reproNative/reproNative.vcxproj | Links Synchronization.lib for WaitOnAddress/WakeByAddressSingle. |
| src/coreclr/nativeaot/BuildIntegration/WindowsAPIs.txt | Adds WaitOnAddress/WakeByAddressSingle to the NativeAOT Windows API list. |
| src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Windows.targets | Adds Synchronization.lib to SDK native libraries for NativeAOT. |
| docs/coding-guidelines/interop-guidelines.md | Updates interop naming/examples (e.g., Mincore, Synch). |
You can also share your feedback on Copilot code review. Take the survey.
Re: #125193
TODO: need to confirm that NuGet restore performance is ok with the updated change.