Skip to content
This repository has been archived by the owner on Jan 14, 2021. It is now read-only.

[WIP][GSoC] CoreRT changes to import filestream into mono #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
// See the LICENSE file in the project root for more information.

using System;
#if MONO
using System.Diagnostics.Private;
#else
using System.Diagnostics;
#endif
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
// See the LICENSE file in the project root for more information.

using System;
#if MONO
using System.Diagnostics.Private;
#else
using System.Diagnostics;
#endif
using System.Diagnostics;
using System.Runtime.InteropServices;

Expand Down
2 changes: 2 additions & 0 deletions src/System.Private.CoreLib/shared/System/Buffers/ArrayPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public abstract class ArrayPool<T>
/// The shared pool instance is created lazily on first access.
/// </remarks>
public static ArrayPool<T> Shared { get; } =
#if !MONO
typeof(T) == typeof(byte) || typeof(T) == typeof(char) ? new TlsOverPerCoreLockedStacksArrayPool<T>() :
#endif
Create();

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions src/System.Private.CoreLib/shared/System/Buffers/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// See the LICENSE file in the project root for more information.

using System.Diagnostics;
#if MONO
using System.Diagnostics.Private;
#endif
using System.Runtime.CompilerServices;

namespace System.Buffers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

using Microsoft.Win32.SafeHandles;
using System.Diagnostics;
#if MONO
using System.Diagnostics.Private;
#endif
using System.Threading;
using System.Threading.Tasks;

Expand Down
6 changes: 5 additions & 1 deletion src/System.Private.CoreLib/shared/System/IO/FileStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Win32.SafeHandles;
#if MONO
using System.Diagnostics.Private;
#endif
using System.Diagnostics;

namespace System.IO
Expand Down Expand Up @@ -161,7 +164,8 @@ public FileStream(string path, FileMode mode, FileAccess access, FileShare share
throw new ArgumentOutOfRangeException(badArg, SR.ArgumentOutOfRange_Enum);

// NOTE: any change to FileOptions enum needs to be matched here in the error validation
if (options != FileOptions.None && (options & ~(FileOptions.WriteThrough | FileOptions.Asynchronous | FileOptions.RandomAccess | FileOptions.DeleteOnClose | FileOptions.SequentialScan | FileOptions.Encrypted | (FileOptions)0x20000000 /* NoBuffering */)) != 0)
if (options != FileOptions.None && (options & ~(FileOptions.WriteThrough | FileOptions.Asynchronous | FileOptions.RandomAccess | FileOptions.DeleteOnClose
| FileOptions.SequentialScan | FileOptions.Encrypted | (FileOptions)0x20000000 /* NoBuffering */ | (FileOptions)1)) != 0)
throw new ArgumentOutOfRangeException(nameof(options), SR.ArgumentOutOfRange_Enum);

if (bufferSize <= 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// See the LICENSE file in the project root for more information.

using System.Diagnostics;
#if MONO
using System.Diagnostics.Private;
#endif
using System.Text;

namespace System.IO
Expand Down
3 changes: 3 additions & 0 deletions src/System.Private.CoreLib/shared/System/IO/PathInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// See the LICENSE file in the project root for more information.

using System.Diagnostics;
#if MONO
using System.Diagnostics.Private;
#endif
using System.Text;

namespace System.IO
Expand Down
89 changes: 89 additions & 0 deletions src/System.Private.CoreLib/src/System/IO/Stream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
// See the LICENSE file in the project root for more information.

using System.Diagnostics;
#if MONO
using System.Diagnostics.Private;
using System.Runtime.ExceptionServices;
#endif
using System.Diagnostics.Contracts;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -738,5 +742,90 @@ public override void EndWrite(IAsyncResult asyncResult)
_stream.EndWrite(asyncResult);
}
}

#if MONO
/// <summary>Used as the IAsyncResult object when using asynchronous IO methods on the base Stream class.</summary>
internal sealed class SynchronousAsyncResult : IAsyncResult {

private readonly Object _stateObject;
private readonly bool _isWrite;
private ManualResetEvent _waitHandle;
private ExceptionDispatchInfo _exceptionInfo;

private bool _endXxxCalled;
private Int32 _bytesRead;

internal SynchronousAsyncResult(Int32 bytesRead, Object asyncStateObject) {
_bytesRead = bytesRead;
_stateObject = asyncStateObject;
//_isWrite = false;
}

internal SynchronousAsyncResult(Object asyncStateObject) {
_stateObject = asyncStateObject;
_isWrite = true;
}

internal SynchronousAsyncResult(Exception ex, Object asyncStateObject, bool isWrite) {
_exceptionInfo = ExceptionDispatchInfo.Capture(ex);
_stateObject = asyncStateObject;
_isWrite = isWrite;
}

public bool IsCompleted {
// We never hand out objects of this type to the user before the synchronous IO completed:
get { return true; }
}

public WaitHandle AsyncWaitHandle {
get {
return LazyInitializer.EnsureInitialized(ref _waitHandle, () => new ManualResetEvent(true));
}
}

public Object AsyncState {
get { return _stateObject; }
}

public bool CompletedSynchronously {
get { return true; }
}

internal void ThrowIfError() {
if (_exceptionInfo != null)
_exceptionInfo.Throw();
}

internal static Int32 EndRead(IAsyncResult asyncResult) {

SynchronousAsyncResult ar = asyncResult as SynchronousAsyncResult;
if (ar == null || ar._isWrite)
__Error.WrongAsyncResult();

if (ar._endXxxCalled)
__Error.EndReadCalledTwice();

ar._endXxxCalled = true;

ar.ThrowIfError();
return ar._bytesRead;
}

internal static void EndWrite(IAsyncResult asyncResult) {

SynchronousAsyncResult ar = asyncResult as SynchronousAsyncResult;
if (ar == null || !ar._isWrite)
__Error.WrongAsyncResult();

if (ar._endXxxCalled)
__Error.EndWriteCalledTwice();

ar._endXxxCalled = true;

ar.ThrowIfError();
}
} // class SynchronousAsyncResult
#endif

}
}