Skip to content

Commit 73adde2

Browse files
committed
Fixes #30581 by adding a path to use newer GetMaximumProcessorCount API on Windows calls to os.cpu_count()
1 parent b4e5fee commit 73adde2

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

Modules/posixmodule.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11128,9 +11128,22 @@ os_cpu_count_impl(PyObject *module)
1112811128
{
1112911129
int ncpu = 0;
1113011130
#ifdef MS_WINDOWS
11131-
SYSTEM_INFO sysinfo;
11132-
GetSystemInfo(&sysinfo);
11133-
ncpu = sysinfo.dwNumberOfProcessors;
11131+
/* Vista is supported and the GetMaximumProcessorCount API is Win7+
11132+
Need to fallback to Vista behavior if this call isn't present */
11133+
HINSTANCE hKernel32;
11134+
hKernel32 = GetModuleHandleW(L"KERNEL32");
11135+
11136+
static DWORD(CALLBACK *_GetMaximumProcessorCount)(WORD) = NULL;
11137+
*(FARPROC*)&_GetMaximumProcessorCount = GetProcAddress(hKernel32,
11138+
"GetMaximumProcessorCount");
11139+
if (_GetMaximumProcessorCount != NULL) {
11140+
ncpu = _GetMaximumProcessorCount(ALL_PROCESSOR_GROUPS);
11141+
}
11142+
else {
11143+
SYSTEM_INFO sysinfo;
11144+
GetSystemInfo(&sysinfo);
11145+
ncpu = sysinfo.dwNumberOfProcessors;
11146+
}
1113411147
#elif defined(__hpux)
1113511148
ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
1113611149
#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)

0 commit comments

Comments
 (0)