BSD compile fixes

- bitmsghash should now build and run on BSD (thanks for
  FreeBSD/Dragonfly maintainers for assistance)
- if it cannot detect the number of cores, will default to one thread
  (previously it broke)
This commit is contained in:
Peter Šurda 2016-05-24 09:42:49 +02:00
parent e353af3195
commit 9e2389306b
Signed by untrusted user: PeterSurda
GPG Key ID: 0C5F50C0B5F37D87

View File

@ -11,7 +11,7 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#ifdef __APPLE__ #if defined(__APPLE__) || defined(__FreeBSD__) || defined (__DragonFly__) || defined (__OpenBSD__) || defined (__NetBSD__)
#include <sys/types.h> #include <sys/types.h>
#include <sys/sysctl.h> #include <sys/sysctl.h>
#endif #endif
@ -89,18 +89,20 @@ void getnumthreads()
#else #else
if (sysctlbyname("hw.logicalcpu", &core_count, &len, 0, 0) == 0) if (sysctlbyname("hw.logicalcpu", &core_count, &len, 0, 0) == 0)
numthreads = core_count; numthreads = core_count;
else if (sysctlbyname("hw.ncpu", &core_count, &len, 0, 0) == 0)
numthreads = core_count;
#endif #endif
for (unsigned int i = 0; i < len * 8; i++) for (unsigned int i = 0; i < len * 8; i++)
#if defined(_WIN32) #if defined(_WIN32)
if (dwProcessAffinity & (1i64 << i)) { if (dwProcessAffinity & (1i64 << i))
#elif defined __linux__ #elif defined __linux__
if (CPU_ISSET(i, &dwProcessAffinity)) { if (CPU_ISSET(i, &dwProcessAffinity))
#else #else
if (dwProcessAffinity & (1 << i)) { if (dwProcessAffinity & (1 << i))
#endif #endif
numthreads++; numthreads++;
printf("Detected core on: %u\n", i); if (numthreads == 0) // something failed
} numthreads = 1;
printf("Number of threads: %i\n", (int)numthreads); printf("Number of threads: %i\n", (int)numthreads);
} }