Skip to content

Commit

Permalink
Add some changes the mirror missed when merging conflicts manually
Browse files Browse the repository at this point in the history
Signed-off-by: dotnet-bot <[email protected]>
  • Loading branch information
safern authored and stephentoub committed Apr 6, 2019
1 parent 0aa8645 commit 8e61998
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/Common/src/CoreLib/System/IO/FileStream.Linux.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.Threading;
Expand Down
1 change: 1 addition & 0 deletions src/Common/src/CoreLib/System/IO/FileStream.OSX.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
namespace System.IO
{
public partial class FileStream : Stream
Expand Down
1 change: 1 addition & 0 deletions src/Common/src/CoreLib/System/IO/FileStream.Win32.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.Diagnostics;
using Microsoft.Win32.SafeHandles;

Expand Down
1 change: 1 addition & 0 deletions src/Common/src/CoreLib/System/IO/FileStream.WinRT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Win32.SafeHandles;
using System.Runtime.InteropServices;

#nullable enable
namespace System.IO
{
public partial class FileStream : Stream
Expand Down
21 changes: 11 additions & 10 deletions src/Common/src/CoreLib/System/IO/FileStream.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.Runtime.Serialization;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -16,15 +17,15 @@ public partial class FileStream : Stream
private const bool DefaultIsAsync = false;
internal const int DefaultBufferSize = 4096;

private byte[] _buffer;
private byte[]? _buffer;
private int _bufferLength;
private readonly SafeFileHandle _fileHandle;
private readonly SafeFileHandle _fileHandle; // only ever null if ctor throws

/// <summary>Whether the file is opened for reading, writing, or both.</summary>
private readonly FileAccess _access;

/// <summary>The path to the opened file.</summary>
private readonly string _path;
private readonly string? _path;

/// <summary>The next available byte to be read from the _buffer.</summary>
private int _readPos;
Expand Down Expand Up @@ -53,7 +54,7 @@ public partial class FileStream : Stream
private readonly bool _useAsyncIO;

/// <summary>cached task for read ops that complete synchronously</summary>
private Task<int> _lastSynchronouslyCompletedTask = null;
private Task<int>? _lastSynchronouslyCompletedTask = null;

/// <summary>
/// Currently cached position in the stream. This should always mirror the underlying file's actual position,
Expand Down Expand Up @@ -191,7 +192,7 @@ public FileStream(string path, FileMode mode, FileAccess access, FileShare share

// don't include inheritable in our bounds check for share
FileShare tempshare = share & ~FileShare.Inheritable;
string badArg = null;
string? badArg = null;

if (mode < FileMode.CreateNew || mode > FileMode.Append)
badArg = nameof(mode);
Expand Down Expand Up @@ -248,7 +249,7 @@ public FileStream(string path, FileMode mode, FileAccess access, FileShare share
// If anything goes wrong while setting up the stream, make sure we deterministically dispose
// of the opened handle.
_fileHandle.Dispose();
_fileHandle = null;
_fileHandle = null!;
throw;
}
}
Expand Down Expand Up @@ -377,15 +378,15 @@ public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken
throw Error.GetFileNotOpen();
}

Task<int> t = ReadAsyncInternal(buffer, cancellationToken, out int synchronousResult);
Task<int>? t = ReadAsyncInternal(buffer, cancellationToken, out int synchronousResult);
return t != null ?
new ValueTask<int>(t) :
new ValueTask<int>(synchronousResult);
}

private Task<int> ReadAsyncTask(byte[] array, int offset, int count, CancellationToken cancellationToken)
{
Task<int> t = ReadAsyncInternal(new Memory<byte>(array, offset, count), cancellationToken, out int synchronousResult);
Task<int>? t = ReadAsyncInternal(new Memory<byte>(array, offset, count), cancellationToken, out int synchronousResult);

if (t == null)
{
Expand Down Expand Up @@ -818,7 +819,7 @@ private void PrepareForWriting()
Dispose(false);
}

public override IAsyncResult BeginRead(byte[] array, int offset, int numBytes, AsyncCallback callback, object state)
public override IAsyncResult BeginRead(byte[] array, int offset, int numBytes, AsyncCallback callback, object? state)
{
if (array == null)
throw new ArgumentNullException(nameof(array));
Expand All @@ -838,7 +839,7 @@ public override IAsyncResult BeginRead(byte[] array, int offset, int numBytes, A
return TaskToApm.Begin(ReadAsyncTask(array, offset, numBytes, CancellationToken.None), callback, state);
}

public override IAsyncResult BeginWrite(byte[] array, int offset, int numBytes, AsyncCallback callback, object state)
public override IAsyncResult BeginWrite(byte[] array, int offset, int numBytes, AsyncCallback callback, object? state)
{
if (array == null)
throw new ArgumentNullException(nameof(array));
Expand Down
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 All @@ -24,7 +25,7 @@ private unsafe class FileStreamCompletionSource : TaskCompletionSource<int>
private const long CompletedCallback = (long)8 << 32;
private const ulong ResultMask = ((ulong)uint.MaxValue) << 32;

private static Action<object> s_cancelCallback;
private static Action<object?> s_cancelCallback;

private readonly FileStream _stream;
private readonly int _numBufferedBytes;
Expand All @@ -36,7 +37,7 @@ private unsafe class FileStreamCompletionSource : TaskCompletionSource<int>
private long _result; // Using long since this needs to be used in Interlocked APIs

// Using RunContinuationsAsynchronously for compat reasons (old API used Task.Factory.StartNew for continuations)
protected FileStreamCompletionSource(FileStream stream, int numBufferedBytes, byte[] bytes)
protected FileStreamCompletionSource(FileStream stream, int numBufferedBytes, byte[]? bytes)
: base(TaskCreationOptions.RunContinuationsAsynchronously)
{
_numBufferedBytes = numBufferedBytes;
Expand All @@ -48,12 +49,12 @@ protected FileStreamCompletionSource(FileStream stream, int numBufferedBytes, by
// thus is already pinned) and if no one else is currently using the preallocated overlapped. This is the fast-path
// for cases where the user-provided buffer is smaller than the FileStream's buffer (such that the FileStream's
// buffer is used) and where operations on the FileStream are not being performed concurrently.
Debug.Assert((bytes == null || ReferenceEquals(bytes, _stream._buffer)));
Debug.Assert(bytes == null || ReferenceEquals(bytes, _stream._buffer));

// The _preallocatedOverlapped is null if the internal buffer was never created, so we check for
// a non-null bytes before using the stream's _preallocatedOverlapped
_overlapped = bytes != null && _stream.CompareExchangeCurrentOverlappedOwner(this, null) == null ?
_stream._fileHandle.ThreadPoolBinding.AllocateNativeOverlapped(_stream._preallocatedOverlapped) :
_stream._fileHandle.ThreadPoolBinding.AllocateNativeOverlapped(_stream._preallocatedOverlapped!) : // allocated when buffer was created, and buffer is non-null
_stream._fileHandle.ThreadPoolBinding.AllocateNativeOverlapped(s_ioCallback, this, bytes);
Debug.Assert(_overlapped != null, "AllocateNativeOverlapped returned null");
}
Expand Down Expand Up @@ -132,14 +133,16 @@ internal virtual void ReleaseNativeResource()
internal static unsafe void IOCallback(uint errorCode, uint numBytes, NativeOverlapped* pOverlapped)
{
// Extract the completion source from the overlapped. The state in the overlapped
// will either be a Win32FileStream (in the case where the preallocated overlapped was used),
// will either be a FileStream (in the case where the preallocated overlapped was used),
// in which case the operation being completed is its _currentOverlappedOwner, or it'll
// be directly the FileStreamCompletion that's completing (in the case where the preallocated
// be directly the FileStreamCompletionSource that's completing (in the case where the preallocated
// overlapped was already in use by another operation).
object state = ThreadPoolBoundHandle.GetNativeOverlappedState(pOverlapped);
object? state = ThreadPoolBoundHandle.GetNativeOverlappedState(pOverlapped);
Debug.Assert(state is FileStream || state is FileStreamCompletionSource);
FileStreamCompletionSource completionSource = state is FileStream fs ?
fs._currentOverlappedOwner :
(FileStreamCompletionSource)state;
fs._currentOverlappedOwner! : // must be owned
(FileStreamCompletionSource)state!;
Debug.Assert(completionSource != null);
Debug.Assert(completionSource._overlapped == pOverlapped, "Overlaps don't match");

// Handle reading from & writing to closed pipes. While I'm not sure
Expand Down Expand Up @@ -198,12 +201,12 @@ private void CompleteCallback(ulong packedResult)
}
}

private static void Cancel(object state)
private static void Cancel(object? state)
{
// WARNING: This may potentially be called under a lock (during cancellation registration)

FileStreamCompletionSource completionSource = state as FileStreamCompletionSource;
Debug.Assert(completionSource != null, "Unknown state passed to cancellation");
Debug.Assert(state is FileStreamCompletionSource, "Unknown state passed to cancellation");
FileStreamCompletionSource completionSource = (FileStreamCompletionSource)state;
Debug.Assert(completionSource._overlapped != null && !completionSource.Task.IsCompleted, "IO should not have completed yet");

// If the handle is still valid, attempt to cancel the IO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
**
===========================================================*/

#nullable enable
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
Expand Down
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
namespace System.IO
{
/// <summary>Provides methods to help in the implementation of Stream-derived types.</summary>
Expand Down
7 changes: 4 additions & 3 deletions src/Common/src/CoreLib/System/IO/UnmanagedMemoryStream.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.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -39,15 +40,15 @@ namespace System.IO
/// </summary>
public class UnmanagedMemoryStream : Stream
{
private SafeBuffer _buffer;
private SafeBuffer? _buffer;
private unsafe byte* _mem;
private long _length;
private long _capacity;
private long _position;
private long _offset;
private FileAccess _access;
private bool _isOpen;
private Task<int> _lastReadTask; // The last successful task returned from ReadAsync
private Task<int>? _lastReadTask; // The last successful task returned from ReadAsync

/// <summary>
/// Creates a closed stream.
Expand Down Expand Up @@ -473,7 +474,7 @@ public override Task<int> ReadAsync(byte[] buffer, int offset, int count, Cancel
try
{
int n = Read(buffer, offset, count);
Task<int> t = _lastReadTask;
Task<int>? t = _lastReadTask;
return (t != null && t.Result == n) ? t : (_lastReadTask = Task.FromResult<Int32>(n));
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
**
===========================================================*/

using System;
using System.Runtime.InteropServices;
using System.Diagnostics.CodeAnalysis;
#nullable enable
using System.Threading;
using System.Threading.Tasks;

Expand Down

0 comments on commit 8e61998

Please sign in to comment.