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

Commit 9dd7d6d

Browse files
authored
Move TaskToApm to shared CoreLib partition (#15113)
- Get TaskToApm in sync with CoreFX copy and move it to shared CoreLib partition - Delete redundant __Error file - Delete remaining uses of InternalBlockCopy and replace it with BlockCopy
1 parent a16fdf0 commit 9dd7d6d

File tree

15 files changed

+96
-235
lines changed

15 files changed

+96
-235
lines changed

src/mscorlib/Resources/Strings.resx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2428,9 +2428,6 @@
24282428
<data name="IndexOutOfRange_ArrayRankIndex" xml:space="preserve">
24292429
<value>Array does not have that many dimensions.</value>
24302430
</data>
2431-
<data name="IndexOutOfRange_IORaceCondition" xml:space="preserve">
2432-
<value>Probable I/O race condition detected while copying memory. The I/O package is not thread safe by default. In multithreaded applications, a stream must be accessed in a thread-safe way, such as a thread-safe wrapper returned by TextReader's or TextWriter's Synchronized methods. This also applies to classes like StreamWriter and StreamReader.</value>
2433-
</data>
24342431
<data name="IndexOutOfRange_UMSPosition" xml:space="preserve">
24352432
<value>Unmanaged memory stream position was beyond the capacity of the stream.</value>
24362433
</data>
@@ -3700,4 +3697,4 @@
37003697
<data name="Arg_TypeNotSupported" xml:space="preserve">
37013698
<value>Specified type is not supported</value>
37023699
</data>
3703-
</root>
3700+
</root>

src/mscorlib/System.Private.CoreLib.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,6 @@
504504
<Compile Include="$(BclSourcesRoot)\System\Threading\Tasks\ConcurrentExclusiveSchedulerPair.cs" />
505505
<Compile Include="$(BclSourcesRoot)\System\Threading\Tasks\ProducerConsumerQueues.cs" />
506506
<Compile Include="$(BclSourcesRoot)\System\Threading\Tasks\TPLETWProvider.cs" />
507-
<Compile Include="$(BclSourcesRoot)\System\Threading\Tasks\TaskToApm.cs" />
508507
<Compile Condition="'$(FeatureCominterop)' == 'true'" Include="$(BclSourcesRoot)\System\Threading\Tasks\IAsyncCausalityTracerStatics.cs" />
509508
</ItemGroup>
510509
<ItemGroup>
@@ -513,7 +512,6 @@
513512
<Compile Include="$(BclSourcesRoot)\System\Threading\ClrThreadPoolPreAllocatedOverlapped.cs" />
514513
</ItemGroup>
515514
<ItemGroup>
516-
<Compile Include="$(BclSourcesRoot)\System\IO\__Error.cs" />
517515
<Compile Include="$(BclSourcesRoot)\System\IO\BinaryReader.cs" />
518516
<Compile Include="$(BclSourcesRoot)\System\IO\Directory.cs" />
519517
<Compile Include="$(BclSourcesRoot)\System\IO\SearchOption.cs" />

src/mscorlib/shared/System.Private.CoreLib.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@
510510
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\SynchronizationLockException.cs" />
511511
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\Tasks\TaskCanceledException.cs" />
512512
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\Tasks\TaskExtensions.cs" />
513+
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\Tasks\TaskToApm.cs" />
513514
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\Tasks\TaskSchedulerException.cs" />
514515
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\Tasks\ValueTask.cs" />
515516
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\ThreadAbortException.cs" />

src/mscorlib/src/System/Threading/Tasks/TaskToApm.cs renamed to src/mscorlib/shared/System/Threading/Tasks/TaskToApm.cs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
// =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
6-
//
7-
//
8-
//
95
// Helper methods for using Tasks to implement the APM pattern.
106
//
117
// Example usage, wrapping a Task<int>-returning FooAsync method with Begin/EndFoo methods:
8+
//
129
// public IAsyncResult BeginFoo(..., AsyncCallback callback, object state)
1310
// {
1411
// Task<int> t = FooAsync(...);
@@ -18,10 +15,7 @@
1815
// {
1916
// return TaskToApm.End<int>(asyncResult);
2017
// }
21-
//
22-
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
2318

24-
using System.IO;
2519
using System.Diagnostics;
2620

2721
namespace System.Threading.Tasks
@@ -48,19 +42,19 @@ public static IAsyncResult Begin(Task task, AsyncCallback callback, object state
4842
IAsyncResult asyncResult;
4943
if (task.IsCompleted)
5044
{
51-
// Synchronous completion
45+
// Synchronous completion.
5246
asyncResult = new TaskWrapperAsyncResult(task, state, completedSynchronously: true);
53-
if (callback != null)
54-
callback(asyncResult);
47+
callback?.Invoke(asyncResult);
5548
}
56-
// Otherwise, we need to schedule a callback. Whether we can use the Task as the IAsyncResult
57-
// depends on whether the Task's AsyncState has reference equality with the requested state.
5849
else
5950
{
60-
// Asynchronous completion
51+
// For asynchronous completion we need to schedule a callback. Whether we can use the Task as the IAsyncResult
52+
// depends on whether the Task's AsyncState has reference equality with the requested state.
6153
asyncResult = task.AsyncState == state ? (IAsyncResult)task : new TaskWrapperAsyncResult(task, state, completedSynchronously: false);
6254
if (callback != null)
55+
{
6356
InvokeCallbackWhenTaskCompletes(task, callback, asyncResult);
57+
}
6458
}
6559
return asyncResult;
6660
}
@@ -78,15 +72,18 @@ public static void End(IAsyncResult asyncResult)
7872
task = twar.Task;
7973
Debug.Assert(task != null, "TaskWrapperAsyncResult should never wrap a null Task.");
8074
}
81-
// Otherwise, the IAsyncResult should be a Task.
8275
else
8376
{
77+
// Otherwise, the IAsyncResult should be a Task.
8478
task = asyncResult as Task;
8579
}
8680

8781
// Make sure we actually got a task, then complete the operation by waiting on it.
8882
if (task == null)
89-
__Error.WrongAsyncResult();
83+
{
84+
throw new ArgumentNullException();
85+
}
86+
9087
task.GetAwaiter().GetResult();
9188
}
9289

@@ -103,15 +100,18 @@ public static TResult End<TResult>(IAsyncResult asyncResult)
103100
task = twar.Task as Task<TResult>;
104101
Debug.Assert(twar.Task != null, "TaskWrapperAsyncResult should never wrap a null Task.");
105102
}
106-
// Otherwise, the IAsyncResult should be a Task<TResult>.
107103
else
108104
{
105+
// Otherwise, the IAsyncResult should be a Task<TResult>.
109106
task = asyncResult as Task<TResult>;
110107
}
111108

112109
// Make sure we actually got a task, then complete the operation by waiting on it.
113110
if (task == null)
114-
__Error.WrongAsyncResult();
111+
{
112+
throw new ArgumentNullException();
113+
}
114+
115115
return task.GetAwaiter().GetResult();
116116
}
117117

@@ -158,9 +158,9 @@ private sealed class TaskWrapperAsyncResult : IAsyncResult
158158
/// <summary>The wrapped Task.</summary>
159159
internal readonly Task Task;
160160
/// <summary>The new AsyncState value.</summary>
161-
private readonly object m_state;
161+
private readonly object _state;
162162
/// <summary>The new CompletedSynchronously value.</summary>
163-
private readonly bool m_completedSynchronously;
163+
private readonly bool _completedSynchronously;
164164

165165
/// <summary>Initializes the IAsyncResult with the Task to wrap and the overriding AsyncState and CompletedSynchronously values.</summary>
166166
/// <param name="task">The Task to wrap.</param>
@@ -172,16 +172,16 @@ internal TaskWrapperAsyncResult(Task task, object state, bool completedSynchrono
172172
Debug.Assert(!completedSynchronously || task.IsCompleted, "If completedSynchronously is true, the task must be completed.");
173173

174174
this.Task = task;
175-
m_state = state;
176-
m_completedSynchronously = completedSynchronously;
175+
_state = state;
176+
_completedSynchronously = completedSynchronously;
177177
}
178178

179179
// The IAsyncResult implementation.
180180
// - IsCompleted and AsyncWaitHandle just pass through to the Task.
181181
// - AsyncState and CompletedSynchronously return the corresponding values stored in this object.
182182

183-
object IAsyncResult.AsyncState { get { return m_state; } }
184-
bool IAsyncResult.CompletedSynchronously { get { return m_completedSynchronously; } }
183+
object IAsyncResult.AsyncState { get { return _state; } }
184+
bool IAsyncResult.CompletedSynchronously { get { return _completedSynchronously; } }
185185
bool IAsyncResult.IsCompleted { get { return this.Task.IsCompleted; } }
186186
WaitHandle IAsyncResult.AsyncWaitHandle { get { return ((IAsyncResult)this.Task).AsyncWaitHandle; } }
187187
}

src/mscorlib/src/System/Buffer.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,6 @@ public static class Buffer
3535
public static extern void BlockCopy(Array src, int srcOffset,
3636
Array dst, int dstOffset, int count);
3737

38-
// A very simple and efficient memmove that assumes all of the
39-
// parameter validation has already been done. The count and offset
40-
// parameters here are in bytes. If you want to use traditional
41-
// array element indices and counts, use Array.Copy.
42-
[MethodImplAttribute(MethodImplOptions.InternalCall)]
43-
internal static extern void InternalBlockCopy(Array src, int srcOffsetBytes,
44-
Array dst, int dstOffsetBytes, int byteCount);
45-
4638
// This is ported from the optimized CRT assembly in memchr.asm. The JIT generates
4739
// pretty good code here and this ends up being within a couple % of the CRT asm.
4840
// It is however cross platform as the CRT hasn't ported their fast version to 64-bit

src/mscorlib/src/System/IO/BinaryReader.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public void Dispose()
117117

118118
public virtual int PeekChar()
119119
{
120-
if (_stream == null) __Error.FileNotOpen();
120+
if (_stream == null) throw Error.GetFileNotOpen();
121121

122122
if (!_stream.CanSeek)
123123
return -1;
@@ -131,7 +131,7 @@ public virtual int Read()
131131
{
132132
if (_stream == null)
133133
{
134-
__Error.FileNotOpen();
134+
throw Error.GetFileNotOpen();
135135
}
136136
return InternalReadOneChar();
137137
}
@@ -145,11 +145,11 @@ public virtual bool ReadBoolean()
145145
public virtual byte ReadByte()
146146
{
147147
// Inlined to avoid some method call overhead with FillBuffer.
148-
if (_stream == null) __Error.FileNotOpen();
148+
if (_stream == null) throw Error.GetFileNotOpen();
149149

150150
int b = _stream.ReadByte();
151151
if (b == -1)
152-
__Error.EndOfFile();
152+
throw Error.GetEndOfFile();
153153
return (byte)b;
154154
}
155155

@@ -165,7 +165,7 @@ public virtual char ReadChar()
165165
int value = Read();
166166
if (value == -1)
167167
{
168-
__Error.EndOfFile();
168+
throw Error.GetEndOfFile();
169169
}
170170
return (char)value;
171171
}
@@ -187,7 +187,7 @@ public virtual int ReadInt32()
187187
{
188188
if (_isMemoryStream)
189189
{
190-
if (_stream == null) __Error.FileNotOpen();
190+
if (_stream == null) throw Error.GetFileNotOpen();
191191
// read directly from MemoryStream buffer
192192
MemoryStream mStream = _stream as MemoryStream;
193193
Debug.Assert(mStream != null, "_stream as MemoryStream != null");
@@ -265,7 +265,7 @@ public virtual decimal ReadDecimal()
265265
public virtual String ReadString()
266266
{
267267
if (_stream == null)
268-
__Error.FileNotOpen();
268+
throw Error.GetFileNotOpen();
269269

270270
int currPos = 0;
271271
int n;
@@ -303,7 +303,7 @@ public virtual String ReadString()
303303
n = _stream.Read(_charBytes, 0, readLength);
304304
if (n == 0)
305305
{
306-
__Error.EndOfFile();
306+
throw Error.GetEndOfFile();
307307
}
308308

309309
charsRead = _decoder.GetChars(_charBytes, 0, n, _charBuffer, 0);
@@ -340,7 +340,7 @@ public virtual int Read(char[] buffer, int index, int count)
340340
}
341341

342342
if (_stream == null)
343-
__Error.FileNotOpen();
343+
throw Error.GetFileNotOpen();
344344

345345
// SafeCritical: index and count have already been verified to be a valid range for the buffer
346346
return InternalReadChars(new Span<char>(buffer, index, count));
@@ -349,7 +349,7 @@ public virtual int Read(char[] buffer, int index, int count)
349349
public virtual int Read(Span<char> buffer)
350350
{
351351
if (_stream == null)
352-
__Error.FileNotOpen();
352+
throw Error.GetFileNotOpen();
353353

354354
return InternalReadChars(buffer);
355355
}
@@ -524,7 +524,7 @@ public virtual char[] ReadChars(int count)
524524
}
525525
if (_stream == null)
526526
{
527-
__Error.FileNotOpen();
527+
throw Error.GetFileNotOpen();
528528
}
529529

530530
if (count == 0)
@@ -538,7 +538,7 @@ public virtual char[] ReadChars(int count)
538538
if (n != count)
539539
{
540540
char[] copy = new char[n];
541-
Buffer.InternalBlockCopy(chars, 0, copy, 0, 2 * n); // sizeof(char)
541+
Buffer.BlockCopy(chars, 0, copy, 0, 2 * n); // sizeof(char)
542542
chars = copy;
543543
}
544544

@@ -556,22 +556,22 @@ public virtual int Read(byte[] buffer, int index, int count)
556556
if (buffer.Length - index < count)
557557
throw new ArgumentException(SR.Argument_InvalidOffLen);
558558

559-
if (_stream == null) __Error.FileNotOpen();
559+
if (_stream == null) throw Error.GetFileNotOpen();
560560
return _stream.Read(buffer, index, count);
561561
}
562562

563563
public virtual int Read(Span<byte> buffer)
564564
{
565565
if (_stream == null)
566-
__Error.FileNotOpen();
566+
throw Error.GetFileNotOpen();
567567

568568
return _stream.Read(buffer);
569569
}
570570

571571
public virtual byte[] ReadBytes(int count)
572572
{
573573
if (count < 0) throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum);
574-
if (_stream == null) __Error.FileNotOpen();
574+
if (_stream == null) throw Error.GetFileNotOpen();
575575

576576
if (count == 0)
577577
{
@@ -594,7 +594,7 @@ public virtual byte[] ReadBytes(int count)
594594
{
595595
// Trim array. This should happen on EOF & possibly net streams.
596596
byte[] copy = new byte[numRead];
597-
Buffer.InternalBlockCopy(result, 0, copy, 0, numRead);
597+
Buffer.BlockCopy(result, 0, copy, 0, numRead);
598598
result = copy;
599599
}
600600

@@ -610,7 +610,7 @@ protected virtual void FillBuffer(int numBytes)
610610
int bytesRead = 0;
611611
int n = 0;
612612

613-
if (_stream == null) __Error.FileNotOpen();
613+
if (_stream == null) throw Error.GetFileNotOpen();
614614

615615
// Need to find a good threshold for calling ReadByte() repeatedly
616616
// vs. calling Read(byte[], int, int) for both buffered & unbuffered
@@ -619,7 +619,7 @@ protected virtual void FillBuffer(int numBytes)
619619
{
620620
n = _stream.ReadByte();
621621
if (n == -1)
622-
__Error.EndOfFile();
622+
throw Error.GetEndOfFile();
623623
_buffer[0] = (byte)n;
624624
return;
625625
}
@@ -629,7 +629,7 @@ protected virtual void FillBuffer(int numBytes)
629629
n = _stream.Read(_buffer, bytesRead, numBytes - bytesRead);
630630
if (n == 0)
631631
{
632-
__Error.EndOfFile();
632+
throw Error.GetEndOfFile();
633633
}
634634
bytesRead += n;
635635
} while (bytesRead < numBytes);

src/mscorlib/src/System/IO/File.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public static byte[] ReadAllBytes(String path)
9999
{
100100
int n = fs.Read(bytes, index, count);
101101
if (n == 0)
102-
__Error.EndOfFile();
102+
throw Error.GetEndOfFile();
103103
index += n;
104104
count -= n;
105105
}

0 commit comments

Comments
 (0)