Rework work-stealing queue list
authorStephen Toub <stoub@microsoft.com>
Thu, 2 Feb 2017 15:25:36 +0000 (10:25 -0500)
committerStephen Toub <stoub@microsoft.com>
Fri, 3 Feb 2017 14:25:11 +0000 (09:25 -0500)
commitfc32e18a5f3ab63d12916e370d36702869f5a6ac
tree5d35b4900a4ec50e70dc5e5a07c8abaf19c26b4c
parent24a05f1855b7ec576b460dfe6a919b53d452516e
Rework work-stealing queue list

The current data structure used to store the list of local work-stealing queues is maintains a sparse array, where entries can be null, and where removals null out entries in an active array that could be being read by another thread concurrently.  This necessitates that threads looking for work need to use volatile reads and null checks on each element.  Further, because the array doubles in size when it grows, and never shrinks, we often end up having many empty slots that threads need to look at as they're looking for work.

It's actually relatively rare for threads to come and go.  While the thread pool does take threads in and out of service, it only rarely actually terminates a thread or asks the OS for a new one, so it's relatively rare that threads are added/removed from the list.  Given that, we can simply use immutable arrays, creating a new array of the exact right size whenever a thread is added or removed.  Then iteration can be done without volatile reads and without null checks, because the contents of the array being read through won't change and won't ever be null.

Commit migrated from https://github.com/dotnet/coreclr/commit/71bc68eced44a188ef51678b1d9c7337051cf3c3
src/coreclr/src/mscorlib/src/System/Threading/ThreadPool.cs