From a29a2af943185eca1464589406799cafbd3026e7 Mon Sep 17 00:00:00 2001 From: Jia Liu Date: Thu, 8 Aug 2024 19:17:47 +0800 Subject: [PATCH] Add error checking to return default value --- common/common.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/common/common.cpp b/common/common.cpp index 7f5ac0934..3edaee9d2 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -112,11 +112,19 @@ int32_t cpu_get_num_physical_cores() { } #elif defined(_WIN32) && (_WIN32_WINNT >= 0x0601) && !defined(__MINGW64__) // windows 7 and later // TODO: windows + arm64 + mingw64 + unsigned int n_threads_win = std::thread::hardware_concurrency(); + unsigned int default_threads = n_threads_win > 0 ? (n_threads_win <= 4 ? n_threads_win : n_threads_win / 2) : 4; + DWORD buffer_size = 0; - GetLogicalProcessorInformationEx(RelationProcessorCore, nullptr, &buffer_size); + if (!GetLogicalProcessorInformationEx(RelationProcessorCore, nullptr, &buffer_size)) { + if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { + return default_threads; + } + } + std::vector buffer(buffer_size); if (!GetLogicalProcessorInformationEx(RelationProcessorCore, reinterpret_cast(buffer.data()), &buffer_size)) { - return 0; + return default_threads; } int32_t num_physical_cores = 0; @@ -129,7 +137,7 @@ int32_t cpu_get_num_physical_cores() { info = reinterpret_cast(reinterpret_cast(info) + info->Size); } - return num_physical_cores; + return num_physical_cores > 0 ? num_physical_cores : default_threads; #endif unsigned int n_threads = std::thread::hardware_concurrency(); return n_threads > 0 ? (n_threads <= 4 ? n_threads : n_threads / 2) : 4;