Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 0c677d5

Browse files
committed
Merge pull request #2737 from ianhays/overlap
Removed USE_OVERLAPPED from FileSystem
2 parents fe9f9c5 + a227ab4 commit 0c677d5

File tree

4 files changed

+3
-91
lines changed

4 files changed

+3
-91
lines changed

src/System.IO.FileSystem/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Windows.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ internal bool? IsAsync
3939
}
4040
}
4141

42-
#if USE_OVERLAPPED
4342
internal ThreadPoolBoundHandle ThreadPoolBinding { get; set; }
44-
#endif
4543

4644
[System.Security.SecurityCritical]
4745
override protected bool ReleaseHandle()

src/System.IO.FileSystem/src/System.IO.FileSystem.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
<PropertyGroup Condition="'$(TargetsUnix)' == 'true'">
1414
<NoWarn>$(NoWarn);414</NoWarn>
1515
</PropertyGroup>
16-
<PropertyGroup Condition="'$(TargetsWindows)' == 'true'">
17-
<!-- The System.Threading.Overlapped contract currently only works on Windows -->
18-
<DefineConstants>$(DefineConstants);USE_OVERLAPPED</DefineConstants>
19-
</PropertyGroup>
2016
<PropertyGroup>
2117
<TargetsBSD Condition="'$(TargetsOSX)' == 'true' or '$(TargetsFreeBSD)' == 'true'">true</TargetsBSD>
2218
</PropertyGroup>

src/System.IO.FileSystem/src/System/IO/Win32FileStream.cs

Lines changed: 3 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,12 @@ namespace System.IO
4141
internal sealed partial class Win32FileStream : FileStreamBase
4242
{
4343
internal const int DefaultBufferSize = 4096;
44-
#if USE_OVERLAPPED
4544
internal const bool DefaultUseAsync = true;
46-
#else
47-
internal const bool DefaultUseAsync = false;
48-
#endif
4945
internal const bool DefaultIsAsync = false;
5046

5147
private byte[] _buffer; // Shared read/write buffer. Alloc on first use.
5248
private String _fileName; // Fully qualified file name.
53-
#if USE_OVERLAPPED
5449
private bool _isAsync; // Whether we opened the handle for overlapped IO
55-
#endif
5650
private bool _canRead;
5751
private bool _canWrite;
5852
private bool _canSeek;
@@ -95,13 +89,8 @@ private void Init(String path, FileMode mode, FileAccess access, FileShare share
9589
if (mode == FileMode.Append)
9690
mode = FileMode.OpenOrCreate;
9791

98-
#if USE_OVERLAPPED
9992
if ((options & FileOptions.Asynchronous) != 0)
10093
_isAsync = true;
101-
#else
102-
// Disallow overlapped IO.
103-
options &= ~FileOptions.Asynchronous;
104-
#endif
10594

10695
int flagsAndAttributes = (int)options;
10796

@@ -115,9 +104,7 @@ private void Init(String path, FileMode mode, FileAccess access, FileShare share
115104
try
116105
{
117106
_handle = Interop.mincore.SafeCreateFile(path, fAccess, share, ref secAttrs, mode, flagsAndAttributes, IntPtr.Zero);
118-
#if USE_OVERLAPPED
119107
_handle.IsAsync = _isAsync;
120-
#endif
121108

122109
if (_handle.IsInvalid)
123110
{
@@ -150,7 +137,6 @@ private void Init(String path, FileMode mode, FileAccess access, FileShare share
150137
throw new NotSupportedException(SR.NotSupported_FileStreamOnNonFiles);
151138
}
152139

153-
#if USE_OVERLAPPED
154140
// This is necessary for async IO using IO Completion ports via our
155141
// managed Threadpool API's. This (theoretically) calls the OS's
156142
// BindIoCompletionCallback method, and passes in a stub for the
@@ -179,7 +165,6 @@ private void Init(String path, FileMode mode, FileAccess access, FileShare share
179165
}
180166
}
181167
}
182-
#endif
183168

184169
_canRead = (access & FileAccess.Read) != 0;
185170
_canWrite = (access & FileAccess.Write) != 0;
@@ -240,9 +225,7 @@ private Win32FileStream(SafeFileHandle handle, FileAccess access, int bufferSize
240225
int handleType = Interop.mincore.GetFileType(_handle);
241226
Debug.Assert(handleType == Interop.mincore.FileTypes.FILE_TYPE_DISK || handleType == Interop.mincore.FileTypes.FILE_TYPE_PIPE || handleType == Interop.mincore.FileTypes.FILE_TYPE_CHAR, "FileStream was passed an unknown file type!");
242227

243-
#if USE_OVERLAPPED
244228
_isAsync = isAsync;
245-
#endif
246229
_canRead = 0 != (access & FileAccess.Read);
247230
_canWrite = 0 != (access & FileAccess.Write);
248231
_canSeek = handleType == Interop.mincore.FileTypes.FILE_TYPE_DISK;
@@ -253,7 +236,6 @@ private Win32FileStream(SafeFileHandle handle, FileAccess access, int bufferSize
253236
_fileName = null;
254237
_isPipe = handleType == Interop.mincore.FileTypes.FILE_TYPE_PIPE;
255238

256-
#if USE_OVERLAPPED
257239
// This is necessary for async IO using IO Completion ports via our
258240
// managed Threadpool API's. This calls the OS's
259241
// BindIoCompletionCallback method, and passes in a stub for the
@@ -280,23 +262,11 @@ private Win32FileStream(SafeFileHandle handle, FileAccess access, int bufferSize
280262
}
281263
}
282264
else if (!_isAsync)
283-
#endif
284265
{
285266
if (handleType != Interop.mincore.FileTypes.FILE_TYPE_PIPE)
286267
VerifyHandleIsSync();
287268
}
288269

289-
#if !USE_OVERLAPPED
290-
// When USE_OVERLAPPED is not supported we ignore the isAsync property and always call
291-
// VerifyHandleIsSync above. We'll then throw here if we were told it was Async.
292-
293-
if (isAsync)
294-
{
295-
// Passed in a synchronous handle and told us isAsync
296-
throw new ArgumentException(SR.Arg_HandleNotAsync, "handle");
297-
}
298-
#endif
299-
300270
if (_canSeek)
301271
SeekCore(0, SeekOrigin.Current);
302272
else
@@ -385,11 +355,7 @@ public override bool CanSeek
385355

386356
public override bool IsAsync
387357
{
388-
#if USE_OVERLAPPED
389358
get { return _isAsync; }
390-
#else
391-
get { return false; }
392-
#endif
393359
}
394360

395361
public override long Length
@@ -483,10 +449,8 @@ protected override void Dispose(bool disposing)
483449
if (_handle != null && !_handle.IsClosed)
484450
_handle.Dispose();
485451

486-
#if USE_OVERLAPPED
487452
if (_handle.ThreadPoolBinding != null)
488453
_handle.ThreadPoolBinding.Dispose();
489-
#endif
490454

491455
_canRead = false;
492456
_canWrite = false;
@@ -563,7 +527,6 @@ private void FlushWrite(bool calledFromFinalizer)
563527
{
564528
Debug.Assert(_readPos == 0 && _readLen == 0, "FileStream: Read buffer must be empty in FlushWrite!");
565529

566-
#if USE_OVERLAPPED
567530
if (_isAsync)
568531
{
569532
Task writeTask = WriteInternalCoreAsync(_buffer, 0, _writePos, CancellationToken.None);
@@ -586,7 +549,6 @@ private void FlushWrite(bool calledFromFinalizer)
586549
}
587550
}
588551
else
589-
#endif
590552
{
591553
WriteCore(_buffer, 0, _writePos);
592554
}
@@ -755,23 +717,18 @@ private unsafe int ReadCore(byte[] buffer, int offset, int count)
755717
Debug.Assert(_writePos == 0, "_writePos == 0");
756718
Debug.Assert(offset >= 0, "offset is negative");
757719
Debug.Assert(count >= 0, "count is negative");
758-
#if USE_OVERLAPPED
759720
if (_isAsync)
760721
{
761722
return ReadInternalCoreAsync(buffer, offset, count, 0, CancellationToken.None).GetAwaiter().GetResult();
762723
}
763-
#endif
764724

765725
// Make sure we are reading from the right spot
766726
if (_exposedHandle)
767727
VerifyOSHandlePosition();
768728

769729
int errorCode = 0;
770-
#if USE_OVERLAPPED
771730
int r = ReadFileNative(_handle, buffer, offset, count, null, out errorCode);
772-
#else
773-
int r = ReadFileNative(_handle, buffer, offset, count, out errorCode);
774-
#endif
731+
775732
if (r == -1)
776733
{
777734
// For pipes, ERROR_BROKEN_PIPE is the normal end of the pipe.
@@ -977,13 +934,11 @@ public override void Write(byte[] array, int offset, int count)
977934
// Reset our buffer. We essentially want to call FlushWrite
978935
// without calling Flush on the underlying Stream.
979936

980-
#if USE_OVERLAPPED
981937
if (_isAsync)
982938
{
983939
WriteInternalCoreAsync(_buffer, 0, _writePos, CancellationToken.None).GetAwaiter().GetResult();
984940
}
985941
else
986-
#endif
987942
{
988943
WriteCore(_buffer, 0, _writePos);
989944
}
@@ -1015,24 +970,18 @@ private unsafe void WriteCore(byte[] buffer, int offset, int count)
1015970
Debug.Assert(_readPos == _readLen, "_readPos == _readLen");
1016971
Debug.Assert(offset >= 0, "offset is negative");
1017972
Debug.Assert(count >= 0, "count is negative");
1018-
#if USE_OVERLAPPED
1019973
if (_isAsync)
1020974
{
1021975
WriteInternalCoreAsync(buffer, offset, count, CancellationToken.None).GetAwaiter().GetResult();
1022976
return;
1023977
}
1024-
#endif
1025978

1026979
// Make sure we are writing to the position that we think we are
1027980
if (_exposedHandle)
1028981
VerifyOSHandlePosition();
1029982

1030983
int errorCode = 0;
1031-
#if USE_OVERLAPPED
1032984
int r = WriteFileNative(_handle, buffer, offset, count, null, out errorCode);
1033-
#else
1034-
int r = WriteFileNative(_handle, buffer, offset, count, out errorCode);
1035-
#endif
1036985

1037986
if (r == -1)
1038987
{
@@ -1057,7 +1006,6 @@ private unsafe void WriteCore(byte[] buffer, int offset, int count)
10571006
return;
10581007
}
10591008

1060-
#if USE_OVERLAPPED
10611009
[System.Security.SecuritySafeCritical] // auto-generated
10621010
private Task<int> ReadInternalAsync(byte[] array, int offset, int numBytes, CancellationToken cancellationToken)
10631011
{
@@ -1278,7 +1226,6 @@ unsafe private Task<int> ReadInternalCoreAsync(byte[] bytes, int offset, int num
12781226

12791227
return completionSource.Task;
12801228
}
1281-
#endif
12821229

12831230
// Reads a byte from the file stream. Returns the byte cast to an int
12841231
// or -1 if reading from the end of the stream.
@@ -1304,7 +1251,6 @@ public override int ReadByte()
13041251
return result;
13051252
}
13061253

1307-
#if USE_OVERLAPPED
13081254
[System.Security.SecuritySafeCritical] // auto-generated
13091255
private Task WriteInternalAsync(byte[] array, int offset, int numBytes, CancellationToken cancellationToken)
13101256
{
@@ -1469,7 +1415,6 @@ private unsafe Task WriteInternalCoreAsync(byte[] bytes, int offset, int numByte
14691415

14701416
return completionSource.Task;
14711417
}
1472-
#endif
14731418

14741419
[System.Security.SecuritySafeCritical] // auto-generated
14751420
public override void WriteByte(byte value)
@@ -1513,11 +1458,7 @@ public override void WriteByte(byte value)
15131458

15141459
// __ConsoleStream also uses this code.
15151460
[System.Security.SecurityCritical] // auto-generated
1516-
#if USE_OVERLAPPED
15171461
private unsafe int ReadFileNative(SafeFileHandle handle, byte[] bytes, int offset, int count, NativeOverlapped* overlapped, out int errorCode)
1518-
#else
1519-
private unsafe int ReadFileNative(SafeFileHandle handle, byte[] bytes, int offset, int count, out int errorCode)
1520-
#endif
15211462
{
15221463
Contract.Requires(handle != null, "handle != null");
15231464
Contract.Requires(offset >= 0, "offset >= 0");
@@ -1529,9 +1470,7 @@ private unsafe int ReadFileNative(SafeFileHandle handle, byte[] bytes, int offse
15291470
throw new IndexOutOfRangeException(SR.IndexOutOfRange_IORaceCondition);
15301471
Contract.EndContractBlock();
15311472

1532-
#if USE_OVERLAPPED
15331473
Debug.Assert((_isAsync && overlapped != null) || (!_isAsync && overlapped == null), "Async IO and overlapped parameters inconsistent in call to ReadFileNative.");
1534-
#endif
15351474

15361475
// You can't use the fixed statement on an array of length 0.
15371476
if (bytes.Length == 0)
@@ -1545,12 +1484,10 @@ private unsafe int ReadFileNative(SafeFileHandle handle, byte[] bytes, int offse
15451484

15461485
fixed (byte* p = bytes)
15471486
{
1548-
#if USE_OVERLAPPED
15491487
if (_isAsync)
15501488
r = Interop.mincore.ReadFile(handle, p + offset, count, IntPtr.Zero, overlapped);
15511489
else
1552-
#endif
1553-
r = Interop.mincore.ReadFile(handle, p + offset, count, out numBytesRead, IntPtr.Zero);
1490+
r = Interop.mincore.ReadFile(handle, p + offset, count, out numBytesRead, IntPtr.Zero);
15541491
}
15551492

15561493
if (r == 0)
@@ -1566,13 +1503,8 @@ private unsafe int ReadFileNative(SafeFileHandle handle, byte[] bytes, int offse
15661503
}
15671504

15681505
[System.Security.SecurityCritical] // auto-generated
1569-
#if USE_OVERLAPPED
15701506
private unsafe int WriteFileNative(SafeFileHandle handle, byte[] bytes, int offset, int count, NativeOverlapped* overlapped, out int errorCode)
15711507
{
1572-
#else
1573-
private unsafe int WriteFileNative(SafeFileHandle handle, byte[] bytes, int offset, int count, out int errorCode)
1574-
{
1575-
#endif
15761508
Contract.Requires(handle != null, "handle != null");
15771509
Contract.Requires(offset >= 0, "offset >= 0");
15781510
Contract.Requires(count >= 0, "count >= 0");
@@ -1585,9 +1517,7 @@ private unsafe int WriteFileNative(SafeFileHandle handle, byte[] bytes, int offs
15851517
throw new IndexOutOfRangeException(SR.IndexOutOfRange_IORaceCondition);
15861518
Contract.EndContractBlock();
15871519

1588-
#if USE_OVERLAPPED
15891520
Debug.Assert((_isAsync && overlapped != null) || (!_isAsync && overlapped == null), "Async IO and overlapped parameters inconsistent in call to WriteFileNative.");
1590-
#endif
15911521

15921522
// You can't use the fixed statement on an array of length 0.
15931523
if (bytes.Length == 0)
@@ -1601,12 +1531,10 @@ private unsafe int WriteFileNative(SafeFileHandle handle, byte[] bytes, int offs
16011531

16021532
fixed (byte* p = bytes)
16031533
{
1604-
#if USE_OVERLAPPED
16051534
if (_isAsync)
16061535
r = Interop.mincore.WriteFile(handle, p + offset, count, IntPtr.Zero, overlapped);
16071536
else
1608-
#endif
1609-
r = Interop.mincore.WriteFile(handle, p + offset, count, out numBytesWritten, IntPtr.Zero);
1537+
r = Interop.mincore.WriteFile(handle, p + offset, count, out numBytesWritten, IntPtr.Zero);
16101538
}
16111539

16121540
if (r == 0)
@@ -1663,20 +1591,16 @@ public override Task<int> ReadAsync(Byte[] buffer, int offset, int count, Cancel
16631591
if (_handle.IsClosed)
16641592
throw __Error.GetFileNotOpen();
16651593

1666-
#if USE_OVERLAPPED
16671594
// If async IO is not supported on this platform or
16681595
// if this Win32FileStream was not opened with FileOptions.Asynchronous.
16691596
if (!_isAsync)
1670-
#endif
16711597
{
16721598
return base.ReadAsync(buffer, offset, count, cancellationToken);
16731599
}
1674-
#if USE_OVERLAPPED
16751600
else
16761601
{
16771602
return ReadInternalAsync(buffer, offset, count, cancellationToken);
16781603
}
1679-
#endif
16801604
}
16811605

16821606
[System.Security.SecuritySafeCritical]
@@ -1688,20 +1612,16 @@ public override Task WriteAsync(Byte[] buffer, int offset, int count, Cancellati
16881612
if (_handle.IsClosed)
16891613
throw __Error.GetFileNotOpen();
16901614

1691-
#if USE_OVERLAPPED
16921615
// If async IO is not supported on this platform or
16931616
// if this Win32FileStream was not opened with FileOptions.Asynchronous.
16941617
if (!_isAsync)
1695-
#endif
16961618
{
16971619
return base.WriteAsync(buffer, offset, count, cancellationToken);
16981620
}
1699-
#if USE_OVERLAPPED
17001621
else
17011622
{
17021623
return WriteInternalAsync(buffer, offset, count, cancellationToken);
17031624
}
1704-
#endif
17051625
}
17061626

17071627
// Unlike Flush(), FlushAsync() always flushes to disk. This is intentional.

src/System.IO.FileSystem/src/System/IO/Win32FileStreamCompletionSource.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ namespace System.IO
1111
{
1212
internal partial class Win32FileStream
1313
{
14-
#if USE_OVERLAPPED
1514
// This is an internal object extending TaskCompletionSource with fields
1615
// for all of the relevant data necessary to complete the IO operation.
1716
// This is used by AsyncFSCallback and all of the async methods.
@@ -208,6 +207,5 @@ private static void Cancel(object state)
208207
}
209208
}
210209
}
211-
#endif
212210
}
213211
}

0 commit comments

Comments
 (0)