Skip to content

Commit

Permalink
Revert "Efficient RandomAccess async I/O on the thread pool. (dotne…
Browse files Browse the repository at this point in the history
…t#55123)"

This reverts commit 0696727.
  • Loading branch information
stephentoub committed Jul 6, 2021
1 parent ce39550 commit 77b05b8
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 279 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Microsoft.Win32.SafeHandles
{
public sealed partial class SafeFileHandle : SafeHandleZeroOrMinusOneIsInvalid
public sealed class SafeFileHandle : SafeHandleZeroOrMinusOneIsInvalid
{
// not using bool? as it's not thread safe
private volatile NullableBool _canSeek = NullableBool.Undefined;
Expand All @@ -23,6 +23,11 @@ private SafeFileHandle(bool ownsHandle)
SetHandle(new IntPtr(-1));
}

public SafeFileHandle(IntPtr preexistingHandle, bool ownsHandle) : this(ownsHandle)
{
SetHandle(preexistingHandle);
}

public bool IsAsync { get; private set; }

internal bool CanSeek => !IsClosed && GetCanSeek();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,33 @@ namespace Microsoft.Win32.SafeHandles
{
public sealed partial class SafeFileHandle : SafeHandleZeroOrMinusOneIsInvalid
{
private OverlappedValueTaskSource? _reusableOverlappedValueTaskSource; // reusable OverlappedValueTaskSource that is currently NOT being used
private ValueTaskSource? _reusableValueTaskSource; // reusable ValueTaskSource that is currently NOT being used

// Rent the reusable OverlappedValueTaskSource, or create a new one to use if we couldn't get one (which
// should only happen on first use or if the SafeFileHandle is being used concurrently).
internal OverlappedValueTaskSource GetOverlappedValueTaskSource() =>
Interlocked.Exchange(ref _reusableOverlappedValueTaskSource, null) ?? new OverlappedValueTaskSource(this);
// Rent the reusable ValueTaskSource, or create a new one to use if we couldn't get one (which
// should only happen on first use or if the FileStream is being used concurrently).
internal ValueTaskSource GetValueTaskSource() => Interlocked.Exchange(ref _reusableValueTaskSource, null) ?? new ValueTaskSource(this);

protected override bool ReleaseHandle()
{
bool result = Interop.Kernel32.CloseHandle(handle);

Interlocked.Exchange(ref _reusableOverlappedValueTaskSource, null)?.Dispose();
Interlocked.Exchange(ref _reusableValueTaskSource, null)?.Dispose();

return result;
}

private void TryToReuse(OverlappedValueTaskSource source)
private void TryToReuse(ValueTaskSource source)
{
source._source.Reset();

if (Interlocked.CompareExchange(ref _reusableOverlappedValueTaskSource, source, null) is not null)
if (Interlocked.CompareExchange(ref _reusableValueTaskSource, source, null) is not null)
{
source._preallocatedOverlapped.Dispose();
}
}

/// <summary>Reusable IValueTaskSource for RandomAccess async operations based on Overlapped I/O.</summary>
internal sealed unsafe class OverlappedValueTaskSource : IValueTaskSource<int>, IValueTaskSource
/// <summary>Reusable IValueTaskSource for FileStream ValueTask-returning async operations.</summary>
internal sealed unsafe class ValueTaskSource : IValueTaskSource<int>, IValueTaskSource
{
internal static readonly IOCompletionCallback s_ioCallback = IOCallback;

Expand All @@ -56,7 +55,7 @@ internal sealed unsafe class OverlappedValueTaskSource : IValueTaskSource<int>,
/// </summary>
internal ulong _result;

internal OverlappedValueTaskSource(SafeFileHandle fileHandle)
internal ValueTaskSource(SafeFileHandle fileHandle)
{
_fileHandle = fileHandle;
_source.RunContinuationsAsynchronously = true;
Expand Down Expand Up @@ -113,7 +112,7 @@ internal void RegisterForCancellation(CancellationToken cancellationToken)
{
_cancellationRegistration = cancellationToken.UnsafeRegister(static (s, token) =>
{
OverlappedValueTaskSource vts = (OverlappedValueTaskSource)s!;
ValueTaskSource vts = (ValueTaskSource)s!;
if (!vts._fileHandle.IsInvalid)
{
try
Expand Down Expand Up @@ -157,7 +156,7 @@ internal void ReleaseResources()
// done by that cancellation registration, e.g. unregistering. As such, we use _result to both track who's
// responsible for calling Complete and for passing the necessary data between parties.

/// <summary>Invoked when the async operation finished being scheduled.</summary>
/// <summary>Invoked when AsyncWindowsFileStreamStrategy has finished scheduling the async operation.</summary>
internal void FinishedScheduling()
{
// Set the value to 1. If it was already non-0, then the asynchronous operation already completed but
Expand All @@ -173,7 +172,7 @@ internal void FinishedScheduling()
/// <summary>Invoked when the asynchronous operation has completed asynchronously.</summary>
private static void IOCallback(uint errorCode, uint numBytes, NativeOverlapped* pOverlapped)
{
OverlappedValueTaskSource? vts = (OverlappedValueTaskSource?)ThreadPoolBoundHandle.GetNativeOverlappedState(pOverlapped);
ValueTaskSource? vts = (ValueTaskSource?)ThreadPoolBoundHandle.GetNativeOverlappedState(pOverlapped);
Debug.Assert(vts is not null);
Debug.Assert(vts._overlapped == pOverlapped, "Overlaps don't match");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public SafeFileHandle() : base(true)
{
}

public SafeFileHandle(IntPtr preexistingHandle, bool ownsHandle) : base(ownsHandle)
{
SetHandle(preexistingHandle);
}

public bool IsAsync => (GetFileOptions() & FileOptions.Asynchronous) != 0;

internal bool CanSeek => !IsClosed && GetFileType() == Interop.Kernel32.FileTypes.FILE_TYPE_DISK;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@
<Compile Include="$(MSBuildThisFileDirectory)Microsoft\Win32\SafeHandles\CriticalHandleZeroOrMinusOneIsInvalid.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Microsoft\Win32\SafeHandles\SafeHandleMinusOneIsInvalid.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Microsoft\Win32\SafeHandles\SafeHandleZeroOrMinusOneIsInvalid.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Microsoft\Win32\SafeHandles\SafeFileHandle.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Microsoft\Win32\SafeHandles\SafeFileHandle.ThreadPoolValueTaskSource.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Microsoft\Win32\SafeHandles\SafeWaitHandle.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\AccessViolationException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Action.cs" />
Expand Down Expand Up @@ -1519,7 +1517,7 @@
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetModuleFileName.cs">
<Link>Common\Interop\Windows\Kernel32\Interop.GetModuleFileName.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetOverlappedResult.cs">
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetOverlappedResult.cs">
<Link>Common\Interop\Windows\Kernel32\Interop.GetOverlappedResult.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetProcessMemoryInfo.cs">
Expand Down Expand Up @@ -1777,7 +1775,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Internal\Console.Windows.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Internal\Win32\RegistryKey.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Microsoft\Win32\SafeHandles\SafeFileHandle.Windows.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Microsoft\Win32\SafeHandles\SafeFileHandle.OverlappedValueTaskSource.Windows.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Microsoft\Win32\SafeHandles\SafeFileHandle.ValueTaskSource.Windows.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Microsoft\Win32\SafeHandles\SafeFindHandle.Windows.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Microsoft\Win32\SafeHandles\SafeRegistryHandle.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Microsoft\Win32\SafeHandles\SafeRegistryHandle.Windows.cs" />
Expand Down
Loading

0 comments on commit 77b05b8

Please sign in to comment.