From 5f1fceaba36abfc1bc0902321ab293b27ee19258 Mon Sep 17 00:00:00 2001 From: savesanketsw Date: Tue, 6 Aug 2024 10:26:28 -0700 Subject: [PATCH] win32 default thread implementation --- common/common.cpp | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/common/common.cpp b/common/common.cpp index 2e8374d50..9ce666cab 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -111,7 +111,32 @@ int32_t cpu_get_num_physical_cores() { return num_physical_cores; } #elif defined(_WIN32) - //TODO: Implement + //returns number of physical processor on windows + unsigned int fallback_threads = std::thread::hardware_concurrency(); + DWORD length = 0; + GetLogicalProcessorInformationEx(RelationAll, nullptr, &length); + + if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { + std::cerr << "GLIPEx INSUFFICIENT_BUFFER" << std::endl; + return fallback_threads > 0 ? (fallback_threads <= 4 ? fallback_threads : fallback_threads / 2) : 4; + } + + std::vector buffer(length); + if (!GetLogicalProcessorInformationEx(RelationAll, reinterpret_cast(buffer.data()), &length)) { + std::cerr << "GLIPEx: Unable to get processor information" << std::endl; + return fallback_threads > 0 ? (fallback_threads <= 4 ? fallback_threads : fallback_threads / 2) : 4; + } + + DWORD physicalCoreCount = 0; + PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX info = reinterpret_cast(buffer.data()); + while (reinterpret_cast(info) < buffer.data() + buffer.size()) { + if (info->Relationship == RelationProcessorCore) { + physicalCoreCount++; + } + info = reinterpret_cast(reinterpret_cast(info) + info->Size); + } + return physicalCoreCount; + #endif unsigned int n_threads = std::thread::hardware_concurrency(); return n_threads > 0 ? (n_threads <= 4 ? n_threads : n_threads / 2) : 4;