Skip to content

Commit

Permalink
Fix build break due to conflicting PRs (dotnet/coreclr#23726)
Browse files Browse the repository at this point in the history
Signed-off-by: dotnet-bot <[email protected]>
  • Loading branch information
stephentoub committed Apr 6, 2019
1 parent ed49d90 commit 6ee20bc
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 54 deletions.
17 changes: 11 additions & 6 deletions src/Common/src/CoreLib/System/IO/FileStream.Unix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
using Microsoft.Win32.SafeHandles;
using System.Diagnostics;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -29,7 +30,7 @@ public partial class FileStream : Stream
/// synchronously from ReadAsync which can be reused if the count matches the next request.
/// Only initialized when <see cref="_useAsyncIO"/> is true.
/// </summary>
private AsyncState _asyncState;
private AsyncState? _asyncState;

/// <summary>Lazily-initialized value for whether the file supports seeking.</summary>
private bool? _canSeek;
Expand Down Expand Up @@ -290,7 +291,7 @@ public override ValueTask DisposeAsync()
// override may already exist on a derived type.
if (_useAsyncIO && _writePos > 0)
{
return new ValueTask(Task.Factory.StartNew(s => ((FileStream)s).Dispose(), this,
return new ValueTask(Task.Factory.StartNew(s => ((FileStream)s!).Dispose(), this, // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default));
}

Expand Down Expand Up @@ -364,7 +365,7 @@ private Task FlushAsyncInternal(CancellationToken cancellationToken)
if (CanWrite)
{
return Task.Factory.StartNew(
state => ((FileStream)state).FlushOSBuffer(),
state => ((FileStream)state!).FlushOSBuffer(), // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
this,
cancellationToken,
TaskCreationOptions.DenyChildAttach,
Expand Down Expand Up @@ -507,9 +508,10 @@ private unsafe int ReadNative(Span<byte> buffer)
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <param name="synchronousResult">If the operation completes synchronously, the number of bytes read.</param>
/// <returns>A task that represents the asynchronous read operation.</returns>
private Task<int> ReadAsyncInternal(Memory<byte> destination, CancellationToken cancellationToken, out int synchronousResult)
private Task<int>? ReadAsyncInternal(Memory<byte> destination, CancellationToken cancellationToken, out int synchronousResult)
{
Debug.Assert(_useAsyncIO);
Debug.Assert(_asyncState != null);

if (!CanRead) // match Windows behavior; this gets thrown synchronously
{
Expand Down Expand Up @@ -567,7 +569,8 @@ private Task<int> ReadAsyncInternal(Memory<byte> destination, CancellationToken
// whereas on Windows it may happen before the write has completed.
Debug.Assert(t.Status == TaskStatus.RanToCompletion);
var thisRef = (FileStream)s;
var thisRef = (FileStream)s!; // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
Debug.Assert(thisRef._asyncState != null);
try
{
Memory<byte> memory = thisRef._asyncState.Memory;
Expand Down Expand Up @@ -668,6 +671,7 @@ private unsafe void WriteNative(ReadOnlySpan<byte> source)
private ValueTask WriteAsyncInternal(ReadOnlyMemory<byte> source, CancellationToken cancellationToken)
{
Debug.Assert(_useAsyncIO);
Debug.Assert(_asyncState != null);

if (cancellationToken.IsCancellationRequested)
return new ValueTask(Task.FromCanceled(cancellationToken));
Expand Down Expand Up @@ -724,7 +728,8 @@ private ValueTask WriteAsyncInternal(ReadOnlyMemory<byte> source, CancellationTo
// whereas on Windows it may happen before the write has completed.
Debug.Assert(t.Status == TaskStatus.RanToCompletion);
var thisRef = (FileStream)s;
var thisRef = (FileStream)s!; // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
Debug.Assert(thisRef._asyncState != null);
try
{
ReadOnlyMemory<byte> readOnlyMemory = thisRef._asyncState.ReadOnlyMemory;
Expand Down
29 changes: 16 additions & 13 deletions src/Common/src/CoreLib/System/IO/FileStream.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
using System.Buffers;
using System.Diagnostics;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -46,11 +47,11 @@ public partial class FileStream : Stream
private bool _isPipe; // Whether to disable async buffering code.
private long _appendStart; // When appending, prevent overwriting file.

private static unsafe IOCompletionCallback s_ioCallback = FileStreamCompletionSource.IOCallback;
private static readonly unsafe IOCompletionCallback s_ioCallback = FileStreamCompletionSource.IOCallback;

private Task _activeBufferOperation = null; // tracks in-progress async ops using the buffer
private PreAllocatedOverlapped _preallocatedOverlapped; // optimization for async ops to avoid per-op allocations
private FileStreamCompletionSource _currentOverlappedOwner; // async op currently using the preallocated overlapped
private Task _activeBufferOperation = Task.CompletedTask; // tracks in-progress async ops using the buffer
private PreAllocatedOverlapped? _preallocatedOverlapped; // optimization for async ops to avoid per-op allocations
private FileStreamCompletionSource? _currentOverlappedOwner; // async op currently using the preallocated overlapped

private void Init(FileMode mode, FileShare share, string originalPath)
{
Expand Down Expand Up @@ -196,8 +197,7 @@ private static unsafe Interop.Kernel32.SECURITY_ATTRIBUTES GetSecAttrs(FileShare
return secAttrs;
}

private bool HasActiveBufferOperation
=> _activeBufferOperation != null && !_activeBufferOperation.IsCompleted;
private bool HasActiveBufferOperation => !_activeBufferOperation.IsCompleted;

public override bool CanSeek => _canSeek;

Expand Down Expand Up @@ -406,7 +406,8 @@ private void SetLengthCore(long value)

// Instance method to help code external to this MarshalByRefObject avoid
// accessing its fields by ref. This avoids a compiler warning.
private FileStreamCompletionSource CompareExchangeCurrentOverlappedOwner(FileStreamCompletionSource newSource, FileStreamCompletionSource existingSource) => Interlocked.CompareExchange(ref _currentOverlappedOwner, newSource, existingSource);
private FileStreamCompletionSource? CompareExchangeCurrentOverlappedOwner(FileStreamCompletionSource? newSource, FileStreamCompletionSource? existingSource) =>
Interlocked.CompareExchange(ref _currentOverlappedOwner, newSource, existingSource);

private int ReadSpan(Span<byte> destination)
{
Expand Down Expand Up @@ -732,7 +733,7 @@ private unsafe void WriteCore(ReadOnlySpan<byte> source)
return;
}

private Task<int> ReadAsyncInternal(Memory<byte> destination, CancellationToken cancellationToken, out int synchronousResult)
private Task<int>? ReadAsyncInternal(Memory<byte> destination, CancellationToken cancellationToken, out int synchronousResult)
{
Debug.Assert(_useAsyncIO);
if (!CanRead) throw Error.GetReadNotSupported();
Expand Down Expand Up @@ -1024,7 +1025,7 @@ private ValueTask WriteAsyncInternal(ReadOnlyMemory<byte> source, CancellationTo
// We return a Task that represents one or both.

// Flush the buffer asynchronously if there's anything to flush
Task flushTask = null;
Task? flushTask = null;
if (_writePos > 0)
{
flushTask = FlushWriteAsync(cancellationToken);
Expand Down Expand Up @@ -1341,6 +1342,7 @@ private async Task AsyncModeCopyToAsync(Stream destination, int bufferSize, Canc
{
cancellationReg = cancellationToken.UnsafeRegister(s =>
{
Debug.Assert(s is AsyncCopyToAwaitable);
var innerAwaitable = (AsyncCopyToAwaitable)s;
unsafe
{
Expand Down Expand Up @@ -1492,7 +1494,7 @@ private sealed unsafe class AsyncCopyToAwaitable : ICriticalNotifyCompletion
/// s_sentinel if the I/O operation completed before the await,
/// s_callback if it completed after the await yielded.
/// </summary>
internal Action _continuation;
internal Action? _continuation;
/// <summary>Last error code from completed operation.</summary>
internal uint _errorCode;
/// <summary>Last number of read bytes from completed operation.</summary>
Expand All @@ -1519,7 +1521,8 @@ internal void ResetForNextOperation()
/// <summary>Overlapped callback: store the results, then invoke the continuation delegate.</summary>
internal static unsafe void IOCallback(uint errorCode, uint numBytes, NativeOverlapped* pOVERLAP)
{
var awaitable = (AsyncCopyToAwaitable)ThreadPoolBoundHandle.GetNativeOverlappedState(pOVERLAP);
var awaitable = (AsyncCopyToAwaitable?)ThreadPoolBoundHandle.GetNativeOverlappedState(pOVERLAP);
Debug.Assert(awaitable != null);

Debug.Assert(!ReferenceEquals(awaitable._continuation, s_sentinel), "Sentinel must not have already been set as the continuation");
awaitable._errorCode = errorCode;
Expand Down Expand Up @@ -1585,7 +1588,7 @@ private Task FlushAsyncInternal(CancellationToken cancellationToken)
if (CanWrite)
{
return Task.Factory.StartNew(
state => ((FileStream)state).FlushOSBuffer(),
state => ((FileStream)state!).FlushOSBuffer(), // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
this,
cancellationToken,
TaskCreationOptions.DenyChildAttach,
Expand Down Expand Up @@ -1634,7 +1637,7 @@ private SafeFileHandle ValidateFileHandle(SafeFileHandle fileHandle)
// probably be consistent w/ every other directory.
int errorCode = Marshal.GetLastWin32Error();

if (errorCode == Interop.Errors.ERROR_PATH_NOT_FOUND && _path.Length == PathInternal.GetRootLength(_path))
if (errorCode == Interop.Errors.ERROR_PATH_NOT_FOUND && _path!.Length == PathInternal.GetRootLength(_path))
errorCode = Interop.Errors.ERROR_ACCESS_DENIED;

throw Win32Marshal.GetExceptionForWin32Error(errorCode, _path);
Expand Down
Loading

0 comments on commit 6ee20bc

Please sign in to comment.