Synchronize Task, Awaiter and ThreadPool implementation with CoreCLR (dotnet/coreclr...
authorJan Kotas <jkotas@microsoft.com>
Sat, 22 Dec 2018 06:49:21 +0000 (22:49 -0800)
committerJan Kotas <jkotas@microsoft.com>
Sat, 22 Dec 2018 14:55:41 +0000 (06:55 -0800)
Synchronized the code as much as possible with CoreCLR. The one major piece that I have excluded is tracing. I will look into doing that separately.

Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
Commit migrated from https://github.com/dotnet/coreclr/commit/0f8e9ee7a47423852b6112006e90504bc60e28e5

src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs
src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/ConfiguredValueTaskAwaitable.cs
src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/TaskAwaiter.cs
src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/ValueTaskAwaiter.cs
src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/YieldAwaitable.cs

index f7e3230..e149a08 100644 (file)
@@ -3623,7 +3623,7 @@ namespace System.Diagnostics.Tracing
         {
 #if !ES_BUILD_PCL
             msg = msg.TrimEnd('\r', '\n') +
-                    string.Format(CultureInfo.InvariantCulture, ", Thrd({0})" + Environment.NewLine, Thread.CurrentThread.ManagedThreadId);
+                    string.Format(CultureInfo.InvariantCulture, ", Thrd({0})" + Environment.NewLine, Environment.CurrentManagedThreadId);
             System.Diagnostics.Debugger.Log(0, null, msg);
 #endif
         }
index b874af2..66ad43e 100644 (file)
@@ -32,10 +32,7 @@ namespace System.Runtime.CompilerServices
 
         /// <summary>Provides an awaiter for a <see cref="ConfiguredValueTaskAwaitable"/>.</summary>
         [StructLayout(LayoutKind.Auto)]
-        public readonly struct ConfiguredValueTaskAwaiter : ICriticalNotifyCompletion
-#if CORECLR
-            , IStateMachineBoxAwareAwaiter
-#endif
+        public readonly struct ConfiguredValueTaskAwaiter : ICriticalNotifyCompletion, IStateMachineBoxAwareAwaiter
         {
             /// <summary>The value being awaited.</summary>
             private readonly ValueTask _value;
@@ -100,7 +97,6 @@ namespace System.Runtime.CompilerServices
                 }
             }
 
-#if CORECLR
             void IStateMachineBoxAwareAwaiter.AwaitUnsafeOnCompleted(IAsyncStateMachineBox box)
             {
                 object obj = _value._obj;
@@ -120,7 +116,6 @@ namespace System.Runtime.CompilerServices
                     TaskAwaiter.UnsafeOnCompletedInternal(Task.CompletedTask, box, _value._continueOnCapturedContext);
                 }
             }
-#endif
         }
     }
 
@@ -143,10 +138,7 @@ namespace System.Runtime.CompilerServices
 
         /// <summary>Provides an awaiter for a <see cref="ConfiguredValueTaskAwaitable{TResult}"/>.</summary>
         [StructLayout(LayoutKind.Auto)]
-        public readonly struct ConfiguredValueTaskAwaiter : ICriticalNotifyCompletion
-#if CORECLR
-            , IStateMachineBoxAwareAwaiter
-#endif
+        public readonly struct ConfiguredValueTaskAwaiter : ICriticalNotifyCompletion, IStateMachineBoxAwareAwaiter
         {
             /// <summary>The value being awaited.</summary>
             private readonly ValueTask<TResult> _value;
@@ -211,7 +203,6 @@ namespace System.Runtime.CompilerServices
                 }
             }
 
-#if CORECLR
             void IStateMachineBoxAwareAwaiter.AwaitUnsafeOnCompleted(IAsyncStateMachineBox box)
             {
                 object obj = _value._obj;
@@ -231,7 +222,6 @@ namespace System.Runtime.CompilerServices
                     TaskAwaiter.UnsafeOnCompletedInternal(Task.CompletedTask, box, _value._continueOnCapturedContext);
                 }
             }
-#endif
         }
     }
 }
index ac280b6..86380f9 100644 (file)
@@ -224,7 +224,6 @@ namespace System.Runtime.CompilerServices
             task.SetContinuationForAwait(continuation, continueOnCapturedContext, flowExecutionContext);
         }
 
-#if CORECLR
         /// <summary>Schedules the continuation onto the <see cref="System.Threading.Tasks.Task"/> associated with this <see cref="TaskAwaiter"/>.</summary>
         /// <param name="task">The task being awaited.</param>
         /// <param name="stateMachineBox">The box to invoke when the await operation completes.</param>
@@ -235,7 +234,13 @@ namespace System.Runtime.CompilerServices
 
             // If TaskWait* ETW events are enabled, trace a beginning event for this await
             // and set up an ending event to be traced when the asynchronous await completes.
-            if (TplEtwProvider.Log.IsEnabled() || Task.s_asyncDebuggingEnabled)
+            if (
+#if CORECLR
+                TplEtwProvider.Log.IsEnabled() || Task.s_asyncDebuggingEnabled
+#else
+                TaskTrace.Enabled
+#endif
+                )
             {
                 task.SetContinuationForAwait(OutputWaitEtwEvents(task, stateMachineBox.MoveNextAction), continueOnCapturedContext, flowExecutionContext: false);
             }
@@ -244,7 +249,7 @@ namespace System.Runtime.CompilerServices
                 task.UnsafeSetContinuationForAwait(stateMachineBox, continueOnCapturedContext);
             }
         }
-#endif
+
         /// <summary>
         /// Outputs a WaitBegin ETW event, and augments the continuation action to output a WaitEnd ETW event.
         /// </summary>
index db14806..56d8bca 100644 (file)
@@ -14,10 +14,7 @@ using Internal.Runtime.CompilerServices;
 namespace System.Runtime.CompilerServices
 {
     /// <summary>Provides an awaiter for a <see cref="ValueTask"/>.</summary>
-    public readonly struct ValueTaskAwaiter : ICriticalNotifyCompletion
-#if CORECLR
-            , IStateMachineBoxAwareAwaiter
-#endif
+    public readonly struct ValueTaskAwaiter : ICriticalNotifyCompletion, IStateMachineBoxAwareAwaiter
     {
         /// <summary>Shim used to invoke an <see cref="Action"/> passed as the state argument to a <see cref="Action{Object}"/>.</summary>
         internal static readonly Action<object> s_invokeActionDelegate = state =>
@@ -90,7 +87,6 @@ namespace System.Runtime.CompilerServices
             }
         }
 
-#if CORECLR
         void IStateMachineBoxAwareAwaiter.AwaitUnsafeOnCompleted(IAsyncStateMachineBox box)
         {
             object obj = _value._obj;
@@ -109,14 +105,10 @@ namespace System.Runtime.CompilerServices
                 TaskAwaiter.UnsafeOnCompletedInternal(Task.CompletedTask, box, continueOnCapturedContext: true);
             }
         }
-#endif
     }
 
     /// <summary>Provides an awaiter for a <see cref="ValueTask{TResult}"/>.</summary>
-    public readonly struct ValueTaskAwaiter<TResult> : ICriticalNotifyCompletion
-#if CORECLR
-            , IStateMachineBoxAwareAwaiter
-#endif
+    public readonly struct ValueTaskAwaiter<TResult> : ICriticalNotifyCompletion, IStateMachineBoxAwareAwaiter
     {
         /// <summary>The value being awaited.</summary>
         private readonly ValueTask<TResult> _value;
@@ -178,7 +170,6 @@ namespace System.Runtime.CompilerServices
             }
         }
 
-#if CORECLR
         void IStateMachineBoxAwareAwaiter.AwaitUnsafeOnCompleted(IAsyncStateMachineBox box)
         {
             object obj = _value._obj;
@@ -197,10 +188,8 @@ namespace System.Runtime.CompilerServices
                 TaskAwaiter.UnsafeOnCompletedInternal(Task.CompletedTask, box, continueOnCapturedContext: true);
             }
         }
-#endif
     }
 
-#if CORECLR
     /// <summary>Internal interface used to enable optimizations from <see cref="AsyncTaskMethodBuilder"/>.</summary>>
     internal interface IStateMachineBoxAwareAwaiter
     {
@@ -208,5 +197,4 @@ namespace System.Runtime.CompilerServices
         /// <param name="box">The box object.</param>
         void AwaitUnsafeOnCompleted(IAsyncStateMachineBox box);
     }
-#endif
 }
index 507ffac..f51aac6 100644 (file)
@@ -46,10 +46,7 @@ namespace System.Runtime.CompilerServices
 
         /// <summary>Provides an awaiter that switches into a target environment.</summary>
         /// <remarks>This type is intended for compiler use only.</remarks>
-        public readonly struct YieldAwaiter : ICriticalNotifyCompletion
-#if CORECLR
-            , IStateMachineBoxAwareAwaiter
-#endif
+        public readonly struct YieldAwaiter : ICriticalNotifyCompletion, IStateMachineBoxAwareAwaiter
         {
             /// <summary>Gets whether a yield is not required.</summary>
             /// <remarks>This property is intended for compiler user rather than use directly in code.</remarks>
@@ -118,7 +115,6 @@ namespace System.Runtime.CompilerServices
                 }
             }
 
-#if CORECLR
             void IStateMachineBoxAwareAwaiter.AwaitUnsafeOnCompleted(IAsyncStateMachineBox box)
             {
                 Debug.Assert(box != null);
@@ -151,7 +147,6 @@ namespace System.Runtime.CompilerServices
                     }
                 }
             }
-#endif
 
             private static Action OutputCorrelationEtwEvent(Action continuation)
             {