Remove lazy initialization of Task.CompletedTask (dotnet/coreclr#8846)
authorStephen Toub <stoub@microsoft.com>
Sun, 8 Jan 2017 18:25:17 +0000 (13:25 -0500)
committerGitHub <noreply@github.com>
Sun, 8 Jan 2017 18:25:17 +0000 (13:25 -0500)
Remove lazy initialization of Task.CompletedTask

Commit migrated from https://github.com/dotnet/coreclr/commit/c58686343b1ff49473df8c962ff30eb91025a179

src/coreclr/src/mscorlib/src/System/Threading/Tasks/Task.cs

index 24a57fa..b385f06 100644 (file)
@@ -147,7 +147,6 @@ namespace System.Threading.Tasks
         private static StackGuard t_stackGuard;  // The stack guard object for this thread
 
         internal static int s_taskIdCounter; //static counter used to generate unique task IDs
-        private readonly static TaskFactory s_factory = new TaskFactory();
 
         private volatile int m_taskId; // this task's unique ID. initialized only if it is ever requested
 
@@ -1615,23 +1614,10 @@ namespace System.Threading.Tasks
         /// of <see cref="System.Threading.Tasks.TaskFactory"/>, as would result from using
         /// the default constructor on TaskFactory.
         /// </remarks>
-        public static TaskFactory Factory { get { return s_factory; } }
-
-        /// <summary>A task that's already been completed successfully.</summary>
-        private static Task s_completedTask;
+        public static TaskFactory Factory { get; } = new TaskFactory();
 
         /// <summary>Gets a task that's already been completed successfully.</summary>
-        /// <remarks>May not always return the same instance.</remarks>        
-        public static Task CompletedTask
-        {
-            get
-            {
-                var completedTask = s_completedTask;
-                if (completedTask == null)
-                    s_completedTask = completedTask = new Task(false, (TaskCreationOptions)InternalTaskOptions.DoNotDispose, default(CancellationToken)); // benign initialization race condition
-                return completedTask;
-            }
-        }
+        public static Task CompletedTask { get; } = new Task(false, (TaskCreationOptions)InternalTaskOptions.DoNotDispose, default(CancellationToken));
 
         /// <summary>
         /// Provides an event that can be used to wait for completion.