support hardware_concurrency static method like c++ std::thread (also replaces cpu_count_get)

master
Daniel Kolesa 2016-01-26 18:55:35 +00:00
parent f0b4c61806
commit e5dc50b0dd
2 changed files with 24 additions and 23 deletions

View File

@ -6,6 +6,7 @@
#ifndef OSTD_PLATFORM_HH
#define OSTD_PLATFORM_HH
#include <stdlib.h>
#include <stdint.h>
#if defined(WIN32) || defined(_WIN32) || (defined(__WIN32) && !defined(__CYGWIN__))
@ -71,13 +72,6 @@
# endif
#endif
#ifndef OSTD_PLATFORM_WIN32
#include <unistd.h>
#else
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#ifdef OSTD_PLATFORM_WIN32
# ifdef OSTD_LIBRARY_DLL
# ifdef OSTD_TOOLCHAIN_GNU
@ -147,22 +141,6 @@ inline uint64_t endian_swap64(uint64_t x) {
#endif
inline int cpu_count_get() {
static int count = 0;
if (count <= 0) {
#ifdef OSTD_PLATFORM_WIN32
SYSTEM_INFO info;
GetSystemInfo(&info);
count = info.dwNumberOfProcessors;
#elif defined(_SC_NPROCESSORS_ONLN)
count = int(sysconf(_SC_NPROCESSORS_ONLN));
#endif
if (count <= 0)
count = 1;
}
return count;
}
}
#endif

View File

@ -9,6 +9,13 @@
#include <stdlib.h>
#include <pthread.h>
#ifndef OSTD_PLATFORM_WIN32
#include <unistd.h>
#else
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#include "ostd/memory.hh"
#include "ostd/platform.hh"
#include "ostd/type_traits.hh"
@ -158,6 +165,22 @@ struct Thread {
other.p_thread = cur;
}
static ostd::uint hardware_concurrency() {
static ostd::uint count = 0;
if (count <= 0) {
#ifdef OSTD_PLATFORM_WIN32
SYSTEM_INFO info;
GetSystemInfo(&info);
count = info.dwNumberOfProcessors;
#elif defined(_SC_NPROCESSORS_ONLN)
count = ostd::uint(sysconf(_SC_NPROCESSORS_ONLN));
#endif
if (count <= 0)
count = 1;
}
return count;
}
private:
pthread_t p_thread;
};