Skip to content

Commit ac128bb

Browse files
Copilotadamsitnik
andauthored
Move event creation logic into SafeFileHandle.RentSyncWaitHandle
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/661f3798-57f4-4862-aef4-5bbca78c8924 Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
1 parent 8ed0afc commit ac128bb

2 files changed

Lines changed: 26 additions & 29 deletions

File tree

src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.OverlappedValueTaskSource.Windows.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,30 @@ public sealed partial class SafeFileHandle : SafeHandleZeroOrMinusOneIsInvalid
2121
internal OverlappedValueTaskSource GetOverlappedValueTaskSource() =>
2222
Interlocked.Exchange(ref _reusableOverlappedValueTaskSource, null) ?? new OverlappedValueTaskSource(this);
2323

24-
internal nint RentSyncWaitHandle() =>
25-
Interlocked.Exchange(ref _reusableWaitHandle, -1);
24+
internal nint RentSyncWaitHandle()
25+
{
26+
nint handle = Interlocked.Exchange(ref _reusableWaitHandle, -1);
27+
if (handle != -1)
28+
{
29+
return handle;
30+
}
31+
32+
using SafeWaitHandle safeHandle = Interop.Kernel32.CreateEventEx(
33+
IntPtr.Zero,
34+
null,
35+
Interop.Kernel32.CREATE_EVENT_MANUAL_RESET,
36+
(uint)(Interop.Kernel32.SYNCHRONIZE | Interop.Kernel32.EVENT_MODIFY_STATE));
37+
38+
if (safeHandle.IsInvalid)
39+
{
40+
throw Win32Marshal.GetExceptionForLastWin32Error();
41+
}
42+
43+
handle = safeHandle.DangerousGetHandle();
44+
safeHandle.SetHandleAsInvalid();
45+
46+
return handle;
47+
}
2648

2749
internal void ReturnSyncWaitHandle(nint waitHandle)
2850
{

src/libraries/System.Private.CoreLib/src/System/IO/RandomAccess.Windows.cs

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ _ when IsEndOfFile(errorCode, handle, fileOffset) => 0,
6767

6868
private static unsafe int ReadSyncUsingAsyncHandle(SafeFileHandle handle, Span<byte> buffer, long fileOffset)
6969
{
70-
IntPtr eventHandle = GetSyncOverlappedEvent(handle);
70+
IntPtr eventHandle = handle.RentSyncWaitHandle();
7171
NativeOverlapped* overlapped = null;
7272

7373
try
@@ -144,7 +144,7 @@ private static unsafe void WriteSyncUsingAsyncHandle(SafeFileHandle handle, Read
144144
return;
145145
}
146146

147-
IntPtr eventHandle = GetSyncOverlappedEvent(handle);
147+
IntPtr eventHandle = handle.RentSyncWaitHandle();
148148
NativeOverlapped* overlapped = null;
149149

150150
try
@@ -686,31 +686,6 @@ private static async ValueTask WriteGatherAtOffsetMultipleSyscallsAsync(SafeFile
686686
}
687687
}
688688

689-
private static IntPtr GetSyncOverlappedEvent(SafeFileHandle safeFileHandle)
690-
{
691-
nint handle = safeFileHandle.RentSyncWaitHandle();
692-
if (handle != -1)
693-
{
694-
return handle;
695-
}
696-
697-
using SafeWaitHandle safeHandle = Interop.Kernel32.CreateEventEx(
698-
IntPtr.Zero,
699-
null,
700-
Interop.Kernel32.CREATE_EVENT_MANUAL_RESET,
701-
(uint)(Interop.Kernel32.SYNCHRONIZE | Interop.Kernel32.EVENT_MODIFY_STATE));
702-
703-
if (safeHandle.IsInvalid)
704-
{
705-
throw Win32Marshal.GetExceptionForLastWin32Error();
706-
}
707-
708-
handle = safeHandle.DangerousGetHandle();
709-
safeHandle.SetHandleAsInvalid();
710-
711-
return handle;
712-
}
713-
714689
private static unsafe NativeOverlapped* AllocNativeOverlappedWithEventHandle(SafeFileHandle handle, long fileOffset, IntPtr eventHandle)
715690
{
716691
NativeOverlapped* result = (NativeOverlapped*)NativeMemory.AllocZeroed((nuint)sizeof(NativeOverlapped));

0 commit comments

Comments
 (0)