gthread: Better fallback for W32 g_get_num_processors()

https://bugzilla.gnome.org/show_bug.cgi?id=748530
This commit is contained in:
Руслан Ижбулатов 2015-04-27 14:48:10 +00:00
parent 75589956a4
commit 999711abc8

View File

@ -1008,21 +1008,31 @@ guint
g_get_num_processors (void) g_get_num_processors (void)
{ {
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
unsigned int count;
SYSTEM_INFO sysinfo;
DWORD_PTR process_cpus; DWORD_PTR process_cpus;
DWORD_PTR system_cpus; DWORD_PTR system_cpus;
/* This *never* fails, use it as fallback */
GetNativeSystemInfo (&sysinfo);
count = (int) sysinfo.dwNumberOfProcessors;
if (GetProcessAffinityMask (GetCurrentProcess (), if (GetProcessAffinityMask (GetCurrentProcess (),
&process_cpus, &system_cpus)) &process_cpus, &system_cpus))
{ {
unsigned int count; unsigned int af_count;
for (count = 0; process_cpus != 0; process_cpus >>= 1) for (af_count = 0; process_cpus != 0; process_cpus >>= 1)
if (process_cpus & 1) if (process_cpus & 1)
count++; af_count++;
if (count > 0) /* Prefer affinity-based result, if available */
return count; if (af_count > 0)
count = af_count;
} }
if (count > 0)
return count;
#elif defined(_SC_NPROCESSORS_ONLN) #elif defined(_SC_NPROCESSORS_ONLN)
{ {
int count; int count;