Repository navigation
Description
In RandomAccess.Windows.cs, we create a dedicated callback:
runtime/src/libraries/System.Private.CoreLib/src/System/IO/RandomAccess.Windows.cs
Line 19 in ad023e0
| private static readonly IOCompletionCallback s_callback = AllocateCallback(); |
Then provide it in these places:
runtime/src/libraries/System.Private.CoreLib/src/System/IO/RandomAccess.Windows.cs
Line 731 in ad023e0
| NativeOverlapped* result = handle.ThreadPoolBinding!.UnsafeAllocateNativeOverlapped(s_callback, resetEvent, null); |
And handle the callback in a dedicated class:
runtime/src/libraries/System.Private.CoreLib/src/System/IO/RandomAccess.Windows.cs
Lines 801 to 825 in ad023e0
| // We need to store the reference count (see the comment in ReleaseRefCount) and an EventHandle to signal the completion. | |
| // We could keep these two things separate, but since ManualResetEvent is sealed and we want to avoid any extra allocations, this type has been created. | |
| // It's basically ManualResetEvent with reference count. | |
| private sealed class CallbackResetEvent : EventWaitHandle | |
| { | |
| private readonly ThreadPoolBoundHandle _threadPoolBoundHandle; | |
| private int _freeWhenZero = 2; // one for the callback and another for the method that calls GetOverlappedResult | |
| internal CallbackResetEvent(ThreadPoolBoundHandle threadPoolBoundHandle) : base(initialState: false, EventResetMode.ManualReset) | |
| { | |
| _threadPoolBoundHandle = threadPoolBoundHandle; | |
| } | |
| internal unsafe void ReleaseRefCount(NativeOverlapped* pOverlapped) | |
| { | |
| // Each SafeFileHandle opened for async IO is bound to ThreadPool. | |
| // It requires us to provide a callback even if we want to use EventHandle and use GetOverlappedResult to obtain the result. | |
| // There can be a race condition between the call to GetOverlappedResult and the callback invocation, | |
| // so we need to track the number of references, and when it drops to zero, then free the native overlapped. | |
| if (Interlocked.Decrement(ref _freeWhenZero) == 0) | |
| { | |
| _threadPoolBoundHandle.FreeNativeOverlapped(pOverlapped); | |
| } | |
| } | |
| } |
Based on my finding from #126807, we don't need to provide a callback when configuring WaitHandle for overlapped provided by ReadFile/WriteFile. What we need to do is setting the low-order bit of hEvent in the OVERLAPPED structure:
Lines 146 to 152 in ce6b594
| private static unsafe NativeOverlapped* AllocateOverlapped(EventWaitHandle waitHandle) | |
| { | |
| NativeOverlapped* overlapped = (NativeOverlapped*)NativeMemory.AllocZeroed((nuint)sizeof(NativeOverlapped)); | |
| overlapped->EventHandle = SetLowOrderBit(waitHandle); | |
| return overlapped; | |
| } |
Lines 218 to 226 in ce6b594
| /// <summary> | |
| /// Returns the event handle with the low-order bit set. | |
| /// Per https://learn.microsoft.com/windows/win32/api/ioapiset/nf-ioapiset-getqueuedcompletionstatus, | |
| /// setting the low-order bit of hEvent in the OVERLAPPED structure prevents the I/O completion | |
| /// from being queued to a completion port bound to the same file object. The kernel masks off | |
| /// the bit when signaling, so the event still works normally. | |
| /// </summary> | |
| private static nint SetLowOrderBit(EventWaitHandle waitHandle) | |
| => waitHandle.SafeWaitHandle.DangerousGetHandle() | 1; |
This should allow us to get rid of CallbackResetEvent, slightly reduce allocations and complexity. The perf might also get better.
Activity
Copilot commentedon Apr 13, 2026
The agent encountered an error and was unable to start working on this issue: The service is temporarily unavailable. Please try again later. (Request id: 2761:C7BFB:196C634:12D7A01:69DD1849)
dotnet-policy-service commentedon Apr 13, 2026
Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.
Copilot commentedon Apr 13, 2026
The agent encountered an error and was unable to start working on this issue: The service is temporarily unavailable. Please try again later. (Request id: 276A:376576:1B56E98:14379C7:69DD1A6D)
Copilot commentedon Apr 13, 2026
The agent encountered an error and was unable to start working on this issue: The service is temporarily unavailable. Please try again later. (Request id: 2764:F4966:1F0385A:16D89FD:69DD1EB9)
adamsitnik commentedon Jun 12, 2026
managed_cachedis what we got in #126845As you can see, sync IO over async file handles is up to two times faster now ;)