dotnet / dotnet/runtime

Use `NtAssociateWaitCompletionPacket` to register wait handle callbacks on Windows.

Open
#131,196 6 comments 0 reactions 1 assignee Claimed by @teo-tsirpanis View on GitHub
area-System.Threading os-windows tenet-performance
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

Since Windows 8, the Windows thread pool has been able to [serve an unbounded number of wait callbacks with a single thread](https://devblogs.microsoft.com/oldnewthing/20220406-00/?p=106434), by using the [`NtAssociateWaitCompletionPacket`](https://learn.microsoft.com/en-us/windows/win32/devnotes/ntassociatewaitcompletionpacket) API to register wait handle completion packets on an I/O completion port. We can make use of this API in the portable thread pool, by leveraging its existing I/O completion port. Other environments like [Go](https://go.dev/src/runtime/netpoll_windows.go) and [Bun](https://github.com/oven-sh/libuv/commit/96873308d4505884ba4f82ac89905fe0fd97eb9e) are using it too.

If it's OK to do, should we keep the existing `PortableThreadPool.WaitThread` mechanism as a fallback if `NtAssociateWaitCompletionPacket` does not exist? It should exist on all Windows versions currently supported by .NET, barring potential outliers like Xbox.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.