[libcxx-commits] [libcxx] [libc++] Fixed get count threads multi-CPU system with NUMA architecture (#72267) (PR #72270)

Martin Storsjö via libcxx-commits libcxx-commits at lists.llvm.org
Wed Nov 15 13:06:08 PST 2023


================
@@ -81,9 +81,58 @@ thread::hardware_concurrency() noexcept
         return 0;
     return static_cast<unsigned>(result);
 #elif defined(_LIBCPP_WIN32API)
-    SYSTEM_INFO info;
-    GetSystemInfo(&info);
-    return info.dwNumberOfProcessors;
+    // This implementation supports both conventional single-cpu PC configurations
+    // and multi-cpu system on NUMA (Non-uniform_memory_access) architecture
+    DWORD length = 0;
+    unsigned concurrency = 0;
+    const auto validConcurrency = [&concurrency]() noexcept -> unsigned
+    {
+        if (concurrency == 0)
+        {
+            SYSTEM_INFO info;
+            GetSystemInfo(&info);
+            return info.dwNumberOfProcessors;
+        }
+        else
+        {
+            return concurrency;
+        }
+    };
+    if (GetLogicalProcessorInformationEx(RelationAll, nullptr, &length) != FALSE)
----------------
mstorsjo wrote:

I'm not really familiar with this one, but I think @aganea wrote some code that uses these same APIs in LLVMSupport a few years ago, hopefully he can have a look.

I checked that this new function is available since Windows 7, and that's the lowest OS version that libc++ supports anyway, so it should be fine to use it without any conditionals.

https://github.com/llvm/llvm-project/pull/72270


More information about the libcxx-commits mailing list