Separate ThreadPool...ThreadLocals get from initalization (dotnet/coreclr#21704)
authorBen Adams <thundercat@illyriad.co.uk>
Sun, 30 Dec 2018 03:06:16 +0000 (04:06 +0100)
committerStephen Toub <stoub@microsoft.com>
Sun, 30 Dec 2018 03:06:16 +0000 (22:06 -0500)
* Seperate ThreadPool...ThreadLocals get from initalization

* Assert null

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

src/coreclr/src/System.Private.CoreLib/src/System/Threading/ThreadPool.cs

index 2f77425..cc42b59 100644 (file)
@@ -402,9 +402,16 @@ namespace System.Threading
             loggingEnabled = FrameworkEventSource.Log.IsEnabled(EventLevel.Verbose, FrameworkEventSource.Keywords.ThreadPool | FrameworkEventSource.Keywords.ThreadTransfer);
         }
 
-        public ThreadPoolWorkQueueThreadLocals EnsureCurrentThreadHasQueue() =>
-            ThreadPoolWorkQueueThreadLocals.threadLocals ??
-            (ThreadPoolWorkQueueThreadLocals.threadLocals = new ThreadPoolWorkQueueThreadLocals(this));
+        public ThreadPoolWorkQueueThreadLocals GetOrCreateThreadLocals() =>
+            ThreadPoolWorkQueueThreadLocals.threadLocals ?? CreateThreadLocals();
+
+        [MethodImpl(MethodImplOptions.NoInlining)]
+        private ThreadPoolWorkQueueThreadLocals CreateThreadLocals()
+        {
+            Debug.Assert(ThreadPoolWorkQueueThreadLocals.threadLocals == null);
+
+            return (ThreadPoolWorkQueueThreadLocals.threadLocals = new ThreadPoolWorkQueueThreadLocals(this));
+        }
 
         internal void EnsureThreadRequested()
         {
@@ -545,7 +552,7 @@ namespace System.Threading
                 //
                 // Use operate on workQueue local to try block so it can be enregistered 
                 ThreadPoolWorkQueue workQueue = outerWorkQueue;
-                ThreadPoolWorkQueueThreadLocals tl = workQueue.EnsureCurrentThreadHasQueue();
+                ThreadPoolWorkQueueThreadLocals tl = workQueue.GetOrCreateThreadLocals();
                 Thread currentThread = tl.currentThread;
 
                 // Start on clean ExecutionContext and SynchronizationContext