From 1799c52320fa19f1ce04e1cb417ab70e8923df22 Mon Sep 17 00:00:00 2001 From: Koundinya Veluri Date: Thu, 8 Feb 2024 10:15:04 -0800 Subject: [PATCH] Fix usage of recycled lists in the thread pool Fixed the usage of ThreadpoolMgr::RecycledLists to use the same processor count upon access as what is used to allocate the array. --- src/coreclr/vm/win32threadpool.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/coreclr/vm/win32threadpool.h b/src/coreclr/vm/win32threadpool.h index 20aeccce7f4e5d..77f245c7fd998f 100644 --- a/src/coreclr/vm/win32threadpool.h +++ b/src/coreclr/vm/win32threadpool.h @@ -752,17 +752,23 @@ class ThreadpoolMgr #ifndef TARGET_UNIX if (CPUGroupInfo::CanEnableThreadUseAllCpuGroups()) - processorNumber = CPUGroupInfo::CalculateCurrentProcessorNumber(); + { + // The current processor number may not be within the total number of active processors determined at + // initialization time. + processorNumber = CPUGroupInfo::CalculateCurrentProcessorNumber() % CPUGroupInfo::GetNumActiveProcessors(); + } else + { // Turns out GetCurrentProcessorNumber can return a value greater than the number of processors reported by // GetSystemInfo, if we're running in WOW64 on a machine with >32 processors. - processorNumber = GetCurrentProcessorNumber()%NumberOfProcessors; + processorNumber = GetCurrentProcessorNumber() % g_SystemInfo.dwNumberOfProcessors; + } #else // !TARGET_UNIX if (PAL_HasGetCurrentProcessorNumber()) { // On linux, GetCurrentProcessorNumber which uses sched_getcpu() can return a value greater than the number // of processors reported by sysconf(_SC_NPROCESSORS_ONLN) when using OpenVZ kernel. - processorNumber = GetCurrentProcessorNumber()%NumberOfProcessors; + processorNumber = GetCurrentProcessorNumber() % PAL_GetTotalCpuCount(); } #endif // !TARGET_UNIX return pRecycledListPerProcessor[processorNumber][memType];